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