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: getresuid02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that getresuid() will be successful to get the real, effective |
| 25 | * and saved user ids after calling process invokes setreuid() to change |
| 26 | * the effective/saved uids 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 | * getresuid() should return with 0 value and the real/effective/saved |
| 30 | * user ids should match the expected values. |
| 31 | * |
| 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Pause for SIGUSR1 if option specified. |
| 36 | * |
| 37 | * Test: |
| 38 | * Loop if the proper options are given. |
| 39 | * Execute system call |
| 40 | * Check return code, if system call failed (return=-1) |
| 41 | * Log the errno and Issue a FAIL message. |
| 42 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
| 45 | * Issue Functionality-Pass message. |
| 46 | * Otherwise, |
| 47 | * Issue Functionality-Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * |
| 51 | * Usage: <for command-line> |
| 52 | * getresuid02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 53 | * where, -c n : Run n copies concurrently. |
| 54 | * -f : Turn off functionality Testing. |
| 55 | * -i n : Execute test n times. |
| 56 | * -I x : Execute test for x seconds. |
| 57 | * -P x : Pause for x seconds between iterations. |
| 58 | * -t : Turn on syscall timing. |
| 59 | * |
| 60 | * HISTORY |
| 61 | * 07/2001 Ported by Wayne Boyer |
| 62 | * |
| 63 | * RESTRICTIONS: |
| 64 | * This test should be run by 'super-user' (root) only. |
| 65 | * |
| 66 | */ |
| 67 | |
| 68 | #include <stdio.h> |
| 69 | #include <unistd.h> |
| 70 | #include <sys/types.h> |
| 71 | #include <errno.h> |
| 72 | #include <fcntl.h> |
| 73 | #include <string.h> |
| 74 | #include <signal.h> |
| 75 | #include <pwd.h> |
| 76 | |
| 77 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
| 79 | #define LTPUSER "nobody" |
| 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | extern int getresuid(uid_t *, uid_t *, uid_t *); |
robbiew | 96d2337 | 2003-03-26 20:40:04 +0000 | [diff] [blame] | 82 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 83 | char *TCID = "getresuid02"; |
| 84 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | uid_t pr_uid, pe_uid, ps_uid; /* calling process real/effective/saved uid */ |
| 86 | |
| 87 | void setup(); /* Main setup function of test */ |
| 88 | void cleanup(); /* cleanup function for the test */ |
| 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 92 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 93 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | uid_t real_uid, /* real/eff./saved user id from getresuid() */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | eff_uid, sav_uid; |
| 96 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 97 | msg = parse_opts(ac, av, NULL, NULL); |
| 98 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | } |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | setup(); |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 107 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 109 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | * Call getresuid() to get the real/effective/saved |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 111 | * user id's of the calling process after |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | * setreuid() in setup. |
| 113 | */ |
| 114 | TEST(getresuid(&real_uid, &eff_uid, &sav_uid)); |
| 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | if (TEST_RETURN == -1) { |
| 117 | tst_resm(TFAIL, "getresuid() Failed, errno=%d : %s", |
| 118 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 119 | continue; |
| 120 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 121 | |
| 122 | if ((real_uid != pr_uid) || (eff_uid != pe_uid) || |
| 123 | (sav_uid != ps_uid)) { |
| 124 | tst_resm(TFAIL, "real:%d, effective:%d, " |
| 125 | "saved-user:%d ids differ", |
| 126 | real_uid, eff_uid, sav_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 128 | tst_resm(TPASS, "Functionality of getresuid() " |
| 129 | "successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 131 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 134 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * void |
| 139 | * setup() - performs all ONE TIME setup for this test. |
| 140 | * Get the real/effective/saved user id of the calling process. |
| 141 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 142 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | struct passwd *user_id; /* passwd struct for test user */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 146 | tst_require_root(NULL); |
| 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 149 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | TEST_PAUSE; |
| 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | /* Real user-id of the calling process */ |
| 153 | pr_uid = getuid(); |
| 154 | |
| 155 | /* Get effective uid of LTPUSER user from passwd file */ |
| 156 | if ((user_id = getpwnam(LTPUSER)) == NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | tst_brkm(TBROK, cleanup, "getpwnam(%s) Failed", LTPUSER); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | /* Effective user-id of the test-user LTPUSER */ |
| 161 | pe_uid = user_id->pw_uid; |
| 162 | |
| 163 | /* Saved user-id of the test-user LTPUSER */ |
| 164 | ps_uid = user_id->pw_uid; |
| 165 | |
| 166 | /* |
| 167 | * Set the effective user-id of the process to that of |
| 168 | * test user LTPUSER |
| 169 | * The real user id remains same as of caller. |
| 170 | */ |
| 171 | if (setreuid(-1, ps_uid) < 0) { |
| 172 | tst_brkm(TBROK, cleanup, |
| 173 | "setreuid(-1, %d) Fails, errno:%d : %s", |
| 174 | ps_uid, errno, strerror(errno)); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * void |
| 180 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 181 | * completion or premature exit. |
| 182 | * Restore the test process uid to root. |
| 183 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 184 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | |
| 187 | /* Reset the effective/saved uid of the calling process */ |
| 188 | if (setreuid(-1, pr_uid) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | tst_brkm(TBROK, NULL, "resetting process effective uid failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 192 | } |