From nobody Sat Jan 15 23:44:24 2022 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 B1BE8194CFF3; Sat, 15 Jan 2022 23:44:24 +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 4Jbvs44H5Bz3qfx; Sat, 15 Jan 2022 23:44:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642290264; 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=UZ0urAVlotB+7FyJ+Ex6Mmn3MHbb2VHIPShnNihWSc4=; b=wlTziSY7ZbMTZ3n7mIU6WLQ4iqyzGkampXVKhqoreWeEdzPCSCDzLSrmJbcpycVpTYQJ8u GJOI2ASw628lvaA4qhs+rlRmNe0WEm1BLGOylvVTaMMd5cmpij5O0MnzBO4koUYyBBWhVd BpzFGqAB2bAcpXgXjRLAQXMWpR1ILKMPsLIIedQFC2SfV0qoaZDc7VNoZx/4HFBXEisa7F KaityZtE9OUrpJ6bERpHJbw7QR4ShCWoiUT2+FDvcm+FLioDBlgZEms8UQ6C8cRcCOIfgS QXazCqyQHDz/GbX9QV+S1U/j4u8O6w1KIFvKpasgPmccOtu65tst/i7fVNOQoQ== 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 72EB710714; Sat, 15 Jan 2022 23:44:24 +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 20FNiO9T061565; Sat, 15 Jan 2022 23:44:24 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 20FNiOkH061564; Sat, 15 Jan 2022 23:44:24 GMT (envelope-from git) Date: Sat, 15 Jan 2022 23:44:24 GMT Message-Id: <202201152344.20FNiOkH061564@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Stefan E=C3=9Fer?= Subject: git: 10af8e45a898 - main - fread.c: fix undefined behavior 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: se X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 10af8e45a89818754b80315539e167ae49599f17 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1642290264; 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=UZ0urAVlotB+7FyJ+Ex6Mmn3MHbb2VHIPShnNihWSc4=; b=mAWNnRY/KU3fIM555wsEzMvTIXWCyky6Rs3mBQzqktUHlnlfzCrFrNVRdMDZaf5MbEBPCi FbHgLePMPUgPbQsjmloxMq8Sow9gYSbk/+kI5QKGvt8oil5H8nVfSI098l0eKyx0HN8rIz OQe9gYPZ0+OnBa4AKryQVGpAQdyTGSZtyHws1TiczFnWeqyV7/WJvFSCg0qR8SoyFARyf1 Hki0A+7UPhkWkisSscZ0pyk60J9zo9KJaxpRdL49w0eFaglijTt6DmwOnb8ZdTENUuu60e 42IPAY4IPzzyGxHIEtPLVEAuQVStMUbbrWuU50V+lnJnRg0yViWcjXCLysAwNA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1642290264; a=rsa-sha256; cv=none; b=UhSM/YTxW3ZKqi2iO1tCf96IQPIosfoyCwKPQbiIzyp+O2BYY23ioO8p6w1JwuOWIfTTVd /ipk7pyU+eMrq3IbBMHkOQvGu71i94Ktozuu24MnnW7MFn99W8K6YumQJJcDOFXUFOXN/t ku7zYktnZob3SVNvlu++/V9Fu7K+fVcKBBLp+E6v4n9bP0p4Bj9HH6aGJhMR0fMps6rD4K u11YczD7aftLEmSnluKIJPQnFFp5mjRELvAoaCnc85m6FbgIxL9riVmvSY8qt6xyyBYQgX Ke1qFXRHQPhQerYvyP9U80VZ/JG1/t1Q1DJgdtlNqEqk7mzQWEXSkY9rfai4OA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by se: URL: https://cgit.FreeBSD.org/src/commit/?id=10af8e45a89818754b80315539e167ae49599f17 commit 10af8e45a89818754b80315539e167ae49599f17 Author: Stefan Eßer AuthorDate: 2022-01-15 23:30:04 +0000 Commit: Stefan Eßer CommitDate: 2022-01-15 23:43:56 +0000 fread.c: fix undefined behavior A case of undefined behavior in __fread() has been detected by UBSAN and reported by Mark Millard: /usr/main-src/lib/libc/stdio/fread.c:133:10: runtime error: applying zero offset to null pointer SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior in /usr/main-src/lib/libc/stdio/fread.c:133:10 While being benign (the NULL pointer is later passed to memcpy() with a length argument of 0), this issue causes in the order of 600 Kyua test cases to fail on systems running a world built with WITH_UBSAN and WITH_ASAN. The undefined behavior can be prevented by skipping operations that have no effect for r == 0. Mark Millard has suggested to only skip this code segment if fp->_p == NULL, but I have verified that for the case of r == 0 no further argument checking is performed on the addresses passed to memcpy() and thus no bugs are hidden from the sanitizers due to the simpler condition chosen. Reported by: Mark Millard (marklmi@yahoo.com) Tested by: Mark Millard (marklmi@yahoo.com) Differential Revision: https://reviews.freebsd.org/D33903 MFC after: 2 weeks --- lib/libc/stdio/fread.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 11f8d13f0caf..cafe86fe7961 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -129,11 +129,13 @@ __fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp) } while (resid > (r = fp->_r)) { - (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); - fp->_p += r; - /* fp->_r = 0 ... done in __srefill */ - p += r; - resid -= r; + if (r != 0) { + (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); + fp->_p += r; + /* fp->_r = 0 ... done in __srefill */ + p += r; + resid -= r; + } if (__srefill(fp)) { /* no more input: return partial result */ return ((total - resid) / size);