2021-03-04 05:54:29

by Yang Li

[permalink] [raw]
Subject: [PATCH] xattr: switch to vmemdup_user()

Replace opencoded alloc and copy with vmemdup_user()

fixed the following coccicheck:
./fs/xattr.c:561:11-19: WARNING opportunity for vmemdup_user

Reported-by: Abaci Robot <[email protected]>
Signed-off-by: Yang Li <[email protected]>
---
fs/xattr.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/fs/xattr.c b/fs/xattr.c
index b3444e0..b947ad2 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -558,11 +558,10 @@ int __vfs_setxattr_noperm(struct user_namespace *mnt_userns,
if (size) {
if (size > XATTR_SIZE_MAX)
return -E2BIG;
- kvalue = kvmalloc(size, GFP_KERNEL);
- if (!kvalue)
- return -ENOMEM;
- if (copy_from_user(kvalue, value, size)) {
- error = -EFAULT;
+ kvalue = vmemdup_user(value, size);
+
+ if (IS_ERR(kvalue)) {
+ r = PTR_ERR(kvalue);
goto out;
}
if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
--
1.8.3.1


2021-03-05 00:12:05

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH] xattr: switch to vmemdup_user()

On Tue, Mar 02, 2021 at 02:32:21PM +0800, Yang Li wrote:
> Replace opencoded alloc and copy with vmemdup_user()
>
> fixed the following coccicheck:
> ./fs/xattr.c:561:11-19: WARNING opportunity for vmemdup_user
>
> Reported-by: Abaci Robot <[email protected]>
> Signed-off-by: Yang Li <[email protected]>

Reviewed-by: Matthew Wilcox (Oracle) <[email protected]>

2021-09-29 22:29:59

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] xattr: switch to vmemdup_user()

Hi Yang,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.15-rc3 next-20210922]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Yang-Li/xattr-switch-to-vmemdup_user/20210929-221309
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git a4e6f95a891ac08bd09d62e3e6dae239b150f4c1
config: hexagon-randconfig-r045-20210929 (attached as .config)
compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project dc6e8dfdfe7efecfda318d43a06fae18b40eb498)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/c26396dd8ad04ac32a6dee98d97706c53432ee2f
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Yang-Li/xattr-switch-to-vmemdup_user/20210929-221309
git checkout c26396dd8ad04ac32a6dee98d97706c53432ee2f
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

>> fs/xattr.c:566:4: error: use of undeclared identifier 'r'
r = PTR_ERR(kvalue);
^
1 error generated.


vim +/r +566 fs/xattr.c

538
539 /*
540 * Extended attribute SET operations
541 */
542 static long
543 setxattr(struct user_namespace *mnt_userns, struct dentry *d,
544 const char __user *name, const void __user *value, size_t size,
545 int flags)
546 {
547 int error;
548 void *kvalue = NULL;
549 char kname[XATTR_NAME_MAX + 1];
550
551 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
552 return -EINVAL;
553
554 error = strncpy_from_user(kname, name, sizeof(kname));
555 if (error == 0 || error == sizeof(kname))
556 error = -ERANGE;
557 if (error < 0)
558 return error;
559
560 if (size) {
561 if (size > XATTR_SIZE_MAX)
562 return -E2BIG;
563 kvalue = vmemdup_user(value, size);
564
565 if (IS_ERR(kvalue)) {
> 566 r = PTR_ERR(kvalue);
567 goto out;
568 }
569 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
570 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
571 posix_acl_fix_xattr_from_user(mnt_userns, kvalue, size);
572 }
573
574 error = vfs_setxattr(mnt_userns, d, kname, kvalue, size, flags);
575 out:
576 kvfree(kvalue);
577
578 return error;
579 }
580

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (3.11 kB)
.config.gz (31.02 kB)
Download all attachments

2021-09-29 22:52:53

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH] xattr: switch to vmemdup_user()

Hi Yang,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v5.15-rc3 next-20210922]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Yang-Li/xattr-switch-to-vmemdup_user/20210929-221309
base: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git a4e6f95a891ac08bd09d62e3e6dae239b150f4c1
config: x86_64-randconfig-c001-20210929 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/c26396dd8ad04ac32a6dee98d97706c53432ee2f
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Yang-Li/xattr-switch-to-vmemdup_user/20210929-221309
git checkout c26396dd8ad04ac32a6dee98d97706c53432ee2f
# save the attached .config to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=x86_64 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>

All errors (new ones prefixed by >>):

fs/xattr.c: In function 'setxattr':
>> fs/xattr.c:566:4: error: 'r' undeclared (first use in this function)
566 | r = PTR_ERR(kvalue);
| ^
fs/xattr.c:566:4: note: each undeclared identifier is reported only once for each function it appears in


vim +/r +566 fs/xattr.c

538
539 /*
540 * Extended attribute SET operations
541 */
542 static long
543 setxattr(struct user_namespace *mnt_userns, struct dentry *d,
544 const char __user *name, const void __user *value, size_t size,
545 int flags)
546 {
547 int error;
548 void *kvalue = NULL;
549 char kname[XATTR_NAME_MAX + 1];
550
551 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
552 return -EINVAL;
553
554 error = strncpy_from_user(kname, name, sizeof(kname));
555 if (error == 0 || error == sizeof(kname))
556 error = -ERANGE;
557 if (error < 0)
558 return error;
559
560 if (size) {
561 if (size > XATTR_SIZE_MAX)
562 return -E2BIG;
563 kvalue = vmemdup_user(value, size);
564
565 if (IS_ERR(kvalue)) {
> 566 r = PTR_ERR(kvalue);
567 goto out;
568 }
569 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
570 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
571 posix_acl_fix_xattr_from_user(mnt_userns, kvalue, size);
572 }
573
574 error = vfs_setxattr(mnt_userns, d, kname, kvalue, size, flags);
575 out:
576 kvfree(kvalue);
577
578 return error;
579 }
580

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]


Attachments:
(No filename) (2.96 kB)
.config.gz (32.15 kB)
Download all attachments