1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2018 Linaro Limited. All rights reserved.
4 * Copyright (c) Linux Test Project, 2018-2023
5 * Author: Rafael David Tinoco <[email protected]>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Verify basic fsetxattr(2) syscall functionality:
12 *
13 * - Set attribute to a regular file, fsetxattr(2) should succeed.
14 * - Set attribute to a directory, fsetxattr(2) should succeed.
15 * - Set attribute to a symlink which points to the regular file,
16 * fsetxattr(2) should return -1 and set errno to EEXIST.
17 * - Set attribute to a FIFO, fsetxattr(2) should return -1 and set
18 * errno to EPERM.
19 * - Set attribute to a char special file, fsetxattr(2) should
20 * return -1 and set errno to EPERM.
21 * - Set attribute to a block special file, fsetxattr(2) should
22 * return -1 and set errno to EPERM.
23 * - Set attribute to a UNIX domain socket, fsetxattr(2) should
24 * return -1 and set errno to EPERM.
25 */
26
27 /*
28 * In the user.* namespace, only regular files and directories can
29 * have extended attributes. Otherwise fsetxattr(2) will return -1
30 * and set errno to EPERM.
31 */
32
33 #include "config.h"
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/sysmacros.h>
37 #include <sys/wait.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <signal.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/socket.h>
46 #include <sys/un.h>
47 #ifdef HAVE_SYS_XATTR_H
48 # include <sys/xattr.h>
49 #endif
50 #include "tst_test.h"
51
52 #ifdef HAVE_SYS_XATTR_H
53 #define XATTR_TEST_KEY "user.testkey"
54 #define XATTR_TEST_VALUE "this is a test value"
55 #define XATTR_TEST_VALUE_SIZE 20
56
57 #define MNTPOINT "mntpoint"
58 #define OFFSET 11
59 #define FILENAME "fsetxattr02testfile"
60 #define DIRNAME "fsetxattr02testdir"
61 #define SYMLINK "fsetxattr02symlink"
62 #define FIFO MNTPOINT"/fsetxattr02fifo"
63 #define CHR MNTPOINT"/fsetxattr02chr"
64 #define BLK MNTPOINT"/fsetxattr02blk"
65 #define SOCK "fsetxattr02sock"
66
67 struct test_case {
68 char *fname;
69 int fd;
70 int fflags;
71 char *key;
72 char *value;
73 size_t size;
74 int flags;
75 int exp_err;
76 int issocket;
77 int needskeyset;
78 };
79 static struct test_case tc[] = {
80 { /* case 00, set attr to reg */
81 .fname = FILENAME,
82 .fflags = O_RDONLY,
83 .key = XATTR_TEST_KEY,
84 .value = XATTR_TEST_VALUE,
85 .size = XATTR_TEST_VALUE_SIZE,
86 .flags = XATTR_CREATE,
87 .exp_err = 0,
88 },
89 { /* case 01, set attr to dir */
90 .fname = DIRNAME,
91 .fflags = O_RDONLY,
92 .key = XATTR_TEST_KEY,
93 .value = XATTR_TEST_VALUE,
94 .size = XATTR_TEST_VALUE_SIZE,
95 .flags = XATTR_CREATE,
96 .exp_err = 0,
97 },
98 { /* case 02, set attr to symlink */
99 .fname = SYMLINK,
100 .fflags = O_RDONLY,
101 .key = XATTR_TEST_KEY,
102 .value = XATTR_TEST_VALUE,
103 .size = XATTR_TEST_VALUE_SIZE,
104 .flags = XATTR_CREATE,
105 .exp_err = EEXIST,
106 .needskeyset = 1,
107 },
108 { /* case 03, set attr to fifo */
109 .fname = FIFO,
110 .fflags = (O_RDONLY | O_NONBLOCK),
111 .key = XATTR_TEST_KEY,
112 .value = XATTR_TEST_VALUE,
113 .size = XATTR_TEST_VALUE_SIZE,
114 .flags = XATTR_CREATE,
115 .exp_err = EPERM,
116 },
117 { /* case 04, set attr to character special */
118 .fname = CHR,
119 .fflags = O_RDONLY,
120 .key = XATTR_TEST_KEY,
121 .value = XATTR_TEST_VALUE,
122 .size = XATTR_TEST_VALUE_SIZE,
123 .flags = XATTR_CREATE,
124 .exp_err = EPERM,
125 },
126 { /* case 05, set attr to block special */
127 .fname = BLK,
128 .fflags = O_RDONLY,
129 .key = XATTR_TEST_KEY,
130 .value = XATTR_TEST_VALUE,
131 .size = XATTR_TEST_VALUE_SIZE,
132 .flags = XATTR_CREATE,
133 .exp_err = EPERM,
134 },
135 { /* case 06, set attr to socket */
136 .fname = SOCK,
137 .fflags = O_RDONLY,
138 .key = XATTR_TEST_KEY,
139 .value = XATTR_TEST_VALUE,
140 .size = XATTR_TEST_VALUE_SIZE,
141 .flags = XATTR_CREATE,
142 .exp_err = EPERM,
143 .issocket = 1,
144 },
145 };
146
verify_fsetxattr(unsigned int i)147 static void verify_fsetxattr(unsigned int i)
148 {
149 const char *fname = strstr(tc[i].fname, "fsetxattr02") + OFFSET;
150
151 /* some tests might require existing keys for each iteration */
152 if (tc[i].needskeyset) {
153 SAFE_FSETXATTR(tc[i].fd, tc[i].key, tc[i].value, tc[i].size,
154 XATTR_CREATE);
155 }
156
157 TEST(fsetxattr(tc[i].fd, tc[i].key, tc[i].value, tc[i].size,
158 tc[i].flags));
159
160 if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
161 tst_brk(TCONF, "fsetxattr(2) not supported");
162
163 /* success */
164
165 if (!tc[i].exp_err) {
166 if (TST_RET) {
167 tst_res(TFAIL | TTERRNO,
168 "fsetxattr(2) on %s failed with %li",
169 fname, TST_RET);
170 return;
171 }
172
173 /* this is needed for subsequent iterations */
174 SAFE_FREMOVEXATTR(tc[i].fd, tc[i].key);
175
176 tst_res(TPASS, "fsetxattr(2) on %s passed", fname);
177 return;
178 }
179
180 if (TST_RET == 0) {
181 tst_res(TFAIL, "fsetxattr(2) on %s passed unexpectedly", fname);
182 return;
183 }
184
185 /* fail */
186
187 if (tc[i].exp_err != TST_ERR) {
188 tst_res(TFAIL | TTERRNO,
189 "fsetxattr(2) on %s should have failed with %s",
190 fname, tst_strerrno(tc[i].exp_err));
191 return;
192 }
193
194 /* key might have been added AND test might have failed, remove it */
195 if (tc[i].needskeyset)
196 SAFE_FREMOVEXATTR(tc[i].fd, tc[i].key);
197
198 tst_res(TPASS | TTERRNO, "fsetxattr(2) on %s failed", fname);
199 }
200
setup(void)201 static void setup(void)
202 {
203 size_t i = 0;
204 struct sockaddr_un sun;
205
206 dev_t dev = makedev(1, 3);
207
208 SAFE_TOUCH(FILENAME, 0644, NULL);
209 SAFE_MKDIR(DIRNAME, 0644);
210 SAFE_SYMLINK(FILENAME, SYMLINK);
211
212 /* root: mknod(2) needs it to create something other than a file */
213 SAFE_MKNOD(FIFO, S_IFIFO | 0777, 0);
214 SAFE_MKNOD(CHR, S_IFCHR | 0777, dev);
215 SAFE_MKNOD(BLK, S_IFBLK | 0777, dev);
216
217 for (i = 0; i < ARRAY_SIZE(tc); i++) {
218
219 if (!tc[i].issocket) {
220 tc[i].fd = SAFE_OPEN(tc[i].fname, tc[i].fflags);
221 continue;
222 }
223
224 /* differently than setxattr calls, when dealing with
225 * sockets, mknod() isn't enough to test fsetxattr(2).
226 * we have to get a real unix socket in order for open()
227 * to get a file desc.
228 */
229 tc[i].fd = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
230
231 memset(&sun, 0, sizeof(struct sockaddr_un));
232 sun.sun_family = AF_UNIX;
233 strncpy(sun.sun_path, tc[i].fname, sizeof(sun.sun_path) - 1);
234
235 SAFE_BIND(tc[i].fd, (const struct sockaddr *) &sun,
236 sizeof(struct sockaddr_un));
237 }
238 }
239
cleanup(void)240 static void cleanup(void)
241 {
242 size_t i = 0;
243
244 for (i = 0; i < ARRAY_SIZE(tc); i++) {
245 if (tc[i].fd > 0)
246 SAFE_CLOSE(tc[i].fd);
247 }
248 }
249
250 static struct tst_test test = {
251 .setup = setup,
252 .test = verify_fsetxattr,
253 .cleanup = cleanup,
254 .tcnt = ARRAY_SIZE(tc),
255 .needs_devfs = 1,
256 .mntpoint = MNTPOINT,
257 .needs_root = 1,
258 .needs_drivers = (const char *const[]) {
259 "brd",
260 NULL,
261 },
262 };
263
264 #else /* HAVE_SYS_XATTR_H */
265 TST_TEST_TCONF("<sys/xattr.h> does not exist");
266 #endif
267