1 /*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17 /*
18 * DESCRIPTION
19 * The calling process is not privileged and euid is not appropriate,
20 * EPERM should return.
21 */
22
23 #include <errno.h>
24 #include <pwd.h>
25 #include "test.h"
26 #include "safe_macros.h"
27
28 char *TCID = "setegid02";
29 int TST_TOTAL = 1;
30 static void setup(void);
31 static void setegid_verify(void);
32 static void cleanup(void);
33
34 static struct passwd *ltpuser;
35
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38 int lc;
39
40 tst_parse_opts(argc, argv, NULL, NULL);
41
42 setup();
43
44 for (lc = 0; TEST_LOOPING(lc); lc++) {
45 tst_count = 0;
46 setegid_verify();
47 }
48
49 cleanup();
50 tst_exit();
51 }
52
setup(void)53 static void setup(void)
54 {
55 tst_require_root();
56
57 tst_sig(NOFORK, DEF_HANDLER, cleanup);
58
59 TEST_PAUSE;
60
61 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
62
63 SAFE_SETEUID(cleanup, ltpuser->pw_uid);
64 }
65
setegid_verify(void)66 static void setegid_verify(void)
67 {
68 TEST(setegid(ltpuser->pw_gid));
69
70 if (TEST_RETURN != -1) {
71 tst_resm(TFAIL, "setegid(%d) succeeded unexpectedly",
72 ltpuser->pw_gid);
73 return;
74 }
75
76 if (TEST_ERRNO == EPERM) {
77 tst_resm(TPASS | TTERRNO, "setegid failed as expected");
78 } else {
79 tst_resm(TFAIL | TTERRNO,
80 "setegid failed unexpectedly; expected: %d - %s",
81 EPERM, strerror(EPERM));
82 }
83 }
84
cleanup(void)85 static void cleanup(void)
86 {
87 }
88