Skip to contentSkip to conversation

bpf: use VM_MAP instead of VM_ALLOC for ringbuf

PATCH Applied Hou Tao 4 participants 5 messages Feb 2, 2022 – Feb 3, 2022

Subsystems (4)
lore
Share & tools
lore

Hou Tao

author
#0 lore raw

PATCH v2 1/1 bpf: use VM_MAP instead of VM_ALLOC for ringbuf

After commit 2fd3fb0be1d1 ("kasan, vmalloc: unpoison VM_ALLOC pages after mapping"), non-VM_ALLOC mappings will be marked as accessible in __get_vm_area_node() when KASAN is enabled. But now the flag for ringbuf area is VM_ALLOC, so KASAN will complain out-of-bound access after vmap() returns. Because the ringbuf area is created by mapping allocated pages, so use VM_MAP instead.

After the change, info in /proc/vmallocinfo also changes from
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmalloc user
to
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmap user

---
v2:
* explain why VM_ALLOC will lead to vmalloc-oob access
* add Reported-by tag
v1: https://lore.kernel.org/bpf/CANUnq3a+sT_qtO1wNQ3GnLGN7FLvSSgvit2UVgqQKRpUvs85VQ@mail.gmail.com/T/#t

---
 kernel/bpf/ringbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/ringbuf.c +1 -1
index 638d7fd7b375..710ba9de12ce 100644
@@ -104,7 +104,7 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
 	}
 
 	rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
-		  VM_ALLOC | VM_USERMAP, PAGE_KERNEL);
+		  VM_MAP | VM_USERMAP, PAGE_KERNEL);
 	if (rb) {
 		kmemleak_not_leak(pages);
 		rb->pages = pages;
Signature · 1 line
2.35.1
maintainer
#1 lore raw

[ +Andrey ]

Quoted 12 lines · Hou Tao

After commit 2fd3fb0be1d1 ("kasan, vmalloc: unpoison VM_ALLOC pages after mapping"), non-VM_ALLOC mappings will be marked as accessible in __get_vm_area_node() when KASAN is enabled. But now the flag for ringbuf area is VM_ALLOC, so KASAN will complain out-of-bound access after vmap() returns. Because the ringbuf area is created by mapping allocated pages, so use VM_MAP instead.

After the change, info in /proc/vmallocinfo also changes from
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmalloc user
to
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmap user

Quoted 5 lines

---
v2:
* explain why VM_ALLOC will lead to vmalloc-oob access

Do you know which tree commit 2fd3fb0be1d1 is, looks like it's neither in bpf nor in bpf-next tree at the moment.

Either way, I presume this fix should be routed via bpf tree rather than bpf-next? (I can add Fixes tag while applying.)

Quoted 20 lines

* add Reported-by tag
v1: https://lore.kernel.org/bpf/CANUnq3a+sT_qtO1wNQ3GnLGN7FLvSSgvit2UVgqQKRpUvs85VQ@mail.gmail.com/T/#t

---
  kernel/bpf/ringbuf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/ringbuf.c +1 -1
index 638d7fd7b375..710ba9de12ce 100644
@@ -104,7 +104,7 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
  	}
 
  	rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
-		  VM_ALLOC | VM_USERMAP, PAGE_KERNEL);
+		  VM_MAP | VM_USERMAP, PAGE_KERNEL);
  	if (rb) {
  		kmemleak_not_leak(pages);
  		rb->pages = pages;
author
#2 lore raw

Hi,

Quoted 13 lines · Hou Tao
Quoted 12 lines

After commit 2fd3fb0be1d1 ("kasan, vmalloc: unpoison VM_ALLOC pages after mapping"), non-VM_ALLOC mappings will be marked as accessible in __get_vm_area_node() when KASAN is enabled. But now the flag for ringbuf area is VM_ALLOC, so KASAN will complain out-of-bound access after vmap() returns. Because the ringbuf area is created by mapping allocated pages, so use VM_MAP instead.

After the change, info in /proc/vmallocinfo also changes from
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmalloc user
to
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmap user

Quoted 9 lines
Quoted 5 lines

---
v2:
* explain why VM_ALLOC will lead to vmalloc-oob access

Do you know which tree commit 2fd3fb0be1d1 is, looks like it's neither in bpf nor in bpf-next tree at the moment.

It is on linux-next tree:

 $ git name-rev 2fd3fb0be1d1
 2fd3fb0be1d1 tags/next-20220201~2^2~96
Quoted 3 lines

Either way, I presume this fix should be routed via bpf tree rather than bpf-next? (I can add Fixes tag while applying.)

Make sense and thanks for that.

Regards,
Tao

Quoted 22 lines
Quoted 20 lines

* add Reported-by tag
v1: https://lore.kernel.org/bpf/CANUnq3a+sT_qtO1wNQ3GnLGN7FLvSSgvit2UVgqQKRpUvs85VQ@mail.gmail.com/T/#t

---
  kernel/bpf/ringbuf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/ringbuf.c +1 -1
index 638d7fd7b375..710ba9de12ce 100644
@@ -104,7 +104,7 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
  	}
 
  	rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
-		  VM_ALLOC | VM_USERMAP, PAGE_KERNEL);
+		  VM_MAP | VM_USERMAP, PAGE_KERNEL);
  	if (rb) {
  		kmemleak_not_leak(pages);
  		rb->pages = pages;
#3 lore raw
Quoted 27 lines · Hou Tao

Hi,

Quoted 13 lines · Hou Tao
Quoted 12 lines

After commit 2fd3fb0be1d1 ("kasan, vmalloc: unpoison VM_ALLOC pages after mapping"), non-VM_ALLOC mappings will be marked as accessible in __get_vm_area_node() when KASAN is enabled. But now the flag for ringbuf area is VM_ALLOC, so KASAN will complain out-of-bound access after vmap() returns. Because the ringbuf area is created by mapping allocated pages, so use VM_MAP instead.

After the change, info in /proc/vmallocinfo also changes from
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmalloc user
to
[start]-[end] 24576 ringbuf_map_alloc+0x171/0x290 vmap user

Quoted 9 lines
Quoted 5 lines

---
v2:
* explain why VM_ALLOC will lead to vmalloc-oob access

Do you know which tree commit 2fd3fb0be1d1 is, looks like it's neither in bpf nor in bpf-next tree at the moment.

It is on linux-next tree:

Quoted 7 lines
 $ git name-rev 2fd3fb0be1d1
 2fd3fb0be1d1 tags/next-20220201~2^2~96
Quoted 3 lines

Either way, I presume this fix should be routed via bpf tree rather than bpf-next? (I can add Fixes tag while applying.)

Make sense and thanks for that.

Added

Fixes 457f44363a88 ("bpf: Implement BPF ring buffer and verifier

support for it")

and pushed to bpf tree, thanks.

Quoted 26 lines

Regards,
Tao

Quoted 22 lines
Quoted 20 lines

* add Reported-by tag
v1: https://lore.kernel.org/bpf/CANUnq3a+sT_qtO1wNQ3GnLGN7FLvSSgvit2UVgqQKRpUvs85VQ@mail.gmail.com/T/#t

---
  kernel/bpf/ringbuf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/ringbuf.c +1 -1
index 638d7fd7b375..710ba9de12ce 100644
@@ -104,7 +104,7 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node)
    }
 
    rb = vmap(pages, nr_meta_pages + 2 * nr_data_pages,
-             VM_ALLOC | VM_USERMAP, PAGE_KERNEL);
+             VM_MAP | VM_USERMAP, PAGE_KERNEL);
    if (rb) {
            kmemleak_not_leak(pages);
            rb->pages = pages;
#4 lore raw
In reply to Hou Tao
This patch was applied to bpf/bpf.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf.git (master)
by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Wed, 2 Feb 2022 14:01:58 +0800 you wrote:

After commit 2fd3fb0be1d1 ("kasan, vmalloc: unpoison VM_ALLOC pages after mapping"), non-VM_ALLOC mappings will be marked as accessible in __get_vm_area_node() when KASAN is enabled. But now the flag for ringbuf area is VM_ALLOC, so KASAN will complain out-of-bound access after vmap() returns. Because the ringbuf area is created by mapping allocated pages, so use VM_MAP instead.

[...]

Here is the summary with links:
- [bpf-next,v2] bpf: use VM_MAP instead of VM_ALLOC for ringbuf

    https://git.kernel.org/bpf/bpf/c/b293dcc473d2

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html