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 77f1a8b commit 146fc9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [0.21.2] - 29 September 2021
### Added
### Changed
### Fixed

- Fixed buffer overflow in `unistd::getgrouplist`.
(#[1545](https://github.com/nix-rust/nix/pull/1545))

## [0.21.1] - 13 August 2021
### Added
### Changed
Expand Down
4 changes: 2 additions & 2 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,8 +1530,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 @@ -1541,6 +1540,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 146fc9c

Please sign in to comment.