I have a problem whereby an executable which has the setuid bit set and is owned by root is not having its euid set to 0 sometimes.
I'm running Debian 11 on a small SBC for a control application. I have a systemd service which runs a bash script which runs an executable. The systemd unit file looks like this
[Unit] Description=Runs fdil_run After=network-online.target StartLimitIntervalSec=108000 StartLimitBurst=12 StartLimitAction=reboot [Service] Type=simple User=nxfence ExecStart=/bin/bash /usr/local/bin/fdil_run Restart=always RestartSec=3600 ExecStartPre=/bin/sleep 10 [Install] WantedBy=multi-user.target and the bash script looks like this
#!/bin/bash fdil As you can see, all the bash script does is run the executable (it's set up like this for historical reasons). The setuid bit is set on the executable -rwsr-xr-x 1 root root. At the start of the executable I have
ruid = getuid (); euid = geteuid (); and a statement to write these values to the system logs so that I can check the values are set correctly.
Most of the time, this set up works fine and checking the logs give ruid = 1000 and euid = 0. Occasionally, however (twice in perhaps the last 30 system boots) the setuid bit is somehow not recognised and inspecting the logs shows ruid = 1000 and euid = 1000. Needless to say, when this happens the code encounters a series of Permission denied errors and hence fails.
Why does it work well most of the time, but fail occasionally? I can only think that it is something to do with systemd needing to reach a particular target before starting the executable in order for the setuid bit to be recognised, but I can find nothing online to suggest this is the case.
One obvious thing would be to get rid of the bash script (since it's not doing anything useful) and run the executable straight from the unit file, but verifying that this has solved the problem would be very difficult because the problem is intermittent. I'd like to know what the underlying cause of the problem is, so that I can know it is solved rather than just trying things at random.
#!shebang line) or it's on a filesystem mounted withnosuid. Neither of these would explain the intermittent nature of this problem, unless you're mounting the filesystem withfdildifferently.User=rootin the unit file?If euid != 0record debug info check/etc/mtabfor mounts with thenosuidorusersoptions. (Explore your Mount/Read/Write/Execute problems withhttps://github.com/waltinator/pathlld, abashscript to show the permissions, mount options along the path to an object or objects.) andexit 1, and have a wrapper script retry on error.if euid == 0invoke the program.