Skip to content

Commit

Permalink
Add setgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Feb 1, 2024
1 parent d268591 commit f385b7f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/backend/libc/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,12 @@ pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {

unsafe { ret_usize(c::getgroups(len, buf.as_mut_ptr().cast()) as isize) }
}

#[cfg(linux_kernel)]
#[inline]
pub(crate) fn setgroups(groups: &[crate::ugid::Gid]) -> io::Result<()> {
syscall! {
fn setgroups(size: c::size_t, list: *const c::gid_t) via SYS_setgroups -> c::c_int
}
ret(unsafe { setgroups(groups.len(), groups.as_ptr().cast()) })
}
8 changes: 7 additions & 1 deletion src/backend/linux_raw/process/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::backend::c;
use crate::backend::conv::slice_mut;
use crate::backend::conv::{
by_mut, by_ref, c_int, c_uint, negative_pid, pass_usize, raw_fd, ret, ret_c_int,
ret_c_int_infallible, ret_c_uint, ret_infallible, ret_owned_fd, ret_usize, size_of,
ret_c_int_infallible, ret_c_uint, ret_infallible, ret_owned_fd, ret_usize, size_of, slice,
slice_just_addr, zero,
};
use crate::fd::{AsRawFd, BorrowedFd, OwnedFd, RawFd};
Expand Down Expand Up @@ -601,3 +601,9 @@ pub(crate) fn getgroups(buf: &mut [Gid]) -> io::Result<usize> {
))
}
}

#[inline]
pub(crate) fn setgroups(gids: &[crate::ugid::Gid]) -> io::Result<()> {
let (addr, len) = slice(gids);
unsafe { ret(syscall_readonly!(__NR_setgroups, len, addr)) }
}
12 changes: 12 additions & 0 deletions src/process/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,15 @@ pub fn getgroups() -> io::Result<Vec<Gid>> {
buffer.resize(buffer.capacity(), Gid::ROOT);
}
}

/// `setgroups(groups)`-Sets the supplementary group IDs for the calling process.
///
/// # References
/// - [Linux]
///
/// [Linux]: https://man7.org/linux/man-pages/man2/setgroups.2.html
#[cfg(linux_kernel)]
#[inline]
pub fn set_groups(groups: &[Gid]) -> io::Result<()> {
backend::process::syscalls::setgroups(groups)
}

0 comments on commit f385b7f

Please sign in to comment.