1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) International Business Machines Corp., 2001
3 */
4
5 /*
6 * Verify that user cannot create a directory inside directory owned by another
7 * user with restrictive permissions and that the errno is set to EACCESS.
8 */
9
10 #include <errno.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <pwd.h>
14 #include <sys/wait.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include "tst_test.h"
18
19 #define TESTDIR "testdir"
20 #define TESTSUBDIR "testdir/testdir"
21
22 static uid_t nobody_uid, bin_uid;
23
verify_mkdir(void)24 static void verify_mkdir(void)
25 {
26 if (mkdir(TESTSUBDIR, 0777) != -1) {
27 tst_res(TFAIL, "mkdir(%s, %#o) succeeded unexpectedly",
28 TESTSUBDIR, 0777);
29 return;
30 }
31
32 if (errno != EACCES) {
33 tst_res(TFAIL | TERRNO, "Expected EACCES got");
34 return;
35 }
36
37 tst_res(TPASS | TERRNO, "mkdir() failed expectedly");
38 }
39
setup(void)40 static void setup(void)
41 {
42 struct passwd *pw;
43 pid_t pid;
44
45 pw = SAFE_GETPWNAM("nobody");
46 nobody_uid = pw->pw_uid;
47 pw = SAFE_GETPWNAM("bin");
48 bin_uid = pw->pw_uid;
49
50 pid = SAFE_FORK();
51 if (pid == 0) {
52 SAFE_SETREUID(nobody_uid, nobody_uid);
53 SAFE_MKDIR(TESTDIR, 0700);
54 exit(0);
55 }
56
57 tst_reap_children();
58
59 SAFE_SETREUID(bin_uid, bin_uid);
60 }
61
62 static struct tst_test test = {
63 .test_all = verify_mkdir,
64 .needs_tmpdir = 1,
65 .needs_root = 1,
66 .setup = setup,
67 .forks_child = 1,
68 };
69