When executing a ksh script under sudo and sudo has a line to verify the checksum, then the $0 is reported as /usr/bin/ksh instead of script being interpreted:
ksh script /usr/local/bin/script.ksh
#!/usr/bin/ksh
echo "\$0: $0"
configure sudo to verify the checksum
# echo "kshuser ALL=NOPASSWD: sha256:$(sha256sum /usr/local/bin/script.ksh)" > /etc/sudoers.d/kshuser
execute the script under sudo
(kshuser) $ sudo /usr/local/bin/script.ksh $0: /usr/bin/ksh
The root cause is likely due to having a execveat() being used by sudo to execute the script, instead of "regular" execve(), as seen with stracing:
5129 11:25:59.445250 execve("/usr/bin/sudo", ["sudo", "/usr/local/bin/script.ksh"], ... : 5131 11:25:59.489322 execveat(6</usr/local/bin/script.ksh>, "", ["/usr/local/bin/script.ksh"], ..., AT_EMPTY_PATH) = 0
The execveat() is done on purpose to make sure the script was not altered.
Due to this bug, it's impossible to know which script is currently executing.
- links to