r/linuxadmin • u/Silva-Engineer • 27d ago
How do you keep NOPASSWD sudo rules for automation narrow?
In sudoers, a * in the arguments matches spaces too, so /bin/cat /var/log/messages* also allows cat /var/log/messages /etc/shadow. When several rules match, the last one wins, even if an earlier one is more specific. Do you list exact command lines, wrap each task in a root-owned script with no arguments, or use a systemd unit instead of sudo?
33
Upvotes
43
u/rdrcrmatt 27d ago
ALL:ALL
Yolo
19
u/Hotshot55 27d ago
If users need access to specific files, just give them read permissions to the file with FACLs instead of messing with sudo rules.
6
u/Line-Noise 27d ago
Write a wrapper script that does exactly what you want and nothing else. Then you allow that script to be run with sudo, not the individual shell commands.
5
u/ericpruitt 27d ago
Recent versions of sudo support regular expressions: https://www.sudo.ws/posts/2022/03/sudo-1.9.10-using-regular-expressions-in-the-sudoers-file/ . On systems where that wasn't an option because of the complexity of the commands, I would make wrapper programs that act as relays to the underlying commands, but that comes with its own risks. Your wrapper could have logic errors, the binary might have errant permission handling/delegation, etc. Scripting languages like Python might import files from the user's home directory which could lead to user being able to execute code with elevant privileges.
5
u/whetu 27d ago
It depends. If it's reading a file, then as another commenter said: ACL's.
sudoershas supported regex since v1.9.10, so you could do something likeRather than
It's literally a textbook example
In older versions of
sudo, a common approach I used to see was an immediate negation, here's an example from my code attic:That's not perfect (it excludes files with spaces in their names, for example) but it goes a long way to closing the obvious hole.
3
u/michaelpaoli 27d ago
And yes, very carefully restrict and review, unless you really want to grant unrestricted access to the target group(s)/ID(s).
And yes, have done lots of reviews of sudo configurations and the like ... and oft found holes that gave access much more than intended. Random example that jumps to mind. Wanted to see if could get unrestricted root from account. Checked sudo ... it had sudo access to unrestricted shell of another account. Checked sudo on that account, it had various things, ... including among them being able to run find(1) as root, with arbitrary arguments - yeah, easy peasy, I'm in - root. Hint, e.g.:
sudo find / -type d -exec /bin/sh \; -prune
Yeah, far too often folks miss, e.g. insecure edit access, things that offer shell escape, exec, reading or writing files that can escalate privilege, etc. E.g tcpdump has -w option, strace has -o option, etc. - often just clobbering the right critical file is sufficient, precise data written might not much matter. Yeah, typically about half the sudoers files or equivalent I'd review, I'd find available exploits in them.
2
u/jippen 27d ago
I usually avoid the problem. Ship logs centrally and you don’t need Audi to deal with them. Containerize to step around most root needs. Configuration management tooling like ansible if you need to change something as root bare metal. Centralized telemetry to prevent needing root to check any given setting.
If you design around ssh and sudo being break glass only, a lot of these concerns can just vanish
2
u/LeBronzelol 24d ago
Root owned scripts seem like the safest way to go here
1
u/kolorcuk 27d ago
So don't use * and make them narrow.
Ai are very good at generating sudo rules. Ask it for your cat, it will give you exact regex
1
u/roiki11 27d ago
Automation can do elevation though?
1
u/overlord_tm 26d ago
Xxl
1
u/that1guyrob 25d ago
In some rare instances I'll put the commands into a script and then specify that exact script in the sudoers file without any arguments. Obviously, you'll just need to be sure that the script itself has exclusive permissions to ensure there isn't any unauthorized modification of the script.
1
u/slash8 23d ago
Root owned executables are probably the least risky.
Scripts are second only because a wring perm on a binary the script runs could be exploitable.
Either way should be safe.
1
u/michaelpaoli 27d ago
You make them as restrictive and narrow as feasible, directly or indirectly.
And when sudo doesn't have the needed granularity, can, e.g., hand off to a specialized (wrapper) program or script to do the needed.
Let's see, example I have that more-or-less jumps to mind ...
https://www.mpaoli.net/~mycert/bin/_acme-challenge_helper.sudoers
And that uses
https://www.mpaoli.net/~mycert/bin/_acme-challenge_helper
Which validates arguments, etc. quite particularly. The first part would be easy enough, takes a single argument of add, del, or delete. But the next bits more complex, it uses/considers 3 environment variables. It fully validates them, such as by precisely checking against regular expression matches:
So, might be able to shoehorn quite a bit into sudoers configuration, but oft better to push at least some of that further along or down the chain .. but well, carefully, and securely, when doing so.
Could even coerce such things into sudoers configuration, but then one would also typically be pushing much of that complexity onto the invoker, e.g.: