From patchwork Wed May 16 20:38:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10404815 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 50F53601D2 for ; Wed, 16 May 2018 20:39:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3FDA326E96 for ; Wed, 16 May 2018 20:39:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 348FA2870C; Wed, 16 May 2018 20:39:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 99FD726E96 for ; Wed, 16 May 2018 20:39:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751430AbeEPUjE (ORCPT ); Wed, 16 May 2018 16:39:04 -0400 Received: from mail-pl0-f54.google.com ([209.85.160.54]:37573 "EHLO mail-pl0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751198AbeEPUjB (ORCPT ); Wed, 16 May 2018 16:39:01 -0400 Received: by mail-pl0-f54.google.com with SMTP id w19-v6so1101360plq.4 for ; Wed, 16 May 2018 13:39:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=a01c9JNcsG2GuDdVaX9rYkv2N5LbD2FX5NxKJzo/NuM=; b=kdnHT6QeFJk6+pn88RaOhat0kDdZFkcy9s8TQeXTnmltq3vepo0uqfdixQL26gbI2v KaBln8FHw5uDvWqNXM1ggZSPLia3tFEDsRkYwvpfruZM+jxtGXfjImwgECOENK1e34dr E1/oVfql976rDwRZ/2IVS79nKaW8jJX6JEXjTDer5+qo34+Ub4T03sXeaJ0OrogRkYol Be9vDuKE6fJxPwuB8lVHLOIzvmMoMe1HqdR9qPWIbGfo8S1hvoT/gDmPhL/bErNJ6cJK gh6o9H0T/XRF7NrvuMIND5OwtWnIYhc+id3HuCyN7AkXJ8+2qeAVxoItz14qHO1PdZAw XHOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=a01c9JNcsG2GuDdVaX9rYkv2N5LbD2FX5NxKJzo/NuM=; b=KU+Pbrg/jwKkSqa39SlF8WcfzFSWMropGgHg9ggLG9UMU2u5s41k2oLhu31x3HENJZ UH6jBCwzmENu3kralT0RXOtg1xI5Tdp2dW67WYqh7k0wB5/NNBjl88mZYEzyQ8u/FUSf UrbzPu0v2T4TSEZcqXrQUK+NVGikxv5Oid5bOAkV6l3I6hd8L0ytSBitgZEy6ZpXsbxH IFlcNNH9kmTRDr1tl6DfGYbbXvO3kGJIALR/Eo7dxYzF8ckAgB/vpS3NCjWWbgqOdy84 2jSFwUEj6JL7ki6fSOiWnTlMn6REwymHdlsYLJWl2BcgAhsAWDHiKV3qmvW8Il2I6/0Y FSmw== X-Gm-Message-State: ALKqPwfWK0lMkv6c4QqUPPWj/CyJ/MIRK6AJsMqG9yVvOWI2QIsZJp47 ++22hk50IdLPfVm9CYt4H1KDNw== X-Google-Smtp-Source: AB8JxZp2Vdpzw9YGiZ/LdFmghqpVXVi+jwHwehCMaALm4TLiLz16Lgx29KKjAXBZ9duiidKhyjug5g== X-Received: by 2002:a17:902:c5:: with SMTP id a63-v6mr2392408pla.149.1526503140673; Wed, 16 May 2018 13:39:00 -0700 (PDT) Received: from vader.hsd1.wa.comcast.net ([2601:602:8800:a9a9:e6a7:a0ff:fe0b:c9a8]) by smtp.gmail.com with ESMTPSA id k8-v6sm3858768pga.94.2018.05.16.13.38.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 May 2018 13:38:59 -0700 (PDT) From: Omar Sandoval To: fstests@vger.kernel.org Cc: "Darrick J . Wong" , Eryu Guan , linux-btrfs@vger.kernel.org, linux-xfs@vger.kernel.org, kernel-team@fb.com Subject: [PATCH v2 5/5] generic: test invalid swap file activation Date: Wed, 16 May 2018 13:38:49 -0700 Message-Id: X-Mailer: git-send-email 2.17.0 In-Reply-To: References: In-Reply-To: References: Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval Swap files cannot have holes, and they must at least two pages. swapon(8) and mkswap(8) have stricter restrictions, so add versions of those commands without any restrictions. Signed-off-by: Omar Sandoval --- .gitignore | 2 ++ src/Makefile | 2 +- src/mkswap.c | 83 +++++++++++++++++++++++++++++++++++++++++++ src/swapon.c | 24 +++++++++++++ tests/generic/490 | 77 +++++++++++++++++++++++++++++++++++++++ tests/generic/490.out | 5 +++ tests/generic/group | 1 + 7 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 src/mkswap.c create mode 100644 src/swapon.c create mode 100755 tests/generic/490 create mode 100644 tests/generic/490.out diff --git a/.gitignore b/.gitignore index 53029e24..efc73a7c 100644 --- a/.gitignore +++ b/.gitignore @@ -92,6 +92,7 @@ /src/lstat64 /src/makeextents /src/metaperf +/src/mkswap /src/mmapcat /src/multi_open_unlink /src/nametest @@ -111,6 +112,7 @@ /src/seek_sanity_test /src/stale_handle /src/stat_test +/src/swapon /src/t_access_root /src/t_dir_offset /src/t_dir_offset2 diff --git a/src/Makefile b/src/Makefile index c42d3bb1..01fe99ef 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,7 +26,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ renameat2 t_getcwd e4compact test-nextquota punch-alternating \ attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \ dio-invalidate-cache stat_test t_encrypted_d_revalidate \ - attr_replace_test + attr_replace_test swapon mkswap SUBDIRS = log-writes perf diff --git a/src/mkswap.c b/src/mkswap.c new file mode 100644 index 00000000..d0bce2bd --- /dev/null +++ b/src/mkswap.c @@ -0,0 +1,83 @@ +/* mkswap(8) without any sanity checks */ + +#include +#include +#include +#include +#include +#include +#include + +struct swap_header { + char bootbits[1024]; + uint32_t version; + uint32_t last_page; + uint32_t nr_badpages; + unsigned char sws_uuid[16]; + unsigned char sws_volume[16]; + uint32_t padding[117]; + uint32_t badpages[1]; +}; + +int main(int argc, char **argv) +{ + struct swap_header *hdr; + FILE *file; + struct stat st; + long page_size; + int ret; + + if (argc != 2) { + fprintf(stderr, "usage: %s PATH\n", argv[0]); + return EXIT_FAILURE; + } + + page_size = sysconf(_SC_PAGESIZE); + if (page_size == -1) { + perror("sysconf"); + return EXIT_FAILURE; + } + + hdr = calloc(1, page_size); + if (!hdr) { + perror("calloc"); + return EXIT_FAILURE; + } + + file = fopen(argv[1], "r+"); + if (!file) { + perror("fopen"); + free(hdr); + return EXIT_FAILURE; + } + + ret = fstat(fileno(file), &st); + if (ret) { + perror("fstat"); + free(hdr); + fclose(file); + return EXIT_FAILURE; + } + + hdr->version = 1; + hdr->last_page = st.st_size / page_size - 1; + memset(&hdr->sws_uuid, 0x99, sizeof(hdr->sws_uuid)); + memcpy((char *)hdr + page_size - 10, "SWAPSPACE2", 10); + + if (fwrite(hdr, page_size, 1, file) != 1) { + perror("fwrite"); + free(hdr); + fclose(file); + return EXIT_FAILURE; + } + + if (fclose(file) == EOF) { + perror("fwrite"); + free(hdr); + return EXIT_FAILURE; + } + + free(hdr); + + return EXIT_SUCCESS; +} diff --git a/src/swapon.c b/src/swapon.c new file mode 100644 index 00000000..0cb7108a --- /dev/null +++ b/src/swapon.c @@ -0,0 +1,24 @@ +/* swapon(8) without any sanity checks; simply calls swapon(2) directly. */ + +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int ret; + + if (argc != 2) { + fprintf(stderr, "usage: %s PATH\n", argv[0]); + return EXIT_FAILURE; + } + + ret = swapon(argv[1], 0); + if (ret) { + perror("swapon"); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/tests/generic/490 b/tests/generic/490 new file mode 100755 index 00000000..6ba2ecb3 --- /dev/null +++ b/tests/generic/490 @@ -0,0 +1,77 @@ +#! /bin/bash +# FS QA Test 490 +# +# Test invalid swap files. +# +#----------------------------------------------------------------------- +# Copyright (c) 2018 Facebook. All Rights Reserved. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it would be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write the Free Software Foundation, +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +#----------------------------------------------------------------------- +# + +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter + +# remove previous $seqres.full before test +rm -f $seqres.full + +_supported_fs generic +_supported_os Linux +_require_scratch_swapfile +_require_test_program mkswap +_require_test_program swapon + +_scratch_mkfs >> $seqres.full 2>&1 +_scratch_mount + +echo "File with holes" +# We can't use _format_swapfile because we're using our custom mkswap and +# swapon. +touch "$SCRATCH_MNT/swap" +$CHATTR_PROG +C "$SCRATCH_MNT/swap" +chmod 0600 "$SCRATCH_MNT/swap" +$XFS_IO_PROG -c "truncate $(($(get_page_size) * 10))" "$SCRATCH_MNT/swap" +"$here/src/mkswap" "$SCRATCH_MNT/swap" +"$here/src/swapon" "$SCRATCH_MNT/swap" +swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1 + +echo "Empty swap file (only swap header)" +rm -f "$SCRATCH_MNT/swap" +touch "$SCRATCH_MNT/swap" +$CHATTR_PROG +C "$SCRATCH_MNT/swap" +chmod 0600 "$SCRATCH_MNT/swap" +_pwrite_byte 0x61 0 $(get_page_size) "$SCRATCH_MNT/swap" >> $seqres.full +"$here/src/mkswap" "$SCRATCH_MNT/swap" +"$here/src/swapon" "$SCRATCH_MNT/swap" +swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1 + +status=0 +exit diff --git a/tests/generic/490.out b/tests/generic/490.out new file mode 100644 index 00000000..96226c64 --- /dev/null +++ b/tests/generic/490.out @@ -0,0 +1,5 @@ +QA output created by 490 +File with holes +swapon: Invalid argument +Empty swap file (only swap header) +swapon: Invalid argument diff --git a/tests/generic/group b/tests/generic/group index a5252aeb..422e21e2 100644 --- a/tests/generic/group +++ b/tests/generic/group @@ -492,3 +492,4 @@ 487 auto quick 488 auto quick swap 489 auto quick swap +490 auto quick swap