Why does setuid root not work with the system() function?
4
votes
2
answers
5475
views
**Objective** : Run a program as root (C++ binary).
The same as : https://unix.stackexchange.com/questions/359598/setuid-bit-not-working-in-ubuntu
And : https://unix.stackexchange.com/questions/150972/why-setuid-does-not-work-on-executable
./a.out output:
`E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
psurana //output for "whoami" Look below for the code.
`
ls -l output:
-rwsrwxr-x 1 root root 46136 Jun 7 20:13 a.out
The Code :
#include
#include
int main(int argc, char *argv[]){
std::string input = "apt-get install " + std::string(argv);
system(input.c_str());
system("whoami");
return 0;
}
**Details:** : compiled the program and then did chown root:root a.out && chmod u+s a.out
. Please look above for ls -l output.
I still do not get the root privileges and the output for system("whoami") in the code is my own username on the machine.
Reading the two linked questions did not yield me anywhere. :(.
both the creator and the owner of the file are root. The setuid bit is set, so it should work. The filesystem is not external either, it is my own machine. How can I make this work?
Asked by Pranay
(981 rep)
Jun 8, 2017, 03:31 AM
Last activity: Aug 27, 2024, 05:50 PM
Last activity: Aug 27, 2024, 05:50 PM