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  * Test Description:
19  *  Verify that,
20  *	File system UID is always set to the same value as the (possibly new)
21  *	effective UID.
22  */
23 
24 #define _GNU_SOURCE
25 
26 #include <errno.h>
27 #include <unistd.h>
28 #include <pwd.h>
29 #include <sys/stat.h>
30 #include "test.h"
31 #include "safe_macros.h"
32 #include "compat_16.h"
33 
34 TCID_DEFINE(setresuid05);
35 int TST_TOTAL = 1;
36 static struct passwd *ltpuser;
37 static void setup(void);
38 static void setresuid_verify(void);
39 static void cleanup(void);
40 
41 int main(int argc, char **argv)
42 {
43 	int i, lc;
44 
45 	tst_parse_opts(argc, argv, NULL, NULL);
46 
47 	setup();
48 
49 	for (lc = 0; TEST_LOOPING(lc); lc++) {
50 		tst_count = 0;
51 		for (i = 0; i < TST_TOTAL; i++)
52 			setresuid_verify();
53 	}
54 
55 	cleanup();
56 	tst_exit();
57 }
58 
59 static void setup(void)
60 {
61 	tst_require_root();
62 
63 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
64 
65 	TEST_PAUSE;
66 
67 	tst_tmpdir();
68 
69 	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
70 
71 	UID16_CHECK(ltpuser->pw_uid, "setresuid", cleanup)
72 }
73 
74 static void setresuid_verify(void)
75 {
76 	struct stat buf;
77 
78 	TEST(SETRESUID(cleanup, -1, ltpuser->pw_uid, -1));
79 
80 	if (TEST_RETURN != 0) {
81 		tst_resm(TFAIL | TTERRNO, "setresuid failed unexpectedly");
82 		return;
83 	}
84 
85 	SAFE_TOUCH(cleanup, "test_file", 0644, NULL);
86 
87 	SAFE_STAT(cleanup, "test_file", &buf);
88 
89 	if (ltpuser->pw_uid == buf.st_uid) {
90 		tst_resm(TPASS, "setresuid succeeded as expected");
91 	} else {
92 		tst_resm(TFAIL,
93 			 "setresuid failed unexpectedly; euid(%d) - st_uid(%d)",
94 			 ltpuser->pw_uid, buf.st_uid);
95 	}
96 }
97 
98 static void cleanup(void)
99 {
100 	if (seteuid(0) < 0)
101 		tst_resm(TWARN | TERRNO, "seteuid failed");
102 
103 	tst_rmdir();
104 }
105