1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /* Copyright (c) International Business Machines Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker */
4*49cdfc7eSAndroid Build Coastguard Worker
5*49cdfc7eSAndroid Build Coastguard Worker /*
6*49cdfc7eSAndroid Build Coastguard Worker * Verify that user cannot create a directory inside directory owned by another
7*49cdfc7eSAndroid Build Coastguard Worker * user with restrictive permissions and that the errno is set to EACCESS.
8*49cdfc7eSAndroid Build Coastguard Worker */
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
15*49cdfc7eSAndroid Build Coastguard Worker
16*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR "testdir"
17*49cdfc7eSAndroid Build Coastguard Worker #define TESTSUBDIR "testdir/testdir"
18*49cdfc7eSAndroid Build Coastguard Worker
verify_mkdir(void)19*49cdfc7eSAndroid Build Coastguard Worker static void verify_mkdir(void)
20*49cdfc7eSAndroid Build Coastguard Worker {
21*49cdfc7eSAndroid Build Coastguard Worker if (mkdir(TESTSUBDIR, 0777) != -1) {
22*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "mkdir(%s, %#o) succeeded unexpectedly",
23*49cdfc7eSAndroid Build Coastguard Worker TESTSUBDIR, 0777);
24*49cdfc7eSAndroid Build Coastguard Worker return;
25*49cdfc7eSAndroid Build Coastguard Worker }
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker if (errno != EACCES) {
28*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO, "Expected EACCES got");
29*49cdfc7eSAndroid Build Coastguard Worker return;
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TERRNO, "mkdir() failed expectedly");
33*49cdfc7eSAndroid Build Coastguard Worker }
34*49cdfc7eSAndroid Build Coastguard Worker
setup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker uid_t test_users[2];
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker tst_get_uids(test_users, 0, 2);
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TESTDIR, 0700);
42*49cdfc7eSAndroid Build Coastguard Worker SAFE_CHOWN(TESTDIR, test_users[0], getgid());
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETREUID(test_users[1], test_users[1]);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
48*49cdfc7eSAndroid Build Coastguard Worker .test_all = verify_mkdir,
49*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
50*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
51*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
52*49cdfc7eSAndroid Build Coastguard Worker };
53