Skip to contentSkip to conversation

potential memleak and use after free in bpf verifier

PATCH In review He Fengqing 3 participants 13 messages Jul 7, 2021 – Jul 14, 2021

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

cover potential memleak and use after free in bpf verifier

While reading the code of bpf verifier, I found these two issues. Patch 1 move the bpf_prog_clone_free function into filter.h, so we can use it in other file. Patch 2 fix memleak in an error handling path in bpf_patch_insn_data function.
Patch 3 fix a use after free in bpf_check function.

He Fengqing (3):
bpf: Move bpf_prog_clone_free into filter.h file
bpf: Fix a memory leak in an error handling path in

    'bpf_patch_insn_data()'

bpf: Fix a use after free in bpf_check()

 include/linux/filter.h | 17 ++++++++++++-
 kernel/bpf/core.c      | 27 +++++---------------
 kernel/bpf/verifier.c  | 58 ++++++++++++++++++++++++++++++++----------
 net/core/filter.c      |  2 +-
 4 files changed, 68 insertions(+), 36 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 bpf: Move bpf_prog_clone_free into filter.h file

Move bpf_prog_clone_free function into filter.h, so we can use it in other file.

---
 include/linux/filter.h | 15 +++++++++++++++
 kernel/bpf/core.c      | 20 +-------------------
 2 files changed, 16 insertions(+), 19 deletions(-)
include/linux/filter.h +15 -0
index 472f97074da0..f39e008a377d 100644
@@ -884,6 +884,21 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
 				  gfp_t gfp_extra_flags);
 void __bpf_prog_free(struct bpf_prog *fp);
 
+static inline void bpf_prog_clone_free(struct bpf_prog *fp)
+{
+	/* aux was stolen by the other clone, so we cannot free
+	 * it from this path! It will be freed eventually by the
+	 * other program on release.
+	 *
+	 * At this point, we don't need a deferred release since
+	 * clone is guaranteed to not be locked.
+	 */
+	fp->aux = NULL;
+	fp->stats = NULL;
+	fp->active = NULL;
+	__bpf_prog_free(fp);
+}
+
 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
 {
 	__bpf_prog_free(fp);
kernel/bpf/core.c +1 -19
index 034ad93a1ad7..49b0311f48c1 100644
@@ -238,10 +238,7 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
 		/* We keep fp->aux from fp_old around in the new
 		 * reallocated structure.
 		 */
-		fp_old->aux = NULL;
-		fp_old->stats = NULL;
-		fp_old->active = NULL;
-		__bpf_prog_free(fp_old);
+		bpf_prog_clone_free(fp_old);
 	}
 
 	return fp;
@@ -1102,21 +1099,6 @@ static struct bpf_prog *bpf_prog_clone_create(struct bpf_prog *fp_other,
 	return fp;
 }
 
-static void bpf_prog_clone_free(struct bpf_prog *fp)
-{
-	/* aux was stolen by the other clone, so we cannot free
-	 * it from this path! It will be freed eventually by the
-	 * other program on release.
-	 *
-	 * At this point, we don't need a deferred release since
-	 * clone is guaranteed to not be locked.
-	 */
-	fp->aux = NULL;
-	fp->stats = NULL;
-	fp->active = NULL;
-	__bpf_prog_free(fp);
-}
-
 void bpf_jit_prog_release_other(struct bpf_prog *fp, struct bpf_prog *fp_other)
 {
 	/* We have to repoint aux->prog to self, as we don't
Signature · 1 line
2.25.1
reviewer
#2 lore raw

Re: [bpf-next 1/3] bpf: Move bpf_prog_clone_free into filter.h file

Quoted 41 lines · He Fengqing

Move bpf_prog_clone_free function into filter.h, so we can use it in other file.

---
 include/linux/filter.h | 15 +++++++++++++++
 kernel/bpf/core.c      | 20 +-------------------
 2 files changed, 16 insertions(+), 19 deletions(-)
include/linux/filter.h +15 -0
index 472f97074da0..f39e008a377d 100644
@@ -884,6 +884,21 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
                                  gfp_t gfp_extra_flags);
 void __bpf_prog_free(struct bpf_prog *fp);
 
+static inline void bpf_prog_clone_free(struct bpf_prog *fp)
+{
+       /* aux was stolen by the other clone, so we cannot free
+        * it from this path! It will be freed eventually by the
+        * other program on release.
+        *
+        * At this point, we don't need a deferred release since
+        * clone is guaranteed to not be locked.
+        */
+       fp->aux = NULL;
+       fp->stats = NULL;
+       fp->active = NULL;
+       __bpf_prog_free(fp);
+}
+
 static inline void bpf_prog_unlock_free(struct bpf_prog *fp)
 {
        __bpf_prog_free(fp);
kernel/bpf/core.c +0 -0
index 034ad93a1ad7..49b0311f48c1 100644

kernel/bpf/core.c struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, · line 240

Quoted 3 lines
kernel/bpf/core.c +0 -0
@@ -238,10 +238,7 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
                /* We keep fp->aux from fp_old around in the new
                 * reallocated structure.
                 */

After the change, we can remove the comment above.

kernel/bpf/core.c struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size, · line 241

Quoted 5 lines
kernel/bpf/core.c +1 -4
-               fp_old->aux = NULL;
-               fp_old->stats = NULL;
-               fp_old->active = NULL;
-               __bpf_prog_free(fp_old);
+               bpf_prog_clone_free(fp_old);

Please add a couple sentences in the commit log about this chanage.

Thanks,
Song

#3 lore raw

PATCH 2/3 bpf: Fix a memory leak in an error handling path in 'bpf_patch_insn_data()'

In reply to He Fengqing

In bpf_patch_insn_data function, if adjust_insn_aux_data() return error, we need to free new_prog.

---
 kernel/bpf/verifier.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +4 -1
index be38bb930bf1..41109f49b724 100644
@@ -11501,8 +11501,11 @@ static struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 of
 				env->insn_aux_data[off].orig_idx);
 		return NULL;
 	}
-	if (adjust_insn_aux_data(env, new_prog, off, len))
+	if (adjust_insn_aux_data(env, new_prog, off, len)) {
+		if (new_prog != env->prog)
+			bpf_prog_clone_free(new_prog);
 		return NULL;
+	}
 	adjust_subprog_starts(env, off, len);
 	adjust_poke_descs(new_prog, off, len);
 	return new_prog;
Signature · 1 line
2.25.1
#4 lore raw

PATCH 3/3 bpf: Fix a use after free in bpf_check()

In reply to He Fengqing

In bpf_patch_insn_data, env->prog was input parameter of bpf_patch_insn_single function. bpf_patch_insn_single call bpf_prog_realloc to realloc ebpf prog. When we need to malloc new prog, bpf_prog_realloc will free the old prog, in this scenery is the env->prog.
Then bpf_patch_insn_data function call adjust_insn_aux_data function, if adjust_insn_aux_data function return error, bpf_patch_insn_data will return NULL.
In bpf_check->convert_ctx_accesses->bpf_patch_insn_data call chain, if bpf_patch_insn_data return NULL, env->prog has been freed in bpf_prog_realloc, then bpf_check will use the freed env->prog.

---
 include/linux/filter.h |  2 +-
 kernel/bpf/core.c      |  9 ++++---
 kernel/bpf/verifier.c  | 53 ++++++++++++++++++++++++++++++++----------
 net/core/filter.c      |  2 +-
 4 files changed, 49 insertions(+), 17 deletions(-)
include/linux/filter.h +1 -1
index f39e008a377d..ec11a5ae92c2 100644
@@ -881,7 +881,7 @@ void bpf_prog_jit_attempt_done(struct bpf_prog *prog);
 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags);
 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
-				  gfp_t gfp_extra_flags);
+				  gfp_t gfp_extra_flags, bool free_old);
 void __bpf_prog_free(struct bpf_prog *fp);
 
 static inline void bpf_prog_clone_free(struct bpf_prog *fp)
kernel/bpf/core.c +6 -3
index 49b0311f48c1..e5616bb1665b 100644
@@ -218,7 +218,7 @@ void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
 }
 
 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
-				  gfp_t gfp_extra_flags)
+				  gfp_t gfp_extra_flags, bool free_old)
 {
 	gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
 	struct bpf_prog *fp;
@@ -238,7 +238,8 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
 		/* We keep fp->aux from fp_old around in the new
 		 * reallocated structure.
 		 */
-		bpf_prog_clone_free(fp_old);
+		if (free_old)
+			bpf_prog_clone_free(fp_old);
 	}
 
 	return fp;
@@ -456,7 +457,7 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
 	 * last page could have large enough tailroom.
 	 */
 	prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
-				    GFP_USER);
+				    GFP_USER, false);
 	if (!prog_adj)
 		return ERR_PTR(-ENOMEM);
 
@@ -1150,6 +1151,8 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
 			return tmp;
 		}
 
+		if (tmp != clone)
+			bpf_prog_clone_free(clone);
 		clone = tmp;
 		insn_delta = rewritten - 1;
 
kernel/bpf/verifier.c +41 -12

113 lines not on this page · raw message

net/core/filter.c +1 -1
index d70187ce851b..8a8d1a3ba5c2 100644
@@ -1268,7 +1268,7 @@ static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
 
 	/* Expand fp for appending the new filter representation. */
 	old_fp = fp;
-	fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
+	fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0, true);
 	if (!fp) {
 		/* The old_fp is still around in case we couldn't
 		 * allocate new memory, so uncharge on that one.
Signature · 1 line
2.25.1
reviewer
#5 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 12 lines · He Fengqing

In bpf_patch_insn_data, env->prog was input parameter of bpf_patch_insn_single function. bpf_patch_insn_single call bpf_prog_realloc to realloc ebpf prog. When we need to malloc new prog, bpf_prog_realloc will free the old prog, in this scenery is the env->prog.
Then bpf_patch_insn_data function call adjust_insn_aux_data function, if adjust_insn_aux_data function return error, bpf_patch_insn_data will return NULL.
In bpf_check->convert_ctx_accesses->bpf_patch_insn_data call chain, if bpf_patch_insn_data return NULL, env->prog has been freed in bpf_prog_realloc, then bpf_check will use the freed env->prog.

Besides "what is the bug", please also describe "how to fix it". For example, add "Fix it by adding a free_old argument to bpf_prog_realloc(), and ...". Also, for the subject of 0/3, it is better to say "fix potential memory leak and ...".

Quoted 68 lines
---
 include/linux/filter.h |  2 +-
 kernel/bpf/core.c      |  9 ++++---
 kernel/bpf/verifier.c  | 53 ++++++++++++++++++++++++++++++++----------
 net/core/filter.c      |  2 +-
 4 files changed, 49 insertions(+), 17 deletions(-)
include/linux/filter.h +1 -1
index f39e008a377d..ec11a5ae92c2 100644
@@ -881,7 +881,7 @@ void bpf_prog_jit_attempt_done(struct bpf_prog *prog);
 struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
 struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags);
 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
-                                 gfp_t gfp_extra_flags);
+                                 gfp_t gfp_extra_flags, bool free_old);
 void __bpf_prog_free(struct bpf_prog *fp);
 
 static inline void bpf_prog_clone_free(struct bpf_prog *fp)
kernel/bpf/core.c +6 -3
index 49b0311f48c1..e5616bb1665b 100644
@@ -218,7 +218,7 @@ void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
 }
 
 struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
-                                 gfp_t gfp_extra_flags)
+                                 gfp_t gfp_extra_flags, bool free_old)
 {
        gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
        struct bpf_prog *fp;
@@ -238,7 +238,8 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
                /* We keep fp->aux from fp_old around in the new
                 * reallocated structure.
                 */
-               bpf_prog_clone_free(fp_old);
+               if (free_old)
+                       bpf_prog_clone_free(fp_old);
        }
 
        return fp;
@@ -456,7 +457,7 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
         * last page could have large enough tailroom.
         */
        prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
-                                   GFP_USER);
+                                   GFP_USER, false);
        if (!prog_adj)
                return ERR_PTR(-ENOMEM);
 
@@ -1150,6 +1151,8 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
                        return tmp;
                }
 
+               if (tmp != clone)
+                       bpf_prog_clone_free(clone);
                clone = tmp;
                insn_delta = rewritten - 1;
 
kernel/bpf/verifier.c +0 -0
index 41109f49b724..e75b933f69e4 100644

kernel/bpf/verifier.c static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, · line 11861

Quoted 8 lines
kernel/bpf/verifier.c +4 -1
@@ -11855,7 +11855,10 @@ static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
                new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
                if (!new_prog)
                        return -ENOMEM;
-               env->prog = new_prog;
+               if (new_prog != env->prog) {
+                       bpf_prog_clone_free(env->prog);
+                       env->prog = new_prog;
+               }

Can we move this check into bpf_patch_insn_data()?

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
                insns = new_prog->insnsi;
                aux = env->insn_aux_data;
                delta += patch_len - 1;

[...]

net/core/filter.c static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) · line 1271

Quoted 10 lines
net/core/filter.c +1 -1
index d70187ce851b..8a8d1a3ba5c2 100644
@@ -1268,7 +1268,7 @@ static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
 
        /* Expand fp for appending the new filter representation. */
        old_fp = fp;
-       fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
+       fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0, true);

Can we add some logic here and not add free_old to bpf_prog_realloc()?

Thanks,
Song

#6 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 14 lines · Song Liu
Quoted 12 lines · He Fengqing

In bpf_patch_insn_data, env->prog was input parameter of bpf_patch_insn_single function. bpf_patch_insn_single call bpf_prog_realloc to realloc ebpf prog. When we need to malloc new prog, bpf_prog_realloc will free the old prog, in this scenery is the env->prog.
Then bpf_patch_insn_data function call adjust_insn_aux_data function, if adjust_insn_aux_data function return error, bpf_patch_insn_data will return NULL.
In bpf_check->convert_ctx_accesses->bpf_patch_insn_data call chain, if bpf_patch_insn_data return NULL, env->prog has been freed in bpf_prog_realloc, then bpf_check will use the freed env->prog.

Quoted 4 lines

Besides "what is the bug", please also describe "how to fix it". For example, add "Fix it by adding a free_old argument to bpf_prog_realloc(), and ...". Also, for the subject of 0/3, it is better to say "fix potential memory leak and ...".

Thanks for your suggestion.

Quoted 64 lines
Quoted 64 lines
---
  include/linux/filter.h |  2 +-
  kernel/bpf/core.c      |  9 ++++---
  kernel/bpf/verifier.c  | 53 ++++++++++++++++++++++++++++++++----------
  net/core/filter.c      |  2 +-
  4 files changed, 49 insertions(+), 17 deletions(-)
include/linux/filter.h +1 -1
index f39e008a377d..ec11a5ae92c2 100644
@@ -881,7 +881,7 @@ void bpf_prog_jit_attempt_done(struct bpf_prog *prog);
  struct bpf_prog *bpf_prog_alloc(unsigned int size, gfp_t gfp_extra_flags);
  struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flags);
  struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
-                                 gfp_t gfp_extra_flags);
+                                 gfp_t gfp_extra_flags, bool free_old);
  void __bpf_prog_free(struct bpf_prog *fp);
 
  static inline void bpf_prog_clone_free(struct bpf_prog *fp)
kernel/bpf/core.c +6 -3
index 49b0311f48c1..e5616bb1665b 100644
@@ -218,7 +218,7 @@ void bpf_prog_fill_jited_linfo(struct bpf_prog *prog,
  }
 
  struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
-                                 gfp_t gfp_extra_flags)
+                                 gfp_t gfp_extra_flags, bool free_old)
  {
         gfp_t gfp_flags = GFP_KERNEL_ACCOUNT | __GFP_ZERO | gfp_extra_flags;
         struct bpf_prog *fp;
@@ -238,7 +238,8 @@ struct bpf_prog *bpf_prog_realloc(struct bpf_prog *fp_old, unsigned int size,
                 /* We keep fp->aux from fp_old around in the new
                  * reallocated structure.
                  */
-               bpf_prog_clone_free(fp_old);
+               if (free_old)
+                       bpf_prog_clone_free(fp_old);
         }
 
         return fp;
@@ -456,7 +457,7 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
          * last page could have large enough tailroom.
          */
         prog_adj = bpf_prog_realloc(prog, bpf_prog_size(insn_adj_cnt),
-                                   GFP_USER);
+                                   GFP_USER, false);
         if (!prog_adj)
                 return ERR_PTR(-ENOMEM);
 
@@ -1150,6 +1151,8 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
                         return tmp;
                 }
 
+               if (tmp != clone)
+                       bpf_prog_clone_free(clone);
                 clone = tmp;
                 insn_delta = rewritten - 1;
 
Quoted 15 lines

kernel/bpf/verifier.c static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env, · line 11861

Quoted 13 lines
kernel/bpf/verifier.c +4 -1
index 41109f49b724..e75b933f69e4 100644
@@ -11855,7 +11855,10 @@ static int opt_subreg_zext_lo32_rnd_hi32(struct bpf_verifier_env *env,
                 new_prog = bpf_patch_insn_data(env, adj_idx, patch, patch_len);
                 if (!new_prog)
                         return -ENOMEM;
-               env->prog = new_prog;
+               if (new_prog != env->prog) {
+                       bpf_prog_clone_free(env->prog);
+                       env->prog = new_prog;
+               }

Can we move this check into bpf_patch_insn_data()?

Ok, I will change this in next version.

Quoted 12 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0
                 insns = new_prog->insnsi;
                 aux = env->insn_aux_data;
                 delta += patch_len - 1;

[...]

Quoted 6 lines
net/core/filter.c +0 -0
index d70187ce851b..8a8d1a3ba5c2 100644
@@ -1268,7 +1268,7 @@ static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp)
 
Quoted 6 lines

net/core/filter.c static struct bpf_prog *bpf_migrate_filter(struct bpf_prog *fp) · line 1271

Quoted 4 lines
net/core/filter.c +1 -1
         /* Expand fp for appending the new filter representation. */
         old_fp = fp;
-       fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0);
+       fp = bpf_prog_realloc(old_fp, bpf_prog_size(new_len), 0, true);

Can we add some logic here and not add free_old to bpf_prog_realloc()?

Ok, maybe we can free old_fp here, never in bpf_prog_realloc.

Quoted 5 lines

Thanks,
Song
.

#7 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 2 lines · He Fengqing

Ok, I will change this in next version.

before you spam the list with the next version
please explain why any of these changes are needed?
I don't see an explanation in the patches and I don't see a bug in the code. Did you check what is the prog clone ?
When is it constructed? Why verifier has anything to do with it?

#8 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 11 lines · Alexei Starovoitov
Quoted 2 lines · He Fengqing

Ok, I will change this in next version.

before you spam the list with the next version
please explain why any of these changes are needed?
I don't see an explanation in the patches and I don't see a bug in the code. Did you check what is the prog clone ?
When is it constructed? Why verifier has anything to do with it? .

I'm sorry, I didn't describe these errors clearly.

bpf_check(bpf_verifier_env)

    |
    |->do_misc_fixups(env)
    |    |
    |    |->bpf_patch_insn_data(env)
    |    |    |
    |    |    |->bpf_patch_insn_single(env->prog)
    |    |    |    |
    |    |    |    |->bpf_prog_realloc(env->prog)
    |    |    |    |    |
    |    |    |    |    |->construct new_prog
    |    |    |    |    |    free old_prog(env->prog)
    |    |    |    |    |
    |    |    |    |    |->return new_prog;
    |    |    |    |
    |    |    |    |->return new_prog;
    |    |    |
    |    |    |->adjust_insn_aux_data
    |    |    |    |
    |    |    |    |->return ENOMEM;
    |    |    |
    |    |    |->return NULL;
    |    |
    |    |->return ENOMEM;

bpf_verifier_env->prog had been freed in bpf_prog_realloc function.

There are two errors here, the first is memleak in the bpf_patch_insn_data function, and the second is use after free in the bpf_check function.

memleak in bpf_patch_insn_data:

Look at the call chain above, if adjust_insn_aux_data function return ENOMEM, bpf_patch_insn_data will return NULL, but we do not free the new_prog.

So in the patch 2, before bpf_patch_insn_data return NULL, we free the new_prog.

use after free in bpf_check:

If bpf_patch_insn_data function return NULL, we will not assign new_prog to the bpf_verifier_env->prog, but bpf_verifier_env->prog has been freed in the bpf_prog_realloc function. Then in bpf_check function, we will use bpf_verifier_env->prog after do_misc_fixups function.

In the patch 3, I added a free_old parameter to bpf_prog_realloc, in this scenario we don't free old_prog. Instead, we free it in the do_misc_fixups function when bpf_patch_insn_data return a valid new_prog.

Thanks for your reviews.

#9 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 62 lines · He Fengqing
Quoted 11 lines · Alexei Starovoitov
Quoted 2 lines · He Fengqing

Ok, I will change this in next version.

before you spam the list with the next version
please explain why any of these changes are needed?
I don't see an explanation in the patches and I don't see a bug in the code. Did you check what is the prog clone ?
When is it constructed? Why verifier has anything to do with it? .

I'm sorry, I didn't describe these errors clearly.

bpf_check(bpf_verifier_env)

     |
     |->do_misc_fixups(env)
     |    |
     |    |->bpf_patch_insn_data(env)
     |    |    |
     |    |    |->bpf_patch_insn_single(env->prog)
     |    |    |    |
     |    |    |    |->bpf_prog_realloc(env->prog)
     |    |    |    |    |
     |    |    |    |    |->construct new_prog
     |    |    |    |    |    free old_prog(env->prog)
     |    |    |    |    |
     |    |    |    |    |->return new_prog;
     |    |    |    |
     |    |    |    |->return new_prog;
     |    |    |
     |    |    |->adjust_insn_aux_data
     |    |    |    |
     |    |    |    |->return ENOMEM;
     |    |    |
     |    |    |->return NULL;
     |    |
     |    |->return ENOMEM;

bpf_verifier_env->prog had been freed in bpf_prog_realloc function.

There are two errors here, the first is memleak in the bpf_patch_insn_data function, and the second is use after free in the bpf_check function.

memleak in bpf_patch_insn_data:

Look at the call chain above, if adjust_insn_aux_data function return ENOMEM, bpf_patch_insn_data will return NULL, but we do not free the new_prog.

So in the patch 2, before bpf_patch_insn_data return NULL, we free the new_prog.

use after free in bpf_check:

Quoted 8 lines

If bpf_patch_insn_data function return NULL, we will not assign new_prog to the bpf_verifier_env->prog, but bpf_verifier_env->prog has been freed in the bpf_prog_realloc function. Then in bpf_check function, we will use bpf_verifier_env->prog after do_misc_fixups function.

In the patch 3, I added a free_old parameter to bpf_prog_realloc, in this scenario we don't free old_prog. Instead, we free it in the do_misc_fixups function when bpf_patch_insn_data return a valid new_prog.

Thanks for explaining.
Why not to make adjust_insn_aux_data() in bpf_patch_insn_data() first then? Just changing the order will resolve both issues, no?

#10 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 68 lines · Alexei Starovoitov
Quoted 67 lines · He Fengqing
Quoted 11 lines · Alexei Starovoitov
Quoted 2 lines · He Fengqing

Ok, I will change this in next version.

before you spam the list with the next version
please explain why any of these changes are needed?
I don't see an explanation in the patches and I don't see a bug in the code. Did you check what is the prog clone ?
When is it constructed? Why verifier has anything to do with it? .

I'm sorry, I didn't describe these errors clearly.

bpf_check(bpf_verifier_env)

      |
      |->do_misc_fixups(env)
      |    |
      |    |->bpf_patch_insn_data(env)
      |    |    |
      |    |    |->bpf_patch_insn_single(env->prog)
      |    |    |    |
      |    |    |    |->bpf_prog_realloc(env->prog)
      |    |    |    |    |
      |    |    |    |    |->construct new_prog
      |    |    |    |    |    free old_prog(env->prog)
      |    |    |    |    |
      |    |    |    |    |->return new_prog;
      |    |    |    |
      |    |    |    |->return new_prog;
      |    |    |
      |    |    |->adjust_insn_aux_data
      |    |    |    |
      |    |    |    |->return ENOMEM;
      |    |    |
      |    |    |->return NULL;
      |    |
      |    |->return ENOMEM;

bpf_verifier_env->prog had been freed in bpf_prog_realloc function.

There are two errors here, the first is memleak in the bpf_patch_insn_data function, and the second is use after free in the bpf_check function.

memleak in bpf_patch_insn_data:

Look at the call chain above, if adjust_insn_aux_data function return ENOMEM, bpf_patch_insn_data will return NULL, but we do not free the new_prog.

So in the patch 2, before bpf_patch_insn_data return NULL, we free the new_prog.

use after free in bpf_check:

If bpf_patch_insn_data function return NULL, we will not assign new_prog to the bpf_verifier_env->prog, but bpf_verifier_env->prog has been freed in the bpf_prog_realloc function. Then in bpf_check function, we will use bpf_verifier_env->prog after do_misc_fixups function.

Quoted 9 lines
Quoted 3 lines

In the patch 3, I added a free_old parameter to bpf_prog_realloc, in this scenario we don't free old_prog. Instead, we free it in the do_misc_fixups function when bpf_patch_insn_data return a valid new_prog.

Thanks for explaining.
Why not to make adjust_insn_aux_data() in bpf_patch_insn_data() first then? Just changing the order will resolve both issues, no?
.

adjust_insn_aux_data() need the new constructed new_prog as an input parameter, so we must call bpf_patch_insn_single() before adjust_insn_aux_data().

But we can make adjust_insn_aux_data() never return ENOMEM. In bpf_patch_insn_data(), first we pre-malloc memory for new aux_data, then call bpf_patch_insn_single() to constructed the new_prog, at last call adjust_insn_aux_data() functin. In this way, adjust_insn_aux_data() never fails.

bpf_patch_insn_data(env) {
	struct bpf_insn_aux_data *new_data = vzalloc();
	struct bpf_prog *new_prog;
	if (new_data == NULL)
		return NULL;

	new_prog = bpf_patch_insn_single(env->prog);
	if (new_prog == NULL) {
		vfree(new_data);
		return NULL;
	}

	adjust_insn_aux_data(new_prog, new_data);
	return new_prog;
}

What do you think about it?

#11 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 76 lines · He Fengqing
Quoted 72 lines · Alexei Starovoitov
Quoted 70 lines · He Fengqing
Quoted 11 lines · Alexei Starovoitov
Quoted 2 lines · He Fengqing

Ok, I will change this in next version.

before you spam the list with the next version
please explain why any of these changes are needed?
I don't see an explanation in the patches and I don't see a bug in the code. Did you check what is the prog clone ?
When is it constructed? Why verifier has anything to do with it? .

I'm sorry, I didn't describe these errors clearly.

bpf_check(bpf_verifier_env)

      |
      |->do_misc_fixups(env)
      |    |
      |    |->bpf_patch_insn_data(env)
      |    |    |
      |    |    |->bpf_patch_insn_single(env->prog)
      |    |    |    |
      |    |    |    |->bpf_prog_realloc(env->prog)
      |    |    |    |    |
      |    |    |    |    |->construct new_prog
      |    |    |    |    |    free old_prog(env->prog)
      |    |    |    |    |
      |    |    |    |    |->return new_prog;
      |    |    |    |
      |    |    |    |->return new_prog;
      |    |    |
      |    |    |->adjust_insn_aux_data
      |    |    |    |
      |    |    |    |->return ENOMEM;
      |    |    |
      |    |    |->return NULL;
      |    |
      |    |->return ENOMEM;

bpf_verifier_env->prog had been freed in bpf_prog_realloc function.

There are two errors here, the first is memleak in the bpf_patch_insn_data function, and the second is use after free in the bpf_check function.

memleak in bpf_patch_insn_data:

Look at the call chain above, if adjust_insn_aux_data function return ENOMEM, bpf_patch_insn_data will return NULL, but we do not free the new_prog.

So in the patch 2, before bpf_patch_insn_data return NULL, we free the new_prog.

use after free in bpf_check:

If bpf_patch_insn_data function return NULL, we will not assign new_prog to the bpf_verifier_env->prog, but bpf_verifier_env->prog has been freed in the bpf_prog_realloc function. Then in bpf_check function, we will use bpf_verifier_env->prog after do_misc_fixups function.

In the patch 3, I added a free_old parameter to bpf_prog_realloc, in this scenario we don't free old_prog. Instead, we free it in the do_misc_fixups function when bpf_patch_insn_data return a valid new_prog.

Quoted 8 lines
Quoted 5 lines

Thanks for explaining.
Why not to make adjust_insn_aux_data() in bpf_patch_insn_data() first then? Just changing the order will resolve both issues, no?
.

adjust_insn_aux_data() need the new constructed new_prog as an input parameter, so we must call bpf_patch_insn_single() before adjust_insn_aux_data().

Right. I forgot about insn_has_def32() logic and commit b325fbca4b13 ("bpf: verifier: mark patched-insn with sub-register zext flag")
that added that extra parameter.

Quoted 21 lines

But we can make adjust_insn_aux_data() never return ENOMEM. In bpf_patch_insn_data(), first we pre-malloc memory for new aux_data, then call bpf_patch_insn_single() to constructed the new_prog, at last call adjust_insn_aux_data() functin. In this way, adjust_insn_aux_data() never fails.

bpf_patch_insn_data(env) {
        struct bpf_insn_aux_data *new_data = vzalloc();
        struct bpf_prog *new_prog;
        if (new_data == NULL)
                return NULL;

        new_prog = bpf_patch_insn_single(env->prog);
        if (new_prog == NULL) {
                vfree(new_data);
                return NULL;
        }

        adjust_insn_aux_data(new_prog, new_data);
        return new_prog;
}
Quoted 1 line

What do you think about it?

That's a good idea. Let's do that. The new size for vzalloc is easy to compute. What should be the commit in the Fixes tag?
commit 8041902dae52 ("bpf: adjust insn_aux_data when patching insns") right?
4 year old bug then.
I wonder why syzbot with malloc error injection didn't catch it sooner.

#12 lore raw

Re: [bpf-next 3/3] bpf: Fix a use after free in bpf_check()

Quoted 114 lines · Alexei Starovoitov
Quoted 76 lines · He Fengqing
Quoted 72 lines · Alexei Starovoitov
Quoted 70 lines · He Fengqing
Quoted 11 lines · Alexei Starovoitov
Quoted 2 lines · He Fengqing

Ok, I will change this in next version.

before you spam the list with the next version
please explain why any of these changes are needed?
I don't see an explanation in the patches and I don't see a bug in the code. Did you check what is the prog clone ?
When is it constructed? Why verifier has anything to do with it? .

I'm sorry, I didn't describe these errors clearly.

bpf_check(bpf_verifier_env)

       |
       |->do_misc_fixups(env)
       |    |
       |    |->bpf_patch_insn_data(env)
       |    |    |
       |    |    |->bpf_patch_insn_single(env->prog)
       |    |    |    |
       |    |    |    |->bpf_prog_realloc(env->prog)
       |    |    |    |    |
       |    |    |    |    |->construct new_prog
       |    |    |    |    |    free old_prog(env->prog)
       |    |    |    |    |
       |    |    |    |    |->return new_prog;
       |    |    |    |
       |    |    |    |->return new_prog;
       |    |    |
       |    |    |->adjust_insn_aux_data
       |    |    |    |
       |    |    |    |->return ENOMEM;
       |    |    |
       |    |    |->return NULL;
       |    |
       |    |->return ENOMEM;

bpf_verifier_env->prog had been freed in bpf_prog_realloc function.

There are two errors here, the first is memleak in the bpf_patch_insn_data function, and the second is use after free in the bpf_check function.

memleak in bpf_patch_insn_data:

Look at the call chain above, if adjust_insn_aux_data function return ENOMEM, bpf_patch_insn_data will return NULL, but we do not free the new_prog.

So in the patch 2, before bpf_patch_insn_data return NULL, we free the new_prog.

use after free in bpf_check:

If bpf_patch_insn_data function return NULL, we will not assign new_prog to the bpf_verifier_env->prog, but bpf_verifier_env->prog has been freed in the bpf_prog_realloc function. Then in bpf_check function, we will use bpf_verifier_env->prog after do_misc_fixups function.

In the patch 3, I added a free_old parameter to bpf_prog_realloc, in this scenario we don't free old_prog. Instead, we free it in the do_misc_fixups function when bpf_patch_insn_data return a valid new_prog.

Quoted 8 lines
Quoted 5 lines

Thanks for explaining.
Why not to make adjust_insn_aux_data() in bpf_patch_insn_data() first then? Just changing the order will resolve both issues, no?
.

adjust_insn_aux_data() need the new constructed new_prog as an input parameter, so we must call bpf_patch_insn_single() before adjust_insn_aux_data().

Right. I forgot about insn_has_def32() logic and commit b325fbca4b13 ("bpf: verifier: mark patched-insn with sub-register zext flag")
that added that extra parameter.

Quoted 21 lines

But we can make adjust_insn_aux_data() never return ENOMEM. In bpf_patch_insn_data(), first we pre-malloc memory for new aux_data, then call bpf_patch_insn_single() to constructed the new_prog, at last call adjust_insn_aux_data() functin. In this way, adjust_insn_aux_data() never fails.

bpf_patch_insn_data(env) {
         struct bpf_insn_aux_data *new_data = vzalloc();
         struct bpf_prog *new_prog;
         if (new_data == NULL)
                 return NULL;

         new_prog = bpf_patch_insn_single(env->prog);
         if (new_prog == NULL) {
                 vfree(new_data);
                 return NULL;
         }

         adjust_insn_aux_data(new_prog, new_data);
         return new_prog;
}
Quoted 1 line

What do you think about it?

Quoted 4 lines

That's a good idea. Let's do that. The new size for vzalloc is easy to compute. What should be the commit in the Fixes tag?
commit 8041902dae52 ("bpf: adjust insn_aux_data when patching insns") right?

Ok, I will add this in the commit message.

Quoted 4 lines

4 year old bug then.
I wonder why syzbot with malloc error injection didn't catch it sooner. .