2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999-2001 Robert N. M. Watson
7 * This software was developed by Robert Watson for the TrustedBSD Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capsicum.h>
38 #include <sys/mount.h>
39 #include <sys/mutex.h>
40 #include <sys/sysproto.h>
41 #include <sys/fcntl.h>
42 #include <sys/namei.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
45 #include <sys/vnode.h>
47 #include <sys/extattr.h>
49 #include <security/audit/audit.h>
50 #include <security/mac/mac_framework.h>
52 static int kern_extattr_set_path(struct thread *td, const char *path,
53 int attrnamespace, const char *attrname, void *data,
54 size_t nbytes, int follow);
55 static int kern_extattr_get_path(struct thread *td, const char *path,
56 int attrnamespace, const char *attrname, void *data,
57 size_t nbytes, int follow);
58 static int kern_extattr_delete_path(struct thread *td, const char *path,
59 int attrnamespace, const char *attrname, int follow);
60 static int kern_extattr_list_path(struct thread *td, const char *path,
61 int attrnamespace, void *data, size_t nbytes, int follow);
64 * Syscall to push extended attribute configuration information into the VFS.
65 * Accepts a path, which it converts to a mountpoint, as well as a command
66 * (int cmd), and attribute name and misc data.
68 * Currently this is used only by UFS1 extended attributes.
70 #ifndef _SYS_SYSPROTO_H_
71 struct extattrctl_args {
80 sys_extattrctl(struct thread *td, struct extattrctl_args *uap)
82 struct vnode *filename_vp;
84 struct mount *mp, *mp_writable;
85 char attrname[EXTATTR_MAXNAMELEN + 1];
88 AUDIT_ARG_CMD(uap->cmd);
89 AUDIT_ARG_VALUE(uap->attrnamespace);
91 * uap->attrname is not always defined. We check again later when we
92 * invoke the VFS call so as to pass in NULL there if needed.
94 if (uap->attrname != NULL) {
95 error = copyinstr(uap->attrname, attrname, sizeof(attrname),
100 AUDIT_ARG_TEXT(attrname);
104 if (uap->filename != NULL) {
105 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE2, UIO_USERSPACE,
110 filename_vp = nd.ni_vp;
114 /* uap->path is always defined. */
115 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
120 mp = nd.ni_vp->v_mount;
121 error = vfs_busy(mp, 0);
128 VOP_UNLOCK(nd.ni_vp);
129 error = vn_start_write(nd.ni_vp, &mp_writable, V_WAIT | V_PCATCH);
134 if (filename_vp != NULL) {
136 * uap->filename is not always defined. If it is,
137 * grab a vnode lock, which VFS_EXTATTRCTL() will
140 error = vn_lock(filename_vp, LK_EXCLUSIVE);
142 vn_finished_write(mp_writable);
147 error = VFS_EXTATTRCTL(mp, uap->cmd, filename_vp, uap->attrnamespace,
148 uap->attrname != NULL ? attrname : NULL);
150 vn_finished_write(mp_writable);
156 * VFS_EXTATTRCTL will have unlocked, but not de-ref'd, filename_vp,
157 * so vrele it if it is defined.
159 if (filename_vp != NULL)
165 * Set a named extended attribute on a file or directory
167 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
168 * kernelspace string pointer "attrname", userspace buffer
169 * pointer "data", buffer length "nbytes", thread "td".
170 * Returns: 0 on success, an error number otherwise
172 * References: vp must be a valid reference for the duration of the call
175 extattr_set_vp(struct vnode *vp, int attrnamespace, const char *attrname,
176 void *data, size_t nbytes, struct thread *td)
184 if (nbytes > IOSIZE_MAX)
187 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
190 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
192 aiov.iov_base = data;
193 aiov.iov_len = nbytes;
194 auio.uio_iov = &aiov;
197 auio.uio_resid = nbytes;
198 auio.uio_rw = UIO_WRITE;
199 auio.uio_segflg = UIO_USERSPACE;
204 error = mac_vnode_check_setextattr(td->td_ucred, vp, attrnamespace,
210 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio,
212 cnt -= auio.uio_resid;
213 td->td_retval[0] = cnt;
219 vn_finished_write(mp);
223 #ifndef _SYS_SYSPROTO_H_
224 struct extattr_set_fd_args {
227 const char *attrname;
233 sys_extattr_set_fd(struct thread *td, struct extattr_set_fd_args *uap)
236 char attrname[EXTATTR_MAXNAMELEN + 1];
240 AUDIT_ARG_FD(uap->fd);
241 AUDIT_ARG_VALUE(uap->attrnamespace);
242 error = copyinstr(uap->attrname, attrname, sizeof(attrname), NULL);
245 AUDIT_ARG_TEXT(attrname);
247 error = getvnode_path(td, uap->fd,
248 cap_rights_init_one(&rights, CAP_EXTATTR_SET), &fp);
252 error = extattr_set_vp(fp->f_vnode, uap->attrnamespace,
253 attrname, uap->data, uap->nbytes, td);
259 #ifndef _SYS_SYSPROTO_H_
260 struct extattr_set_file_args {
263 const char *attrname;
269 sys_extattr_set_file(struct thread *td, struct extattr_set_file_args *uap)
272 return (kern_extattr_set_path(td, uap->path, uap->attrnamespace,
273 uap->attrname, uap->data, uap->nbytes, FOLLOW));
276 #ifndef _SYS_SYSPROTO_H_
277 struct extattr_set_link_args {
280 const char *attrname;
286 sys_extattr_set_link(struct thread *td, struct extattr_set_link_args *uap)
289 return (kern_extattr_set_path(td, uap->path, uap->attrnamespace,
290 uap->attrname, uap->data, uap->nbytes, NOFOLLOW));
294 kern_extattr_set_path(struct thread *td, const char *path, int attrnamespace,
295 const char *uattrname, void *data, size_t nbytes, int follow)
298 char attrname[EXTATTR_MAXNAMELEN + 1];
301 AUDIT_ARG_VALUE(attrnamespace);
302 error = copyinstr(uattrname, attrname, sizeof(attrname), NULL);
305 AUDIT_ARG_TEXT(attrname);
307 NDINIT(&nd, LOOKUP, follow | AUDITVNODE1, UIO_USERSPACE, path);
313 error = extattr_set_vp(nd.ni_vp, attrnamespace, attrname, data,
321 * Get a named extended attribute on a file or directory
323 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
324 * kernelspace string pointer "attrname", userspace buffer
325 * pointer "data", buffer length "nbytes", thread "td".
326 * Returns: 0 on success, an error number otherwise
328 * References: vp must be a valid reference for the duration of the call
331 extattr_get_vp(struct vnode *vp, int attrnamespace, const char *attrname,
332 void *data, size_t nbytes, struct thread *td)
334 struct uio auio, *auiop;
340 if (nbytes > IOSIZE_MAX)
343 vn_lock(vp, LK_SHARED | LK_RETRY);
346 * Slightly unusual semantics: if the user provides a NULL data
347 * pointer, they don't want to receive the data, just the maximum
354 aiov.iov_base = data;
355 aiov.iov_len = nbytes;
356 auio.uio_iov = &aiov;
359 auio.uio_resid = nbytes;
360 auio.uio_rw = UIO_READ;
361 auio.uio_segflg = UIO_USERSPACE;
369 error = mac_vnode_check_getextattr(td->td_ucred, vp, attrnamespace,
375 error = VOP_GETEXTATTR(vp, attrnamespace, attrname, auiop, sizep,
379 cnt -= auio.uio_resid;
380 td->td_retval[0] = cnt;
382 td->td_retval[0] = size;
390 #ifndef _SYS_SYSPROTO_H_
391 struct extattr_get_fd_args {
394 const char *attrname;
400 sys_extattr_get_fd(struct thread *td, struct extattr_get_fd_args *uap)
403 char attrname[EXTATTR_MAXNAMELEN + 1];
407 AUDIT_ARG_FD(uap->fd);
408 AUDIT_ARG_VALUE(uap->attrnamespace);
409 error = copyinstr(uap->attrname, attrname, sizeof(attrname), NULL);
412 AUDIT_ARG_TEXT(attrname);
414 error = getvnode_path(td, uap->fd,
415 cap_rights_init_one(&rights, CAP_EXTATTR_GET), &fp);
419 error = extattr_get_vp(fp->f_vnode, uap->attrnamespace,
420 attrname, uap->data, uap->nbytes, td);
426 #ifndef _SYS_SYSPROTO_H_
427 struct extattr_get_file_args {
430 const char *attrname;
436 sys_extattr_get_file(struct thread *td, struct extattr_get_file_args *uap)
438 return (kern_extattr_get_path(td, uap->path, uap->attrnamespace,
439 uap->attrname, uap->data, uap->nbytes, FOLLOW));
442 #ifndef _SYS_SYSPROTO_H_
443 struct extattr_get_link_args {
446 const char *attrname;
452 sys_extattr_get_link(struct thread *td, struct extattr_get_link_args *uap)
454 return (kern_extattr_get_path(td, uap->path, uap->attrnamespace,
455 uap->attrname, uap->data, uap->nbytes, NOFOLLOW));
459 kern_extattr_get_path(struct thread *td, const char *path, int attrnamespace,
460 const char *uattrname, void *data, size_t nbytes, int follow)
463 char attrname[EXTATTR_MAXNAMELEN + 1];
466 AUDIT_ARG_VALUE(attrnamespace);
467 error = copyinstr(uattrname, attrname, sizeof(attrname), NULL);
470 AUDIT_ARG_TEXT(attrname);
472 NDINIT(&nd, LOOKUP, follow | AUDITVNODE1, UIO_USERSPACE, path);
478 error = extattr_get_vp(nd.ni_vp, attrnamespace, attrname, data,
486 * extattr_delete_vp(): Delete a named extended attribute on a file or
489 * Arguments: unlocked vnode "vp", attribute namespace "attrnamespace",
490 * kernelspace string pointer "attrname", proc "p"
491 * Returns: 0 on success, an error number otherwise
493 * References: vp must be a valid reference for the duration of the call
496 extattr_delete_vp(struct vnode *vp, int attrnamespace, const char *attrname,
502 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
505 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
508 error = mac_vnode_check_deleteextattr(td->td_ucred, vp, attrnamespace,
514 error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, td->td_ucred,
516 if (error == EOPNOTSUPP)
517 error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
523 vn_finished_write(mp);
527 #ifndef _SYS_SYSPROTO_H_
528 struct extattr_delete_fd_args {
531 const char *attrname;
535 sys_extattr_delete_fd(struct thread *td, struct extattr_delete_fd_args *uap)
538 char attrname[EXTATTR_MAXNAMELEN + 1];
542 AUDIT_ARG_FD(uap->fd);
543 AUDIT_ARG_VALUE(uap->attrnamespace);
544 error = copyinstr(uap->attrname, attrname, sizeof(attrname), NULL);
547 AUDIT_ARG_TEXT(attrname);
549 error = getvnode_path(td, uap->fd,
550 cap_rights_init_one(&rights, CAP_EXTATTR_DELETE), &fp);
554 error = extattr_delete_vp(fp->f_vnode, uap->attrnamespace,
560 #ifndef _SYS_SYSPROTO_H_
561 struct extattr_delete_file_args {
564 const char *attrname;
568 sys_extattr_delete_file(struct thread *td, struct extattr_delete_file_args *uap)
571 return (kern_extattr_delete_path(td, uap->path, uap->attrnamespace,
572 uap->attrname, FOLLOW));
575 #ifndef _SYS_SYSPROTO_H_
576 struct extattr_delete_link_args {
579 const char *attrname;
583 sys_extattr_delete_link(struct thread *td, struct extattr_delete_link_args *uap)
586 return (kern_extattr_delete_path(td, uap->path, uap->attrnamespace,
587 uap->attrname, NOFOLLOW));
591 kern_extattr_delete_path(struct thread *td, const char *path, int attrnamespace,
592 const char *uattrname, int follow)
595 char attrname[EXTATTR_MAXNAMELEN + 1];
598 AUDIT_ARG_VALUE(attrnamespace);
599 error = copyinstr(uattrname, attrname, sizeof(attrname), NULL);
602 AUDIT_ARG_TEXT(attrname);
604 NDINIT(&nd, LOOKUP, follow | AUDITVNODE1, UIO_USERSPACE, path);
610 error = extattr_delete_vp(nd.ni_vp, attrnamespace, attrname, td);
616 * Retrieve a list of extended attributes on a file or directory.
618 * Arguments: unlocked vnode "vp", attribute namespace 'attrnamespace",
619 * userspace buffer pointer "data", buffer length "nbytes",
621 * Returns: 0 on success, an error number otherwise
623 * References: vp must be a valid reference for the duration of the call
626 extattr_list_vp(struct vnode *vp, int attrnamespace, void *data,
627 size_t nbytes, struct thread *td)
629 struct uio auio, *auiop;
635 if (nbytes > IOSIZE_MAX)
642 aiov.iov_base = data;
643 aiov.iov_len = nbytes;
644 auio.uio_iov = &aiov;
647 auio.uio_resid = nbytes;
648 auio.uio_rw = UIO_READ;
649 auio.uio_segflg = UIO_USERSPACE;
656 vn_lock(vp, LK_SHARED | LK_RETRY);
659 error = mac_vnode_check_listextattr(td->td_ucred, vp, attrnamespace);
666 error = VOP_LISTEXTATTR(vp, attrnamespace, auiop, sizep,
671 cnt -= auio.uio_resid;
672 td->td_retval[0] = cnt;
674 td->td_retval[0] = size;
678 #ifndef _SYS_SYSPROTO_H_
679 struct extattr_list_fd_args {
687 sys_extattr_list_fd(struct thread *td, struct extattr_list_fd_args *uap)
693 AUDIT_ARG_FD(uap->fd);
694 AUDIT_ARG_VALUE(uap->attrnamespace);
695 error = getvnode_path(td, uap->fd,
696 cap_rights_init_one(&rights, CAP_EXTATTR_LIST), &fp);
700 error = extattr_list_vp(fp->f_vnode, uap->attrnamespace, uap->data,
707 #ifndef _SYS_SYSPROTO_H_
708 struct extattr_list_file_args {
716 sys_extattr_list_file(struct thread *td, struct extattr_list_file_args *uap)
719 return (kern_extattr_list_path(td, uap->path, uap->attrnamespace,
720 uap->data, uap->nbytes, FOLLOW));
723 #ifndef _SYS_SYSPROTO_H_
724 struct extattr_list_link_args {
732 sys_extattr_list_link(struct thread *td, struct extattr_list_link_args *uap)
735 return (kern_extattr_list_path(td, uap->path, uap->attrnamespace,
736 uap->data, uap->nbytes, NOFOLLOW));
740 kern_extattr_list_path(struct thread *td, const char *path, int attrnamespace,
741 void *data, size_t nbytes, int follow)
746 AUDIT_ARG_VALUE(attrnamespace);
747 NDINIT(&nd, LOOKUP, follow | AUDITVNODE1, UIO_USERSPACE, path);
753 error = extattr_list_vp(nd.ni_vp, attrnamespace, data, nbytes, td);