From 536d35eb15af9a747bb8f427ca6e6d339f7d062c Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 7 Oct 2021 15:59:16 -0400 Subject: [PATCH] make tests ignore root more robustly (#42533) --- base/libc.jl | 27 +++++++++++++++------------ src/sys.c | 24 +++++++++++++++++++++--- test/file.jl | 5 ++++- test/read.jl | 4 ++-- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/base/libc.jl b/base/libc.jl index 98d2910917ee4..8cce4ce2a259b 100644 --- a/base/libc.jl +++ b/base/libc.jl @@ -404,36 +404,36 @@ srand(seed=floor(Int, time()) % Cuint) = ccall(:srand, Cvoid, (Cuint,), seed) struct Cpasswd username::Cstring - uid::Clong - gid::Clong + uid::Culong + gid::Culong shell::Cstring homedir::Cstring gecos::Cstring - Cpasswd() = new(C_NULL, -1, -1, C_NULL, C_NULL, C_NULL) + Cpasswd() = new(C_NULL, typemax(Culong), typemax(Culong), C_NULL, C_NULL, C_NULL) end mutable struct Cgroup - groupname::Cstring # group name - gid::Clong # group ID - mem::Ptr{Cstring} # group members - Cgroup() = new(C_NULL, -1, C_NULL) + groupname::Cstring # group name + gid::Culong # group ID + mem::Ptr{Cstring} # group members + Cgroup() = new(C_NULL, typemax(Culong), C_NULL) end struct Passwd username::String - uid::Int - gid::Int + uid::UInt + gid::UInt shell::String homedir::String gecos::String end struct Group groupname::String - gid::Int + gid::UInt mem::Vector{String} end function getpwuid(uid::Unsigned, throw_error::Bool=true) ref_pd = Ref(Cpasswd()) - ret = ccall(:jl_os_get_passwd, Cint, (Ref{Cpasswd}, UInt), ref_pd, uid) + ret = ccall(:jl_os_get_passwd, Cint, (Ref{Cpasswd}, Culong), ref_pd, uid) if ret != 0 throw_error && Base.uv_error("getpwuid", ret) return @@ -452,7 +452,7 @@ function getpwuid(uid::Unsigned, throw_error::Bool=true) end function getgrgid(gid::Unsigned, throw_error::Bool=true) ref_gp = Ref(Cgroup()) - ret = ccall(:jl_os_get_group, Cint, (Ref{Cgroup}, UInt), ref_gp, gid) + ret = ccall(:jl_os_get_group, Cint, (Ref{Cgroup}, Culong), ref_gp, gid) if ret != 0 throw_error && Base.uv_error("getgrgid", ret) return @@ -475,6 +475,9 @@ function getgrgid(gid::Unsigned, throw_error::Bool=true) return gp end +getuid() = ccall(:jl_getuid, Culong, ()) +geteuid() = ccall(:jl_geteuid, Culong, ()) + # Include dlopen()/dlpath() code include("libdl.jl") using .Libdl diff --git a/src/sys.c b/src/sys.c index 1b7d83f7d7f69..4a61b40da7193 100644 --- a/src/sys.c +++ b/src/sys.c @@ -229,7 +229,25 @@ JL_DLLEXPORT double jl_stat_ctime(char *statbuf) return (double)s->st_ctim.tv_sec + (double)s->st_ctim.tv_nsec * 1e-9; } -JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, size_t uid) +JL_DLLEXPORT unsigned long jl_getuid(void) +{ +#ifdef _OS_WINDOWS_ + return -1; +#else + return getuid(); +#endif +} + +JL_DLLEXPORT unsigned long jl_geteuid(void) +{ +#ifdef _OS_WINDOWS_ + return -1; +#else + return geteuid(); +#endif +} + +JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, unsigned long uid) { #ifdef _OS_WINDOWS_ return UV_ENOTSUP; @@ -342,11 +360,11 @@ JL_DLLEXPORT int jl_os_get_passwd(uv_passwd_t *pwd, size_t uid) typedef struct jl_group_s { char* groupname; - long gid; + unsigned long gid; char** members; } jl_group_t; -JL_DLLEXPORT int jl_os_get_group(jl_group_t *grp, size_t gid) +JL_DLLEXPORT int jl_os_get_group(jl_group_t *grp, unsigned long gid) { #ifdef _OS_WINDOWS_ return UV_ENOTSUP; diff --git a/test/file.jl b/test/file.jl index 3d300668aadf3..39c73eed390de 100644 --- a/test/file.jl +++ b/test/file.jl @@ -531,7 +531,10 @@ end if !Sys.iswindows() # chown will give an error if the user does not have permissions to change files - if get(ENV, "USER", "") == "root" || get(ENV, "HOME", "") == "/root" + uid = Libc.geteuid() + @test stat(file).uid == uid + @test uid == Libc.getuid() + if uid == 0 # root user chown(file, -2, -1) # Change the file owner to nobody @test stat(file).uid != 0 chown(file, 0, -2) # Change the file group to nogroup (and owner back to root) diff --git a/test/read.jl b/test/read.jl index 78ecded83c80a..81ee7fea21fba 100644 --- a/test/read.jl +++ b/test/read.jl @@ -461,7 +461,7 @@ rm(f) io = Base.Filesystem.open(f, Base.Filesystem.JL_O_WRONLY | Base.Filesystem.JL_O_CREAT | Base.Filesystem.JL_O_EXCL, 0o000) @test write(io, "abc") == 3 close(io) -if !Sys.iswindows() && get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root" +if !Sys.iswindows() && Libc.geteuid() != 0 # root user # msvcrt _wchmod documentation states that all files are readable, # so we don't test that it correctly set the umask on windows @test_throws SystemError open(f) @@ -511,7 +511,7 @@ close(f1) close(f2) @test eof(f1) @test_throws Base.IOError eof(f2) -if get(ENV, "USER", "") != "root" && get(ENV, "HOME", "") != "/root" +if Libc.geteuid() != 0 # root user @test_throws SystemError open(f, "r+") @test_throws Base.IOError Base.Filesystem.open(f, Base.Filesystem.JL_O_RDWR) else