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

Keep some groups regardless of nogroups and restore nogroups on nvidia #4725

Merged
merged 4 commits into from
Nov 30, 2021

Conversation

kmk3
Copy link
Collaborator

@kmk3 kmk3 commented Nov 30, 2021

$ git log --reverse --pretty='* %s' master..
* Keep audio and video groups regardless of nogroups
* Keep render, lp, input and other groups regardless of nogroups
* Make nogroups work on nvidia again
* etc: Remove comments about nogroups and noroot on nvidia

This should fix audio on seatd (#4531/#4603) and also make nogroups work
regardless of no3d on nvidia (#841/#3644).

Note: This is a continuation of #4632.

Note2: I have not tested this yet.

Cc: @crocket (from #4603) @hlein (from #4632)

Currently, on systems that use seat managers that do not implement
seat-based ACLs (such as seatd), sound is broken whenever `nogroups` is
used.  This happens because without ACLs, access to the audio devices in
/dev is controlled by the standard group permissions and the "audio"
group is always dropped when `nogroups` is used.  This patch makes the
"audio" and "video" groups be dropped if and only if `noaudio` and
`novideo` are in effect, respectively (and independently of `nogroups`).
See netblue30#4603 and the linked issues/discussions for details.

Note: This is a continuation of commit ea564eb ("Consider nosound and
novideo when keeping groups") / PR netblue30#4632.

Relates to netblue30#2042 and netblue30#4531.
Mappings of command -> group that this commit adds:

* no3d -> render
* noprinters -> lp
* nodvd -> cdrom (Debian[1] and Gentoo[2]), optical (Arch[3])
* noinput -> input

Mappings that were considered but that are not added:

* notv -> ? (unknown group)
* nou2f -> ? (devices are apparently owned by root; see netblue30#4603)

Based on @rusty-snake's suggestion:
netblue30#4603 (comment)

See the previous commit ("Keep audio and video groups regardless of
nogroups") for details.

Relates to netblue30#2042 and netblue30#4632.

[1] https://wiki.debian.org/SystemGroups
[2] https://api.gentoo.org/uid-gid.txt
[3] https://wiki.archlinux.org/title/Users_and_groups
Remove workaround from commit 623e682 ("temporary fix for
nvidia/nogroups/noroot issue (netblue30#3644, netblue30#841)", 2020-10-02) and from commit
cb460c3 ("more nvidia (netblue30#3644)", 2020-10-03).

The handling of the "render" and "video" groups is separate from
`nogroups` now, so disabling `nogroups` on nvidia shouldn't be necessary
anymore.  See the previous 2 commits for details.

See also the discussion on PR netblue30#4632.
`nogroups` should not have been causing issues with rendering on nvidia
since commit 623e682 ("temporary fix for nvidia/nogroups/noroot issue
(netblue30#3644, netblue30#841)", 2020-10-02) and commit cb460c3 ("more nvidia (netblue30#3644)",
2020-10-03), which had made it a no-op on nvidia.  And the handling of
the "render" and "video" groups are independent to the handling of
`nogroups` now; see the previous 3 commits.

Commits which introduced the comments on each profile:

* kodi.profile: commit ce462b6 ("fix netblue30#3501", 2020-07-16)
* mpsyt.profile: commit e17b48f ("new profile mpsyt.profile",
  2018-11-28)
* mpv.profile: commit cc7c489 ("Document netblue30#1945", 2018-07-25)
* steam.profile: commit d6f8169 ("steam fixes; netblue30#841, netblue30#3267",
  2020-03-15)

Commands used to find the comments:

    git grep -i nvidia -- etc/profile-* | grep -v private-etc

Relates to netblue30#4632.
@kmk3
Copy link
Collaborator Author

kmk3 commented Nov 30, 2021

Should also fix access to gamepads when using seatd:

@crocket commented on Oct 10:

Either nogroup or noroot breaks ALSA(/dev/snd) and gamepad(/dev/input) on retroarch on Gentoo Linux.

$ ls -alh /dev/snd
total 0
drwxr-xr-x  3 root root      280 Oct 10 07:51 ./
drwxr-xr-x 18 root root     4.1K Oct 10 07:51 ../
drwxr-xr-x  2 root root       80 Oct 10 07:51 by-path/
crw-rw----  1 root audio 116, 10 Oct 10 07:51 controlC0
crw-rw----  1 root audio 116,  4 Oct 10 07:51 controlC1
crw-rw----  1 root audio 116,  9 Oct 10 07:51 hwC0D0
crw-rw----  1 root audio 116,  3 Oct 10 07:51 hwC1D0
crw-rw----  1 root audio 116,  6 Oct 10 07:51 pcmC0D0c
crw-rw----  1 root audio 116,  5 Oct 10 07:59 pcmC0D0p
crw-rw----  1 root audio 116,  7 Oct 10 07:51 pcmC0D1p
crw-rw----  1 root audio 116,  8 Oct 10 07:51 pcmC0D2c
crw-rw----  1 root audio 116,  2 Oct 10 07:51 pcmC1D3p
crw-rw----  1 root audio 116,  1 Oct 10 07:51 seq
crw-rw----  1 root audio 116, 33 Oct 10 07:51 timer
$ ls -alh /dev/input
total 0
drwxr-xr-x  4 root root     520 Oct 10 07:51 ./
drwxr-xr-x 18 root root    4.1K Oct 10 07:51 ../
drwxr-xr-x  2 root root     220 Oct 10 07:51 by-id/
drwxr-xr-x  2 root root     220 Oct 10 07:51 by-path/
crw-rw----  1 root input 13, 64 Oct 10 07:51 event0
crw-rw----  1 root input 13, 65 Oct 10 07:51 event1
crw-rw----  1 root input 13, 74 Oct 10 07:51 event10
crw-rw----  1 root input 13, 75 Oct 10 07:51 event11
crw-rw----  1 root input 13, 76 Oct 10 07:51 event12
crw-rw----  1 root input 13, 77 Oct 10 07:51 event13
crw-rw----  1 root input 13, 78 Oct 10 07:51 event14
crw-rw----  1 root input 13, 79 Oct 10 07:51 event15
crw-rw----  1 root input 13, 80 Oct 10 07:51 event16
crw-rw----  1 root input 13, 81 Oct 10 07:51 event17
crw-rw----  1 root input 13, 66 Oct 10 07:51 event2
crw-rw----  1 root input 13, 67 Oct 10 07:51 event3
crw-rw----  1 root input 13, 68 Oct 10 07:51 event4
crw-rw----  1 root input 13, 69 Oct 10 07:51 event5
crw-rw----  1 root input 13, 70 Oct 10 07:51 event6
crw-rw----  1 root input 13, 71 Oct 10 07:51 event7
crw-rw----  1 root input 13, 72 Oct 10 07:51 event8
crw-rw----  1 root input 13, 73 Oct 10 07:51 event9
crw-rw-r--  1 root input 13,  0 Oct 10 07:51 js0
crw-rw-r--  1 root input 13,  1 Oct 10 07:51 js1
crw-rw----  1 root input 13, 63 Oct 10 07:51 mice
crw-rw----  1 root input 13, 32 Oct 10 07:51 mouse0

@crocket
Copy link
Contributor

crocket commented Nov 30, 2021

The concept behind this pull request should work, but it doesn't detect presence of (e)logind.

@glitsj16
Copy link
Collaborator

LGTM, but I don't have access to either gamepad or nvidia GPU to properly test...

@netblue30 netblue30 merged commit 75bc0f8 into netblue30:master Nov 30, 2021
@netblue30
Copy link
Owner

Merged, thanks!

@kmk3
Copy link
Collaborator Author

kmk3 commented Nov 30, 2021

Well, this does not seem to work:

$ groups | grep audio >/dev/null && echo works
works
$ firejail --quiet --noprofile --nogroups groups | grep audio >/dev/null && echo works
$

Sorry, I should have opened this as a draft.

I suspect that it fails because of this:

src/firejail/util.c:

// drop privileges
// - for root group or if nogroups is set, supplementary groups are not configured
void drop_privs(int nogroups) {
	gid_t gid = getgid();
	if (arg_debug)
		printf("Drop privileges: pid %d, uid %d, gid %d, nogroups %d\n",  getpid(), getuid(), gid, nogroups);

	// configure supplementary groups
	EUID_ROOT();
	if (gid == 0 || nogroups) {
		if (setgroups(0, NULL) < 0)
			errExit("setgroups");
		if (arg_debug)
			printf("No supplementary groups\n");
	}
	else if (arg_noroot)
		clean_supplementary_groups(gid);

	// set uid/gid
	if (setresgid(-1, getgid(), getgid()) != 0)
		errExit("setresgid");
	if (setresuid(-1, getuid(), getuid()) != 0)
		errExit("setresuid");
}

Anyway, I'm working on it.

Note: If nogroups still works the same way, removing the nvidia workaround
means that this PR probably broke nvidia again. If I can't fix nogroups, I
suppose that I'll just revert that commit.

@kmk3 kmk3 deleted the fix-groups-misc2 branch November 30, 2021 18:13
@kmk3
Copy link
Collaborator Author

kmk3 commented Nov 30, 2021

@crocket commented on Nov 30:

The concept behind this pull request should work, but it doesn't detect
presence of (e)logind.

Good catch, that should probably be taken into consideration. Now that you
mention it, I remember writing something like if (!arg_nogroups || !have_seat_acls) a while back, but I forgot about it (and didn't immediately
find it on any related branch) when making this PR.

Relevant comment:

@topimiettinen commented on Oct 15:

if !arg_nosound:
    drop_group("audio")
if !arg_novideo:
    drop_group("video")
if !arg_nodvd:
    drop_group("cdrom")
if !arg_noinput:
    drop_group("input")
if !arg_no3d: // /dev/dri/card0 has group video
    drop_group("render")
if !arg_noprinters:
    drop_group("lp")

This could work well if also coupled with the logind check, so users on
distros with logind would not get extra permissions but others would get at
least some groups automatically when needed.

@rusty-snake
Copy link
Collaborator

IMHO nogroups should always mean no-groups. If we want selective nogroups, we should make this explicit, e.g. groups restricted.

kmk3 added a commit to kmk3/firejail that referenced this pull request Dec 1, 2021
This amends commit 11418a4 ("dns fixes", 2019-10-31).

fwarning already prints "Warning: " at the beginning.

Kind of relates to commit 6ddedeb ("Make nogroups work on nvidia
again", 2021-11-29) / PR netblue30#4725, which removed code affected by this.

Command used to find the duplicates:

    git grep -i -F 'fwarning("Warning:' -- src
kmk3 added a commit to kmk3/firejail that referenced this pull request Dec 1, 2021
This amends commit b828a90 ("Keep audio and video groups regardless of
nogroups", 2021-11-28) from PR netblue30#4725.

The commit above did not change the behavior (the groups are still not
kept).  With this commit, it appears to work properly:

    $ groups | grep audio >/dev/null && echo kept
    kept
    # with check_can_drop_all_groups == 0
    $ firejail --quiet --noprofile --nogroups groups |
      grep audio >/dev/null && echo kept
    kept
    # with check_can_drop_all_groups == 1
    $ firejail --quiet --noprofile --nogroups groups |
      grep audio >/dev/null && echo kept
    $

Add a new check_can_drop_all_groups function to check whether the
supplementary groups can be safely dropped without potentially causing
issues with audio, 3D hardware acceleration or input (and maybe more).
It returns false if nvidia (and no `no3d`) is used or if (e)logind is
not running, as in either case the supplementary groups might be needed.

Note: With this, the behavior from before netblue30#4725 is restored on (e)logind
systems (when not using nvidia), as it makes the supplementary groups
always be dropped.

Note2: Even with the static variable, these checks still happen at least
once per translation unit (so twice in total per my testing).

This also amends (/kind of reverts) commit 6ddedeb ("Make nogroups
work on nvidia again", 2021-11-29) from PR netblue30#4725, as it restores the
nvidia check from it into the new check_can_drop_all_groups function.
kmk3 added a commit to kmk3/firejail that referenced this pull request Dec 1, 2021
This amends commit b828a90 ("Keep audio and video groups regardless of
nogroups", 2021-11-28) from PR netblue30#4725.

The commit above did not change the behavior (the groups are still not
kept).  With this commit, it appears to work properly:

    $ groups | grep audio >/dev/null && echo kept
    kept
    # with check_can_drop_all_groups == 0
    $ firejail --quiet --noprofile --nogroups groups |
      grep audio >/dev/null && echo kept
    kept
    # with check_can_drop_all_groups == 1
    $ firejail --quiet --noprofile --nogroups groups |
      grep audio >/dev/null && echo kept
    $

Add a new check_can_drop_all_groups function to check whether the
supplementary groups can be safely dropped without potentially causing
issues with audio, 3D hardware acceleration or input (and maybe more).
It returns false if nvidia (and no `no3d`) is used or if (e)logind is
not running, as in either case the supplementary groups might be needed.

Note: With this, the behavior from before netblue30#4725 is restored on (e)logind
systems (when not using nvidia), as it makes the supplementary groups
always be dropped.

Note2: Even with the static variable, these checks still happen at least
once per translation unit (so twice in total per my testing).

This also amends (/kind of reverts) commit 6ddedeb ("Make nogroups
work on nvidia again", 2021-11-29) from PR netblue30#4725, as it restores the
nvidia check from it into the new check_can_drop_all_groups function.
@kmk3
Copy link
Collaborator Author

kmk3 commented Dec 1, 2021

@rusty-snake commented on Nov 30:

IMHO nogroups should always mean no-groups. If we want selective
nogroups, we should make this explicit, e.g. groups restricted.

With #4732, the supplementary groups should always be dropped when (e)logind is
running (and nvidia is not used), so nothing should really change on such
systems compared to the behavior from before this PR.

I added the conditional group keeping because that should fix the audio/input
issues on seatd (and potentially also fix group-related issues on nvidia, if
any) without requiring a new command or changes to profiles (and because there
already was a nogroups workaround for nvidia anyway).

@rusty-snake
Copy link
Collaborator

Ahh Ok thanks. Then I'm fine with the change (if RELNOTEs/manpage document it).

kmk3 added a commit to kmk3/firejail that referenced this pull request Dec 7, 2021
This amends commit b828a90 ("Keep audio and video groups regardless of
nogroups", 2021-11-28) from PR netblue30#4725.

The commit above did not change the behavior (the groups are still not
kept).  With this commit, it appears to work properly:

    $ groups | grep audio >/dev/null && echo kept
    kept
    # with check_can_drop_all_groups == 0
    $ firejail --quiet --noprofile --nogroups groups |
      grep audio >/dev/null && echo kept
    kept
    # with check_can_drop_all_groups == 1
    $ firejail --quiet --noprofile --nogroups groups |
      grep audio >/dev/null && echo kept
    $

Add a new check_can_drop_all_groups function to check whether the
supplementary groups can be safely dropped without potentially causing
issues with audio, 3D hardware acceleration or input (and maybe more).
It returns false if nvidia (and no `no3d`) is used or if (e)logind is
not running, as in either case the supplementary groups might be needed.

Note: With this, the behavior from before netblue30#4725 is restored on (e)logind
systems (when not using nvidia), as it makes the supplementary groups
always be dropped on such systems.

Note2: Even with the static variable, these checks still happen at least
twice.  It seems that it happens once per translation unit (and I think
that it may happen more times if there are multiple processes involved).

This also amends (/kind of reverts) commit 6ddedeb ("Make nogroups
work on nvidia again", 2021-11-29) from PR netblue30#4725, as it restores the
nvidia check from it into the new check_can_drop_all_groups function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (on RELNOTES)
Development

Successfully merging this pull request may close these issues.

5 participants