plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: getresgid03 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that getresgid() will be successful to get the real, effective |
| 25 | * and saved user ids after calling process invokes setresgid() to change |
| 26 | * the effective gid to that of specified user. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 27 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | * Expected Result: |
| 29 | * getresgid() should return with 0 value and the effective user id |
| 30 | * should match the egid of specified user, real/saved user ids should |
| 31 | * remain unchanged. |
| 32 | * |
| 33 | * Algorithm: |
| 34 | * Setup: |
| 35 | * Setup signal handling. |
| 36 | * Pause for SIGUSR1 if option specified. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Execute system call |
| 41 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * Otherwise, |
| 48 | * Issue Functionality-Fail message. |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * |
| 52 | * Usage: <for command-line> |
| 53 | * getresgid03 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -f : Turn off functionality Testing. |
| 56 | * -i n : Execute test n times. |
| 57 | * -I x : Execute test for x seconds. |
| 58 | * -P x : Pause for x seconds between iterations. |
| 59 | * -t : Turn on syscall timing. |
| 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS: |
| 65 | * This test should be run by 'super-user' (root) only. |
| 66 | * |
| 67 | */ |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 68 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | #include <stdio.h> |
| 71 | #include <unistd.h> |
| 72 | #include <sys/types.h> |
| 73 | #include <errno.h> |
| 74 | #include <fcntl.h> |
| 75 | #include <string.h> |
| 76 | #include <signal.h> |
| 77 | #include <pwd.h> |
| 78 | |
| 79 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | extern int getresgid(gid_t *, gid_t *, gid_t *); |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 82 | extern int setresgid(gid_t, gid_t, gid_t); |
| 83 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 84 | char *TCID = "getresgid03"; |
| 85 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | gid_t pr_gid, pe_gid, ps_gid; /* calling process real/effective/saved gid */ |
| 87 | |
| 88 | void setup(); /* Main setup function of test */ |
| 89 | void cleanup(); /* cleanup function for the test */ |
| 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 93 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 94 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | gid_t real_gid, /* real/eff./saved user id from getresgid() */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | eff_gid, sav_gid; |
| 97 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 98 | msg = parse_opts(ac, av, NULL, NULL); |
| 99 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | setup(); |
| 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 107 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 110 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | * Call getresgid() to get the real/effective/saved |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 112 | * user id's of the calling process after |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | * setregid() in setup. |
| 114 | */ |
| 115 | TEST(getresgid(&real_gid, &eff_gid, &sav_gid)); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | if (TEST_RETURN == -1) { |
| 118 | tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s", |
| 119 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 120 | continue; |
| 121 | } |
| 122 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 123 | * Verify the real/effective/saved gid |
| 124 | * values returned by getresgid with the |
| 125 | * expected values. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 127 | if ((real_gid != pr_gid) || (eff_gid != pe_gid) || |
| 128 | (sav_gid != ps_gid)) { |
| 129 | tst_resm(TFAIL, "real:%d, effective:%d, " |
| 130 | "saved-user:%d ids differ", |
| 131 | real_gid, eff_gid, sav_gid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | tst_resm(TPASS, "Functionality of getresgid() " |
| 134 | "successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 136 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 139 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * setup() - performs all ONE TIME setup for this test. |
| 144 | * Make sure test process gid is root. |
| 145 | * Get the real/effective/saved user id of the calling process. |
| 146 | * Get the user info. of test user "ltpuser1" from /etc/passwd file. |
| 147 | * Set the eff. user id of test process to that of "ltpuser1" user. |
| 148 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 149 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | struct passwd *user_id; /* passwd struct for test user */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 153 | tst_require_root(NULL); |
| 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
| 158 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | /* Real user-id of the calling process */ |
| 160 | pr_gid = getgid(); |
| 161 | |
| 162 | /* Saved user-id of the calling process. */ |
| 163 | ps_gid = getegid(); |
| 164 | |
| 165 | /* Get effective gid of "ltpuser1" user from passwd file */ |
| 166 | if ((user_id = getpwnam("nobody")) == NULL) { |
| 167 | tst_brkm(TBROK, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | "getpwnam(nobody) Failed, errno=%d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | /* Effective user-id of the test-user "ltpuser1" */ |
| 172 | pe_gid = user_id->pw_gid; |
| 173 | |
| 174 | /* |
| 175 | * Set the effective user-id of the process to that of |
| 176 | * test user "ltpuser1". |
| 177 | * The real/saved user id remains same as of caller. |
| 178 | */ |
| 179 | if (setresgid(-1, pe_gid, -1) < 0) { |
| 180 | tst_brkm(TBROK, cleanup, |
| 181 | "setresgid(-1, %d, -1) Fails, errno:%d : %s", |
| 182 | ps_gid, errno, strerror(errno)); |
| 183 | } |
| 184 | } |
| 185 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | /* |
| 187 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 188 | * completion or premature exit. |
| 189 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 190 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
| 193 | /* Reset the effective/saved gid of the calling process */ |
| 194 | if (setregid(-1, pr_gid) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 195 | tst_brkm(TBROK, NULL, "resetting process effective gid failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 198 | } |