xref: /freebsd-14.2/sys/compat/linux/linux_xattr.c (revision 803280ea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2023 Dmitry Chagin <[email protected]>
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 static char *extattr_namespace_names[] = EXTATTR_NAMESPACE_NAMES;
81 
82 
83 static int
84 xatrr_to_extattr(const char *uattrname, int *attrnamespace, char *attrname)
85 {
86 	char uname[LINUX_XATTR_NAME_MAX + 1], *dot;
87 	size_t len, cplen;
88 	int error;
89 
90 	error = copyinstr(uattrname, uname, sizeof(uname), &cplen);
91 	if (error != 0)
92 		return (error);
93 	if (cplen == sizeof(uname))
94 		return (ERANGE);
95 	dot = strchr(uname, '.');
96 	if (dot == NULL)
97 		return (ENOTSUP);
98 	*dot = '\0';
99 	for (*attrnamespace = EXTATTR_NAMESPACE_USER;
100 	    *attrnamespace < nitems(extattr_namespace_names);
101 	    (*attrnamespace)++) {
102 		if (bcmp(uname, extattr_namespace_names[*attrnamespace],
103 		    dot - uname + 1) == 0) {
104 			dot++;
105 			len = strlen(dot) + 1;
106 			bcopy(dot, attrname, len);
107 			return (0);
108 		}
109 	}
110 	return (ENOTSUP);
111 }
112 
113 static int
114 listxattr(struct thread *td, struct listxattr_args *args)
115 {
116 	char attrname[LINUX_XATTR_NAME_MAX + 1];
117 	char *data, *prefix, *key;
118 	struct uio auio;
119 	struct iovec aiov;
120 	unsigned char keylen;
121 	size_t sz, cnt, rs, prefixlen, pairlen;
122 	int attrnamespace, error;
123 
124 	if (args->size != 0)
125 		sz = min(LINUX_XATTR_LIST_MAX, args->size);
126 	else
127 		sz = LINUX_XATTR_LIST_MAX;
128 
129 	data = malloc(sz, M_LINUX, M_WAITOK);
130 	auio.uio_iov = &aiov;
131 	auio.uio_iovcnt = 1;
132 	auio.uio_rw = UIO_READ;
133 	auio.uio_segflg = UIO_SYSSPACE;
134 	auio.uio_td = td;
135 	cnt = 0;
136 	for (attrnamespace = EXTATTR_NAMESPACE_USER;
137 	    attrnamespace < nitems(extattr_namespace_names);
138 	    attrnamespace++) {
139 		aiov.iov_base = data;
140 		aiov.iov_len = sz;
141 		auio.uio_resid = sz;
142 		auio.uio_offset = 0;
143 
144 		if (args->path != NULL)
145 			error = kern_extattr_list_path(td, args->path,
146 			    attrnamespace, &auio, args->follow, UIO_USERSPACE);
147 		else
148 			error = kern_extattr_list_fd(td, args->fd,
149 			    attrnamespace, &auio);
150 		rs = sz - auio.uio_resid;
151 		if (error != 0 || rs == 0)
152 			continue;
153 		prefix = extattr_namespace_names[attrnamespace];
154 		prefixlen = strlen(prefix);
155 		key = data;
156 		while (rs > 0) {
157 			keylen = (unsigned char)key[0];
158 			pairlen = prefixlen + 1 + keylen + 1;
159 			if (cnt + pairlen > LINUX_XATTR_LIST_MAX) {
160 				error = E2BIG;
161 				break;
162 			}
163 			if ((args->list != NULL && cnt > args->size) ||
164 			    pairlen >= sizeof(attrname)) {
165 				error = ERANGE;
166 				break;
167 			}
168 			++key;
169 			if (args->list != NULL) {
170 				sprintf(attrname, "%s.%.*s", prefix, keylen, key);
171 				error = copyout(attrname, args->list, pairlen);
172 				if (error != 0)
173 					break;
174 				args->list += pairlen;
175 			}
176 			cnt += pairlen;
177 			key += keylen;
178 			rs -= (keylen + 1);
179 		}
180 	}
181 	if (error == 0)
182 		td->td_retval[0] = cnt;
183 	free(data, M_LINUX);
184 	return (error);
185 }
186 
187 int
188 linux_listxattr(struct thread *td, struct linux_listxattr_args *args)
189 {
190 	struct listxattr_args eargs = {
191 		.fd = -1,
192 		.path = args->path,
193 		.list = args->list,
194 		.size = args->size,
195 		.follow = FOLLOW,
196 	};
197 
198 	return (listxattr(td, &eargs));
199 }
200 
201 int
202 linux_llistxattr(struct thread *td, struct linux_llistxattr_args *args)
203 {
204 	struct listxattr_args eargs = {
205 		.fd = -1,
206 		.path = args->path,
207 		.list = args->list,
208 		.size = args->size,
209 		.follow = NOFOLLOW,
210 	};
211 
212 	return (listxattr(td, &eargs));
213 }
214 
215 int
216 linux_flistxattr(struct thread *td, struct linux_flistxattr_args *args)
217 {
218 	struct listxattr_args eargs = {
219 		.fd = args->fd,
220 		.path = NULL,
221 		.list = args->list,
222 		.size = args->size,
223 		.follow = 0,
224 	};
225 
226 	return (listxattr(td, &eargs));
227 }
228 
229 static int
230 linux_path_removexattr(struct thread *td, const char *upath, const char *uname,
231     int follow)
232 {
233 	char attrname[LINUX_XATTR_NAME_MAX + 1];
234 	int attrnamespace, error;
235 
236 	error = xatrr_to_extattr(uname, &attrnamespace, attrname);
237 	if (error != 0)
238 		return (error);
239 
240 	return (kern_extattr_delete_path(td, upath, attrnamespace,
241 	    attrname, follow, UIO_USERSPACE));
242 }
243 
244 int
245 linux_removexattr(struct thread *td, struct linux_removexattr_args *args)
246 {
247 
248 	return (linux_path_removexattr(td, args->path, args->name,
249 	    FOLLOW));
250 }
251 
252 int
253 linux_lremovexattr(struct thread *td, struct linux_lremovexattr_args *args)
254 {
255 
256 	return (linux_path_removexattr(td, args->path, args->name,
257 	    NOFOLLOW));
258 }
259 
260 int
261 linux_fremovexattr(struct thread *td, struct linux_fremovexattr_args *args)
262 {
263 	char attrname[LINUX_XATTR_NAME_MAX + 1];
264 	int attrnamespace, error;
265 
266 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
267 	if (error != 0)
268 		return (error);
269 	return (kern_extattr_delete_fd(td, args->fd, attrnamespace,
270 	    attrname));
271 }
272 
273 static int
274 getxattr(struct thread *td, struct getxattr_args *args)
275 {
276 	char attrname[LINUX_XATTR_NAME_MAX + 1];
277 	int attrnamespace, error;
278 
279 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
280 	if (error != 0)
281 		return (error);
282 	if (args->path != NULL)
283 		error = kern_extattr_get_path(td, args->path, attrnamespace,
284 		    attrname, args->value, args->size, args->follow, UIO_USERSPACE);
285 	else
286 		error = kern_extattr_get_fd(td, args->fd, attrnamespace,
287 		    attrname, args->value, args->size);
288 	return (error);
289 }
290 
291 int
292 linux_getxattr(struct thread *td, struct linux_getxattr_args *args)
293 {
294 	struct getxattr_args eargs = {
295 		.fd = -1,
296 		.path = args->path,
297 		.name = args->name,
298 		.value = args->value,
299 		.size = args->size,
300 		.follow = FOLLOW,
301 	};
302 
303 	return (getxattr(td, &eargs));
304 }
305 
306 int
307 linux_lgetxattr(struct thread *td, struct linux_lgetxattr_args *args)
308 {
309 	struct getxattr_args eargs = {
310 		.fd = -1,
311 		.path = args->path,
312 		.name = args->name,
313 		.value = args->value,
314 		.size = args->size,
315 		.follow = NOFOLLOW,
316 	};
317 
318 	return (getxattr(td, &eargs));
319 }
320 
321 int
322 linux_fgetxattr(struct thread *td, struct linux_fgetxattr_args *args)
323 {
324 	struct getxattr_args eargs = {
325 		.fd = args->fd,
326 		.path = NULL,
327 		.name = args->name,
328 		.value = args->value,
329 		.size = args->size,
330 		.follow = 0,
331 	};
332 
333 	return (getxattr(td, &eargs));
334 }
335 
336 static int
337 setxattr(struct thread *td, struct setxattr_args *args)
338 {
339 	char attrname[LINUX_XATTR_NAME_MAX + 1];
340 	int attrnamespace, error;
341 
342 	if ((args->flags & ~(LINUX_XATTR_FLAGS)) != 0 ||
343 	    args->flags == (LINUX_XATTR_FLAGS))
344 		return (EINVAL);
345 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
346 	if (error != 0)
347 		return (error);
348 
349 	if ((args->flags & (LINUX_XATTR_FLAGS)) != 0 ) {
350 		if (args->path != NULL)
351 			error = kern_extattr_get_path(td, args->path,
352 			    attrnamespace, attrname, NULL, args->size,
353 			    args->follow, UIO_USERSPACE);
354 		else
355 			error = kern_extattr_get_fd(td, args->fd,
356 			    attrnamespace, attrname, NULL, args->size);
357 		if ((args->flags & LINUX_XATTR_CREATE) != 0) {
358 			if (error == 0)
359 				error = EEXIST;
360 			else if (error == ENOATTR)
361 				error = 0;
362 		}
363 		if (error != 0)
364 			goto out;
365 	}
366 	if (args->path != NULL)
367 		error = kern_extattr_set_path(td, args->path, attrnamespace,
368 		    attrname, args->value, args->size, args->follow,
369 		    UIO_USERSPACE);
370 	else
371 		error = kern_extattr_set_fd(td, args->fd, attrnamespace,
372 		    attrname, args->value, args->size);
373 out:
374 	td->td_retval[0] = 0;
375 	return (error);
376 }
377 
378 int
379 linux_setxattr(struct thread *td, struct linux_setxattr_args *args)
380 {
381 	struct setxattr_args eargs = {
382 		.fd = -1,
383 		.path = args->path,
384 		.name = args->name,
385 		.value = args->value,
386 		.size = args->size,
387 		.flags = args->flags,
388 		.follow = FOLLOW,
389 	};
390 
391 	return (setxattr(td, &eargs));
392 }
393 
394 int
395 linux_lsetxattr(struct thread *td, struct linux_lsetxattr_args *args)
396 {
397 	struct setxattr_args eargs = {
398 		.fd = -1,
399 		.path = args->path,
400 		.name = args->name,
401 		.value = args->value,
402 		.size = args->size,
403 		.flags = args->flags,
404 		.follow = NOFOLLOW,
405 	};
406 
407 	return (setxattr(td, &eargs));
408 }
409 
410 int
411 linux_fsetxattr(struct thread *td, struct linux_fsetxattr_args *args)
412 {
413 	struct setxattr_args eargs = {
414 		.fd = args->fd,
415 		.path = NULL,
416 		.name = args->name,
417 		.value = args->value,
418 		.size = args->size,
419 		.flags = args->flags,
420 		.follow = 0,
421 	};
422 
423 	return (setxattr(td, &eargs));
424 }
425