I am trying to create a shutdown command that can be run as a user, i.c., a flask web page that is not running as root. Sounds simple, just put a shutdown
command in a SETUID script. Because SETUID does not work on shell scripts, I created an executable from a C program.
The problem is that this does not work on the target machine, a Raspberry Pi Zero W. I tested the same stuff on my Ubuntu 20.4 pc, and there it runs flawlessly. So the method in itself seems correct, but there is a raspberry pi issue.
The Pi runs this OS:
cat /etc/issue
-->
Raspbian GNU/Linux 10 \n \l
This is usershutdown.c :
#include <stdio.h>
#include <stdlib.h>
int main(){
system("/sbin/poweroff");
}
These are the permissions of the executable:
-rwsr-xr-x 1 root root 7988 Dec 20 23:59 usershutdown
I checked the mount options of the root disk in /etc/fstab, where I added ,suid
and rebooted:
PARTUUID=738a4d67-02 / ext4 defaults,noatime,suid 0 1
And these are the error messages on the Pi when calling the exec as intended:
$ ./usershutdown
Failed to set wall message, ignoring: Interactive authentication required.
Failed to power off system via logind: Interactive authentication required.
Failed to open initctl fifo: Permission denied
Failed to talk to init daemon.
$
This is what does work on the Pi, when calling the exec as root/sudo, the ssh connection to it is closed and the device shuts down without error:
$ sudo ./usershutdown
$ Connection to picamhq closed by remote host.
Connection to picamhq closed.
$
How do I fix this?