The sudoers
manual also has a whole section how a proper policy in the /etc/sudoers
configuration file (or of course the included drop-in policy files) can offer limited protection against shell escapes with the RESRICT
and NOEXEC
options.
That might be more generic than creating a wrapper script for a particular utility.
restrict
Avoid giving users access to commands that allow the user to run arbitrary commands. Many editors have a restricted mode where shell escapes are disabled, though sudoedit is a better solution to running editors via sudo. Due to the large number of programs that offer shell escapes, restricting users to the set of programs that do not is often unworkable.
and
noexec
Many systems that support shared libraries have the ability to override default library functions by pointing an environment variable (usually LD_PRELOAD) to an alternate shared library. On such systems, sudo's noexec
functionality can be used to prevent a program run by sudo from executing any other programs. Note, ... ...
To enable noexec
for a command, use the NOEXEC
tag as documented in the User Specification section above. Here is that example again:
aaron shanty = NOEXEC: /usr/bin/more, /usr/bin/vi
This allows user aaron to run /usr/bin/more
and /usr/bin/vi
with noexec enabled. This will prevent those two commands from executing other commands (such as a shell).
So in your example:
john ALL=NOEXEC:NOPASSWD: /usr/bin/apt,/usr/bin/apt-get
should be enough to still allow user john
to run apt
and apt-get
with elevated privileges, without being prompted for his password and without having the ability to use a shell escape or other trick exceeds the limits of his authority.
Note that doing so may have unintended results as well. For example
after installing a new service apt may not be able to start the associated service.