about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorElliot Roberts <Elliot0000101@gmail.com>2022-04-12 07:46:07 -0700
committerPavel Grigorenko <grigorenkopv@ya.ru>2024-02-26 23:11:28 +0300
commit3a6af84fca1b41614887748e9fdb0e2b2a13fb96 (patch)
tree9fa28510cf7d4f32c6b03b9018d76d57436a2356 /library/std/src/sys
parent829308e9af1a1f55540d2c8b0e007e1d0cb9c8c6 (diff)
change std::process to drop supplementary groups based on CAP_SETGID
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/pal/unix/process/process_unix.rs14
1 files changed, 11 insertions, 3 deletions
diff --git a/library/std/src/sys/pal/unix/process/process_unix.rs b/library/std/src/sys/pal/unix/process/process_unix.rs
index 97cbd1929d3..f017d39d804 100644
--- a/library/std/src/sys/pal/unix/process/process_unix.rs
+++ b/library/std/src/sys/pal/unix/process/process_unix.rs
@@ -330,14 +330,22 @@ impl Command {
             if let Some(u) = self.get_uid() {
                 // When dropping privileges from root, the `setgroups` call
                 // will remove any extraneous groups. We only drop groups
-                // if the current uid is 0 and we weren't given an explicit
+                // if we have CAP_SETGID and we weren't given an explicit
                 // set of groups. If we don't call this, then even though our
                 // uid has dropped, we may still have groups that enable us to
                 // do super-user things.
                 //FIXME: Redox kernel does not support setgroups yet
                 #[cfg(not(target_os = "redox"))]
-                if libc::getuid() == 0 && self.get_groups().is_none() {
-                    cvt(libc::setgroups(0, crate::ptr::null()))?;
+                if self.get_groups().is_none() {
+                    let res = cvt(libc::setgroups(0, crate::ptr::null()));
+                    if let Err(e) = res {
+                        // Here we ignore the case of not having CAP_SETGID.
+                        // An alternative would be to require CAP_SETGID (in
+                        // addition to CAP_SETUID) for setting the UID.
+                        if e.raw_os_error() != Some(libc::EPERM) {
+                            return Err(e.into());
+                        }
+                    }
                 }
                 cvt(libc::setuid(u as uid_t))?;
             }