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 @@ -1,43 +1,46 @@ # $FreeBSD$ .PATH: ${SRCTOP}/sys/geom/eli ${SRCTOP}/sys/crypto/sha2 PACKAGE= tests WARNS?= 3 TESTSDIR= ${TESTSBASE}/sys/geom/class/${.CURDIR:T} ATF_TESTS_C= pbkdf2_test ATF_TESTS_SH+= attach_test ATF_TESTS_SH+= configure_test ATF_TESTS_SH+= delkey_test ATF_TESTS_SH+= detach_test ATF_TESTS_SH+= init_test ATF_TESTS_SH+= integrity_test ATF_TESTS_SH+= kill_test ATF_TESTS_SH+= misc_test ATF_TESTS_SH+= onetime_test ATF_TESTS_SH+= online_resize_test ATF_TESTS_SH+= reentrancy_test ATF_TESTS_SH+= resize_test ATF_TESTS_SH+= setkey_test ${PACKAGE}FILES+= conf.sh CFLAGS.pbkdf2_test= -I${SRCTOP}/sys SRCS.pbkdf2_test= \ hmac_test.c \ g_eli_crypto.c \ g_eli_hmac.c \ pkcs5v2.c \ sha512c.c \ sha256c.c LIBADD.pbkdf2_test= crypto +PROGS+= unaligned_io +BINDIR?= ${TESTSDIR} + testvect.h: python gentestvect.py > ${.TARGET} .include diff --git a/tests/sys/geom/class/eli/misc_test.sh b/tests/sys/geom/class/eli/misc_test.sh index d38c8bcafeaf..0b852d1e24d2 100644 --- a/tests/sys/geom/class/eli/misc_test.sh +++ b/tests/sys/geom/class/eli/misc_test.sh @@ -1,173 +1,207 @@ # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018 Alan Somers # # 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. # # $FreeBSD$ . $(atf_get_srcdir)/conf.sh atf_test_case preserve_props cleanup preserve_props_head() { atf_set "descr" "geli should preserve basic GEOM properties" atf_set "require.user" "root" atf_set "timeout" 15 } preserve_props_body() { geli_test_setup md=$(attach_md -s1m) atf_check geli onetime /dev/${md} md_secsize=$(diskinfo ${md} | cut -wf 2) md_stripesize=$(diskinfo ${md} | cut -wf 5) eli_secsize=$(diskinfo ${md}.eli | cut -wf 2) eli_stripesize=$(diskinfo ${md}.eli | cut -wf 5) atf_check_equal "$md_secsize" "$eli_secsize" atf_check_equal "$md_stripesize" "$eli_stripesize" } preserve_props_cleanup() { geli_test_cleanup } atf_test_case preserve_disk_props cleanup preserve_disk_props_head() { atf_set "descr" "geli should preserve properties for disks" atf_set "require.user" "root" atf_set "require.config" "disks" atf_set "timeout" 15 } preserve_disk_props_body() { geli_test_setup disks=`atf_config_get disks` disk=${disks%% *} if [ -z "$disk" ]; then atf_skip "Must define disks (see tests(7))" fi atf_check geli onetime ${disk} disk_ident=$(diskinfo -s ${disk}) disk_descr=$(diskinfo -v ${disk} | awk '/Disk descr/ {print $1}') disk_rotrate=$(diskinfo -v ${disk} | awk '/Rotation rate/ {print $1}') disk_zonemode=$(diskinfo -v ${disk} | awk '/Zone Mode/ {print $1}') eli_ident=$(diskinfo -s ${disk}.eli) eli_descr=$(diskinfo -v ${disk}.eli | awk '/Disk descr/ {print $1}') eli_rotrate=$(diskinfo -v ${disk}.eli | awk '/Rotation/ {print $1}') eli_zonemode=$(diskinfo -v ${disk}.eli | awk '/Zone Mode/ {print $1}') atf_check_equal "$disk_ident" "$eli_ident" atf_check_equal "$disk_descr" "$eli_descr" atf_check_equal "$disk_rotrate" "$eli_rotrate" atf_check_equal "$disk_zonemode" "$eli_zonemode" } preserve_disk_props_cleanup() { disk_cleanup geli_test_cleanup } atf_test_case physpath cleanup physpath_head() { atf_set "descr" "geli should append /eli to the underlying device's physical path" atf_set "require.user" "root" atf_set "timeout" 15 } physpath_body() { geli_test_setup if ! error_message=$(geom_load_class_if_needed nop); then atf_skip "$error_message" fi md=$(attach_md -s1m) # If the underlying device has no physical path, then geli should not # create one. atf_check -o empty -e ignore diskinfo -p $md atf_check -s exit:0 geli onetime $md atf_check -o empty -e ignore diskinfo -p $md.eli atf_check -s exit:0 geli kill $md # If the underlying device does have a physical path, then geli should # append "/eli" physpath="some/physical/path" atf_check gnop create -z $physpath ${md} atf_check -s exit:0 geli onetime $md.nop atf_check -o match:"^${physpath}/eli$" diskinfo -p $md.nop.eli } physpath_cleanup() { if [ -f "$TEST_MDS_FILE" ]; then while read md; do [ -c /dev/${md}.nop.eli ] && \ geli detach $md.nop.eli 2>/dev/null [ -c /dev/${md}.nop ] && \ gnop destroy -f $md.nop 2>/dev/null [ -c /dev/${md}.eli ] && \ geli detach $md.eli 2>/dev/null mdconfig -d -u $md 2>/dev/null done < $TEST_MDS_FILE fi 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 mdconfig -d -u $test_md 2>/dev/null done < $MD_DEVS rm $MD_DEVS fi if [ -f "$PLAINFILES" ]; then while read f; do rm -f ${f} done < ${PLAINFILES} rm ${PLAINFILES} fi true } disk_cleanup() { disks=`atf_config_get disks` disk=${disks%% *} if [ -n "$disk" ]; then geli kill ${disk} 2>/dev/null fi } 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); +}