Shell – Allow users to run only specific binaries with root permissions/privileges

permissionsshell-scriptsudo

I would like to allow a specific user to be able to sudo /sbin/iptables only.

I have a bash script which configures iptables. The problem is that configuring /sbin/iptables as sudoable is not enough – The script itself also has to be sudoable.

The problem is that that file could be edited by the user (he may change configurations there), giving him the ability to run any command as root.

What is the proper approach to this problem?

Best Answer

I can see two methods:

  1. Allow the user to use /sbin/iptables through sudo without restriction (which is something dangerous, this means you trust the user somehow), and run the script with the permissions of the user. The script will invoke sudo each time /sbin/iptables is needed. This is assuming the script execution will be quick, since some password input will eventually be required at regular intervals¹.

    • Advantage: you don't need to have any trust in the script.
    • Disadvantage: as already mentioned, allowing the user to use /sbin/iptables without restriction is something dangerous.
  2. Allow the user to call only the script through sudo.

    • Advantage: the use of /sbin/iptables is restricted by the script.
    • Disadvantage: the script must be flawless.

About the problem you mentioned: if the script is owned by let's say root and has usual permissions: rwxr-xr-x, others users cannot modify it, they can only execute it (eventually through sudo to obtain more privileges).

With solution 2, and in the case of shell scripts (compared to more robust binaries/programs), beware of environment variables and the several external factors that can modify the execution of your script. Check that your sudo configuration resets properly the definition of every potentially harmful variable.


1. In fact, sudo can be configured with NOPASSWD if needed.

Related Question