Skip to contentSkip to conversation

vfs: sanity check the length passed to inode_set_cached_link()

PATCH Applied Mateusz Guzik 4 participants 6 messages Feb 4, 2025 – Feb 5, 2025 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfs: sanity check the length passed to inode_set_cached_link()

This costs a strlen() call when instatianating a symlink.

Preferably it would be hidden behind VFS_WARN_ON (or compatible), but there is no such facility at the moment. With the facility in place the call can be patched out in production kernels.

In the meantime, since the cost is being paid unconditionally, use the result to a fixup the bad caller.

This is not expected to persist in the long run (tm).

Sample splat:
bad length passed for symlink [/tmp/syz-imagegen43743633/file0/file0] (got 131109, expected 37) [rest of WARN blurp goes here]

---

This has a side effect of working around the panic reported in:
https://lore.kernel.org/all/[email protected]/

I'm confident this merely exposed a bug in ext4, see:
https://lore.kernel.org/all/CAGudoHEv+Diti3r0x9VmF5ixgRVKk4trYnX_skVJNkQoTMaDHg@mail.gmail.com/#t

Nonethelss, should help catch future problems.

 include/linux/fs.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/fs.h +13 -0
index be3ad155ec9f..1437a3323731 100644
@@ -791,6 +791,19 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
+	int testlen;
+
+	/*
+	 * TODO: patch it into a debug-only check if relevant macros show up.
+	 * In the meantime, since we are suffering strlen even on production kernels
+	 * to find the right length, do a fixup if the wrong value got passed.
+	 */
+	testlen = strlen(link);
+	if (testlen != linklen) {
+		WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
+			  link, linklen, testlen);
+		linklen = testlen;
+	}
 	inode->i_link = link;
 	inode->i_linklen = linklen;
 	inode->i_opflags |= IOP_CACHED_LINK;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 39 lines · Mateusz Guzik

This costs a strlen() call when instatianating a symlink.

Preferably it would be hidden behind VFS_WARN_ON (or compatible), but there is no such facility at the moment. With the facility in place the call can be patched out in production kernels.

In the meantime, since the cost is being paid unconditionally, use the result to a fixup the bad caller.

This is not expected to persist in the long run (tm).

Sample splat:
bad length passed for symlink [/tmp/syz-imagegen43743633/file0/file0] (got 131109, expected 37) [rest of WARN blurp goes here]

---

This has a side effect of working around the panic reported in:
https://lore.kernel.org/all/[email protected]/

I'm confident this merely exposed a bug in ext4, see:
https://lore.kernel.org/all/CAGudoHEv+Diti3r0x9VmF5ixgRVKk4trYnX_skVJNkQoTMaDHg@mail.gmail.com/#t

Nonethelss, should help catch future problems.

 include/linux/fs.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/fs.h +1 -0
index be3ad155ec9f..1437a3323731 100644
@@ -791,6 +791,19 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
+       int testlen;

include/linux/fs.h struct inode { · line 804

Quoted 10 lines
include/linux/fs.h +10 -0
+
+       /*
+        * TODO: patch it into a debug-only check if relevant macros show up.
+        * In the meantime, since we are suffering strlen even on production kernels
+        * to find the right length, do a fixup if the wrong value got passed.
+        */
+       testlen = strlen(link);
+       if (testlen != linklen) {
+               WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
+                         link, linklen, testlen);

I just remembered that path_noexec thing got a CVE.

I suspect someone may try to assign one here for user-mountable disk images triggering this warn.

Can be patched to "printk_once", but then it does not implicitly tell which fs is messing up due to lack of a backtrace.

Quoted 8 lines
include/linux/fs.h +2 -1
+               linklen = testlen;
+       }
        inode->i_link = link;
        inode->i_linklen = linklen;
        inode->i_opflags |= IOP_CACHED_LINK;
--

2.43.0

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
#2 lore raw
In reply to Mateusz Guzik
Quoted 15 lines · Mateusz Guzik

This costs a strlen() call when instatianating a symlink.

Preferably it would be hidden behind VFS_WARN_ON (or compatible), but there is no such facility at the moment. With the facility in place the call can be patched out in production kernels.

In the meantime, since the cost is being paid unconditionally, use the result to a fixup the bad caller.

This is not expected to persist in the long run (tm).

Sample splat:
bad length passed for symlink [/tmp/syz-imagegen43743633/file0/file0] (got 131109, expected 37) [rest of WARN blurp goes here]

Quoted 8 lines

---

This has a side effect of working around the panic reported in:
https://lore.kernel.org/all/[email protected]/

I'm confident this merely exposed a bug in ext4, see:
https://lore.kernel.org/all/CAGudoHEv+Diti3r0x9VmF5ixgRVKk4trYnX_skVJNkQoTMaDHg@mail.gmail.com/#t

Yes, ext4 should not continue to load a fast symlink where i_size > sizeof(i_data). I'm a little surprised that nd_terminate_link exists to paper over this situation, but the commit adding it (035146851cfa2fe) is from 2008 when we were all young and naïve.

But yes, would be nice to have a CONFIG_VFS_DEBUG behind which to hide assertions so that insufficient validation in ext4 will burble up through Ted's test infrastructure. ;)

That said, I just saw this in ext4_get_link:

	nd_terminate_link(bh->b_data, inode->i_size,
			  inode->i_sb->s_blocksize - 1);

Which is writing into a buffer head without a transaction? Granted symlink targets in ext4 are written out to disk with the null terminator so I guess that's not a big deal.

--D

Quoted 34 lines

Nonethelss, should help catch future problems.

 include/linux/fs.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/fs.h +13 -0
index be3ad155ec9f..1437a3323731 100644
@@ -791,6 +791,19 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
+	int testlen;
+
+	/*
+	 * TODO: patch it into a debug-only check if relevant macros show up.
+	 * In the meantime, since we are suffering strlen even on production kernels
+	 * to find the right length, do a fixup if the wrong value got passed.
+	 */
+	testlen = strlen(link);
+	if (testlen != linklen) {
+		WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
+			  link, linklen, testlen);
+		linklen = testlen;
+	}
 	inode->i_link = link;
 	inode->i_linklen = linklen;
 	inode->i_opflags |= IOP_CACHED_LINK;
Signature · 1 line
2.43.0
maintainer Applied
#3 lore raw
In reply to Mateusz Guzik
Quoted 10 lines · Mateusz Guzik

This costs a strlen() call when instatianating a symlink.

Preferably it would be hidden behind VFS_WARN_ON (or compatible), but there is no such facility at the moment. With the facility in place the call can be patched out in production kernels.

In the meantime, since the cost is being paid unconditionally, use the result to a fixup the bad caller.

[...]

Seems ok if ugly.

---

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] vfs: sanity check the length passed to inode_set_cached_link()

      https://git.kernel.org/vfs/vfs/c/c1c84bb08cc7
reviewer Reviewed-by on v1 1/1
#4 lore raw
In reply to Mateusz Guzik
Quoted 9 lines · Mateusz Guzik

This costs a strlen() call when instatianating a symlink.

Preferably it would be hidden behind VFS_WARN_ON (or compatible), but there is no such facility at the moment. With the facility in place the call can be patched out in production kernels.

In the meantime, since the cost is being paid unconditionally, use the result to a fixup the bad caller.

Quoted 7 lines

This is not expected to persist in the long run (tm).

Sample splat:
bad length passed for symlink [/tmp/syz-imagegen43743633/file0/file0] (got 131109, expected 37) [rest of WARN blurp goes here]

Yeah, it looks a bit pointless to pass the length in only to compare it against strlen(). But as a quick fix until we figure out something more clever it's fine I guess.

Feel free to add:

Honza

Quoted 40 lines

---

This has a side effect of working around the panic reported in:
https://lore.kernel.org/all/[email protected]/

I'm confident this merely exposed a bug in ext4, see:
https://lore.kernel.org/all/CAGudoHEv+Diti3r0x9VmF5ixgRVKk4trYnX_skVJNkQoTMaDHg@mail.gmail.com/#t

Nonethelss, should help catch future problems.

 include/linux/fs.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/fs.h +13 -0
index be3ad155ec9f..1437a3323731 100644
@@ -791,6 +791,19 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
+	int testlen;
+
+	/*
+	 * TODO: patch it into a debug-only check if relevant macros show up.
+	 * In the meantime, since we are suffering strlen even on production kernels
+	 * to find the right length, do a fixup if the wrong value got passed.
+	 */
+	testlen = strlen(link);
+	if (testlen != linklen) {
+		WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
+			  link, linklen, testlen);
+		linklen = testlen;
+	}
 	inode->i_link = link;
 	inode->i_linklen = linklen;
 	inode->i_opflags |= IOP_CACHED_LINK;
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw
Quoted 17 lines · Jan Kara
Quoted 15 lines · Mateusz Guzik

This costs a strlen() call when instatianating a symlink.

Preferably it would be hidden behind VFS_WARN_ON (or compatible), but there is no such facility at the moment. With the facility in place the call can be patched out in production kernels.

In the meantime, since the cost is being paid unconditionally, use the result to a fixup the bad caller.

This is not expected to persist in the long run (tm).

Sample splat:
bad length passed for symlink [/tmp/syz-imagegen43743633/file0/file0] (got 131109, expected 37) [rest of WARN blurp goes here]

Quoted 6 lines
Quoted 1 line

Yeah, it looks a bit pointless to pass the length in only to compare it against strlen(). But as a quick fix until we figure out something more clever it's fine I guess.

There is some dayjob stuff I need to take care of.

Once I'm both out from under the ruble *and* bored enough, I'll post provisional support for VFS_WARN_ON and VFS_BUG_ON macros.

But that's for -next.

Quoted 49 lines

Feel free to add:

Honza

Quoted 38 lines

---

This has a side effect of working around the panic reported in:
https://lore.kernel.org/all/[email protected]/

I'm confident this merely exposed a bug in ext4, see:
https://lore.kernel.org/all/CAGudoHEv+Diti3r0x9VmF5ixgRVKk4trYnX_skVJNkQoTMaDHg@mail.gmail.com/#t

Nonethelss, should help catch future problems.

 include/linux/fs.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/linux/fs.h +13 -1
index be3ad155ec9f..1437a3323731 100644
@@ -791,6 +791,19 @@ struct inode {
 
 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
 {
+     int testlen;
+
+     /*
+      * TODO: patch it into a debug-only check if relevant macros show up.
+      * In the meantime, since we are suffering strlen even on production kernels
+      * to find the right length, do a fixup if the wrong value got passed.
+      */
+     testlen = strlen(link);
+     if (testlen != linklen) {
+             WARN_ONCE(1, "bad length passed for symlink [%s] (got %d, expected %d)",
+                       link, linklen, testlen);
+             linklen = testlen;
+     }
      inode->i_link = link;
      inode->i_linklen = linklen;
      inode->i_opflags |= IOP_CACHED_LINK;
--
Quoted 2 lines

2.43.0

--
Jan Kara <[email protected]>
SUSE Labs, CR

Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>