16f906eab90659aa74fbd44d545e276c56362f43frobbiew/* 26f906eab90659aa74fbd44d545e276c56362f43frobbiew * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. 36f906eab90659aa74fbd44d545e276c56362f43frobbiew * 46f906eab90659aa74fbd44d545e276c56362f43frobbiew * This program is free software; you can redistribute it and/or modify it 56f906eab90659aa74fbd44d545e276c56362f43frobbiew * under the terms of version 2 of the GNU General Public License as 66f906eab90659aa74fbd44d545e276c56362f43frobbiew * published by the Free Software Foundation. 76f906eab90659aa74fbd44d545e276c56362f43frobbiew * 86f906eab90659aa74fbd44d545e276c56362f43frobbiew * This program is distributed in the hope that it would be useful, but 96f906eab90659aa74fbd44d545e276c56362f43frobbiew * WITHOUT ANY WARRANTY; without even the implied warranty of 106f906eab90659aa74fbd44d545e276c56362f43frobbiew * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 116f906eab90659aa74fbd44d545e276c56362f43frobbiew * 126f906eab90659aa74fbd44d545e276c56362f43frobbiew * You should have received a copy of the GNU General Public License along 13fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * with this program; if not, write the Free Software Foundation, Inc., 14fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 156f906eab90659aa74fbd44d545e276c56362f43frobbiew * 166f906eab90659aa74fbd44d545e276c56362f43frobbiew */ 176f906eab90659aa74fbd44d545e276c56362f43frobbiew/********************************************************** 184bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 196f906eab90659aa74fbd44d545e276c56362f43frobbiew * TEST IDENTIFIER : setresgid01 204bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 216f906eab90659aa74fbd44d545e276c56362f43frobbiew * EXECUTED BY : root / superuser 224bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 236f906eab90659aa74fbd44d545e276c56362f43frobbiew * TEST TITLE : Checking functionality of setresgid(2) 244bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 256f906eab90659aa74fbd44d545e276c56362f43frobbiew * TEST CASE TOTAL : 5 264bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 276f906eab90659aa74fbd44d545e276c56362f43frobbiew * AUTHOR : Madhu T L <madhu.tarikere@wipro.com> 284bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 296f906eab90659aa74fbd44d545e276c56362f43frobbiew * SIGNALS 306f906eab90659aa74fbd44d545e276c56362f43frobbiew * Uses SIGUSR1 to pause before test if option set. 316f906eab90659aa74fbd44d545e276c56362f43frobbiew * (See the parse_opts(3) man page). 326f906eab90659aa74fbd44d545e276c56362f43frobbiew * 336f906eab90659aa74fbd44d545e276c56362f43frobbiew * DESCRIPTION 346f906eab90659aa74fbd44d545e276c56362f43frobbiew * Verify that, 356f906eab90659aa74fbd44d545e276c56362f43frobbiew * 1. setresgid(2) is successful for setresgid(-1, -1, -1) 366f906eab90659aa74fbd44d545e276c56362f43frobbiew * 2. setresgid(2) is successful for setresgid(-1, -1, nobody) 376f906eab90659aa74fbd44d545e276c56362f43frobbiew * 3. setresgid(2) is successful for setresgid(-1, nobody, -1) 386f906eab90659aa74fbd44d545e276c56362f43frobbiew * 4. setresgid(2) is successful for setresgid(nobody, -1, -1) 396f906eab90659aa74fbd44d545e276c56362f43frobbiew * 5. setresgid(2) is successful for setresgid(root, root, root) 404bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 416f906eab90659aa74fbd44d545e276c56362f43frobbiew * Setup: 426f906eab90659aa74fbd44d545e276c56362f43frobbiew * Setup signal handling. 436f906eab90659aa74fbd44d545e276c56362f43frobbiew * Test caller is superuser 446f906eab90659aa74fbd44d545e276c56362f43frobbiew * Check existence of root and nobody user id's 456f906eab90659aa74fbd44d545e276c56362f43frobbiew * Pause for SIGUSR1 if option specified. 464bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 476f906eab90659aa74fbd44d545e276c56362f43frobbiew * Test: 486f906eab90659aa74fbd44d545e276c56362f43frobbiew * Loop if the proper options are given. 496f906eab90659aa74fbd44d545e276c56362f43frobbiew * Execute system call 506f906eab90659aa74fbd44d545e276c56362f43frobbiew * Check return value and functionality, if success, 516f906eab90659aa74fbd44d545e276c56362f43frobbiew * Issue PASS message 526f906eab90659aa74fbd44d545e276c56362f43frobbiew * Otherwise, 536f906eab90659aa74fbd44d545e276c56362f43frobbiew * Issue FAIL message 544bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 556f906eab90659aa74fbd44d545e276c56362f43frobbiew * Cleanup: 566f906eab90659aa74fbd44d545e276c56362f43frobbiew * Print errno log and/or timing stats if options given 574bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 586f906eab90659aa74fbd44d545e276c56362f43frobbiew * USAGE: <for command-line> 596f906eab90659aa74fbd44d545e276c56362f43frobbiew * setresgid01 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t] 606f906eab90659aa74fbd44d545e276c56362f43frobbiew * where, -c n : Run n copies concurrently. 616f906eab90659aa74fbd44d545e276c56362f43frobbiew * -e : Turn on errno logging. 626f906eab90659aa74fbd44d545e276c56362f43frobbiew * -f : Turn off functional testing 636f906eab90659aa74fbd44d545e276c56362f43frobbiew * -h : Show help screen 646f906eab90659aa74fbd44d545e276c56362f43frobbiew * -i n : Execute test n times. 656f906eab90659aa74fbd44d545e276c56362f43frobbiew * -I x : Execute test for x seconds. 666f906eab90659aa74fbd44d545e276c56362f43frobbiew * -p : Pause for SIGUSR1 before starting 676f906eab90659aa74fbd44d545e276c56362f43frobbiew * -P x : Pause for x seconds between iterations. 686f906eab90659aa74fbd44d545e276c56362f43frobbiew * -t : Turn on syscall timing. 694bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * 706f906eab90659aa74fbd44d545e276c56362f43frobbiew ****************************************************************/ 716f906eab90659aa74fbd44d545e276c56362f43frobbiew 72bed036a06374f3abca1c7cc3d246b1ed141c1a84vapier#define _GNU_SOURCE 1 736f906eab90659aa74fbd44d545e276c56362f43frobbiew#include <errno.h> 746f906eab90659aa74fbd44d545e276c56362f43frobbiew#include <pwd.h> 756f906eab90659aa74fbd44d545e276c56362f43frobbiew#include <sys/types.h> 766f906eab90659aa74fbd44d545e276c56362f43frobbiew#include <unistd.h> 776f906eab90659aa74fbd44d545e276c56362f43frobbiew#include "test.h" 7875fb057ebd66c8d8012ec5a53f1a187551296b00Han Pingtian#include "compat_16.h" 796f906eab90659aa74fbd44d545e276c56362f43frobbiew 806f906eab90659aa74fbd44d545e276c56362f43frobbiew#define EXP_RET_VAL 0 816f906eab90659aa74fbd44d545e276c56362f43frobbiew 8256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstruct test_case_t { /* test case structure */ 8356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak uid_t *rgid; /* real GID */ 8456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak uid_t *egid; /* effective GID */ 8556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak uid_t *sgid; /* saved GID */ 866f906eab90659aa74fbd44d545e276c56362f43frobbiew struct passwd *exp_rgid; /* Expected real GID */ 876f906eab90659aa74fbd44d545e276c56362f43frobbiew struct passwd *exp_egid; /* Expected effective GID */ 886f906eab90659aa74fbd44d545e276c56362f43frobbiew struct passwd *exp_sgid; /* Expected saved GID */ 8956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak char *desc; /* Test description */ 906f906eab90659aa74fbd44d545e276c56362f43frobbiew}; 916f906eab90659aa74fbd44d545e276c56362f43frobbiew 9275fb057ebd66c8d8012ec5a53f1a187551296b00Han PingtianTCID_DEFINE(setresgid01); 936f906eab90659aa74fbd44d545e276c56362f43frobbiewstatic int testno; 946f906eab90659aa74fbd44d545e276c56362f43frobbiewstatic struct passwd nobody, root; 956f906eab90659aa74fbd44d545e276c56362f43frobbiewstatic uid_t nobody_gid, root_gid, neg = -1; 966f906eab90659aa74fbd44d545e276c56362f43frobbiew 976f906eab90659aa74fbd44d545e276c56362f43frobbiewstatic int test_functionality(uid_t, uid_t, uid_t); 986f906eab90659aa74fbd44d545e276c56362f43frobbiewstatic void setup(void); 996f906eab90659aa74fbd44d545e276c56362f43frobbiewstatic void cleanup(void); 1006f906eab90659aa74fbd44d545e276c56362f43frobbiew 1016f906eab90659aa74fbd44d545e276c56362f43frobbiew/* Don't change order of these test cases */ 10256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic struct test_case_t tdat[] = { 10356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak {&neg, &neg, &neg, &root, &root, &root, 10456207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "setresgid(-1, -1, -1)"}, 10556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak {&neg, &neg, &nobody.pw_gid, &root, &root, &nobody, 10656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "setresgid(-1, -1, nobody)"}, 10756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak {&neg, &nobody.pw_gid, &neg, &root, &nobody, &nobody, 10856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "setresgid(-1, nobody, -1)"}, 10956207cec7732e09c216c751c0b5f88a242bacae6subrata_modak {&nobody.pw_gid, &neg, &neg, &nobody, &nobody, &nobody, 11056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "setresgid(nobody, -1, -1)"}, 11156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak {&root.pw_gid, &root.pw_gid, &root.pw_gid, &root, &root, &root, 11256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "setresgid(root, root, root)"}, 1136f906eab90659aa74fbd44d545e276c56362f43frobbiew}; 1146f906eab90659aa74fbd44d545e276c56362f43frobbiew 1156f906eab90659aa74fbd44d545e276c56362f43frobbiewint TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]); 1166f906eab90659aa74fbd44d545e276c56362f43frobbiew 11756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int argc, char **argv) 1186f906eab90659aa74fbd44d545e276c56362f43frobbiew{ 11989af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis int lc; 1206f906eab90659aa74fbd44d545e276c56362f43frobbiew 121d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis tst_parse_opts(argc, argv, NULL, NULL); 1226f906eab90659aa74fbd44d545e276c56362f43frobbiew 1236f906eab90659aa74fbd44d545e276c56362f43frobbiew setup(); 1246f906eab90659aa74fbd44d545e276c56362f43frobbiew 1256f906eab90659aa74fbd44d545e276c56362f43frobbiew for (lc = 0; TEST_LOOPING(lc); lc++) { 126d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang /* reset tst_count in case we are looping */ 127d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang tst_count = 0; 1286f906eab90659aa74fbd44d545e276c56362f43frobbiew 1296f906eab90659aa74fbd44d545e276c56362f43frobbiew for (testno = 0; testno < TST_TOTAL; ++testno) { 1306f906eab90659aa74fbd44d545e276c56362f43frobbiew 13175fb057ebd66c8d8012ec5a53f1a187551296b00Han Pingtian TEST(SETRESGID(cleanup, *tdat[testno].rgid, *tdat[testno].egid, 13256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *tdat[testno].sgid)); 1336f906eab90659aa74fbd44d545e276c56362f43frobbiew 1346f906eab90659aa74fbd44d545e276c56362f43frobbiew if (TEST_RETURN == EXP_RET_VAL) { 13556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak if (!test_functionality 13656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak (tdat[testno].exp_rgid->pw_gid, 13756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak tdat[testno].exp_egid->pw_gid, 13856207cec7732e09c216c751c0b5f88a242bacae6subrata_modak tdat[testno].exp_sgid->pw_gid)) { 1396f906eab90659aa74fbd44d545e276c56362f43frobbiew 1406f906eab90659aa74fbd44d545e276c56362f43frobbiew tst_resm(TPASS, "Test for %s " 14156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "successful", 14256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak tdat[testno].desc); 1436f906eab90659aa74fbd44d545e276c56362f43frobbiew } else { 1446f906eab90659aa74fbd44d545e276c56362f43frobbiew tst_resm(TFAIL, "Functionality test " 14556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak "for %s failed", 14656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak tdat[testno].desc); 1476f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1486f906eab90659aa74fbd44d545e276c56362f43frobbiew } else { 1496f906eab90659aa74fbd44d545e276c56362f43frobbiew tst_resm(TFAIL, "Test for %s failed; returned" 150b0b6f454a93242fc457249b6a22b7118c5479bf3vapier " %ld (expected %d), errno %d (expected" 15156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak " 0)", tdat[testno].desc, 15256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak TEST_RETURN, EXP_RET_VAL, TEST_ERRNO); 1536f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1546f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1556f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1566f906eab90659aa74fbd44d545e276c56362f43frobbiew cleanup(); 1576f906eab90659aa74fbd44d545e276c56362f43frobbiew 15853740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper tst_exit(); 1596f906eab90659aa74fbd44d545e276c56362f43frobbiew} 1606f906eab90659aa74fbd44d545e276c56362f43frobbiew 16156207cec7732e09c216c751c0b5f88a242bacae6subrata_modakstatic int test_functionality(uid_t exp_rgid, uid_t exp_egid, uid_t exp_sgid) 1626f906eab90659aa74fbd44d545e276c56362f43frobbiew{ 1636f906eab90659aa74fbd44d545e276c56362f43frobbiew uid_t cur_rgid, cur_egid, cur_sgid; 1646f906eab90659aa74fbd44d545e276c56362f43frobbiew 1656f906eab90659aa74fbd44d545e276c56362f43frobbiew /* Get current real, effective and saved group id's */ 16656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak if (getresgid(&cur_rgid, &cur_egid, &cur_sgid) == -1) { 1676f906eab90659aa74fbd44d545e276c56362f43frobbiew tst_brkm(TBROK, cleanup, "getresgid() failed"); 1682c28215423293e443469a07ae7011135d058b671Garrett Cooper 1696f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1706f906eab90659aa74fbd44d545e276c56362f43frobbiew 17156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak if ((cur_rgid == exp_rgid) && (cur_egid == exp_egid) 17256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak && (cur_sgid == exp_sgid)) { 1736f906eab90659aa74fbd44d545e276c56362f43frobbiew return 0; 1746f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1756f906eab90659aa74fbd44d545e276c56362f43frobbiew return 1; 1766f906eab90659aa74fbd44d545e276c56362f43frobbiew} 1776f906eab90659aa74fbd44d545e276c56362f43frobbiew 1786f906eab90659aa74fbd44d545e276c56362f43frobbiew/* 1796f906eab90659aa74fbd44d545e276c56362f43frobbiew * setup() 1806f906eab90659aa74fbd44d545e276c56362f43frobbiew * performs all ONE TIME setup for this test 1816f906eab90659aa74fbd44d545e276c56362f43frobbiew */ 18256207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid setup(void) 1836f906eab90659aa74fbd44d545e276c56362f43frobbiew{ 1846f906eab90659aa74fbd44d545e276c56362f43frobbiew struct passwd *passwd_p; 1856f906eab90659aa74fbd44d545e276c56362f43frobbiew 186d1e794d62b1bf619df8390535e4c2a58899b1145Cyril Hrubis tst_require_root(); 1876f906eab90659aa74fbd44d545e276c56362f43frobbiew 188d4ceb37d3ab506483612ef0ad74c88e5828a9779Nicolas Joly tst_sig(NOFORK, DEF_HANDLER, cleanup); 1896f906eab90659aa74fbd44d545e276c56362f43frobbiew 19056207cec7732e09c216c751c0b5f88a242bacae6subrata_modak if ((passwd_p = getpwnam("root")) == NULL) { 19153740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper tst_brkm(TBROK, NULL, "getpwnam() failed for root"); 1922c28215423293e443469a07ae7011135d058b671Garrett Cooper 1936f906eab90659aa74fbd44d545e276c56362f43frobbiew } 1946f906eab90659aa74fbd44d545e276c56362f43frobbiew root = *passwd_p; 19575fb057ebd66c8d8012ec5a53f1a187551296b00Han Pingtian GID16_CHECK((root_gid = root.pw_gid), "setresgid", cleanup) 1966f906eab90659aa74fbd44d545e276c56362f43frobbiew 19756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak if ((passwd_p = getpwnam("nobody")) == NULL) { 19853740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper tst_brkm(TBROK, NULL, "nobody user id doesn't exist"); 1992c28215423293e443469a07ae7011135d058b671Garrett Cooper 2006f906eab90659aa74fbd44d545e276c56362f43frobbiew } 2016f906eab90659aa74fbd44d545e276c56362f43frobbiew nobody = *passwd_p; 20275fb057ebd66c8d8012ec5a53f1a187551296b00Han Pingtian GID16_CHECK((nobody_gid = nobody.pw_gid), "setresgid", cleanup) 2036f906eab90659aa74fbd44d545e276c56362f43frobbiew 2046f906eab90659aa74fbd44d545e276c56362f43frobbiew /* Pause if that option was specified 2056f906eab90659aa74fbd44d545e276c56362f43frobbiew * TEST_PAUSE contains the code to fork the test with the -c option. 2066f906eab90659aa74fbd44d545e276c56362f43frobbiew */ 2076f906eab90659aa74fbd44d545e276c56362f43frobbiew TEST_PAUSE; 2086f906eab90659aa74fbd44d545e276c56362f43frobbiew} 2096f906eab90659aa74fbd44d545e276c56362f43frobbiew 2106f906eab90659aa74fbd44d545e276c56362f43frobbiew/* 2116f906eab90659aa74fbd44d545e276c56362f43frobbiew * cleanup() 2126f906eab90659aa74fbd44d545e276c56362f43frobbiew * performs all ONE TIME cleanup for this test at 2136f906eab90659aa74fbd44d545e276c56362f43frobbiew * completion or premature exit 2146f906eab90659aa74fbd44d545e276c56362f43frobbiew */ 21556207cec7732e09c216c751c0b5f88a242bacae6subrata_modakvoid cleanup(void) 2166f906eab90659aa74fbd44d545e276c56362f43frobbiew{ 2176f906eab90659aa74fbd44d545e276c56362f43frobbiew 218354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao} 219