• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  */
5 
6 /*
7  * DESCRIPTION
8  *	Check if setuid behaves correctly with file permissions. The test
9  *	creates a file as ROOT with permissions 0644, does a setuid and then
10  *	tries to open the file with RDWR permissions. The same test is done
11  *	in a fork to check if new UIDs are correctly passed to the son.
12  */
13 
14 #include <errno.h>
15 #include <pwd.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <fcntl.h>
19 #include "tst_test.h"
20 #include "compat_tst_16.h"
21 
22 #define  FILENAME  "setuid04_testfile"
23 
24 static void dosetuid(void);
25 
verify_setuid(void)26 static void verify_setuid(void)
27 {
28 	pid_t pid;
29 
30 	pid = SAFE_FORK();
31 	if (!pid)
32 		dosetuid();
33 	else
34 		dosetuid();
35 }
36 
dosetuid(void)37 static void dosetuid(void)
38 {
39 	int tst_fd;
40 
41 	TEST(tst_fd = open(FILENAME, O_RDWR));
42 	if (TST_RET != -1) {
43 		tst_res(TFAIL, "open() succeeded unexpectedly");
44 		close(tst_fd);
45 		return;
46 	}
47 
48 	if (TST_ERR == EACCES)
49 		tst_res(TPASS, "open() returned errno EACCES");
50 	else
51 		tst_res(TFAIL | TTERRNO, "open() returned unexpected errno");
52 }
53 
setup(void)54 static void setup(void)
55 {
56 	struct passwd *pw;
57 	uid_t uid;
58 
59 	pw = SAFE_GETPWNAM("nobody");
60 	uid = pw->pw_uid;
61 
62 	UID16_CHECK(uid, setuid);
63 	/* Create test file */
64 	SAFE_TOUCH(FILENAME, 0644, NULL);
65 
66 	if (SETUID(uid) == -1) {
67 		tst_brk(TBROK,
68 			"setuid() failed to set the effective uid to %d", uid);
69 	}
70 }
71 
72 static struct tst_test test = {
73 	.setup = setup,
74 	.needs_root = 1,
75 	.test_all = verify_setuid,
76 	.needs_tmpdir = 1,
77 	.forks_child = 1,
78 };
79