mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ocfs2: validate l_tree_depth to avoid out-of-bounds access
@ 2025-02-13 19:16 Vasiliy Kovalev
  2025-02-14  6:18 ` Joseph Qi
  0 siblings, 1 reply; 6+ messages in thread
From: Vasiliy Kovalev @ 2025-02-13 19:16 UTC (permalink / raw)
  To: linux-kernel, ocfs2-devel, Kurt Hackel, Joseph Qi, Joel Becker,
	Mark Fasheh
  Cc: kovalev, lvc-project, syzbot+66c146268dc88f4341fd

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: syzbot+66c146268dc88f4341fd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=66c146268dc88f4341fd [1]
Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
---
 fs/ocfs2/extent_map.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c
index 930150ed5db15..0d9c25fdec029 100644
--- a/fs/ocfs2/extent_map.c
+++ b/fs/ocfs2/extent_map.c
@@ -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) {
-- 
2.42.2


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

* Re: [PATCH] ocfs2: validate l_tree_depth to avoid out-of-bounds access
  2025-02-13 19:16 [PATCH] ocfs2: validate l_tree_depth to avoid out-of-bounds access Vasiliy Kovalev
@ 2025-02-14  6:18 ` Joseph Qi
  2025-02-14  8:00   ` [PATCH v2] " Vasiliy Kovalev
  0 siblings, 1 reply; 6+ messages in thread
From: Joseph Qi @ 2025-02-14  6:18 UTC (permalink / raw)
  To: Vasiliy Kovalev
  Cc: lvc-project, syzbot+66c146268dc88f4341fd, linux-kernel,
	ocfs2-devel, Kurt Hackel, Joel Becker, Mark Fasheh



On 2025/2/14 03:16, Vasiliy Kovalev wrote:
> 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: syzbot+66c146268dc88f4341fd@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=66c146268dc88f4341fd [1]
> Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
> ---
>  fs/ocfs2/extent_map.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/fs/ocfs2/extent_map.c b/fs/ocfs2/extent_map.c
> index 930150ed5db15..0d9c25fdec029 100644
> --- a/fs/ocfs2/extent_map.c
> +++ b/fs/ocfs2/extent_map.c
> @@ -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.

> +			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

> +				    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) {


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

* [PATCH v2] ocfs2: validate l_tree_depth to avoid out-of-bounds access
  2025-02-14  6:18 ` Joseph Qi
@ 2025-02-14  8:00   ` Vasiliy Kovalev
  2025-02-14  8:32     ` Joseph Qi
  0 siblings, 1 reply; 6+ messages in thread
From: Vasiliy Kovalev @ 2025-02-14  8:00 UTC (permalink / raw)
  To: joseph.qi
  Cc: jlbec, kovalev, kurt.hackel, linux-kernel, lvc-project, mark,
	ocfs2-devel, syzbot+66c146268dc88f4341fd

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: syzbot+66c146268dc88f4341fd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=66c146268dc88f4341fd [1]
Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
---
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(+)

diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
index 4414743b638e8..cec8fc5cb8e87 100644
--- a/fs/ocfs2/alloc.c
+++ b/fs/ocfs2/alloc.c
@@ -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",
-- 
2.42.2


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

* Re: [PATCH v2] ocfs2: validate l_tree_depth to avoid out-of-bounds access
  2025-02-14  8:00   ` [PATCH v2] " Vasiliy Kovalev
@ 2025-02-14  8:32     ` Joseph Qi
  2025-02-14  8:49       ` [PATCH v3] " Vasiliy Kovalev
  0 siblings, 1 reply; 6+ messages in thread
From: Joseph Qi @ 2025-02-14  8:32 UTC (permalink / raw)
  To: Vasiliy Kovalev
  Cc: jlbec, kurt.hackel, linux-kernel, lvc-project, mark, ocfs2-devel,
	syzbot+66c146268dc88f4341fd



On 2025/2/14 16:00, Vasiliy Kovalev wrote:
> 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: syzbot+66c146268dc88f4341fd@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=66c146268dc88f4341fd [1]
> Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
> ---
> 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(+)
> 
> diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
> index 4414743b638e8..cec8fc5cb8e87 100644
> --- a/fs/ocfs2/alloc.c
> +++ b/fs/ocfs2/alloc.c
> @@ -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".

> +			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",


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

* [PATCH v3] ocfs2: validate l_tree_depth to avoid out-of-bounds access
  2025-02-14  8:32     ` Joseph Qi
@ 2025-02-14  8:49       ` Vasiliy Kovalev
  2025-02-14  9:55         ` Joseph Qi
  0 siblings, 1 reply; 6+ messages in thread
From: Vasiliy Kovalev @ 2025-02-14  8:49 UTC (permalink / raw)
  To: joseph.qi
  Cc: jlbec, kovalev, kurt.hackel, linux-kernel, lvc-project, mark,
	ocfs2-devel, syzbot+66c146268dc88f4341fd

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: syzbot+66c146268dc88f4341fd@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=66c146268dc88f4341fd [1]
Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>
---
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(+)

diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
index 4414743b638e8..cec8fc5cb8e87 100644
--- a/fs/ocfs2/alloc.c
+++ b/fs/ocfs2/alloc.c
@@ -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",
-- 
2.42.2


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

* Re: [PATCH v3] ocfs2: validate l_tree_depth to avoid out-of-bounds access
  2025-02-14  8:49       ` [PATCH v3] " Vasiliy Kovalev
@ 2025-02-14  9:55         ` Joseph Qi
  0 siblings, 0 replies; 6+ messages in thread
From: Joseph Qi @ 2025-02-14  9:55 UTC (permalink / raw)
  To: Vasiliy Kovalev, akpm
  Cc: jlbec, kurt.hackel, linux-kernel, lvc-project, mark, ocfs2-devel,
	syzbot+66c146268dc88f4341fd



On 2025/2/14 16:49, Vasiliy Kovalev wrote:
> 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: syzbot+66c146268dc88f4341fd@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=66c146268dc88f4341fd [1]
> Signed-off-by: Vasiliy Kovalev <kovalev@altlinux.org>

Looks fine.
Reviewed-by: Joseph Qi <joseph.qi@linux.alibaba.com>

> ---
> 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(+)
> 
> diff --git a/fs/ocfs2/alloc.c b/fs/ocfs2/alloc.c
> index 4414743b638e8..cec8fc5cb8e87 100644
> --- a/fs/ocfs2/alloc.c
> +++ b/fs/ocfs2/alloc.c
> @@ -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",


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

end of thread, other threads:[~2025-02-14  9:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-13 19:16 [PATCH] ocfs2: validate l_tree_depth to avoid out-of-bounds access Vasiliy Kovalev
2025-02-14  6:18 ` Joseph Qi
2025-02-14  8:00   ` [PATCH v2] " Vasiliy Kovalev
2025-02-14  8:32     ` Joseph Qi
2025-02-14  8:49       ` [PATCH v3] " Vasiliy Kovalev
2025-02-14  9:55         ` Joseph Qi

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

all inboxes | Powered by JetHome®