mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: cuigaosheng <cuigaosheng1@huawei.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>, Martin Lau <kafai@fb.com>,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	john fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	open list <linux-kernel@vger.kernel.org>, <gongruiqi1@huawei.com>,
	<wangweiyang2@huawei.com>
Subject: Re: [PATCH -next] libbpf: Add additional null-pointer checking in make_parent_dir
Date: Fri, 22 Apr 2022 10:45:08 +0800	[thread overview]
Message-ID: <45fdf83c-7492-1c31-af8d-7b2c9bdaf5a7@huawei.com> (raw)
In-Reply-To: <CAEf4Bza3inoAHsS0w=nKXNgxyFqzPXJVyDHq03Foody6Vgp7=Q@mail.gmail.com>

I don't understand why we don't check path for NULL, bpf_link__pin is an external
interface, It will be called by external functions and provide input parameters,
for example in samples/bpf/hbm.c:
> 201 link = bpf_program__attach_cgroup(bpf_prog, cg1); 202 if 
> (libbpf_get_error(link)) { 203 fprintf(stderr, "ERROR: 
> bpf_program__attach_cgroup failed\n"); 204 goto err; 205 } 206 207 
> sprintf(cg_pin_path, "/sys/fs/bpf/hbm%d", cg_id); 208 rc = 
> bpf_link__pin(link, cg_pin_path); 209 if (rc < 0) { 210 printf("ERROR: 
> bpf_link__pin failed: %d\n", rc); 211 goto err; 212 }
if cg_pin_path is NULL, strdup(NULL) will trigger a segmentation fault in
make_parent_dir, I think we should avoid this and add null-pointer checking
for path, just like check_path:
> 7673 static int check_path(const char *path) 7674 { 7675 char *cp, 
> errmsg[STRERR_BUFSIZE]; 7676 struct statfs st_fs; 7677 char *dname, 
> *dir; 7678 int err = 0; 7679 7680 if (path == NULL) 7681 return 
> -EINVAL; 7682 7683 dname = strdup(path); 7684 if (dname == NULL) 7685 
> return -ENOMEM; 7686 7687 dir = dirname(dname); 7688 if (statfs(dir, 
> &st_fs)) { 7689 cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); 
> 7690 pr_warn("failed to statfs %s: %s\n", dir, cp); 7691 err = -errno; 
> 7692 } 7693 free(dname); 7694 7695 if (!err && st_fs.f_type != 
> BPF_FS_MAGIC) { 7696 pr_warn("specified path %s is not on BPF FS\n", 
> path); 7697 err = -EINVAL; 7698 } 7699 7700 return err; 7701 }

Thanks.

在 2022/4/22 0:55, Andrii Nakryiko 写道:
> On Thu, Apr 21, 2022 at 6:01 AM Gaosheng Cui <cuigaosheng1@huawei.com> wrote:
>> The make_parent_dir is called without null-pointer checking for path,
>> such as bpf_link__pin. To ensure there is no null-pointer dereference
>> in make_parent_dir, so make_parent_dir requires additional null-pointer
>> checking for path.
>>
>> Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
>> ---
>>   tools/lib/bpf/libbpf.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index b53e51884f9e..5786e6184bf5 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
>> @@ -7634,6 +7634,9 @@ static int make_parent_dir(const char *path)
>>          char *dname, *dir;
>>          int err = 0;
>>
>> +       if (path == NULL)
>> +               return -EINVAL;
>> +
> API contract is that path shouldn't be NULL. Just like we don't check
> link, obj, prog for NULL in every single API, I don't think we need to
> do it here, unless I'm missing something?
>
>>          dname = strdup(path);
>>          if (dname == NULL)
>>                  return -ENOMEM;
>> --
>> 2.25.1
>>
> .

  reply	other threads:[~2022-04-22  2:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-21 13:00 Gaosheng Cui
2022-04-21 16:55 ` Andrii Nakryiko
2022-04-22  2:45   ` cuigaosheng [this message]
2022-04-22  2:55   ` cuigaosheng
2022-04-22 21:46     ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45fdf83c-7492-1c31-af8d-7b2c9bdaf5a7@huawei.com \
    --to=cuigaosheng1@huawei.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=gongruiqi1@huawei.com \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=wangweiyang2@huawei.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®