ardaku / whoami

Rust crate to get the current user and environment.
Apache License 2.0
195 stars 31 forks source link

Check return value of getpwuid_r. #27

Closed adeschamps closed 3 years ago

adeschamps commented 3 years ago

This adds a check for the return value when calling getpwuid_r.

There are situations where getpwuid_r returns non-zero on error, and also situations where it returns 0 but sets the result pointer to NULL. The former case was not being handled, but the latter case was. However if you're (un)lucky, getpwuid_r will return non-zero, but the pointer still happens to be null, in which case this issue won't be noticed.

This situation came up when running a statically linked binary inside a FROM scratch Docker container, where there's no /etc/passwd file.

Here's a repo that demonstrates the bug and the fix: https://github.com/adeschamps/whoami-bug

AldaronLau commented 3 years ago

@adeschamps Thanks, this looks good!

AldaronLau commented 3 years ago

@adeschamps @bowlofeggs I just released whoami 1.1.3 with this bug fix.