Function users::switch::switch_user_group [−][src]
pub fn switch_user_group(uid: uid_t, gid: gid_t) -> Result<SwitchUserGuard>
Sets the effective user and the effective group for the current scope.
Typically, trying to switch to any user or group other than the ones already running the process requires root privileges.
Security considerations
- Because Rust does not guarantee running the destructor, it’s a good idea
to call
std::mem::drop
on the guard manually in security-sensitive situations. - This function switches the group before the user to prevent the user’s
privileges being dropped before trying to change the group (look up
POS36-C
). - This function will panic upon failing to set either walue, so the program does not continue executing with too many privileges.
libc functions used
Errors
This function will return Err
when an I/O error occurs during either
the seteuid
or setegid
calls.
Examples
use users::switch::switch_user_group; use std::mem::drop; { let guard = switch_user_group(1001, 1001); // current and effective user and group IDs are 1001 drop(guard); } // back to the old values