diff mbox series

[V2] net: bpf: switch over to memdup_user()

Message ID 1634556651-38702-1-git-send-email-wangqing@vivo.com (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [V2] net: bpf: switch over to memdup_user() | expand

Checks

Context Check Description
netdev/cover_letter success Single patches do not need cover letters
netdev/fixes_present success Fixes tag not required for -next series
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 12 of 12 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/module_param success Was 0 now: 0
netdev/build_32bit fail Errors and warnings before: 17 this patch: 17
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success No Fixes tag
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 39 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 13 this patch: 13
netdev/header_inline success No static functions without inline keyword in header files
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf success VM_Test
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-next success VM_Test

Commit Message

王擎 Oct. 18, 2021, 11:30 a.m. UTC
This patch fixes the following Coccinelle warning:

net/bpf/test_run.c:361:8-15: WARNING opportunity for memdup_user
net/bpf/test_run.c:1055:8-15: WARNING opportunity for memdup_user

Use memdup_user rather than duplicating its implementation
This is a little bit restricted to reduce false positives

Signed-off-by: Qing Wang <wangqing@vivo.com>
---
 net/bpf/test_run.c | 21 ++++++---------------
 1 file changed, 6 insertions(+), 15 deletions(-)

Comments

John Fastabend Oct. 18, 2021, 4:26 p.m. UTC | #1
Qing Wang wrote:
> This patch fixes the following Coccinelle warning:
> 
> net/bpf/test_run.c:361:8-15: WARNING opportunity for memdup_user
> net/bpf/test_run.c:1055:8-15: WARNING opportunity for memdup_user
> 
> Use memdup_user rather than duplicating its implementation
> This is a little bit restricted to reduce false positives
> 
> Signed-off-by: Qing Wang <wangqing@vivo.com>
> ---
>  net/bpf/test_run.c | 21 ++++++---------------
>  1 file changed, 6 insertions(+), 15 deletions(-)

LGTM, but subject line should be '[PATCH bpf-next v2]' there is no reason
to push to fixes trees here.

Also might be worth noting that the original kzalloc could have just been
a kalloc because copy_from_user will zero any extra bytes.

Acked-by: John Fastabend <john.fastabend@gmail.com>

> 
> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> index 5296087..fbda8f5
> --- a/net/bpf/test_run.c
> +++ b/net/bpf/test_run.c
> @@ -358,13 +358,9 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
>  		return -EINVAL;
>  
>  	if (ctx_size_in) {
> -		info.ctx = kzalloc(ctx_size_in, GFP_USER);
> -		if (!info.ctx)
> -			return -ENOMEM;
> -		if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) {
> -			err = -EFAULT;
> -			goto out;
> -		}
> +		info.ctx = memdup_user(ctx_in, ctx_size_in);
> +		if (IS_ERR(info.ctx))
> +			return PTR_ERR(info.ctx);
>  	} else {
>  		info.ctx = NULL;
>  	}
> @@ -392,7 +388,6 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
>  	    copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
>  		err = -EFAULT;
>  
> -out:
>  	kfree(info.ctx);
>  	return err;
>  }
> @@ -1052,13 +1047,9 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog,
>  		return -EINVAL;
>  
>  	if (ctx_size_in) {
> -		ctx = kzalloc(ctx_size_in, GFP_USER);
> -		if (!ctx)
> -			return -ENOMEM;
> -		if (copy_from_user(ctx, ctx_in, ctx_size_in)) {
> -			err = -EFAULT;
> -			goto out;
> -		}
> +		ctx = memdup_user(ctx_in, ctx_size_in);
> +		if (IS_ERR(ctx))
> +			return PTR_ERR(ctx);
>  	}
>  
>  	rcu_read_lock_trace();
> -- 
> 2.7.4
>
diff mbox series

Patch

diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index 5296087..fbda8f5
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -358,13 +358,9 @@  int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
 		return -EINVAL;
 
 	if (ctx_size_in) {
-		info.ctx = kzalloc(ctx_size_in, GFP_USER);
-		if (!info.ctx)
-			return -ENOMEM;
-		if (copy_from_user(info.ctx, ctx_in, ctx_size_in)) {
-			err = -EFAULT;
-			goto out;
-		}
+		info.ctx = memdup_user(ctx_in, ctx_size_in);
+		if (IS_ERR(info.ctx))
+			return PTR_ERR(info.ctx);
 	} else {
 		info.ctx = NULL;
 	}
@@ -392,7 +388,6 @@  int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
 	    copy_to_user(&uattr->test.retval, &info.retval, sizeof(u32)))
 		err = -EFAULT;
 
-out:
 	kfree(info.ctx);
 	return err;
 }
@@ -1052,13 +1047,9 @@  int bpf_prog_test_run_syscall(struct bpf_prog *prog,
 		return -EINVAL;
 
 	if (ctx_size_in) {
-		ctx = kzalloc(ctx_size_in, GFP_USER);
-		if (!ctx)
-			return -ENOMEM;
-		if (copy_from_user(ctx, ctx_in, ctx_size_in)) {
-			err = -EFAULT;
-			goto out;
-		}
+		ctx = memdup_user(ctx_in, ctx_size_in);
+		if (IS_ERR(ctx))
+			return PTR_ERR(ctx);
 	}
 
 	rcu_read_lock_trace();