fs/btrfs: fix path buffer overflow in subvolume listing
PATCH 1/1 fs/btrfs: fix path buffer overflow in subvolume listing
get_path_in_subvol() and list_one_subvol() accumulate a path across the on-disk inode-ref / root-ref chain with strncat(path_ret, tmp, PATH_MAX). strncat()'s bound limits the source copy, not the destination total, and there is no depth/cycle limit, so a deep or cyclic ref chain overflows the PATH_MAX path_ret buffer (CWE-787/120).
Use strlcat()/strlcpy(), which bound the total length, and reject the path if it would be truncated (which also terminates a cyclic chain).
Reachable via the btrsubvol command on a crafted btrfs image.
--- fs/btrfs/subvolume.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)
fs/btrfs/subvolume.c +20 -5
@@ -64,7 +64,16 @@ static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) (unsigned long)(iref + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +/* +* strncat()'s bound limits the source copy, not the +* destination, so a deep/cyclic ref chain would overflow +* path_ret (PATH_MAX). strlcat() bounds the total; reject +* the path if it would be truncated. +*/ +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + }btrfs_item_key_to_cpu (path.nodes[0 ], &key, path.slots[0 ]); cur = key.offset;
@@ -128,7 +137,10 @@ static int list_one_subvol(struct btrfs_root *root, char *path_ret) (unsigned long)(rr + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + }/* Get the path inside the parent subvolume */ btrfs_item_key_to_cpu (path.nodes[0 ], &key, path.slots[0 ]);
@@ -146,9 +158,12 @@ static int list_one_subvol(struct btrfs_root *root, char *path_ret) cur = key.offset; }/* Add the leading '/' */ -strncpy (tmp, path_ret,PATH_MAX ); -strncpy (path_ret,"/" ,PATH_MAX ); -strncat (path_ret, tmp,PATH_MAX ); +strlcpy (tmp, path_ret,PATH_MAX ); +strlcpy (path_ret,"/" ,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + } out:btrfs_release_path (&path);free (tmp);
Signature · 1 line
2.53.0
Quoted 6 lines · Dilip Mallavarapu
get_path_in_subvol()andlist_one_subvol()accumulate a path across the on-disk inode-ref / root-ref chain withstrncat(path_ret, tmp, PATH_MAX). strncat()'s bound limits the source copy, not the destination total, and there is no depth/cycle limit, so a deep or cyclic ref chain overflows thePATH_MAXpath_retbuffer (CWE-787/120).
Quoted 6 lines
Use strlcat()/strlcpy(), which bound the total length, and reject the path if it would be truncated (which also terminates a cyclic chain).
Reachable via the btrsubvol command on a crafted btrfs image.
Signed-off-by Dilip Mallavarapu <[email protected]>
Just a nitpick on the comment.
Quoted 9 lines
--- fs/btrfs/subvolume.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)fs/btrfs/subvolume.c +0
-0
fs/btrfs/subvolume.c static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) · line 72
Quoted 10 lines
fs/btrfs/subvolume.c +6
-1@@ -64,7 +64,16 @@ static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) (unsigned long)(iref + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +/* +* strncat()'s bound limits the source copy, not the +* destination, so a deep/cyclic ref chain would overflow +* path_ret (PATH_MAX). strlcat() bounds the total; reject +* the path if it would be truncated. +*/
This comment seems overkilled, and it only covers one call site.
To be honest, I don't think there is any comment needed for usage of strlcat()/strlcpy().
Thanks,
Qu
Quoted 35 lines
fs/btrfs/subvolume.c +14
-4+if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + }btrfs_item_key_to_cpu (path.nodes[0 ], &key, path.slots[0 ]); cur = key.offset;@@ -128,7 +137,10 @@ static int list_one_subvol(struct btrfs_root *root, char *path_ret) (unsigned long)(rr + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + }/* Get the path inside the parent subvolume */ btrfs_item_key_to_cpu (path.nodes[0 ], &key, path.slots[0 ]);@@ -146,9 +158,12 @@ static int list_one_subvol(struct btrfs_root *root, char *path_ret) cur = key.offset; }/* Add the leading '/' */ -strncpy (tmp, path_ret,PATH_MAX ); -strncpy (path_ret,"/" ,PATH_MAX ); -strncat (path_ret, tmp,PATH_MAX ); +strlcpy (tmp, path_ret,PATH_MAX ); +strlcpy (path_ret,"/" ,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + } out:btrfs_release_path (&path);free (tmp);
Quoted 42 lines · Qu Wenruo
Quoted 6 lines · Dilip Mallavarapu
get_path_in_subvol()andlist_one_subvol()accumulate a path across the on-disk inode-ref / root-ref chain withstrncat(path_ret, tmp, PATH_MAX). strncat()'s bound limits the source copy, not the destination total, and there is no depth/cycle limit, so a deep or cyclic ref chain overflows thePATH_MAXpath_retbuffer (CWE-787/120).Quoted 6 lines
Use strlcat()/strlcpy(), which bound the total length, and reject the path if it would be truncated (which also terminates a cyclic chain).
Reachable via the btrsubvol command on a crafted btrfs image.
Signed-off-by Dilip Mallavarapu <[email protected]>Reviewed-by Qu Wenruo <[email protected]>Just a nitpick on the comment.
Quoted 9 lines
--- fs/btrfs/subvolume.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)fs/btrfs/subvolume.c +0
-0fs/btrfs/subvolume.c static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) · line 72
Quoted 10 lines
fs/btrfs/subvolume.c +6
-1@@ -64,7 +64,16 @@ static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) (unsigned long)(iref + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +/* +* strncat()'s bound limits the source copy, not the +* destination, so a deep/cyclic ref chain would overflow +* path_ret (PATH_MAX). strlcat() bounds the total; reject +* the path if it would be truncated. +*/ This comment seems overkilled, and it only covers one call site.
Quoted 2 lines
To be honest, I don't think there is any comment needed for usage of strlcat()/strlcpy().
Yes, and it's the kind of thing which AI tends to do, and then expects human reviewers to remark on. Please note that the AI policy for U-Boot is "please don't".
Signature · 1 line
Tom
Thanks Qu, I'll drop the comment in v2.
Tom, noted on the AI policy. To be upfront: I did use an LLM to help with the analysis and drafting here. The bugs and the fixes I've gone through and verified myself, but I should have said so when Quentin asked, and I'll keep future submissions in my own words. Happy to continue only as far as is useful to you, the findings stand on their own either way.
Dilip
Quoted 56 lines · Tom Rini
Quoted 5 lines · Qu Wenruo
Quoted 2 lines · Dilip Mallavarapu
get_path_in_subvol()andlist_one_subvol()accumulate a path across the on-disk inode-ref / root-ref chain with strncat(path_ret, tmp,
PATH_MAX).Quoted 1 line
Quoted 1 line
strncat()'s bound limits the source copy, not the destination total,
and
Quoted 18 lines
Quoted 9 lines
there is no depth/cycle limit, so a deep or cyclic ref chain overflows the
PATH_MAXpath_retbuffer (CWE-787/120).Use strlcat()/strlcpy(), which bound the total length, and reject the path if it would be truncated (which also terminates a cyclic chain).
Reachable via the btrsubvol command on a crafted btrfs image.
Signed-off-by Dilip Mallavarapu <[email protected]>Reviewed-by Qu Wenruo <[email protected]>Just a nitpick on the comment.
Quoted 4 lines
--- fs/btrfs/subvolume.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-)Quoted 5 lines
fs/btrfs/subvolume.c static int get_path_in_subvol(struct btrfs_root
Quoted 5 lines
fs/btrfs/subvolume.c +0
-0@@ -64,7 +64,16 @@ static int get_path_in_subvol(struct btrfs_root*root, u64 ino, char
*path_ret)Quoted 11 lines
fs/btrfs/subvolume.c static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) · line 72
Quoted 10 lines
fs/btrfs/subvolume.c +6
-1(unsigned long)(iref + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +/* +* strncat()'s bound limits the source copy, not the +* destination, so a deep/cyclic ref chain would overflow +* path_ret (PATH_MAX). strlcat() bounds the total; reject +* the path if it would be truncated. +*/ Quoted 4 lines
This comment seems overkilled, and it only covers one call site.
To be honest, I don't think there is any comment needed for usage of strlcat()/strlcpy().
Yes, and it's the kind of thing which AI tends to do, and then expects human reviewers to remark on. Please note that the AI policy for U-Boot is "please don't".
--
Tom
Quoted 7 lines · Dilip
Thanks Qu, I'll drop the comment in v2.
Tom, noted on the AI policy. To be upfront: I did use an LLM to help with the analysis and drafting here. The bugs and the fixes I've gone through and verified myself, but I should have said so when Quentin asked, and I'll keep future submissions in my own words. Happy to continue only as far as is useful to you, the findings stand on their own either way.
I want to address the general notion you're making here, that LLM-discovered findings stand on their own.
They do not. Setting aside (because *largely* they're better today than 6 months ago) if they're technically true the question is, do they architecturally matter? We're a bootloader, not a general purpose operating system. We have real resource constraints. We (generally) aren't sitting on the network. Someone has to have physical access to the device. Often devices aren't going to be looking at removable storage.
This is also not to say that correctness doesn't matter, it does. And reasonable defense-in-depth programming techniques are useful too.
But they are things that require looking at the system as a whole, and seeing what needs to be done. And what is even the threat model and so is or is not relevant.
Signature · 1 line
Tom
PATCH v2 1/1 fs/btrfs: fix path buffer overflow in subvolume listing
In reply to Dilip Mallavarapuget_path_in_subvol() and list_one_subvol() accumulate a path across the on-disk inode-ref / root-ref chain with strncat(path_ret, tmp, PATH_MAX). strncat()'s bound limits the source copy, not the destination total, and there is no depth/cycle limit, so a deep or cyclic ref chain overflows the PATH_MAX path_ret buffer (CWE-787/120).
Use strlcat()/strlcpy(), which bound the total length, and reject the path if it would be truncated (which also terminates a cyclic chain).
Reachable via the btrsubvol command on a crafted btrfs image.
---
Changes in v2:
- Drop the inline explanatory comment (Qu Wenruo, Tom Rini)
fs/btrfs/subvolume.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
fs/btrfs/subvolume.c +14 -5
@@ -64,7 +64,10 @@ static int get_path_in_subvol(struct btrfs_root *root, u64 ino, char *path_ret) (unsigned long)(iref + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + }btrfs_item_key_to_cpu (path.nodes[0 ], &key, path.slots[0 ]); cur = key.offset;
@@ -128,7 +131,10 @@ static int list_one_subvol(struct btrfs_root *root, char *path_ret) (unsigned long)(rr + 1), name_len); path_ret[name_len] ='/' ; path_ret[name_len +1 ] ='\0' ; -strncat (path_ret, tmp,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + }/* Get the path inside the parent subvolume */ btrfs_item_key_to_cpu (path.nodes[0 ], &key, path.slots[0 ]);
@@ -146,9 +152,12 @@ static int list_one_subvol(struct btrfs_root *root, char *path_ret) cur = key.offset; }/* Add the leading '/' */ -strncpy (tmp, path_ret,PATH_MAX ); -strncpy (path_ret,"/" ,PATH_MAX ); -strncat (path_ret, tmp,PATH_MAX ); +strlcpy (tmp, path_ret,PATH_MAX ); +strlcpy (path_ret,"/" ,PATH_MAX ); +if (strlcat (path_ret, tmp,PATH_MAX ) >=PATH_MAX ) { + ret = -ENAMETOOLONG ; + goto out; + } out:btrfs_release_path (&path);free (tmp);
Signature · 1 line
2.53.0