Skip to contentSkip to conversation

fuse: don't truncate cached, mutated symlink

PATCH Applied Miklos Szeredi 7 participants 8 messages Feb 20, 2025 – Feb 25, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fuse: don't truncate cached, mutated symlink

Fuse allows the value of a symlink to change and this property is exploited by some filesystems (e.g. CVMFS).

It has been observed, that sometimes after changing the symlink contents, the value is truncated to the old size.

This is caused by fuse_getattr() racing with fuse_reverse_inval_inode(). fuse_reverse_inval_inode() updates the fuse_inode's attr_version, which results in fuse_change_attributes() exiting before updating the cached attributes

This is okay, as the cached attributes remain invalid and the next call to fuse_change_attributes() will likely update the inode with the correct values.

The reason this causes problems is that cached symlinks will be returned through page_get_link(), which truncates the symlink to inode->i_size. This is correct for filesystems that don't mutate symlinks, but in this case it causes bad behavior.

The solution is to just remove this truncation. This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely. If that happens we'd need to make this behavior conditional.

---
 fs/fuse/dir.c      |  2 +-
 fs/namei.c         | 24 +++++++++++++++++++-----
 include/linux/fs.h |  2 ++
 3 files changed, 22 insertions(+), 6 deletions(-)
fs/fuse/dir.c +1 -1
index 589e88822368..83c56ce6ad20 100644
@@ -1645,7 +1645,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
 		goto out_err;
 
 	if (fc->cache_symlinks)
-		return page_get_link(dentry, inode, callback);
+		return page_get_link_raw(dentry, inode, callback);
 
 	err = -ECHILD;
 	if (!dentry)
fs/namei.c +19 -5
index 3ab9440c5b93..ecb7b95c2ca3 100644
@@ -5356,10 +5356,9 @@ const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
 EXPORT_SYMBOL(vfs_get_link);
 
 /* get the link contents into pagecache */
-const char *page_get_link(struct dentry *dentry, struct inode *inode,
-			  struct delayed_call *callback)
+static char *__page_get_link(struct dentry *dentry, struct inode *inode,
+			     struct delayed_call *callback)
 {
-	char *kaddr;
 	struct page *page;
 	struct address_space *mapping = inode->i_mapping;
 
@@ -5378,8 +5377,23 @@ const char *page_get_link(struct dentry *dentry, struct inode *inode,
 	}
 	set_delayed_call(callback, page_put_link, page);
 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
-	kaddr = page_address(page);
-	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
+	return page_address(page);
+}
+
+const char *page_get_link_raw(struct dentry *dentry, struct inode *inode,
+			      struct delayed_call *callback)
+{
+	return __page_get_link(dentry, inode, callback);
+}
+EXPORT_SYMBOL_GPL(page_get_link_raw);
+
+const char *page_get_link(struct dentry *dentry, struct inode *inode,
+					struct delayed_call *callback)
+{
+	char *kaddr = __page_get_link(dentry, inode, callback);
+
+	if (!IS_ERR(kaddr))
+		nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
 	return kaddr;
 }
 
include/linux/fs.h +2 -0
index 2c3b2f8a621f..9346adf28f7b 100644
@@ -3452,6 +3452,8 @@ extern const struct file_operations generic_ro_fops;
 
 extern int readlink_copy(char __user *, int, const char *, int);
 extern int page_readlink(struct dentry *, char __user *, int);
+extern const char *page_get_link_raw(struct dentry *, struct inode *,
+				     struct delayed_call *);
 extern const char *page_get_link(struct dentry *, struct inode *,
 				 struct delayed_call *);
 extern void page_put_link(void *);
Signature · 1 line
2.48.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 95 lines · Miklos Szeredi

Fuse allows the value of a symlink to change and this property is exploited by some filesystems (e.g. CVMFS).

It has been observed, that sometimes after changing the symlink contents, the value is truncated to the old size.

This is caused by fuse_getattr() racing with fuse_reverse_inval_inode(). fuse_reverse_inval_inode() updates the fuse_inode's attr_version, which results in fuse_change_attributes() exiting before updating the cached attributes

This is okay, as the cached attributes remain invalid and the next call to fuse_change_attributes() will likely update the inode with the correct values.

The reason this causes problems is that cached symlinks will be returned through page_get_link(), which truncates the symlink to inode->i_size. This is correct for filesystems that don't mutate symlinks, but in this case it causes bad behavior.

The solution is to just remove this truncation. This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely. If that happens we'd need to make this behavior conditional.

---
 fs/fuse/dir.c      |  2 +-
 fs/namei.c         | 24 +++++++++++++++++++-----
 include/linux/fs.h |  2 ++
 3 files changed, 22 insertions(+), 6 deletions(-)
fs/fuse/dir.c +1 -1
index 589e88822368..83c56ce6ad20 100644
@@ -1645,7 +1645,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
 		goto out_err;
 
 	if (fc->cache_symlinks)
-		return page_get_link(dentry, inode, callback);
+		return page_get_link_raw(dentry, inode, callback);
 
 	err = -ECHILD;
 	if (!dentry)
fs/namei.c +19 -5
index 3ab9440c5b93..ecb7b95c2ca3 100644
@@ -5356,10 +5356,9 @@ const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
 EXPORT_SYMBOL(vfs_get_link);
 
 /* get the link contents into pagecache */
-const char *page_get_link(struct dentry *dentry, struct inode *inode,
-			  struct delayed_call *callback)
+static char *__page_get_link(struct dentry *dentry, struct inode *inode,
+			     struct delayed_call *callback)
 {
-	char *kaddr;
 	struct page *page;
 	struct address_space *mapping = inode->i_mapping;
 
@@ -5378,8 +5377,23 @@ const char *page_get_link(struct dentry *dentry, struct inode *inode,
 	}
 	set_delayed_call(callback, page_put_link, page);
 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
-	kaddr = page_address(page);
-	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
+	return page_address(page);
+}
+
+const char *page_get_link_raw(struct dentry *dentry, struct inode *inode,
+			      struct delayed_call *callback)
+{
+	return __page_get_link(dentry, inode, callback);
+}
+EXPORT_SYMBOL_GPL(page_get_link_raw);
+
+const char *page_get_link(struct dentry *dentry, struct inode *inode,
+					struct delayed_call *callback)
+{
+	char *kaddr = __page_get_link(dentry, inode, callback);
+
+	if (!IS_ERR(kaddr))
+		nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
 	return kaddr;
 }
 
include/linux/fs.h +0 -0
index 2c3b2f8a621f..9346adf28f7b 100644

include/linux/fs.h extern const struct file_operations generic_ro_fops; · line 3459

Quoted 8 lines
include/linux/fs.h +2 -0
@@ -3452,6 +3452,8 @@ extern const struct file_operations generic_ro_fops;
 
 extern int readlink_copy(char __user *, int, const char *, int);
 extern int page_readlink(struct dentry *, char __user *, int);
+extern const char *page_get_link_raw(struct dentry *, struct inode *,
+				     struct delayed_call *);
 extern const char *page_get_link(struct dentry *, struct inode *,
 				 struct delayed_call *);
 extern void page_put_link(void *);

I had looked at it last year already, but always wanted to write a test for it (and never found the time).

maintainer Applied
#2 lore raw
In reply to Miklos Szeredi
Quoted 6 lines · Miklos Szeredi

Fuse allows the value of a symlink to change and this property is exploited by some filesystems (e.g. CVMFS).

It has been observed, that sometimes after changing the symlink contents, the value is truncated to the old size.

Quoted 6 lines

This is caused by fuse_getattr() racing with fuse_reverse_inval_inode(). fuse_reverse_inval_inode() updates the fuse_inode's attr_version, which results in fuse_change_attributes() exiting before updating the cached attributes

[...]

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] fuse: don't truncate cached, mutated symlink

      https://git.kernel.org/vfs/vfs/c/b4c173dfbb6c
Reviewed-by Tested-by on v1 1/1
#3 lore raw
In reply to Miklos Szeredi
Quoted 20 lines · Miklos Szeredi

Fuse allows the value of a symlink to change and this property is exploited by some filesystems (e.g. CVMFS).

It has been observed, that sometimes after changing the symlink contents, the value is truncated to the old size.

This is caused by fuse_getattr() racing with fuse_reverse_inval_inode(). fuse_reverse_inval_inode() updates the fuse_inode's attr_version, which results in fuse_change_attributes() exiting before updating the cached attributes

This is okay, as the cached attributes remain invalid and the next call to fuse_change_attributes() will likely update the inode with the correct values.

The reason this causes problems is that cached symlinks will be returned through page_get_link(), which truncates the symlink to inode->i_size. This is correct for filesystems that don't mutate symlinks, but in this case it causes bad behavior.

Quoted 8 lines

The solution is to just remove this truncation. This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely. If that happens we'd need to make this behavior conditional.

OK, I finally managed to reproduce the bug (thanks for the hints, Sam!) and I can also confirm this patch fixes it.

So, feel free to add my

Cheers,

Signature · 1 line
Luís
Quoted 80 lines
---
 fs/fuse/dir.c      |  2 +-
 fs/namei.c         | 24 +++++++++++++++++++-----
 include/linux/fs.h |  2 ++
 3 files changed, 22 insertions(+), 6 deletions(-)
fs/fuse/dir.c +1 -1
index 589e88822368..83c56ce6ad20 100644
@@ -1645,7 +1645,7 @@ static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
 		goto out_err;
 
 	if (fc->cache_symlinks)
-		return page_get_link(dentry, inode, callback);
+		return page_get_link_raw(dentry, inode, callback);
 
 	err = -ECHILD;
 	if (!dentry)
fs/namei.c +19 -5
index 3ab9440c5b93..ecb7b95c2ca3 100644
@@ -5356,10 +5356,9 @@ const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
 EXPORT_SYMBOL(vfs_get_link);
 
 /* get the link contents into pagecache */
-const char *page_get_link(struct dentry *dentry, struct inode *inode,
-			  struct delayed_call *callback)
+static char *__page_get_link(struct dentry *dentry, struct inode *inode,
+			     struct delayed_call *callback)
 {
-	char *kaddr;
 	struct page *page;
 	struct address_space *mapping = inode->i_mapping;
 
@@ -5378,8 +5377,23 @@ const char *page_get_link(struct dentry *dentry, struct inode *inode,
 	}
 	set_delayed_call(callback, page_put_link, page);
 	BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
-	kaddr = page_address(page);
-	nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
+	return page_address(page);
+}
+
+const char *page_get_link_raw(struct dentry *dentry, struct inode *inode,
+			      struct delayed_call *callback)
+{
+	return __page_get_link(dentry, inode, callback);
+}
+EXPORT_SYMBOL_GPL(page_get_link_raw);
+
+const char *page_get_link(struct dentry *dentry, struct inode *inode,
+					struct delayed_call *callback)
+{
+	char *kaddr = __page_get_link(dentry, inode, callback);
+
+	if (!IS_ERR(kaddr))
+		nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
 	return kaddr;
 }
 
include/linux/fs.h +2 -0
index 2c3b2f8a621f..9346adf28f7b 100644
@@ -3452,6 +3452,8 @@ extern const struct file_operations generic_ro_fops;
 
 extern int readlink_copy(char __user *, int, const char *, int);
 extern int page_readlink(struct dentry *, char __user *, int);
+extern const char *page_get_link_raw(struct dentry *, struct inode *,
+				     struct delayed_call *);
 extern const char *page_get_link(struct dentry *, struct inode *,
 				 struct delayed_call *);
 extern void page_put_link(void *);
Signature · 1 line
2.48.1
maintainer
#4 lore raw
In reply to Miklos Szeredi
Quoted 4 lines · Miklos Szeredi

The solution is to just remove this truncation. This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely. If that happens we'd need to make this behavior conditional.

Note, BTW, that page *contents* must not change at all, so truncation is only safe if we have ->i_size guaranteed to be stable.

Core pathwalk really counts upon the string remaining immutable, and that does include the string returned by ->get_link().

maintainer
#5 lore raw
Quoted 8 lines · Al Viro
Quoted 4 lines · Miklos Szeredi

The solution is to just remove this truncation. This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely. If that happens we'd need to make this behavior conditional.

Quoted 5 lines

Note, BTW, that page *contents* must not change at all, so truncation is only safe if we have ->i_size guaranteed to be stable.

Core pathwalk really counts upon the string remaining immutable, and that does include the string returned by ->get_link().

Page contents will not change after initial readlink, but page could get invalidated and a fresh page filled with new value for the same object.

It's weird behavior all right, but seems to be useful in a couple of cases where invalidating the dentry wouldn't work (I don't remember the details, cvmfs folks can help with that).

Thanks,
Miklos

maintainer
#6 lore raw
Quoted 12 lines · Miklos Szeredi
Quoted 11 lines · Al Viro
Quoted 4 lines · Miklos Szeredi

The solution is to just remove this truncation. This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely. If that happens we'd need to make this behavior conditional.

Note, BTW, that page *contents* must not change at all, so truncation is only safe if we have ->i_size guaranteed to be stable.

Quoted 6 lines
Quoted 2 lines

Core pathwalk really counts upon the string remaining immutable, and that does include the string returned by ->get_link().

Page contents will not change after initial readlink, but page could get invalidated and a fresh page filled with new value for the same object.

*nod*

My point is that truncation of something that might be traversed by another pathwalk is a hard bug - we only get away with doing that in page_get_link() because it's idempotent. If ->i_size can change, we are not allowed to do that.

#7 lore raw

Hello Miklos,

Thank you so much for the patch!

I finally managed to build RHEL9 with your patch and I confirm that truncation of the symlinks is not happening anymore.

However, I will need to do some further testing as I see that sometimes the updating of symlinks is not propagated correctly.
This should not be a kernel problem.
I believe this is more a CVMFS problem, its revision-based update process, that there is no way to pause user interaction with the fuse mount, and the asynchronous removal from inodes/dentries.

Thanks again
Laura

________________________________________
From: Al Viro <[email protected]> on behalf of Al Viro <[email protected]>
Sent: Sunday, February 23, 2025 20:41
To: Miklos Szeredi <[email protected]>
Cc: Miklos Szeredi <[email protected]>; [email protected] <[email protected]>; Christian Brauner <[email protected]>; Bernd Schubert <[email protected]>; Laura Promberger <[email protected]>; Sam Lewis <[email protected]> Subject: Re: [PATCH] fuse: don't truncate cached, mutated symlink

Quoted 12 lines · Miklos Szeredi
Quoted 11 lines · Al Viro
Quoted 4 lines · Miklos Szeredi

The solution is to just remove this truncation.  This can cause a regression in a filesystem that relies on supplying a symlink larger than the file size, but this is unlikely.  If that happens we'd need to make this behavior conditional.

Note, BTW, that page *contents* must not change at all, so truncation is only safe if we have ->i_size guaranteed to be stable.

Quoted 6 lines
Quoted 2 lines

Core pathwalk really counts upon the string remaining immutable, and that does include the string returned by ->get_link().

Page contents will not change after initial readlink, but page could get invalidated and a fresh page filled with new value for the same object.

*nod*

My point is that truncation of something that might be traversed by another pathwalk is a hard bug - we only get away with doing that in page_get_link() because it's idempotent.  If ->i_size can change, we are not allowed to do that.