blob: 65976f3b9d8f148a4a0bf8709dc47856377ae286 [file] [log] [blame]
robbiew438efb62003-04-08 15:39:09 +00001/*
Zeng Linggangb5ec8a12014-04-25 10:47:42 +08002 * Copyright (c) Dan Kegel 2003
robbiew438efb62003-04-08 15:39:09 +00003 *
Zeng Linggangb5ec8a12014-04-25 10:47:42 +08004 * 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.
robbiew438efb62003-04-08 15:39:09 +00008 *
Zeng Linggangb5ec8a12014-04-25 10:47:42 +08009 * 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.
robbiew438efb62003-04-08 15:39:09 +000013 *
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080014 * 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
robbiew438efb62003-04-08 15:39:09 +000017 */
18
subrata_modak4bb656a2009-02-26 12:02:09 +000019/*
robbiew438efb62003-04-08 15:39:09 +000020 * Test Name: setegid01
21 *
22 * Test Description:
23 * Verify that setegid does not modify the saved gid or real gid.
24 */
vapierbed036a2006-05-26 06:48:55 +000025
26#define _GNU_SOURCE 1
robbiew438efb62003-04-08 15:39:09 +000027#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 Linggangb5ec8a12014-04-25 10:47:42 +080033#include "safe_macros.h"
robbiew438efb62003-04-08 15:39:09 +000034
robbiew438efb62003-04-08 15:39:09 +000035char *TCID = "setegid01";
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080036int TST_TOTAL = 1;
37static void setup(void);
38static void setegid_verify(void);
39static void cleanup(void);
robbiew438efb62003-04-08 15:39:09 +000040
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080041static gid_t nobody_gid;
robbiew438efb62003-04-08 15:39:09 +000042
43int main(int argc, char **argv)
44{
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080045 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020046 const char *msg;
robbiew438efb62003-04-08 15:39:09 +000047
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080048 msg = parse_opts(argc, argv, NULL, NULL);
49 if (msg != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +000050 tst_brkm(TBROK, NULL, "Option parsing error - %s", msg);
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080051
52 setup();
53
54 for (lc = 0; TEST_LOOPING(lc); lc++) {
55 tst_count = 0;
56 setegid_verify();
subrata_modak56207ce2009-03-23 13:35:39 +000057 }
robbiew438efb62003-04-08 15:39:09 +000058
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080059 cleanup();
60 tst_exit();
61}
robbiew438efb62003-04-08 15:39:09 +000062
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080063static void setup(void)
64{
65 struct passwd *nobody;
robbiew438efb62003-04-08 15:39:09 +000066
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080067 tst_require_root(NULL);
robbiew438efb62003-04-08 15:39:09 +000068
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080069 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
78static 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);
robbiew438efb62003-04-08 15:39:09 +000093
subrata_modak56207ce2009-03-23 13:35:39 +000094 /* make sure it at least does what its name says */
95 if (nobody_gid != cur_egid) {
Zeng Linggangb5ec8a12014-04-25 10:47:42 +080096 tst_resm(TFAIL, "setegid() failed to change the effective gid");
97 return;
subrata_modak56207ce2009-03-23 13:35:39 +000098 }
robbiew438efb62003-04-08 15:39:09 +000099
subrata_modak56207ce2009-03-23 13:35:39 +0000100 /* 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 Linggangb5ec8a12014-04-25 10:47:42 +0800105 tst_resm(TFAIL, "setegid() changed the saved set-gid");
106 return;
subrata_modak56207ce2009-03-23 13:35:39 +0000107 }
108 if (orig_rgid != cur_rgid) {
Zeng Linggangb5ec8a12014-04-25 10:47:42 +0800109 tst_resm(TFAIL, "setegid() changed the real gid");
110 return;
subrata_modak56207ce2009-03-23 13:35:39 +0000111 }
Zeng Linggangb5ec8a12014-04-25 10:47:42 +0800112
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_modak56207ce2009-03-23 13:35:39 +0000122 tst_resm(TPASS, "setegid() passed functional test");
Zeng Linggangb5ec8a12014-04-25 10:47:42 +0800123}
124
125static void cleanup(void)
126{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700127}