From nobody Tue Jan 16 02:56:25 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TDYYk1lHGz56SLh; Tue, 16 Jan 2024 02:56:26 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TDYYk18x5z4fYY; Tue, 16 Jan 2024 02:56:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705373786; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=e1HeVwNy2Ndgw+xxmVYouU57WsHbcHh0S4cgXHSX0xE=; b=Nbp69x2Yl/nx4rsb+moVwRww0Dnd8YVoRfZR0LfKE/iywjOQk57YYv7L0Lz3MytsSdLfL5 PJoOwH0ZBRPYsz377is8b3Bfi667PbYoU/ZMtp9T7t8r4aWRUM7gz1ZB9f8pyal051MY3h sWb+2BMJjUVZYECwc2vSW3EOYyB8rBeUZJiZOu540tRlVmpg9BULxVzEwrYbp9onNLldH6 kuz92tLEe5uooAmE0QQyCmng0SENTkUlPQRKR7O+vbW9M99cfGzh431h5UUBmztSM1DLNA 8jdtOi9OOcoqL9Hx0MCjw8Mu4myzwmuTFfIcXbqRj2tttiBNYpQO/aVx03dMEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1705373786; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=e1HeVwNy2Ndgw+xxmVYouU57WsHbcHh0S4cgXHSX0xE=; b=uMeHMglk6+70jl+q9X8lPG93qwM7cO3H2NkSA6eg7QwHIt1rQUA/7yhS3eD+9G0SWZ/kCh Je4D/s7Y9u1I9CyqNfdiiGKPqPHuSILq2fuOcGnE/QxHTvGWb4BVZqWfMqnWxg9NZ/syeU EoBYtQxsj5ht/XBa8gzUiQghDtOIAlM0vmrjbNGzsLfeE9WaUEEcKF8h4Hi9RYXbl43OiR sz3T6M1cf6Wmk9CgHdOwmldKXsBaSdt1o63+WsxAO5l7EBxAy6xV1K4OfCp4bRLsFMl/jR 8FuA95G49cUa62HBHnduPw2qweSx/c0ul8RJlXg/LLGyGzsyPmkZjr/lrJhWqQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1705373786; a=rsa-sha256; cv=none; b=UPCdrl2UuiH8Mx8Ymc8lfXFihLIYjh2gFXSV81KAUew9cQmlUz48cz+j7Nh3Doj1ZOgS7h mpFU+PY6kqHBlwwwlMiDRGrPhZRXN4+za6AgHdeI3j8xY/XdcKnbSK1yVPcFdUzK6ZQJJB hCtxKgMzjyS0hn7ehTmbxY2hFGpaphhvHvk/4NFZEG2KzjUES/aItVLEdNkWJQ1BvBJkf0 USU5CK1uSSkEFNW65d907z7CCftkqxVHp/uqxiYAIq+ujMSdqRXXKL9EO71pGow+EyhtVG GsN22YjopKrNBYe973xCI1A7Ht6TZ3GYRtJSRIXBlzmF9KYhr+uYfmiKgSj3fg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4TDYYk0FrJzN02; Tue, 16 Jan 2024 02:56:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 40G2uPsM071494; Tue, 16 Jan 2024 02:56:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 40G2uPNx071491; Tue, 16 Jan 2024 02:56:25 GMT (envelope-from git) Date: Tue, 16 Jan 2024 02:56:25 GMT Message-Id: <202401160256.40G2uPNx071491@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: d51dac5f1370 - main - kern: tty: fix EOF handling for canonical reads List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d51dac5f1370bdca1ea20c6b48cdea463f6f5dda Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=d51dac5f1370bdca1ea20c6b48cdea463f6f5dda commit d51dac5f1370bdca1ea20c6b48cdea463f6f5dda Author: Kyle Evans AuthorDate: 2024-01-16 02:55:58 +0000 Commit: Kyle Evans CommitDate: 2024-01-16 02:55:58 +0000 kern: tty: fix EOF handling for canonical reads If the read(2) buffer is one byte short of an EOF, then we'll end up reading the line into the buffer, then re-entering and seeing an EOF at the beginning of the inq, assuming it's a zero-length line. Fix this corner-case by searching one more byte than we have available for an EOF. If we found it, then we'll trim it here; otherwise, we'll limit our read to just the space we have in the out buffer and the next read(2) will (potentially) read the remainder of the line. Fix FIONREAD while we're here to match what an application can expect read(2) to return -- scan for the first break character in the part of the input that's been canonicalized, we'll never return more than that. PR: 276220 Reviewed by: cy, imp (both previous version), kib Differential Revision: https://reviews.freebsd.org/D43378 --- sys/kern/tty.c | 2 +- sys/kern/tty_ttydisc.c | 72 ++++++++++++++++++++++++++++++++++++++++++++------ sys/sys/ttydisc.h | 1 + 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/sys/kern/tty.c b/sys/kern/tty.c index 61a6beafa2c0..e4186a67cb31 100644 --- a/sys/kern/tty.c +++ b/sys/kern/tty.c @@ -1673,7 +1673,7 @@ tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, /* This device supports non-blocking operation. */ return (0); case FIONREAD: - *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq); + *(int *)data = ttydisc_bytesavail(tp); return (0); case FIONWRITE: case TIOCOUTQ: diff --git a/sys/kern/tty_ttydisc.c b/sys/kern/tty_ttydisc.c index 3016f5e4ccb5..f6e9e9869951 100644 --- a/sys/kern/tty_ttydisc.c +++ b/sys/kern/tty_ttydisc.c @@ -112,15 +112,20 @@ ttydisc_close(struct tty *tp) ttyhook_close(tp); } -static int -ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) +/* + * Populate our break array; it should likely be at least 4 bytes in size to + * allow for \n, VEOF, and VEOL. + */ +static void +ttydisc_read_break(struct tty *tp, char *breakc, size_t breaksz) { - char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */ - int error; - size_t clen, flen = 0, n = 1; - unsigned char lastc = _POSIX_VDISABLE; + size_t n = 0; + MPASS(breaksz != 0); + + breakc[n++] = CNL; #define BREAK_ADD(c) do { \ + MPASS(n < breaksz - 1); /* NUL terminated */ \ if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \ breakc[n++] = tp->t_termios.c_cc[c]; \ } while (0) @@ -128,7 +133,48 @@ ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) BREAK_ADD(VEOF); BREAK_ADD(VEOL); #undef BREAK_ADD + breakc[n] = '\0'; +} + +size_t +ttydisc_bytesavail(struct tty *tp) +{ + size_t clen; + char breakc[4]; + unsigned char lastc = _POSIX_VDISABLE; + + clen = ttyinq_bytescanonicalized(&tp->t_inq); + if (!CMP_FLAG(l, ICANON) || clen == 0) + return (clen); + + ttydisc_read_break(tp, &breakc[0], sizeof(breakc)); + clen = ttyinq_findchar(&tp->t_inq, breakc, clen, &lastc); + + /* + * We might have a partial line canonicalized in the input queue if we, + * for instance, switched to ICANON after taking some input in raw mode. + * In this case, read(2) will block because we only have a partial line. + */ + if (lastc == _POSIX_VDISABLE) + return (0); + + /* If VEOF was our terminal, it must be discarded (not counted). */ + if (CMP_CC(VEOF, lastc)) + clen--; + + return (clen); +} + +static int +ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) +{ + char breakc[4]; /* enough to hold \n, VEOF and VEOL. */ + int error; + size_t clen, flen = 0; + unsigned char lastc = _POSIX_VDISABLE; + + ttydisc_read_break(tp, &breakc[0], sizeof(breakc)); do { error = tty_wait_background(tp, curthread, SIGTTIN); @@ -153,7 +199,7 @@ ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) * cause the TTY layer to return data in chunks using * the blocksize (except the first and last blocks). */ - clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid, + clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid + 1, &lastc); /* No more data. */ @@ -169,10 +215,20 @@ ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) continue; } - /* Don't send the EOF char back to userspace. */ + /* + * Don't send the EOF char back to userspace. Our above call to + * ttyinq_findchar overreads by 1 character in case we would + * otherwise be leaving an EOF for the next read(). We'll trim + * clen back down to uio_resid whether we find our EOF or not. + */ if (CMP_CC(VEOF, lastc)) flen = 1; + /* + * Trim clen back down to the buffer size, since we had + * intentionally over-read. + */ + clen = MIN(uio->uio_resid + flen, clen); MPASS(flen <= clen); /* Read and throw away the EOF character. */ diff --git a/sys/sys/ttydisc.h b/sys/sys/ttydisc.h index 0458fae6e34b..81d436139555 100644 --- a/sys/sys/ttydisc.h +++ b/sys/sys/ttydisc.h @@ -44,6 +44,7 @@ struct uio; /* Top half routines. */ void ttydisc_open(struct tty *tp); void ttydisc_close(struct tty *tp); +size_t ttydisc_bytesavail(struct tty *tp); int ttydisc_read(struct tty *tp, struct uio *uio, int ioflag); int ttydisc_write(struct tty *tp, struct uio *uio, int ioflag); void ttydisc_optimize(struct tty *tp);