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