Skip to content

Commit d9a807f

Browse files
Xu Kuohaigregkh
authored andcommitted
bpf: Prevent tail call between progs attached to different hooks
commit 28ead3eaabc16ecc907cfb71876da028080f6356 upstream. bpf progs can be attached to kernel functions, and the attached functions can take different parameters or return different return values. If prog attached to one kernel function tail calls prog attached to another kernel function, the ctx access or return value verification could be bypassed. For example, if prog1 is attached to func1 which takes only 1 parameter and prog2 is attached to func2 which takes two parameters. Since verifier assumes the bpf ctx passed to prog2 is constructed based on func2's prototype, verifier allows prog2 to access the second parameter from the bpf ctx passed to it. The problem is that verifier does not prevent prog1 from passing its bpf ctx to prog2 via tail call. In this case, the bpf ctx passed to prog2 is constructed from func1 instead of func2, that is, the assumption for ctx access verification is bypassed. Another example, if BPF LSM prog1 is attached to hook file_alloc_security, and BPF LSM prog2 is attached to hook bpf_lsm_audit_rule_known. Verifier knows the return value rules for these two hooks, e.g. it is legal for bpf_lsm_audit_rule_known to return positive number 1, and it is illegal for file_alloc_security to return positive number. So verifier allows prog2 to return positive number 1, but does not allow prog1 to return positive number. The problem is that verifier does not prevent prog1 from calling prog2 via tail call. In this case, prog2's return value 1 will be used as the return value for prog1's hook file_alloc_security. That is, the return value rule is bypassed. This patch adds restriction for tail call to prevent such bypasses. Signed-off-by: Xu Kuohai <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> [Minor conflict resolved due to code context change.] Signed-off-by: Jianqi Ren <[email protected]> Signed-off-by: He Zhe <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4759acb commit d9a807f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ struct bpf_map {
250250
* same prog type, JITed flag and xdp_has_frags flag.
251251
*/
252252
struct {
253+
const struct btf_type *attach_func_proto;
253254
spinlock_t lock;
254255
enum bpf_prog_type type;
255256
bool jited;

kernel/bpf/core.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,7 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
21212121
{
21222122
enum bpf_prog_type prog_type = resolve_prog_type(fp);
21232123
bool ret;
2124+
struct bpf_prog_aux *aux = fp->aux;
21242125

21252126
if (fp->kprobe_override)
21262127
return false;
@@ -2132,12 +2133,26 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
21322133
*/
21332134
map->owner.type = prog_type;
21342135
map->owner.jited = fp->jited;
2135-
map->owner.xdp_has_frags = fp->aux->xdp_has_frags;
2136+
map->owner.xdp_has_frags = aux->xdp_has_frags;
2137+
map->owner.attach_func_proto = aux->attach_func_proto;
21362138
ret = true;
21372139
} else {
21382140
ret = map->owner.type == prog_type &&
21392141
map->owner.jited == fp->jited &&
2140-
map->owner.xdp_has_frags == fp->aux->xdp_has_frags;
2142+
map->owner.xdp_has_frags == aux->xdp_has_frags;
2143+
if (ret &&
2144+
map->owner.attach_func_proto != aux->attach_func_proto) {
2145+
switch (prog_type) {
2146+
case BPF_PROG_TYPE_TRACING:
2147+
case BPF_PROG_TYPE_LSM:
2148+
case BPF_PROG_TYPE_EXT:
2149+
case BPF_PROG_TYPE_STRUCT_OPS:
2150+
ret = false;
2151+
break;
2152+
default:
2153+
break;
2154+
}
2155+
}
21412156
}
21422157
spin_unlock(&map->owner.lock);
21432158

0 commit comments

Comments
 (0)