robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 1 | /* |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 2 | * Copyright (c) Dan Kegel 2003 |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 3 | * |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 8 | * |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 13 | * |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 19 | /* |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 20 | * Test Name: setegid01 |
| 21 | * |
| 22 | * Test Description: |
| 23 | * Verify that setegid does not modify the saved gid or real gid. |
| 24 | */ |
vapier | bed036a | 2006-05-26 06:48:55 +0000 | [diff] [blame] | 25 | |
| 26 | #define _GNU_SOURCE 1 |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 27 | #include <pwd.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <stdio.h> |
| 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
| 32 | #include "test.h" |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 33 | #include "safe_macros.h" |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 34 | |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 35 | char *TCID = "setegid01"; |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 36 | int TST_TOTAL = 1; |
| 37 | static void setup(void); |
| 38 | static void setegid_verify(void); |
| 39 | static void cleanup(void); |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 40 | |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 41 | static gid_t nobody_gid; |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 42 | |
| 43 | int main(int argc, char **argv) |
| 44 | { |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 45 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 46 | const char *msg; |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 47 | |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 48 | msg = parse_opts(argc, argv, NULL, NULL); |
| 49 | if (msg != NULL) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 50 | tst_brkm(TBROK, NULL, "Option parsing error - %s", msg); |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 51 | |
| 52 | setup(); |
| 53 | |
| 54 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 55 | tst_count = 0; |
| 56 | setegid_verify(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | } |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 58 | |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 59 | cleanup(); |
| 60 | tst_exit(); |
| 61 | } |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 62 | |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 63 | static void setup(void) |
| 64 | { |
| 65 | struct passwd *nobody; |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 66 | |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 67 | tst_require_root(NULL); |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 68 | |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 69 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 70 | |
| 71 | TEST_PAUSE; |
| 72 | |
| 73 | nobody = SAFE_GETPWNAM(cleanup, "nobody"); |
| 74 | |
| 75 | nobody_gid = nobody->pw_gid; |
| 76 | } |
| 77 | |
| 78 | static void setegid_verify(void) |
| 79 | { |
| 80 | gid_t cur_rgid, cur_egid, cur_sgid; |
| 81 | gid_t orig_rgid, orig_egid, orig_sgid; |
| 82 | |
| 83 | SAFE_GETRESGID(cleanup, &orig_rgid, &orig_egid, &orig_sgid); |
| 84 | tst_resm(TINFO, "getresgid reports rgid %d, egid %d, sgid %d", |
| 85 | orig_rgid, orig_egid, orig_sgid); |
| 86 | |
| 87 | tst_resm(TINFO, "calling setegid(nobody_gid %d)", nobody_gid); |
| 88 | SAFE_SETEGID(cleanup, nobody_gid); |
| 89 | |
| 90 | SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &cur_sgid); |
| 91 | tst_resm(TINFO, "getresgid reports rgid %d, egid %d, sgid %d", cur_rgid, |
| 92 | cur_egid, cur_sgid); |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 93 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 94 | /* make sure it at least does what its name says */ |
| 95 | if (nobody_gid != cur_egid) { |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 96 | tst_resm(TFAIL, "setegid() failed to change the effective gid"); |
| 97 | return; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | } |
robbiew | 438efb6 | 2003-04-08 15:39:09 +0000 | [diff] [blame] | 99 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | /* SUSv3 says the real group ID and saved set-gid must |
| 101 | * remain unchanged by setgid. See |
| 102 | * http://www.opengroup.org/onlinepubs/007904975/functions/setegid.html |
| 103 | */ |
| 104 | if (orig_sgid != cur_sgid) { |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 105 | tst_resm(TFAIL, "setegid() changed the saved set-gid"); |
| 106 | return; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | } |
| 108 | if (orig_rgid != cur_rgid) { |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 109 | tst_resm(TFAIL, "setegid() changed the real gid"); |
| 110 | return; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | } |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 112 | |
| 113 | SAFE_SETEGID(cleanup, orig_egid); |
| 114 | |
| 115 | SAFE_GETRESGID(cleanup, &cur_rgid, &cur_egid, &orig_sgid); |
| 116 | |
| 117 | if (orig_egid != cur_egid) { |
| 118 | tst_resm(TFAIL, "setegid() failed to reset effective gid back"); |
| 119 | return; |
| 120 | } |
| 121 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | tst_resm(TPASS, "setegid() passed functional test"); |
Zeng Linggang | b5ec8a1 | 2014-04-25 10:47:42 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static void cleanup(void) |
| 126 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 127 | } |