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