Skip to contentSkip to conversation

bpf: enforce BPF ringbuf size to be the power of 2

PATCH In review Andrii Nakryiko 3 participants 3 messages Jun 30, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 bpf: enforce BPF ringbuf size to be the power of 2

BPF ringbuf assumes the size to be a multiple of page size and the power of 2 value. The latter is important to avoid division while calculating position inside the ring buffer and using (N-1) mask instead. This patch fixes omission to enforce power-of-2 size rule.

Fixes 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")
---
 kernel/bpf/ringbuf.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
kernel/bpf/ringbuf.c +8 -10
index 180414bb0d3e..0af88bbc1c15 100644
@@ -132,15 +132,6 @@ static struct bpf_ringbuf *bpf_ringbuf_alloc(size_t data_sz, int numa_node)
 {
 	struct bpf_ringbuf *rb;
 
-	if (!data_sz || !PAGE_ALIGNED(data_sz))
-		return ERR_PTR(-EINVAL);
-
-#ifdef CONFIG_64BIT
-	/* on 32-bit arch, it's impossible to overflow record's hdr->pgoff */
-	if (data_sz > RINGBUF_MAX_DATA_SZ)
-		return ERR_PTR(-E2BIG);
-#endif
-
 	rb = bpf_ringbuf_area_alloc(data_sz, numa_node);
 	if (!rb)
 		return ERR_PTR(-ENOMEM);
@@ -166,9 +157,16 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
 		return ERR_PTR(-EINVAL);
 
 	if (attr->key_size || attr->value_size ||
-	    attr->max_entries == 0 || !PAGE_ALIGNED(attr->max_entries))
+	    !is_power_of_2(attr->max_entries) ||
+	    !PAGE_ALIGNED(attr->max_entries))
 		return ERR_PTR(-EINVAL);
 
+#ifdef CONFIG_64BIT
+	/* on 32-bit arch, it's impossible to overflow record's hdr->pgoff */
+	if (attr->max_entries > RINGBUF_MAX_DATA_SZ)
+		return ERR_PTR(-E2BIG);
+#endif
+
 	rb_map = kzalloc(sizeof(*rb_map), GFP_USER);
 	if (!rb_map)
 		return ERR_PTR(-ENOMEM);
Signature · 1 line
2.24.1
maintainer
#1 lore raw
Quoted 7 lines · Andrii Nakryiko

BPF ringbuf assumes the size to be a multiple of page size and the power of 2 value. The latter is important to avoid division while calculating position inside the ring buffer and using (N-1) mask instead. This patch fixes omission to enforce power-of-2 size rule.

Fixes 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")

Lgtm, applied, thanks!

[...]

kernel/bpf/ringbuf.c static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr) · line 161

Quoted 7 lines
kernel/bpf/ringbuf.c +2 -1
@@ -166,9 +157,16 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
  		return ERR_PTR(-EINVAL);
 
  	if (attr->key_size || attr->value_size ||
-	    attr->max_entries == 0 || !PAGE_ALIGNED(attr->max_entries))
+	    !is_power_of_2(attr->max_entries) ||
+	    !PAGE_ALIGNED(attr->max_entries))

Technically !IS_ALIGNED(attr->max_entries, PAGE_SIZE) might have been a bit cleaner since PAGE_ALIGNED() is only intended for pointers, though, not wrong here given max_entries is u32.

Thanks,
Daniel

#2 lore raw
Quoted 7 lines · Daniel Borkmann
Quoted 5 lines · Andrii Nakryiko

BPF ringbuf assumes the size to be a multiple of page size and the power of 2 value. The latter is important to avoid division while calculating position inside the ring buffer and using (N-1) mask instead. This patch fixes omission to enforce power-of-2 size rule.

Quoted 5 lines
Quoted 2 lines
Fixes 457f44363a88 ("bpf: Implement BPF ring buffer and verifier support for it")

Lgtm, applied, thanks!

Thanks, Daniel!

Quoted 4 lines

[...]

Quoted 3 lines
(quoted diff) +0 -0
@@ -166,9 +157,16 @@ static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr)
              return ERR_PTR(-EINVAL);
 
Quoted 8 lines

kernel/bpf/ringbuf.c static struct bpf_map *ringbuf_map_alloc(union bpf_attr *attr) · line 161

Quoted 4 lines
kernel/bpf/ringbuf.c +2 -1
      if (attr->key_size || attr->value_size ||
-         attr->max_entries == 0 || !PAGE_ALIGNED(attr->max_entries))
+         !is_power_of_2(attr->max_entries) ||
+         !PAGE_ALIGNED(attr->max_entries))

Technically !IS_ALIGNED(attr->max_entries, PAGE_SIZE) might have been a bit cleaner since PAGE_ALIGNED() is only intended for pointers, though, not wrong here given max_entries is u32.

I've found a bunch of uses on non-pointers, e.g., if (!PAGE_ALIGNED(fs_info->nodesize)) { in BTRFS code, so assumed it's intended to be used more generically. But let me know if you want me to do IS_ALIGNED change.

Quoted 3 lines

Thanks,
Daniel