mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH bpf-next v2] bpftool: Check map name length when map create
@ 2025-02-11 10:38 Rong Tao
  2025-02-11 10:48 ` Quentin Monnet
  0 siblings, 1 reply; 4+ messages in thread
From: Rong Tao @ 2025-02-11 10:38 UTC (permalink / raw)
  To: qmo, ast, daniel
  Cc: rongtao, rtoax, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	open list:BPF [TOOLING] (bpftool),
	open list

From: Rong Tao <rongtao@cestc.cn>

The size of struct bpf_map::name is BPF_OBJ_NAME_LEN (16).

bpf(2) {
  map_create() {
    bpf_obj_name_cpy(map->name, attr->map_name, sizeof(attr->map_name));
  }
}

When specifying a map name using bpftool map create name, no error is
reported if the name length is greater than 15.

    $ sudo bpftool map create /sys/fs/bpf/12345678901234567890 \
        type array key 4 value 4 entries 5 name 12345678901234567890

Users will think that 12345678901234567890 is legal, but this name cannot
be used to index a map.

    $ sudo bpftool map show name 12345678901234567890
    Error: can't parse name

    $ sudo bpftool map show
    ...
    1249: array  name 123456789012345  flags 0x0
    	key 4B  value 4B  max_entries 5  memlock 304B

    $ sudo bpftool map show name 123456789012345
    1249: array  name 123456789012345  flags 0x0
    	key 4B  value 4B  max_entries 5  memlock 304B

The map name provided in the command line is truncated, but no error is
reported. This submission checks the length of the map name.

Signed-off-by: Rong Tao <rongtao@cestc.cn>
---
 tools/bpf/bpftool/map.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index ed4a9bd82931..bd5df44e2420 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -1270,6 +1270,11 @@ static int do_create(int argc, char **argv)
 		} else if (is_prefix(*argv, "name")) {
 			NEXT_ARG();
 			map_name = GET_ARG();
+			if (strlen(map_name) > BPF_OBJ_NAME_LEN - 1) {
+				p_err("map name must be no longer than %d characters\n",
+				      BPF_OBJ_NAME_LEN - 1);
+				goto exit;
+			}
 		} else if (is_prefix(*argv, "key")) {
 			if (parse_u32_arg(&argc, &argv, &key_size,
 					  "key size"))
-- 
2.48.1


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

* Re: [PATCH bpf-next v2] bpftool: Check map name length when map create
  2025-02-11 10:38 [PATCH bpf-next v2] bpftool: Check map name length when map create Rong Tao
@ 2025-02-11 10:48 ` Quentin Monnet
  2025-02-11 21:07   ` Andrii Nakryiko
  0 siblings, 1 reply; 4+ messages in thread
From: Quentin Monnet @ 2025-02-11 10:48 UTC (permalink / raw)
  To: Rong Tao, ast, daniel
  Cc: rongtao, Andrii Nakryiko, Martin KaFai Lau, Eduard Zingerman,
	Song Liu, Yonghong Song, John Fastabend, KP Singh,
	Stanislav Fomichev, Hao Luo, Jiri Olsa,
	open list:BPF [TOOLING] (bpftool),
	open list

2025-02-11 18:38 UTC+0800 ~ Rong Tao <rtoax@foxmail.com>
> From: Rong Tao <rongtao@cestc.cn>
> 
> The size of struct bpf_map::name is BPF_OBJ_NAME_LEN (16).
> 
> bpf(2) {
>   map_create() {
>     bpf_obj_name_cpy(map->name, attr->map_name, sizeof(attr->map_name));
>   }
> }
> 
> When specifying a map name using bpftool map create name, no error is
> reported if the name length is greater than 15.
> 
>     $ sudo bpftool map create /sys/fs/bpf/12345678901234567890 \
>         type array key 4 value 4 entries 5 name 12345678901234567890
> 
> Users will think that 12345678901234567890 is legal, but this name cannot
> be used to index a map.
> 
>     $ sudo bpftool map show name 12345678901234567890
>     Error: can't parse name
> 
>     $ sudo bpftool map show
>     ...
>     1249: array  name 123456789012345  flags 0x0
>     	key 4B  value 4B  max_entries 5  memlock 304B
> 
>     $ sudo bpftool map show name 123456789012345
>     1249: array  name 123456789012345  flags 0x0
>     	key 4B  value 4B  max_entries 5  memlock 304B
> 
> The map name provided in the command line is truncated, but no error is
> reported. This submission checks the length of the map name.
> 
> Signed-off-by: Rong Tao <rongtao@cestc.cn>


Reviewed-by: Quentin Monnet <qmo@kernel.org>

Thank you!

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

* Re: [PATCH bpf-next v2] bpftool: Check map name length when map create
  2025-02-11 10:48 ` Quentin Monnet
@ 2025-02-11 21:07   ` Andrii Nakryiko
  2025-02-12  1:14     ` Rong Tao
  0 siblings, 1 reply; 4+ messages in thread
From: Andrii Nakryiko @ 2025-02-11 21:07 UTC (permalink / raw)
  To: Quentin Monnet
  Cc: Rong Tao, ast, daniel, rongtao, Andrii Nakryiko,
	Martin KaFai Lau, Eduard Zingerman, Song Liu, Yonghong Song,
	John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	open list:BPF [TOOLING] (bpftool),
	open list

On Tue, Feb 11, 2025 at 2:48 AM Quentin Monnet <qmo@kernel.org> wrote:
>
> 2025-02-11 18:38 UTC+0800 ~ Rong Tao <rtoax@foxmail.com>
> > From: Rong Tao <rongtao@cestc.cn>
> >
> > The size of struct bpf_map::name is BPF_OBJ_NAME_LEN (16).
> >
> > bpf(2) {
> >   map_create() {
> >     bpf_obj_name_cpy(map->name, attr->map_name, sizeof(attr->map_name));
> >   }
> > }
> >
> > When specifying a map name using bpftool map create name, no error is
> > reported if the name length is greater than 15.
> >
> >     $ sudo bpftool map create /sys/fs/bpf/12345678901234567890 \
> >         type array key 4 value 4 entries 5 name 12345678901234567890
> >
> > Users will think that 12345678901234567890 is legal, but this name cannot
> > be used to index a map.
> >
> >     $ sudo bpftool map show name 12345678901234567890
> >     Error: can't parse name
> >
> >     $ sudo bpftool map show
> >     ...
> >     1249: array  name 123456789012345  flags 0x0
> >       key 4B  value 4B  max_entries 5  memlock 304B
> >
> >     $ sudo bpftool map show name 123456789012345
> >     1249: array  name 123456789012345  flags 0x0
> >       key 4B  value 4B  max_entries 5  memlock 304B
> >
> > The map name provided in the command line is truncated, but no error is
> > reported. This submission checks the length of the map name.
> >
> > Signed-off-by: Rong Tao <rongtao@cestc.cn>
>
>
> Reviewed-by: Quentin Monnet <qmo@kernel.org>
>

Would it make sense to just warn but proceed with a truncated name?
libbpf truncates the name when creating a map, but preserves the
original name in BTF (and in memory, fetchable through
bpf_map__name()). So from the user's perspective that map is still
named "blah-blah-something-long", even if the kernel records just a
prefix of that.

Basically, instead of forcing users to count the first 15 characters,
warn, but do the right thing anyways?

> Thank you!

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

* Re: [PATCH bpf-next v2] bpftool: Check map name length when map create
  2025-02-11 21:07   ` Andrii Nakryiko
@ 2025-02-12  1:14     ` Rong Tao
  0 siblings, 0 replies; 4+ messages in thread
From: Rong Tao @ 2025-02-12  1:14 UTC (permalink / raw)
  To: Andrii Nakryiko, Quentin Monnet
  Cc: ast, daniel, rongtao, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, John Fastabend,
	KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
	open list:BPF [TOOLING] (bpftool),
	open list


On 2/12/25 05:07, Andrii Nakryiko wrote:
> On Tue, Feb 11, 2025 at 2:48 AM Quentin Monnet <qmo@kernel.org> wrote:
>> 2025-02-11 18:38 UTC+0800 ~ Rong Tao <rtoax@foxmail.com>
>>> From: Rong Tao <rongtao@cestc.cn>
>>>
>>> The size of struct bpf_map::name is BPF_OBJ_NAME_LEN (16).
>>>
>>> bpf(2) {
>>>    map_create() {
>>>      bpf_obj_name_cpy(map->name, attr->map_name, sizeof(attr->map_name));
>>>    }
>>> }
>>>
>>> When specifying a map name using bpftool map create name, no error is
>>> reported if the name length is greater than 15.
>>>
>>>      $ sudo bpftool map create /sys/fs/bpf/12345678901234567890 \
>>>          type array key 4 value 4 entries 5 name 12345678901234567890
>>>
>>> Users will think that 12345678901234567890 is legal, but this name cannot
>>> be used to index a map.
>>>
>>>      $ sudo bpftool map show name 12345678901234567890
>>>      Error: can't parse name
>>>
>>>      $ sudo bpftool map show
>>>      ...
>>>      1249: array  name 123456789012345  flags 0x0
>>>        key 4B  value 4B  max_entries 5  memlock 304B
>>>
>>>      $ sudo bpftool map show name 123456789012345
>>>      1249: array  name 123456789012345  flags 0x0
>>>        key 4B  value 4B  max_entries 5  memlock 304B
>>>
>>> The map name provided in the command line is truncated, but no error is
>>> reported. This submission checks the length of the map name.
>>>
>>> Signed-off-by: Rong Tao <rongtao@cestc.cn>
>>
>> Reviewed-by: Quentin Monnet <qmo@kernel.org>
>>
> Would it make sense to just warn but proceed with a truncated name?
> libbpf truncates the name when creating a map, but preserves the
> original name in BTF (and in memory, fetchable through
> bpf_map__name()). So from the user's perspective that map is still
> named "blah-blah-something-long", even if the kernel records just a
> prefix of that.
>
> Basically, instead of forcing users to count the first 15 characters,
> warn, but do the right thing anyways?

Yes, you're right, i'll submit v3.

Rong Tao

>
>> Thank you!


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

end of thread, other threads:[~2025-02-12  1:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-11 10:38 [PATCH bpf-next v2] bpftool: Check map name length when map create Rong Tao
2025-02-11 10:48 ` Quentin Monnet
2025-02-11 21:07   ` Andrii Nakryiko
2025-02-12  1:14     ` Rong Tao

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®