git: ba90708fb116 - releng/13.3 - kern: tty: fix ttyinq_read_uio assertion

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Wed, 07 Feb 2024 05:21:58 UTC
The branch releng/13.3 has been updated by kevans:

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

commit ba90708fb116a6587a992f0a14fd807f44b72309
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-01-16 02:55:58 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2024-02-07 05:18:59 +0000

    kern: tty: fix ttyinq_read_uio assertion
    
    It's clear from later context that `rlen` was always expected to include
    `flen`, as we'll trim `flen` bytes from the end of the read.  Relax our
    initial assertion to only require the total size less trimmed bytes to
    lie within the out buffer size.
    
    While we're here, I note that if we have to read more than one block and
    we're trimming from the end then we'll do the wrong thing and omit
    `flen` bytes from every block, rather than just the end.  Add an
    assertion to make sure we're not doing that, but the only caller that
    specifies a non-zero `flen` today will only really be doing so if rlen
    is entirely within a single buffer.
    
    Approved by:    re (cperciva)
    Reviewed by:    cy, imp
    
    (cherry picked from commit 09a43b8790bdeb97fbecd3ea767c2f599eb4a4d3)
    (cherry picked from commit 81ef0de636ff8ba0b8057ced593f2ab92597b1a6)
---
 sys/kern/tty_inq.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/sys/kern/tty_inq.c b/sys/kern/tty_inq.c
index daf3bde77712..0bf7c2fa5b5e 100644
--- a/sys/kern/tty_inq.c
+++ b/sys/kern/tty_inq.c
@@ -165,7 +165,8 @@ ttyinq_read_uio(struct ttyinq *ti, struct tty *tp, struct uio *uio,
     size_t rlen, size_t flen)
 {
 
-	MPASS(rlen <= uio->uio_resid);
+	/* rlen includes flen, flen bytes will be trimmed from the end. */
+	MPASS(rlen - flen <= uio->uio_resid);
 
 	while (rlen > 0) {
 		int error;
@@ -192,6 +193,14 @@ ttyinq_read_uio(struct ttyinq *ti, struct tty *tp, struct uio *uio,
 		MPASS(clen >= flen);
 		rlen -= clen;
 
+		/*
+		 * Caller shouldn't request that we trim anything if we might be
+		 * reading across blocks.  We could handle it, but today we do
+		 * not.
+		 */
+		if (flen > 0)
+			MPASS(rlen == 0);
+
 		/*
 		 * We can prevent buffering in some cases:
 		 * - We need to read the block until the end.