Skip to contentSkip to conversation

vfs_getxattr_alloc(): don't allocate buf on failure

PATCH Superseded Miklos Szeredi 3 participants 4 messages Aug 2, 2022 – Aug 3, 2022 VFS

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfs_getxattr_alloc(): don't allocate buf on failure

Some callers of vfs_getxattr_alloc() assume that on failure the allocated buffer does not need to be freed.

Callers could be fixed, but fixing the semantics of vfs_getxattr_alloc() is simpler and makes sure that this class of bugs does not occur again.

Reported-and-tested-by [email protected]
Fixes 1601fbad2b14 ("xattr: define vfs_getxattr_alloc and vfs_xattr_cmp")
---
 fs/xattr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/xattr.c +4 -1
index e8dd03e4561e..1800cfa97411 100644
@@ -383,7 +383,10 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
 	}
 
 	error = handler->get(handler, dentry, inode, name, value, error);
-	*xattr_value = value;
+	if (error < 0 && value != *xattr_value)
+		kfree(value);
+	else
+		*xattr_value = value;
 	return error;
 }
 
Signature · 1 line
2.35.3
maintainer
#1 lore raw
Quoted 18 lines · Miklos Szeredi

Some callers of vfs_getxattr_alloc() assume that on failure the allocated buffer does not need to be freed.

Callers could be fixed, but fixing the semantics of vfs_getxattr_alloc() is simpler and makes sure that this class of bugs does not occur again.

Reported-and-tested-by [email protected]
Fixes 1601fbad2b14 ("xattr: define vfs_getxattr_alloc and vfs_xattr_cmp")
---
 fs/xattr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/xattr.c +0 -0
index e8dd03e4561e..1800cfa97411 100644

fs/xattr.c vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry, · line 391

Quoted 10 lines
fs/xattr.c +4 -1
@@ -383,7 +383,10 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
 	}
 
 	error = handler->get(handler, dentry, inode, name, value, error);
-	*xattr_value = value;
+	if (error < 0 && value != *xattr_value)
+		kfree(value);
+	else
+		*xattr_value = value;
 	return error;
 }

Think what happens if it had been called with non-NULL *xattr_value, found that it needed realloc, had krealloc() succeed (and free the original), only to fail in ->get().

Your variant will leave *xattr_value pointing to already freed object, with no way for the caller to tell that from failure before it got to krealloc().

IOW, that's unusable for callers with preallocated buffer - in particular, ones that call that thing in a loop.

maintainer
#2 lore raw
Quoted 34 lines · Al Viro
Quoted 18 lines · Miklos Szeredi

Some callers of vfs_getxattr_alloc() assume that on failure the allocated buffer does not need to be freed.

Callers could be fixed, but fixing the semantics of vfs_getxattr_alloc() is simpler and makes sure that this class of bugs does not occur again.

Reported-and-tested-by [email protected]
Fixes 1601fbad2b14 ("xattr: define vfs_getxattr_alloc and vfs_xattr_cmp")
---
 fs/xattr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/xattr.c +0 -0
index e8dd03e4561e..1800cfa97411 100644

fs/xattr.c vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry, · line 391

Quoted 10 lines
fs/xattr.c +4 -1
@@ -383,7 +383,10 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
 	}
 
 	error = handler->get(handler, dentry, inode, name, value, error);
-	*xattr_value = value;
+	if (error < 0 && value != *xattr_value)
+		kfree(value);
+	else
+		*xattr_value = value;
 	return error;
 }

Think what happens if it had been called with non-NULL *xattr_value, found that it needed realloc, had krealloc() succeed (and free the original), only to fail in ->get().

Quoted 6 lines

Your variant will leave *xattr_value pointing to already freed object, with no way for the caller to tell that from failure before it got to krealloc().

IOW, that's unusable for callers with preallocated buffer - in particular, ones that call that thing in a loop.

FWIW, if we change calling conventions so that in some cases caller need not kfree() whatever's in *xattr_value, about the only variant I see is to have the damn thing freed and replaced with NULL on *all* failure exits. Might or might not make sense, not sure...

#3 lore raw
Quoted 40 lines · Al Viro
Quoted 38 lines · Al Viro
Quoted 18 lines · Miklos Szeredi

Some callers of vfs_getxattr_alloc() assume that on failure the allocated buffer does not need to be freed.

Callers could be fixed, but fixing the semantics of vfs_getxattr_alloc() is simpler and makes sure that this class of bugs does not occur again.

Reported-and-tested-by [email protected]
Fixes 1601fbad2b14 ("xattr: define vfs_getxattr_alloc and vfs_xattr_cmp")
---
 fs/xattr.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/xattr.c +0 -0
index e8dd03e4561e..1800cfa97411 100644

fs/xattr.c vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry, · line 391

Quoted 10 lines
fs/xattr.c +4 -1
@@ -383,7 +383,10 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
    }
 
    error = handler->get(handler, dentry, inode, name, value, error);
-   *xattr_value = value;
+   if (error < 0 && value != *xattr_value)
+           kfree(value);
+   else
+           *xattr_value = value;
    return error;
 }

Think what happens if it had been called with non-NULL *xattr_value, found that it needed realloc, had krealloc() succeed (and free the original), only to fail in ->get().

Your variant will leave *xattr_value pointing to already freed object, with no way for the caller to tell that from failure before it got to krealloc().

Quoted 7 lines
Quoted 2 lines

IOW, that's unusable for callers with preallocated buffer - in particular, ones that call that thing in a loop.

FWIW, if we change calling conventions so that in some cases caller need not kfree() whatever's in *xattr_value, about the only variant I see is to have the damn thing freed and replaced with NULL on *all* failure exits. Might or might not make sense, not sure...

The minimal semantic change that would fix all buggy callers is to change the non-loop case (i.e. when called only once with a xattr_value pointing to NULL.

In the looping case callers correctly handle the error, since an explicit kfree in the helper is unexpected.

Posted v2.

Thanks,
Miklos