Is there a way to disable `su` on a Linux machine? Or apparmor an entire account so that it cannot `su`
Hm
https://www.tecmint.com/disable-su-access-sudo-users/
But this only appears to disable su root. There seems to also be a way to do this with PAM but also that might only disable su root.
@mcc i think that technically only disables "sudo su", not regular "su"?
@ratsnakegames @mcc yeah, I was going to suggest that also. This is specifically disabling the path where you can run `sudo su` and type in the password of the current user (as opposed to `root`'s password) in order to execute su.
@lambdageek @ratsnakegames 😞 oh. DISABLE_SU made it sound like it disables su :(
@mcc I'll take this into account for my "Advanced shell: remote access and session multiplexing" tutorials. I'm trying to have a sandbox (virtual) machine for them to donexercices CTF-style.
@mcc if I have `sudo`, I don't need `su`:
`sudo -i` is sufficient to get a shell as root. `sudo su` is not required.
IMHO: If you don't want others to become root, revoke their sudo privs and set a root password.
@anton but i can prevent someone from sudoing by not putting them in sudoers, right? also i didn't mention root
@mcc just did this the other day! You can do it with PAM:
auth required pam_wheel.so use_uid deny group=(probably sudo but you could do all)
has to go in the pam.d config before the password auth rules.
@mcc I know a number of admins who remove su and disallow root logins because well-constrained sudoers is safer and has much better logging.
@mcc chmod 0000 /bin/su
@grumpybozo hm, would that prevent you from doing whatever the C equivalent of su is? or is there no C equivalent
@mcc Well, su is written in C so having C rely on the compiled binary would make no sense. There’s a standard C syscall: setuid().
An attacker could not reverse that chmod because the 4th zero removes the setuid bit on the su binary.
@grumpybozo I guess the question is, does su work because it calls a linux syscall that does a su-like thing, or does it work because the su executable is setuid root
@mcc @grumpybozo The part that gives you permission to change users isn’t in the su binary. It’s in the setuid bit in the file permissions. That lets su run as the owner (typically ‘root’), which then has the ability to setuid() to a different user. If you’re already root (or have become root through some other mechanism like sudo), this part is moot.
Traditionally you’d restrict su by not putting a user in the “wheel”group (gid 0); AIUI su checks this membership before proceeding. Though I imagine PAM makes this more complex.
@mcc mayebe dpkg-statoverride to remove the setuid bit (and maybe also the executable bits for good measure)?
This is morally equivalent to the various folks suggesting deleting or chmodding `su` but in a way that will survive util-linux updates.
note: I'm kind of guessing, haven't tried it.
@mcc wait are *accounts* a thing you can apparmor? I thought it worked on executables. (in fairness I am not particularly clear on what it does other than breaking file-selection dialog boxes sometimes)
@mcc why not go to the nuclear option and 'sudo rm -i $(which su)'?
@mcc Sure. /etc/pam.d/su and /etc/pam.d/su-l contain the authentication policies for su.
su on Linux does not require a user to be in the 'wheel' group. You can use pam_wheel to add that requirement and get a win in security without any risk. First, add yourself to the wheel group with: usermod -a -G wheel mcc.
I personally set PAM to additionally require a Security Key keypress, using pam_u2f with userpresence=1 for login, su, sudo and so on.
You could have a policy for su such as pam_warn (syslog message) followed by pam_deny. You could trial that with just pam_warn and see if that produces log messages in the ordinary operation of the computer.
@mcc Linux has the PR_SET_NO_NEW_PRIVS process control flag that will prevent a process or its descendants from gaining privileges by executing setuid binaries.
You can have it applied to the login sessions of a particular user via /etc/security/limits.conf (search for nonewprivs in the limits.conf man page).
It seems like in an environment where sudo exists `su` exists only for RCE'd servers to call it