bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference
@ 2019-07-02 10:25 Leo Yan
  2019-07-02 15:34 ` Yonghong Song
  2019-07-03 10:23 ` Daniel Borkmann
  0 siblings, 2 replies; 4+ messages in thread
From: Leo Yan @ 2019-07-02 10:25 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau, Song Liu,
	Yonghong Song, netdev, bpf, linux-kernel, Dan Carpenter
  Cc: Leo Yan

Based on the following report from Smatch, fix the potential
NULL pointer dereference check.

  tools/lib/bpf/libbpf.c:3493
  bpf_prog_load_xattr() warn: variable dereferenced before check 'attr'
  (see line 3483)

3479 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
3480                         struct bpf_object **pobj, int *prog_fd)
3481 {
3482         struct bpf_object_open_attr open_attr = {
3483                 .file           = attr->file,
3484                 .prog_type      = attr->prog_type,
                                       ^^^^^^
3485         };

At the head of function, it directly access 'attr' without checking if
it's NULL pointer.  This patch moves the values assignment after
validating 'attr' and 'attr->file'.

Signed-off-by: Leo Yan <leo.yan@linaro.org>
---
 tools/lib/bpf/libbpf.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 197b574406b3..809b633fa3d9 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -3479,10 +3479,7 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type,
 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
 			struct bpf_object **pobj, int *prog_fd)
 {
-	struct bpf_object_open_attr open_attr = {
-		.file		= attr->file,
-		.prog_type	= attr->prog_type,
-	};
+	struct bpf_object_open_attr open_attr;
 	struct bpf_program *prog, *first_prog = NULL;
 	enum bpf_attach_type expected_attach_type;
 	enum bpf_prog_type prog_type;
@@ -3495,6 +3492,9 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
 	if (!attr->file)
 		return -EINVAL;
 
+	open_attr.file = attr->file;
+	open_attr.prog_type = attr->prog_type;
+
 	obj = bpf_object__open_xattr(&open_attr);
 	if (IS_ERR_OR_NULL(obj))
 		return -ENOENT;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference
  2019-07-02 10:25 [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference Leo Yan
@ 2019-07-02 15:34 ` Yonghong Song
  2019-07-03 10:23 ` Daniel Borkmann
  1 sibling, 0 replies; 4+ messages in thread
From: Yonghong Song @ 2019-07-02 15:34 UTC (permalink / raw)
  To: Leo Yan, Alexei Starovoitov, Daniel Borkmann, Martin Lau,
	Song Liu, netdev, bpf, linux-kernel, Dan Carpenter



On 7/2/19 3:25 AM, Leo Yan wrote:
> Based on the following report from Smatch, fix the potential
> NULL pointer dereference check.
> 
>    tools/lib/bpf/libbpf.c:3493
>    bpf_prog_load_xattr() warn: variable dereferenced before check 'attr'
>    (see line 3483)
> 
> 3479 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
> 3480                         struct bpf_object **pobj, int *prog_fd)
> 3481 {
> 3482         struct bpf_object_open_attr open_attr = {
> 3483                 .file           = attr->file,
> 3484                 .prog_type      = attr->prog_type,
>                                         ^^^^^^
> 3485         };
> 
> At the head of function, it directly access 'attr' without checking if
> it's NULL pointer.  This patch moves the values assignment after
> validating 'attr' and 'attr->file'.
> 
> Signed-off-by: Leo Yan <leo.yan@linaro.org>

Acked-by: Yonghong Song <yhs@fb.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference
  2019-07-02 10:25 [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference Leo Yan
  2019-07-02 15:34 ` Yonghong Song
@ 2019-07-03 10:23 ` Daniel Borkmann
  2019-07-03 11:46   ` Leo Yan
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Borkmann @ 2019-07-03 10:23 UTC (permalink / raw)
  To: Leo Yan, Alexei Starovoitov, Martin KaFai Lau, Song Liu,
	Yonghong Song, netdev, bpf, linux-kernel, Dan Carpenter

On 07/02/2019 12:25 PM, Leo Yan wrote:
> Based on the following report from Smatch, fix the potential
> NULL pointer dereference check.
> 
>   tools/lib/bpf/libbpf.c:3493
>   bpf_prog_load_xattr() warn: variable dereferenced before check 'attr'
>   (see line 3483)
> 
> 3479 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
> 3480                         struct bpf_object **pobj, int *prog_fd)
> 3481 {
> 3482         struct bpf_object_open_attr open_attr = {
> 3483                 .file           = attr->file,
> 3484                 .prog_type      = attr->prog_type,
>                                        ^^^^^^
> 3485         };
> 
> At the head of function, it directly access 'attr' without checking if
> it's NULL pointer.  This patch moves the values assignment after
> validating 'attr' and 'attr->file'.
> 
> Signed-off-by: Leo Yan <leo.yan@linaro.org>
> ---
>  tools/lib/bpf/libbpf.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 197b574406b3..809b633fa3d9 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -3479,10 +3479,7 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type,
>  int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
>  			struct bpf_object **pobj, int *prog_fd)
>  {
> -	struct bpf_object_open_attr open_attr = {
> -		.file		= attr->file,
> -		.prog_type	= attr->prog_type,
> -	};

Applied, thanks! Fyi, I retained the zeroing of open_attr as otherwise if we ever
extend struct bpf_object_open_attr in future, we'll easily miss this and pass in
garbage to bpf_object__open_xattr().

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference
  2019-07-03 10:23 ` Daniel Borkmann
@ 2019-07-03 11:46   ` Leo Yan
  0 siblings, 0 replies; 4+ messages in thread
From: Leo Yan @ 2019-07-03 11:46 UTC (permalink / raw)
  To: Daniel Borkmann
  Cc: Alexei Starovoitov, Martin KaFai Lau, Song Liu, Yonghong Song,
	netdev, bpf, linux-kernel, Dan Carpenter

On Wed, Jul 03, 2019 at 12:23:05PM +0200, Daniel Borkmann wrote:
> On 07/02/2019 12:25 PM, Leo Yan wrote:
> > Based on the following report from Smatch, fix the potential
> > NULL pointer dereference check.
> > 
> >   tools/lib/bpf/libbpf.c:3493
> >   bpf_prog_load_xattr() warn: variable dereferenced before check 'attr'
> >   (see line 3483)
> > 
> > 3479 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
> > 3480                         struct bpf_object **pobj, int *prog_fd)
> > 3481 {
> > 3482         struct bpf_object_open_attr open_attr = {
> > 3483                 .file           = attr->file,
> > 3484                 .prog_type      = attr->prog_type,
> >                                        ^^^^^^
> > 3485         };
> > 
> > At the head of function, it directly access 'attr' without checking if
> > it's NULL pointer.  This patch moves the values assignment after
> > validating 'attr' and 'attr->file'.
> > 
> > Signed-off-by: Leo Yan <leo.yan@linaro.org>
> > ---
> >  tools/lib/bpf/libbpf.c | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index 197b574406b3..809b633fa3d9 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -3479,10 +3479,7 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type,
> >  int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
> >  			struct bpf_object **pobj, int *prog_fd)
> >  {
> > -	struct bpf_object_open_attr open_attr = {
> > -		.file		= attr->file,
> > -		.prog_type	= attr->prog_type,
> > -	};
> 
> Applied, thanks! Fyi, I retained the zeroing of open_attr as otherwise if we ever
> extend struct bpf_object_open_attr in future, we'll easily miss this and pass in
> garbage to bpf_object__open_xattr().

Thanks for the info, Daniel.

I checked the link [1] and thanks for the improvement when applied this
patch.

Thanks,
Leo Yan

[1] https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/commit/?id=33bae185f74d49a0d7b1bfaafb8e959efce0f243

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-07-03 11:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-02 10:25 [PATCH] bpf, libbpf: Smatch: Fix potential NULL pointer dereference Leo Yan
2019-07-02 15:34 ` Yonghong Song
2019-07-03 10:23 ` Daniel Borkmann
2019-07-03 11:46   ` Leo Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).