Skip to content

Commit 3c2107b

Browse files
committed
unistd: avoid infinite loop caused by reserve_double_buffer_size
Functions such as Group::from_anything use reserve_double_buffer_size in a loop, expecting it to return ERANGE if the passed limit is reached. However, the returned vector is passed as pointer to a libc function that writes data into memory and doesn't update the length of the Vec. Because of this, the previous code would never return ERANGE and the calling loops would never exit if they hit a case where the required buffer was larger than the maximum buffer. This fixes the problem by checking the capacity rather than the length. Signed-off-by: Steven Danna <[email protected]>
1 parent 627dff9 commit 3c2107b

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2222
(#[1198](https://github.com/nix-rust/nix/pull/1198))
2323

2424
### Fixed
25+
26+
- Fixed a bug in nix::unistd that would result in an infinite loop
27+
when a group or user lookup required a buffer larger than
28+
16KB. (#[1198](https://github.com/nix-rust/nix/pull/1198))
29+
2530
### Removed
2631

2732
## [0.17.0] - 3 February 2020

src/unistd.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,13 @@ pub fn mkfifo<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
507507
}
508508

509509
/// Creates new fifo special file (named pipe) with path `path` and access rights `mode`.
510-
///
510+
///
511511
/// If `dirfd` has a value, then `path` is relative to directory associated with the file descriptor.
512-
///
513-
/// If `dirfd` is `None`, then `path` is relative to the current working directory.
514-
///
512+
///
513+
/// If `dirfd` is `None`, then `path` is relative to the current working directory.
514+
///
515515
/// # References
516-
///
516+
///
517517
/// [mkfifoat(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/mkfifoat.html).
518518
// mkfifoat is not implemented in OSX or android
519519
#[inline]
@@ -559,7 +559,7 @@ pub fn symlinkat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
559559
fn reserve_double_buffer_size<T>(buf: &mut Vec<T>, limit: usize) -> Result<()> {
560560
use std::cmp::min;
561561

562-
if buf.len() >= limit {
562+
if buf.capacity() >= limit {
563563
return Err(Error::Sys(Errno::ERANGE))
564564
}
565565

0 commit comments

Comments
 (0)