authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-09-03 15:16:26+02:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2020-09-03 15:16:26+02:00
log01a365f1b008fc1546f99c339dbae99521c169cd
treed4edd8dd0b6e13ff5aab6c4857da5daf0f94e21b
parente8a2aecd2f3ed13d7b9fb74248d455752de19840
signaturelock-open Commit is signed but in an unrecognized format.

std: ensure seteuid/setegid do not change saved id


1 files changed, 16 insertions(+), 2 deletions(-)

lib/std/os/linux.zig+16-2
......@@ -720,11 +720,25 @@ pub fn getegid() gid_t {
720720}
721721
722722pub fn seteuid(euid: uid_t) usize {
723 return setresuid(std.math.maxInt(uid_t), euid);
723 // We use setresuid here instead of setreuid to ensure that the saved uid
724 // is not changed. This is what musl and recent glibc versions do as well.
725 //
726 // The setresuid(2) man page says that if -1 is passed the corresponding
727 // id will not be changed. Since uid_t is unsigned, this wraps around to the
728 // max value in C.
729 comptime assert(@typeInfo(uid_t) == .Int and !@typeInfo(uid_t).Int.is_signed);
730 return setresuid(std.math.maxInt(uid_t), euid, std.math.maxInt(uid_t));
724731}
725732
726733pub fn setegid(egid: gid_t) usize {
727 return setregid(std.math.maxInt(gid_t), egid);
734 // We use setresgid here instead of setregid to ensure that the saved uid
735 // is not changed. This is what musl and recent glibc versions do as well.
736 //
737 // The setresgid(2) man page says that if -1 is passed the corresponding
738 // id will not be changed. Since gid_t is unsigned, this wraps around to the
739 // max value in C.
740 comptime assert(@typeInfo(uid_t) == .Int and !@typeInfo(uid_t).Int.is_signed);
741 return setresgid(std.math.maxInt(gid_t), egid, std.math.maxInt(gid_t));
728742}
729743
730744pub fn getresuid(ruid: *uid_t, euid: *uid_t, suid: *uid_t) usize {