linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josef Bacik <josef@toxicpanda.com>
To: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH v2 07/12] btrfs-progs: add the ability to corrupt fields of the super block
Date: Wed, 18 Aug 2021 17:33:19 -0400	[thread overview]
Message-ID: <7c5a5c52f3cf9ba6b0188b5509e39de93f6bb662.1629322156.git.josef@toxicpanda.com> (raw)
In-Reply-To: <cover.1629322156.git.josef@toxicpanda.com>

Doing the extent tree v2 work I generated an invalid super block with
the wrong bytes_used set, and only noticed because it affected the block
groups as well.  Neither modes of fsck check for a valid bytes_used, so
add the ability to corrupt this field so I can generate a testcase for
fsck.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 btrfs-corrupt-block.c | 66 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 65 insertions(+), 1 deletion(-)

diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
index 80622f29..7e576897 100644
--- a/btrfs-corrupt-block.c
+++ b/btrfs-corrupt-block.c
@@ -355,6 +355,24 @@ enum btrfs_block_group_field {
 	BTRFS_BLOCK_GROUP_ITEM_BAD,
 };
 
+enum btrfs_super_field {
+	BTRFS_SUPER_FIELD_FLAGS,
+	BTRFS_SUPER_FIELD_TOTAL_BYTES,
+	BTRFS_SUPER_FIELD_BYTES_USED,
+	BTRFS_SUPER_FIELD_BAD,
+};
+
+static enum btrfs_super_field convert_super_field(char *field)
+{
+	if (!strncmp(field, "flags", FIELD_BUF_LEN))
+		return BTRFS_SUPER_FIELD_FLAGS;
+	if (!strncmp(field, "total_bytes", FIELD_BUF_LEN))
+		return BTRFS_SUPER_FIELD_TOTAL_BYTES;
+	if (!strncmp(field, "bytes_used", FIELD_BUF_LEN))
+		return BTRFS_SUPER_FIELD_BYTES_USED;
+	return BTRFS_SUPER_FIELD_BAD;
+}
+
 static enum btrfs_block_group_field convert_block_group_field(char *field)
 {
 	if (!strncmp(field, "used", FIELD_BUF_LEN))
@@ -460,6 +478,41 @@ static u8 generate_u8(u8 orig)
 	return ret;
 }
 
+static int corrupt_super_block(struct btrfs_root *root, char *field)
+{
+	struct btrfs_fs_info *fs_info = root->fs_info;
+	enum btrfs_super_field corrupt_field;
+	u64 orig, bogus;
+
+	corrupt_field = convert_super_field(field);
+	if (corrupt_field == BTRFS_SUPER_FIELD_BAD) {
+		fprintf(stderr, "Invalid field %s\n", field);
+		return -EINVAL;
+	}
+
+	switch (corrupt_field) {
+	case BTRFS_SUPER_FIELD_FLAGS:
+		orig = btrfs_super_flags(fs_info->super_copy);
+		bogus = generate_u64(orig);
+		btrfs_set_super_flags(fs_info->super_copy, bogus);
+		break;
+	case BTRFS_SUPER_FIELD_TOTAL_BYTES:
+		orig = btrfs_super_total_bytes(fs_info->super_copy);
+		bogus = generate_u64(orig);
+		btrfs_set_super_total_bytes(fs_info->super_copy, bogus);
+		break;
+	case BTRFS_SUPER_FIELD_BYTES_USED:
+		orig = btrfs_super_bytes_used(fs_info->super_copy);
+		bogus = generate_u64(orig);
+		btrfs_set_super_bytes_used(fs_info->super_copy, bogus);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return write_all_supers(fs_info);
+}
+
 static int corrupt_block_group(struct btrfs_root *root, u64 bg, char *field)
 {
 	struct btrfs_trans_handle *trans;
@@ -1240,6 +1293,7 @@ int main(int argc, char **argv)
 	int corrupt_di = 0;
 	int delete = 0;
 	int should_corrupt_key = 0;
+	int corrupt_super = 0;
 	u64 metadata_block = 0;
 	u64 inode = 0;
 	u64 file_extent = (u64)-1;
@@ -1274,11 +1328,12 @@ int main(int argc, char **argv)
 			{ "root", no_argument, NULL, 'r'},
 			{ "csum", required_argument, NULL, 'C'},
 			{ "block-group", required_argument, NULL, 'B'},
+			{ "super", no_argument, NULL, 's'},
 			{ "help", no_argument, NULL, GETOPT_VAL_HELP},
 			{ NULL, 0, NULL, 0 }
 		};
 
-		c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:I:D:d:r:C:B:",
+		c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:I:D:d:r:C:B:s",
 				long_options, NULL);
 		if (c < 0)
 			break;
@@ -1344,6 +1399,9 @@ int main(int argc, char **argv)
 			case 'B':
 				block_group = arg_strtou64(optarg);
 				break;
+			case 's':
+				corrupt_super = 1;
+				break;
 			case GETOPT_VAL_HELP:
 			default:
 				print_usage(c != GETOPT_VAL_HELP);
@@ -1491,6 +1549,12 @@ int main(int argc, char **argv)
 		ret = corrupt_block_group(root, block_group, field);
 		goto out_close;
 	}
+	if (corrupt_super) {
+		if (*field == 0)
+			print_usage(1);
+		ret = corrupt_super_block(root, field);
+		goto out_close;
+	}
 	/*
 	 * If we made it here and we have extent set then we didn't specify
 	 * inode and we're screwed.
-- 
2.26.3


  parent reply	other threads:[~2021-08-18 21:33 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-18 21:33 [PATCH v2 00/12] btrfs-progs: make check handle invalid bg items Josef Bacik
2021-08-18 21:33 ` [PATCH v2 01/12] btrfs-progs: fix running lowmem check tests Josef Bacik
2021-08-19  5:40   ` Qu Wenruo
2021-08-23 14:54   ` David Sterba
2021-08-18 21:33 ` [PATCH v2 02/12] btrfs-progs: do not infinite loop on corrupt keys with lowmem mode Josef Bacik
2021-08-19  5:42   ` Qu Wenruo
2021-08-23 15:04     ` David Sterba
2021-08-23 18:44       ` Josef Bacik
2021-08-23 23:34         ` Qu Wenruo
2021-08-18 21:33 ` [PATCH v2 03/12] btrfs-progs: propagate fs root errors in " Josef Bacik
2021-08-19  5:43   ` Qu Wenruo
2021-08-18 21:33 ` [PATCH v2 04/12] btrfs-progs: propagate extent item " Josef Bacik
2021-08-19  5:45   ` Qu Wenruo
2021-08-18 21:33 ` [PATCH v2 05/12] btrfs-progs: do not double add unaligned extent records Josef Bacik
2021-08-18 21:33 ` [PATCH v2 06/12] btrfs-progs: add the ability to corrupt block group items Josef Bacik
2021-08-18 21:33 ` Josef Bacik [this message]
2021-08-23 14:59   ` [PATCH v2 07/12] btrfs-progs: add the ability to corrupt fields of the super block David Sterba
2021-08-18 21:33 ` [PATCH v2 08/12] btrfs-progs: make check detect and fix invalid used for block groups Josef Bacik
2021-08-19  5:54   ` Qu Wenruo
2021-08-18 21:33 ` [PATCH v2 09/12] btrfs-progs: make check detect and fix problems with super_bytes_used Josef Bacik
2021-08-19  5:56   ` Qu Wenruo
2021-08-18 21:33 ` [PATCH v2 10/12] btrfs-progs: check btrfs_super_used in lowmem check Josef Bacik
2021-08-19  5:57   ` Qu Wenruo
2021-08-18 21:33 ` [PATCH v2 11/12] btrfs-progs: add a test image with a corrupt block group item Josef Bacik
2021-08-18 21:33 ` [PATCH v2 12/12] btrfs-progs: add a test image with an invalid super bytes_used Josef Bacik
2021-08-23 18:31 ` [PATCH v2 00/12] btrfs-progs: make check handle invalid bg items David Sterba

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=7c5a5c52f3cf9ba6b0188b5509e39de93f6bb662.1629322156.git.josef@toxicpanda.com \
    --to=josef@toxicpanda.com \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@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).