From nobody Mon Jun 19 13:08:38 2023 X-Original-To: dev-commits-src-branches@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 4Ql97V5Wg1z4g5ky; Mon, 19 Jun 2023 13:08:38 +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 4Ql97V2B9kz3r7c; Mon, 19 Jun 2023 13:08:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1687180118; 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=nPcWEGTFcxrpQMIxezv7yszBOqJaJMgooRDQPWRjjNI=; b=nrYLTRYXIEx+0exk12ruOaSBeIi1iYchf3+HPMb52086kyJcy6prhjwqvTl3361cpW6z6N I6zalkIUzGFVf7Wf90QO5dnCakE1XUHFwJCyg7olem4+uxWBMsm7f+IQMkSK+e/d28JLQH eNRB5Ol7k0CYzcV/txsLPpUl6nCquaidh+sMHAbdLj3KxGiB6iVKLj1EUuLifUIwaMpBeL DJUvKVSfj8aZVlhwNhO24nrTAYby4GzUteVm1yxZgtox5NWpESEUE4uQuFdsd0B1NqwIxS AlMPDciiH0AxVIV3uKkoBiKlTgT7/08MMKh8OfkvlxaMqQl0Opby+/sn0T0UQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1687180118; 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=nPcWEGTFcxrpQMIxezv7yszBOqJaJMgooRDQPWRjjNI=; b=IJvRd28eyFi1Nn7j6Ny/3X0KJR+oRXtYbgbyS3G2p9sjUwvCSaneZAWFIDNSaAf7ftxnQ3 t/tA6NGC3L8YDrvr/95AZjFRlEIhQXf0XBYnetQPzdn5gaAHpi5DrMA2vcxmaooE5PFOax LS3TpnXmAMUDno6SLuag35UEXQ4Z75zx97HUhn9zYar/sBVmyh4s/YCjjtOn52ccFoFQU/ f3bayz9BFpDpHFPnVG8IktdHJxsuWuehRkiwENex6vTZ2lrHnGiEQ2ol4OmJPfoaVnIB24 q84sXPwg1RcaV53KrvLWmAeo/RB9p1PrZmRoykxfG0b1TnzWN8ojBOfpuvFpmQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1687180118; a=rsa-sha256; cv=none; b=Pz6xn1Mn7C4X1mIljMHUGhPTb5x/9zViA8OXjqCar28WhDuNggLaKe4i9RhpUuzK7HMuhv ZUg1VZ2aWLdF3sW4rA/sGdO5FSyJxZrQzKCX9XbII0rA3YoaxN1BnSqmNuP2BWvxGJ/jKF g9YsPzqs7eiHqKCAkRITUpraLeIkaKHR07feqiT+QPYTIAVOkuECooLTyuVTaofCryaVNs syDzTs0FnFW6KVKC1DKP7K69uAFEebqjDTD95jzfa5cwzsTLwmNH4DoMZPAKpJZLFJVYpC cXw/soe3vZ29fpUagTbWUjhA6ztao6A9boV9KYgWLxyrNegrwV1dz20He/HuMw== 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 4Ql97V1HQdzHxv; Mon, 19 Jun 2023 13:08:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35JD8cc7035514; Mon, 19 Jun 2023 13:08:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35JD8ctr035513; Mon, 19 Jun 2023 13:08:38 GMT (envelope-from git) Date: Mon, 19 Jun 2023 13:08:38 GMT Message-Id: <202306191308.35JD8ctr035513@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: 299a7961f47d - stable/13 - opencrypto: Handle end-of-cursor conditions in crypto_cursor_segment() List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 299a7961f47d84f4bcb19ca6756ae61cc2d5d756 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=299a7961f47d84f4bcb19ca6756ae61cc2d5d756 commit 299a7961f47d84f4bcb19ca6756ae61cc2d5d756 Author: Mark Johnston AuthorDate: 2023-06-12 16:09:34 +0000 Commit: Mark Johnston CommitDate: 2023-06-19 12:56:56 +0000 opencrypto: Handle end-of-cursor conditions in crypto_cursor_segment() Some consumers, e.g., swcr_encdec(), may call crypto_cursor_segment() after having advanced the cursor to the end of the buffer. In this case I believe the right behaviour is to return NULL and a length of 0. When this occurs with a CRYPTO_BUF_VMPAGE buffer, the cc_vmpage pointer will point past the end of the page pointer array, so crypto_cursor_segment() ends up dereferencing a random pointer before the function returns a length of 0. The uio-backed cursor has a similar problem. Address this by keeping track of the residual buffer length and returning immediately once the length is zero. PR: 271766 Reported by: Andrew "RhodiumToad" Gierth Reviewed by: jhb MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40428 (cherry picked from commit 718d4a1d5643c2faf409001320c3fd64aae57638) --- sys/opencrypto/criov.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/sys/opencrypto/criov.c b/sys/opencrypto/criov.c index e54d31388133..2c3bc6b37cb9 100644 --- a/sys/opencrypto/criov.c +++ b/sys/opencrypto/criov.c @@ -320,6 +320,7 @@ crypto_cursor_init(struct crypto_buffer_cursor *cc, break; case CRYPTO_BUF_UIO: cc->cc_iov = cb->cb_uio->uio_iov; + cc->cc_buf_len = cb->cb_uio->uio_resid; break; default: #ifdef INVARIANTS @@ -385,6 +386,7 @@ crypto_cursor_advance(struct crypto_buffer_cursor *cc, size_t amount) cc->cc_offset += amount; break; } + cc->cc_buf_len -= remain; amount -= remain; cc->cc_iov++; cc->cc_offset = 0; @@ -405,14 +407,34 @@ crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len) { switch (cc->cc_type) { case CRYPTO_BUF_CONTIG: - *len = cc->cc_buf_len; - return (cc->cc_buf); + case CRYPTO_BUF_UIO: + case CRYPTO_BUF_VMPAGE: + if (cc->cc_buf_len == 0) { + *len = 0; + return (NULL); + } + break; case CRYPTO_BUF_MBUF: case CRYPTO_BUF_SINGLE_MBUF: if (cc->cc_mbuf == NULL) { *len = 0; return (NULL); } + break; + default: +#ifdef INVARIANTS + panic("%s: invalid buffer type %d", __func__, cc->cc_type); +#endif + *len = 0; + return (NULL); + } + + switch (cc->cc_type) { + case CRYPTO_BUF_CONTIG: + *len = cc->cc_buf_len; + return (cc->cc_buf); + case CRYPTO_BUF_MBUF: + case CRYPTO_BUF_SINGLE_MBUF: if (cc->cc_mbuf->m_flags & M_EXTPG) return (m_epg_segment(cc->cc_mbuf, cc->cc_offset, len)); *len = cc->cc_mbuf->m_len - cc->cc_offset; @@ -425,11 +447,7 @@ crypto_cursor_segment(struct crypto_buffer_cursor *cc, size_t *len) *len = cc->cc_iov->iov_len - cc->cc_offset; return ((char *)cc->cc_iov->iov_base + cc->cc_offset); default: -#ifdef INVARIANTS - panic("%s: invalid buffer type %d", __func__, cc->cc_type); -#endif - *len = 0; - return (NULL); + __assert_unreachable(); } } @@ -520,6 +538,7 @@ crypto_cursor_copyback(struct crypto_buffer_cursor *cc, int size, todo = MIN(remain, size); memcpy(dst, src, todo); src += todo; + cc->cc_buf_len -= todo; if (todo < remain) { cc->cc_offset += todo; break; @@ -609,6 +628,7 @@ crypto_cursor_copydata(struct crypto_buffer_cursor *cc, int size, void *vdst) todo = MIN(remain, size); memcpy(dst, src, todo); dst += todo; + cc->cc_buf_len -= todo; if (todo < remain) { cc->cc_offset += todo; break;