From owner-svn-src-head@freebsd.org Fri Feb 21 01:40:51 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B848424D1FA; Fri, 21 Feb 2020 01:40:51 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48NvLB3B7Fz3KXw; Fri, 21 Feb 2020 01:40:50 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4755130E6; Fri, 21 Feb 2020 01:40:50 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 01L1eoGl060182; Fri, 21 Feb 2020 01:40:50 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 01L1enQ3060179; Fri, 21 Feb 2020 01:40:49 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202002210140.01L1enQ3060179@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Fri, 21 Feb 2020 01:40:49 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r358191 - head/sys/security/audit X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/security/audit X-SVN-Commit-Revision: 358191 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 21 Feb 2020 01:40:51 -0000 Author: mjg Date: Fri Feb 21 01:40:49 2020 New Revision: 358191 URL: https://svnweb.freebsd.org/changeset/base/358191 Log: audit: provide audit_canon_path variant which accepts vnodes Modified: head/sys/security/audit/audit.h head/sys/security/audit/audit_arg.c head/sys/security/audit/audit_bsm_klib.c head/sys/security/audit/audit_private.h Modified: head/sys/security/audit/audit.h ============================================================================== --- head/sys/security/audit/audit.h Fri Feb 21 01:40:20 2020 (r358190) +++ head/sys/security/audit/audit.h Fri Feb 21 01:40:49 2020 (r358191) @@ -120,6 +120,10 @@ void audit_arg_upath1(struct thread *td, int dirfd, c void audit_arg_upath1_canon(char *upath); void audit_arg_upath2(struct thread *td, int dirfd, char *upath); void audit_arg_upath2_canon(char *upath); +void audit_arg_upath1_vp(struct thread *td, struct vnode *rdir, + struct vnode *cdir, char *upath); +void audit_arg_upath2_vp(struct thread *td, struct vnode *rdir, + struct vnode *cdir, char *upath); void audit_arg_vnode1(struct vnode *vp); void audit_arg_vnode2(struct vnode *vp); void audit_arg_text(const char *text); @@ -362,6 +366,16 @@ void audit_thread_free(struct thread *td); audit_arg_upath2_canon((upath)); \ } while (0) +#define AUDIT_ARG_UPATH1_VP(td, rdir, cdir, upath) do { \ + if (AUDITING_TD(curthread)) \ + audit_arg_upath1_vp((td), (rdir), (cdir), (upath)); \ +} while (0) + +#define AUDIT_ARG_UPATH2_VP(td, rdir, cdir, upath) do { \ + if (AUDITING_TD(curthread)) \ + audit_arg_upath2_vp((td), (rdir), (cdir), (upath)); \ +} while (0) + #define AUDIT_ARG_VALUE(value) do { \ if (AUDITING_TD(curthread)) \ audit_arg_value((value)); \ @@ -448,6 +462,8 @@ void audit_thread_free(struct thread *td); #define AUDIT_ARG_UPATH1_CANON(upath) #define AUDIT_ARG_UPATH2(td, dirfd, upath) #define AUDIT_ARG_UPATH2_CANON(upath) +#define AUDIT_ARG_UPATH1_VP(td, rdir, cdir, upath) +#define AUDIT_ARG_UPATH2_VP(td, rdir, cdir, upath) #define AUDIT_ARG_VALUE(value) #define AUDIT_ARG_VNODE1(vp) #define AUDIT_ARG_VNODE2(vp) Modified: head/sys/security/audit/audit_arg.c ============================================================================== --- head/sys/security/audit/audit_arg.c Fri Feb 21 01:40:20 2020 (r358190) +++ head/sys/security/audit/audit_arg.c Fri Feb 21 01:40:49 2020 (r358191) @@ -767,6 +767,44 @@ audit_arg_upath2(struct thread *td, int dirfd, char *u ARG_SET_VALID(ar, ARG_UPATH2); } +static void +audit_arg_upath_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir, + char *upath, char **pathp) +{ + + if (*pathp == NULL) + *pathp = malloc(MAXPATHLEN, M_AUDITPATH, M_WAITOK); + audit_canon_path_vp(td, rdir, cdir, upath, *pathp); +} + +void +audit_arg_upath1_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir, + char *upath) +{ + struct kaudit_record *ar; + + ar = currecord(); + if (ar == NULL) + return; + + audit_arg_upath_vp(td, rdir, cdir, upath, &ar->k_ar.ar_arg_upath1); + ARG_SET_VALID(ar, ARG_UPATH1); +} + +void +audit_arg_upath2_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir, + char *upath) +{ + struct kaudit_record *ar; + + ar = currecord(); + if (ar == NULL) + return; + + audit_arg_upath_vp(td, rdir, cdir, upath, &ar->k_ar.ar_arg_upath2); + ARG_SET_VALID(ar, ARG_UPATH2); +} + /* * Variants on path auditing that do not canonicalise the path passed in; * these are for use with filesystem-like subsystems that employ string names, Modified: head/sys/security/audit/audit_bsm_klib.c ============================================================================== --- head/sys/security/audit/audit_bsm_klib.c Fri Feb 21 01:40:20 2020 (r358190) +++ head/sys/security/audit/audit_bsm_klib.c Fri Feb 21 01:40:49 2020 (r358191) @@ -421,38 +421,23 @@ auditon_command_event(int cmd) * leave the filename starting with '/' in the audit log in this case. */ void -audit_canon_path(struct thread *td, int dirfd, char *path, char *cpath) +audit_canon_path_vp(struct thread *td, struct vnode *rdir, struct vnode *cdir, + char *path, char *cpath) { struct vnode *vp; char *rbuf, *fbuf, *copy; - struct filedesc *fdp; struct sbuf sbf; - cap_rights_t rights; int error; WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s: at %s:%d", __func__, __FILE__, __LINE__); copy = path; - fdp = td->td_proc->p_fd; - FILEDESC_SLOCK(fdp); - if (*path == '/') { - vp = fdp->fd_rdir; - vrefact(vp); - } else { - if (dirfd == AT_FDCWD) { - vp = fdp->fd_cdir; - vrefact(vp); - } else { - error = fgetvp(td, dirfd, cap_rights_init(&rights), &vp); - if (error != 0) { - FILEDESC_SUNLOCK(fdp); - cpath[0] = '\0'; - return; - } - } - } - FILEDESC_SUNLOCK(fdp); + if (*path == '/') + vp = rdir; + else + vp = cdir; + MPASS(vp != NULL); /* * NB: We require that the supplied array be at least MAXPATHLEN bytes * long. If this is not the case, then we can run into serious trouble. @@ -474,7 +459,6 @@ audit_canon_path(struct thread *td, int dirfd, char *p * in the future. */ error = vn_fullpath_global(td, vp, &rbuf, &fbuf); - vrele(vp); if (error) { cpath[0] = '\0'; return; @@ -503,4 +487,44 @@ audit_canon_path(struct thread *td, int dirfd, char *p return; } sbuf_finish(&sbf); +} + +void +audit_canon_path(struct thread *td, int dirfd, char *path, char *cpath) +{ + struct vnode *cdir, *rdir; + struct filedesc *fdp; + cap_rights_t rights; + int error; + + WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "%s: at %s:%d", + __func__, __FILE__, __LINE__); + + rdir = cdir = NULL; + fdp = td->td_proc->p_fd; + FILEDESC_SLOCK(fdp); + if (*path == '/') { + rdir = fdp->fd_rdir; + vrefact(rdir); + } else { + if (dirfd == AT_FDCWD) { + cdir = fdp->fd_cdir; + vrefact(cdir); + } else { + error = fgetvp(td, dirfd, cap_rights_init(&rights), &cdir); + if (error != 0) { + FILEDESC_SUNLOCK(fdp); + cpath[0] = '\0'; + return; + } + } + } + FILEDESC_SUNLOCK(fdp); + + audit_canon_path_vp(td, rdir, cdir, path, cpath); + + if (rdir != NULL) + vrele(rdir); + if (cdir != NULL) + vrele(cdir); } Modified: head/sys/security/audit/audit_private.h ============================================================================== --- head/sys/security/audit/audit_private.h Fri Feb 21 01:40:20 2020 (r358190) +++ head/sys/security/audit/audit_private.h Fri Feb 21 01:40:49 2020 (r358191) @@ -472,6 +472,8 @@ au_event_t audit_semsys_to_event(int which); au_event_t audit_shmsys_to_event(int which); void audit_canon_path(struct thread *td, int dirfd, char *path, char *cpath); +void audit_canon_path_vp(struct thread *td, struct vnode *rdir, + struct vnode *cdir, char *path, char *cpath); au_event_t auditon_command_event(int cmd); /*