r/linuxadmin • • 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

30 comments sorted by

43

u/rdrcrmatt 27d ago

ALL:ALL

Yolo

9

u/hvindin 27d ago

But seriously, sudo rules for automation as ALL:ALL is fine.

Automation runs under its own account managed by the same automation. Test the automation outside prod.

1

u/Queasy_Feedback_6785 26d ago

Wait, so every job that uses that automation account has full root access? Because I'm just wondering how you would separate out a read only monitoring job from the jobs that have to actually change the host

3

u/hvindin 26d ago

Jobs that need full root access use that account.

Jobs that need other access use other accounts with the correct permissions.

My point is that if you are building a new host and running provisioning logic, trying to limit down the access is silly, the automation that builds the host owns the host. It will define the correct state of the host and permissions shouldn't get in the way.

How do you secure those privileged credentials and ensure they are only used for this automation? Different problem.

4

u/Irythros 27d ago

Also going with ALL:ALL

If it does something bad, rebuild. Don't be treating the server as a pet.

1

u/ibz096 24d ago

How do you do that when you have no control of the app deployment? We just build the server/vm and hand it off to the contractors to deploy.

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.

2

u/sobrique 25d ago

And plenty of commands work just as well if not better with setcap instead of sudo.

1

u/Bearchlld 27d ago

I would go this route as well but not sure if it's best practice. As long as you pair it with a proper "log-read" security group I think it should be fine.

Maybe for the sudo solution you could do both "/bin/cat /var/log/messages*.xz" (or .1 or whatever your log rollover is setup for) and "/bin/cat /var/log/messages"

6

u/ABCDwp 27d ago

The first one of those matches sudo /bin/cat /var/log/messages /etc/shadow /tmp/empty.xz, which clearly isn't what you want.

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.

1

u/RIPenemie 25d ago

How would you handle stuff like ansible where you don't necessarily know the commands being run?

1

u/sobrique 25d ago

You don't.

There's no way to safely permit arbitrary code to run as root.

If you cannot define - robustly - what should and shouldn't work with elevated privileges, you cannot control it.

The same applies for anything using AI tools for much the same reason.

1

u/denverpilot 21d ago

Nothing should be arbitrary about an Ansible playbook.

You’re moving that trust to your playbook publishing procedures and accessibility to running those playbooks when you’re using something like Ansible.

But you’d better know what’s being run in those playbooks.

5

u/ericpruitt 27d ago

In sudoers, a * in the arguments matches spaces too

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.

sudoers has supported regex since v1.9.10, so you could do something like

/bin/cat ^/var/log/messages[A-Za-z0-9_.-]+$

Rather than

/bin/cat /var/log/messages*

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:

SYBASEDBA_GROUP SYBASEDBA_HOSTS=(ALL) /bin/chmod 660 /sybase/dumps/PAYROLL/*
SYBASEDBA_GROUP SYBASEDBA_HOSTS=(ALL) !/bin/chmod 660 /sybase/dumps/PAYROLL/*[ ]* 

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.

2

u/sobrique 25d ago

I would argue that just altering the permissions on /var/log/messages at that point would be safer though.

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.

8

u/BarServer 27d ago

Server of a friend got pwned recently. He had the following sudo-rule to allow a self-written webinterface to update packages:

www-data ALL=(ALL) NOPASSWD: /usr/bin/apt-get

Yeah... Turns out

sudo apt-get update -o APT::Update::Pre-Invoke::=/bin/sh

gives you a rootshell too.

2

u/michaelpaoli 26d ago

Yup, easy peasy. I use Post-Invoke/Pre-Invoke all the time, though not directly on CLI:

$ cat /etc/apt/apt.conf.d/15local-remount
DPkg
{
   Pre-Invoke {"mount -o remount,rw /usr || :; mount -o remount,rw /boot; mount -o remount,rw /boot/efi;";};
   Post-Invoke {"mount -o remount,ro /boot/efi; mount -o remount,ro /boot; mount -o remount,ro /usr || :;";};
}
$ 

So, yeah, fail to specify the command argument(s) to sudo, that leaves it open to arbitrary arguments and options. "Oops".

2

u/sobrique 25d ago

Yup. No shortage of those.

Sudo is fine if you can be extremely specific about what is allowed.

But you have to be extremely cagey about any user input, to any non trivial command.

Wrapper scripts can do some of the work too, as long as they have pretty strict parsing of any arguments.

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/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

Defaults:mycert env_check="CERTBOT_DOMAIN CERTBOT_TOKEN CERTBOT_VALIDATION"

mycert ALL=(:bind) NOPASSWD: /usr/local/bin/_acme-challenge_helper
mycert ALL=(bind) NOPASSWD: /usr/local/bin/_acme-challenge_helper
mycert ALL=(root) NOPASSWD: /usr/local/bin/_acme-challenge_helper

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:

re='[-.0-9A-Z_a-z]\{87\}$'
expr x"$CERTBOT_VALIDATION" : "x$re" >>/dev/null \
||
{
echo "$my_prog_basename: invalid value:" \
"CERTBOT_VALIDATION=$CERTBOT_VALIDATION" 1>&2
exit 1
}

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.:

# grep -v '^#' /etc/sudoers.d/test
test ALL=(ALL:ALL) NOPASSWD: /usr/bin/sh -c if\ [\ x"$0"\ \=\ xfoo\ \];\ then\ echo\ T;\ else\ echo\ F;\ fi
test ALL=(ALL:ALL) NOPASSWD: /usr/bin/sh -c if\ [\ x"$0"\ \=\ xfoo\ \];\ then\ echo\ T;\ else\ echo\ F;\ fi\ *
# 
$ sudo -n /usr/bin/sh -c 'if [ x"$0" = xfoo ]; then echo T; else echo F; fi'
F
$ sudo -n /usr/bin/sh -c 'if [ x"$0" = xfoo ]; then echo T; else echo F; fi' foo
T
$