authorgravatar for klingt.net@gmail.comAndreas Linz <klingt.net@gmail.com> 2020-12-23 10:16:27+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-23 11:16:27+02:00
log7e63f7ad0311c80bcec89649d723af8a97935ba2
treec5186540d15202aa105e1853bf14ef52d37f8eb0
parent4420dabdf5cb1ad421d62d946738eb61fcff0ce5
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Truncate user and group ids for 64 bit Linux systems (#7466)

* Truncate user and group ids Calls to `getuid`, `getgid` and their `eid` variants fail to compile on 64bit Linux systems because the return value of the syscall is of `usize` and needs to be truncated to fit the size of `uid_t` that is 32 bit. Thanks to @FireFox317 for figuring this out in Zig's Discord channel! * Add a regression test for user and group ids * Replace @truncate with @intCast This should be safe because `uid_t` will be 32-bit. * Add missing import for getauxval * Add missing package names * Revert "Add missing import for getauxval" This reverts commit 38f93dc89effdf657f2b81a56b96527ce4083f52. * Skip user and group test if builtin.link_libc

2 files changed, 17 insertions(+), 8 deletions(-)

lib/std/os/linux.zig+8-8
...@@ -745,33 +745,33 @@ pub fn setregid(rgid: gid_t, egid: gid_t) usize {...@@ -745,33 +745,33 @@ pub fn setregid(rgid: gid_t, egid: gid_t) usize {
745745
746pub fn getuid() uid_t {746pub fn getuid() uid_t {
747 if (@hasField(SYS, "getuid32")) {747 if (@hasField(SYS, "getuid32")) {
748 return @as(uid_t, syscall0(.getuid32));748 return @intCast(uid_t, syscall0(.getuid32));
749 } else {749 } else {
750 return @as(uid_t, syscall0(.getuid));750 return @intCast(uid_t, syscall0(.getuid));
751 }751 }
752}752}
753753
754pub fn getgid() gid_t {754pub fn getgid() gid_t {
755 if (@hasField(SYS, "getgid32")) {755 if (@hasField(SYS, "getgid32")) {
756 return @as(gid_t, syscall0(.getgid32));756 return @intCast(gid_t, syscall0(.getgid32));
757 } else {757 } else {
758 return @as(gid_t, syscall0(.getgid));758 return @intCast(gid_t, syscall0(.getgid));
759 }759 }
760}760}
761761
762pub fn geteuid() uid_t {762pub fn geteuid() uid_t {
763 if (@hasField(SYS, "geteuid32")) {763 if (@hasField(SYS, "geteuid32")) {
764 return @as(uid_t, syscall0(.geteuid32));764 return @intCast(uid_t, syscall0(.geteuid32));
765 } else {765 } else {
766 return @as(uid_t, syscall0(.geteuid));766 return @intCast(uid_t, syscall0(.geteuid));
767 }767 }
768}768}
769769
770pub fn getegid() gid_t {770pub fn getegid() gid_t {
771 if (@hasField(SYS, "getegid32")) {771 if (@hasField(SYS, "getegid32")) {
772 return @as(gid_t, syscall0(.getegid32));772 return @intCast(gid_t, syscall0(.getegid32));
773 } else {773 } else {
774 return @as(gid_t, syscall0(.getegid));774 return @intCast(gid_t, syscall0(.getegid));
775 }775 }
776}776}
777777
lib/std/os/linux/test.zig+9
...@@ -9,6 +9,7 @@ const linux = std.os.linux;...@@ -9,6 +9,7 @@ const linux = std.os.linux;
9const mem = std.mem;9const mem = std.mem;
10const elf = std.elf;10const elf = std.elf;
11const expect = std.testing.expect;11const expect = std.testing.expect;
12const expectEqual = std.testing.expectEqual;
12const fs = std.fs;13const fs = std.fs;
1314
14test "fallocate" {15test "fallocate" {
...@@ -99,3 +100,11 @@ test "statx" {...@@ -99,3 +100,11 @@ test "statx" {
99 expect(@bitCast(u64, @as(i64, stat_buf.blksize)) == statx_buf.blksize);100 expect(@bitCast(u64, @as(i64, stat_buf.blksize)) == statx_buf.blksize);
100 expect(@bitCast(u64, @as(i64, stat_buf.blocks)) == statx_buf.blocks);101 expect(@bitCast(u64, @as(i64, stat_buf.blocks)) == statx_buf.blocks);
101}102}
103
104test "user and group ids" {
105 if (builtin.link_libc) return error.SkipZigTest;
106 expectEqual(linux.getauxval(elf.AT_UID), linux.getuid());
107 expectEqual(linux.getauxval(elf.AT_GID), linux.getgid());
108 expectEqual(linux.getauxval(elf.AT_EUID), linux.geteuid());
109 expectEqual(linux.getauxval(elf.AT_EGID), linux.getegid());
110}