• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2018 Linaro Limited. All rights reserved.
4  * Author: Rafael David Tinoco <rafael.tinoco@linaro.org>
5  */
6 
7 /*
8  * Basic tests for fsetxattr(2) and make sure fsetxattr(2) handles error
9  * conditions correctly.
10  *
11  * There are 9 test cases:
12  * 1. Any other flags being set except XATTR_CREATE and XATTR_REPLACE,
13  *    fsetxattr(2) should return -1 and set errno to EINVAL
14  * 2. With XATTR_REPLACE flag set but the attribute does not exist,
15  *    fsetxattr(2) should return -1 and set errno to ENODATA
16  * 3. Create new attr with name length greater than XATTR_NAME_MAX(255)
17  *    fsetxattr(2) should return -1 and set errno to ERANGE
18  * 4. Create new attr whose value length is greater than XATTR_SIZE_MAX(65536)
19  *    fsetxattr(2) should return -1 and set errno to E2BIG
20  * 5. Create new attr whose value length is zero,
21  *    fsetxattr(2) should succeed
22  * 6. Replace the attr value without XATTR_REPLACE flag being set,
23  *    fsetxattr(2) should return -1 and set errno to EEXIST
24  * 7. Replace attr value with XATTR_REPLACE flag being set,
25  *    fsetxattr(2) should succeed
26  * 8. Create new attr whose key length is zero,
27  *    fsetxattr(2) should return -1 and set errno to ERANGE
28  * 9. Create new attr whose key is NULL,
29  *    fsetxattr(2) should return -1 and set errno to EFAULT
30  */
31 
32 #include "config.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/wait.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #ifdef HAVE_SYS_XATTR_H
44 # include <sys/xattr.h>
45 #endif
46 #include "tst_test.h"
47 
48 #ifdef HAVE_SYS_XATTR_H
49 #define XATTR_NAME_MAX 255
50 #define XATTR_NAME_LEN (XATTR_NAME_MAX + 2)
51 #define XATTR_SIZE_MAX 65536
52 #define XATTR_TEST_KEY "user.testkey"
53 #define XATTR_TEST_VALUE "this is a test value"
54 #define XATTR_TEST_VALUE_SIZE 20
55 #define MNTPOINT "mntpoint"
56 #define FNAME MNTPOINT"/fsetxattr01testfile"
57 
58 static int fd = -1;
59 static char long_key[XATTR_NAME_LEN];
60 static char *long_value;
61 static char *xattr_value = XATTR_TEST_VALUE;
62 
63 struct test_case {
64 	char *key;
65 	char **value;
66 	size_t size;
67 	int flags;
68 	int exp_err;
69 	int keyneeded;
70 };
71 
72 struct test_case tc[] = {
73 	{			/* case 00, invalid flags */
74 	 .key = XATTR_TEST_KEY,
75 	 .value = &xattr_value,
76 	 .size = XATTR_TEST_VALUE_SIZE,
77 	 .flags = ~0,
78 	 .exp_err = EINVAL,
79 	 },
80 	{			/* case 01, replace non-existing attribute */
81 	 .key = XATTR_TEST_KEY,
82 	 .value = &xattr_value,
83 	 .size = XATTR_TEST_VALUE_SIZE,
84 	 .flags = XATTR_REPLACE,
85 	 .exp_err = ENODATA,
86 	 },
87 	{			/* case 02, long key name */
88 	 .key = long_key,
89 	 .value = &xattr_value,
90 	 .size = XATTR_TEST_VALUE_SIZE,
91 	 .flags = XATTR_CREATE,
92 	 .exp_err = ERANGE,
93 	 },
94 	{			/* case 03, long value */
95 	 .key = XATTR_TEST_KEY,
96 	 .value = &long_value,
97 	 .size = XATTR_SIZE_MAX + 1,
98 	 .flags = XATTR_CREATE,
99 	 .exp_err = E2BIG,
100 	 },
101 	{			/* case 04, zero length value */
102 	 .key = XATTR_TEST_KEY,
103 	 .value = &xattr_value,
104 	 .size = 0,
105 	 .flags = XATTR_CREATE,
106 	 .exp_err = 0,
107 	 },
108 	{			/* case 05, create existing attribute */
109 	 .key = XATTR_TEST_KEY,
110 	 .value = &xattr_value,
111 	 .size = XATTR_TEST_VALUE_SIZE,
112 	 .flags = XATTR_CREATE,
113 	 .exp_err = EEXIST,
114 	 .keyneeded = 1,
115 	 },
116 	{			/* case 06, replace existing attribute */
117 	 .key = XATTR_TEST_KEY,
118 	 .value = &xattr_value,
119 	 .size = XATTR_TEST_VALUE_SIZE,
120 	 .flags = XATTR_REPLACE,
121 	 .exp_err = 0,
122 	 .keyneeded = 1,
123 	},
124 	{			/* case 07, zero length key */
125 	 .key = "",
126 	 .value = &xattr_value,
127 	 .size = XATTR_TEST_VALUE_SIZE,
128 	 .flags = XATTR_CREATE,
129 	 .exp_err = ERANGE,
130 	},
131 	{			/* case 08, NULL key */
132 	 .value = &xattr_value,
133 	 .size = XATTR_TEST_VALUE_SIZE,
134 	 .flags = XATTR_CREATE,
135 	 .exp_err = EFAULT,
136 	},
137 };
138 
verify_fsetxattr(unsigned int i)139 static void verify_fsetxattr(unsigned int i)
140 {
141 	/* some tests might require existing keys for each iteration */
142 	if (tc[i].keyneeded) {
143 		SAFE_FSETXATTR(fd, tc[i].key, tc[i].value, tc[i].size,
144 				XATTR_CREATE);
145 	}
146 
147 	TEST(fsetxattr(fd, tc[i].key, *tc[i].value, tc[i].size, tc[i].flags));
148 
149 	if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
150 		tst_brk(TCONF, "fsetxattr(2) not supported");
151 
152 	/* success */
153 
154 	if (!tc[i].exp_err) {
155 		if (TST_RET) {
156 			tst_res(TFAIL | TTERRNO,
157 				"fsetxattr(2) failed with %li", TST_RET);
158 			return;
159 		}
160 
161 		/* this is needed for subsequent iterations */
162 		SAFE_FREMOVEXATTR(fd, tc[i].key);
163 
164 		tst_res(TPASS, "fsetxattr(2) passed");
165 
166 		return;
167 	}
168 
169 	if (TST_RET == 0) {
170 		tst_res(TFAIL, "fsetxattr(2) passed unexpectedly");
171 		return;
172 	}
173 
174 	/* error */
175 
176 	if (tc[i].exp_err != TST_ERR) {
177 		tst_res(TFAIL | TTERRNO, "fsetxattr(2) should fail with %s",
178 			tst_strerrno(tc[i].exp_err));
179 		return;
180 	}
181 
182 	/* key might have been added AND test might have failed, remove it */
183 	if (tc[i].keyneeded)
184 		SAFE_FREMOVEXATTR(fd, tc[i].key);
185 
186 	tst_res(TPASS | TTERRNO, "fsetxattr(2) failed");
187 }
188 
cleanup(void)189 static void cleanup(void)
190 {
191 	if (fd > 0)
192 		SAFE_CLOSE(fd);
193 }
194 
setup(void)195 static void setup(void)
196 {
197 	size_t i = 0;
198 
199 	snprintf(long_key, 6, "%s", "user.");
200 	memset(long_key + 5, 'k', XATTR_NAME_LEN - 5);
201 	long_key[XATTR_NAME_LEN - 1] = '\0';
202 
203 	long_value = SAFE_MALLOC(XATTR_SIZE_MAX + 2);
204 	memset(long_value, 'v', XATTR_SIZE_MAX + 2);
205 	long_value[XATTR_SIZE_MAX + 1] = '\0';
206 
207 	SAFE_TOUCH(FNAME, 0644, NULL);
208 	fd = SAFE_OPEN(FNAME, O_RDONLY);
209 
210 	for (i = 0; i < ARRAY_SIZE(tc); i++) {
211 		if (!tc[i].key)
212 			tc[i].key = tst_get_bad_addr(cleanup);
213 	}
214 }
215 
216 static struct tst_test test = {
217 	.setup = setup,
218 	.test = verify_fsetxattr,
219 	.cleanup = cleanup,
220 	.tcnt = ARRAY_SIZE(tc),
221 	.mntpoint = MNTPOINT,
222 	.mount_device = 1,
223 	.all_filesystems = 1,
224 	.needs_root = 1,
225 };
226 
227 #else /* HAVE_SYS_XATTR_H */
228 TST_TEST_TCONF("<sys/xattr.h> does not exist");
229 #endif
230