syscalls/setresgid03: Convert to new API
PATCH v2 1/4 syscalls/setresgid03: Convert to new API
The original test needs user "bin" which may not exist on some systems. Use any non-root ID instead.
---
Changes since v1:
- use TST_EXP_FAIL() to simplify result validation
.../kernel/syscalls/setresgid/setresgid03.c | 264 ++++-------------- 1 file changed, 56 insertions(+), 208 deletions(-)
testcases/kernel/syscalls/setresgid/setresgid03.c +56 -208
@@ -1,233 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of version 2 of the GNU General Public License as -* published by the Free Software Foundation. -* -* This program is distributed in the hope that it would be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* -* You should have received a copy of the GNU General Public License along -* with this program; if not, write the Free Software Foundation, Inc., -* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -* +* AUTHOR: Madhu T L <[email protected]> +* Copyright (C) 2021 SUSE LLC <[email protected]> */ -/********************************************************** -* -* TEST IDENTIFIER : setresgid03 -* -* EXECUTED BY : root / superuser -* -* TEST TITLE : Checking error conditions for setresgid(2) -* -* TEST CASE TOTAL : 4 -* -* AUTHOR : Madhu T L <[email protected]> -* -* SIGNALS -* Uses SIGUSR1 to pause before test if option set. -* (See the parse_opts(3) man page). -* -* DESCRIPTION -* Verify that, -* 1. setresgid(2) fails with EPERM for unprivileged user in setting -* saved group id. -* 2. setresgid(2) fails with EPERM for unprivileged user in setting -* effective group id. -* 3. setresgid(2) fails with EPERM for unprivileged user in setting -* real group id. -* 4. setresgid(2) fails with EPERM for unprivileged user in setting -* real/effective/saved group id. -* -* Setup: -* Setup signal handling. -* Test caller is superuser -* Check existence of user id's root/bin/nobody -* Set real/effective/saved gid to nobody -* Set effective uid to nobody -* Pause for SIGUSR1 if option specified. -* -* Test: -* Loop if the proper options are given. -* Execute system call -* Check return value, errno and functionality, if success, -* Issue PASS message -* Otherwise, -* Issue FAIL message -* -* Cleanup: -* Print errno log and/or timing stats if options given -* -* USAGE: <for command-line> -* setresgid03 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -f : Turn off functional testing -* -h : Show help screen -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -p : Pause for SIGUSR1 before starting -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. + +/*\ +* [Description] * -****************************************************************/ +* Verify that setresgid() fails with EPERM if unprivileged user tries to set +* process group ID which requires higher permissions. +*/ -#define _GNU_SOURCE 1 -#include <errno.h> -#include <pwd.h> #include <sys/types.h> -#include <unistd.h> -#include "test.h" -#include "safe_macros.h" -#include "compat_16.h" +#include <pwd.h> -#define EXP_RET_VAL -1 -#define EXP_ERRNO EPERM -#define TEST_DESC "unprivileged user" +#include "tst_test.h" +#include "tst_uid.h" +#include "compat_tst_16.h" struct test_case_t {/* test case structure */ -uid_t *rgid;/* real GID */ -uid_t *egid;/* effective GID */ -uid_t *sgid;/* saved GID */ -struct passwd *exp_rgid;/* Expected real GID */ -struct passwd *exp_egid;/* Expected effective GID */ -struct passwd *exp_sgid;/* Expected saved GID */ +gid_t *rgid;/* real GID */ +gid_t *egid;/* effective GID */ +gid_t *sgid;/* saved GID */ +gid_t *exp_rgid;/* Expected real GID */ +gid_t *exp_egid;/* Expected effective GID */ +gid_t *exp_sgid;/* Expected saved GID */ +char *desc;/* Test description */ }; -TCID_DEFINE (setresgid03); -static int testno; -static struct passwd nobody, bin, root; -static uid_t nobody_gid, bin_gid, neg =-1 ; - -static int test_functionality (uid_t ,uid_t ,uid_t ); -static void setup (void ); -static void cleanup (void ); - -static struct test_case_t tdat[] = { - {&neg, &neg, &bin.pw_gid, &nobody, &nobody, &nobody}, - {&neg, &bin.pw_gid, &neg, &nobody, &nobody, &nobody}, - {&bin.pw_gid, &neg, &neg, &nobody, &nobody, &nobody}, - {&bin.pw_gid, &bin.pw_gid, &bin.pw_gid, &nobody, &nobody, &nobody}, +static gid_t nobody_gid, other_gid, neg =-1 ; + +static struct test_case_t test_cases[] = { + {&neg, &neg, &other_gid, &nobody_gid, &nobody_gid, &nobody_gid, +"setresgid(-1, -1, other)" }, + {&neg, &other_gid, &neg, &nobody_gid, &nobody_gid, &nobody_gid, +"setresgid(-1, other, -1)" }, + {&other_gid, &neg, &neg, &nobody_gid, &nobody_gid, &nobody_gid, +"setresgid(other, -1, -1)" }, + {&other_gid, &other_gid, &other_gid, &nobody_gid, &nobody_gid, + &nobody_gid,"setresgid(other, other, other)" }, }; -int TST_TOTAL =sizeof (tdat) /sizeof (tdat[0 ]); - -int main (int argc,char **argv) +static void setup (void ) { -int lc; - -tst_parse_opts (argc, argv,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { -/* reset tst_count in case we are looping */ - tst_count =0 ; +gid_t test_groups[2 ]; +struct passwd *pw =SAFE_GETPWNAM ("nobody" ); -for (testno =0 ; testno <TST_TOTAL ; ++testno) { + nobody_gid = test_groups[0 ] = pw->pw_gid; +tst_get_gids (test_groups,1 ,2 ); + other_gid = test_groups[1 ]; -TEST (SETRESGID (cleanup, *tdat[testno].rgid, *tdat[testno].egid, - *tdat[testno].sgid)); +GID16_CHECK (nobody_gid, setresgid); +GID16_CHECK (other_gid, setresgid); -if ((TEST_RETURN ==EXP_RET_VAL ) && - (TEST_ERRNO ==EXP_ERRNO )) { - -if (!test_functionality - (tdat[testno].exp_rgid->pw_gid, - tdat[testno].exp_egid->pw_gid, - tdat[testno].exp_sgid->pw_gid)) { - -tst_resm (TPASS ,"setresgid() failed as " -"expected for %s : errno %d" , -TEST_DESC ,TEST_ERRNO ); - }else { -tst_resm (TFAIL ,"Functionality test " -"for setresgid() for %s failed" , -TEST_DESC ); - } - - }else { -tst_resm (TFAIL ,"setresgid() returned " -"unexpected results for %s ; returned" -" %ld (expected %d), errno %d (expected" -" %d)" ,TEST_DESC , -TEST_RETURN ,EXP_RET_VAL ,TEST_ERRNO , -EXP_ERRNO ); - } - } - } -cleanup (); - -tst_exit (); -} - -static int test_functionality (uid_t exp_rgid,uid_t exp_egid,uid_t exp_sgid) -{ -uid_t cur_rgid, cur_egid, cur_sgid; - -/* Get current real, effective and saved group id */ -SAFE_GETRESGID (cleanup, &cur_rgid, &cur_egid, &cur_sgid); - -if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid) - && (cur_sgid == exp_sgid)) { -return 0 ; - } -return 1 ; +/* Set real/effective/saved gid to nobody */ +SAFE_SETRESGID (nobody_gid, nobody_gid, nobody_gid); +SAFE_SETUID (pw->pw_uid); } -/* -* setup() -* performs all ONE TIME setup for this test -*/ -void setup (void ) +static void run (unsigned int n) { -struct passwd *passwd_p; - -tst_require_root (); +const struct test_case_t *tc = test_cases + n; -tst_sig (NOFORK ,DEF_HANDLER , cleanup); +TST_EXP_FAIL (SETRESGID (*tc->rgid, *tc->egid, *tc->sgid),EPERM ,"%s" , + tc->desc); -if ((passwd_p =getpwnam ("root" )) ==NULL ) { -tst_brkm (TBROK ,NULL ,"getpwnam() failed for root" ); +if (!TST_PASS ) +return ; - } - root = *passwd_p; - -if ((passwd_p =getpwnam ("bin" )) ==NULL ) { -tst_brkm (TBROK ,NULL ,"bin user id doesn't exist" ); - - } - bin = *passwd_p; - GID16_CHECK((bin_gid = bin.pw_gid), "setresgid", cleanup) - -if ((passwd_p =getpwnam ("nobody" )) ==NULL ) { -tst_brkm (TBROK ,NULL ,"nobody user id doesn't exist" ); - - } - nobody = *passwd_p; -GID16_CHECK ((nobody_gid = nobody.pw_gid),"setresgid" , cleanup) - - /* Set real/effective/saved gid to nobody */ -if (setresgid (nobody_gid, nobody_gid, nobody_gid) ==-1 ) { -tst_brkm (TBROK ,NULL ,"setup() failed for setting while" -" setting real/effective/saved gid" ); - } -/* Set euid to nobody */ -SAFE_SETUID (NULL , nobody.pw_uid); -/* Pause if that option was specified -* TEST_PAUSE contains the code to fork the test with the -c option. -*/ -TEST_PAUSE ; +tst_check_resgid (tc->desc, *tc->exp_rgid, *tc->exp_egid, + *tc->exp_sgid); } -/* -* cleanup() -* performs all ONE TIME cleanup for this test at -* completion or premature exit -*/ -void cleanup (void ) -{ - -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (test_cases), + .setup = setup, + .needs_root =1 , +};
Signature · 1 line
2.33.0
PATCH v2 2/4 syscalls/setresuid01: Convert to new API
The original test needs user "bin" which may not exist on some systems. Use any non-root UID instead.
---
Changes since v1: None
.../kernel/syscalls/setresuid/setresuid01.c | 256 ++++-------------- 1 file changed, 59 insertions(+), 197 deletions(-)
testcases/kernel/syscalls/setresuid/setresuid01.c +59 -197
@@ -1,221 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-only /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2001 +* 07/2001 ported by John George +* Copyright (C) 2021 SUSE LLC <[email protected]> */ -/* -* NAME -* setresuid01.c +/*\ +* [Description] * -* DESCRIPTION -* Test setresuid() when executed by root. -* -* ALGORITHM -* -* Setup: -* Setup signal handling -* Get user information. -* Pause for SIGUSER1 if option specified. -* Setup test values. -* Loop if the proper options are given. -* For each test execute the system call -* Check return code, if system call failed (return=-1) -* Log the errno and Issue a FAIL message. -* Otherwise, -* Verify the Functionality of system call -* if successful, -* Issue Functionality-Pass message. -* Otherwise, -* Issue Functionality-Fail message. -* Cleanup: -* Print errno log and/or timing stats if options given. -* -* USAGE: <for command-line> -* setresuid01 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -f : Turn off functionality Testing. -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* History -* 07/2001 John George -* -Ported -* -* Restrictions -* This test must be ran as root. -* nobody and bin must be valid users. +* Test setresuid() when executed by root. */ -#define _GNU_SOURCE 1 -#include <pwd.h> -#include <stdlib.h> -#include <string.h> -#include "test.h" -#include <errno.h> -#include "compat_16.h" - -TCID_DEFINE (setresuid01); +#include "tst_test.h" +#include "tst_uid.h" +#include "compat_tst_16.h" -uid_t nobody_pw_uid, root_pw_uid, bin_pw_uid; -uid_t neg_one =-1 ; +static uid_t root_uid, main_uid, other_uid, neg_one =-1 ; -struct passwd nobody, bin, root; - -/* -* 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. -*/ - -struct test_data_t { +static struct test_data_t {uid_t *real_uid;uid_t *eff_uid;uid_t *sav_uid; -struct passwd *exp_real_usr; -struct passwd *exp_eff_usr; -struct passwd *exp_sav_usr; +uid_t *exp_real_uid; +uid_t *exp_eff_uid; +uid_t *exp_sav_uid;char *test_msg; } test_data[] = { - { - &neg_one, &neg_one, &neg_one, &root, &root, &root, -"After setresuid(-1, -1, -1)," }, { - &neg_one, &neg_one, &nobody_pw_uid, &root, &root, &nobody, -"After setresuid(-1, -1, nobody)," }, { - &neg_one, &bin_pw_uid, &neg_one, &root, &bin, &nobody, -"After setresuid(-1, bin, -1)," }, { - &neg_one, &neg_one, &root_pw_uid, &root, &bin, &root, -"After setresuid(-1, -1, root)," }, { - &neg_one, &neg_one, &bin_pw_uid, &root, &bin, &bin, -"After setresuid(-1, -1, bin)," }, { - &neg_one, &root_pw_uid, &neg_one, &root, &root, &bin, -"After setresuid(-1, root, -1)," }, { - &nobody_pw_uid, &neg_one, &neg_one, &nobody, &root, &bin, -"After setresuid(nobody, -1, -1)" }, { - &neg_one, &root_pw_uid, &neg_one, &nobody, &root, &bin, -"After setresuid(-1, root, -1)," }, { -&root_pw_uid, &neg_one, &root_pw_uid, &root, &root, &root, -"After setresuid(root, -1, -1)," },}; - -int TST_TOTAL =sizeof (test_data) /sizeof (test_data[0 ]); - -void setup (void ); -void cleanup (void ); - -void -uid_verify (struct passwd *ru,struct passwd *eu,struct passwd *su,char *when); - -int main (int ac,char **av) + {&neg_one, &neg_one, &neg_one, &root_uid, &root_uid, &root_uid, +"After setresuid(-1, -1, -1)," }, + {&neg_one, &neg_one, &main_uid, &root_uid, &root_uid, &main_uid, +"After setresuid(-1, -1, main)," }, + {&neg_one, &other_uid, &neg_one, &root_uid, &other_uid, &main_uid, +"After setresuid(-1, other, -1)," }, + {&neg_one, &neg_one, &root_uid, &root_uid, &other_uid, &root_uid, +"After setresuid(-1, -1, root)," }, + {&neg_one, &neg_one, &other_uid, &root_uid, &other_uid, &other_uid, +"After setresuid(-1, -1, other)," }, + {&neg_one, &root_uid, &neg_one, &root_uid, &root_uid, &other_uid, +"After setresuid(-1, root, -1)," }, + {&main_uid, &neg_one, &neg_one, &main_uid, &root_uid, &other_uid, +"After setresuid(main, -1, -1)" }, + {&neg_one, &root_uid, &neg_one, &main_uid, &root_uid, &other_uid, +"After setresuid(-1, root, -1)," }, + {&root_uid, &neg_one, &root_uid, &root_uid, &root_uid, &root_uid, +"After setresuid(root, -1, -1)," }, +}; + +static void setup (void ) { -int lc; - -tst_parse_opts (ac, av,NULL ,NULL ); +uid_t test_users[2 ]; -setup (); + root_uid =getuid (); +tst_get_uids (test_users,0 ,2 ); + main_uid = test_users[0 ]; + other_uid = test_users[1 ]; -for (lc =0 ;TEST_LOOPING (lc); lc++) { -int i; - -/* reset tst_count in case we are looping */ - tst_count =0 ; - -for (i =0 ; i <TST_TOTAL ; i++) { -/* Set the real, effective or user id */ -TEST (SETRESUID (cleanup, *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid)); - -if (TEST_RETURN ==-1 ) { -tst_resm (TFAIL ,"setresuid(%d, %d, %d) failed" , - *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid); - }else { -uid_verify (test_data[i].exp_real_usr, - test_data[i].exp_eff_usr, - test_data[i].exp_sav_usr, - test_data[i].test_msg); - } - } - } - -cleanup (); -tst_exit (); +UID16_CHECK (root_uid, setresuid); +UID16_CHECK (main_uid, setresuid); +UID16_CHECK (other_uid, setresuid); } -/* -* setup() -* performs all ONE TIME setup for this test -*/ -void setup (void ) +static void run (unsigned int n) { -tst_require_root (); - -tst_sig (FORK ,DEF_HANDLER , cleanup); - -if (getpwnam ("nobody" ) ==NULL ) { -tst_brkm (TBROK ,NULL ,"nobody must be a valid user." ); - } - -if (getpwnam ("bin" ) ==NULL ) { -tst_brkm (TBROK ,NULL ,"bin must be a valid user." ); - } +const struct test_data_t *tc = test_data + n; - root = *(getpwnam ("root" )); -UID16_CHECK ((root_pw_uid = root.pw_uid),"setresuid" , cleanup) +TST_EXP_PASS_SILENT (SETRESUID (*tc->real_uid, *tc->eff_uid, + *tc->sav_uid),"%s" , tc->test_msg); - nobody = *(getpwnam("nobody")); -UID16_CHECK ((nobody_pw_uid = nobody.pw_uid),"setresuid" , cleanup) +if (!TST_PASS ) +return ; - bin = *(getpwnam("bin")); -UID16_CHECK ((bin_pw_uid = bin.pw_uid),"setresuid" , cleanup) - -/* Pause if that option was specified -* TEST_PAUSE contains the code to fork the test with the -c option. -*/ - TEST_PAUSE; +if (tst_check_resuid (tc->test_msg, *tc->exp_real_uid, + *tc->exp_eff_uid, *tc->exp_sav_uid)) +tst_res (TPASS ,"%s works as expected" , tc->test_msg); } -/* -* cleanup() -* performs all ONE TIME cleanup for this test at -* completion or premature exit -*/ -void cleanup (void ) -{ - -} - -void -uid_verify (struct passwd *ru,struct passwd *eu,struct passwd *su,char *when) -{ -uid_t cur_ru, cur_eu, cur_su; -if (getresuid (&cur_ru, &cur_eu, &cur_su) !=0 ) { -tst_brkm (TBROK , cleanup,"Set getresuid() failed" ); - } -if ((cur_ru != ru->pw_uid) || (cur_eu != eu->pw_uid) || (cur_su != - su->pw_uid)) { -tst_resm (TFAIL ,"ERROR: %s real uid = %d; effective uid = %d; " -"saved uid = %d" , when, cur_ru, cur_eu, cur_su); -tst_resm (TINFO ,"Expected: real uid = %d, effective uid = %d " -"saved uid = %d" , ru->pw_uid, eu->pw_uid, su->pw_uid); - }else { -tst_resm (TPASS , -"real uid = %d, effective uid = %d, and saved uid = " -"%d as expected" , cur_ru, cur_eu, cur_su); - } -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (test_data), + .setup = setup, + .needs_root =1 , +};
Signature · 1 line
2.33.0
PATCH v2 2/4 syscalls/setresuid01: Convert to new API
Quoted 16 lines · Martin Doucha
The original test needs user "bin" which may not exist on some systems. Use any non-root UID instead.
Signed-off-by Martin Doucha <[email protected]>---
Changes since v1: None
.../kernel/syscalls/setresuid/setresuid01.c | 256 ++++-------------- 1 file changed, 59 insertions(+), 197 deletions(-)testcases/kernel/syscalls/setresuid/setresuid01.c +0
-0
testcases/kernel/syscalls/setresuid/setresuid01.c line 8
Quoted 9 lines
testcases/kernel/syscalls/setresuid/setresuid01.c +1
-7@@ -1,221 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-only /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version.
Oops, I've accidentally copy-pasted the wrong SPDX header to all three setresuid tests. Patch 1 is correct as is but patches 2-4 should have GPL-2.0-or-later instead. Please update it before merge.
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 v2 2/4 syscalls/setresuid01: Convert to new API
Hi!
Quoted 3 lines
Oops, I've accidentally copy-pasted the wrong SPDX header to all three setresuid tests. Patch 1 is correct as is but patches 2-4 should have GPL-2.0-or-later instead. Please update it before merge.
I've fixed that and also removed useless comments from the test structure in setresgid03.c and pushed the patchset, thanks!
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v2 3/4 syscalls/setresuid02: Convert to new API
In reply to Martin DouchaThe original test needs user "bin" which may not exist on some systems. Use any non-root UID instead.
---
Changes since v1: None
.../kernel/syscalls/setresuid/setresuid02.c | 256 ++++-------------- 1 file changed, 52 insertions(+), 204 deletions(-)
testcases/kernel/syscalls/setresuid/setresuid02.c +52 -204
@@ -1,232 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only /* -* -* Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2001 +* 07/2001 ported by John George +* Copyright (C) 2021 SUSE LLC <[email protected]> */ -/* -* NAME -* setresuid02.c -* -* DESCRIPTION -* Test that a non-root user can change the real, effective and saved -* uid values through the setresuid system call. -* -* ALGORITHM -* -* Setup: -* Setup signal handling -* Get user information. -* Pause for SIGUSER1 if option specified. -* -* Setup test values. -* Loop if the proper options are given. -* For each test set execute the system call -* Check that we received the expected result. -* Verify that the uid, euid and suid values are still correct. -* Cleanup: -* Print errno log and/or timing stats if option given. -* -* USAGE: <for command-line> -* setresuid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -f : Turn off functionality Testing. -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* -* History -* 07/2001 John George -* -Ported +/*\ +* [Description] * -* Restrictions -* This test must be run by root. -* nobody and bin must be a valid users. +* Test that a non-root user can change the real, effective and saved uid +* values through the setresuid system call. */ #define _GNU_SOURCE 1 +#include <sys/types.h> #include <pwd.h> -#include <stdlib.h> -#include "test.h" -#include <errno.h> -#include <sys/wait.h> -#include "compat_16.h" -TCID_DEFINE (setresuid02); +#include "tst_test.h" +#include "tst_uid.h" +#include "compat_tst_16.h" -uid_t neg_one =-1 ; +static uid_t nobody_uid, other_uid, neg_one =-1 ; -/* flag to tell parent if child passed or failed. */ -int flag =0 ; - -uid_t nobody_pw_uid, bin_pw_uid; -char user1name[] ="nobody" ; -char user2name[] ="bin" ; - -struct passwd nobody, 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. -*/ - -struct test_data_t { +static struct test_data_t {uid_t *real_uid;uid_t *eff_uid;uid_t *sav_uid; -struct passwd *exp_real_usr; -struct passwd *exp_eff_usr; -struct passwd *exp_sav_usr; +uid_t *exp_real_uid; +uid_t *exp_eff_uid; +uid_t *exp_sav_uid;char *test_msg; } test_data[] = { - { - &neg_one, &neg_one, &bin_pw_uid, &nobody, &bin, &bin, -"After setresuid(-1, -1, bin)," }, { - &neg_one, &nobody_pw_uid, &neg_one, &nobody, &nobody, &bin, -"After setresuid(-1, nobody -1)," }, { -&bin_pw_uid, &neg_one, &neg_one, &bin, &nobody, &bin, -"After setresuid(bin, -1 -1)," },}; - -int TST_TOTAL =sizeof (test_data) /sizeof (test_data[0 ]); - -void setup (void ); -void cleanup (void ); - -void -uid_verify (struct passwd *ru,struct passwd *eu,struct passwd *su,char *); - -int main (int ac,char **av) + {&neg_one, &neg_one, &other_uid, &nobody_uid, &other_uid, &other_uid, +"setresuid(-1, -1, other)" }, + {&neg_one, &nobody_uid, &neg_one, &nobody_uid, &nobody_uid, &other_uid, +"setresuid(-1, nobody -1)" }, + {&other_uid, &neg_one, &neg_one, &other_uid, &nobody_uid, &other_uid, +"setresuid(other, -1 -1)" }, +/* Return to initial state */ + {&nobody_uid, &other_uid, &nobody_uid, &nobody_uid, &other_uid, + &nobody_uid,"setresuid(nobody, other, nobody)" }, +}; + +static void setup (void ) { -int lc; +uid_t test_users[2 ]; +struct passwd *pw =SAFE_GETPWNAM ("nobody" ); -tst_parse_opts (ac, av,NULL ,NULL ); + nobody_uid = test_users[0 ] = pw->pw_uid; +tst_get_uids (test_users,1 ,2 ); + other_uid = test_users[1 ]; -setup (); +UID16_CHECK (nobody_uid, setresuid); +UID16_CHECK (other_uid, setresuid); -for (lc =0 ;TEST_LOOPING (lc); lc++) { -int i, pid; - -/* reset tst_count in case we are looping */ - tst_count =0 ; - -/* set the appropriate ownership values */ -if (setresuid (nobody_pw_uid, bin_pw_uid, nobody_pw_uid) ==-1 ) { -tst_brkm (TFAIL , cleanup,"Initial setresuid failed" ); - } - -if ((pid =FORK_OR_VFORK ()) ==-1 ) { -tst_brkm (TBROK , cleanup,"fork failed" ); - }else if (pid ==0 ) {/* child */ - -for (i =0 ; i <TST_TOTAL ; i++) { - -/* Set the real, effective or saved user id */ -TEST (SETRESUID (NULL , *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid)); - -if (TEST_RETURN !=-1 ) { -tst_resm (TPASS ,"setresuid(%d, %d, %d) " -"succeeded as expected." , - *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid); - }else { -tst_resm (TFAIL ,"setresuid(%d, %d, %d) " -"did not return as expected." , - *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid); - flag =-1 ; - } - -uid_verify (test_data[i].exp_real_usr, - test_data[i].exp_eff_usr, - test_data[i].exp_sav_usr, - test_data[i].test_msg); - } -exit (flag); - }else {/* parent */ -tst_record_childstatus (cleanup, pid); - } - } -cleanup (); -tst_exit (); +SAFE_SETRESUID (nobody_uid, other_uid, nobody_uid); } -/* -* setup() -* performs all ONE TIME setup for this test -*/ -void setup (void ) +static void run (unsigned int n) { -tst_require_root (); - -tst_sig (FORK ,DEF_HANDLER , cleanup); +const struct test_data_t *tc = test_data + n; -if (getpwnam ("nobody" ) ==NULL ) { -tst_brkm (TBROK ,NULL ,"nobody must be a valid user." ); - } +TST_EXP_PASS_SILENT (SETRESUID (*tc->real_uid, *tc->eff_uid, + *tc->sav_uid),"%s" , tc->test_msg); -if (getpwnam ("bin" ) ==NULL ) { -tst_brkm (TBROK ,NULL ,"bin must be a valid user." ); - } - - nobody = *(getpwnam ("nobody" )); -UID16_CHECK ((nobody_pw_uid = nobody.pw_uid),"setresuid" , cleanup) - - bin = *(getpwnam("bin")); -UID16_CHECK ((bin_pw_uid = bin.pw_uid),"setresuid" , cleanup) - -/* Pause if that option was specified -* TEST_PAUSE contains the code to fork the test with the -i option. -* You want to make sure you do this before you create your temporary -* directory. -*/ - TEST_PAUSE; -} - -/* -* cleanup() -* performs all ONE TIME cleanup for this test at -* completion or premature exit -*/ -void cleanup (void ) -{ +if (!TST_PASS ) +return ; +if (tst_check_resuid (tc->test_msg, *tc->exp_real_uid, + *tc->exp_eff_uid, *tc->exp_sav_uid)) +tst_res (TPASS ,"%s works as expected" , tc->test_msg); } -void -uid_verify (struct passwd *ru,struct passwd *eu,struct passwd *su,char *when) -{ -uid_t cur_ru, cur_eu, cur_su; -if (getresuid (&cur_ru, &cur_eu, &cur_su) !=0 ) { - flag =-1 ; -tst_brkm (TBROK , cleanup,"Set getresuid() failed" ); - } -if ((cur_ru != ru->pw_uid) || (cur_eu != eu->pw_uid) || (cur_su != - su->pw_uid)) { -tst_resm (TFAIL ,"ERROR: %s real uid = %d; effective uid = %d; " -"saved uid = %d" , when, cur_ru, cur_eu, cur_su); -tst_resm (TINFO ,"Expected: real uid = %d, effective uid = %d " -"saved uid = %d" , ru->pw_uid, eu->pw_uid, su->pw_uid); - flag =-1 ; - }else { -tst_resm (TINFO ,"real uid = %d, effective uid = %d, and " -"saved uid = %d as expected" , cur_ru, cur_eu, cur_su); - } -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (test_data), + .setup = setup, + .needs_root =1 , +};
Signature · 1 line
2.33.0
PATCH v2 4/4 syscalls/setresuid03: Convert to new API
In reply to Martin DouchaThe original test looks up specific usernames which may not exist on some systems. Use any two non-root UIDs instead.
---
Changes since v1:
- use TST_EXP_FAIL() to simplify result validation
.../kernel/syscalls/setresuid/setresuid03.c | 263 ++++-------------- 1 file changed, 47 insertions(+), 216 deletions(-)
testcases/kernel/syscalls/setresuid/setresuid03.c +47 -216
@@ -1,245 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0-only /* -* * Copyright (c) International Business Machines Corp., 2001 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* 07/2001 ported by John George +* Copyright (C) 2021 SUSE LLC <[email protected]> */ -/* -* NAME -* setresuid03.c -* -* DESCRIPTION -* Test that the setresuid system call sets the proper errno -* values when a non-root user attempts to change the real, effective or -* saved uid to a value other than one of the current uid, the current -* effective uid of the current saved uid. Also verify that setresuid -* fails if an invalid uid value is given. -* -* ALGORITHM -* -* Setup: -* Setup signal handling -* Get user information. -* Pause for SIGUSER1 if option specified. -* -* Setup test values. -* Loop if the proper options are given. -* For each test set execute the system call -* Check that we received the expected result. -* Verify that the uid, euid and suid values are still correct. -* Cleanup: -* Print errno log and/or timing stats if option given. -* -* USAGE: <for command-line> -* setresuid03 [-c n] [-f] [-i n] [-I x] [-P x] [-t] -* where, -c n : Run n copies concurrently. -* -e : Turn on errno logging. -* -f : Turn off functionality Testing. -* -i n : Execute test n times. -* -I x : Execute test for x seconds. -* -P x : Pause for x seconds between iterations. -* -t : Turn on syscall timing. -* -* History -* 07/2001 John George -* -Ported +/*\ +* [Description] * -* Restrictions -* This test must be run by root. -* nobody and bin must be a valid users. +* Test that the setresuid system call sets the proper errno values when +* a non-root user attempts to change the real, effective or saved uid +* to a value other than one of the current uid, the current effective uid +* or the current saved uid. */ -#define _GNU_SOURCE 1 -#include <pwd.h> -#include <stdlib.h> -#include "test.h" -#include <errno.h> -#include <sys/wait.h> +#include "tst_test.h" +#include "tst_uid.h" +#include "compat_tst_16.h" -#include <compat_16.h> +static uid_t root_uid, main_uid, other_uid, neg_one =-1 ; -TCID_DEFINE (setresuid03); - -uid_t neg_one =-1 ; - -/* flag to tell parent if child passed or failed. */ -int flag =0 ; - -uid_t root_pw_uid, nobody_pw_uid, bin_pw_uid; -char user1name[] ="nobody" ; -char user2name[] ="bin" ; -char rootname[] ="root" ; - -struct passwd nobody, bin, root; - -/* -* 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. -*/ - -struct test_data_t { +static struct test_data_t {uid_t *real_uid;uid_t *eff_uid;uid_t *sav_uid;int exp_errno; -struct passwd *exp_real_usr; -struct passwd *exp_eff_usr; -struct passwd *exp_sav_usr; +uid_t *exp_real_uid; +uid_t *exp_eff_uid; +uid_t *exp_sav_uid;char *test_msg; } test_data[] = { - { - &nobody_pw_uid, &neg_one, &neg_one,EPERM , &root, &bin, &bin, -"After setresuid(root, -1, -1)," }, { - &neg_one, &neg_one, &nobody_pw_uid,EPERM , &root, &bin, &bin, -"After setresuid(-1, -1, bin)," }, { - &neg_one, &nobody_pw_uid, &neg_one,EPERM , &root, &bin, &bin, -"After setresuid(-1, -1, bin)," } + {&other_uid, &neg_one, &neg_one,EPERM , &root_uid, &main_uid, + &main_uid,"setresuid(other, -1, -1)" }, + {&neg_one, &neg_one, &other_uid,EPERM , &root_uid, &main_uid, + &main_uid,"setresuid(-1, -1, other)" }, + {&neg_one, &other_uid, &neg_one,EPERM , &root_uid, &main_uid, + &main_uid,"setresuid(-1, other, -1)" } }; -int TST_TOTAL =sizeof (test_data) /sizeof (test_data[0 ]); - -void setup (void ); -void cleanup (void ); - -void -uid_verify (struct passwd *ru,struct passwd *eu,struct passwd *su,char *); - -int main (int ac,char **av) +static void setup (void ) { -int lc; +uid_t test_users[2 ]; -tst_parse_opts (ac, av,NULL ,NULL ); + root_uid =getuid (); +tst_get_uids (test_users,0 ,2 ); + main_uid = test_users[0 ]; + other_uid = test_users[1 ]; -setup (); +UID16_CHECK (root_uid, setresuid); +UID16_CHECK (main_uid, setresuid); +UID16_CHECK (other_uid, setresuid); -for (lc =0 ;TEST_LOOPING (lc); lc++) { -int i, pid; - -/* reset tst_count in case we are looping */ - tst_count =0 ; - -/* set the appropriate ownership values */ -if (setresuid (root_pw_uid, bin_pw_uid, bin_pw_uid) - ==-1 ) { -tst_brkm (TFAIL , cleanup,"Initial setresuid failed" ); - } - -if ((pid =FORK_OR_VFORK ()) ==-1 ) { -tst_brkm (TBROK , cleanup,"fork failed" ); - }else if (pid ==0 ) {/* child */ - -for (i =0 ; i <TST_TOTAL ; i++) { - -/* Set the real, effective or saved user id */ -TEST (SETRESUID (NULL , *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid)); - -if (TEST_RETURN ==-1 &&TEST_ERRNO == - test_data[i].exp_errno) { -tst_resm (TPASS ,"setresuid(%d, %d, %d) " -"failed as expected." , - *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid); - }else { -tst_resm (TFAIL ,"setresuid(%d, %d, %d) " -"did not fail as expected." , - *test_data[i].real_uid, - *test_data[i].eff_uid, - *test_data[i].sav_uid); - flag =-1 ; - } - -uid_verify (test_data[i].exp_real_usr, - test_data[i].exp_eff_usr, - test_data[i].exp_sav_usr, - test_data[i].test_msg); - } -exit (flag); - }else {/* parent */ -tst_record_childstatus (cleanup, pid); - } - } -cleanup (); -tst_exit (); +SAFE_SETRESUID (root_uid, main_uid, main_uid); } -/* -* setup() -* performs all ONE TIME setup for this test -*/ -void setup (void ) +static void run (unsigned int n) { -tst_require_root (); - -tst_sig (FORK ,DEF_HANDLER , cleanup); - -if (getpwnam ("nobody" ) ==NULL ) { -tst_brkm (TBROK ,NULL ,"nobody must be a valid user." ); - } +const struct test_data_t *tc = test_data + n; -if (getpwnam ("bin" ) ==NULL ) { -tst_brkm (TBROK ,NULL ,"bin must be a valid user." ); - } +TST_EXP_FAIL (SETRESUID (*tc->real_uid, *tc->eff_uid, *tc->sav_uid), + tc->exp_errno,"%s" , tc->test_msg); - root = *(getpwnam ("root" )); -UID16_CHECK ((root_pw_uid = root.pw_uid),"setresuid" , cleanup) +if (!TST_PASS ) +return ; - nobody = *(getpwnam("nobody")); -UID16_CHECK ((nobody_pw_uid = nobody.pw_uid),"setresuid" , cleanup) - - bin = *(getpwnam("bin")); -UID16_CHECK ((bin_pw_uid = bin.pw_uid),"setresuid" , cleanup) - -/* Pause if that option was specified -* TEST_PAUSE contains the code to fork the test with the -i option. -* You want to make sure you do this before you create your temporary -* directory. -*/ - TEST_PAUSE; +tst_check_resuid (tc->test_msg, *tc->exp_real_uid, *tc->exp_eff_uid, + *tc->exp_sav_uid); } -/* -* cleanup() -* performs all ONE TIME cleanup for this test at -* completion or premature exit -*/ -void cleanup (void ) -{ - -} - -void -uid_verify (struct passwd *ru,struct passwd *eu,struct passwd *su,char *when) -{ -uid_t cur_ru, cur_eu, cur_su; -if (getresuid (&cur_ru, &cur_eu, &cur_su) !=0 ) { - flag =-1 ; -tst_brkm (TBROK , cleanup,"Set getresuid() failed" ); - } -if ((cur_ru != ru->pw_uid) || (cur_eu != eu->pw_uid) || (cur_su != - su->pw_uid)) { -tst_resm (TFAIL ,"ERROR: %s real uid = %d; effective uid = %d; " -"saved uid = %d" , when, cur_ru, cur_eu, cur_su); -tst_resm (TINFO ,"Expected: real uid = %d, effective uid = %d " -"saved uid = %d" , ru->pw_uid, eu->pw_uid, su->pw_uid); - flag =-1 ; - }else { -tst_resm (TINFO ,"real uid = %d, effective uid = %d, and " -"saved uid = %d as expected" , cur_ru, cur_eu, cur_su); - } -} +static struct tst_test test = { + .test = run, + .tcnt =ARRAY_SIZE (test_data), + .setup = setup, + .needs_root =1 , +};
Signature · 1 line
2.33.0