Skip to content

Commit f50b49a

Browse files
sinkapAlexei Starovoitov
authored andcommitted
bpf: btf: Fix arg verification in btf_ctx_access()
The bounds checking for the arguments accessed in the BPF program breaks when the expected_attach_type is not BPF_TRACE_FEXIT, BPF_LSM_MAC or BPF_MODIFY_RETURN resulting in no check being done for the default case (the programs which do not receive the return value of the attached function in its arguments) when the index of the argument being accessed is equal to the number of arguments (nr_args). This was a result of a misplaced "else if" block introduced by the Commit 6ba43b7 ("bpf: Attachment verification for BPF_MODIFY_RETURN") Fixes: 6ba43b7 ("bpf: Attachment verification for BPF_MODIFY_RETURN") Reported-by: Jann Horn <[email protected]> Signed-off-by: KP Singh <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0fc31b1 commit f50b49a

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

kernel/bpf/btf.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3709,9 +3709,16 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
37093709
nr_args--;
37103710
}
37113711

3712+
if (arg > nr_args) {
3713+
bpf_log(log, "func '%s' doesn't have %d-th argument\n",
3714+
tname, arg + 1);
3715+
return false;
3716+
}
3717+
37123718
if (arg == nr_args) {
3713-
if (prog->expected_attach_type == BPF_TRACE_FEXIT ||
3714-
prog->expected_attach_type == BPF_LSM_MAC) {
3719+
switch (prog->expected_attach_type) {
3720+
case BPF_LSM_MAC:
3721+
case BPF_TRACE_FEXIT:
37153722
/* When LSM programs are attached to void LSM hooks
37163723
* they use FEXIT trampolines and when attached to
37173724
* int LSM hooks, they use MODIFY_RETURN trampolines.
@@ -3728,7 +3735,8 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
37283735
if (!t)
37293736
return true;
37303737
t = btf_type_by_id(btf, t->type);
3731-
} else if (prog->expected_attach_type == BPF_MODIFY_RETURN) {
3738+
break;
3739+
case BPF_MODIFY_RETURN:
37323740
/* For now the BPF_MODIFY_RETURN can only be attached to
37333741
* functions that return an int.
37343742
*/
@@ -3742,17 +3750,19 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type,
37423750
btf_kind_str[BTF_INFO_KIND(t->info)]);
37433751
return false;
37443752
}
3753+
break;
3754+
default:
3755+
bpf_log(log, "func '%s' doesn't have %d-th argument\n",
3756+
tname, arg + 1);
3757+
return false;
37453758
}
3746-
} else if (arg >= nr_args) {
3747-
bpf_log(log, "func '%s' doesn't have %d-th argument\n",
3748-
tname, arg + 1);
3749-
return false;
37503759
} else {
37513760
if (!t)
37523761
/* Default prog with 5 args */
37533762
return true;
37543763
t = btf_type_by_id(btf, args[arg].type);
37553764
}
3765+
37563766
/* skip modifiers */
37573767
while (btf_type_is_modifier(t))
37583768
t = btf_type_by_id(btf, t->type);

0 commit comments

Comments
 (0)