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