Skip to content

Commit

Permalink
Fix memory unsafety in unistd::getgrouplist
Browse files Browse the repository at this point in the history
Fixes #1541
  • Loading branch information
vitalyd authored and asomers committed Sep 29, 2021
1 parent 9a2f86f commit b3b9fd3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -91,9 +91,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Added more errno definitions for better backwards compatibility with
Nix 0.21.0.
(#[1467](https://github.com/nix-rust/nix/pull/1467))

- Fixed potential undefined behavior in `Signal::try_from` on some platforms.
(#[1484](https://github.com/nix-rust/nix/pull/1484))
- Fixed buffer overflow in `unistd::getgrouplist`.
(#[1542](https://github.com/nix-rust/nix/pull/1542))

### Removed

Expand Down
4 changes: 2 additions & 2 deletions src/unistd.rs
Expand Up @@ -1540,8 +1540,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
Ok(None) | Err(_) => <c_int>::max_value(),
};
use std::cmp::min;
let mut ngroups = min(ngroups_max, 8);
let mut groups = Vec::<Gid>::with_capacity(ngroups as usize);
let mut groups = Vec::<Gid>::with_capacity(min(ngroups_max, 8) as usize);
cfg_if! {
if #[cfg(any(target_os = "ios", target_os = "macos"))] {
type getgrouplist_group_t = c_int;
Expand All @@ -1551,6 +1550,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
}
let gid: gid_t = group.into();
loop {
let mut ngroups = groups.capacity() as i32;
let ret = unsafe {
libc::getgrouplist(user.as_ptr(),
gid as getgrouplist_group_t,
Expand Down

0 comments on commit b3b9fd3

Please sign in to comment.