UID/GID lookup fixes
cover UID/GID lookup fixes
Some LTP tests assume that certain user/group names exist on all systems. That is not true and those tests fail as a result.
This patchset replaces lookups of specific named users/groups with either some unused group ID or any existing user/group IDs, depending on test scenario. There are more tests with the same issue but they also require porting to the new LTP API. I'll submit those later one by one.
Martin Doucha (7):
chmod05, fchmod05: Use free GID instead of "bin" group Simplify syscalls/mkdir02
Add user/group ID lookup helper functions syscalls/kill05: Use any two unprivileged users syscalls/mkdir04: Simplify test and use any existing users syscalls/setregid02-04: Simplify GID handling syscalls/setregid02-04: Eliminate named group lookups
include/tst_safe_macros.h | 6 ++ include/tst_uid.h | 17 +++ lib/tst_uid.c | 100 ++++++++++++++++++ testcases/kernel/syscalls/chmod/chmod05.c | 7 +- testcases/kernel/syscalls/fchmod/fchmod05.c | 7 +- testcases/kernel/syscalls/kill/kill05.c | 23 ++-- testcases/kernel/syscalls/mkdir/mkdir02.c | 77 +++++--------- testcases/kernel/syscalls/mkdir/mkdir04.c | 30 ++---- .../kernel/syscalls/setregid/setregid02.c | 75 ++++++------- .../kernel/syscalls/setregid/setregid03.c | 92 +++++++--------- .../kernel/syscalls/setregid/setregid04.c | 58 +++++----- 11 files changed, 279 insertions(+), 213 deletions(-)
Signature · 1 line
2.33.0
PATCH 1/7 chmod05, fchmod05: Use free GID instead of "bin" group
The bin user group does not exist on some systems which will cause test failure. Set test directory ownership to any unused GID instead.
--- testcases/kernel/syscalls/chmod/chmod05.c | 7 ++++--- testcases/kernel/syscalls/fchmod/fchmod05.c | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/chmod/chmod05.c +4 -3
@@ -37,6 +37,7 @@#include <pwd.h> #include "tst_test.h" +#include "tst_uid.h" #define MODE_RWX (mode_t)(S_IRWXU | S_IRWXG | S_IRWXO)#define DIR_MODE (mode_t)(S_ISVTX | S_ISGID | S_IFDIR)
@@ -69,10 +70,10 @@ static void test_chmod(void)static void setup (void ) {struct passwd *nobody_u; -struct group *bin_gr; +gid_t free_gid; nobody_u =SAFE_GETPWNAM ("nobody" ); - bin_gr =SAFE_GETGRNAM ("bin" ); + free_gid =tst_get_free_gid (nobody_u->pw_gid);/* * Create a test directory under temporary directory with specified
@@ -83,7 +84,7 @@ static void setup(void)if (setgroups (1 , &nobody_u->pw_gid) ==-1 )tst_brk (TBROK |TERRNO ,"setgroups to nobody's gid failed" ); -SAFE_CHOWN (TESTDIR , nobody_u->pw_uid, bin_gr->gr_gid); +SAFE_CHOWN (TESTDIR , nobody_u->pw_uid, free_gid);/* change to nobody:nobody */ SAFE_SETEGID (nobody_u->pw_gid);
testcases/kernel/syscalls/fchmod/fchmod05.c +4 -3
@@ -20,6 +20,7 @@#include <errno.h> #include "tst_test.h" +#include "tst_uid.h" #include "fchmod.h" #define PERMS_DIR 043777
@@ -50,10 +51,10 @@ static void verify_fchmod(void)static void setup (void ) {struct passwd *ltpuser; -struct group *ltpgroup; +gid_t free_gid; ltpuser =SAFE_GETPWNAM ("nobody" ); - ltpgroup =SAFE_GETGRNAM ("bin" ); + free_gid =tst_get_free_gid (ltpuser->pw_gid);SAFE_MKDIR (TESTDIR ,DIR_MODE );
@@ -62,7 +63,7 @@ static void setup(void) tst_strerrno(TST_ERR)); } -SAFE_CHOWN (TESTDIR , ltpuser->pw_uid, ltpgroup->gr_gid); +SAFE_CHOWN (TESTDIR , ltpuser->pw_uid, free_gid);SAFE_SETEGID (ltpuser->pw_gid);SAFE_SETEUID (ltpuser->pw_uid);
Signature · 1 line
2.33.0
PATCH 1/7 chmod05, fchmod05: Use free GID instead of "bin" group
Hi!
Pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 2/7 Simplify syscalls/mkdir02
In reply to Martin Douchamkdir02 does a lot of unnecessary fork()ing left over from the old version. The same test can be done with just user "nobody", one unused GID and no fork()ing at all.
--- testcases/kernel/syscalls/mkdir/mkdir02.c | 77 +++++++---------------- 1 file changed, 24 insertions(+), 53 deletions(-)
testcases/kernel/syscalls/mkdir/mkdir02.c +24 -53
@@ -9,85 +9,57 @@* bit is set in the parent directory. */ -#include <errno.h> #include <sys/stat.h> #include <sys/types.h> #include <pwd.h> -#include <sys/wait.h> -#include <unistd.h> -#include <stdlib.h> #include "tst_test.h" +#include "tst_uid.h" #define TESTDIR1 "testdir1"#define TESTDIR2 "testdir1/testdir2" -static uid_t nobody_uid, bin_uid; -static gid_t nobody_gid, bin_gid; +static gid_t free_gid;static void verify_mkdir (void ) { -struct stat buf1, buf2; -pid_t pid; +struct stat statbuf;int fail =0 ; - pid =SAFE_FORK (); -if (pid ==0 ) { -SAFE_SETREGID (bin_gid, bin_gid); -SAFE_SETREUID (bin_uid, bin_uid); -SAFE_MKDIR (TESTDIR2 ,0777 ); +SAFE_MKDIR (TESTDIR2 ,0777 ); +SAFE_STAT (TESTDIR2 , &statbuf); -SAFE_STAT (TESTDIR2 , &buf2); -SAFE_STAT (TESTDIR1 , &buf1); - -if (buf2.st_gid != buf1.st_gid) { -tst_res (TFAIL , -"New dir FAILED to inherit GID have %d expected %d" , - buf2.st_gid, buf1.st_gid); - fail =1 ; - } - -if (!(buf2.st_mode &S_ISGID )) { -tst_res (TFAIL ,"New dir FAILED to inherit S_ISGID" ); - fail =1 ; - } - -if (!fail) -tst_res (TPASS ,"New dir inherited GID and S_ISGID" ); +if (statbuf.st_gid != free_gid) { +tst_res (TFAIL , +"New dir FAILED to inherit GID: has %d, expected %d" , + statbuf.st_gid, free_gid); + fail =1 ; + } -exit (0 ); +if (!(statbuf.st_mode &S_ISGID )) { +tst_res (TFAIL ,"New dir FAILED to inherit S_ISGID" ); + fail =1 ; } -tst_reap_children (); +if (!fail) +tst_res (TPASS ,"New dir inherited GID and S_ISGID" ); +SAFE_RMDIR (TESTDIR2 ); }static void setup (void ) { -struct passwd *pw; -struct stat buf; -pid_t pid; +struct passwd *pw =SAFE_GETPWNAM ("nobody" ); - pw =SAFE_GETPWNAM ("nobody" ); - nobody_uid = pw->pw_uid; - nobody_gid = pw->pw_gid; - pw =SAFE_GETPWNAM ("bin" ); - bin_uid = pw->pw_uid; - bin_gid = pw->pw_gid; + free_gid =tst_get_free_gid (pw->pw_gid);umask (0 ); +SAFE_MKDIR (TESTDIR1 ,0777 ); +SAFE_CHMOD (TESTDIR1 ,0777 |S_ISGID ); +SAFE_CHOWN (TESTDIR1 ,getuid (), free_gid); - pid =SAFE_FORK (); -if (pid ==0 ) { -SAFE_SETREGID (nobody_gid, nobody_gid); -SAFE_SETREUID (nobody_uid, nobody_uid); -SAFE_MKDIR (TESTDIR1 ,0777 ); -SAFE_STAT (TESTDIR1 , &buf); -SAFE_CHMOD (TESTDIR1 , buf.st_mode |S_ISGID ); -exit (0 ); - } - -tst_reap_children (); +SAFE_SETREGID (pw->pw_gid, pw->pw_gid); +SAFE_SETREUID (pw->pw_uid, pw->pw_uid); }static struct tst_test test = {
@@ -95,5 +67,4 @@ static struct tst_test test = { .needs_tmpdir = 1, .needs_root = 1, .test_all = verify_mkdir, - .forks_child = 1, };
Signature · 1 line
2.33.0
PATCH 2/7 Simplify syscalls/mkdir02
Hi!
Pushed, thanks.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 3/7 Add user/group ID lookup helper functions
In reply to Martin Doucha--- include/tst_safe_macros.h | 6 +++ include/tst_uid.h | 17 +++++++ lib/tst_uid.c | 100 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+)
include/tst_safe_macros.h +6 -0
@@ -625,4 +625,10 @@ int safe_sysinfo(const char *file, const int lineno, struct sysinfo *info);#define SAFE_SYSINFO (info) \ safe_sysinfo(__FILE__, __LINE__, (info)) +struct passwd *safe_getpwent (const char *file,const int lineno); +#define SAFE_GETPWENT () safe_getpwent(__FILE__, __LINE__) + +struct group *safe_getgrent (const char *file,const int lineno); +#define SAFE_GETGRENT () safe_getgrent(__FILE__, __LINE__) +#endif /* SAFE_MACROS_H__ */
include/tst_uid.h +17 -0
@@ -15,4 +15,21 @@gid_t tst_get_free_gid_ (const char *file,const int lineno,gid_t skip);#define tst_get_free_gid (skip) tst_get_free_gid_(__FILE__, __LINE__, (skip)) +/* +* Get a specific number of unique existing non-root user or group IDs. +* Multiple calls will return additional results, if any. Both functions call +* SAFE_GETPWENT() and SAFE_GETGRENT() respectively. +* Call endpwent()/endgrent() after the last use of these functions. +* Call setpwent()/setgrent() to read user/group IDs from the beginning again. +*/ +int tst_get_uids_ (const char *file,const int lineno,unsigned int count, +uid_t *buf); +#define tst_get_uids (count, buf) \ + tst_get_uids_(__FILE__, __LINE__, (count), (buf)) + +int tst_get_gids_ (const char *file,const int lineno,unsigned int count, +gid_t *buf); +#define tst_get_gids (count, buf) \ + tst_get_gids_(__FILE__, __LINE__, (count), (buf)) +#endif /* TST_UID_H_ */
lib/tst_uid.c +100 -0
111 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH 3/7 Add user/group ID lookup helper functions
Hi!
Quoted 25 lines
lib/tst_uid.c +8
-0@@ -3,8 +3,10 @@* Copyright (c) 2021 Linux Test Project */ +#define _XOPEN_SOURCE 500#include <sys/types.h> #include <grp.h> +#include <pwd.h> #include <errno.h> #define TST_NO_DEFAULT_MAIN @@ -36,3 +38,101 @@ gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip)tst_brk_ (file, lineno,TBROK ,"No free group ID found" );return (gid_t)-1 ; } + +struct passwd *safe_getpwent (const char *file,const int lineno) +{ +struct passwd *ret; + + errno =0 ;
lib/tst_uid.c gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip) · line 56
Quoted 10 lines
lib/tst_uid.c +10
-0+ ret =getpwent (); + +if (!ret) { +if (errno) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"getpwent() failed" ); + }else { +tst_brk_ (file, lineno,TBROK , +"getpwent() failed: end of file" ); + }
I'm not sure if "no more users in the /etc/passwd" should be reported as an error.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 3/7 Add user/group ID lookup helper functions
Quoted 12 lines · Cyril Hrubis
Hi!
lib/tst_uid.c gid_t tst_get_free_gid_(const char *file, const int lineno, gid_t skip) · line 56
Quoted 8 lines
lib/tst_uid.c +8
-0+if (!ret) { +if (errno) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"getpwent() failed" ); + }else { +tst_brk_ (file, lineno,TBROK , +"getpwent() failed: end of file" ); + }I'm not sure if "no more users in the
/etc/passwd" should be reported as an error.
The alternative would be that pretty much every test that calls SAFE_GETPWENT() in setup() will need to check for NULL and explicitly call tst_brk(). I don't see any use for allowing SAFE_GETPWENT() to return NULL outside cleanup().
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
In reply to Martin DouchaUsernames such as "bin" may not exist on some systems. Find and use any two unprivileged user IDs for the test instead of specific named users.
Includes minor code style cleanup in wait_for_flag().
--- testcases/kernel/syscalls/kill/kill05.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
testcases/kernel/syscalls/kill/kill05.c +10 -13
@@ -17,15 +17,18 @@* This test must be run as root. */ +#define _XOPEN_SOURCE 500#include <sys/wait.h> +#include <sys/types.h> #include <pwd.h> #include <stdlib.h> #include "tst_test.h" #include "libnewipc.h" #include "tst_safe_sysv_ipc.h" #include "tst_safe_macros.h" +#include "tst_uid.h" -static uid_t nobody_uid, bin_uid; +static uid_t test_users[2 ];static int *flag;static int shm_id =-1 ;static key_t shm_key;
@@ -35,8 +38,8 @@ static void wait_for_flag(int value)while (1 ) {if (*flag == value)break ; -else -usleep (100 ); + +usleep (100 ); } }
@@ -47,14 +50,14 @@ static void do_master_child(void) *flag =0 ; pid1 =SAFE_FORK ();if (pid1 ==0 ) { -SAFE_SETREUID (nobody_uid, nobody_uid); +SAFE_SETREUID (test_users[0 ], test_users[0 ]); *flag =1 ;wait_for_flag (2 );exit (0 ); } -SAFE_SETREUID (bin_uid, bin_uid); +SAFE_SETREUID (test_users[1 ], test_users[1 ]);wait_for_flag (1 );TEST (kill (pid1,SIGKILL ));
@@ -85,17 +88,11 @@ static void verify_kill(void)static void setup (void ) { -struct passwd *pw; - shm_key =GETIPCKEY (); shm_id =SAFE_SHMGET (shm_key,getpagesize (),0666 |IPC_CREAT ); flag =SAFE_SHMAT (shm_id,0 ,0 ); - - pw =SAFE_GETPWNAM ("nobody" ); - nobody_uid = pw->pw_uid; - - pw =SAFE_GETPWNAM ("bin" ); - bin_uid = pw->pw_uid; +tst_get_uids (2 , test_users); +endpwent (); }static void cleanup (void )
Signature · 1 line
2.33.0
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
Hi!
Quoted 2 lines
Usernames such as "bin" may not exist on some systems. Find and use any two unprivileged user IDs for the test instead of specific named users.
I do wonder if we have to lookup the UIDs in this case. As long as I understand the test correctly we are testing that no signals are send to a process if the sender real of effective UID do not match either of real and saved set UIDs of the receiving process. If my understanding is correct we can just take two arbitrary UIDs that != 0 and the test should work just fine.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
Quoted 10 lines · Cyril Hrubis
Hi!
Quoted 2 lines
Usernames such as "bin" may not exist on some systems. Find and use any two unprivileged user IDs for the test instead of specific named users.
I do wonder if we have to lookup the UIDs in this case. As long as I understand the test correctly we are testing that no signals are send to a process if the sender real of effective UID do not match either of real and saved set UIDs of the receiving process. If my understanding is correct we can just take two arbitrary UIDs that != 0 and the test should work just fine.
We could just use UID 1 and 2 for this test but I'd rather not assume that set*uid() functions allow setting unused UIDs. Even if it works now, it could easily break in the future or on some special system auth backends.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
Hi!
Quoted 11 lines
Quoted 6 lines
I do wonder if we have to lookup the UIDs in this case. As long as I understand the test correctly we are testing that no signals are send to a process if the sender real of effective UID do not match either of real and saved set UIDs of the receiving process. If my understanding is correct we can just take two arbitrary UIDs that != 0 and the test should work just fine.
We could just use UID 1 and 2 for this test but I'd rather not assume that set*uid() functions allow setting unused UIDs. Even if it works now, it could easily break in the future or on some special system auth backends.
Don't we already depend on being able to use unused GID? How is this different?
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
Quoted 8 lines · Cyril Hrubis
Hi!
Quoted 7 lines
Quoted 6 lines
I do wonder if we have to lookup the UIDs in this case. As long as I understand the test correctly we are testing that no signals are send to a process if the sender real of effective UID do not match either of real and saved set UIDs of the receiving process. If my understanding is correct we can just take two arbitrary UIDs that != 0 and the test should work just fine.
Quoted 7 lines
Quoted 4 lines
We could just use UID 1 and 2 for this test but I'd rather not assume that set*uid() functions allow setting unused UIDs. Even if it works now, it could easily break in the future or on some special system auth backends.
Don't we already depend on being able to use unused GID? How is this different?
We only set unused user/group IDs on files and directories using chown(). That's perfectly safe because inode owner is just an arbitrary number that doesn't need to exist as a real entry in the user/group database. You can get the same result by creating the files under a valid owner and then deleting the respective user/group so the system has to be able to handle that situation.
But setting a non-existent UID/GID on a running process is different because the kernel might try to look up some additional data in the user/group database and block the credentials change if the lookup fails.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
Hi!
Quoted 3 lines
But setting a non-existent UID/GID on a running process is different because the kernel might try to look up some additional data in the user/group database and block the credentials change if the lookup fails.
No it will not, kernel does not care and never cared about system users and groups, that's purely userspace concept. The login(1) process that sets up all the process IDS as well as shell variables.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
Quoted 8 lines · Cyril Hrubis
Hi!
Quoted 3 lines
But setting a non-existent UID/GID on a running process is different because the kernel might try to look up some additional data in the user/group database and block the credentials change if the lookup fails.
No it will not, kernel does not care and never cared about system users and groups, that's purely userspace concept. The login(1) process that sets up all the process IDS as well as shell variables.
The man pages give no guarantee that it'll stay that way. I'd rather play it safe with process credentials. From test setup perspective, it's still less work than the old lookup of specific usernames.
Signature · 7 lines
Martin Doucha [email protected] QA Engineer for Software Maintenance SUSE LINUX, s.r.o. CORSO IIa Krizikova 148/34 186 00 Prague 8 Czech Republic
PATCH 4/7 syscalls/kill05: Use any two unprivileged users
In reply to Cyril HrubisHi!
Quoted 7 lines
Quoted 4 lines
We could just use UID 1 and 2 for this test but I'd rather not assume that set*uid() functions allow setting unused UIDs. Even if it works now, it could easily break in the future or on some special system auth backends.
Don't we already depend on being able to use unused GID? How is this different?
Hmm we pass the unused GID mostly to chown(), but still.
The Linux manual page specify that setuid() may fail to change if the UID is not valid inside a namespace, that means that either there is no UID map or particular UID is missing from the UID map. So reading /etc/passwd hypotetically help if we are inside of a thight container and /etc/passwd matches the UID map, but I'm not sure if this is worth of the effort.
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 5/7 syscalls/mkdir04: Simplify test and use any existing users
In reply to Martin Douchamkdir04 does not need to fork() in setup(). Usernames such as "bin" may not exist on some systems. Find and use any two unprivileged users instead of specific named users.
--- testcases/kernel/syscalls/mkdir/mkdir04.c | 30 ++++++----------------- 1 file changed, 8 insertions(+), 22 deletions(-)
testcases/kernel/syscalls/mkdir/mkdir04.c +8 -22
@@ -7,20 +7,16 @@* user with restrictive permissions and that the errno is set to EACCESS. */ +#define _XOPEN_SOURCE 500#include <errno.h> -#include <sys/stat.h> #include <sys/types.h> #include <pwd.h> -#include <sys/wait.h> -#include <unistd.h> -#include <stdlib.h> #include "tst_test.h" +#include "tst_uid.h" #define TESTDIR "testdir"#define TESTSUBDIR "testdir/testdir" -static uid_t nobody_uid, bin_uid; -static void verify_mkdir (void ) {if (mkdir (TESTSUBDIR ,0777 ) !=-1 ) {
@@ -39,24 +35,15 @@ static void verify_mkdir(void)static void setup (void ) { -struct passwd *pw; -pid_t pid; - - pw =SAFE_GETPWNAM ("nobody" ); - nobody_uid = pw->pw_uid; - pw =SAFE_GETPWNAM ("bin" ); - bin_uid = pw->pw_uid; +uid_t test_users[2 ]; - pid =SAFE_FORK (); -if (pid ==0 ) { -SAFE_SETREUID (nobody_uid, nobody_uid); -SAFE_MKDIR (TESTDIR ,0700 ); -exit (0 ); - } +tst_get_uids (2 , test_users); +endpwent (); -tst_reap_children (); +SAFE_MKDIR (TESTDIR ,0700 ); +SAFE_CHOWN (TESTDIR , test_users[0 ],getgid ()); -SAFE_SETREUID (bin_uid, bin_uid); +SAFE_SETREUID (test_users[1 ], test_users[1 ]); }static struct tst_test test = {
@@ -64,5 +51,4 @@ static struct tst_test test = { .needs_tmpdir = 1, .needs_root = 1, .setup = setup, - .forks_child = 1, };
Signature · 1 line
2.33.0
PATCH 6/7 syscalls/setregid02-04: Simplify GID handling
In reply to Martin Doucha--- .../kernel/syscalls/setregid/setregid02.c | 48 +++++++--------- .../kernel/syscalls/setregid/setregid03.c | 57 ++++++++----------- .../kernel/syscalls/setregid/setregid04.c | 42 +++++++------- 3 files changed, 61 insertions(+), 86 deletions(-)
testcases/kernel/syscalls/setregid/setregid02.c +19 -29
@@ -19,12 +19,10 @@#include "tst_test.h" #include "compat_tst_16.h" -static gid_t neg_one =-1 ; +static gid_t root_gid, nobody_gid, other_gid, neg_one =-1 ;static struct passwd *ltpuser; -static struct group ltpgroup, root, bin; -/* * The following structure contains all test data. Each structure in the array * is used for a separate test. The tests are executed in the for loop below.
@@ -34,50 +32,41 @@ static struct tcase {gid_t *real_gid;gid_t *eff_gid;int exp_errno; -struct group *exp_real_usr; -struct group *exp_eff_usr; +gid_t *exp_real_usr; +gid_t *exp_eff_usr;char *test_msg; } tcases[] = { { - &neg_one, &root.gr_gid, EPERM, <pgroup, <pgroup, + &neg_one, &root_gid, EPERM, &nobody_gid, &nobody_gid, "After setregid(-1, root),"}, { - &neg_one, &bin.gr_gid, EPERM, <pgroup, <pgroup, + &neg_one, &other_gid, EPERM, &nobody_gid, &nobody_gid, "After setregid(-1, bin)"}, { - &root.gr_gid, &neg_one, EPERM, <pgroup, <pgroup, + &root_gid, &neg_one, EPERM, &nobody_gid, &nobody_gid, "After setregid(root,-1),"}, { - &bin.gr_gid, &neg_one, EPERM, <pgroup, <pgroup, + &other_gid, &neg_one, EPERM, &nobody_gid, &nobody_gid, "After setregid(bin, -1),"}, { - &root.gr_gid, &bin.gr_gid, EPERM, <pgroup, <pgroup, + &root_gid, &other_gid, EPERM, &nobody_gid, &nobody_gid, "After setregid(root, bin)"}, { - &bin.gr_gid, &root.gr_gid, EPERM, <pgroup, <pgroup, + &other_gid, &root_gid, EPERM, &nobody_gid, &nobody_gid, "After setregid(bin, root),"} }; -static struct group get_group_by_name(const char *name) +static gid_t get_group_by_name(const char *name) { struct group *ret = SAFE_GETGRNAM(name); GID16_CHECK(ret->gr_gid, setregid); - return *ret; -} - -static struct group get_group_by_gid(gid_t gid) -{ - struct group *ret = SAFE_GETGRGID(gid); - - GID16_CHECK(ret->gr_gid, setregid); - - return *ret; + return ret->gr_gid; } -void gid_verify(struct group *rg, struct group *eg, char *when) +void gid_verify(gid_t rg, gid_t eg, char *when) { - if ((getgid() != rg->gr_gid) || (getegid() != eg->gr_gid)) { + if ((getgid() != rg) || (getegid() != eg)) { tst_res(TFAIL, "ERROR: %s real gid = %d; effective gid = %d", when, getgid(), getegid()); tst_res(TINFO, "Expected: real gid = %d; effective gid = %d", - rg->gr_gid, eg->gr_gid); + rg, eg); return; }
@@ -107,7 +96,7 @@ static void run(unsigned int n)"setregid(%d, %d) did not fail (ret: %ld) as expected (ret: -1)." , *tc->real_gid, *tc->eff_gid, TST_RET); } -gid_verify (tc->exp_real_usr, tc->exp_eff_usr, tc->test_msg); +gid_verify (*tc->exp_real_usr, *tc->exp_eff_usr, tc->test_msg); }static void setup (void )
@@ -117,9 +106,10 @@ static void setup(void)SAFE_SETGID (ltpuser->pw_gid);SAFE_SETUID (ltpuser->pw_uid); - root =get_group_by_name ("root" ); - ltpgroup =get_group_by_gid (ltpuser->pw_gid); - bin =get_group_by_name ("bin" ); + nobody_gid = ltpuser->pw_gid; +GID16_CHECK (nobody_gid, setregid); + root_gid =get_group_by_name ("root" ); + other_gid =get_group_by_name ("bin" ); }static struct tst_test test = {
testcases/kernel/syscalls/setregid/setregid03.c +23 -34
115 lines not on this page · raw message
testcases/kernel/syscalls/setregid/setregid04.c +19 -23
89 lines not on this page · raw message
Signature · 1 line
2.33.0
PATCH 7/7 syscalls/setregid02-04: Eliminate named group lookups
In reply to Martin DouchaGroup names like "bin" or "daemon" may not exist on some systems. Find and use any unprivieged group IDs instead of specific named groups.
--- .../kernel/syscalls/setregid/setregid02.c | 39 +++++++++-------- .../kernel/syscalls/setregid/setregid03.c | 43 +++++++++---------- .../kernel/syscalls/setregid/setregid04.c | 22 +++++++--- 3 files changed, 58 insertions(+), 46 deletions(-)
testcases/kernel/syscalls/setregid/setregid02.c +21 -18
@@ -17,6 +17,7 @@#include <stdlib.h> #include "tst_test.h" +#include "tst_uid.h" #include "compat_tst_16.h" static gid_t root_gid, nobody_gid, other_gid, neg_one =-1 ;
@@ -40,26 +41,17 @@ static struct tcase { &neg_one, &root_gid, EPERM, &nobody_gid, &nobody_gid, "After setregid(-1, root),"}, { &neg_one, &other_gid,EPERM , &nobody_gid, &nobody_gid, - "After setregid(-1, bin)"}, { + "After setregid(-1, other)"}, { &root_gid, &neg_one,EPERM , &nobody_gid, &nobody_gid, "After setregid(root,-1),"}, { &other_gid, &neg_one,EPERM , &nobody_gid, &nobody_gid, - "After setregid(bin, -1),"}, { + "After setregid(other, -1),"}, { &root_gid, &other_gid,EPERM , &nobody_gid, &nobody_gid, - "After setregid(root, bin)"}, { + "After setregid(root, other)"}, { &other_gid, &root_gid,EPERM , &nobody_gid, &nobody_gid, - "After setregid(bin, root),"} + "After setregid(other, root),"} }; -static gid_t get_group_by_name (const char *name) -{ -struct group *ret =SAFE_GETGRNAM (name); - -GID16_CHECK (ret->gr_gid, setregid); - -return ret->gr_gid; -} -void gid_verify (gid_t rg,gid_t eg,char *when) {if ((getgid () != rg) || (getegid () != eg)) {
@@ -101,15 +93,26 @@ static void run(unsigned int n)static void setup (void ) { +struct group *grp; + ltpuser =SAFE_GETPWNAM ("nobody" ); + nobody_gid = ltpuser->pw_gid; + root_gid =getgid (); +setgrent (); -SAFE_SETGID (ltpuser->pw_gid); -SAFE_SETUID (ltpuser->pw_uid); +do { + grp =SAFE_GETGRENT (); + }while (grp->gr_gid == root_gid || grp->gr_gid == nobody_gid); - nobody_gid = ltpuser->pw_gid; + other_gid = grp->gr_gid; +endgrent (); + +GID16_CHECK (root_gid, setregid);GID16_CHECK (nobody_gid, setregid); - root_gid =get_group_by_name ("root" ); - other_gid =get_group_by_name ("bin" ); +GID16_CHECK (other_gid, setregid); + +SAFE_SETGID (ltpuser->pw_gid); +SAFE_SETUID (ltpuser->pw_uid); }static struct tst_test test = {
testcases/kernel/syscalls/setregid/setregid03.c +21 -22
74 lines not on this page · raw message
testcases/kernel/syscalls/setregid/setregid04.c +16 -6
@@ -9,7 +9,11 @@* Test setregid() when executed by root. */ +#include <sys/types.h> +#include <grp.h> +#include "tst_test.h" +#include "tst_uid.h" #include "compat_tst_16.h" static gid_t first_gid, second_gid, root_gid, neg_one =-1 ;
@@ -30,7 +34,7 @@ struct test_data_t { &root_gid, &root_gid, &root_gid, &root_gid, "After setregid(root, root),"}, { &first_gid, &neg_one, &first_gid, &root_gid, - "After setregid(nobody, -1)"}, { + "After setregid(first, -1)"}, { &root_gid, &neg_one, &root_gid, &root_gid, "After setregid(root,-1),"}, { &neg_one, &neg_one, &root_gid, &root_gid,
@@ -40,11 +44,11 @@ struct test_data_t { &root_gid, &neg_one, &root_gid, &root_gid, "After setregid(root, -1),"}, { &second_gid, &first_gid, &second_gid, &first_gid, - "After setregid(daemon, nobody)"}, { + "After setregid(second, first)"}, { &neg_one, &neg_one, &second_gid, &first_gid, "After setregid(-1, -1)"}, { &neg_one, &first_gid, &second_gid, &first_gid, - "After setregid(-1, nobody)"} + "After setregid(-1, first)"} };static void gid_verify (gid_t rg,gid_t eg,const char *when)
@@ -77,9 +81,15 @@ static void run(unsigned int i)static void setup (void ) { - root_gid =SAFE_GETGRNAM ("root" )->gr_gid; - first_gid =SAFE_GETGRNAM_FALLBACK ("nobody" ,"nogroup" )->gr_gid; - second_gid =SAFE_GETGRNAM ("daemon" )->gr_gid; +gid_t groups[3 ]; + +tst_get_gids (3 , groups); + root_gid =getgid (); + +/* At most one of the groups may be equal to root_gid */ + first_gid = groups[0 ] != root_gid ? groups[0 ] : groups[2 ]; + second_gid = groups[1 ] != root_gid ? groups[1 ] : groups[2 ]; +endgrent (); }static struct tst_test test = {
Signature · 1 line
2.33.0