From nobody Tue Oct 29 19:13:01 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 4XdKf54VSZz5bpKm; Tue, 29 Oct 2024 19:13:01 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XdKf52cPbz4F75; Tue, 29 Oct 2024 19:13:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730229181; 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=lbEFEpHLR3mpzdGFfIoBhIP/EdISrooUG+zSPIeH9JY=; b=YbQLX4X5VR6fE4DctKECF9yYR2tughnf66wR8TKzaeBm/3//ebdtJknF/6zCv1muyTq8Cf 9ebFrF5UISRVFWaGCDIHqtGZPNbMdr+4fdCyB3KywWmwC67xfHwbGC1pLWDU9Kkv68oiCF J7lVFsiNzPII1yjQ6ItGkp49YRe3Gtr0wourLOul/RI5jcu5cU4ENPNh9lU4tDfLlxEBWn ESQCZRcDTuudxwRxbseFE/mViuJ8MUIZ8599vgWUFL+81S879Mq1xPcd74OWj1SIcetZo4 wsQRyXC4wc/BdjkF3FlYzpDEmOxHmaxRnI/Sql2RP/alZIvjer+6OWYJg7zWDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1730229181; 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=lbEFEpHLR3mpzdGFfIoBhIP/EdISrooUG+zSPIeH9JY=; b=HA7KTU7LsO/1J5vsN8Q4yexHClVjltpZFkrrDT7tb8UOn2fXS+n+j0/L3XJ9fMlTK4MynQ B0GPk8kvn3mFs0Nj1e7h0Mq11Uv1vz15ZAa46ldjRoKQoPVu2eF3AVlKlGCz8G+Aa4o7DK qs/semw8fyf82uoyCl4n9n5LPGuFw+ye5LzCN2VOhZ6XFvOYwO+uuoH+Tltn5P+vY5tJhn dLwR0I1I6E9TzGZKniYCsPq651ikZKHLxh5sfqqJhkoALOeKOyTfL1+x2gZ8/6HXHFYhog UKuGh1UrF/yWUbk6QJQGlyF2vxh5qpISQ+9ZFwOwLo1trh2anQiQFLqZPMpRhg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1730229181; a=rsa-sha256; cv=none; b=SvmQ26cTnNLMN11XOd8zip9J2kudM5mAofxOALTVr9LFPezrH2QUdGZA78EjWjvNiWJRrB fRaLitszoKnqpXokcRiiDDxFreZDrs5hZjqRy/iDtgni/HHhYL5TGx3fBnnboB37dwkACg 3K8cg37cClynL49ViE6HDZd0F+TGbdFNqGPbCHLIVhYAIiHnx2PwN0uw4Jzm+Z1zHfcswa +JBjJS8XUWKPoB1YftkXMhudvo1HSms2zfr5UF7c03/wy1rg+VT5vDj0U8Bq59HeaMHPPh RnJ/hsUOoVqWRU/G/INW+4hVhFdu5wyKokJ5V9XPiin+jgOyICps0+mEXsJk+A== 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 4XdKf52DRGzc6Q; Tue, 29 Oct 2024 19:13:01 +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 49TJD1CR074450; Tue, 29 Oct 2024 19:13:01 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49TJD1Dv074447; Tue, 29 Oct 2024 19:13:01 GMT (envelope-from git) Date: Tue, 29 Oct 2024 19:13:01 GMT Message-Id: <202410291913.49TJD1Dv074447@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 2b9e6e090a13 - stable/13 - 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/stable/13 X-Git-Reftype: branch X-Git-Commit: 2b9e6e090a13ecba890fc4ad318a46ed23e25bad Auto-Submitted: auto-generated The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=2b9e6e090a13ecba890fc4ad318a46ed23e25bad commit 2b9e6e090a13ecba890fc4ad318a46ed23e25bad Author: Ed Maste AuthorDate: 2024-10-23 13:41:51 +0000 Commit: Ed Maste CommitDate: 2024-10-29 19:12:17 +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 (cherry picked from commit 0953460ce149e6f384aafbcb1e6213dfbf8f6a16) (cherry picked from commit 6b9f7133aba44189d9625c352bc2c2a59baf18ef) (cherry picked from commit 4fbd6e0e3ca8e69d2d3789ecda6e4dd76c34e06a) --- lib/libc/stdio/fmemopen.c | 10 +++++----- lib/libc/tests/stdio/fmemopen2_test.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c index 295592ac896f..d6f83722bb6f 100644 --- a/lib/libc/stdio/fmemopen.c +++ b/lib/libc/stdio/fmemopen.c @@ -75,10 +75,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); } @@ -137,9 +136,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 d68150d19594..2839ee04bb52 100644 --- a/lib/libc/tests/stdio/fmemopen2_test.c +++ b/lib/libc/tests/stdio/fmemopen2_test.c @@ -133,9 +133,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); @@ -272,6 +274,36 @@ 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"); + ATF_REQUIRE(fp != NULL); + sz = fread(buf, sizeof(buf), 1, fp); + ATF_REQUIRE(sz == 0); + ATF_REQUIRE(errno == EBADF); + fclose(fp); +} + ATF_TP_ADD_TCS(tp) { @@ -281,6 +313,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()); }