1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) Bull S.A. 2001
4 * Copyright (c) International Business Machines Corp., 2001
5 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
6 */
7
8 /*
9 * DESCRIPTION
10 * Testcase for testing that fchdir(2) sets EACCES errno
11 *
12 * ALGORITHM
13 * 1. create a child process, sets its uid to ltpuser1
14 * 2. this child creates a directory with perm 400,
15 * 3. this child opens the directory and gets a file descriptor
16 * 4. this child attempts to fchdir(2) to the directory created in 2.
17 * and expects to get an EACCES.
18 */
19
20 #include <errno.h>
21 #include <pwd.h>
22 #include "tst_test.h"
23
24 #define DIRNAME "fchdir03_dir"
25
26 static int fd;
27
verify_fchdir(void)28 void verify_fchdir(void)
29 {
30 TEST(fchdir(fd));
31
32 if (TST_RET != -1) {
33 tst_res(TFAIL, "fchdir() succeeded unexpectedly");
34 return;
35 }
36
37 if (TST_ERR != EACCES) {
38 tst_res(TFAIL | TTERRNO, "fchdir() should fail with EACCES");
39 return;
40 }
41
42 tst_res(TPASS | TTERRNO, "fchdir() failed expectedly");
43 }
44
setup(void)45 void setup(void)
46 {
47 struct passwd *pw;
48
49 pw = SAFE_GETPWNAM("nobody");
50 SAFE_SETEUID(pw->pw_uid);
51 SAFE_MKDIR(DIRNAME, 0400);
52
53 fd = SAFE_OPEN(DIRNAME, O_RDONLY);
54 }
55
56 static struct tst_test test = {
57 .setup = setup,
58 .test_all = verify_fchdir,
59 .needs_tmpdir = 1,
60 .needs_root = 1,
61 };
62