We have a legacy C code used to allow less privileged user to run custom scripts with escalated privilege. This has SUID bit set. This code restricts the PATH env to a specific folder and then uses
system() api to execute the script with restricted shell :
/bin/bash -r -c "script <arg>"
As the path is restricted, it can execute only scripts from that specific folder.
Now knowing all the pitfalls for command injection with
system() api, what measures can be taken to avoid command injection? This is used in many places in various scripts etc, so don't want to do a completely new implementation to avoid any regression.