1 /*
2 * Check decoding of fchmodat syscall.
3 *
4 * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2016-2021 The strace developers.
6 * All rights reserved.
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "tests.h"
12 #include "scno.h"
13
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #include "secontext.h"
19
20 int
21 main(void)
22 {
23 /*
24 * Make sure the current workdir of the tracee
25 * is different from the current workdir of the tracer.
26 */
27 create_and_enter_subdir("fchmodat_subdir");
28
29 char *my_secontext = SECONTEXT_PID_MY();
30
31 static const char sample[] = "fchmodat_sample_file";
32 if (open(sample, O_RDONLY | O_CREAT, 0400) < 0)
33 perror_msg_and_fail("open");
34
35 char *sample_secontext = SECONTEXT_FILE(sample);
36
37 /*
38 * Tests with AT_FDCWD.
39 */
40
41 long rc = syscall(__NR_fchmodat, -100, sample, 0600);
42 printf("%s%s(AT_FDCWD, \"%s\"%s, 0600) = %s\n",
43 my_secontext, "fchmodat",
44 sample, sample_secontext,
45 sprintrc(rc));
46
47 if (unlink(sample))
48 perror_msg_and_fail("unlink");
49
50 rc = syscall(__NR_fchmodat, -100, sample, 051);
51 printf("%s%s(AT_FDCWD, \"%s\", 051) = %s\n",
52 my_secontext, "fchmodat",
53 sample, sprintrc(rc));
54
55 rc = syscall(__NR_fchmodat, -100, sample, 004);
56 printf("%s%s(AT_FDCWD, \"%s\", 004) = %s\n",
57 my_secontext, "fchmodat",
58 sample, sprintrc(rc));
59
60 /*
61 * Tests with dirfd.
62 */
63
64 int cwd_fd = get_dir_fd(".");
65 char *cwd = get_fd_path(cwd_fd);
66 char *cwd_secontext = SECONTEXT_FILE(".");
67 char *sample_realpath = xasprintf("%s/%s", cwd, sample);
68
69 /* no file */
70 rc = syscall(__NR_fchmodat, cwd_fd, sample, 0400);
71 printf("%s%s(%d%s, \"%s\", 0400) = %s\n",
72 my_secontext, "fchmodat",
73 cwd_fd, cwd_secontext,
74 sample,
75 sprintrc(rc));
76
77 if (open(sample, O_RDONLY | O_CREAT, 0400) < 0)
78 perror_msg_and_fail("open");
79
80 rc = syscall(__NR_fchmodat, cwd_fd, sample, 0400);
81 printf("%s%s(%d%s, \"%s\"%s, 0400) = %s\n",
82 my_secontext, "fchmodat",
83 cwd_fd, cwd_secontext,
84 sample, sample_secontext,
85 sprintrc(rc));
86
87 /* cwd_fd ignored when path is absolute */
88 if (chdir("../.."))
89 perror_msg_and_fail("chdir");
90
91 rc = syscall(__NR_fchmodat, cwd_fd, sample_realpath, 0400);
92 printf("%s%s(%d%s, \"%s\"%s, 0400) = %s\n",
93 my_secontext, "fchmodat",
94 cwd_fd, cwd_secontext,
95 sample_realpath, sample_secontext,
96 sprintrc(rc));
97
98 if (fchdir(cwd_fd))
99 perror_msg_and_fail("fchdir");
100
101 if (unlink(sample))
102 perror_msg_and_fail("unlink");
103
104 leave_and_remove_subdir();
105
106 puts("+++ exited with 0 +++");
107 return 0;
108 }