Skip to content

Commit b3f58e8

Browse files
bors[bot]blyxxyz
andauthored
Merge #1521
1521: Prevent buffer over-read in getgroups() r=asomers a=blyxxyz An edge case I found in another wrapper around `getgroups()`. `@jhscheer` pointed me to this implementation which had the same issue. Co-authored-by: Jan Verbeek <jan.verbeek@posteo.nl>
2 parents bf4f273 + a8751ec commit b3f58e8

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/unistd.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,14 @@ pub fn getgroups() -> Result<Vec<Gid>> {
14201420
// Next, get the number of groups so we can size our Vec
14211421
let ngroups = unsafe { libc::getgroups(0, ptr::null_mut()) };
14221422

1423+
// If there are no supplementary groups, return early.
1424+
// This prevents a potential buffer over-read if the number of groups
1425+
// increases from zero before the next call. It would return the total
1426+
// number of groups beyond the capacity of the buffer.
1427+
if ngroups == 0 {
1428+
return Ok(Vec::new());
1429+
}
1430+
14231431
// Now actually get the groups. We try multiple times in case the number of
14241432
// groups has changed since the first call to getgroups() and the buffer is
14251433
// now too small.

0 commit comments

Comments
 (0)