From patchwork Thu Jan 5 01:17:45 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 9498209 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 BC85B606A9 for ; Thu, 5 Jan 2017 01:17:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B63D628339 for ; Thu, 5 Jan 2017 01:17:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AB157283A6; Thu, 5 Jan 2017 01:17:52 +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=-6.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY 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 1775B28339 for ; Thu, 5 Jan 2017 01:17:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935749AbdAEBRu (ORCPT ); Wed, 4 Jan 2017 20:17:50 -0500 Received: from aserp1040.oracle.com ([141.146.126.69]:45722 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933960AbdAEBRt (ORCPT ); Wed, 4 Jan 2017 20:17:49 -0500 Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v051HkO8028714 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 5 Jan 2017 01:17:47 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id v051HkZB005179 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 5 Jan 2017 01:17:46 GMT Received: from abhmp0007.oracle.com (abhmp0007.oracle.com [141.146.116.13]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v051Hkfd011338; Thu, 5 Jan 2017 01:17:46 GMT Received: from localhost (/24.21.211.40) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 04 Jan 2017 17:17:45 -0800 Subject: [PATCH 7/9] populate: discover XFS structure fields and fuzz verbs, and use them to fuzz fields From: "Darrick J. Wong" To: eguan@redhat.com, darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org, fstests@vger.kernel.org Date: Wed, 04 Jan 2017 17:17:45 -0800 Message-ID: <148357906498.10436.15608215429466271747.stgit@birch.djwong.org> In-Reply-To: <148357902056.10436.12795484251467155460.stgit@birch.djwong.org> References: <148357902056.10436.12795484251467155460.stgit@birch.djwong.org> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Source-IP: userv0022.oracle.com [156.151.31.74] Sender: fstests-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Create some routines to help us perform targeted fuzzing of individual fields in various XFS structures. Specifically, we want the caller to drop the xfs_db iocursor on the victim field; from there, the scripts should discover all available fields and fuzzing verbs, and try each fuzz verb on every available field. Signed-off-by: Darrick J. Wong --- v2: fix some errors found by Eryu Guan. --- common/fuzzy | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe fstests" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/common/fuzzy b/common/fuzzy index 1cc1d5e..2edcd9f 100644 --- a/common/fuzzy +++ b/common/fuzzy @@ -81,3 +81,192 @@ _scratch_scrub() { ;; esac } + +# Filter the xfs_db print command's field debug information +# into field name and type. +__filter_xfs_db_print_fields() { + grep ' = ' | while read key equals value; do + fuzzkey="$(echo "${key}" | sed -e 's/\([a-zA-Z0-9_]*\)\[\([0-9]*\)-[0-9]*\]/\1[\2]/g')" + if [[ "${value}" == "["* ]]; then + echo "${value}" | sed -e 's/^.//g' -e 's/.$//g' -e 's/,/\n/g' | while read subfield; do + echo "${fuzzkey}.${subfield}" + done + else + echo "${fuzzkey}" + fi + done +} + +# Navigate to some part of the filesystem and print the field info. +_scratch_xfs_list_metadata_fields() { + if [ -n "${SCRATCH_XFS_LIST_METADATA_FIELDS}" ]; then + echo "${SCRATCH_XFS_LIST_METADATA_FIELDS}" | sed -e 's/ /\n/g' + return; + fi + + (for arg in "$@"; do + echo "${arg}" + done + echo "print") | _scratch_xfs_db | __filter_xfs_db_print_fields +} + +# Get a metadata field +_scratch_xfs_get_metadata_field() { + key="$1" + shift + + grep_key="$(echo "${key}" | tr '[]()' '....')" + (for arg in "$@"; do + echo "${arg}" + done + echo "print ${key}") | _scratch_xfs_db | grep "^${grep_key}" | \ + sed -e 's/^.* = //g' +} + +# Set a metadata field +_scratch_xfs_set_metadata_field() { + key="$1" + value="$2" + shift; shift + (for arg in "$@"; do + echo "${arg}" + done + echo "write -d ${key} ${value}") | _scratch_xfs_db -x + echo +} + +# Fuzz a metadata field +_scratch_xfs_fuzz_metadata_field() { + key="$1" + value="$2" + shift; shift + + if [ "${key}" = "crc" ]; then + fuzz_arg="-c" + else + fuzz_arg="-d" + fi + oldval="$(_scratch_xfs_get_metadata_field "${key}" "$@")" + (for arg in "$@"; do + echo "${arg}" + done + echo "fuzz ${fuzz_arg} ${key} ${value}") | _scratch_xfs_db -x + echo + newval="$(_scratch_xfs_get_metadata_field "${key}" "$@" 2> /dev/null)" + if [ "${oldval}" = "${newval}" ]; then + echo "Field ${key} already set to ${oldval}, skipping test." + return 1 + fi + return 0 +} + +# Try to forcibly unmount the scratch fs +__scratch_xfs_fuzz_unmount() +{ + while ! _scratch_unmount 2>/dev/null; do sleep 0.2; done +} + +# Restore metadata to scratch device prior to field-fuzzing. +__scratch_xfs_fuzz_mdrestore() +{ + test -e "${POPULATE_METADUMP}" || _fail "Need to set POPULATE_METADUMP" + + __scratch_xfs_fuzz_unmount + xfs_mdrestore "${POPULATE_METADUMP}" "${SCRATCH_DEV}" +} + +__fuzz_notify() { + echo "$@" + test -w /dev/ttyprintk && echo "$@" >> /dev/ttyprintk +} + +# Fuzz one field of some piece of metadata +__scratch_xfs_fuzz_field_test() { + field="$1" + fuzzverb="$2" + shift; shift + + # Set the new field value + __fuzz_notify "+ Fuzz ${field} = ${fuzzverb}" + echo "========================" + _scratch_xfs_fuzz_metadata_field "${field}" ${fuzzverb} "$@" + res=$? + test $res -ne 0 && return + + # Try to catch the error with scrub + echo "+ Try to catch the error" + _scratch_mount 2>&1 + res=$? + if [ $res -eq 0 ]; then + _scratch_scrub -a 1 -e continue 2>&1 + res=$? + test $res -eq 0 && \ + (>&2 echo "scrub didn't fail with ${field} = ${fuzzverb}.") + + # Try modifying the filesystem! + __fuzz_notify "++ Try to write filesystem" + #_scratch_fuzz_modify 100 2>&1 + __scratch_xfs_fuzz_unmount + fi + + # Repair the filesystem + echo "+ Fix the error" + _repair_scratch_fs 2>&1 + res=$? + test $res -ne 0 && \ + (>&2 echo "repair failed ($res) with ${field} = ${fuzzverb}.") + + # See if scrub finds a clean fs + echo "+ Make sure error is gone" + _scratch_mount 2>&1 + res=$? + if [ $res -eq 0 ]; then + _scratch_scrub -e continue 2>&1 + res=$? + test $res -ne 0 && \ + (>&2 echo "re-scrub ($res) with ${field} = ${fuzzverb}.") + + # Try modifying the filesystem again! + __fuzz_notify "++ Try to write filesystem again" + _scratch_fuzz_modify 100 2>&1 + __scratch_xfs_fuzz_unmount + else + (>&2 echo "re-mount failed ($res) with ${field} = ${fuzzverb}.") + fi + + # See if repair finds a clean fs + _scratch_xfs_repair -n 2>&1 + res=$? + test $res -ne 0 && \ + (>&2 echo "re-repair failed ($res) with ${field} = ${fuzzverb}.") +} + +# Make sure we have all the pieces we need for field fuzzing +_require_scratch_xfs_fuzz_fields() +{ + _require_scratch + _require_scrub + _require_populate_commands + _scratch_mkfs_xfs >/dev/null 2>&1 + _require_xfs_db_command "fuzz" +} + +# Grab the list of available fuzzing verbs +_scratch_xfs_list_fuzz_verbs() { + if [ -n "${SCRATCH_XFS_LIST_FUZZ_VERBS}" ]; then + echo "${SCRATCH_XFS_LIST_FUZZ_VERBS}" | sed -e 's/ /\n/g' + return; + fi + _scratch_xfs_db -x -c 'sb 0' -c 'fuzz' | grep '^Verbs:' | \ + sed -e 's/[,.]//g' -e 's/Verbs: //g' -e 's/ /\n/g' +} + +# Fuzz the fields of some piece of metadata +_scratch_xfs_fuzz_fields() { + _scratch_xfs_list_metadata_fields "$@" | while read field; do + _scratch_xfs_list_fuzz_verbs | while read fuzzverb; do + __scratch_xfs_fuzz_mdrestore + __scratch_xfs_fuzz_field_test "${field}" "${fuzzverb}" "$@" + done + done +}