git: 8c9aa94b42bf - main - Convert runtime param checks to KASSERTs for fo_fspacectl

From: Ka Ho Ng <khng_at_FreeBSD.org>
Date: Sat, 23 Jul 2022 19:17:28 UTC
The branch main has been updated by khng:

URL: https://cgit.FreeBSD.org/src/commit/?id=8c9aa94b42bfe58f46be862ad5a08a68d9a19a4a

commit 8c9aa94b42bfe58f46be862ad5a08a68d9a19a4a
Author:     Ka Ho Ng <khng@FreeBSD.org>
AuthorDate: 2022-07-23 19:14:45 +0000
Commit:     Ka Ho Ng <khng@FreeBSD.org>
CommitDate: 2022-07-23 19:16:23 +0000

    Convert runtime param checks to KASSERTs for fo_fspacectl
    
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D35880
---
 sys/kern/uipc_shm.c  | 10 +++++-----
 sys/kern/vfs_vnops.c |  8 +++++---
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/sys/kern/uipc_shm.c b/sys/kern/uipc_shm.c
index 1ca088edfd78..0af2b22866f4 100644
--- a/sys/kern/uipc_shm.c
+++ b/sys/kern/uipc_shm.c
@@ -1990,16 +1990,16 @@ shm_fspacectl(struct file *fp, int cmd, off_t *offset, off_t *length, int flags,
 	off_t off, len;
 	int error;
 
-	/* This assumes that the caller already checked for overflow. */
+	KASSERT(cmd == SPACECTL_DEALLOC, ("shm_fspacectl: Invalid cmd"));
+	KASSERT((flags & ~SPACECTL_F_SUPPORTED) == 0,
+	    ("shm_fspacectl: non-zero flags"));
+	KASSERT(*offset >= 0 && *length > 0 && *length <= OFF_MAX - *offset,
+	    ("shm_fspacectl: offset/length overflow or underflow"));
 	error = EINVAL;
 	shmfd = fp->f_data;
 	off = *offset;
 	len = *length;
 
-	if (cmd != SPACECTL_DEALLOC || off < 0 || len <= 0 ||
-	    len > OFF_MAX - off || flags != 0)
-		return (EINVAL);
-
 	rl_cookie = rangelock_wlock(&shmfd->shm_rl, off, off + len,
 	    &shmfd->shm_mtx);
 	switch (cmd) {
diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index d5234b44e5eb..29851dcfaaa2 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -3607,11 +3607,13 @@ vn_fspacectl(struct file *fp, int cmd, off_t *offset, off_t *length, int flags,
 	struct vnode *vp;
 	int ioflag;
 
+	KASSERT(cmd == SPACECTL_DEALLOC, ("vn_fspacectl: Invalid cmd"));
+	KASSERT((flags & ~SPACECTL_F_SUPPORTED) == 0,
+	    ("vn_fspacectl: non-zero flags"));
+	KASSERT(*offset >= 0 && *length > 0 && *length <= OFF_MAX - *offset,
+	    ("vn_fspacectl: offset/length overflow or underflow"));
 	vp = fp->f_vnode;
 
-	if (cmd != SPACECTL_DEALLOC || *offset < 0 || *length <= 0 ||
-	    *length > OFF_MAX - *offset || flags != 0)
-		return (EINVAL);
 	if (vp->v_type != VREG)
 		return (ENODEV);