fs/hfsplus/xattr: Use memcpy() and strscpy() to build xattr_name
Subsystems (2)
PATCH 1/1 fs/hfsplus/xattr: Use memcpy() and strscpy() to build xattr_name
From: David Laight <[email protected]>
xattr_name is kmalloc()ed at the (assumed) maximal size and then the prefix and name concatenated together.
Use memcpy() for the prefix - its length is passed and strscpy() for the name to ensure it really doesnt overflow.
Prior to bf29e886b242c the buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".
---
This is one of a group of patches that remove potentially unbounded strcpy() calls.
They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').
Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().
All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)
fs/hfsplus/xattr.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
fs/hfsplus/xattr.c +6 -6
@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode, const char *name, xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );if (!xattr_name)return -ENOMEM ; -strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); +strscpy (xattr_name + prefixlen, name, xattr_name_len - prefixlen); res =__hfsplus_setxattr (inode, xattr_name, value, size, flags);kfree (xattr_name);
@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1;int res;char *xattr_name;
@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, inode->i_ino, name ? name :NULL , prefix ? prefix : NULL); - xattr_name =kmalloc (NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 , -GFP_KERNEL ); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );if (!xattr_name)return -ENOMEM ; -strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); +strscpy (xattr_name + prefixlen, name, xattr_name_len - prefixlen); res =__hfsplus_getxattr (inode, xattr_name, value, size);kfree (xattr_name);
Signature · 1 line
2.39.5
Viacheslav Dubeyko
maintainerQuoted 9 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for the
name to ensure it really doesnt overflow.
Quoted 3 lines
Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".
What do you mean by "the buffer isn't created just long enough"? And what is your vision of correct implementation of the logic?
Thanks,
Slava.
Quoted 77 lines
Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +2
-2@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode, constchar *name, xattr_name = kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name) return -ENOMEM; -strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); +strscpy (xattr_name + prefixlen, name, xattr_name_len - prefixlen); res = __hfsplus_setxattr(inode, xattr_name, value, size,flags);
kfree(xattr_name);fs/hfsplus/xattr.c +1
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 ; int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c +2
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); + strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
res =__hfsplus_getxattr(inode, xattr_name, value, size);kfree(xattr_name);
Quoted 10 lines · On Tue, 09 Jun 2026 18:04:46 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 9 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for the
name to ensure it really doesnt overflow.
Quoted 6 lines
Quoted 3 lines
Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".What do you mean by "the buffer isn't created just long enough"? And what is your vision of correct implementation of the logic?
The old code is:
Quoted 5 lines
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)return -ENOMEM; strcpy(xattr_name, prefix); strcpy(xattr_name + prefixlen, name);
It would be more usual to do:
size_t name_len = strlen(name) + 1; xattr_name = kmalloc(prefixlen + name_len); memcpy(xattr_name, prefix, prefixlen) memcpy(xattr_name + prefixlen, name, name_len);
So that the buffer is allocated the correct size for the strings.
(Not that it really makes much difference whether you do kmalloc(32) or kmalloc(1024) for a temporary buffer - both come of a per-cpu list.)
What I couldn't decide, but seems to be inferred from some of fixes, was whether a double-terminator was needed.
(That might just be the attribute value.)
One of the related functions got fixed to zero fill a buffer in a loop because (AFICT) something was reading beyond a '\0' terminator.
This code does seem strange though.
It seems to add a text prefix here and then the called function does string compares to find out which prefix has added and than act differently based on the prefix.
I didn't try to follow things any further.
I also don't know if 'name' itself can be 127 wide characters (before the prefix is added)?
The fixed length buffer isn't long enough if all 127 characters require 6 bytes to encode.
-- David
Quoted 3 lines
Thanks,
Slava.
Viacheslav Dubeyko
maintainerQuoted 37 lines · David Laight
Quoted 13 lines · On Tue, 09 Jun 2026 18:04:46 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 11 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for
the
name to ensure it really doesnt overflow.Quoted 8 lines
Quoted 4 lines
Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".What do you mean by "the buffer isn't created just long enough"? And
what is your vision of correct implementation of the logic?The old code is:
Quoted 5 lines
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)return -ENOMEM; strcpy(xattr_name, prefix); strcpy(xattr_name + prefixlen, name);It would be more usual to do:
size_t name_len = strlen(name) + 1; xattr_name = kmalloc(prefixlen + name_len); memcpy(xattr_name, prefix, prefixlen) memcpy(xattr_name + prefixlen, name, name_len);
Quoted 5 lines
So that the buffer is allocated the correct size for the strings.
(Not that it really makes much difference whether you do kmalloc(32) or
kmalloc(1024) for a temporary buffer - both come of a per-cpu list.)
The xattr name cannot contain more than 127 symbols [1]. So, the buffer of fixed size is allocated to keep any potential string inside of this limit no matter of size of the prefix and name. This is because we must have string that not longer that 127 symbols. If it is longer than that, then something is going wrong. Following to your logic, there is no big difference to allocate the buffer for 127 symbols or for precise length of the name. So, your suggestion doesn't make sense to me.
Quoted 7 lines
What I couldn't decide, but seems to be inferred from some of fixes, was whether a double-terminator was needed.
(That might just be the attribute value.)
One of the related functions got fixed to zero fill a buffer in a loop
because (AFICT) something was reading beyond a '\0' terminator.
The rest is dedicated to Unicode peculiarities, as far as I can see.
Quoted 6 lines
This code does seem strange though.
It seems to add a text prefix here and then the called function does string compares to find out which prefix has added and than act differently based on the prefix.
I didn't try to follow things any further.
There is no strange thing here. Different prefixes are processed by different logic.
Quoted 7 lines
I also don't know if 'name' itself can be 127 wide characters (before the
prefix is added)?
The fixed length buffer isn't long enough if all 127 characters require
6 bytes to encode.
The whole string cannot be bigger than 127 symbols.
Thanks,
Slava.
[1] https://elixir.bootlin.com/linux/v7.1-rc6/source/include/linux/hfs_common.h#L78
Viacheslav Dubeyko
maintainerQuoted 46 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0
fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const · line 554
Quoted 7 lines
fs/hfsplus/xattr.c +1
-2@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode, constchar *name, xattr_name = kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name) return -ENOMEM; -strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen);
What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*() family of methods, then why is it correct to use for second operation?
fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const · line 556
Quoted 2 lines
fs/hfsplus/xattr.c +1
-0+strscpy (xattr_name + prefixlen, name, xattr_name_len - prefixlen);
Why strscpy() is better than strncpy()? What is the main argument here?
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 11 lines
fs/hfsplus/xattr.c +0
-0res = __hfsplus_setxattr(inode, xattr_name, value, size,flags);
kfree(xattr_name);fs/hfsplus/xattr.c +1
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 ;
Frankly speaking, it looks like a constant that should be declared in hfs_common.h. Even if we would like to declare it here, then it should be const size_t, from my point of view.
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 12 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );
Finally, I think kzalloc() should be much better for both cases.
Thanks,
Slava.
Quoted 11 lines
if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c +2
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); + strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
res =__hfsplus_getxattr(inode, xattr_name, value, size);kfree(xattr_name);
Quoted 80 lines · Viacheslav Dubeyko
Quoted 46 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const · line 554
Quoted 7 lines
fs/hfsplus/xattr.c +1
-2@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode, constchar *name, xattr_name = kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name) return -ENOMEM; -strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*() family of methods, then why is it correct to use for second operation?
fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const · line 556
Quoted 2 lines
fs/hfsplus/xattr.c +1
-0+strscpy (xattr_name + prefixlen, name, xattr_name_len - prefixlen);Why
strscpy()is better thanstrncpy()? What is the main argument here?fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 11 lines
fs/hfsplus/xattr.c +0
-0res = __hfsplus_setxattr(inode, xattr_name, value, size,flags);
kfree(xattr_name);fs/hfsplus/xattr.c +1
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 ;Frankly speaking, it looks like a constant that should be declared in
hfs_common.h. Even if we would like to declare it here, then it should be constsize_t, from my point of view.
Quoted 14 lines
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 12 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );Finally, I think
kzalloc()should be much better for both cases.
kasprintf()?
--D
Quoted 15 lines
Thanks,
Slava.Quoted 11 lines
if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c +2
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); + strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
res =__hfsplus_getxattr(inode, xattr_name, value, size);kfree(xattr_name);
Quoted 44 lines · On Wed, 10 Jun 2026 18:05:02 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 37 lines · David Laight
Quoted 13 lines · On Tue, 09 Jun 2026 18:04:46 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 11 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for
the
name to ensure it really doesnt overflow.Quoted 8 lines
Quoted 4 lines
Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".What do you mean by "the buffer isn't created just long enough"? And
what is your vision of correct implementation of the logic?The old code is:
Quoted 5 lines
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)return -ENOMEM; strcpy(xattr_name, prefix); strcpy(xattr_name + prefixlen, name);It would be more usual to do:
size_t name_len = strlen(name) + 1; xattr_name = kmalloc(prefixlen + name_len); memcpy(xattr_name, prefix, prefixlen) memcpy(xattr_name + prefixlen, name, name_len);Quoted 5 lines
So that the buffer is allocated the correct size for the strings.
(Not that it really makes much difference whether you do kmalloc(32) or
kmalloc(1024) for a temporary buffer - both come of a per-cpu list.)
Quoted 7 lines
The xattr name cannot contain more than 127 symbols [1]. So, the buffer of fixed size is allocated to keep any potential string inside of this limit no matter of size of the prefix and name. This is because we must have string that not longer that 127 symbols. If it is longer than that, then something is going wrong. Following to your logic, there is no big difference to allocate the buffer for 127 symbols or for precise length of the name. So, your suggestion doesn't make sense to me.
This code has no idea of the number of symbols.
Assuming that the callers haven't passed something that is overlong is just asking for trouble - especially since it doesn't matter to this code.
The caller is unlikely to know the length of the prefix - so may be passing in a 'name' that is nearly 127 symbols long.
So adding the prefix can generate something longer than 127 symbols. The called code has to handle/detect that.
Quoted 10 lines
Quoted 8 lines
What I couldn't decide, but seems to be inferred from some of fixes, was whether a double-terminator was needed.
(That might just be the attribute value.)
One of the related functions got fixed to zero fill a buffer in a loop
because (AFICT) something was reading beyond a '\0' terminator.The rest is dedicated to Unicode peculiarities, as far as I can see.
Eh? a UTF-8 encoded buffer is terminated by a single '\0'. The changes that stopped 'old values' being used on a later loop iteration seems to be fixed by zeroing after the '\0', but neither the code change nor commit message makes it clear what was fixed.
Quoted 10 lines
Quoted 7 lines
This code does seem strange though.
It seems to add a text prefix here and then the called function does string compares to find out which prefix has added and than act differently based on the prefix.
I didn't try to follow things any further.There is no strange thing here. Different prefixes are processed by different logic.
So why do string compares for the prefixes?
Quoted 10 lines
Quoted 8 lines
I also don't know if 'name' itself can be 127 wide characters (before the
prefix is added)?
The fixed length buffer isn't long enough if all 127 characters require
6 bytes to encode.The whole string cannot be bigger than 127 symbols.
'cannot' or 'must not'?
As I said above where is the actual guarantee that the limit isn't exceeded.
David
Quoted 5 lines
Thanks,
Slava.[1] https://elixir.bootlin.com/linux/v7.1-rc6/source/include/linux/hfs_common.h#L78
Quoted 42 lines · On Wed, 10 Jun 2026 20:50:33 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 41 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and then fixing the code by hand.
Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
Quoted 16 lines
fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const · line 554
Quoted 12 lines
fs/hfsplus/xattr.c +1
-2@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode, constchar *name, xattr_name = kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name) return -ENOMEM; -strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*() family of methods, then why is it correct to use for second operation?
They all just copy memory...memcpy() copies a number of bytes,strcpy() copies up to (and including) a zero byte.strscpy() copies up to a zero byte, but no more than the specified length
and always zero terminates the written data.
memcpy() is always going to be faster because it doesn't need to look at the data being copied.
Quoted 5 lines
fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const · line 556
Quoted 3 lines
fs/hfsplus/xattr.c +1
-0+strscpy (xattr_name + prefixlen, name, xattr_name_len - prefixlen);Why
strscpy()is better thanstrncpy()? What is the main argument here?
strncpy() is completely broken (but not as badly as strncat).
And, replying to the next email.
You really don't want to use kasprintf(), especially just to concatenate two strings.
Quoted 16 lines
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 12 lines
fs/hfsplus/xattr.c +0
-0res = __hfsplus_setxattr(inode, xattr_name, value, size,flags);
kfree(xattr_name);fs/hfsplus/xattr.c +1
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 ;Frankly speaking, it looks like a constant that should be declared in
hfs_common.h. Even if we would like to declare it here, then it should be constsize_t, from my point of view.
There is little point marking variables as 'const'.
Quoted 15 lines
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 13 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );Finally, I think
kzalloc()should be much better for both cases.
No point taking the cost of zeroing large amounts of memory you aren't going to access.
David
Quoted 15 lines
Thanks,
Slava.Quoted 11 lines
if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c +2
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen); + strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
res =__hfsplus_getxattr(inode, xattr_name, value, size);kfree(xattr_name);
Viacheslav Dubeyko
maintainerQuoted 81 lines · David Laight
Quoted 62 lines · On Wed, 10 Jun 2026 20:50:33 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 60 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for
the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been
called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and
then fixing the code by hand.Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions
as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode,const
char *name,
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c +0
-1-strcpy (xattr_name, prefix);Quoted 8 lines
fs/hfsplus/xattr.c
Quoted 2 lines
fs/hfsplus/xattr.c +1
-1-strcpy (xattr_name + prefixlen, name); + memcpy(xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*()
family of methods, then why is it correct to use for second operation?They all just copy memory...
memcpy()copies a number of bytes,strcpy()copies up to (and including) a zero byte.strscpy()copies up to a zero byte, but no more than the specified length
and always zero terminates the written data.
Quoted 2 lines
memcpy()is always going to be faster because it doesn't need to look at the data being copied.
You need to take into account that it is Unicode based symbols. I dislike to use memcpy() because prefixlen is only number of symbols but not size in bytes.
Frankly speaking, I dislike your approach in this patch. It is not safe enough.
Quoted 34 lines
Quoted 7 lines
Quoted 4 lines
fs/hfsplus/xattr.c +1
-0+ strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
Why
strscpy()is better thanstrncpy()? What is the main argument here?
strncpy()is completely broken (but not as badly as strncat).And, replying to the next email.
You really don't want to usekasprintf(), especially just to concatenate
two strings.Quoted 10 lines
Quoted 10 lines
res =
__hfsplus_setxattr(inode, xattr_name, value, size, flags);
kfree(xattr_name);fs/hfsplus/xattr.c +0
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) {Quoted 8 lines
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 2 lines
fs/hfsplus/xattr.c +1
-0+size_t xattr_name_len =NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1;Frankly speaking, it looks like a constant that should be declared in
hfs_common.h. Even if we would like to declare it here, then it should
be constsize_t, from my point of view.
Quoted 1 line
There is little point marking variables as 'const'.
This is why I am talking about declaration in hfs_common.h.
Quoted 18 lines
Quoted 17 lines
Quoted 13 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 2 lines
fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name = kmalloc(xattr_name_len, GFP_KERNEL);Finally, I think
kzalloc()should be much better for both cases.
Quoted 2 lines
No point taking the cost of zeroing large amounts of memory you aren't going to access.
What do you mean by huge? It's only 127 symbol in maximum. It's pretty nothing.
Thanks,
Slava.
Quoted 85 lines · On Thu, 11 Jun 2026 21:02:05 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 81 lines · David Laight
Quoted 62 lines · On Wed, 10 Jun 2026 20:50:33 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 60 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for
the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been
called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and
then fixing the code by hand.Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions
as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode,const
char *name,
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c +0
-1-strcpy (xattr_name, prefix);Quoted 8 lines
fs/hfsplus/xattr.c
Quoted 2 lines
fs/hfsplus/xattr.c +1
-1-strcpy (xattr_name + prefixlen, name); + memcpy(xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*()
family of methods, then why is it correct to use for second operation?They all just copy memory...
memcpy()copies a number of bytes,strcpy()copies up to (and including) a zero byte.strscpy()copies up to a zero byte, but no more than the specified length
and always zero terminates the written data.Quoted 2 lines
memcpy()is always going to be faster because it doesn't need to look at the data being copied.
Quoted 6 lines
You need to take into account that it is Unicode based symbols. I dislike to use
memcpy()because prefixlen is only number of symbols but not size in bytes.Frankly speaking, I dislike your approach in this patch. It is not safe enough.
The code relies on prefixlen being the number of bytes. It is the size used for the kmalloc().
The current strcpy() (for the prefix) relies on the caller passing in the correct size that matches the length.
Although the strings are Unicode it doesn't really matter here at all. They are UTF-8 encoded which means they can be processed as normal '\0' terminated C strings.
The only difficulty is that the length of the C string can be much longer than the number of Unicode characters.
Provided you make the use the lengths of the '\0' terminated strings everything is fine - which is what the change does.
The existing code just hopes that no one manages to pass in a string that is too long for the buffer.
Maybe it shouldn't happen - but any test is way earlier in the function call stack.
If there is a bug somewhere you've got a heap overrun.
You really do need to ensure that the copies use exactly the same lengths that were used for the kmalloc().
There is also the possibility that the strings can get changed by another thread (maybe you can prove it doesn't happen here, but it really is better to be safe) so you really need to only calculate the length once, strlen() shouldn't be followed by strcpy(). For absolute safety you need to use memcpy() and then write in the terminating '\0'.
David
Quoted 65 lines
Quoted 35 lines
Quoted 8 lines
Quoted 5 lines
fs/hfsplus/xattr.c +1
-0+ strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
Why
strscpy()is better thanstrncpy()? What is the main argument here?
strncpy()is completely broken (but not as badly as strncat).And, replying to the next email.
You really don't want to usekasprintf(), especially just to concatenate
two strings.Quoted 10 lines
Quoted 10 lines
res =
__hfsplus_setxattr(inode, xattr_name, value, size, flags);
kfree(xattr_name);fs/hfsplus/xattr.c +0
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) {Quoted 8 lines
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 2 lines
fs/hfsplus/xattr.c +1
-0+size_t xattr_name_len =NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1;Frankly speaking, it looks like a constant that should be declared in
hfs_common.h. Even if we would like to declare it here, then it should
be constsize_t, from my point of view.Quoted 1 line
There is little point marking variables as 'const'.
This is why I am talking about declaration in
hfs_common.h.Quoted 18 lines
Quoted 17 lines
Quoted 13 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 2 lines
fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name = kmalloc(xattr_name_len, GFP_KERNEL);Finally, I think
kzalloc()should be much better for both cases.Quoted 2 lines
No point taking the cost of zeroing large amounts of memory you aren't going to access.
What do you mean by huge? It's only 127 symbol in maximum. It's pretty nothing.
Thanks,
Slava.
Viacheslav Dubeyko
maintainerQuoted 112 lines · Darrick J. Wong
Quoted 95 lines · Viacheslav Dubeyko
Quoted 59 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for
the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been
called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and
then fixing the code by hand.Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions
as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode,const
char *name,
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const char *name, · line 553
Quoted 3 lines
fs/hfsplus/xattr.c +1
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*()
family of methods, then why is it correct to use for second operation?Quoted 2 lines
fs/hfsplus/xattr.c +1
-0+ strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
Why
strscpy()is better thanstrncpy()? What is the main argument here?fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 11 lines
res =
__hfsplus_setxattr(inode, xattr_name, value, size, flags);
kfree(xattr_name);fs/hfsplus/xattr.c +1
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 ;Frankly speaking, it looks like a constant that should be declared in
hfs_common.h. Even if we would like to declare it here, then it should
be constsize_t, from my point of view.Quoted 15 lines
fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 13 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );Finally, I think
kzalloc()should be much better for both cases.
Quoted 2 lines
kasprintf()?Quoted 1 line
It sounds much better than suggested fix.
Thanks,
Slava.
Quoted 113 lines · On Mon, 15 Jun 2026 23:33:09 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 112 lines · Darrick J. Wong
Quoted 110 lines · Viacheslav Dubeyko
Quoted 59 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()for
the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just been
called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.
The changes were made by getting the compiler to detect the calls and
then fixing the code by hand.Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy(). As well as 'difficult to fix' files, this included 'show' functions
as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode,const
char *name,
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const char *name, · line 553
Quoted 3 lines
fs/hfsplus/xattr.c +1
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); +memcpy (xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's wrong with str*() method here? Otherwise, if it is wrong to use str*()
family of methods, then why is it correct to use for second operation?Quoted 2 lines
fs/hfsplus/xattr.c +1
-0+ strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
Why
strscpy()is better thanstrncpy()? What is the main argument here?fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, · line 703
Quoted 11 lines
res =
__hfsplus_setxattr(inode, xattr_name, value, size, flags);
kfree(xattr_name);fs/hfsplus/xattr.c +1
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode,const char *name, void *value, size_t size, const char *prefix, size_t prefixlen) { + size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *HFSPLUS_ATTR_MAX_STRLEN +1 ;Frankly speaking, it looks like a constant that should be declared in
hfs_common.h. Even if we would like to declare it here, then it should
be constsize_t, from my point of view.fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 13 lines
fs/hfsplus/xattr.c +0
-0int res;char
*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name =kmalloc (xattr_name_len,GFP_KERNEL );Finally, I think
kzalloc()should be much better for both cases.
Quoted 4 lines
Quoted 2 lines
kasprintf()?Quoted 1 line
It sounds much better than suggested fix.
If performance matters here it will be a lot slower.
The snprintf() code itself is slow and kasprintf() has to do it twice. (As well as looking at the strings twice.)
It also only allocates a buffer that is big enough for a single terminating '\0' - and (at least some versions) of this code zero the rest of the buffer (possibly to avoid a bug).
One option would be something like kstrdup() that concatenates two strings.
David
Quoted 3 lines
Thanks,
Slava.
Viacheslav Dubeyko
maintainerQuoted 142 lines · David Laight
Quoted 129 lines · On Mon, 15 Jun 2026 23:33:09 -0700 Viacheslav Dubeyko <[email protected]> wrote:
Quoted 128 lines · Darrick J. Wong
Quoted 125 lines · Viacheslav Dubeyko
Quoted 66 lines · On Mon, 2026-06-08 at 10:55 +0100, [email protected] wrote:
From: David Laight <[email protected]>
xattr_nameis kmalloc()ed at the (assumed) maximal size and then
the
prefix
and name concatenated together.
Usememcpy()for the prefix - its length is passed andstrscpy()
for
the
name to ensure it really doesnt overflow.Prior to
bf29e886b242cthe buffers were smaller and on-stack. (But I cant see the copy in the old code.)
I am also not sure why the buffer isnt created "just long enough".Signed-off-by David Laight <[email protected]>---
This is one of a group of patches that remove potentially unboundedstrcpy()calls.They are mostly replaced by
strscpy()or, whenstrlen()has just
been
called, withmemcpy()(usually including the '\0').Calls with copy string literals into arrays are left unchanged.
They are safe and easily detected as such.The changes were made by getting the compiler to detect the calls
and
then fixing the code by hand.Note that all the changes are only compile tested.
Some Makefiles were changed to allow files to contain
strcpy().
As well as 'difficult to fix' files, this included 'show' functions
as they really need to usesysfs_emit()orseq_printf().All the patches are being sent individually to avoid very long cc
lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)fs/hfsplus/xattr.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)fs/hfsplus/xattr.c +0
-0@@ -550,8 +550,8 @@ int hfsplus_setxattr(struct inode *inode,const
char *name,
xattr_name=kmalloc(xattr_name_len, GFP_KERNEL); if (!xattr_name)
return-ENOMEM;fs/hfsplus/xattr.c int hfsplus_setxattr(struct inode *inode, const char *name, · line 553
Quoted 3 lines
fs/hfsplus/xattr.c +1
-2-strcpy (xattr_name, prefix); -strcpy (xattr_name + prefixlen, name); + memcpy(xattr_name, prefix, prefixlen);What's the point to mix memcpy and str*() family of methods? What's
wrong with str*() method here? Otherwise, if it is wrong to use str*()
family of methods, then why is it correct to use for second operation?Quoted 3 lines
fs/hfsplus/xattr.c +1
-1+ strscpy(xattr_name + prefixlen, name, xattr_name_len -prefixlen);
Why
strscpy()is better thanstrncpy()? What is the main argument
here?Quoted 13 lines
res = __hfsplus_setxattr(inode,
xattr_name, value, size,
flags);
kfree(xattr_name);fs/hfsplus/xattr.c +0
-0@@ -698,6 +698,7 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, void *value, size_t size,const char *prefix,
size_tprefixlen)
{fs/hfsplus/xattr.c +1
-0+ size_t xattr_name_len = NLS_MAX_CHARSET_SIZE *Quoted 1 line
HFSPLUS_ATTR_MAX_STRLEN+ 1;Frankly speaking, it looks like a constant that should be declared
inhfs_common.h. Even if we would like to declare it here, then it should
be constsize_t, from my point of view.fs/hfsplus/xattr.c ssize_t hfsplus_getxattr(struct inode *inode, const char *name, · line 709
Quoted 13 lines
int res;
char*xattr_name;fs/hfsplus/xattr.c +0
-0@@ -705,13 +706,12 @@ ssize_t hfsplus_getxattr(struct inode *inode, const char *name, inode->i_ino, name ? name : NULL, prefix ? prefix : NULL);fs/hfsplus/xattr.c +0
-1- xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE *
HFSPLUS_ATTR_MAX_STRLEN+ 1,fs/hfsplus/xattr.c +1
-1- GFP_KERNEL); + xattr_name = kmalloc(xattr_name_len, GFP_KERNEL);Finally, I think
kzalloc()should be much better for both cases.Quoted 4 lines
Quoted 2 lines
kasprintf()?Quoted 1 line
It sounds much better than suggested fix.
If performance matters here it will be a lot slower. The
snprintf()code itself is slow andkasprintf()has to do it twice.
(As well as looking at the strings twice.)
Quoted 8 lines
It also only allocates a buffer that is big enough for a single terminating '\0' - and (at least some versions) of this code zero the rest of the buffer (possibly to avoid a bug).
One option would be something like
kstrdup()that concatenates two strings.
We are talking about xattr operations that is not very frequent ones. Also, xattr names usually are not very long. So, it means that we need to manage small memory/string blobs.
From my point of view, the benefit of kasprintf() that we can combine the whole operation into one line of code. It sounds like nice cleanup to me. I don't think that kasprintf() can introduce a significant performance degradation for HFS+ xattr functionality support.
Thanks,
Slava.