Skip to contentSkip to conversation

bpf/verifier: Use kmalloc_size_roundup() to match ksize() usage

PATCH Superseded Kees Cook 6 participants 12 messages Oct 29, 2022 – Nov 15, 2022

Subsystems (2)

v2 of 3 Superseded 1 of 3 applied Latest is v3 →

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

v2 cover bpf/verifier: Use kmalloc_size_roundup() to match ksize() usage

v2:
- split up patch into logical changes
- simplify copy_array, which can use ksize() directly
v1: https://lore.kernel.org/all/[email protected]/

Hi,

Here's the next version of removing the BPF verifier's dependency on the side-effects of ksize(), so we can remove the special handling needed for KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE in ksize().

Thanks,

-Kees

Kees Cook (3):
bpf/verifier: Fix potential memory leak in array reallocation bpf/verifier: Use kmalloc_size_roundup() to match ksize() usage bpf/verifier: Take advantage of full allocation sizes

 kernel/bpf/verifier.c | 44 ++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/3 bpf/verifier: Fix potential memory leak in array reallocation

If an error (NULL) is returned by krealloc(), callers of realloc_array() were setting their allocation pointers to NULL, but on error krealloc() does not touch the original allocation. This would result in a memory resource leak. Instead, free the old allocation on the error handling path.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 kernel/bpf/verifier.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +7 -2
index 014ee0953dbd..eb8c34db74c7 100644
@@ -1027,12 +1027,17 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
  */
 static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
 {
+	void *new_arr;
+
 	if (!new_n || old_n == new_n)
 		goto out;
 
-	arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
-	if (!arr)
+	new_arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
+	if (!new_arr) {
+		kfree(arr);
 		return NULL;
+	}
+	arr = new_arr;
 
 	if (new_n > old_n)
 		memset(arr + old_n * size, 0, (new_n - old_n) * size);
Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v2 3/3 bpf/verifier: Take advantage of full allocation sizes

In reply to Kees Cook

Since the full kmalloc bucket size is being explicitly allocated, pass back the resulting details to take advantage of the full size so that reallocation checking will be needed less frequently.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 kernel/bpf/verifier.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +16 -11
index 1c040d27b8f6..e58b554e862b 100644
@@ -1020,20 +1020,23 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
 	return dst ? dst : ZERO_SIZE_PTR;
 }
 
-/* resize an array from old_n items to new_n items. the array is reallocated if it's too
- * small to hold new_n items. new items are zeroed out if the array grows.
+/* Resize an array from old_n items to *new_n items. The array is
+ * reallocated if it's too small to hold *new_n items. New items are
+ * zeroed out if the array grows. Allocation is rounded up to next kmalloc
+ * bucket size to reduce frequency of resizing. *new_n contains the new
+ * total number of items that will fit.
  *
- * Contrary to krealloc_array, does not free arr if new_n is zero.
+ * Contrary to krealloc, does not free arr if new_n is zero.
  */
-static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
+static void *realloc_array(void *arr, size_t old_n, size_t *new_n, size_t size)
 {
 	size_t alloc_size;
 	void *new_arr;
 
-	if (!new_n || old_n == new_n)
+	if (!new_n || !*new_n || old_n == *new_n)
 		goto out;
 
-	alloc_size = kmalloc_size_roundup(size_mul(new_n, size));
+	alloc_size = kmalloc_size_roundup(size_mul(*new_n, size));
 	new_arr = krealloc(arr, alloc_size, GFP_KERNEL);
 	if (!new_arr) {
 		kfree(arr);
@@ -1041,8 +1044,9 @@ static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
 	}
 	arr = new_arr;
 
-	if (new_n > old_n)
-		memset(arr + old_n * size, 0, (new_n - old_n) * size);
+	*new_n = alloc_size / size;
+	if (*new_n > old_n)
+		memset(arr + old_n * size, 0, (*new_n - old_n) * size);
 
 out:
 	return arr ? arr : ZERO_SIZE_PTR;
@@ -1074,7 +1078,7 @@ static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_st
 
 static int resize_reference_state(struct bpf_func_state *state, size_t n)
 {
-	state->refs = realloc_array(state->refs, state->acquired_refs, n,
+	state->refs = realloc_array(state->refs, state->acquired_refs, &n,
 				    sizeof(struct bpf_reference_state));
 	if (!state->refs)
 		return -ENOMEM;
@@ -1090,11 +1094,12 @@ static int grow_stack_state(struct bpf_func_state *state, int size)
 	if (old_n >= n)
 		return 0;
 
-	state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state));
+	state->stack = realloc_array(state->stack, old_n, &n,
+				     sizeof(struct bpf_stack_state));
 	if (!state->stack)
 		return -ENOMEM;
 
-	state->allocated_stack = size;
+	state->allocated_stack = n * BPF_REG_SIZE;
 	return 0;
 }
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 2/3 bpf/verifier: Use kmalloc_size_roundup() to match ksize() usage

In reply to Kees Cook

Round up allocations with kmalloc_size_roundup() so that the verifier's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE. Pass the new size information back up to callers so they can use the space immediately, so array resizing to happen less frequently as well.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 kernel/bpf/verifier.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +8 -4
index eb8c34db74c7..1c040d27b8f6 100644
@@ -1008,9 +1008,9 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
 	if (unlikely(check_mul_overflow(n, size, &bytes)))
 		return NULL;
 
-	if (ksize(dst) < bytes) {
+	if (ksize(dst) < ksize(src)) {
 		kfree(dst);
-		dst = kmalloc_track_caller(bytes, flags);
+		dst = kmalloc_track_caller(kmalloc_size_roundup(bytes), flags);
 		if (!dst)
 			return NULL;
 	}
@@ -1027,12 +1027,14 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
  */
 static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
 {
+	size_t alloc_size;
 	void *new_arr;
 
 	if (!new_n || old_n == new_n)
 		goto out;
 
-	new_arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
+	alloc_size = kmalloc_size_roundup(size_mul(new_n, size));
+	new_arr = krealloc(arr, alloc_size, GFP_KERNEL);
 	if (!new_arr) {
 		kfree(arr);
 		return NULL;
@@ -2504,9 +2506,11 @@ static int push_jmp_history(struct bpf_verifier_env *env,
 {
 	u32 cnt = cur->jmp_history_cnt;
 	struct bpf_idx_pair *p;
+	size_t alloc_size;
 
 	cnt++;
-	p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER);
+	alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p)));
+	p = krealloc(cur->jmp_history, alloc_size, GFP_USER);
 	if (!p)
 		return -ENOMEM;
 	p[cnt - 1].idx = env->insn_idx;
Signature · 1 line
2.34.1
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH bpf-next v2 1/3] bpf/verifier: Fix potential memory leak in array reallocation

In reply to Kees Cook
Quoted 7 lines · Kees Cook

If an error (NULL) is returned by krealloc(), callers of realloc_array() were setting their allocation pointers to NULL, but on error krealloc() does not touch the original allocation. This would result in a memory resource leak. Instead, free the old allocation on the error handling path.

Quoted 13 lines
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Quoted 31 lines
---
 kernel/bpf/verifier.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +7 -3
index 014ee0953dbd..eb8c34db74c7 100644
@@ -1027,12 +1027,17 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
  */
 static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
 {
+       void *new_arr;
+
        if (!new_n || old_n == new_n)
                goto out;
 
-       arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
-       if (!arr)
+       new_arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
+       if (!new_arr) {
+               kfree(arr);
                return NULL;
+       }
+       arr = new_arr;
 
        if (new_n > old_n)
                memset(arr + old_n * size, 0, (new_n - old_n) * size);
--

2.34.1

#9 lore raw

Re: [PATCH bpf-next v2 3/3] bpf/verifier: Take advantage of full allocation sizes

In reply to Kees Cook
Quoted 68 lines · Kees Cook

Since the full kmalloc bucket size is being explicitly allocated, pass back the resulting details to take advantage of the full size so that reallocation checking will be needed less frequently.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 kernel/bpf/verifier.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +12 -8
index 1c040d27b8f6..e58b554e862b 100644
@@ -1020,20 +1020,23 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
        return dst ? dst : ZERO_SIZE_PTR;
 }
 
-/* resize an array from old_n items to new_n items. the array is reallocated if it's too
- * small to hold new_n items. new items are zeroed out if the array grows.
+/* Resize an array from old_n items to *new_n items. The array is
+ * reallocated if it's too small to hold *new_n items. New items are
+ * zeroed out if the array grows. Allocation is rounded up to next kmalloc
+ * bucket size to reduce frequency of resizing. *new_n contains the new
+ * total number of items that will fit.
  *
- * Contrary to krealloc_array, does not free arr if new_n is zero.
+ * Contrary to krealloc, does not free arr if new_n is zero.
  */
-static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
+static void *realloc_array(void *arr, size_t old_n, size_t *new_n, size_t size)
 {
        size_t alloc_size;
        void *new_arr;
 
-       if (!new_n || old_n == new_n)
+       if (!new_n || !*new_n || old_n == *new_n)
                goto out;
 
-       alloc_size = kmalloc_size_roundup(size_mul(new_n, size));
+       alloc_size = kmalloc_size_roundup(size_mul(*new_n, size));
        new_arr = krealloc(arr, alloc_size, GFP_KERNEL);
        if (!new_arr) {
                kfree(arr);
@@ -1041,8 +1044,9 @@ static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
        }
        arr = new_arr;
 
-       if (new_n > old_n)
-               memset(arr + old_n * size, 0, (new_n - old_n) * size);
+       *new_n = alloc_size / size;
+       if (*new_n > old_n)
+               memset(arr + old_n * size, 0, (*new_n - old_n) * size);
 
 out:
        return arr ? arr : ZERO_SIZE_PTR;

[..]

kernel/bpf/verifier.c static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_st · line 1084

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
@@ -1074,7 +1078,7 @@ static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_st
 
 static int resize_reference_state(struct bpf_func_state *state, size_t n)
 {
-       state->refs = realloc_array(state->refs, state->acquired_refs, n,
+       state->refs = realloc_array(state->refs, state->acquired_refs, &n,
                                    sizeof(struct bpf_reference_state));
        if (!state->refs)
                return -ENOMEM;

Patches 1 & 2 look good, but not sure about this part. We later do the following in the same routine:

state->acquired_refs = n;

And acquire_reference_state() does "new_ofs = state->acquired_refs;" to append..

Which changes semantics a bit? It used to mean array size, now it means array capacity.
Should we keep this part as is but add a shortcut to realloc_array when ksize(ptr) == kmalloc_size_roundup(new size) -> reuse existing array?
Or am I missing something? (haven't looked too deep)

Quoted 18 lines
kernel/bpf/verifier.c +3 -3
@@ -1090,11 +1094,12 @@ static int grow_stack_state(struct bpf_func_state *state, int size)
        if (old_n >= n)
                return 0;
 
-       state->stack = realloc_array(state->stack, old_n, n, sizeof(struct bpf_stack_state));
+       state->stack = realloc_array(state->stack, old_n, &n,
+                                    sizeof(struct bpf_stack_state));
        if (!state->stack)
                return -ENOMEM;
 
-       state->allocated_stack = size;
+       state->allocated_stack = n * BPF_REG_SIZE;
        return 0;
 }
 
--

2.34.1

author
#10 lore raw

Re: [PATCH bpf-next v2 3/3] bpf/verifier: Take advantage of full allocation sizes

Quoted 74 lines · Stanislav Fomichev
Quoted 68 lines · Kees Cook

Since the full kmalloc bucket size is being explicitly allocated, pass back the resulting details to take advantage of the full size so that reallocation checking will be needed less frequently.

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 kernel/bpf/verifier.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +12 -8
index 1c040d27b8f6..e58b554e862b 100644
@@ -1020,20 +1020,23 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
        return dst ? dst : ZERO_SIZE_PTR;
 }
 
-/* resize an array from old_n items to new_n items. the array is reallocated if it's too
- * small to hold new_n items. new items are zeroed out if the array grows.
+/* Resize an array from old_n items to *new_n items. The array is
+ * reallocated if it's too small to hold *new_n items. New items are
+ * zeroed out if the array grows. Allocation is rounded up to next kmalloc
+ * bucket size to reduce frequency of resizing. *new_n contains the new
+ * total number of items that will fit.
  *
- * Contrary to krealloc_array, does not free arr if new_n is zero.
+ * Contrary to krealloc, does not free arr if new_n is zero.
  */
-static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
+static void *realloc_array(void *arr, size_t old_n, size_t *new_n, size_t size)
 {
        size_t alloc_size;
        void *new_arr;
 
-       if (!new_n || old_n == new_n)
+       if (!new_n || !*new_n || old_n == *new_n)
                goto out;
 
-       alloc_size = kmalloc_size_roundup(size_mul(new_n, size));
+       alloc_size = kmalloc_size_roundup(size_mul(*new_n, size));
        new_arr = krealloc(arr, alloc_size, GFP_KERNEL);
        if (!new_arr) {
                kfree(arr);
@@ -1041,8 +1044,9 @@ static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
        }
        arr = new_arr;
 
-       if (new_n > old_n)
-               memset(arr + old_n * size, 0, (new_n - old_n) * size);
+       *new_n = alloc_size / size;
+       if (*new_n > old_n)
+               memset(arr + old_n * size, 0, (*new_n - old_n) * size);
 
 out:
        return arr ? arr : ZERO_SIZE_PTR;

[..]

Quoted 2 lines
kernel/bpf/verifier.c +0 -0
@@ -1074,7 +1078,7 @@ static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_st
 
Quoted 10 lines

kernel/bpf/verifier.c static int copy_stack_state(struct bpf_func_state *dst, const struct bpf_func_st · line 1084

Quoted 7 lines
kernel/bpf/verifier.c +1 -1
 static int resize_reference_state(struct bpf_func_state *state, size_t n)
 {
-       state->refs = realloc_array(state->refs, state->acquired_refs, n,
+       state->refs = realloc_array(state->refs, state->acquired_refs, &n,
                                    sizeof(struct bpf_reference_state));
        if (!state->refs)
                return -ENOMEM;

Patches 1 & 2 look good, but not sure about this part. We later do the following in the same routine:

I'm totally fine leaving off #3. 1 is a bug fix, 2 is what I need to get the ksize side-effect managed in bpf, and 3 was maybe an optimization.

Signature · 1 line
Kees Cook
#11 lore raw
In reply to Kees Cook
This series was applied to bpf/bpf.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Fri, 28 Oct 2022 19:54:29 -0700 you wrote:

v2:
- split up patch into logical changes
- simplify copy_array, which can use ksize() directly
v1: https://lore.kernel.org/all/[email protected]/

Hi,

[...]

Here is the summary with links:
- [bpf-next,v2,1/3] bpf/verifier: Fix potential memory leak in array reallocation

    https://git.kernel.org/bpf/bpf/c/42378a9ca553

- [bpf-next,v2,2/3] bpf/verifier: Use kmalloc_size_roundup() to match ksize() usage (no matching commit)
- [bpf-next,v2,3/3] bpf/verifier: Take advantage of full allocation sizes (no matching commit)

You are awesome, thank you!

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

Re: [PATCH bpf-next v2 1/3] bpf/verifier: Fix potential memory leak in array reallocation

In reply to Bill Wendling

[ +Lorenz ]

Quoted 41 lines · Bill Wendling
Quoted 7 lines · Kees Cook

If an error (NULL) is returned by krealloc(), callers of realloc_array() were setting their allocation pointers to NULL, but on error krealloc() does not touch the original allocation. This would result in a memory resource leak. Instead, free the old allocation on the error handling path.

Quoted 13 lines
Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc John Fastabend <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Martin KaFai Lau <[email protected]>
Cc Song Liu <[email protected]>
Cc Yonghong Song <[email protected]>
Cc KP Singh <[email protected]>
Cc Stanislav Fomichev <[email protected]>
Cc Hao Luo <[email protected]>
Cc Jiri Olsa <[email protected]>
Quoted 17 lines
---
  kernel/bpf/verifier.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +2 -0
index 014ee0953dbd..eb8c34db74c7 100644
@@ -1027,12 +1027,17 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
   */
  static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
  {
+       void *new_arr;
+
         if (!new_n || old_n == new_n)
                 goto out;
 
Quoted 8 lines

kernel/bpf/verifier.c static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t · line 1040

Quoted 8 lines
kernel/bpf/verifier.c +5 -2
-       arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
-       if (!arr)
+       new_arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
+       if (!new_arr) {
+               kfree(arr);
                 return NULL;
+       }
+       arr = new_arr;

Fyi, I took this fix into bpf tree and improved commit log a bit with the one from Zhengchao [0] given yours came in first. Fixes tag would have been nice, I added the c69431aab67a to the commit message while applying.

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Quoted 5 lines
Quoted 5 lines
kernel/bpf/verifier.c +0 -1
         if (new_n > old_n)
                 memset(arr + old_n * size, 0, (new_n - old_n) * size);
--

2.34.1

maintainer
#6 lore raw
In reply to Kees Cook
Quoted 6 lines · Kees Cook

Round up allocations with kmalloc_size_roundup() so that the verifier's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE. Pass the new size information back up to callers so they can use the space immediately, so array resizing to happen less frequently as well.

[...]

The commit message is a bit cryptic here without further context. Is this a bug fix or improvement? I read the latter, but it would be good to have more context here for reviewers (maybe Link tag pointing to some discussion or the like). Also, why is the kmalloc_size_roundup() not hidden for kmalloc callers, isn't this a tree-wide issue?

Thanks,
Daniel

Quoted 49 lines
  kernel/bpf/verifier.c | 12 ++++++++----
  1 file changed, 8 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +8 -4
index eb8c34db74c7..1c040d27b8f6 100644
@@ -1008,9 +1008,9 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
  	if (unlikely(check_mul_overflow(n, size, &bytes)))
  		return NULL;
 
-	if (ksize(dst) < bytes) {
+	if (ksize(dst) < ksize(src)) {
  		kfree(dst);
-		dst = kmalloc_track_caller(bytes, flags);
+		dst = kmalloc_track_caller(kmalloc_size_roundup(bytes), flags);
  		if (!dst)
  			return NULL;
  	}
@@ -1027,12 +1027,14 @@ static void *copy_array(void *dst, const void *src, size_t n, size_t size, gfp_t
   */
  static void *realloc_array(void *arr, size_t old_n, size_t new_n, size_t size)
  {
+	size_t alloc_size;
  	void *new_arr;
 
  	if (!new_n || old_n == new_n)
  		goto out;
 
-	new_arr = krealloc_array(arr, new_n, size, GFP_KERNEL);
+	alloc_size = kmalloc_size_roundup(size_mul(new_n, size));
+	new_arr = krealloc(arr, alloc_size, GFP_KERNEL);
  	if (!new_arr) {
  		kfree(arr);
  		return NULL;
@@ -2504,9 +2506,11 @@ static int push_jmp_history(struct bpf_verifier_env *env,
  {
  	u32 cnt = cur->jmp_history_cnt;
  	struct bpf_idx_pair *p;
+	size_t alloc_size;
 
  	cnt++;
-	p = krealloc(cur->jmp_history, cnt * sizeof(*p), GFP_USER);
+	alloc_size = kmalloc_size_roundup(size_mul(cnt, sizeof(*p)));
+	p = krealloc(cur->jmp_history, alloc_size, GFP_USER);
  	if (!p)
  		return -ENOMEM;
  	p[cnt - 1].idx = env->insn_idx;
author
#7 lore raw
Quoted 11 lines · Daniel Borkmann
Quoted 6 lines · Kees Cook

Round up allocations with kmalloc_size_roundup() so that the verifier's use of ksize() is always accurate and no special handling of the memory is needed by KASAN, UBSAN_BOUNDS, nor FORTIFY_SOURCE. Pass the new size information back up to callers so they can use the space immediately, so array resizing to happen less frequently as well.

[...]

The commit message is a bit cryptic here without further context. Is this a bug fix or improvement? I read the latter, but it would be good to have

It's an improvement -- e.g. it depends on the recently added kmalloc_size_roundup() helper.

Quoted 3 lines

more context here for reviewers (maybe Link tag pointing to some discussion or the like). Also, why is the kmalloc_size_roundup() not hidden for kmalloc callers, isn't this a tree-wide issue?

The main issue is that _most_ allocation callers want an explicitly sized allocation (and not "more"), and that dynamic runtime analysis tools (e.g. KASAN, UBSAN_BOUNDS, FORTIFY_SOURCE, etc) are looking for precise bounds checking (i.e. not something that is rounded up). A tiny handful of allocations were doing an implicit alloc/realloc loop that actually depended on ksize(), and didn't actually always call realloc. This has created a long series of bugs and problems over many years related to the runtime bounds checking, so these callers are finally being adjusted to _not_ depend on the ksize() side-effect, by doing one of several things:

- tracking the allocation size precisely and just never calling ksize() at all[1].

- always calling realloc and not using ksize() at all. (This solution ends up actually be a subset of the next solution.)

- using kmalloc_size_roundup() to explicitly round up the desired allocation size immediately[2].

The bpf/verifier case is this another of this latter case.

Because some of the dynamic bounds checking depends on the size being an _argument_ to an allocator function (i.e. see the __alloc_size attribute), the ksize() users are rare, and it could waste local variables, it was been deemed better to explicitly separate the rounding up from the allocation itself[3].

Hopefully that helps clarify! :)

-Kees

[1] e.g.:

    https://git.kernel.org/linus/712f210a457d
    https://git.kernel.org/linus/72c08d9f4c72

[2] e.g.:

    https://git.kernel.org/netdev/net-next/c/12d6c1d3a2ad
    https://git.kernel.org/netdev/net-next/c/ab3f7828c979
    https://git.kernel.org/netdev/net-next/c/d6dd508080a3

[3] https://lore.kernel.org/lkml/[email protected]/

Signature · 1 line
Kees Cook
#4 lore raw

Re: [PATCH bpf-next v2 1/3] bpf/verifier: Fix potential memory leak in array reallocation

In reply to Daniel Borkmann
Quoted 1 line · Daniel Borkmann

[ +Lorenz ]

Thank you Kees and Daniel for fixing this!