xref: /freebsd/sys/compat/linux/linux_xattr.c (revision 09e32b2fddf5f673f76e2fffa415a73d99a6f309)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Dmitry Chagin <dchagin@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/extattr.h>
30 #include <sys/fcntl.h>
31 #include <sys/namei.h>
32 #include <sys/proc.h>
33 #include <sys/syscallsubr.h>
34 
35 #ifdef COMPAT_LINUX32
36 #include <machine/../linux32/linux.h>
37 #include <machine/../linux32/linux32_proto.h>
38 #else
39 #include <machine/../linux/linux.h>
40 #include <machine/../linux/linux_proto.h>
41 #endif
42 
43 #include <compat/linux/linux_util.h>
44 
45 #define	LINUX_XATTR_SIZE_MAX	65536
46 #define	LINUX_XATTR_LIST_MAX	65536
47 #define	LINUX_XATTR_NAME_MAX	255
48 
49 #define	LINUX_XATTR_CREATE	0x1
50 #define	LINUX_XATTR_REPLACE	0x2
51 #define	LINUX_XATTR_FLAGS	LINUX_XATTR_CREATE|LINUX_XATTR_REPLACE
52 
53 struct listxattr_args {
54 	int		fd;
55 	const char	*path;
56 	char		*list;
57 	l_size_t	size;
58 	int		follow;
59 };
60 
61 struct setxattr_args {
62 	int		fd;
63 	const char	*path;
64 	const char	*name;
65 	void 		*value;
66 	l_size_t	size;
67 	l_int		flags;
68 	int		follow;
69 };
70 
71 struct getxattr_args {
72 	int		fd;
73 	const char	*path;
74 	const char	*name;
75 	void 		*value;
76 	l_size_t	size;
77 	int		follow;
78 };
79 
80 struct removexattr_args {
81 	int		fd;
82 	const char	*path;
83 	const char	*name;
84 	int		follow;
85 };
86 
87 static char *extattr_namespace_names[] = EXTATTR_NAMESPACE_NAMES;
88 
89 
90 static int
91 error_to_xattrerror(int attrnamespace, int error)
92 {
93 
94 	if (attrnamespace == EXTATTR_NAMESPACE_SYSTEM && error == EPERM)
95 		return (ENOTSUP);
96 	else
97 		return (error);
98 }
99 
100 static int
101 xatrr_to_extattr(const char *uattrname, int *attrnamespace, char *attrname)
102 {
103 	char uname[LINUX_XATTR_NAME_MAX + 1], *dot;
104 	size_t len, cplen;
105 	int error;
106 
107 	error = copyinstr(uattrname, uname, sizeof(uname), &cplen);
108 	if (error != 0)
109 		return (error);
110 	if (cplen == sizeof(uname))
111 		return (ERANGE);
112 	dot = strchr(uname, '.');
113 	if (dot == NULL)
114 		return (ENOTSUP);
115 	*dot = '\0';
116 	for (*attrnamespace = EXTATTR_NAMESPACE_USER;
117 	    *attrnamespace < nitems(extattr_namespace_names);
118 	    (*attrnamespace)++) {
119 		if (bcmp(uname, extattr_namespace_names[*attrnamespace],
120 		    dot - uname + 1) == 0) {
121 			dot++;
122 			len = strlen(dot) + 1;
123 			bcopy(dot, attrname, len);
124 			return (0);
125 		}
126 	}
127 	return (ENOTSUP);
128 }
129 
130 static int
131 listxattr(struct thread *td, struct listxattr_args *args)
132 {
133 	char attrname[LINUX_XATTR_NAME_MAX + 1];
134 	char *data, *prefix, *key;
135 	struct uio auio;
136 	struct iovec aiov;
137 	unsigned char keylen;
138 	size_t sz, cnt, rs, prefixlen, pairlen;
139 	int attrnamespace, error;
140 
141 	if (args->size != 0)
142 		sz = min(LINUX_XATTR_LIST_MAX, args->size);
143 	else
144 		sz = LINUX_XATTR_LIST_MAX;
145 
146 	data = malloc(sz, M_LINUX, M_WAITOK);
147 	auio.uio_iov = &aiov;
148 	auio.uio_iovcnt = 1;
149 	auio.uio_rw = UIO_READ;
150 	auio.uio_segflg = UIO_SYSSPACE;
151 	auio.uio_td = td;
152 	cnt = 0;
153 	for (attrnamespace = EXTATTR_NAMESPACE_USER;
154 	    attrnamespace < nitems(extattr_namespace_names);
155 	    attrnamespace++) {
156 		aiov.iov_base = data;
157 		aiov.iov_len = sz;
158 		auio.uio_resid = sz;
159 		auio.uio_offset = 0;
160 
161 		if (args->path != NULL)
162 			error = kern_extattr_list_path(td, args->path,
163 			    attrnamespace, &auio, args->follow, UIO_USERSPACE);
164 		else
165 			error = kern_extattr_list_fd(td, args->fd,
166 			    attrnamespace, &auio);
167 		rs = sz - auio.uio_resid;
168 		if (error != 0 || rs == 0)
169 			continue;
170 		prefix = extattr_namespace_names[attrnamespace];
171 		prefixlen = strlen(prefix);
172 		key = data;
173 		while (rs > 0) {
174 			keylen = (unsigned char)key[0];
175 			pairlen = prefixlen + 1 + keylen + 1;
176 			if (cnt + pairlen > LINUX_XATTR_LIST_MAX) {
177 				error = E2BIG;
178 				break;
179 			}
180 			if ((args->list != NULL && cnt > args->size) ||
181 			    pairlen >= sizeof(attrname)) {
182 				error = ERANGE;
183 				break;
184 			}
185 			++key;
186 			if (args->list != NULL) {
187 				sprintf(attrname, "%s.%.*s", prefix, keylen, key);
188 				error = copyout(attrname, args->list, pairlen);
189 				if (error != 0)
190 					break;
191 				args->list += pairlen;
192 			}
193 			cnt += pairlen;
194 			key += keylen;
195 			rs -= (keylen + 1);
196 		}
197 	}
198 	if (error == 0)
199 		td->td_retval[0] = cnt;
200 	free(data, M_LINUX);
201 	return (error_to_xattrerror(attrnamespace, error));
202 }
203 
204 int
205 linux_listxattr(struct thread *td, struct linux_listxattr_args *args)
206 {
207 	struct listxattr_args eargs = {
208 		.fd = -1,
209 		.path = args->path,
210 		.list = args->list,
211 		.size = args->size,
212 		.follow = FOLLOW,
213 	};
214 
215 	return (listxattr(td, &eargs));
216 }
217 
218 int
219 linux_llistxattr(struct thread *td, struct linux_llistxattr_args *args)
220 {
221 	struct listxattr_args eargs = {
222 		.fd = -1,
223 		.path = args->path,
224 		.list = args->list,
225 		.size = args->size,
226 		.follow = NOFOLLOW,
227 	};
228 
229 	return (listxattr(td, &eargs));
230 }
231 
232 int
233 linux_flistxattr(struct thread *td, struct linux_flistxattr_args *args)
234 {
235 	struct listxattr_args eargs = {
236 		.fd = args->fd,
237 		.path = NULL,
238 		.list = args->list,
239 		.size = args->size,
240 		.follow = 0,
241 	};
242 
243 	return (listxattr(td, &eargs));
244 }
245 
246 static int
247 removexattr(struct thread *td, struct removexattr_args *args)
248 {
249 	char attrname[LINUX_XATTR_NAME_MAX + 1];
250 	int attrnamespace, error;
251 
252 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
253 	if (error != 0)
254 		return (error);
255 	if (args->path != NULL)
256 		error = kern_extattr_delete_path(td, args->path, attrnamespace,
257 		    attrname, args->follow, UIO_USERSPACE);
258 	else
259 		error = kern_extattr_delete_fd(td, args->fd, attrnamespace,
260 		    attrname);
261 	return (error_to_xattrerror(attrnamespace, error));
262 }
263 
264 int
265 linux_removexattr(struct thread *td, struct linux_removexattr_args *args)
266 {
267 	struct removexattr_args eargs = {
268 		.fd = -1,
269 		.path = args->path,
270 		.name = args->name,
271 		.follow = FOLLOW,
272 	};
273 
274 	return (removexattr(td, &eargs));
275 }
276 
277 int
278 linux_lremovexattr(struct thread *td, struct linux_lremovexattr_args *args)
279 {
280 	struct removexattr_args eargs = {
281 		.fd = -1,
282 		.path = args->path,
283 		.name = args->name,
284 		.follow = NOFOLLOW,
285 	};
286 
287 	return (removexattr(td, &eargs));
288 }
289 
290 int
291 linux_fremovexattr(struct thread *td, struct linux_fremovexattr_args *args)
292 {
293 	struct removexattr_args eargs = {
294 		.fd = args->fd,
295 		.path = NULL,
296 		.name = args->name,
297 		.follow = 0,
298 	};
299 
300 	return (removexattr(td, &eargs));
301 }
302 
303 static int
304 getxattr(struct thread *td, struct getxattr_args *args)
305 {
306 	char attrname[LINUX_XATTR_NAME_MAX + 1];
307 	int attrnamespace, error;
308 
309 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
310 	if (error != 0)
311 		return (error);
312 	if (args->path != NULL)
313 		error = kern_extattr_get_path(td, args->path, attrnamespace,
314 		    attrname, args->value, args->size, args->follow, UIO_USERSPACE);
315 	else
316 		error = kern_extattr_get_fd(td, args->fd, attrnamespace,
317 		    attrname, args->value, args->size);
318 	return (error == EPERM ? ENOATTR : error);
319 }
320 
321 int
322 linux_getxattr(struct thread *td, struct linux_getxattr_args *args)
323 {
324 	struct getxattr_args eargs = {
325 		.fd = -1,
326 		.path = args->path,
327 		.name = args->name,
328 		.value = args->value,
329 		.size = args->size,
330 		.follow = FOLLOW,
331 	};
332 
333 	return (getxattr(td, &eargs));
334 }
335 
336 int
337 linux_lgetxattr(struct thread *td, struct linux_lgetxattr_args *args)
338 {
339 	struct getxattr_args eargs = {
340 		.fd = -1,
341 		.path = args->path,
342 		.name = args->name,
343 		.value = args->value,
344 		.size = args->size,
345 		.follow = NOFOLLOW,
346 	};
347 
348 	return (getxattr(td, &eargs));
349 }
350 
351 int
352 linux_fgetxattr(struct thread *td, struct linux_fgetxattr_args *args)
353 {
354 	struct getxattr_args eargs = {
355 		.fd = args->fd,
356 		.path = NULL,
357 		.name = args->name,
358 		.value = args->value,
359 		.size = args->size,
360 		.follow = 0,
361 	};
362 
363 	return (getxattr(td, &eargs));
364 }
365 
366 static int
367 setxattr(struct thread *td, struct setxattr_args *args)
368 {
369 	char attrname[LINUX_XATTR_NAME_MAX + 1];
370 	int attrnamespace, error;
371 
372 	if ((args->flags & ~(LINUX_XATTR_FLAGS)) != 0 ||
373 	    args->flags == (LINUX_XATTR_FLAGS))
374 		return (EINVAL);
375 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
376 	if (error != 0)
377 		return (error);
378 
379 	if ((args->flags & (LINUX_XATTR_FLAGS)) != 0 ) {
380 		if (args->path != NULL)
381 			error = kern_extattr_get_path(td, args->path,
382 			    attrnamespace, attrname, NULL, args->size,
383 			    args->follow, UIO_USERSPACE);
384 		else
385 			error = kern_extattr_get_fd(td, args->fd,
386 			    attrnamespace, attrname, NULL, args->size);
387 		if ((args->flags & LINUX_XATTR_CREATE) != 0) {
388 			if (error == 0)
389 				error = EEXIST;
390 			else if (error == ENOATTR)
391 				error = 0;
392 		}
393 		if (error != 0)
394 			goto out;
395 	}
396 	if (args->path != NULL)
397 		error = kern_extattr_set_path(td, args->path, attrnamespace,
398 		    attrname, args->value, args->size, args->follow,
399 		    UIO_USERSPACE);
400 	else
401 		error = kern_extattr_set_fd(td, args->fd, attrnamespace,
402 		    attrname, args->value, args->size);
403 out:
404 	td->td_retval[0] = 0;
405 	return (error_to_xattrerror(attrnamespace, error));
406 }
407 
408 int
409 linux_setxattr(struct thread *td, struct linux_setxattr_args *args)
410 {
411 	struct setxattr_args eargs = {
412 		.fd = -1,
413 		.path = args->path,
414 		.name = args->name,
415 		.value = args->value,
416 		.size = args->size,
417 		.flags = args->flags,
418 		.follow = FOLLOW,
419 	};
420 
421 	return (setxattr(td, &eargs));
422 }
423 
424 int
425 linux_lsetxattr(struct thread *td, struct linux_lsetxattr_args *args)
426 {
427 	struct setxattr_args eargs = {
428 		.fd = -1,
429 		.path = args->path,
430 		.name = args->name,
431 		.value = args->value,
432 		.size = args->size,
433 		.flags = args->flags,
434 		.follow = NOFOLLOW,
435 	};
436 
437 	return (setxattr(td, &eargs));
438 }
439 
440 int
441 linux_fsetxattr(struct thread *td, struct linux_fsetxattr_args *args)
442 {
443 	struct setxattr_args eargs = {
444 		.fd = args->fd,
445 		.path = NULL,
446 		.name = args->name,
447 		.value = args->value,
448 		.size = args->size,
449 		.flags = args->flags,
450 		.follow = 0,
451 	};
452 
453 	return (setxattr(td, &eargs));
454 }
455