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