Skip to content

Commit 6ba43b7

Browse files
sinkapAlexei Starovoitov
authored andcommitted
bpf: Attachment verification for BPF_MODIFY_RETURN
- Allow BPF_MODIFY_RETURN attachment only to functions that are: * Whitelisted for error injection by checking within_error_injection_list. Similar discussions happened for the bpf_override_return helper. * security hooks, this is expected to be cleaned up with the LSM changes after the KRSI patches introduce the LSM_HOOK macro: https://lore.kernel.org/bpf/20200220175250.10795-1-kpsingh@chromium.org/ - The attachment is currently limited to functions that return an int. This can be extended later other types (e.g. PTR). Signed-off-by: KP Singh <kpsingh@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200304191853.1529-5-kpsingh@chromium.org
1 parent ae24082 commit 6ba43b7

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

kernel/bpf/btf.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,14 +3710,26 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
37103710
nr_args--;
37113711
}
37123712

3713-
if ((prog->expected_attach_type == BPF_TRACE_FEXIT ||
3714-
prog->expected_attach_type == BPF_MODIFY_RETURN) &&
3715-
arg == nr_args) {
3716-
if (!t)
3717-
/* Default prog with 5 args. 6th arg is retval. */
3718-
return true;
3719-
/* function return type */
3720-
t = btf_type_by_id(btf, t->type);
3713+
if (arg == nr_args) {
3714+
if (prog->expected_attach_type == BPF_TRACE_FEXIT) {
3715+
if (!t)
3716+
return true;
3717+
t = btf_type_by_id(btf, t->type);
3718+
} else if (prog->expected_attach_type == BPF_MODIFY_RETURN) {
3719+
/* For now the BPF_MODIFY_RETURN can only be attached to
3720+
* functions that return an int.
3721+
*/
3722+
if (!t)
3723+
return false;
3724+
3725+
t = btf_type_skip_modifiers(btf, t->type, NULL);
3726+
if (!btf_type_is_int(t)) {
3727+
bpf_log(log,
3728+
"ret type %s not allowed for fmod_ret\n",
3729+
btf_kind_str[BTF_INFO_KIND(t->info)]);
3730+
return false;
3731+
}
3732+
}
37213733
} else if (arg >= nr_args) {
37223734
bpf_log(log, "func '%s' doesn't have %d-th argument\n",
37233735
tname, arg + 1);

kernel/bpf/verifier.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/sort.h>
2020
#include <linux/perf_event.h>
2121
#include <linux/ctype.h>
22+
#include <linux/error-injection.h>
2223

2324
#include "disasm.h"
2425

@@ -9800,6 +9801,33 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
98009801

98019802
return 0;
98029803
}
9804+
#define SECURITY_PREFIX "security_"
9805+
9806+
static int check_attach_modify_return(struct bpf_verifier_env *env)
9807+
{
9808+
struct bpf_prog *prog = env->prog;
9809+
unsigned long addr = (unsigned long) prog->aux->trampoline->func.addr;
9810+
9811+
if (within_error_injection_list(addr))
9812+
return 0;
9813+
9814+
/* This is expected to be cleaned up in the future with the KRSI effort
9815+
* introducing the LSM_HOOK macro for cleaning up lsm_hooks.h.
9816+
*/
9817+
if (!strncmp(SECURITY_PREFIX, prog->aux->attach_func_name,
9818+
sizeof(SECURITY_PREFIX) - 1)) {
9819+
9820+
if (!capable(CAP_MAC_ADMIN))
9821+
return -EPERM;
9822+
9823+
return 0;
9824+
}
9825+
9826+
verbose(env, "fmod_ret attach_btf_id %u (%s) is not modifiable\n",
9827+
prog->aux->attach_btf_id, prog->aux->attach_func_name);
9828+
9829+
return -EINVAL;
9830+
}
98039831

98049832
static int check_attach_btf_id(struct bpf_verifier_env *env)
98059833
{
@@ -10000,6 +10028,9 @@ static int check_attach_btf_id(struct bpf_verifier_env *env)
1000010028
}
1000110029
tr->func.addr = (void *)addr;
1000210030
prog->aux->trampoline = tr;
10031+
10032+
if (prog->expected_attach_type == BPF_MODIFY_RETURN)
10033+
ret = check_attach_modify_return(env);
1000310034
out:
1000410035
mutex_unlock(&tr->mutex);
1000510036
if (ret)

0 commit comments

Comments
 (0)