Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH 1/3] btrfs: Introduce btrfs_check_namelen to avoid reading beyond boundary
@ 2017-05-25  2:09 Su Yue
  2017-05-25  2:09 ` [PATCH 2/3] btrfs: check namelen with boundary in verify dir_item Su Yue
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Su Yue @ 2017-05-25  2:09 UTC (permalink / raw)
  To: linux-btrfs

When reading out name from inode_ref, dir_item, it's possible that
corrupted name_len lead to read beyond boundary.

Since there are already patches for btrfs-progs, this is for btrfs.

Introduce function btrfs_check_namelen, it should be called before reading
name from extent_buffer.
The function compares arg @namelen with boundary then returns 'proper'
namelen.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 fs/btrfs/ctree.h    |  2 ++
 fs/btrfs/dir-item.c | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 643c70d2b2e6..f49e04e7612b 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -3074,6 +3074,8 @@ int btrfs_find_name_in_ext_backref(struct btrfs_path *path,
 				   int name_len,
 				   struct btrfs_inode_extref **extref_ret);
 
+u16 btrfs_check_namelen(struct extent_buffer *leaf, int slot,
+			unsigned long start, u32 namelen);
 /* file-item.c */
 struct btrfs_dio_private;
 int btrfs_del_csums(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index c24d615e3d7f..7af5ad8e9a3c 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -484,3 +484,25 @@ int verify_dir_item(struct btrfs_fs_info *fs_info,
 
 	return 0;
 }
+
+/*
+ * Returns >0: the value @namelen after cut according item boundary
+ * Returns  0: on error
+ */
+u16 btrfs_check_namelen(struct extent_buffer *leaf, int slot,
+			unsigned long start, u32 namelen)
+{
+	u32 end;
+	u64 ret = namelen;
+
+	end = btrfs_leaf_data(leaf) + btrfs_item_end_nr(leaf, slot);
+
+	if (start > end)
+		return 0;
+	if (start + namelen > end) {
+		ret = end - start;
+		if (ret > U16_MAX)
+			ret = 0;
+	}
+	return ret;
+}
-- 
2.13.0




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

end of thread, other threads:[~2017-05-31  2:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-25  2:09 [PATCH 1/3] btrfs: Introduce btrfs_check_namelen to avoid reading beyond boundary Su Yue
2017-05-25  2:09 ` [PATCH 2/3] btrfs: check namelen with boundary in verify dir_item Su Yue
2017-05-29 15:24   ` David Sterba
2017-05-25  2:09 ` [PATCH 3/3] btrfs: check namelen before read/memcmp_extent_buffer Su Yue
2017-05-29 15:43   ` David Sterba
2017-05-31  2:31     ` Su Yue
2017-05-29 15:22 ` [PATCH 1/3] btrfs: Introduce btrfs_check_namelen to avoid reading beyond boundary David Sterba
2017-05-29 15:33   ` David Sterba

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