Skip to contentSkip to conversation

bpf: Fix memory leaks in __check_func_call

PATCH Superseded Wang Yufen 2 participants 3 messages Nov 3, 2022 – Nov 8, 2022

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Wang Yufen

author
#0 lore raw

PATCH v3 1/1 bpf: Fix memory leaks in __check_func_call

kmemleak reports this issue:

unreferenced object 0xffff88817139d000 (size 2048):
comm "test_progs", pid 33246, jiffies 4307381979 (age 45851.820s) hex dump (first 32 bytes):

    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
    [<0000000045f075f0>] kmalloc_trace+0x27/0xa0
    [<0000000098b7c90a>] __check_func_call+0x316/0x1230
    [<00000000b4c3c403>] check_helper_call+0x172e/0x4700
    [<00000000aa3875b7>] do_check+0x21d8/0x45e0
    [<000000001147357b>] do_check_common+0x767/0xaf0
    [<00000000b5a595b4>] bpf_check+0x43e3/0x5bc0
    [<0000000011e391b1>] bpf_prog_load+0xf26/0x1940
    [<0000000007f765c0>] __sys_bpf+0xd2c/0x3650
    [<00000000839815d6>] __x64_sys_bpf+0x75/0xc0
    [<00000000946ee250>] do_syscall_64+0x3b/0x90
    [<0000000000506b7f>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

The root case here is: In function prepare_func_exit(), the callee is not released in the abnormal scenario after "state->curframe--;". To fix, move "state->curframe--;" to the very bottom of the function, right when we free callee and reset frame[] pointer to NULL, as Andrii suggested.

In addition, function __check_func_call() has a similar problem. In the abnormal scenario before "state->curframe++;", the callee is alse not released.

Fixes 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper")
Fixes fd978bf7fd31 ("bpf: Add reference tracking to verifier")
---
 kernel/bpf/verifier.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +9 -4
index 7f0a9f6..eff7a5a 100644
@@ -6736,11 +6736,11 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 	/* Transfer references to the callee */
 	err = copy_reference_state(callee, caller);
 	if (err)
-		return err;
+		goto err_out;
 
 	err = set_callee_state_cb(env, caller, callee, *insn_idx);
 	if (err)
-		return err;
+		goto err_out;
 
 	clear_caller_saved_regs(env, caller->regs);
 
@@ -6757,6 +6757,11 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		print_verifier_state(env, callee, true);
 	}
 	return 0;
+
+err_out:
+	kfree(callee);
+	state->frame[state->curframe + 1] = NULL;
+	return err;
 }
 
 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
@@ -6970,8 +6975,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 		return -EINVAL;
 	}
 
-	state->curframe--;
-	caller = state->frame[state->curframe];
+	caller = state->frame[state->curframe - 1];
 	if (callee->in_callback_fn) {
 		/* enforce R0 return value range [0, 1]. */
 		struct tnum range = callee->callback_ret_range;
@@ -7001,6 +7005,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
 			return err;
 	}
 
+	state->curframe--;
 	*insn_idx = callee->callsite + 1;
 	if (env->log.level & BPF_LOG_LEVEL) {
 		verbose(env, "returning from callee:\n");
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 56 lines · Wang Yufen

kmemleak reports this issue:

unreferenced object 0xffff88817139d000 (size 2048):

   comm "test_progs", pid 33246, jiffies 4307381979 (age 45851.820s)
   hex dump (first 32 bytes):
     01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
     00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   backtrace:
     [<0000000045f075f0>] kmalloc_trace+0x27/0xa0
     [<0000000098b7c90a>] __check_func_call+0x316/0x1230
     [<00000000b4c3c403>] check_helper_call+0x172e/0x4700
     [<00000000aa3875b7>] do_check+0x21d8/0x45e0
     [<000000001147357b>] do_check_common+0x767/0xaf0
     [<00000000b5a595b4>] bpf_check+0x43e3/0x5bc0
     [<0000000011e391b1>] bpf_prog_load+0xf26/0x1940
     [<0000000007f765c0>] __sys_bpf+0xd2c/0x3650
     [<00000000839815d6>] __x64_sys_bpf+0x75/0xc0
     [<00000000946ee250>] do_syscall_64+0x3b/0x90
     [<0000000000506b7f>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

The root case here is: In function prepare_func_exit(), the callee is not released in the abnormal scenario after "state->curframe--;". To fix, move "state->curframe--;" to the very bottom of the function, right when we free callee and reset frame[] pointer to NULL, as Andrii suggested.

In addition, function __check_func_call() has a similar problem. In the abnormal scenario before "state->curframe++;", the callee is alse not released.

Fixes 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper")
Fixes fd978bf7fd31 ("bpf: Add reference tracking to verifier")
---
  kernel/bpf/verifier.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +2 -2
index 7f0a9f6..eff7a5a 100644
@@ -6736,11 +6736,11 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  	/* Transfer references to the callee */
  	err = copy_reference_state(callee, caller);
  	if (err)
-		return err;
+		goto err_out;
 
  	err = set_callee_state_cb(env, caller, callee, *insn_idx);
  	if (err)
-		return err;
+		goto err_out;
 
  	clear_caller_saved_regs(env, caller->regs);
 

kernel/bpf/verifier.c static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6762

Quoted 6 lines
kernel/bpf/verifier.c +3 -0
@@ -6757,6 +6757,11 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  		print_verifier_state(env, callee, true);
  	}
  	return 0;
+
+err_out:
+	kfree(callee);

Is it sure that free_func_state() is not needed ?

Quoted 16 lines
kernel/bpf/verifier.c +3 -2
+	state->frame[state->curframe + 1] = NULL;
+	return err;
  }
 
  int map_set_for_each_callback_args(struct bpf_verifier_env *env,
@@ -6970,8 +6975,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
  		return -EINVAL;
  	}
 
-	state->curframe--;
-	caller = state->frame[state->curframe];
+	caller = state->frame[state->curframe - 1];
  	if (callee->in_callback_fn) {
  		/* enforce R0 return value range [0, 1]. */
  		struct tnum range = callee->callback_ret_range;

kernel/bpf/verifier.c static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) · line 7008

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -7001,6 +7005,7 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx)
  			return err;
  	}
 
+	state->curframe--;

nit. state->curframe is always pointing to callee upto this point? Instead of doing another +1 dance in the latter 'state->frame[state->curframe + 1] = NULL;', how about do it later like:

	/* clear everything in the callee */
        free_func_state(callee);
	state->frame[state->curframe--] = NULL;

It shouldn't affect the earlier print_verifier_state() which explicitly takes callee and caller as its arg, right?

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
  	*insn_idx = callee->callsite + 1;
  	if (env->log.level & BPF_LOG_LEVEL) {
  		verbose(env, "returning from callee:\n");
author
#2 lore raw
Quoted 55 lines · Martin KaFai Lau
Quoted 54 lines · Wang Yufen

kmemleak reports this issue:

unreferenced object 0xffff88817139d000 (size 2048):
   comm "test_progs", pid 33246, jiffies 4307381979 (age 45851.820s) hex dump (first 32 bytes):
     01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ backtrace:

     [<0000000045f075f0>] kmalloc_trace+0x27/0xa0
     [<0000000098b7c90a>] __check_func_call+0x316/0x1230
     [<00000000b4c3c403>] check_helper_call+0x172e/0x4700
     [<00000000aa3875b7>] do_check+0x21d8/0x45e0
     [<000000001147357b>] do_check_common+0x767/0xaf0
     [<00000000b5a595b4>] bpf_check+0x43e3/0x5bc0
     [<0000000011e391b1>] bpf_prog_load+0xf26/0x1940
     [<0000000007f765c0>] __sys_bpf+0xd2c/0x3650
     [<00000000839815d6>] __x64_sys_bpf+0x75/0xc0
     [<00000000946ee250>] do_syscall_64+0x3b/0x90
     [<0000000000506b7f>] entry_SYSCALL_64_after_hwframe+0x63/0xcd

The root case here is: In function prepare_func_exit(), the callee is not released in the abnormal scenario after "state->curframe--;". To fix, move "state->curframe--;" to the very bottom of the function, right when we free callee and reset frame[] pointer to NULL, as Andrii suggested.

In addition, function __check_func_call() has a similar problem. In the abnormal scenario before "state->curframe++;", the callee is alse not released.

Fixes 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper")
Fixes fd978bf7fd31 ("bpf: Add reference tracking to verifier")

---
  kernel/bpf/verifier.c | 13 +++++++++----

  1 file changed, 9 insertions(+), 4 deletions(-)
kernel/bpf/verifier.c +0 -0
index 7f0a9f6..eff7a5a 100644
@@ -6736,11 +6736,11 @@ static int __check_func_call(struct 

bpf_verifier_env *env, struct bpf_insn *insn
      /* Transfer references to the callee */
      err = copy_reference_state(callee, caller);
      if (err)
-        return err;
+        goto err_out;
        err = set_callee_state_cb(env, caller, callee, *insn_idx);
      if (err)
-        return err;
+        goto err_out;
        clear_caller_saved_regs(env, caller->regs);
  @@ -6757,6 +6757,11 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn

Quoted 8 lines

kernel/bpf/verifier.c static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 6762

Quoted 6 lines

          print_verifier_state(env, callee, true); }
      return 0;

kernel/bpf/verifier.c +3 -0
+
+err_out:
+    kfree(callee);

Is it sure that free_func_state() is not needed ?

No, free_func_state() is needed. Sorry, I didn't notice that memory is alloced for callee->refs in

copy_reference_state()-> copy_array() -> kmalloc_track_caller().  will change in v4

Thanks.

Wang

Quoted 23 lines
Quoted 16 lines

+    state->frame[state->curframe + 1] = NULL;
+    return err;
  }
    int map_set_for_each_callback_args(struct bpf_verifier_env *env,

kernel/bpf/verifier.c +1 -1
@@ -6970,8 +6975,7 @@ static int prepare_func_exit(struct 
 bpf_verifier_env *env, int *insn_idx)
           return -EINVAL;
       }
   -    state->curframe--;
-    caller = state->frame[state->curframe];
+    caller = state->frame[state->curframe - 1];
       if (callee->in_callback_fn) {
           /* enforce R0 return value range [0, 1]. */

          struct tnum range = callee->callback_ret_range;

kernel/bpf/verifier.c static int prepare_func_exit(struct · line 7008

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
@@ -7001,6 +7005,7 @@ static int prepare_func_exit(struct 
 bpf_verifier_env *env, int *insn_idx)
               return err;
       }
   +    state->curframe--;

nit. state->curframe is always pointing to callee upto this point?  Instead of doing another +1 dance in the latter 'state->frame[state->curframe + 1] = NULL;', how about do it later like:

Quoted 6 lines

    /* clear everything in the callee */
        free_func_state(callee);
    state->frame[state->curframe--] = NULL;

It shouldn't affect the earlier print_verifier_state() which explicitly takes callee and caller as its arg, right?

Yes, state->curframe not affect print_verifier_state(),  doing "state->curframe--"  at the end of the function is better, also will change in v4.  Thanks!

Quoted 5 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0
       *insn_idx = callee->callsite + 1;
       if (env->log.level & BPF_LOG_LEVEL) {

          verbose(env, "returning from callee:\n");