On Sun, Feb 18, 2024 at 11:55:01AM +0100, Christophe Leroy wrote:
> set_memory_ro() can fail, leaving memory unprotected.
> 
> Check its return and take it into account as an error.
> 
> Signed-off-by: Christophe Leroy <[email protected]>
> ---
>  include/linux/filter.h | 5 +++--
>  kernel/bpf/core.c      | 4 +++-
>  kernel/bpf/verifier.c  | 4 +++-
>  3 files changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/include/linux/filter.h b/include/linux/filter.h
> index fee070b9826e..fc0994dc5c72 100644
> --- a/include/linux/filter.h
> +++ b/include/linux/filter.h
> @@ -881,14 +881,15 @@ bpf_ctx_narrow_access_offset(u32 off, u32 size, u32 
> size_default)
>  
>  #define bpf_classic_proglen(fprog) (fprog->len * sizeof(fprog->filter[0]))
>  
> -static inline void bpf_prog_lock_ro(struct bpf_prog *fp)
> +static inline int __must_check bpf_prog_lock_ro(struct bpf_prog *fp)
>  {
>  #ifndef CONFIG_BPF_JIT_ALWAYS_ON
>       if (!fp->jited) {
>               set_vm_flush_reset_perms(fp);
> -             set_memory_ro((unsigned long)fp, fp->pages);
> +             return set_memory_ro((unsigned long)fp, fp->pages);
>       }
>  #endif
> +     return 0;
>  }
>  
>  static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 71c459a51d9e..c49619ef55d0 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -2392,7 +2392,9 @@ struct bpf_prog *bpf_prog_select_runtime(struct 
> bpf_prog *fp, int *err)
>       }
>  
>  finalize:
> -     bpf_prog_lock_ro(fp);
> +     *err = bpf_prog_lock_ro(fp);
> +     if (*err)
> +             return fp;

Weird error path, but yes.

>  
>       /* The tail call compatibility check can only be done at
>        * this late stage as we need to determine, if we deal
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index c5d68a9d8acc..1f831a6b4bbc 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -19020,7 +19020,9 @@ static int jit_subprogs(struct bpf_verifier_env *env)
>        * bpf_prog_load will add the kallsyms for the main program.
>        */
>       for (i = 1; i < env->subprog_cnt; i++) {
> -             bpf_prog_lock_ro(func[i]);
> +             err = bpf_prog_lock_ro(func[i]);
> +             if (err)
> +                     goto out_free;
>               bpf_prog_kallsyms_add(func[i]);
>       }

Just to double-check if memory permissions being correctly restored on
this error path, I walked back through it and see that it ultimately
lands on vfree(), which appears to just throw the entire mapping away,
so I think that's safe. :)

Reviewed-by: Kees Cook <[email protected]>

-- 
Kees Cook

Reply via email to