From nobody Wed Oct 23 17:11:21 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 4XYbDT4VdLz5ZS94; Wed, 23 Oct 2024 17:11:21 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XYbDT3dryz52F0; Wed, 23 Oct 2024 17:11:21 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729703481; 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=Maf2477hpFAskeArXUeJrNbik/ceLXuc1k//1TJydL8=; b=O2t1i7lMNyZyEFZYD2DkgebAnUD0Fywc65XnPt9/KGBLeoWQegWYTAt3POaxbBFGq8xBiB roVEoWvzEXGw2aRNIxfNaQa1PhmBZwgu3ja3c8C1MwA2o3zzgJPY+CzK2OxB5xR9djMHRL 02S/5H9ordC3kkGv8aDgjcYN/gUkdGQPlsHs4eOlLxOB1OtkI8/zWUe023EbHahYOOykG0 XmQulTu2C3GHUJfOlylIF9OJdi9eEFoypCsi09gVu9mAh0IQWjfZf/n58ja/ECMG/2ltwF nhSSxjwZA67fqFsXcVCWqyfQeRnhnDU7MgsITYLBEK5iEJDyemGRjrm3EYowWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1729703481; 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=Maf2477hpFAskeArXUeJrNbik/ceLXuc1k//1TJydL8=; b=yw0r9ymfR+1SgnnWklPoYgR9jwrAwKAJDI8alKsu6D5l/o26F8t1Q7ZgHGb24IeaaKrenf SEX13LdSg4DqZ4agDMeZCYD1CqnUtd3deUMau91+zz5/v0kK52Vmjh03B4fcOnJ6HaP70a zK+ywqYuTnIbM2iHu/li8rCTxjKojmHLlzywh/cQcMQGYsK1uSQcj+0JS/fv+VCnBKHtO9 D5BaPGr3AuMa9h+Mr09VqQ19gLNZZewA0Kg43iPxEB3d9a25HGNJTOduZLycepVB6jOcj3 T9rwClUfcMfEQxqvi6sOzCwbnSqiAkSXmyvEEVFq+RmfaiiS/x93tofHfulf6g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1729703481; a=rsa-sha256; cv=none; b=BhzZGFIy4Yu3ALMHHj5cAYjLW2B8n0g7kYWWf8Kfp/FuQ664viHVEEU8LhJCc/Acto31pB IrYEkN38vAmG+d6F6UijaJVFeQxnzYjoHIRvTfsVSgY648B1ndLeLUCl32eawFVtnR1OhC cE053++p1JblpMBXmuKmBDQ/jqfepLHqWKA0AEV+7eQuN9s0d3cUse1xihUz0TRZJt9zeF ZPWuq86CN5mVjfa+7CKrXUIo+9sJ43+Wgw/ksg7Ga/kCy4hdiGLrgNImpnNxtkWJ6mR7UE mX6J5E0Gs4+yvvJZ4uo0xrJ7579ePXgYRjvcKmxgFavebvzZk/MKYZaLEsP1mQ== 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 4XYbDT3BF4z165c; Wed, 23 Oct 2024 17:11:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49NHBLjb022288; Wed, 23 Oct 2024 17:11:21 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49NHBLPG022285; Wed, 23 Oct 2024 17:11:21 GMT (envelope-from git) Date: Wed, 23 Oct 2024 17:11:21 GMT Message-Id: <202410231711.49NHBLPG022285@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ed Maste Subject: git: 0953460ce149 - main - libc: fix access mode tests in fmemopen(3) 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0953460ce149e6f384aafbcb1e6213dfbf8f6a16 Auto-Submitted: auto-generated The branch main has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=0953460ce149e6f384aafbcb1e6213dfbf8f6a16 commit 0953460ce149e6f384aafbcb1e6213dfbf8f6a16 Author: Ed Maste AuthorDate: 2024-10-23 13:41:51 +0000 Commit: Ed Maste CommitDate: 2024-10-23 17:11:14 +0000 libc: fix access mode tests in fmemopen(3) Previously a stream opened as read-only could be written to. Add a test case for the fix. Also correct another incorrect access mode check that worked by accident, and improve the tests for that. PR: 281953 Reported by: Erkki Moorits, fuz Reviewed by: fuz, khng (earlier) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D47265 --- lib/libc/stdio/fmemopen.c | 10 +++++----- lib/libc/tests/stdio/fmemopen2_test.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c index 2f835a34951b..dc323921d93a 100644 --- a/lib/libc/stdio/fmemopen.c +++ b/lib/libc/stdio/fmemopen.c @@ -74,10 +74,9 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode) } /* - * There's no point in requiring an automatically allocated buffer - * in write-only mode. + * An automatically allocated buffer is only allowed in read-write mode. */ - if (!(flags & O_RDWR) && buf == NULL) { + if ((flags & O_ACCMODE) != O_RDWR && buf == NULL) { errno = EINVAL; return (NULL); } @@ -136,9 +135,10 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode) break; } + /* Disable read in O_WRONLY mode, and write in O_RDONLY mode. */ f = funopen(ck, - flags & O_WRONLY ? NULL : fmemopen_read, - flags & O_RDONLY ? NULL : fmemopen_write, + (flags & O_ACCMODE) == O_WRONLY ? NULL : fmemopen_read, + (flags & O_ACCMODE) == O_RDONLY ? NULL : fmemopen_write, fmemopen_seek, fmemopen_close); if (f == NULL) { diff --git a/lib/libc/tests/stdio/fmemopen2_test.c b/lib/libc/tests/stdio/fmemopen2_test.c index 2e1b9ea917a5..4a7242ee6b2f 100644 --- a/lib/libc/tests/stdio/fmemopen2_test.c +++ b/lib/libc/tests/stdio/fmemopen2_test.c @@ -132,9 +132,11 @@ ATF_TC_BODY(test_autoalloc, tc) /* Open a FILE * using a wrong mode */ fp = fmemopen(NULL, 512, "r"); ATF_REQUIRE(fp == NULL); + ATF_REQUIRE(errno == EINVAL); fp = fmemopen(NULL, 512, "w"); ATF_REQUIRE(fp == NULL); + ATF_REQUIRE(errno == EINVAL); } ATF_TC_WITHOUT_HEAD(test_data_length); @@ -271,6 +273,35 @@ ATF_TC_BODY(test_size_0, tc) ATF_REQUIRE(errno == EINVAL); } +/* + * PR281953 - ensure we cannot write in read-only only mode, and cannot read in + * write-only mode. + */ +ATF_TC_WITHOUT_HEAD(test_rdonly_wronly); +ATF_TC_BODY(test_rdonly_wronly, tc) +{ + FILE *fp; + char buf[16]; + char buf_orig[16] = "input data"; + char buf_write[16] = "write"; + size_t sz; + + memcpy(buf, buf_orig, sizeof(buf)); + fp = fmemopen(buf, sizeof(buf), "r"); + ATF_REQUIRE(fp != NULL); + sz = fwrite(buf_write, 1, strlen(buf_write), fp); + ATF_REQUIRE(sz == 0); + ATF_REQUIRE(errno == EBADF); + ATF_REQUIRE(memcmp(buf, buf_orig, sizeof(buf)) == 0); + fclose(fp); + + fp = fmemopen(buf_orig, sizeof(buf), "w"); + sz = fread(buf, sizeof(buf), 1, fp); + ATF_REQUIRE(sz == 0); + ATF_REQUIRE(errno == EBADF); + fclose(fp); +} + ATF_TP_ADD_TCS(tp) { @@ -280,6 +311,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, test_binary); ATF_TP_ADD_TC(tp, test_append_binary_pos); ATF_TP_ADD_TC(tp, test_size_0); + ATF_TP_ADD_TC(tp, test_rdonly_wronly); return (atf_no_error()); }