Skip to content

Commit 1671edc

Browse files
vitalydasomers
authored andcommitted
Fix memory unsafety in unistd::getgrouplist
Fixes #1541
1 parent 9a2f86f commit 1671edc

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
9191
- Added more errno definitions for better backwards compatibility with
9292
Nix 0.21.0.
9393
(#[1467](https://github.com/nix-rust/nix/pull/1467))
94-
9594
- Fixed potential undefined behavior in `Signal::try_from` on some platforms.
9695
(#[1484](https://github.com/nix-rust/nix/pull/1484))
96+
- Fixed buffer overflow in `unistd::getgrouplist`.
97+
(#[1545](https://github.com/nix-rust/nix/pull/1545))
9798

9899
### Removed
99100

src/unistd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,8 +1540,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
15401540
Ok(None) | Err(_) => <c_int>::max_value(),
15411541
};
15421542
use std::cmp::min;
1543-
let mut ngroups = min(ngroups_max, 8);
1544-
let mut groups = Vec::<Gid>::with_capacity(ngroups as usize);
1543+
let mut groups = Vec::<Gid>::with_capacity(min(ngroups_max, 8) as usize);
15451544
cfg_if! {
15461545
if #[cfg(any(target_os = "ios", target_os = "macos"))] {
15471546
type getgrouplist_group_t = c_int;
@@ -1551,6 +1550,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
15511550
}
15521551
let gid: gid_t = group.into();
15531552
loop {
1553+
let mut ngroups = groups.capacity() as i32;
15541554
let ret = unsafe {
15551555
libc::getgrouplist(user.as_ptr(),
15561556
gid as getgrouplist_group_t,

0 commit comments

Comments
 (0)