diff mbox series

lsm: convert security_setselfattr() to use memdup_user()

Message ID 20231102015354.511078-2-paul@paul-moore.com (mailing list archive)
State Accepted
Delegated to: Paul Moore
Headers show
Series lsm: convert security_setselfattr() to use memdup_user() | expand

Commit Message

Paul Moore Nov. 2, 2023, 1:53 a.m. UTC
As suggested by the kernel test robot, memdup_user() is a better
option than the combo of kmalloc()/copy_from_user().

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202310270805.2ArE52i5-lkp@intel.com/
Signed-off-by: Paul Moore <paul@paul-moore.com>
---
 security/security.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Comments

Casey Schaufler Nov. 2, 2023, 4:42 p.m. UTC | #1
On 11/1/2023 6:53 PM, Paul Moore wrote:
> As suggested by the kernel test robot, memdup_user() is a better
> option than the combo of kmalloc()/copy_from_user().
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202310270805.2ArE52i5-lkp@intel.com/
> Signed-off-by: Paul Moore <paul@paul-moore.com>

Acked-by: Casey Schaufler <casey@schaufler-ca.com>

> ---
>  security/security.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
>
> diff --git a/security/security.c b/security/security.c
> index a808fd5eba6d..d7b15ea67c3f 100644
> --- a/security/security.c
> +++ b/security/security.c
> @@ -4011,14 +4011,9 @@ int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
>  	if (size > PAGE_SIZE)
>  		return -E2BIG;
>  
> -	lctx = kmalloc(size, GFP_KERNEL);
> -	if (lctx == NULL)
> -		return -ENOMEM;
> -
> -	if (copy_from_user(lctx, uctx, size)) {
> -		rc = -EFAULT;
> -		goto free_out;
> -	}
> +	lctx = memdup_user(uctx, size);
> +	if (IS_ERR(lctx))
> +		return PTR_ERR(lctx);
>  
>  	if (size < lctx->len || size < lctx->ctx_len + sizeof(*lctx) ||
>  	    lctx->len < lctx->ctx_len + sizeof(*lctx)) {
Paul Moore Nov. 5, 2023, 11:24 p.m. UTC | #2
On Wed, Nov 1, 2023 at 9:54 PM Paul Moore <paul@paul-moore.com> wrote:
>
> As suggested by the kernel test robot, memdup_user() is a better
> option than the combo of kmalloc()/copy_from_user().
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202310270805.2ArE52i5-lkp@intel.com/
> Signed-off-by: Paul Moore <paul@paul-moore.com>
> ---
>  security/security.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)

Merged into lsm/dev-staging.
Paul Moore Nov. 13, 2023, 4:09 a.m. UTC | #3
On Wed, Nov 1, 2023 at 9:54 PM Paul Moore <paul@paul-moore.com> wrote:
>
> As suggested by the kernel test robot, memdup_user() is a better
> option than the combo of kmalloc()/copy_from_user().
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202310270805.2ArE52i5-lkp@intel.com/
> Signed-off-by: Paul Moore <paul@paul-moore.com>
> ---
>  security/security.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)

Merged into lsm/dev.
diff mbox series

Patch

diff --git a/security/security.c b/security/security.c
index a808fd5eba6d..d7b15ea67c3f 100644
--- a/security/security.c
+++ b/security/security.c
@@ -4011,14 +4011,9 @@  int security_setselfattr(unsigned int attr, struct lsm_ctx __user *uctx,
 	if (size > PAGE_SIZE)
 		return -E2BIG;
 
-	lctx = kmalloc(size, GFP_KERNEL);
-	if (lctx == NULL)
-		return -ENOMEM;
-
-	if (copy_from_user(lctx, uctx, size)) {
-		rc = -EFAULT;
-		goto free_out;
-	}
+	lctx = memdup_user(uctx, size);
+	if (IS_ERR(lctx))
+		return PTR_ERR(lctx);
 
 	if (size < lctx->len || size < lctx->ctx_len + sizeof(*lctx) ||
 	    lctx->len < lctx->ctx_len + sizeof(*lctx)) {