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