bnoeafk
bnoeafk

Reputation: 539

C program using reboot with u+s permissions

... I'm new to C programming on Ubuntu, so please bear with me if i'm being too much of a noob.

I have a C file that when compiled, is allocated to a particular user (testUser) and is run as they log in as their shell. The user doesn't have sudo rights to the system in question. Basically this shell permits the user to update a file (/var/wwww/testfile) upon login and then reboots the system. Of course, it's the reboot which is giving me some issues, as they don't have superuser rights.

//file: testShell.c
#include <unistd.h>
//#include <linux/reboot.h>

int main(void)
{
  execl("/usr/bin/nano", "nano", "/var/www/testfile", NULL);
  execl("/usr/bin/shutdown", "shutdown", "-h 0", NULL);
  //reboot(LINUX_REBOOT_CMD_RESTART);
  return 0;
}
  1. The file compiles just fine to testShell
  2. I chown root:root testShell
  3. Grant SetUID using chmod u+s testShell
  4. Copy the file cp testShell /bin
  5. Update the users account to use the shell chsh -s /bin/testShell testUser

I've read the man pages on shutdown and tried within the program itself using reboot (you can see in this particular version of the file, I've commented out the header file and call) but I still can't get this user to be able to reboot the system (Ubuntu 12.04 presently). I've even tried the "init 6" system call that was posted here, but all to no avail. I've also read that using the system() call isn't a particularly good idea: I've tried it none-the-less and still no joy.

It was my understanding that if I allocate the permissions correctly and then SetUID the file, anyone running that file would implicitly be running it under the owners rights, root in this case. In fact, the /var/www/testfile that the testUser is updating, is owned by root, so something's working correctly.

Any ideas where I'm going wrong?

Upvotes: 2

Views: 408

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149075

It is really simple : you use directly execl to start nano and ... never return from it if it correctly works !

You should use a fork - exec- wait.

You will find a complete example on this other post from SO https://stackoverflow.com/a/19099707/3545273

Upvotes: 3

Related Questions