Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 28ead3e

Browse files
Xu Kuohaianakryiko
authored andcommitted
bpf: Prevent tail call between progs attached to different hooks
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]>
1 parent 5d99e19 commit 28ead3e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ struct bpf_map {
294294
* same prog type, JITed flag and xdp_has_frags flag.
295295
*/
296296
struct {
297+
const struct btf_type *attach_func_proto;
297298
spinlock_t lock;
298299
enum bpf_prog_type type;
299300
bool jited;

kernel/bpf/core.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,7 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
23022302
{
23032303
enum bpf_prog_type prog_type = resolve_prog_type(fp);
23042304
bool ret;
2305+
struct bpf_prog_aux *aux = fp->aux;
23052306

23062307
if (fp->kprobe_override)
23072308
return false;
@@ -2311,7 +2312,7 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
23112312
* in the case of devmap and cpumap). Until device checks
23122313
* are implemented, prohibit adding dev-bound programs to program maps.
23132314
*/
2314-
if (bpf_prog_is_dev_bound(fp->aux))
2315+
if (bpf_prog_is_dev_bound(aux))
23152316
return false;
23162317

23172318
spin_lock(&map->owner.lock);
@@ -2321,12 +2322,26 @@ bool bpf_prog_map_compatible(struct bpf_map *map,
23212322
*/
23222323
map->owner.type = prog_type;
23232324
map->owner.jited = fp->jited;
2324-
map->owner.xdp_has_frags = fp->aux->xdp_has_frags;
2325+
map->owner.xdp_has_frags = aux->xdp_has_frags;
2326+
map->owner.attach_func_proto = aux->attach_func_proto;
23252327
ret = true;
23262328
} else {
23272329
ret = map->owner.type == prog_type &&
23282330
map->owner.jited == fp->jited &&
2329-
map->owner.xdp_has_frags == fp->aux->xdp_has_frags;
2331+
map->owner.xdp_has_frags == aux->xdp_has_frags;
2332+
if (ret &&
2333+
map->owner.attach_func_proto != aux->attach_func_proto) {
2334+
switch (prog_type) {
2335+
case BPF_PROG_TYPE_TRACING:
2336+
case BPF_PROG_TYPE_LSM:
2337+
case BPF_PROG_TYPE_EXT:
2338+
case BPF_PROG_TYPE_STRUCT_OPS:
2339+
ret = false;
2340+
break;
2341+
default:
2342+
break;
2343+
}
2344+
}
23302345
}
23312346
spin_unlock(&map->owner.lock);
23322347

0 commit comments

Comments
 (0)