Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aur-build: drop privileges with setpriv(1) #977

Merged
merged 13 commits into from
Apr 2, 2022
Merged

aur-build: drop privileges with setpriv(1) #977

merged 13 commits into from
Apr 2, 2022

Conversation

AladW
Copy link
Member

@AladW AladW commented Apr 2, 2022

There are three cases:

  1. If UID == 0, drop privileges for user commands (e.g. repo-add, gpg, makepkg)
  2. If UID > 0, raise privileges for root commands (pacman)
  3. Otherwise, run the command as-is (e.g. aur-chroot)

This approach and gpg-set-passphrase(1) renders examples/chroot-batch obsolete, demonstrative purposes excepted. It can replaced by a corresponding section in aur-build(1).

@AladW AladW added this to the 9.1 milestone Apr 2, 2022
@AladW AladW force-pushed the build-runprivs branch 7 times, most recently from d925d6a to 333c9c2 Compare April 2, 2022 15:04
@AladW AladW changed the title aur-build: add run_privs() for privilege management aur-build: drop privileges with runuser(1) Apr 2, 2022
AladW added 12 commits April 2, 2022 21:13
There are three cases:

1. If UID==0, drop privileges for user commands (repo-add, gpg, makepkg)
2. If UID>0, raise privileges for root commands (pacman)
3. Otherwise, run the command as-is.

This approach and gpg-set-passphrase(1) renders examples/chroot-batch
obsolete, demonstrative purposes excepted.
The main difference between both commands is that runuser(1) uses a PAM
session while setpriv(1) does not. From a practical perspective,
runuser(1) is more lenient with environment variables:

  $ sudo setpriv --reuid archie printenv HOME DISPLAY
  /root
  :0:0

  $ sudo setpriv --reuid archie --reset-env printenv HOME DISPLAY
  /home/archie

  $ sudo runuser --pty -u archie printenv HOME DISPLAY
  /home/archie
  :0:0

  $ sudo runuser --pty -u archie -p printenv HOME DISPLAY
  /root
  :0:0

Having both HOME and DISPLAY set allows in particular to use `gpg(1)`
with a graphical pinentry.
Together with runuser(1), this removes the need for the chroot-batch
example.
Printing the runuser(1) command-line is only used in one place. Compose
as_user and run_msg instead.
/dev/stdout points to /proc/self/fd/1, which has chmod 700 for the owning
process. In particular, when running `aur-repo --status` with setpriv(1) or
runuser(1) from a process run as root, the error

  /dev/stdout: Permission denied

occurs. Remove the redirection and only add it if --status-file is specified.
runuser(1) --pty results in messy output, even when the man page claims it does
not. The flag was used for the better security advertised in the manual.

Instead of removing --pty, switch back to setpriv(1) without --reset-env and
USER, HOME, LOGNAME and SHELL set manually.
This way "as_user" can be removed from the "Running makepkg ..."
diagnostic
Fix a mistake introduced with 5c7db7e

Add a similar `elif` statement for pacman.conf just in case.
@AladW
Copy link
Member Author

AladW commented Apr 2, 2022

This turned out to be a lot more work than anticipated. On the flip-side, using setpriv(1) exposed two long-standing bugs in aur-repo.

@AladW AladW changed the title aur-build: drop privileges with runuser(1) aur-build: drop privileges with setpriv(1) Apr 2, 2022
@AladW AladW merged commit e5c02ab into master Apr 2, 2022
@AladW AladW deleted the build-runprivs branch April 2, 2022 19:37
@AladW
Copy link
Member Author

AladW commented Apr 5, 2022

Another observation is that this seems much easier in languages exposing setuid(), e.g. perl https://perldoc.perl.org/perlvar#$UID

@Earnestly
Copy link
Contributor

Yes, but that means using setuid. I'd suggest avoiding that if you can (the shell doesn't expose it usually due to race conditions, openbsd does because it has tricks to prevent this problem.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants