Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

Linux seems to at least write one group always from getgroups(2). However, POSIX doesn't guarantee this, and a system might have 0 groups.

It is implementation‐defined whether getgroups() also returns
the effective group ID in the grouplist array.

Considering such a system, the call getgroups(0,NULL) could indeed return 0, and the second call to getgroups might return a higher value, if the group list has grown in between (race condition). If this is the case, we'd return an array of 0 elements (or 1, due to the MALLOC() trick to avoid calling it with 0), with no elements filled, but where ngids has been updated to have a positive value. When the caller of agetgroups() reads the array, they'd overrun the buffer.

Fixes: 05322ed (2025-01-24; "lib/shadow/grp/: agetgroups(): Add function")
Fixes: de941a7 (2025-01-24; "lib/, src/: Simplify allocation of buffer")

…ux systems

Linux seems to at least write one group always from getgroups(2).
However, POSIX doesn't guarantee this, and a system might have 0 groups.

	It is implementation‐defined whether getgroups() also returns
	the effective group ID in the grouplist array.

Considering such a system, the call getgroups(0,NULL) could indeed
return 0, and the second call to getgroups might return a higher value,
if the group list has grown in between (race condition).  If this is the
case, we'd return an array of 0 elements (or 1, due to the MALLOC()
trick to avoid calling it with 0), with no elements filled, but where
ngids has been updated to have a positive value.  When the caller of
agetgroups() reads the array, they'd overrun the buffer.

Fixes: 05322ed (2025-01-24; "lib/shadow/grp/: agetgroups(): Add function")
Fixes: de941a7 (2025-01-24; "lib/, src/: Simplify allocation of buffer")
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@alejandro-colomar alejandro-colomar requested a review from hallyn July 16, 2025 12:58
@hallyn hallyn merged commit dc2c85d into shadow-maint:master Jul 16, 2025
10 checks passed
@alejandro-colomar
Copy link
Collaborator Author

@karelzak I see the exact same bug in util-linux. I guess that even if you don't pretend util-linux to ever run on a non-Linux system, it would be good to write the code portably, in case someone else tries to port it to other systems. Should I send a PR for fixing util-linux?

@karelzak
Copy link
Contributor

@alejandro-colomar thanks. I'll fix it in util-linux too. It's better to use robust code.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants