File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,13 @@ pub trait CommandExt: Sealed {
3939 /// Sets the child process's user ID. This translates to a
4040 /// `setuid` call in the child process. Failure in the `setuid`
4141 /// call will cause the spawn to fail.
42+ ///
43+ /// # Notes
44+ ///
45+ /// This will also trigger a call to `setgroups(0, NULL)` in the child
46+ /// process if no groups have been specified.
47+ /// This removes supplementary groups that might have given the child
48+ /// unwanted permissions.
4249 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
4350 fn uid ( & mut self , id : UserId ) -> & mut process:: Command ;
4451
Original file line number Diff line number Diff line change @@ -330,14 +330,22 @@ impl Command {
330330 if let Some ( u) = self . get_uid ( ) {
331331 // When dropping privileges from root, the `setgroups` call
332332 // will remove any extraneous groups. We only drop groups
333- // if the current uid is 0 and we weren't given an explicit
333+ // if we have CAP_SETGID and we weren't given an explicit
334334 // set of groups. If we don't call this, then even though our
335335 // uid has dropped, we may still have groups that enable us to
336336 // do super-user things.
337337 //FIXME: Redox kernel does not support setgroups yet
338338 #[ cfg( not( target_os = "redox" ) ) ]
339- if libc:: getuid ( ) == 0 && self . get_groups ( ) . is_none ( ) {
340- cvt ( libc:: setgroups ( 0 , crate :: ptr:: null ( ) ) ) ?;
339+ if self . get_groups ( ) . is_none ( ) {
340+ let res = cvt ( libc:: setgroups ( 0 , crate :: ptr:: null ( ) ) ) ;
341+ if let Err ( e) = res {
342+ // Here we ignore the case of not having CAP_SETGID.
343+ // An alternative would be to require CAP_SETGID (in
344+ // addition to CAP_SETUID) for setting the UID.
345+ if e. raw_os_error ( ) != Some ( libc:: EPERM ) {
346+ return Err ( e. into ( ) ) ;
347+ }
348+ }
341349 }
342350 cvt ( libc:: setuid ( u as uid_t ) ) ?;
343351 }
You can’t perform that action at this time.
0 commit comments