From nobody Mon Oct 02 19:26:33 2023 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 4RzrY606Ngz4wPLt; Mon, 2 Oct 2023 19:26:34 +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 4RzrY56gN8z4fHq; Mon, 2 Oct 2023 19:26:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696274793; 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=MZxeFTiFKkmGRCCHruFmszZrnNM4arAP9VdVebceWCg=; b=HbQMU6FOykEc7QU5DBUq7ijm+/ytmMSBFryWnHhuIT3E7VseHizfc2ihnLSU75sjGDlyNO SH+RlUZTvq8pE2fsdw88ACOuLKgYfPfg1EmvXgeYQqs4Gt1k7J4eJt4Af6XPqZyMokY2+E Utb5uWGBIEEyKs72sucL9vJGa3+vTYi12Ri5FG8mktwtTKDg3+666C1RRnrVVGrxd8glfF 0qMG37iHdub1S2gy+/0GG69h2kpRjOawXLdjBYSDzAw1W+ZQmvrjloT3Rvs+0B3K6LK20c /1B/TV1ZMQ7xgojD01aMZusKXVgzKbUxNaDildOjPxN3jT73dq6LwIorgWGItg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1696274793; a=rsa-sha256; cv=none; b=nZkvNT0xV+ocd2ePAfH+0tlcUWdcV70pFAk0aDO2y7TKvnG5Zby+DigiWREZ/H+wk4k4Va tQnHqOWkIaUn/4F8mBF1EVp12hKqCgjqydQE+4fGG2YSSL4N8Cz4FkZ77/PwVIVoP1fRPH X9CdrKpDX4g30IisqCqCIdHN4fDyJ4TxGbzTjXELyf0WH2wzoxmVl/NpfKJ/A1Y9RVGjN0 PnlqRU3hKSbjA3BVYJIwMCjnmvftfAsCMa7/fDDvz0xbhD+29hb5oFljRLIZIIqoSlJXJS OP9eYSTwEYegRiZyfPYcSveRLjI56CYwT0+G4AKitycTEYwqJzu3oiT3qAF0Vg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1696274793; 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=MZxeFTiFKkmGRCCHruFmszZrnNM4arAP9VdVebceWCg=; b=sGR1DMcB2bWirFi2EFrHLBQyXDFYOMZ+8LU6SPcdBprtexA3PKlHsPGEdvsP1Xq1H2U52g cTm4yk/nMPuCn3fNyl9lAXIk1yGCov3d9UN6MyBNSktjmXFhbTopl+wL903xz1bAA5jVxA 5qoA/kdvP2sl5nQW0fkeRYZdKtvWsRSHQgnBa54eVo6ebi+voJZOAc9psVSGjgArFP39vx y/eYJ3I5HsPsM7M4D60dHgvsaNipRBXtL7sXJPOX6yJ+zAd3PWy9Ibm2Ch5o3dcnoSRjHf adWRUHoNe/O/GaWnqGoDmoNv7a8rdAzDkA53kcjiTvFddPb7G/IiiZJjyPUscA== 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 4RzrY55Htgz8qB; Mon, 2 Oct 2023 19:26:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 392JQXiR085790; Mon, 2 Oct 2023 19:26:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 392JQXeV085787; Mon, 2 Oct 2023 19:26:33 GMT (envelope-from git) Date: Mon, 2 Oct 2023 19:26:33 GMT Message-Id: <202310021926.392JQXeV085787@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Robert Clausecker Subject: git: c6cc06d465a9 - main - lib/libc/tests/string: expand memcmp test to bcmp, timingsafe_{b,mem}cmp 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: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c6cc06d465a98270ef734ffec7c9012ec3a1bd17 Auto-Submitted: auto-generated The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=c6cc06d465a98270ef734ffec7c9012ec3a1bd17 commit c6cc06d465a98270ef734ffec7c9012ec3a1bd17 Author: Robert Clausecker AuthorDate: 2023-08-30 15:30:24 +0000 Commit: Robert Clausecker CommitDate: 2023-10-02 19:25:31 +0000 lib/libc/tests/string: expand memcmp test to bcmp, timingsafe_{b,mem}cmp The four functions more or less perform the same operation. Reuse the same unit test with slight changes so we can cover them all. Constant-time operation is not verified for the timingsafe_* functions. Sponsored by: The FreeBSD Foundation Approved by: ngie MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D41528 --- lib/libc/tests/string/Makefile | 3 + lib/libc/tests/string/bcmp_test.c | 32 ++++++++++ lib/libc/tests/string/memcmp_test.c | 82 ++++++++++++++++++-------- lib/libc/tests/string/timingsafe_bcmp_test.c | 32 ++++++++++ lib/libc/tests/string/timingsafe_memcmp_test.c | 32 ++++++++++ 5 files changed, 155 insertions(+), 26 deletions(-) diff --git a/lib/libc/tests/string/Makefile b/lib/libc/tests/string/Makefile index 1994b128ab57..e312787c8831 100644 --- a/lib/libc/tests/string/Makefile +++ b/lib/libc/tests/string/Makefile @@ -2,6 +2,7 @@ # ensure libc functions are tested, not clang's builtins CFLAGS+= -fno-builtin +ATF_TESTS_C+= bcmp_test ATF_TESTS_C+= ffs_test ATF_TESTS_C+= ffsl_test ATF_TESTS_C+= ffsll_test @@ -16,6 +17,8 @@ ATF_TESTS_C+= strerror2_test ATF_TESTS_C+= strspn_test ATF_TESTS_C+= strverscmp_test ATF_TESTS_C+= strxfrm_test +ATF_TESTS_C+= timingsafe_bcmp_test +ATF_TESTS_C+= timingsafe_memcmp_test ATF_TESTS_C+= wcscasecmp_test ATF_TESTS_C+= wcscoll_test ATF_TESTS_C+= wcsnlen_test diff --git a/lib/libc/tests/string/bcmp_test.c b/lib/libc/tests/string/bcmp_test.c new file mode 100644 index 000000000000..fdf5e48b3eb4 --- /dev/null +++ b/lib/libc/tests/string/bcmp_test.c @@ -0,0 +1,32 @@ +/*- + * Copyright (c) 2023 The FreeBSD Foundation + * + * This software was developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE + */ + +#define MEMCMP bcmp +#define RES(x) ((x) != 0) + +#include "memcmp_test.c" diff --git a/lib/libc/tests/string/memcmp_test.c b/lib/libc/tests/string/memcmp_test.c index f42bccfca529..487abe68f421 100644 --- a/lib/libc/tests/string/memcmp_test.c +++ b/lib/libc/tests/string/memcmp_test.c @@ -1,7 +1,11 @@ /*- * Copyright (c) 2016 Jilles Tjoelker + * Copyright (c) 2023 The FreeBSD Foundation * All rights reserved. * + * Portions of this software were developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -33,14 +37,40 @@ #include +#ifndef MEMCMP +#define MEMCMP memcmp +#endif + +/* + * On FreeBSD we demand that memcmp returns the difference between the + * characters at the first site of mismatch. However, ISO/IEC 9899:1990 + * only specifies that a number greater than, equal to, or less than + * zero shall be returned. If a unit test for this less strict + * behaviour is desired, define RES(x) to be (((x) > 0) - ((x) < 0)). + */ +#ifndef RES +#define RES(x) (x) +#endif + static int (*memcmp_fn)(const void *, const void *, size_t); +static void +check_memcmp(const char *a, const char *b, size_t len, int expected) +{ + int got; + + got = memcmp_fn(a, b, len); + ATF_CHECK_EQ_MSG(RES(expected), RES(got), + "%s(%p, %p, %zu) gave %d, but wanted %d", + __XSTRING(MEMCMP), a, b, len, got, expected); +} + ATF_TC_WITHOUT_HEAD(zero); ATF_TC_BODY(zero, tc) { - assert(memcmp_fn("a", "b", 0) == 0); - assert(memcmp_fn("", "", 0) == 0); + check_memcmp("a", "b", 0, 0); + check_memcmp("", "", 0, 0); } ATF_TC_WITHOUT_HEAD(eq); @@ -52,9 +82,9 @@ ATF_TC_BODY(eq, tc) for (i = 0; i < 256; i++) data1[i] = data2[i] = i ^ 0x55; for (i = 1; i < 256; i++) - assert(memcmp_fn(data1, data2, i) == 0); + check_memcmp(data1, data2, i, 0); for (i = 1; i < 256; i++) - assert(memcmp_fn(data1 + i, data2 + i, 256 - i) == 0); + check_memcmp(data1 + i, data2 + i, 256 - i, 0); } ATF_TC_WITHOUT_HEAD(neq); @@ -68,9 +98,9 @@ ATF_TC_BODY(neq, tc) data2[i] = i ^ 0x55; } for (i = 1; i < 256; i++) - assert(memcmp_fn(data1, data2, i) != 0); + check_memcmp(data1, data2, i, -0x55); for (i = 1; i < 256; i++) - assert(memcmp_fn(data1 + i, data2 + i, 256 - i) != 0); + check_memcmp(data1 + i, data2 + i, 256 - i, i - (i ^ 0x55)); } ATF_TC_WITHOUT_HEAD(diff); @@ -84,32 +114,32 @@ ATF_TC_BODY(diff, tc) data1[128] = 255; data2[128] = 0; for (i = 1; i < 66; i++) { - assert(memcmp_fn(data1 + 128, data2 + 128, i) == 255); - assert(memcmp_fn(data2 + 128, data1 + 128, i) == -255); - assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i) == 255); - assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i) == -255); - assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i * 2) == 255); - assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i * 2) == -255); + check_memcmp(data1 + 128, data2 + 128, i, 255); + check_memcmp(data2 + 128, data1 + 128, i, -255); + check_memcmp(data1 + 129 - i, data2 + 129 - i, i, 255); + check_memcmp(data2 + 129 - i, data1 + 129 - i, i, -255); + check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, 255); + check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, -255); } data1[128] = 'c'; data2[128] = 'e'; for (i = 1; i < 66; i++) { - assert(memcmp_fn(data1 + 128, data2 + 128, i) == -2); - assert(memcmp_fn(data2 + 128, data1 + 128, i) == 2); - assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i) == -2); - assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i) == 2); - assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i * 2) == -2); - assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i * 2) == 2); + check_memcmp(data1 + 128, data2 + 128, i, -2); + check_memcmp(data2 + 128, data1 + 128, i, 2); + check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2); + check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2); + check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2); + check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2); } memset(data1 + 129, 'A', sizeof(data1) - 129); memset(data2 + 129, 'Z', sizeof(data2) - 129); for (i = 1; i < 66; i++) { - assert(memcmp_fn(data1 + 128, data2 + 128, i) == -2); - assert(memcmp_fn(data2 + 128, data1 + 128, i) == 2); - assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i) == -2); - assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i) == 2); - assert(memcmp_fn(data1 + 129 - i, data2 + 129 - i, i * 2) == -2); - assert(memcmp_fn(data2 + 129 - i, data1 + 129 - i, i * 2) == 2); + check_memcmp(data1 + 128, data2 + 128, i, -2); + check_memcmp(data2 + 128, data1 + 128, i, 2); + check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2); + check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2); + check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2); + check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2); } } @@ -118,9 +148,9 @@ ATF_TP_ADD_TCS(tp) void *dl_handle; dl_handle = dlopen(NULL, RTLD_LAZY); - memcmp_fn = dlsym(dl_handle, "test_memcmp"); + memcmp_fn = dlsym(dl_handle, "test_" __XSTRING(MEMCMP)); if (memcmp_fn == NULL) - memcmp_fn = memcmp; + memcmp_fn = MEMCMP; ATF_TP_ADD_TC(tp, zero); ATF_TP_ADD_TC(tp, eq); diff --git a/lib/libc/tests/string/timingsafe_bcmp_test.c b/lib/libc/tests/string/timingsafe_bcmp_test.c new file mode 100644 index 000000000000..96bf789633f2 --- /dev/null +++ b/lib/libc/tests/string/timingsafe_bcmp_test.c @@ -0,0 +1,32 @@ +/*- + * Copyright (c) 2023 The FreeBSD Foundation + * + * This software was developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE + */ + +#define MEMCMP timingsafe_bcmp +#define RES(x) ((x) != 0) + +#include "memcmp_test.c" diff --git a/lib/libc/tests/string/timingsafe_memcmp_test.c b/lib/libc/tests/string/timingsafe_memcmp_test.c new file mode 100644 index 000000000000..5f97e41fcf8a --- /dev/null +++ b/lib/libc/tests/string/timingsafe_memcmp_test.c @@ -0,0 +1,32 @@ +/*- + * Copyright (c) 2023 The FreeBSD Foundation + * + * This software was developed by Robert Clausecker + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE + */ + +#define MEMCMP timingsafe_memcmp +#define RES(x) (((x) > 0) - ((x) < 0)) + +#include "memcmp_test.c"