1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
// SPDX-License-Identifier: GPL-2.0-or-later
#define _GNU_SOURCE
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syscall.h>
#include <unistd.h>
#include "kselftest.h"
struct testdir {
char *dirname;
int dfd;
};
int sys_fchmodat2(int dfd, const char *filename, mode_t mode, int flags)
{
int ret = syscall(__NR_fchmodat2, dfd, filename, mode, flags);
return ret >= 0 ? ret : -errno;
}
static void setup_testdir(struct testdir *testdir)
{
int ret, dfd;
char dirname[] = "/tmp/ksft-fchmodat2.XXXXXX";
/* Make the top-level directory. */
if (!mkdtemp(dirname))
ksft_exit_fail_msg("%s: failed to create tmpdir\n", __func__);
dfd = open(dirname, O_PATH | O_DIRECTORY);
if (dfd < 0) {
ksft_perror("failed to open tmpdir");
goto err;
}
ret = openat(dfd, "regfile", O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (ret < 0) {
ksft_perror("failed to create file in tmpdir");
goto err;
}
close(ret);
ret = symlinkat("regfile", dfd, "symlink");
if (ret < 0) {
ksft_perror("symlinkat() failed");
goto err_regfile;
}
testdir->dirname = strdup(dirname);
if (!testdir->dirname) {
ksft_perror("Out of memory");
goto err_symlink;
}
testdir->dfd = dfd;
return;
err_symlink:
unlinkat(testdir->dfd, "symlink", 0);
err_regfile:
unlinkat(testdir->dfd, "regfile", 0);
err:
unlink(dirname);
ksft_exit_fail();
}
static void cleanup_testdir(struct testdir *testdir)
{
unlinkat(testdir->dfd, "regfile", 0);
unlinkat(testdir->dfd, "symlink", 0);
rmdir(testdir->dirname);
free(testdir->dirname);
}
int expect_mode(int dfd, const char *filename, mode_t expect_mode)
{
struct stat st;
int ret = fstatat(dfd, filename, &st, AT_SYMLINK_NOFOLLOW);
if (ret) {
ksft_perror("fstatat() failed\n");
return 0;
}
return (st.st_mode == expect_mode);
}
void test_regfile(void)
{
struct testdir testdir;
int ret;
setup_testdir(&testdir);
ret = sys_fchmodat2(testdir.dfd, "regfile", 0640, 0);
if (ret < 0) {
ksft_perror("fchmodat2(noflag) failed");
goto out;
}
if (!expect_mode(testdir.dfd, "regfile", 0100640)) {
ksft_print_msg("%s: wrong file mode bits after fchmodat2\n",
__func__);
ret = 1;
goto out;
}
ret = sys_fchmodat2(testdir.dfd, "regfile", 0600, AT_SYMLINK_NOFOLLOW);
if (ret < 0) {
ksft_perror("fchmodat2(AT_SYMLINK_NOFOLLOW) failed");
goto out;
}
if (!expect_mode(testdir.dfd, "regfile", 0100600)) {
ksft_print_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
__func__);
ret = 1;
}
out:
ksft_test_result(ret == 0, "fchmodat2(regfile)\n");
cleanup_testdir(&testdir);
}
void test_symlink(void)
{
struct testdir testdir;
int ret;
setup_testdir(&testdir);
ret = sys_fchmodat2(testdir.dfd, "symlink", 0640, 0);
if (ret < 0) {
ksft_perror("fchmodat2(noflag) failed");
goto err;
}
if (!expect_mode(testdir.dfd, "regfile", 0100640)) {
ksft_print_msg("%s: wrong file mode bits after fchmodat2\n",
__func__);
goto err;
}
if (!expect_mode(testdir.dfd, "symlink", 0120777)) {
ksft_print_msg("%s: wrong symlink mode bits after fchmodat2\n",
__func__);
goto err;
}
ret = sys_fchmodat2(testdir.dfd, "symlink", 0600, AT_SYMLINK_NOFOLLOW);
/*
* On certain filesystems (xfs or btrfs), chmod operation fails. So we
* first check the symlink target but if the operation fails we mark the
* test as skipped.
*
* https://sourceware.org/legacy-ml/libc-alpha/2020-02/msg00467.html
*/
if (ret == 0 && !expect_mode(testdir.dfd, "symlink", 0120600)) {
ksft_print_msg("%s: wrong symlink mode bits after fchmodat2 with nofollow\n",
__func__);
ret = 1;
goto err;
}
if (!expect_mode(testdir.dfd, "regfile", 0100640)) {
ksft_print_msg("%s: wrong file mode bits after fchmodat2 with nofollow\n",
__func__);
}
if (ret != 0)
ksft_test_result_skip("fchmodat2(symlink)\n");
else
ksft_test_result_pass("fchmodat2(symlink)\n");
cleanup_testdir(&testdir);
return;
err:
ksft_test_result_fail("fchmodat2(symlink)\n");
cleanup_testdir(&testdir);
}
#define NUM_TESTS 2
int main(int argc, char **argv)
{
ksft_print_header();
ksft_set_plan(NUM_TESTS);
test_regfile();
test_symlink();
ksft_finished();
}
|