xref: /freebsd/sys/compat/linux/linux_xattr.c (revision d0b2dbfa)
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/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/extattr.h>
31 #include <sys/fcntl.h>
32 #include <sys/namei.h>
33 #include <sys/proc.h>
34 #include <sys/syscallsubr.h>
35 
36 #ifdef COMPAT_LINUX32
37 #include <machine/../linux32/linux.h>
38 #include <machine/../linux32/linux32_proto.h>
39 #else
40 #include <machine/../linux/linux.h>
41 #include <machine/../linux/linux_proto.h>
42 #endif
43 
44 #include <compat/linux/linux_util.h>
45 
46 #define	LINUX_XATTR_SIZE_MAX	65536
47 #define	LINUX_XATTR_LIST_MAX	65536
48 #define	LINUX_XATTR_NAME_MAX	255
49 
50 #define	LINUX_XATTR_CREATE	0x1
51 #define	LINUX_XATTR_REPLACE	0x2
52 #define	LINUX_XATTR_FLAGS	LINUX_XATTR_CREATE|LINUX_XATTR_REPLACE
53 
54 struct listxattr_args {
55 	int		fd;
56 	const char	*path;
57 	char		*list;
58 	l_size_t	size;
59 	int		follow;
60 };
61 
62 struct setxattr_args {
63 	int		fd;
64 	const char	*path;
65 	const char	*name;
66 	void 		*value;
67 	l_size_t	size;
68 	l_int		flags;
69 	int		follow;
70 };
71 
72 static char *extattr_namespace_names[] = EXTATTR_NAMESPACE_NAMES;
73 
74 
75 static int
76 xatrr_to_extattr(const char *uattrname, int *attrnamespace, char *attrname)
77 {
78 	char uname[LINUX_XATTR_NAME_MAX + 1], *dot;
79 	size_t len, cplen;
80 	int error;
81 
82 	error = copyinstr(uattrname, uname, sizeof(uname), &cplen);
83 	if (error != 0)
84 		return (error);
85 	if (cplen == sizeof(uname))
86 		return (ERANGE);
87 	dot = strchr(uname, '.');
88 	if (dot == NULL)
89 		return (ENOTSUP);
90 	*dot = '\0';
91 	for (*attrnamespace = EXTATTR_NAMESPACE_USER;
92 	    *attrnamespace < nitems(extattr_namespace_names);
93 	    (*attrnamespace)++) {
94 		if (bcmp(uname, extattr_namespace_names[*attrnamespace],
95 		    dot - uname + 1) == 0) {
96 			dot++;
97 			len = strlen(dot) + 1;
98 			bcopy(dot, attrname, len);
99 			return (0);
100 		}
101 	}
102 	return (ENOTSUP);
103 }
104 
105 static int
106 listxattr(struct thread *td, struct listxattr_args *args)
107 {
108 	char attrname[LINUX_XATTR_NAME_MAX + 1];
109 	char *data, *prefix, *key;
110 	struct uio auio;
111 	struct iovec aiov;
112 	unsigned char keylen;
113 	size_t sz, cnt, rs, prefixlen, pairlen;
114 	int attrnamespace, error;
115 
116 	if (args->size != 0)
117 		sz = min(LINUX_XATTR_LIST_MAX, args->size);
118 	else
119 		sz = LINUX_XATTR_LIST_MAX;
120 
121 	data = malloc(sz, M_LINUX, M_WAITOK);
122 	auio.uio_iov = &aiov;
123 	auio.uio_iovcnt = 1;
124 	auio.uio_rw = UIO_READ;
125 	auio.uio_segflg = UIO_SYSSPACE;
126 	auio.uio_td = td;
127 	cnt = 0;
128 	for (attrnamespace = EXTATTR_NAMESPACE_USER;
129 	    attrnamespace < nitems(extattr_namespace_names);
130 	    attrnamespace++) {
131 		aiov.iov_base = data;
132 		aiov.iov_len = sz;
133 		auio.uio_resid = sz;
134 		auio.uio_offset = 0;
135 
136 		if (args->path != NULL)
137 			error = kern_extattr_list_path(td, args->path,
138 			    attrnamespace, &auio, args->follow, UIO_USERSPACE);
139 		else
140 			error = kern_extattr_list_fd(td, args->fd,
141 			    attrnamespace, &auio);
142 		rs = sz - auio.uio_resid;
143 		if (error != 0 || rs == 0)
144 			continue;
145 		prefix = extattr_namespace_names[attrnamespace];
146 		prefixlen = strlen(prefix);
147 		key = data;
148 		while (rs > 0) {
149 			keylen = (unsigned char)key[0];
150 			pairlen = prefixlen + 1 + keylen + 1;
151 			if (cnt + pairlen > LINUX_XATTR_LIST_MAX) {
152 				error = E2BIG;
153 				break;
154 			}
155 			if ((args->list != NULL && cnt > args->size) ||
156 			    pairlen >= sizeof(attrname)) {
157 				error = ERANGE;
158 				break;
159 			}
160 			++key;
161 			if (args->list != NULL) {
162 				sprintf(attrname, "%s.%.*s", prefix, keylen, key);
163 				error = copyout(attrname, args->list, pairlen);
164 				if (error != 0)
165 					break;
166 				args->list += pairlen;
167 			}
168 			cnt += pairlen;
169 			key += keylen;
170 			rs -= (keylen + 1);
171 		}
172 	}
173 	if (error == 0)
174 		td->td_retval[0] = cnt;
175 	free(data, M_LINUX);
176 	return (error);
177 }
178 
179 int
180 linux_listxattr(struct thread *td, struct linux_listxattr_args *args)
181 {
182 	struct listxattr_args eargs = {
183 		.fd = -1,
184 		.path = args->path,
185 		.list = args->list,
186 		.size = args->size,
187 		.follow = FOLLOW,
188 	};
189 
190 	return (listxattr(td, &eargs));
191 }
192 
193 int
194 linux_llistxattr(struct thread *td, struct linux_llistxattr_args *args)
195 {
196 	struct listxattr_args eargs = {
197 		.fd = -1,
198 		.path = args->path,
199 		.list = args->list,
200 		.size = args->size,
201 		.follow = NOFOLLOW,
202 	};
203 
204 	return (listxattr(td, &eargs));
205 }
206 
207 int
208 linux_flistxattr(struct thread *td, struct linux_flistxattr_args *args)
209 {
210 	struct listxattr_args eargs = {
211 		.fd = args->fd,
212 		.path = NULL,
213 		.list = args->list,
214 		.size = args->size,
215 		.follow = 0,
216 	};
217 
218 	return (listxattr(td, &eargs));
219 }
220 
221 static int
222 linux_path_removexattr(struct thread *td, const char *upath, const char *uname,
223     int follow)
224 {
225 	char attrname[LINUX_XATTR_NAME_MAX + 1];
226 	int attrnamespace, error;
227 
228 	error = xatrr_to_extattr(uname, &attrnamespace, attrname);
229 	if (error != 0)
230 		return (error);
231 
232 	return (kern_extattr_delete_path(td, upath, attrnamespace,
233 	    attrname, follow, UIO_USERSPACE));
234 }
235 
236 int
237 linux_removexattr(struct thread *td, struct linux_removexattr_args *args)
238 {
239 
240 	return (linux_path_removexattr(td, args->path, args->name,
241 	    FOLLOW));
242 }
243 
244 int
245 linux_lremovexattr(struct thread *td, struct linux_lremovexattr_args *args)
246 {
247 
248 	return (linux_path_removexattr(td, args->path, args->name,
249 	    NOFOLLOW));
250 }
251 
252 int
253 linux_fremovexattr(struct thread *td, struct linux_fremovexattr_args *args)
254 {
255 	char attrname[LINUX_XATTR_NAME_MAX + 1];
256 	int attrnamespace, error;
257 
258 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
259 	if (error != 0)
260 		return (error);
261 	return (kern_extattr_delete_fd(td, args->fd, attrnamespace,
262 	    attrname));
263 }
264 
265 static int
266 linux_path_getxattr(struct thread *td, const char *upath, const char *uname,
267     void *value, l_size_t size, int follow)
268 {
269 	char attrname[LINUX_XATTR_NAME_MAX + 1];
270 	int attrnamespace, error;
271 
272 	error = xatrr_to_extattr(uname, &attrnamespace, attrname);
273 	if (error != 0)
274 		return (error);
275 
276 	return (kern_extattr_get_path(td, upath, attrnamespace,
277 	    attrname, value, size, follow, UIO_USERSPACE));
278 }
279 
280 int
281 linux_getxattr(struct thread *td, struct linux_getxattr_args *args)
282 {
283 
284 	return (linux_path_getxattr(td, args->path, args->name,
285 	    args->value, args->size, FOLLOW));
286 }
287 
288 int
289 linux_lgetxattr(struct thread *td, struct linux_lgetxattr_args *args)
290 {
291 
292 	return (linux_path_getxattr(td, args->path, args->name,
293 	    args->value, args->size, NOFOLLOW));
294 }
295 
296 int
297 linux_fgetxattr(struct thread *td, struct linux_fgetxattr_args *args)
298 {
299 	char attrname[LINUX_XATTR_NAME_MAX + 1];
300 	int attrnamespace, error;
301 
302 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
303 	if (error != 0)
304 		return (error);
305 	return (kern_extattr_get_fd(td, args->fd, attrnamespace,
306 	    attrname, args->value, args->size));
307 }
308 
309 static int
310 setxattr(struct thread *td, struct setxattr_args *args)
311 {
312 	char attrname[LINUX_XATTR_NAME_MAX + 1];
313 	int attrnamespace, error;
314 
315 	if ((args->flags & ~(LINUX_XATTR_FLAGS)) != 0 ||
316 	    args->flags == (LINUX_XATTR_FLAGS))
317 		return (EINVAL);
318 	error = xatrr_to_extattr(args->name, &attrnamespace, attrname);
319 	if (error != 0)
320 		return (error);
321 
322 	if ((args->flags & (LINUX_XATTR_FLAGS)) != 0 ) {
323 		if (args->path != NULL)
324 			error = kern_extattr_get_path(td, args->path,
325 			    attrnamespace, attrname, NULL, args->size,
326 			    args->follow, UIO_USERSPACE);
327 		else
328 			error = kern_extattr_get_fd(td, args->fd,
329 			    attrnamespace, attrname, NULL, args->size);
330 		if ((args->flags & LINUX_XATTR_CREATE) != 0) {
331 			if (error == 0)
332 				error = EEXIST;
333 			else if (error == ENOATTR)
334 				error = 0;
335 		}
336 		if (error != 0)
337 			goto out;
338 	}
339 	if (args->path != NULL)
340 		error = kern_extattr_set_path(td, args->path, attrnamespace,
341 		    attrname, args->value, args->size, args->follow,
342 		    UIO_USERSPACE);
343 	else
344 		error = kern_extattr_set_fd(td, args->fd, attrnamespace,
345 		    attrname, args->value, args->size);
346 out:
347 	td->td_retval[0] = 0;
348 	return (error);
349 }
350 
351 int
352 linux_setxattr(struct thread *td, struct linux_setxattr_args *args)
353 {
354 	struct setxattr_args eargs = {
355 		.fd = -1,
356 		.path = args->path,
357 		.name = args->name,
358 		.value = args->value,
359 		.size = args->size,
360 		.flags = args->flags,
361 		.follow = FOLLOW,
362 	};
363 
364 	return (setxattr(td, &eargs));
365 }
366 
367 int
368 linux_lsetxattr(struct thread *td, struct linux_lsetxattr_args *args)
369 {
370 	struct setxattr_args eargs = {
371 		.fd = -1,
372 		.path = args->path,
373 		.name = args->name,
374 		.value = args->value,
375 		.size = args->size,
376 		.flags = args->flags,
377 		.follow = NOFOLLOW,
378 	};
379 
380 	return (setxattr(td, &eargs));
381 }
382 
383 int
384 linux_fsetxattr(struct thread *td, struct linux_fsetxattr_args *args)
385 {
386 	struct setxattr_args eargs = {
387 		.fd = args->fd,
388 		.path = NULL,
389 		.name = args->name,
390 		.value = args->value,
391 		.size = args->size,
392 		.flags = args->flags,
393 		.follow = 0,
394 	};
395 
396 	return (setxattr(td, &eargs));
397 }
398