public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: sanity check global roots key.offset
@ 2022-02-08 19:30 Josef Bacik
  2022-02-09 15:25 ` David Sterba
  0 siblings, 1 reply; 2+ messages in thread
From: Josef Bacik @ 2022-02-08 19:30 UTC (permalink / raw)
  To: linux-btrfs, kernel-team

For !extent tree v2 we should validate the key.offset == 0, and for
extent tree v2 we should validate that key.offset < nr_global_roots.  If
this fails we need to fail to load the global root so that the
appropriate action is taken.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
Dave, this fixes GH issue https://github.com/kdave/btrfs-progs/issues/446, the
problem was we weren't catching that nr_global_roots was incorrect and then
segfaulting later.  I think I sent this as part of a series, but it stands
alone.

 kernel-shared/ctree.h   | 1 +
 kernel-shared/disk-io.c | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/kernel-shared/ctree.h b/kernel-shared/ctree.h
index 6ca49c09..bf71fc85 100644
--- a/kernel-shared/ctree.h
+++ b/kernel-shared/ctree.h
@@ -1233,6 +1233,7 @@ struct btrfs_fs_info {
 
 	u64 super_bytenr;
 	u64 total_pinned;
+	u64 nr_global_roots;
 
 	struct list_head dirty_cowonly_roots;
 	struct list_head recow_ebs;
diff --git a/kernel-shared/disk-io.c b/kernel-shared/disk-io.c
index 364a0bd8..c03211ef 100644
--- a/kernel-shared/disk-io.c
+++ b/kernel-shared/disk-io.c
@@ -933,6 +933,7 @@ struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr)
 	fs_info->data_alloc_profile = (u64)-1;
 	fs_info->metadata_alloc_profile = (u64)-1;
 	fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
+	fs_info->nr_global_roots = 1;
 	return fs_info;
 free_all:
 	btrfs_free_fs_info(fs_info);
@@ -1075,6 +1076,13 @@ static int load_global_roots_objectid(struct btrfs_fs_info *fs_info,
 		if (key.objectid != objectid)
 			break;
 
+		if (key.offset >= fs_info->nr_global_roots) {
+			warning("global root with too large of an offset [%llu %llu]\n",
+				key.objectid, key.offset);
+			ret = -EINVAL;
+			break;
+		}
+
 		root = calloc(1, sizeof(*root));
 		if (!root) {
 			ret = -ENOMEM;
-- 
2.26.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] btrfs-progs: sanity check global roots key.offset
  2022-02-08 19:30 [PATCH] btrfs-progs: sanity check global roots key.offset Josef Bacik
@ 2022-02-09 15:25 ` David Sterba
  0 siblings, 0 replies; 2+ messages in thread
From: David Sterba @ 2022-02-09 15:25 UTC (permalink / raw)
  To: Josef Bacik; +Cc: linux-btrfs, kernel-team

On Tue, Feb 08, 2022 at 02:30:05PM -0500, Josef Bacik wrote:
> For !extent tree v2 we should validate the key.offset == 0, and for
> extent tree v2 we should validate that key.offset < nr_global_roots.  If
> this fails we need to fail to load the global root so that the
> appropriate action is taken.
> 
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
> Dave, this fixes GH issue https://github.com/kdave/btrfs-progs/issues/446, the
> problem was we weren't catching that nr_global_roots was incorrect and then
> segfaulting later.  I think I sent this as part of a series, but it stands
> alone.

Thanks, I think I've merged all the prep stuff, in kernel and in progs
too, so I'll do another batch for 5.17.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-09 15:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-08 19:30 [PATCH] btrfs-progs: sanity check global roots key.offset Josef Bacik
2022-02-09 15:25 ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox