From 15a79977126928ad5a761e94988014ee8f839f46 Mon Sep 17 00:00:00 2001 From: ngie Date: Tue, 18 Jul 2017 17:36:25 +0000 Subject: [PATCH] MFC r302500,r319339,r319543,r319544,r319551,r321138: r302500 (by cem): dd(1): Enable access to SIZE_T_MAX character devices On machines where SIZE_T_MAX exceeds OFF_MAX (signed 64-bit), permit seeking character devices to negative off_t values. This enables dd(1) to interact with kernel KVA in /dev/kmem on amd64, for example. r319339 (by asomers): Fix integer overflow detection in dd dd(1) tried to detect whether the seek offset would overflow, but it failed to account for the case where the provided argument was negative and the file was a regular file (negative seeks are allowed for character devices). I fixed it, and added a regression test. CID: 1368659 r319543: Stylistic tweaks Move opening braces of functions from the last column to column 0. MFC with: r319339 r319544: Mark :seek_overflow as an expected failure MFC with: r319339 PR: 219757 r319551 (by asomers): Fix bin/dd/dd2_tests:seek_overflow on UFS and TMPFS Split the postive and negative parts into separate test cases. The positive test case can only run on ZFS, because only ZFS supports files that large. PR: 219757 r321138: Remove unnecessary make logic added in r319339 This makes the change cleaner and easier to backport to ^/stable/10. git-svn-id: svn://svn.freebsd.org/base/stable/10@321140 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- bin/dd/args.c | 8 ----- bin/dd/position.c | 33 +++++++++++++++++-- bin/dd/tests/Makefile | 6 +--- bin/dd/tests/dd2_test.sh | 69 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 15 deletions(-) create mode 100755 bin/dd/tests/dd2_test.sh diff --git a/bin/dd/args.c b/bin/dd/args.c index 2f197f8da..c406c438c 100644 --- a/bin/dd/args.c +++ b/bin/dd/args.c @@ -165,14 +165,6 @@ jcl(char **argv) errx(1, "cbs meaningless if not doing record operations"); } else cfunc = def; - - /* - * Bail out if the calculation of a file offset would overflow. - */ - if (in.offset > OFF_MAX / (ssize_t)in.dbsz || - out.offset > OFF_MAX / (ssize_t)out.dbsz) - errx(1, "seek offsets cannot be larger than %jd", - (intmax_t)OFF_MAX); } static int diff --git a/bin/dd/position.c b/bin/dd/position.c index 57bfde592..505429553 100644 --- a/bin/dd/position.c +++ b/bin/dd/position.c @@ -45,12 +45,41 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include "dd.h" #include "extern.h" +static off_t +seek_offset(IO *io) +{ + off_t n; + size_t sz; + + n = io->offset; + sz = io->dbsz; + + _Static_assert(sizeof(io->offset) == sizeof(int64_t), "64-bit off_t"); + + /* + * If the lseek offset will be negative, verify that this is a special + * device file. Some such files (e.g. /dev/kmem) permit "negative" + * offsets. + * + * Bail out if the calculation of a file offset would overflow. + */ + if ((io->flags & ISCHR) == 0 && (n < 0 || n > OFF_MAX / (ssize_t)sz)) + errx(1, "seek offsets cannot be larger than %jd", + (intmax_t)OFF_MAX); + else if ((io->flags & ISCHR) != 0 && (uint64_t)n > UINT64_MAX / sz) + errx(1, "seek offsets cannot be larger than %ju", + (uintmax_t)UINT64_MAX); + + return ((off_t)( (uint64_t)n * sz )); +} + /* * Position input/output data streams before starting the copy. Device type * dependent. Seekable devices use lseek, and the rest position by reading. @@ -68,7 +97,7 @@ pos_in(void) /* If known to be seekable, try to seek on it. */ if (in.flags & ISSEEK) { errno = 0; - if (lseek(in.fd, in.offset * in.dbsz, SEEK_CUR) == -1 && + if (lseek(in.fd, seek_offset(&in), SEEK_CUR) == -1 && errno != 0) err(1, "%s", in.name); return; @@ -136,7 +165,7 @@ pos_out(void) */ if (out.flags & (ISSEEK | ISPIPE)) { errno = 0; - if (lseek(out.fd, out.offset * out.dbsz, SEEK_CUR) == -1 && + if (lseek(out.fd, seek_offset(&out), SEEK_CUR) == -1 && errno != 0) err(1, "%s", out.name); return; diff --git a/bin/dd/tests/Makefile b/bin/dd/tests/Makefile index f87af6524..029808e6e 100644 --- a/bin/dd/tests/Makefile +++ b/bin/dd/tests/Makefile @@ -1,10 +1,6 @@ # $FreeBSD$ -OBJTOP= ${.OBJDIR:H:H:H} -SRCTOP= ${.CURDIR:H:H:H} -TESTSRC= ${SRCTOP}/contrib/netbsd-tests/bin/dd -TESTSDIR= ${TESTSBASE}/bin/dd - +ATF_TESTS_SH= dd2_test NETBSD_ATF_TESTS_SH= dd_test .include diff --git a/bin/dd/tests/dd2_test.sh b/bin/dd/tests/dd2_test.sh new file mode 100755 index 000000000..c0b33fc17 --- /dev/null +++ b/bin/dd/tests/dd2_test.sh @@ -0,0 +1,69 @@ +# +# Copyright (c) 2017 Spectra Logic Corporation +# All rights reserved. +# +# 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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_test_case max_seek +max_seek_head() +{ + atf_set "descr" "dd(1) can seek by the maximum amount" +} +max_seek_body() +{ + case `df -T . | tail -n 1 | cut -wf 2` in + "ufs") + atf_skip "UFS's maximum file size is too small";; + "zfs") ;; # ZFS is fine + "tmpfs") + atf_skip "tmpfs can't create arbitrarily large spare files";; + *) atf_skip "Unknown file system";; + esac + + touch f.in + seek=`echo "2^63 / 4096 - 1" | bc` + atf_check -s exit:0 -e ignore dd if=f.in of=f.out bs=4096 seek=$seek +} + +atf_test_case seek_overflow +seek_overflow_head() +{ + atf_set "descr" "dd(1) should reject too-large seek values" +} +seek_overflow_body() +{ + touch f.in + seek=`echo "2^63 / 4096" | bc` + atf_check -s not-exit:0 -e match:"seek offsets cannot be larger than" \ + dd if=f.in of=f.out bs=4096 seek=$seek + atf_check -s not-exit:0 -e match:"seek offsets cannot be larger than" \ + dd if=f.in of=f.out bs=4096 seek=-1 +} + +atf_init_test_cases() +{ + atf_add_test_case max_seek + atf_add_test_case seek_overflow +} -- 2.45.0