From c79956843e88154334303b607bea64601a6b5059 Mon Sep 17 00:00:00 2001 From: Zeb Piasecki Date: Tue, 23 Mar 2021 19:37:28 -0400 Subject: bug: Fix getpwuid segfault (#440) Fixes a rare segfault if a uid does not have a passwd entry. The unsafe block at https://github.com/ClementTsang/bottom/blob/master/src/app/data_harvester/processes.rs#L137 can return a null pointer as specified at https://www.gnu.org/software/libc/manual/html_node/Lookup-User.html. --- src/app/data_harvester/processes.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/app/data_harvester/processes.rs b/src/app/data_harvester/processes.rs index 56bf09d4..4d7beda0 100644 --- a/src/app/data_harvester/processes.rs +++ b/src/app/data_harvester/processes.rs @@ -134,7 +134,13 @@ impl UserTable { if let Some(user) = self.uid_user_mapping.get(&uid) { Ok(user.clone()) } else { + // SAFETY: getpwuid returns a null pointer if no passwd entry is found for the uid let passwd = unsafe { libc::getpwuid(uid) }; + + if passwd.is_null() { + return Err(error::BottomError::QueryError("Missing passwd".into())); + } + let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) } .to_str()? .to_string(); -- cgit v1.2.3