From nobody Mon Jun 19 13:08:40 2023 X-Original-To: dev-commits-src-branches@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 4Ql97X46xKz4g5g2; Mon, 19 Jun 2023 13:08:40 +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 4Ql97X3cpcz3rBm; Mon, 19 Jun 2023 13:08:40 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1687180120; 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=thyBsR6+t03fx49mAtXgKBdkdHt2A4sD0pfscojiAEk=; b=k5ko8J2pSjagZsFONfW69+7sTJ7ZtfsPJyXOt1K8eUXAlTRS9V3pf7KnzmxiPkTO+nR+VO KNjq30lIDrPM31Wqo/TlGS4Gq90OrpZXmDgaTS0/rRx71cTKloMeo9hcE7/6YFOTQ+dMC2 rcpWWSwVt3pFONahUKR59H6aw4XRZAtN3wA4sVXLk9lK0m+uNZbrtzBqaQHL8/IHJ+bvAu 8+Nnzo40hVCn78qh1uccgon9x1DxhOdQ9Myk84E/J7ctn23VijbWbv3P60sH78yKGUAAju 4yGWnX6c+p91Z3nnWfCRlSZR1CQXQCQbhMBtQiH3xQJuC1P7uqXWTPWqyzRP8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1687180120; 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=thyBsR6+t03fx49mAtXgKBdkdHt2A4sD0pfscojiAEk=; b=liTjp1ZmlWO+qhWR+lqeUr0asc+qV35fo7w0EedRsIbuitDAW9kkDZiFppwoMMs97C9m10 HgTXoW5HKnNOVPGnYdipyRwr8BnYgHJFR1A7MFIo2nIi7bBDN97i06ITsnYw7an8UYsuVu kMJWeRwSaC7ICXLuOyrJ1o0uxtdizZv1NFyCw5mwjNWgHUzxW0pm+o5FQtf2mp6+eSZZRa kvcS/5FKh9RJbMdjnTZjgqg6LpxBh+SOhagYK324fH7lgXBe5NFVqtCiIP2oGBj647gMSS Ub7Ogm43N4QVAIM75xv1DIcTPG9Is/YC7HV6i3eHJ3ZNQsjyqI98MqPOvM7aGg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1687180120; a=rsa-sha256; cv=none; b=cbeyKXzXYcLwlPydvsRC7fweQ1JmAZtyuImOpI2zadtM5R0zVoJxMEJJmCgIHnXSV+2z3W DwLYBmCqVryEuqDgePZP8xNRqu8gKQZtiTwazL31zBB778HFdlSGy5iiYGkgf5LWm6oOie pxJ4dkVvlyBO3qjjKmI1fWEel36ozULYAiuFJzQIz7hIqtbZqjo5nx5qIzd+Y9bVGD2wCZ MxmAsizp9pW8SEy06HvAFTn2gfuG/gqcm6vijzOmJbfcxEdO0VRYo706mXzZ5lNgND3zxQ WQTAFWiuc4B0PwqThZxy+V9QJjsZbrb7nQLeJ8Edeb4BBKML9tYeBsWx7fgB3Q== 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 4Ql97X2bSNzHxw; Mon, 19 Jun 2023 13:08:40 +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 35JD8eDH035554; Mon, 19 Jun 2023 13:08:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35JD8eSs035553; Mon, 19 Jun 2023 13:08:40 GMT (envelope-from git) Date: Mon, 19 Jun 2023 13:08:40 GMT Message-Id: <202306191308.35JD8eSs035553@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: c4aae5668c69 - stable/13 - geli tests: Add a regression test for PR 271766 List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: c4aae5668c69df4205ab3df382056a71a0e23bb7 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=c4aae5668c69df4205ab3df382056a71a0e23bb7 commit c4aae5668c69df4205ab3df382056a71a0e23bb7 Author: Mark Johnston AuthorDate: 2023-06-12 16:11:20 +0000 Commit: Mark Johnston CommitDate: 2023-06-19 12:57:17 +0000 geli tests: Add a regression test for PR 271766 This test case catches both of the bugs reported there. PR: 271766 Reviewed by: imp MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D40469 (cherry picked from commit 9c0467929abaab97f45fc07507b6f30c80211239) --- tests/sys/geom/class/eli/Makefile | 3 + tests/sys/geom/class/eli/misc_test.sh | 38 ++++++++- tests/sys/geom/class/eli/unaligned_io.c | 131 ++++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 2 deletions(-) diff --git a/tests/sys/geom/class/eli/Makefile b/tests/sys/geom/class/eli/Makefile index c7f1342f45ae..bdb1101ef059 100644 --- a/tests/sys/geom/class/eli/Makefile +++ b/tests/sys/geom/class/eli/Makefile @@ -37,6 +37,9 @@ SRCS.pbkdf2_test= \ LIBADD.pbkdf2_test= crypto +PROGS+= unaligned_io +BINDIR?= ${TESTSDIR} + testvect.h: python gentestvect.py > ${.TARGET} diff --git a/tests/sys/geom/class/eli/misc_test.sh b/tests/sys/geom/class/eli/misc_test.sh index 4dc7cb9bf367..420f47f786e6 100644 --- a/tests/sys/geom/class/eli/misc_test.sh +++ b/tests/sys/geom/class/eli/misc_test.sh @@ -135,17 +135,51 @@ physpath_cleanup() true } +unaligned_io_test() +{ + cipher=$1 + secsize=$2 + ealgo=${cipher%%:*} + keylen=${cipher##*:} + + atf_check -s exit:0 -e ignore \ + geli init -B none -e $ealgo -l $keylen -P -K keyfile \ + -s $secsize ${md} + atf_check geli attach -p -k keyfile ${md} + + atf_check $(atf_get_srcdir)/unaligned_io /dev/${md}.eli +} + +atf_test_case unaligned_io cleanup +unaligned_io_head() +{ + atf_set "descr" "regression test for PR 271766" + atf_set "require.user" "root" +} +unaligned_io_body() +{ + geli_test_setup + + sectors=4 + + atf_check dd if=/dev/random of=keyfile bs=512 count=16 status=none + for_each_geli_config_nointegrity unaligned_io_test +} +unaligned_io_cleanup() +{ + geli_test_cleanup +} + atf_init_test_cases() { atf_add_test_case physpath atf_add_test_case preserve_props atf_add_test_case preserve_disk_props + atf_add_test_case unaligned_io } - common_cleanup() { - if [ -f "$MD_DEVS" ]; then while read test_md; do gnop destroy -f ${test_md}.nop 2>/dev/null diff --git a/tests/sys/geom/class/eli/unaligned_io.c b/tests/sys/geom/class/eli/unaligned_io.c new file mode 100644 index 000000000000..707d15f40be4 --- /dev/null +++ b/tests/sys/geom/class/eli/unaligned_io.c @@ -0,0 +1,131 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2023 The FreeBSD Foundation + * + * This software was developed by Mark Johnston 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. + */ + +/* + * Idea from a test case by Andrew "RhodiumToad" Gierth in Bugzilla PR 271766. + */ + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +int +main(int argc, char **argv) +{ + const char *disk; + char *buf1, *buf2; + off_t disksz; + size_t bufsz, iosz; + ssize_t n; + unsigned int offsets, secsz; + int fd; + + if (argc != 2) + errx(1, "Usage: %s ", argv[0]); + disk = argv[1]; + + fd = open(disk, O_RDWR); + if (fd < 0) + err(1, "open(%s)", disk); + + if (ioctl(fd, DIOCGSECTORSIZE, &secsz) != 0) + err(1, "ioctl(DIOCGSECTORSIZE)"); + if (secsz == 0) + errx(1, "ioctl(DIOCGSECTORSIZE) returned 0"); + if (ioctl(fd, DIOCGMEDIASIZE, &disksz) != 0) + err(1, "ioctl(DIOCGMEDIASIZE)"); + if (disksz / secsz < 2) + errx(1, "disk needs to be at least 2 sectors in size"); + iosz = 2 * secsz; + + bufsz = iosz + secsz; + buf1 = mmap(NULL, bufsz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0); + if (buf1 == MAP_FAILED) + err(1, "mmap"); + buf2 = mmap(NULL, bufsz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, + -1, 0); + if (buf2 == MAP_FAILED) + err(1, "mmap"); + + arc4random_buf(buf1, bufsz); + n = pwrite(fd, buf1, bufsz, 0); + if (n < 0 || (size_t)n != bufsz) + err(1, "pwrite"); + + /* + * Limit the number of offsets we test with, to avoid spending too much + * time when the sector size is large. + */ + offsets = MAX(EALG_MAX_BLOCK_LEN, HMAC_MAX_BLOCK_LEN) + 1; + + /* + * Read test: read the first 2 sectors into buf1, then do the same with + * buf2, except at varying offsets into buf2. After each read, compare + * the buffers and make sure they're identical. This exercises corner + * cases in the crypto layer's buffer handling. + */ + n = pread(fd, buf1, iosz, 0); + if (n < 0 || (size_t)n != iosz) + err(1, "pread"); + for (unsigned int i = 0; i < offsets; i++) { + n = pread(fd, buf2 + i, iosz, 0); + if (n < 0 || (size_t)n != iosz) + err(1, "pread"); + if (memcmp(buf1, buf2 + i, iosz) != 0) + errx(1, "read mismatch at offset %u/%u", i, secsz); + } + + /* + * Write test. Try writing buffers at various alignments, and verify + * that we read back what we wrote. + */ + arc4random_buf(buf1, bufsz); + for (unsigned int i = 0; i < offsets; i++) { + n = pwrite(fd, buf1 + i, iosz, 0); + if (n < 0 || (size_t)n != iosz) + err(1, "pwrite"); + n = pread(fd, buf2, iosz, 0); + if (n < 0 || (size_t)n != iosz) + err(1, "pread"); + if (memcmp(buf1 + i, buf2, iosz) != 0) + errx(1, "write mismatch at offset %u/%u", i, secsz); + } + + return (0); +}