Skip to contentSkip to conversation

ocfs2: validate l_tree_depth to avoid out-of-bounds access

PATCH Superseded Vasiliy Kovalev 2 participants 6 messages Feb 13, 2025 – Feb 14, 2025 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: validate l_tree_depth to avoid out-of-bounds access

The l_tree_depth field in struct ocfs2_extent_list is defined as __le16, but according to the comments in the structure definition, the high 8 bits must not be used, meaning its maximum valid value is 255.

Add a check to prevent out-of-bounds access if l_tree_depth has an invalid value, which may occur when reading from a corrupted mounted disk [1].

Fixes ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Reported-by [email protected]
---
 fs/ocfs2/extent_map.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/ocfs2/extent_map.c +9 -0
index 930150ed5db15..0d9c25fdec029 100644
@@ -415,6 +415,15 @@ static int ocfs2_get_clusters_nocache(struct inode *inode,
 	tree_height = le16_to_cpu(el->l_tree_depth);
 
 	if (tree_height > 0) {
+		if (unlikely(tree_height > 255)) {
+			ocfs2_error(inode->i_sb,
+				    "Inode %lu has too large l_tree_depth=%u in leaf block %llu\n",
+				    inode->i_ino, tree_height,
+				    (unsigned long long)di_bh->b_blocknr);
+			ret = -EROFS;
+			goto out;
+		}
+
 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
 				      &eb_bh);
 		if (ret) {
Signature · 1 line
2.42.2
maintainer
#1 lore raw
Quoted 22 lines · Vasiliy Kovalev

The l_tree_depth field in struct ocfs2_extent_list is defined as __le16, but according to the comments in the structure definition, the high 8 bits must not be used, meaning its maximum valid value is 255.

Add a check to prevent out-of-bounds access if l_tree_depth has an invalid value, which may occur when reading from a corrupted mounted disk [1].

Fixes ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Reported-by [email protected]
---
 fs/ocfs2/extent_map.c | 9 +++++++++
 1 file changed, 9 insertions(+)
fs/ocfs2/extent_map.c +0 -0
index 930150ed5db15..0d9c25fdec029 100644

fs/ocfs2/extent_map.c static int ocfs2_get_clusters_nocache(struct inode *inode, · line 418

Quoted 4 lines
fs/ocfs2/extent_map.c +1 -0
@@ -415,6 +415,15 @@ static int ocfs2_get_clusters_nocache(struct inode *inode,
 	tree_height = le16_to_cpu(el->l_tree_depth);
 
 	if (tree_height > 0) {
+		if (unlikely(tree_height > 255)) {

In fact we won't allow larger tree depth.
IIRC, it is OCFS2_MAX_PATH_DEPTH, refer ocfs2_new_path(). BTW, I'd like add this check in __ocfs2_find_path() instead.

fs/ocfs2/extent_map.c static int ocfs2_get_clusters_nocache(struct inode *inode, · line 420

Quoted 2 lines
fs/ocfs2/extent_map.c +2 -0
+			ocfs2_error(inode->i_sb,
+				    "Inode %lu has too large l_tree_depth=%u in leaf block %llu\n",

It's not leaf, but root.

Thanks,
Joseph

Quoted 9 lines
fs/ocfs2/extent_map.c +6 -0
+				    inode->i_ino, tree_height,
+				    (unsigned long long)di_bh->b_blocknr);
+			ret = -EROFS;
+			goto out;
+		}
+
 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, v_cluster,
 				      &eb_bh);
 		if (ret) {
#2 lore raw

PATCH v2 1/1 ocfs2: validate l_tree_depth to avoid out-of-bounds access

The l_tree_depth field is 16-bit (__le16), but the actual maximum depth is limited to OCFS2_MAX_PATH_DEPTH.

Add a check to prevent out-of-bounds access if l_tree_depth has an invalid value, which may occur when reading from a corrupted mounted disk [1].

Fixes ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Reported-by [email protected]

---
v2: Restricted depth to OCFS2_MAX_PATH_DEPTH and moved the check

    to __ocfs2_find_path().
---
 fs/ocfs2/alloc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/alloc.c +8 -0
index 4414743b638e8..cec8fc5cb8e87 100644
@@ -1803,6 +1803,14 @@ static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
 
 	el = root_el;
 	while (el->l_tree_depth) {
+		if (unlikely(le16_to_cpu(el->l_tree_depth) > OCFS2_MAX_PATH_DEPTH)) {
+			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+				    "Owner %llu has invalid tree depth %u in extent list\n",
+				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
+				    le16_to_cpu(el->l_tree_depth));
+			ret = -EROFS;
+			goto out;
+		}
 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
 				    "Owner %llu has empty extent list at depth %u\n",
Signature · 1 line
2.42.2
maintainer
#3 lore raw
Quoted 23 lines · Vasiliy Kovalev

The l_tree_depth field is 16-bit (__le16), but the actual maximum depth is limited to OCFS2_MAX_PATH_DEPTH.

Add a check to prevent out-of-bounds access if l_tree_depth has an invalid value, which may occur when reading from a corrupted mounted disk [1].

Fixes ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Reported-by [email protected]

---
v2: Restricted depth to OCFS2_MAX_PATH_DEPTH and moved the check

    to __ocfs2_find_path().
---
 fs/ocfs2/alloc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/alloc.c +0 -0
index 4414743b638e8..cec8fc5cb8e87 100644

fs/ocfs2/alloc.c static int __ocfs2_find_path(struct ocfs2_caching_info *ci, · line 1806

Quoted 4 lines
fs/ocfs2/alloc.c +1 -0
@@ -1803,6 +1803,14 @@ static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
 
 	el = root_el;
 	while (el->l_tree_depth) {
+		if (unlikely(le16_to_cpu(el->l_tree_depth) > OCFS2_MAX_PATH_DEPTH)) {

More precisely, it's ">= OCFS2_MAX_PATH_DEPTH".

Quoted 10 lines
fs/ocfs2/alloc.c +7 -0
+			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+				    "Owner %llu has invalid tree depth %u in extent list\n",
+				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
+				    le16_to_cpu(el->l_tree_depth));
+			ret = -EROFS;
+			goto out;
+		}
 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
 				    "Owner %llu has empty extent list at depth %u\n",
#4 lore raw

PATCH v3 1/1 ocfs2: validate l_tree_depth to avoid out-of-bounds access

The l_tree_depth field is 16-bit (__le16), but the actual maximum depth is limited to OCFS2_MAX_PATH_DEPTH.

Add a check to prevent out-of-bounds access if l_tree_depth has an invalid value, which may occur when reading from a corrupted mounted disk [1].

Fixes ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Reported-by [email protected]

---
v3: Change the condition "> OCFS2_MAX_PATH_DEPTH" to ">= OCFS2_MAX_PATH_DEPTH"
v2: Restricted depth to OCFS2_MAX_PATH_DEPTH and moved the check

    to __ocfs2_find_path().
---
 fs/ocfs2/alloc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/alloc.c +8 -0
index 4414743b638e8..cec8fc5cb8e87 100644
@@ -1803,6 +1803,14 @@ static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
 
 	el = root_el;
 	while (el->l_tree_depth) {
+		if (unlikely(le16_to_cpu(el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH)) {
+			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+				    "Owner %llu has invalid tree depth %u in extent list\n",
+				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
+				    le16_to_cpu(el->l_tree_depth));
+			ret = -EROFS;
+			goto out;
+		}
 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
 				    "Owner %llu has empty extent list at depth %u\n",
Signature · 1 line
2.42.2
maintainer Reviewed-by on v3 1/1
#5 lore raw
Quoted 11 lines · Vasiliy Kovalev

The l_tree_depth field is 16-bit (__le16), but the actual maximum depth is limited to OCFS2_MAX_PATH_DEPTH.

Add a check to prevent out-of-bounds access if l_tree_depth has an invalid value, which may occur when reading from a corrupted mounted disk [1].

Fixes ccd979bdbce9 ("[PATCH] OCFS2: The Second Oracle Cluster Filesystem")
Reported-by [email protected]

Looks fine.

Quoted 27 lines

---
v3: Change the condition "> OCFS2_MAX_PATH_DEPTH" to ">= OCFS2_MAX_PATH_DEPTH"
v2: Restricted depth to OCFS2_MAX_PATH_DEPTH and moved the check

    to __ocfs2_find_path().
---
 fs/ocfs2/alloc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
fs/ocfs2/alloc.c +8 -0
index 4414743b638e8..cec8fc5cb8e87 100644
@@ -1803,6 +1803,14 @@ static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
 
 	el = root_el;
 	while (el->l_tree_depth) {
+		if (unlikely(le16_to_cpu(el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH)) {
+			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
+				    "Owner %llu has invalid tree depth %u in extent list\n",
+				    (unsigned long long)ocfs2_metadata_cache_owner(ci),
+				    le16_to_cpu(el->l_tree_depth));
+			ret = -EROFS;
+			goto out;
+		}
 		if (le16_to_cpu(el->l_next_free_rec) == 0) {
 			ocfs2_error(ocfs2_metadata_cache_get_super(ci),
 				    "Owner %llu has empty extent list at depth %u\n",