1 /*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it
13 * is free of the rightful claim of any third person regarding
14 * infringement or the like. Any license provided herein, whether
15 * implied or otherwise, applies only to this software file. Patent
16 * licenses, if any, provided herein do not apply to combinations of
17 * this program with other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
23 */
24
25 /*
26 * Basic tests for setxattr(2) and make sure setxattr(2) handles error
27 * conditions correctly.
28 *
29 * There are 7 test cases:
30 * 1. Any other flags being set except XATTR_CREATE and XATTR_REPLACE,
31 * setxattr(2) should return -1 and set errno to EINVAL
32 * 2. With XATTR_REPLACE flag set but the attribute does not exist,
33 * setxattr(2) should return -1 and set errno to ENODATA
34 * 3. Create new attr with name length greater than XATTR_NAME_MAX(255)
35 * setxattr(2) should return -1 and set errno to ERANGE
36 * 4. Create new attr whose value length is greater than XATTR_SIZE_MAX(65536)
37 * setxattr(2) should return -1 and set errno to E2BIG
38 * 5. Create new attr whose value length is zero,
39 * setxattr(2) should succeed
40 * 6. Replace the attr value without XATTR_REPLACE flag being set,
41 * setxattr(2) should return -1 and set errno to EEXIST
42 * 7. Replace attr value with XATTR_REPLACE flag being set,
43 * setxattr(2) should succeed
44 * 8. Create new attr whose key length is zero,
45 * setxattr(2) should return -1 and set errno to ERANGE
46 * 9. Create new attr whose key is NULL,
47 * setxattr(2) should return -1 and set errno to EFAULT
48 */
49
50 #include "config.h"
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <sys/wait.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #ifdef HAVE_SYS_XATTR_H
62 # include <sys/xattr.h>
63 #endif
64 #include "tst_test.h"
65
66 #ifdef HAVE_SYS_XATTR_H
67 #define XATTR_NAME_MAX 255
68 #define XATTR_NAME_LEN (XATTR_NAME_MAX + 2)
69 #define XATTR_SIZE_MAX 65536
70 #define XATTR_TEST_KEY "user.testkey"
71 #define XATTR_TEST_VALUE "this is a test value"
72 #define XATTR_TEST_VALUE_SIZE 20
73 #define MNTPOINT "mntpoint"
74 #define FNAME MNTPOINT"/setxattr01testfile"
75
76 static char long_key[XATTR_NAME_LEN];
77 static char *long_value;
78 static char *xattr_value = XATTR_TEST_VALUE;
79
80 struct test_case {
81 char *key;
82 char **value;
83 size_t size;
84 int flags;
85 int exp_err;
86 int keyneeded;
87 };
88 struct test_case tc[] = {
89 { /* case 00, invalid flags */
90 .key = XATTR_TEST_KEY,
91 .value = &xattr_value,
92 .size = XATTR_TEST_VALUE_SIZE,
93 .flags = ~0,
94 .exp_err = EINVAL,
95 },
96 { /* case 01, replace non-existing attribute */
97 .key = XATTR_TEST_KEY,
98 .value = &xattr_value,
99 .size = XATTR_TEST_VALUE_SIZE,
100 .flags = XATTR_REPLACE,
101 .exp_err = ENODATA,
102 },
103 { /* case 02, long key name */
104 .key = long_key,
105 .value = &xattr_value,
106 .size = XATTR_TEST_VALUE_SIZE,
107 .flags = XATTR_CREATE,
108 .exp_err = ERANGE,
109 },
110 { /* case 03, long value */
111 .key = XATTR_TEST_KEY,
112 .value = &long_value,
113 .size = XATTR_SIZE_MAX + 1,
114 .flags = XATTR_CREATE,
115 .exp_err = E2BIG,
116 },
117 { /* case 04, zero length value */
118 .key = XATTR_TEST_KEY,
119 .value = &xattr_value,
120 .size = 0,
121 .flags = XATTR_CREATE,
122 .exp_err = 0,
123 },
124 { /* case 05, create existing attribute */
125 .key = XATTR_TEST_KEY,
126 .value = &xattr_value,
127 .size = XATTR_TEST_VALUE_SIZE,
128 .flags = XATTR_CREATE,
129 .exp_err = EEXIST,
130 .keyneeded = 1,
131 },
132 { /* case 06, replace existing attribute */
133 .key = XATTR_TEST_KEY,
134 .value = &xattr_value,
135 .size = XATTR_TEST_VALUE_SIZE,
136 .flags = XATTR_REPLACE,
137 .exp_err = 0,
138 .keyneeded = 1,
139 },
140 { /* case 07, zero length key */
141 .key = "",
142 .value = &xattr_value,
143 .size = XATTR_TEST_VALUE_SIZE,
144 .flags = XATTR_CREATE,
145 .exp_err = ERANGE,
146 },
147 { /* case 08, NULL key */
148 .value = &xattr_value,
149 .size = XATTR_TEST_VALUE_SIZE,
150 .flags = XATTR_CREATE,
151 .exp_err = EFAULT,
152 },
153 };
154
verify_setxattr(unsigned int i)155 static void verify_setxattr(unsigned int i)
156 {
157 /* some tests might require existing keys for each iteration */
158 if (tc[i].keyneeded) {
159 SAFE_SETXATTR(FNAME, tc[i].key, tc[i].value, tc[i].size,
160 XATTR_CREATE);
161 }
162
163 TEST(setxattr(FNAME, tc[i].key, *tc[i].value, tc[i].size, tc[i].flags));
164
165 if (TST_RET == -1 && TST_ERR == EOPNOTSUPP)
166 tst_brk(TCONF, "setxattr(2) not supported");
167
168 /* success */
169
170 if (!tc[i].exp_err) {
171 if (TST_RET) {
172 tst_res(TFAIL | TTERRNO,
173 "setxattr(2) failed with %li", TST_RET);
174 return;
175 }
176
177 /* this is needed for subsequent iterations */
178 SAFE_REMOVEXATTR(FNAME, tc[i].key);
179
180 tst_res(TPASS, "setxattr(2) passed");
181
182 return;
183 }
184
185 if (TST_RET == 0) {
186 tst_res(TFAIL, "setxattr(2) passed unexpectedly");
187 return;
188 }
189
190 /* error */
191
192 if (tc[i].exp_err != TST_ERR) {
193 tst_res(TFAIL | TTERRNO, "setxattr(2) should fail with %s",
194 tst_strerrno(tc[i].exp_err));
195 return;
196 }
197
198 /* key might have been added AND test might have failed, remove it */
199 if (tc[i].keyneeded)
200 SAFE_REMOVEXATTR(FNAME, tc[i].key);
201
202 tst_res(TPASS | TTERRNO, "setxattr(2) failed");
203 }
204
setup(void)205 static void setup(void)
206 {
207 size_t i = 0;
208
209 snprintf(long_key, 6, "%s", "user.");
210 memset(long_key + 5, 'k', XATTR_NAME_LEN - 5);
211 long_key[XATTR_NAME_LEN - 1] = '\0';
212
213 long_value = SAFE_MALLOC(XATTR_SIZE_MAX + 2);
214 memset(long_value, 'v', XATTR_SIZE_MAX + 2);
215 long_value[XATTR_SIZE_MAX + 1] = '\0';
216
217 SAFE_TOUCH(FNAME, 0644, NULL);
218
219 for (i = 0; i < ARRAY_SIZE(tc); i++) {
220 if (!tc[i].key)
221 tc[i].key = tst_get_bad_addr(NULL);
222 }
223 }
224
225 static struct tst_test test = {
226 .setup = setup,
227 .test = verify_setxattr,
228 .tcnt = ARRAY_SIZE(tc),
229 .mntpoint = MNTPOINT,
230 .mount_device = 1,
231 .all_filesystems = 1,
232 .needs_tmpdir = 1,
233 .needs_root = 1,
234 };
235
236 #else /* HAVE_SYS_XATTR_H */
237 TST_TEST_TCONF("<sys/xattr.h> does not exist");
238 #endif
239