linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eryu Guan <eguan@redhat.com>
To: "Darrick J. Wong" <darrick.wong@oracle.com>
Cc: david@fromorbit.com, linux-xfs@vger.kernel.org, fstests@vger.kernel.org
Subject: Re: [PATCH 7/9] populate: discover XFS structure fields and fuzz verbs, and use them to fuzz fields
Date: Wed, 9 Nov 2016 11:32:02 +0800	[thread overview]
Message-ID: <20161109033202.GP27776@eguan.usersys.redhat.com> (raw)
In-Reply-To: <147830507411.1919.17534443838350465519.stgit@birch.djwong.org>

On Fri, Nov 04, 2016 at 05:17:54PM -0700, Darrick J. Wong wrote:
> 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 <darrick.wong@oracle.com>
> ---
>  common/fuzzy |  191 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 191 insertions(+)
> 
> 
> diff --git a/common/fuzzy b/common/fuzzy
> index 20f1d29..6af47f1 100644
> --- a/common/fuzzy
> +++ b/common/fuzzy
> @@ -81,3 +81,194 @@ _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

Shouldn't this be

while ! _scratch_unmount 2>/dev/null; do sleep 0.2; done

so it only sleeps and try again if umount failed?

Thanks,
Eryu

> +}
> +
> +# 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
> +	_require_command "$XFS_DB_PROG" "xfs_db"
> +	_scratch_mkfs_xfs >/dev/null 2>&1
> +	_scratch_xfs_db -x -c 'fuzz' 2>&1 | grep -q 'not found' && \
> +		_notrun "xfs_db does not have fuzz command"
> +}
> +
> +# 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
> +}
> 

  reply	other threads:[~2016-11-09  3:32 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-05  0:17 [PATCH v2 0/9] xfstests: online scrub/repair support Darrick J. Wong
2016-11-05  0:17 ` [PATCH 1/9] populate: create all types of XFS metadata Darrick J. Wong
2016-11-07 15:34   ` Eryu Guan
2016-11-09  0:29     ` Darrick J. Wong
2016-11-05  0:17 ` [PATCH 2/9] populate: add _require_populate_commands to check for tools Darrick J. Wong
2016-11-05  0:17 ` [PATCH 3/9] populate: optionally fill the filesystem when populating fs Darrick J. Wong
2016-11-05  0:17 ` [PATCH 4/9] populate: fix some silly errors when modifying a fs while fuzzing Darrick J. Wong
2016-11-05  0:17 ` [PATCH 5/9] common/fuzzy: move fuzzing helper functions here Darrick J. Wong
2016-11-05  0:17 ` [PATCH 6/9] populate: cache scratch metadata images Darrick J. Wong
2016-11-05  0:17 ` [PATCH 7/9] populate: discover XFS structure fields and fuzz verbs, and use them to fuzz fields Darrick J. Wong
2016-11-09  3:32   ` Eryu Guan [this message]
2016-11-09  7:52     ` Darrick J. Wong
2016-11-09  3:44   ` Eryu Guan
2016-11-09  7:53     ` Darrick J. Wong
2016-11-05  0:18 ` [PATCH 8/9] xfs: fuzz every field of every structure Darrick J. Wong
2016-11-09  8:09   ` Eryu Guan
2016-11-09  8:52     ` Darrick J. Wong
2016-11-09  9:13       ` Eryu Guan
2016-11-09  9:25         ` Darrick J. Wong
2016-11-09 10:04           ` Eryu Guan
2016-11-05  0:18 ` [PATCH 9/9] common/populate: create attrs in different namespaces Darrick J. Wong
  -- strict thread matches above, loose matches on Subject: below --
2017-01-05  1:17 [PATCH v4 0/9] xfstests: online scrub/repair support Darrick J. Wong
2017-01-05  1:17 ` [PATCH 7/9] populate: discover XFS structure fields and fuzz verbs, and use them to fuzz fields Darrick J. Wong
2017-01-21  8:10 [PATCH v5 0/9] xfstests: online scrub/repair support Darrick J. Wong
2017-01-21  8:11 ` [PATCH 7/9] populate: discover XFS structure fields and fuzz verbs, and use them to fuzz fields Darrick J. Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161109033202.GP27776@eguan.usersys.redhat.com \
    --to=eguan@redhat.com \
    --cc=darrick.wong@oracle.com \
    --cc=david@fromorbit.com \
    --cc=fstests@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).