File tree 2 files changed +18
-3
lines changed
2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,13 @@ pub trait CommandExt: Sealed {
21
21
/// Sets the child process's user ID. This translates to a
22
22
/// `setuid` call in the child process. Failure in the `setuid`
23
23
/// call will cause the spawn to fail.
24
+ ///
25
+ /// # Notes
26
+ ///
27
+ /// This will also trigger a call to `setgroups(0, NULL)` in the child
28
+ /// process if no groups have been specified.
29
+ /// This removes supplementary groups that might have given the child
30
+ /// unwanted permissions.
24
31
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
25
32
fn uid (
26
33
& mut self ,
Original file line number Diff line number Diff line change @@ -304,14 +304,22 @@ impl Command {
304
304
if let Some ( u) = self . get_uid ( ) {
305
305
// When dropping privileges from root, the `setgroups` call
306
306
// will remove any extraneous groups. We only drop groups
307
- // if the current uid is 0 and we weren't given an explicit
307
+ // if we have CAP_SETGID and we weren't given an explicit
308
308
// set of groups. If we don't call this, then even though our
309
309
// uid has dropped, we may still have groups that enable us to
310
310
// do super-user things.
311
311
//FIXME: Redox kernel does not support setgroups yet
312
312
#[ cfg( not( target_os = "redox" ) ) ]
313
- if libc:: getuid ( ) == 0 && self . get_groups ( ) . is_none ( ) {
314
- cvt ( libc:: setgroups ( 0 , ptr:: null ( ) ) ) ?;
313
+ if self . get_groups ( ) . is_none ( ) {
314
+ let res = cvt ( libc:: setgroups ( 0 , ptr:: null ( ) ) ) ;
315
+ if let Err ( e) = res {
316
+ // Here we ignore the case of not having CAP_SETGID.
317
+ // An alternative would be to require CAP_SETGID (in
318
+ // addition to CAP_SETUID) for setting the UID.
319
+ if e. raw_os_error ( ) != Some ( libc:: EPERM ) {
320
+ return Err ( e. into ( ) ) ;
321
+ }
322
+ }
315
323
}
316
324
cvt ( libc:: setuid ( u as uid_t ) ) ?;
317
325
}
You can’t perform that action at this time.
0 commit comments