Skip to contentSkip to conversation

btrfs-progs: check: Detect file extent end overflow

PATCH In review Qu Wenruo 4 participants 5 messages Feb 19, 2020 – Feb 21, 2020 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/2 btrfs-progs: check: Detect file extent end overflow

There is a report about tree-checker rejecting some leaves due to bad EXTENT_DATA.

The offending EXTENT_DATA looks like:

	item 72 key (1359622 EXTENT_DATA 475136) itemoff 11140 itemsize 53
		generation 954719 type 1 (regular)
		extent data disk byte 0 nr 0
		extent data offset 0 nr 18446744073709486080 ram 18446744073709486080
		extent compression 0 (none)

Add such check to both original and lowmem mode to detect such problem.

---
 check/main.c          | 4 ++++
 check/mode-common.h   | 7 +++++++
 check/mode-lowmem.c   | 7 +++++++
 check/mode-original.h | 1 +
 4 files changed, 19 insertions(+)
check/main.c +4 -0
index d02dd1636852..f71bf4f74129 100644
@@ -597,6 +597,8 @@ static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
 		fprintf(stderr, ", odd file extent");
 	if (errors & I_ERR_BAD_FILE_EXTENT)
 		fprintf(stderr, ", bad file extent");
+	if (errors & I_ERR_FILE_EXTENT_OVERFLOW)
+		fprintf(stderr, ", file extent end overflow");
 	if (errors & I_ERR_FILE_EXTENT_OVERLAP)
 		fprintf(stderr, ", file extent overlap");
 	if (errors & I_ERR_FILE_EXTENT_TOO_LARGE)
@@ -1595,6 +1597,8 @@ static int process_file_extent(struct btrfs_root *root,
 		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
 		disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
 		extent_offset = btrfs_file_extent_offset(eb, fi);
+		if (u64_add_overflow(key->offset, num_bytes))
+			rec->errors |= I_ERR_FILE_EXTENT_OVERFLOW;
 		if (num_bytes == 0 || (num_bytes & mask))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (num_bytes + extent_offset >
check/mode-common.h +7 -0
index edf9257edaf0..daa5741e1d67 100644
@@ -173,4 +173,11 @@ static inline u32 btrfs_type_to_imode(u8 type)
 
 	return imode_by_btrfs_type[(type)];
 }
+
+static inline bool u64_add_overflow(u64 a, u64 b)
+{
+	if (a > (u64)-1 - b)
+		return true;
+	return false;
+}
 #endif
check/mode-lowmem.c +7 -0
index 630fabf66919..d257a44f3086 100644
@@ -2085,6 +2085,13 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,
 		err |= INVALID_GENERATION;
 	}
 
+	/* Extent end shouldn't overflow */
+	if (u64_add_overflow(fkey.offset, extent_num_bytes)) {
+		error(
+	"file extent end over flow, file offset %llu extent num bytes %llu",
+			fkey.offset, extent_num_bytes);
+		err |= FILE_EXTENT_ERROR;
+	}
 	/*
 	 * Check EXTENT_DATA csum
 	 *
check/mode-original.h +1 -0
index b075a95c9757..07d741f4a1b5 100644
@@ -186,6 +186,7 @@ struct unaligned_extent_rec_t {
 #define I_ERR_MISMATCH_DIR_HASH		(1 << 18)
 #define I_ERR_INVALID_IMODE		(1 << 19)
 #define I_ERR_INVALID_GEN		(1 << 20)
+#define I_ERR_FILE_EXTENT_OVERFLOW	(1 << 21)
 
 struct inode_record {
 	struct list_head backrefs;
Signature · 1 line
2.25.0
author
#1 lore raw

PATCH 2/2 btrfs-progs: fsck-tests: Add test image for file extent overflow

Now it only tests detection.

---
 .../045-file-extent-overflow/default.img.xz      | Bin 0 -> 1864 bytes
 .../fsck-tests/045-file-extent-overflow/test.sh  |  15 +++++++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 tests/fsck-tests/045-file-extent-overflow/default.img.xz
 create mode 100755 tests/fsck-tests/045-file-extent-overflow/test.sh
tests/fsck-tests/045-file-extent-overflow/default.img.xz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..ba6bb471e1e455dba6dd73556368fb0dce04f5aa
GIT binary patch
tests/fsck-tests/045-file-extent-overflow/test.sh new file +15 -0
new file mode 100755
index 000000000000..7a635a490d84
@@ -0,0 +1,15 @@
+#!/bin/bash
+#
+# Make sure btrfs check can detect file extent end overflow
+
+source "$TEST_TOP/common"
+
+check_prereq btrfs
+
+check_image() {
+	run_mustfail \
+		"btrfs check failed to detect file extent overflow" \
+		"$TOP/btrfs" check "$1"
+}
+
+check_all_images
Signature · 1 line
2.25.0
#2 lore raw
In reply to Qu Wenruo
Quoted 48 lines · Qu Wenruo

There is a report about tree-checker rejecting some leaves due to bad EXTENT_DATA.

The offending EXTENT_DATA looks like:

	item 72 key (1359622 EXTENT_DATA 475136) itemoff 11140 itemsize 53
		generation 954719 type 1 (regular)
		extent data disk byte 0 nr 0
		extent data offset 0 nr 18446744073709486080 ram 18446744073709486080
		extent compression 0 (none)

Add such check to both original and lowmem mode to detect such problem.

---
 check/main.c          | 4 ++++
 check/mode-common.h   | 7 +++++++
 check/mode-lowmem.c   | 7 +++++++
 check/mode-original.h | 1 +
 4 files changed, 19 insertions(+)
check/main.c +4 -0
index d02dd1636852..f71bf4f74129 100644
@@ -597,6 +597,8 @@ static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
 		fprintf(stderr, ", odd file extent");
 	if (errors & I_ERR_BAD_FILE_EXTENT)
 		fprintf(stderr, ", bad file extent");
+	if (errors & I_ERR_FILE_EXTENT_OVERFLOW)
+		fprintf(stderr, ", file extent end overflow");
 	if (errors & I_ERR_FILE_EXTENT_OVERLAP)
 		fprintf(stderr, ", file extent overlap");
 	if (errors & I_ERR_FILE_EXTENT_TOO_LARGE)
@@ -1595,6 +1597,8 @@ static int process_file_extent(struct btrfs_root *root,
 		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
 		disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
 		extent_offset = btrfs_file_extent_offset(eb, fi);
+		if (u64_add_overflow(key->offset, num_bytes))
+			rec->errors |= I_ERR_FILE_EXTENT_OVERFLOW;
 		if (num_bytes == 0 || (num_bytes & mask))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (num_bytes + extent_offset >
check/mode-common.h +0 -0
index edf9257edaf0..daa5741e1d67 100644

check/mode-common.h static inline u32 btrfs_type_to_imode(u8 type) · line 177

Quoted 5 lines
check/mode-common.h +2 -0
@@ -173,4 +173,11 @@ static inline u32 btrfs_type_to_imode(u8 type)
 
 	return imode_by_btrfs_type[(type)];
 }
+
+static inline bool u64_add_overflow(u64 a, u64 b)

Rename this to check_add_overflow and use the generic version from the kernel :

#define check_add_overflow(a, b, d) ({ \

        typeof(a) __a = (a);                    \

        typeof(b) __b = (b);                    \

        typeof(d) __d = (d);                    \

        (void) (&__a == &__b);                  \

        (void) (&__a == __d);                   \

        __builtin_add_overflow(__a, __b, __d);  \

})

Quoted 37 lines
check/mode-common.h +5 -0
+{
+	if (a > (u64)-1 - b)
+		return true;
+	return false;
+}
 #endif
check/mode-lowmem.c +7 -0
index 630fabf66919..d257a44f3086 100644
@@ -2085,6 +2085,13 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,
 		err |= INVALID_GENERATION;
 	}
 
+	/* Extent end shouldn't overflow */
+	if (u64_add_overflow(fkey.offset, extent_num_bytes)) {
+		error(
+	"file extent end over flow, file offset %llu extent num bytes %llu",
+			fkey.offset, extent_num_bytes);
+		err |= FILE_EXTENT_ERROR;
+	}
 	/*
 	 * Check EXTENT_DATA csum
 	 *
check/mode-original.h +1 -0
index b075a95c9757..07d741f4a1b5 100644
@@ -186,6 +186,7 @@ struct unaligned_extent_rec_t {
 #define I_ERR_MISMATCH_DIR_HASH		(1 << 18)
 #define I_ERR_INVALID_IMODE		(1 << 19)
 #define I_ERR_INVALID_GEN		(1 << 20)
+#define I_ERR_FILE_EXTENT_OVERFLOW	(1 << 21)
 
 struct inode_record {
 	struct list_head backrefs;
#3 lore raw
Quoted 52 lines · Nikolay Borisov
Quoted 49 lines · Qu Wenruo

There is a report about tree-checker rejecting some leaves due to bad EXTENT_DATA.

The offending EXTENT_DATA looks like:

	item 72 key (1359622 EXTENT_DATA 475136) itemoff 11140 itemsize 53
		generation 954719 type 1 (regular)
		extent data disk byte 0 nr 0
		extent data offset 0 nr 18446744073709486080 ram 18446744073709486080
		extent compression 0 (none)

Add such check to both original and lowmem mode to detect such problem.

---
 check/main.c          | 4 ++++
 check/mode-common.h   | 7 +++++++
 check/mode-lowmem.c   | 7 +++++++
 check/mode-original.h | 1 +
 4 files changed, 19 insertions(+)
check/main.c +4 -0
index d02dd1636852..f71bf4f74129 100644
@@ -597,6 +597,8 @@ static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
 		fprintf(stderr, ", odd file extent");
 	if (errors & I_ERR_BAD_FILE_EXTENT)
 		fprintf(stderr, ", bad file extent");
+	if (errors & I_ERR_FILE_EXTENT_OVERFLOW)
+		fprintf(stderr, ", file extent end overflow");
 	if (errors & I_ERR_FILE_EXTENT_OVERLAP)
 		fprintf(stderr, ", file extent overlap");
 	if (errors & I_ERR_FILE_EXTENT_TOO_LARGE)
@@ -1595,6 +1597,8 @@ static int process_file_extent(struct btrfs_root *root,
 		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
 		disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
 		extent_offset = btrfs_file_extent_offset(eb, fi);
+		if (u64_add_overflow(key->offset, num_bytes))
+			rec->errors |= I_ERR_FILE_EXTENT_OVERFLOW;
 		if (num_bytes == 0 || (num_bytes & mask))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (num_bytes + extent_offset >
check/mode-common.h +0 -0
index edf9257edaf0..daa5741e1d67 100644
@@ -173,4 +173,11 @@ static inline u32 btrfs_type_to_imode(u8 type)
 
Quoted 7 lines

check/mode-common.h static inline u32 btrfs_type_to_imode(u8 type) · line 177

Quoted 4 lines
check/mode-common.h +2 -0
 	return imode_by_btrfs_type[(type)];
 }
+
+static inline bool u64_add_overflow(u64 a, u64 b)

Rename this to check_add_overflow and use the generic version from the kernel :

That's also my first idea.

But I'm not a fan of the 3rd parameter, and there is no other type other than u64, so I hesitate to use the generic one.

However since you mentioned the kernel one, I guess it's time to backport it to user space.

Thanks,
Qu

Quoted 54 lines

#define check_add_overflow(a, b, d) ({ \

        typeof(a) __a = (a);                    \

        typeof(b) __b = (b);                    \

        typeof(d) __d = (d);                    \

        (void) (&__a == &__b);                  \

        (void) (&__a == __d);                   \

        __builtin_add_overflow(__a, __b, __d);  \

})

Quoted 37 lines
check/mode-common.h +5 -0
+{
+	if (a > (u64)-1 - b)
+		return true;
+	return false;
+}
 #endif
check/mode-lowmem.c +7 -0
index 630fabf66919..d257a44f3086 100644
@@ -2085,6 +2085,13 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,
 		err |= INVALID_GENERATION;
 	}
 
+	/* Extent end shouldn't overflow */
+	if (u64_add_overflow(fkey.offset, extent_num_bytes)) {
+		error(
+	"file extent end over flow, file offset %llu extent num bytes %llu",
+			fkey.offset, extent_num_bytes);
+		err |= FILE_EXTENT_ERROR;
+	}
 	/*
 	 * Check EXTENT_DATA csum
 	 *
check/mode-original.h +1 -0
index b075a95c9757..07d741f4a1b5 100644
@@ -186,6 +186,7 @@ struct unaligned_extent_rec_t {
 #define I_ERR_MISMATCH_DIR_HASH		(1 << 18)
 #define I_ERR_INVALID_IMODE		(1 << 19)
 #define I_ERR_INVALID_GEN		(1 << 20)
+#define I_ERR_FILE_EXTENT_OVERFLOW	(1 << 21)
 
 struct inode_record {
 	struct list_head backrefs;
#4 lore raw
Quoted 23 lines · Qu Wenruo
Quoted 15 lines
Quoted 15 lines
check/main.c +2 -0
@@ -1595,6 +1597,8 @@ static int process_file_extent(struct btrfs_root *root,
 		num_bytes = btrfs_file_extent_num_bytes(eb, fi);
 		disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
 		extent_offset = btrfs_file_extent_offset(eb, fi);
+		if (u64_add_overflow(key->offset, num_bytes))
+			rec->errors |= I_ERR_FILE_EXTENT_OVERFLOW;
 		if (num_bytes == 0 || (num_bytes & mask))
 			rec->errors |= I_ERR_BAD_FILE_EXTENT;
 		if (num_bytes + extent_offset >
check/mode-common.h +0 -0
index edf9257edaf0..daa5741e1d67 100644
@@ -173,4 +173,11 @@ static inline u32 btrfs_type_to_imode(u8 type)
 
Quoted 7 lines

check/mode-common.h static inline u32 btrfs_type_to_imode(u8 type) · line 177

Quoted 4 lines
check/mode-common.h +2 -0
 	return imode_by_btrfs_type[(type)];
 }
+
+static inline bool u64_add_overflow(u64 a, u64 b)

Rename this to check_add_overflow and use the generic version from the kernel :

Quoted 7 lines

That's also my first idea.

But I'm not a fan of the 3rd parameter, and there is no other type other than u64, so I hesitate to use the generic one.

However since you mentioned the kernel one, I guess it's time to backport it to user space.

Yes please, copying the support code from kernel is ok.