bpf: Fix tr dereferencing
PATCH 1/1 bpf: Fix tr dereferencing
Fix 'tr' dereferencing bug when CONFIG_BPF_JIT is turned off.
Like 'bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, when CONFIG_BPF_JIT is turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.
--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)
kernel/bpf/syscall.c +2 -2
@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock; } }else {
kernel/bpf/trampoline.c +3 -3
@@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,bpf_lsm_find_cgroup_shim (prog, &bpf_func); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);mutex_lock (&tr->mutex);
@@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key, tr =bpf_trampoline_lookup (key);if (!tr) -return NULL ; +return ERR_PTR (-ENOMEM );mutex_lock (&tr->mutex);if (tr->func.addr)
kernel/bpf/verifier.c +2 -2
@@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);if (tgt_prog && tgt_prog->aux->tail_call_reachable) tr->flags =BPF_TRAMP_F_TAIL_CALL_CTX ;
base-commit: cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c
Signature · 1 line
2.41.0
Quoted 7 lines · Leon Hwang
Fix 'tr' dereferencing bug when
CONFIG_BPF_JITis turned off.Like '
bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, whenCONFIG_BPF_JITis turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.
Quoted 7 lines
Fixes 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")Fixes f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Leon Hwang <[email protected]>
it does not apply cleanly on bpf/master for me, but nice catch
jirka
Quoted 66 lines
--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)kernel/bpf/syscall.c +2
-2@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock; } }else {kernel/bpf/trampoline.c +3
-3@@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,bpf_lsm_find_cgroup_shim (prog, &bpf_func); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);mutex_lock (&tr->mutex);@@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key, tr =bpf_trampoline_lookup (key);if (!tr) -return NULL ; +return ERR_PTR (-ENOMEM );mutex_lock (&tr->mutex);if (tr->func.addr)kernel/bpf/verifier.c +2
-2@@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);if (tgt_prog && tgt_prog->aux->tail_call_reachable) tr->flags =BPF_TRAMP_F_TAIL_CALL_CTX ;base-commit:
cbb1dbcd99b0ae74c45c4c83c6d213c12c31785cSignature · 1 line
2.41.0
Quoted 26 lines · Leon Hwang
Fix 'tr' dereferencing bug when
CONFIG_BPF_JITis turned off.Like '
bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, whenCONFIG_BPF_JITis turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.Fixes 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")Fixes f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)kernel/bpf/syscall.c +0
-0
kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3216
Quoted 8 lines
kernel/bpf/syscall.c +2
-2@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock;
IS_ERR does not check the null case, so this should be IS_ERR_OR_NULL instead.
Quoted 46 lines
kernel/bpf/syscall.c +0
-0} }else {kernel/bpf/trampoline.c +3
-3@@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,bpf_lsm_find_cgroup_shim (prog, &bpf_func); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);mutex_lock (&tr->mutex);@@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key, tr =bpf_trampoline_lookup (key);if (!tr) -return NULL ; +return ERR_PTR (-ENOMEM );mutex_lock (&tr->mutex);if (tr->func.addr)kernel/bpf/verifier.c +2
-2@@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);if (tgt_prog && tgt_prog->aux->tail_call_reachable) tr->flags =BPF_TRAMP_F_TAIL_CALL_CTX ;base-commit:
cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c--
2.41.0
Quoted 29 lines · Hengqi Chen
Quoted 28 lines · Leon Hwang
Fix 'tr' dereferencing bug when
CONFIG_BPF_JITis turned off.Like '
bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, whenCONFIG_BPF_JITis turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.Fixes 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")Fixes f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)kernel/bpf/syscall.c +0
-0@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, }
Quoted 8 lines
kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3216
Quoted 6 lines
kernel/bpf/syscall.c +2
-2tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock;
IS_ERRdoes not check the null case, so this should beIS_ERR_OR_NULLinstead.
Actually, bpf_trampoline_get() would not return NULL. It returns ERR_PTR(-ENOMEM) or a valid ptr.
Thanks,
Leon
Quoted 47 lines
Quoted 47 lines
kernel/bpf/syscall.c +0
-0} }else {kernel/bpf/trampoline.c +3
-3@@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,bpf_lsm_find_cgroup_shim (prog, &bpf_func); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);mutex_lock (&tr->mutex);@@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key, tr =bpf_trampoline_lookup (key);if (!tr) -return NULL ; +return ERR_PTR (-ENOMEM );mutex_lock (&tr->mutex);if (tr->func.addr)kernel/bpf/verifier.c +2
-2@@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);if (tgt_prog && tgt_prog->aux->tail_call_reachable) tr->flags =BPF_TRAMP_F_TAIL_CALL_CTX ;base-commit:
cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c--
2.41.0
Hengqi Chen
Reviewed-byQuoted 42 lines · Leon Hwang
Quoted 29 lines · Hengqi Chen
Quoted 28 lines · Leon Hwang
Fix 'tr' dereferencing bug when
CONFIG_BPF_JITis turned off.Like '
bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, whenCONFIG_BPF_JITis turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.Fixes 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")Fixes f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)kernel/bpf/syscall.c +0
-0@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, }Quoted 8 lines
kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3216
Quoted 6 lines
kernel/bpf/syscall.c +2
-2tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock;
IS_ERRdoes not check the null case, so this should beIS_ERR_OR_NULLinstead.
Quoted 3 lines
Actually,
bpf_trampoline_get()would not return NULL. It returnsERR_PTR(-ENOMEM)or a valid ptr.
OK, I missed the change in bpf_trampoline_get(). Anyway,
Quoted 50 lines
Thanks,
LeonQuoted 47 lines
Quoted 47 lines
kernel/bpf/syscall.c +0
-0} }else {kernel/bpf/trampoline.c +3
-3@@ -697,8 +697,8 @@ int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,bpf_lsm_find_cgroup_shim (prog, &bpf_func); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);mutex_lock (&tr->mutex);@@ -775,7 +775,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key, tr =bpf_trampoline_lookup (key);if (!tr) -return NULL ; +return ERR_PTR (-ENOMEM );mutex_lock (&tr->mutex);if (tr->func.addr)kernel/bpf/verifier.c +2
-2@@ -19771,8 +19771,8 @@ static int check_attach_btf_id(struct bpf_verifier_env *env) key =bpf_trampoline_compute_key (tgt_prog, prog->aux->attach_btf, btf_id); tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) -return -ENOMEM ; +if (IS_ERR (tr)) +return PTR_ERR (tr);if (tgt_prog && tgt_prog->aux->tail_call_reachable) tr->flags =BPF_TRAMP_F_TAIL_CALL_CTX ;base-commit:
cbb1dbcd99b0ae74c45c4c83c6d213c12c31785c--
2.41.0
Quoted 44 lines · Hengqi Chen
Quoted 42 lines · Leon Hwang
Quoted 37 lines · Hengqi Chen
Quoted 26 lines · Leon Hwang
Fix 'tr' dereferencing bug when
CONFIG_BPF_JITis turned off.Like '
bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, whenCONFIG_BPF_JITis turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.Fixes 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")Fixes f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)kernel/bpf/syscall.c +0
-0kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3216
Quoted 8 lines
kernel/bpf/syscall.c +2
-2@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, } tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock;
IS_ERRdoes not check the null case, so this should beIS_ERR_OR_NULLinstead.
Quoted 7 lines
Quoted 3 lines
Actually,
bpf_trampoline_get()would not return NULL. It returnsERR_PTR(-ENOMEM)or a valid ptr.OK, I missed the change in
bpf_trampoline_get(). Anyway,Reviewed-by Hengqi Chen <[email protected]>
That's too much churn to address !JIT config.
Just make it return NULL in that case,
instead of hacking things all over the place.
Quoted 49 lines · Alexei Starovoitov
Quoted 48 lines · Hengqi Chen
Quoted 45 lines · Leon Hwang
Quoted 29 lines · Hengqi Chen
Quoted 28 lines · Leon Hwang
Fix 'tr' dereferencing bug when
CONFIG_BPF_JITis turned off.Like '
bpf_trampoline_get_progs()', return 'ERR_PTR()' and then check by 'IS_ERR()'. As a result, whenCONFIG_BPF_JITis turned off, it's able to handle the case that 'bpf_trampoline_get()' returns 'ERR_PTR(-EOPNOTSUPP)'.Fixes 4a1e7c0c63e0 ("bpf: Support attaching freplace programs to multiple attach points")Fixes f7b12b6fea00 ("bpf: verifier: refactor check_attach_btf_id()")Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")Reported-by kernel test robot <[email protected]>Reported-by Dan Carpenter <[email protected]>Signed-off-by Leon Hwang <[email protected]>--- kernel/bpf/syscall.c | 4 ++-- kernel/bpf/trampoline.c | 6 +++--- kernel/bpf/verifier.c | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-)kernel/bpf/syscall.c +0
-0@@ -3211,8 +3211,8 @@ static int bpf_tracing_prog_attach(struct bpf_prog *prog, }Quoted 8 lines
kernel/bpf/syscall.c static int bpf_tracing_prog_attach(struct bpf_prog *prog, · line 3216
Quoted 6 lines
kernel/bpf/syscall.c +2
-2tr =bpf_trampoline_get (key, &tgt_info); -if (!tr) { - err = -ENOMEM ; +if (IS_ERR (tr)) { + err =PTR_ERR (tr); goto out_unlock;
IS_ERRdoes not check the null case, so this should beIS_ERR_OR_NULLinstead.Actually,
bpf_trampoline_get()would not return NULL. It returnsERR_PTR(-ENOMEM)or a valid ptr.
Quoted 7 lines
Quoted 3 lines
OK, I missed the change in
bpf_trampoline_get(). Anyway,Reviewed-by Hengqi Chen <[email protected]>That's too much churn to address !JIT config.
Just make it return NULL in that case,
instead of hacking things all over the place.
OK, I'll do it in v2 patch.
Thanks,
Leon