From nobody Thu Dec 23 16:45:14 2021 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 C9CA518F54DC; Thu, 23 Dec 2021 16:45:14 +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 4JKbf23bBvz3jqy; Thu, 23 Dec 2021 16:45:14 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 5B18E13D76; Thu, 23 Dec 2021 16:45:14 +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 1BNGjEen051046; Thu, 23 Dec 2021 16:45:14 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BNGjEFk051045; Thu, 23 Dec 2021 16:45:14 GMT (envelope-from git) Date: Thu, 23 Dec 2021 16:45:14 GMT Message-Id: <202112231645.1BNGjEFk051045@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: 0aa8b18bc9bb - main - libc: Fix regexec when sizeof(char *) > sizeof(long) 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: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0aa8b18bc9bb1d948d4152c50819d69940d68045 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1640277914; 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=iKtrQ2D+t2V7Rku8EcaexJXkejfr0sJq6vzGK4WVHug=; b=jG3mm2BV6bOMbufQXTdIJ9tTXibQcHUhs3zcjk75Ov8B5Zc5p4NMPlRMpoyzw+BcXslBOy lCpqz6T4UD4ES/SKHYlNBTjVHAruR0UcY2ZaOBmgUanflQyqxeXMRs3yedYmV1wqkKxkcp 0MkLxJyGGvA5+fBNLoSKTWLUV4QN1klHYuajNNmrvhOu0G1bzpnCgGJMQkrD6Xrfrgj3Zd ibVMPiUUwfCVd7z+0AKM40y6PW/e9R1JgyrgFFB9dSvGExS9NAxAOfYWJzFdKR9rkfh9Om jjc/+ASBiuFIHVXmV1OjBF6Q+sTk0tJEJfa0nYTJjs5oOuqH3Aa1gbOpTTWGLQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1640277914; a=rsa-sha256; cv=none; b=O/QFbMeZth8m7fRY+criK8vvrnV03TsRgnp7EXUVZLN85R1c0rOkATo3l+bNF5vA1WcdpN hMQU+dqEukDqZ0c+NE6FFx5K0+K3Ot90ZO7RWscHEEOEdyFnbcx+GO56AiQdxVCK+fzBhd T6HD+bVsa8BxsvQJjpXRyZwJVOoIzDLY2LxLyER07iTQZTKUDAy4ECJFpbLfENK+PY3j9W 3+lfeiS0yVT91aufTL6U1t6q/IqwigggoNMUMLvzsuQpW8JCqPtPfnEuqGkQ1+zIEKV1uM ld0cWA4Iw9GJyEyDy07CVa5l7uD/Az3FVdAOVsC0n16Zg9XYNIs7iPsfeDQUow== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=0aa8b18bc9bb1d948d4152c50819d69940d68045 commit 0aa8b18bc9bb1d948d4152c50819d69940d68045 Author: Jessica Clarke AuthorDate: 2021-12-23 16:38:10 +0000 Commit: Jessica Clarke CommitDate: 2021-12-23 16:38:10 +0000 libc: Fix regexec when sizeof(char *) > sizeof(long) The states macro is the type for engine.c to use, with states1 being a local macro for regexec to use to determine whether it can use the small matcher or not (by comparing nstates and 8*sizeof(states1)). However, macro bodies are expanded in the context of their use, and so when regexec uses states1 it uses the current value of states, which is left over as char * from the large version (or, really, the multi-byte one, but that reuses large's states). For all supported architectures in FreeBSD, the two have the same size, and so this confusion is harmless. However, for architectures like CHERI where that is not the case (or Windows's LLP64 as discovered by LLVM and fixed in 2010 in 2e071faed8e2) and sizeof(char *) is bigger than sizeof(long) regexec will erroneously try to use the small matcher when nstates is between sizeof(long) and sizeof(char *) (i.e. between 64 and 128 on CHERI, or 32 and 64 on LLP64) and end up overflowing the number of bits in the underlying long if it ever uses those high states. On weirder architectures where sizeof(long) is greater than sizeof(char *) this also fixes it to not fall back on the large matcher prematurely, but such architectures are likely limited to the embedded space, if they exist at all. Fix this by swapping round states and states1, so that states1 is defined directly as being long and states is an alias for it for the small matcher case. Found by: CHERI --- lib/libc/regex/regexec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/libc/regex/regexec.c b/lib/libc/regex/regexec.c index bf27d05f86c6..d7aa46f45b2b 100644 --- a/lib/libc/regex/regexec.c +++ b/lib/libc/regex/regexec.c @@ -97,8 +97,8 @@ xmbrtowc_dummy(wint_t *wi, } /* macros for manipulating states, small version */ -#define states long -#define states1 states /* for later use in regexec() decision */ +#define states1 long /* for later use in regexec() decision */ +#define states states1 #define CLEAR(v) ((v) = 0) #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n))) #define SET1(v, n) ((v) |= (unsigned long)1 << (n))