git: 02645b886bc6 - main - ktrace: use the limit of the trace initiator for file size limit on writes

Konstantin Belousov kib at FreeBSD.org
Sat May 22 12:16:58 UTC 2021


The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=02645b886bc62dfd8a998fd51d2e6c1bbca03ecb

commit 02645b886bc62dfd8a998fd51d2e6c1bbca03ecb
Author:     Konstantin Belousov <kib at FreeBSD.org>
AuthorDate: 2021-05-14 23:51:01 +0000
Commit:     Konstantin Belousov <kib at FreeBSD.org>
CommitDate: 2021-05-22 12:16:09 +0000

    ktrace: use the limit of the trace initiator for file size limit on writes
    
    Reviewed by:    markj
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D30257
---
 sys/kern/kern_ktrace.c |  6 ++++++
 sys/kern/vfs_vnops.c   | 18 ++++++++++--------
 sys/sys/proc.h         |  1 +
 3 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/sys/kern/kern_ktrace.c b/sys/kern/kern_ktrace.c
index c03eea4991a1..b686f2e2717a 100644
--- a/sys/kern/kern_ktrace.c
+++ b/sys/kern/kern_ktrace.c
@@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/namei.h>
 #include <sys/priv.h>
 #include <sys/proc.h>
+#include <sys/resourcevar.h>
 #include <sys/unistd.h>
 #include <sys/vnode.h>
 #include <sys/socket.h>
@@ -148,6 +149,7 @@ static struct sx ktrace_sx;
 struct ktr_io_params {
 	struct vnode	*vp;
 	struct ucred	*cr;
+	off_t		lim;
 	u_int		refs;
 };
 
@@ -465,6 +467,7 @@ ktr_io_params_alloc(struct thread *td, struct vnode *vp)
 	res = malloc(sizeof(struct ktr_io_params), M_KTRACE, M_WAITOK);
 	res->vp = vp;
 	res->cr = crhold(td->td_ucred);
+	res->lim = lim_cur(td, RLIMIT_FSIZE);
 	res->refs = 1;
 	return (res);
 }
@@ -1255,6 +1258,7 @@ ktr_writerequest(struct thread *td, struct ktr_request *req)
 	struct uio auio;
 	struct iovec aiov[3];
 	struct mount *mp;
+	off_t lim;
 	int datalen, buflen;
 	int error;
 
@@ -1282,6 +1286,7 @@ ktr_writerequest(struct thread *td, struct ktr_request *req)
 
 	vp = kiop->vp;
 	cred = kiop->cr;
+	lim = kiop->lim;
 
 	vrefact(vp);
 	KASSERT(cred != NULL, ("ktr_writerequest: cred == NULL"));
@@ -1319,6 +1324,7 @@ ktr_writerequest(struct thread *td, struct ktr_request *req)
 
 	vn_start_write(vp, &mp, V_WAIT);
 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
+	td->td_ktr_io_lim = lim;
 #ifdef MAC
 	error = mac_vnode_check_write(cred, NOCRED, vp);
 	if (error == 0)
diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index 69da69da19d4..9c309c83f805 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -2359,18 +2359,20 @@ int
 vn_rlimit_fsize(const struct vnode *vp, const struct uio *uio,
     struct thread *td)
 {
+	off_t lim;
 
 	if (vp->v_type != VREG || td == NULL ||
 	    (td->td_pflags2 & TDP2_ACCT) != 0)
 		return (0);
-	if ((uoff_t)uio->uio_offset + uio->uio_resid >
-	    lim_cur(td, RLIMIT_FSIZE)) {
-		PROC_LOCK(td->td_proc);
-		kern_psignal(td->td_proc, SIGXFSZ);
-		PROC_UNLOCK(td->td_proc);
-		return (EFBIG);
-	}
-	return (0);
+	ktr_write = (td->td_pflags & TDP_INKTRACE) != 0;
+	lim = ktr_write ? td->td_ktr_io_lim : lim_cur(td, RLIMIT_FSIZE);
+	if ((uoff_t)uio->uio_offset + uio->uio_resid < lim)
+		return (0);
+
+	PROC_LOCK(td->td_proc);
+	kern_psignal(td->td_proc, SIGXFSZ);
+	PROC_UNLOCK(td->td_proc);
+	return (EFBIG);
 }
 
 int
diff --git a/sys/sys/proc.h b/sys/sys/proc.h
index c54d4399f948..926f0de14b84 100644
--- a/sys/sys/proc.h
+++ b/sys/sys/proc.h
@@ -378,6 +378,7 @@ struct thread {
 	void		*td_lkpi_task;	/* LinuxKPI task struct pointer */
 	int		td_pmcpend;
 	void		*td_coredump;	/* (c) coredump request. */
+	off_t		td_ktr_io_lim;	/* (k) limit for ktrace file size */
 #ifdef EPOCH_TRACE
 	SLIST_HEAD(, epoch_tracker) td_epochs;
 #endif


More information about the dev-commits-src-main mailing list