-1

I am making a c program to shutdown Ubuntu without system call using following link.

code.c
#include <unistd.h>
#include <sys/reboot.h>
int main() {
reboot(RB_POWER_OFF);
}
gcc code.c -o out.exe

Here if I run this executable file(out.exe) as root user then only it will shutdown system and if I run same file in normal user mode it is not working.So what changes I have to do in my code in order to run this code in normal user mode?

raj123
  • 564
  • 2
  • 10
  • 27

2 Answers2

3

For a large number of reasons, it is better to power off the machine using e.g.

execl("/bin/shutdown", "shutdown", "-P", "now", (char *)0);

or reboot using

execl("/bin/shutdown", "shutdown", "-r", "now", (char *)0);

There is no shell involved at all. The current process is replaced by the shutdown system management command, which already has the necessary privileges if the user the process is running has is allowed to shutdown or reboot the machine.

(That is, code following the above statement is not executed at all, except if the system utility is missing. It will be there on any functioning system, even embedded ones.)

You can even replace the "now" with a fixed string like "+1m", to shutdown or reboot after one minute has elapsed. (During that time, everything else will continue running normally. Running shutdown with just a -c parameter during that period will cancel the pending shutdown/reboot.)

If you do this from a GUI application, only do it where a normal program would either return from main(), or exit().


What are those reasons?

Simplicity, robustness, the principle of least surprise, proper management of privileges for shutdown/reboot, not requiring special privileges and thus reduced threat surface (bugs in your program less likely to grant special privileges, because there are none). For starters.

Nominal Animal
  • 38,216
  • 5
  • 59
  • 86
  • @raj123: Yes. The only risk here is an attacker being able to shut down the system remotely, by exploiting some bug or security hole elsewhere in your application (causing that line of the code to be run unexpectedly) -- assuming your application communicates over the network, or the machines have untrusted users who can execute your application. To avoid that risk, you just need to sanitize/check inputs, and consider what it allows untrusted users to do. – Nominal Animal Nov 29 '18 at 05:59
0

If the code runs as a non-root user, it doesn't have permission to reboot the system.

The simplest thing to do is make the executable set-uid root:

sudo chown root:root out.exe
sudo chmod 4755 out.exe

Then any user can run it, and it will run with root permissions.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • But see [Exploiting SUID Executables](https://www.pentestpartners.com/security-blog/exploiting-suid-executables/) – David C. Rankin Nov 29 '18 at 03:53
  • C file API is calling by another file so I have to set root privilege inside C function NOT EXECUTABLE BINARY FILE. @dbush – raj123 Nov 30 '18 at 02:16