1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
5 */
6
7 /*
8 * DESCRIPTION
9 * Testcase for testing that chdir(2) sets EACCES errno
10 *
11 * ALGORITHM
12 * 1. running as root, create a directory with perm 700
13 * 2. create a child process, sets its uid to nobody
14 * 3. this child attempts to chdir(2) to the directory created in 1
15 * and expects to get an EACCES.
16 */
17
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include "tst_test.h"
22
23 #define DIRNAME "chdir03_dir"
24
25 static uid_t nobody_uid;
26
verify_chdir(void)27 void verify_chdir(void)
28 {
29 pid_t pid;
30
31 pid = SAFE_FORK();
32 if (!pid) {
33 SAFE_SETUID(nobody_uid);
34
35 TEST(chdir(DIRNAME));
36
37 if (TST_RET != -1) {
38 tst_res(TFAIL, "chdir() succeeded unexpectedly");
39 return;
40 }
41
42 if (TST_ERR != EACCES) {
43 tst_res(TFAIL | TTERRNO,
44 "chdir() should fail with EACCES");
45 return;
46 }
47
48 tst_res(TPASS | TTERRNO, "chdir() failed expectedly");
49 }
50 }
51
setup(void)52 void setup(void)
53 {
54 struct passwd *pw;
55
56 pw = SAFE_GETPWNAM("nobody");
57 nobody_uid = pw->pw_uid;
58
59 SAFE_MKDIR(DIRNAME, 0700);
60 }
61
62 static struct tst_test test = {
63 .setup = setup,
64 .test_all = verify_chdir,
65 .needs_tmpdir = 1,
66 .needs_root = 1,
67 .forks_child = 1,
68 };
69