From owner-svn-src-head@freebsd.org Thu Dec 17 18:52:05 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 E9D4E4C041B; Thu, 17 Dec 2020 18:52:05 +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) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 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 4Cxh1d6LT6z4Zq7; Thu, 17 Dec 2020 18:52:05 +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 CBC551D769; Thu, 17 Dec 2020 18:52:05 +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 0BHIq5xt040544; Thu, 17 Dec 2020 18:52:05 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0BHIq5RU040541; Thu, 17 Dec 2020 18:52:05 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202012171852.0BHIq5RU040541@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Thu, 17 Dec 2020 18:52:05 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r368731 - in head/sys: kern security/audit X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: kern security/audit X-SVN-Commit-Revision: 368731 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.34 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: Thu, 17 Dec 2020 18:52:06 -0000 Author: mjg Date: Thu Dec 17 18:52:04 2020 New Revision: 368731 URL: https://svnweb.freebsd.org/changeset/base/368731 Log: audit: rework AUDIT_SYSCLOSE This in particular avoids spurious lookups on close. Modified: head/sys/kern/kern_descrip.c head/sys/security/audit/audit.h head/sys/security/audit/audit_arg.c Modified: head/sys/kern/kern_descrip.c ============================================================================== --- head/sys/kern/kern_descrip.c Thu Dec 17 18:51:09 2020 (r368730) +++ head/sys/kern/kern_descrip.c Thu Dec 17 18:52:04 2020 (r368731) @@ -107,7 +107,7 @@ __read_mostly uma_zone_t pwd_zone; VFS_SMR_DECLARE; static int closefp(struct filedesc *fdp, int fd, struct file *fp, - struct thread *td, bool holdleaders); + struct thread *td, bool holdleaders, bool audit); static int fd_first_free(struct filedesc *fdp, int low, int size); static void fdgrowtable(struct filedesc *fdp, int nfd); static void fdgrowtable_exp(struct filedesc *fdp, int nfd); @@ -998,7 +998,7 @@ kern_dup(struct thread *td, u_int mode, int flags, int error = 0; if (delfp != NULL) { - (void) closefp(fdp, new, delfp, td, true); + (void) closefp(fdp, new, delfp, td, true, false); FILEDESC_UNLOCK_ASSERT(fdp); } else { unlock: @@ -1240,7 +1240,8 @@ fgetown(struct sigio **sigiop) } static int -closefp_impl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td) +closefp_impl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, + bool audit) { int error; @@ -1262,6 +1263,10 @@ closefp_impl(struct filedesc *fdp, int fd, struct file mq_fdclose(td, fd, fp); FILEDESC_XUNLOCK(fdp); +#ifdef AUDIT + if (AUDITING_TD(td) && audit) + audit_sysclose(td, fd, fp); +#endif error = closef(fp, td); /* @@ -1277,7 +1282,7 @@ closefp_impl(struct filedesc *fdp, int fd, struct file static int closefp_hl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, - bool holdleaders) + bool holdleaders, bool audit) { int error; @@ -1295,7 +1300,7 @@ closefp_hl(struct filedesc *fdp, int fd, struct file * } } - error = closefp_impl(fdp, fd, fp, td); + error = closefp_impl(fdp, fd, fp, td, audit); if (holdleaders) { FILEDESC_XLOCK(fdp); fdp->fd_holdleaderscount--; @@ -1311,15 +1316,15 @@ closefp_hl(struct filedesc *fdp, int fd, struct file * static int closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, - bool holdleaders) + bool holdleaders, bool audit) { FILEDESC_XLOCK_ASSERT(fdp); if (__predict_false(td->td_proc->p_fdtol != NULL)) { - return (closefp_hl(fdp, fd, fp, td, holdleaders)); + return (closefp_hl(fdp, fd, fp, td, holdleaders, audit)); } else { - return (closefp_impl(fdp, fd, fp, td)); + return (closefp_impl(fdp, fd, fp, td, audit)); } } @@ -1347,8 +1352,6 @@ kern_close(struct thread *td, int fd) fdp = td->td_proc->p_fd; - AUDIT_SYSCLOSE(td, fd); - FILEDESC_XLOCK(fdp); if ((fp = fget_locked(fdp, fd)) == NULL) { FILEDESC_XUNLOCK(fdp); @@ -1357,7 +1360,7 @@ kern_close(struct thread *td, int fd) fdfree(fdp, fd); /* closefp() drops the FILEDESC lock for us. */ - return (closefp(fdp, fd, fp, td, true)); + return (closefp(fdp, fd, fp, td, true, true)); } int @@ -2671,7 +2674,7 @@ fdcloseexec(struct thread *td) (fde->fde_flags & UF_EXCLOSE))) { FILEDESC_XLOCK(fdp); fdfree(fdp, i); - (void) closefp(fdp, i, fp, td, false); + (void) closefp(fdp, i, fp, td, false, false); FILEDESC_UNLOCK_ASSERT(fdp); } } Modified: head/sys/security/audit/audit.h ============================================================================== --- head/sys/security/audit/audit.h Thu Dec 17 18:51:09 2020 (r368730) +++ head/sys/security/audit/audit.h Thu Dec 17 18:52:04 2020 (r368731) @@ -140,7 +140,7 @@ void audit_arg_argv(char *argv, int argc, int length) void audit_arg_envv(char *envv, int envc, int length); void audit_arg_rights(cap_rights_t *rightsp); void audit_arg_fcntl_rights(uint32_t fcntlrights); -void audit_sysclose(struct thread *td, int fd); +void audit_sysclose(struct thread *td, int fd, struct file *fp); void audit_cred_copy(struct ucred *src, struct ucred *dest); void audit_cred_destroy(struct ucred *cred); void audit_cred_init(struct ucred *cred); Modified: head/sys/security/audit/audit_arg.c ============================================================================== --- head/sys/security/audit/audit_arg.c Thu Dec 17 18:51:09 2020 (r368730) +++ head/sys/security/audit/audit_arg.c Thu Dec 17 18:52:04 2020 (r368731) @@ -995,12 +995,10 @@ audit_arg_fcntl_rights(uint32_t fcntlrights) * call itself. */ void -audit_sysclose(struct thread *td, int fd) +audit_sysclose(struct thread *td, int fd, struct file *fp) { - cap_rights_t rights; struct kaudit_record *ar; struct vnode *vp; - struct file *fp; KASSERT(td != NULL, ("audit_sysclose: td == NULL")); @@ -1010,12 +1008,10 @@ audit_sysclose(struct thread *td, int fd) audit_arg_fd(fd); - if (getvnode(td, fd, cap_rights_init(&rights), &fp) != 0) - return; - vp = fp->f_vnode; + if (vp == NULL) + return; vn_lock(vp, LK_SHARED | LK_RETRY); audit_arg_vnode1(vp); VOP_UNLOCK(vp); - fdrop(fp, td); }