Skip to content

Commit c9f9df3

Browse files
committed
Daniel Borkmann says: ==================== pull-request: bpf 2024-05-13 We've added 3 non-merge commits during the last 2 day(s) which contain a total of 2 files changed, 62 insertions(+), 8 deletions(-). The main changes are: 1) Fix a case where syzkaller found that it's unexpectedly possible to attach a cgroup_skb program to the sockopt hooks. The fix adds missing attach_type enforcement for the link_create case along with selftests, from Stanislav Fomichev. * tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: selftests/bpf: Add sockopt case to verify prog_type selftests/bpf: Extend sockopt tests to use BPF_LINK_CREATE bpf: Add BPF_PROG_TYPE_CGROUP_SKB attach type enforcement in BPF_LINK_CREATE ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 1164057 + 3e9bc04 commit c9f9df3

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

kernel/bpf/syscall.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3985,6 +3985,11 @@ static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
39853985
* check permissions at attach time.
39863986
*/
39873987
return -EPERM;
3988+
3989+
ptype = attach_type_to_prog_type(attach_type);
3990+
if (prog->type != ptype)
3991+
return -EINVAL;
3992+
39883993
return prog->enforce_expected_attach_type &&
39893994
prog->expected_attach_type != attach_type ?
39903995
-EINVAL : 0;

tools/testing/selftests/bpf/prog_tests/sockopt.c

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum sockopt_test_error {
2424
static struct sockopt_test {
2525
const char *descr;
2626
const struct bpf_insn insns[64];
27+
enum bpf_prog_type prog_type;
2728
enum bpf_attach_type attach_type;
2829
enum bpf_attach_type expected_attach_type;
2930

@@ -928,9 +929,40 @@ static struct sockopt_test {
928929

929930
.error = EPERM_SETSOCKOPT,
930931
},
932+
933+
/* ==================== prog_type ==================== */
934+
935+
{
936+
.descr = "can attach only BPF_CGROUP_SETSOCKOP",
937+
.insns = {
938+
/* return 1 */
939+
BPF_MOV64_IMM(BPF_REG_0, 1),
940+
BPF_EXIT_INSN(),
941+
942+
},
943+
.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
944+
.attach_type = BPF_CGROUP_SETSOCKOPT,
945+
.expected_attach_type = 0,
946+
.error = DENY_ATTACH,
947+
},
948+
949+
{
950+
.descr = "can attach only BPF_CGROUP_GETSOCKOP",
951+
.insns = {
952+
/* return 1 */
953+
BPF_MOV64_IMM(BPF_REG_0, 1),
954+
BPF_EXIT_INSN(),
955+
956+
},
957+
.prog_type = BPF_PROG_TYPE_CGROUP_SKB,
958+
.attach_type = BPF_CGROUP_GETSOCKOPT,
959+
.expected_attach_type = 0,
960+
.error = DENY_ATTACH,
961+
},
931962
};
932963

933964
static int load_prog(const struct bpf_insn *insns,
965+
enum bpf_prog_type prog_type,
934966
enum bpf_attach_type expected_attach_type)
935967
{
936968
LIBBPF_OPTS(bpf_prog_load_opts, opts,
@@ -947,7 +979,7 @@ static int load_prog(const struct bpf_insn *insns,
947979
}
948980
insns_cnt++;
949981

950-
fd = bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCKOPT, NULL, "GPL", insns, insns_cnt, &opts);
982+
fd = bpf_prog_load(prog_type, NULL, "GPL", insns, insns_cnt, &opts);
951983
if (verbose && fd < 0)
952984
fprintf(stderr, "%s\n", bpf_log_buf);
953985

@@ -1036,13 +1068,18 @@ static int call_getsockopt(bool use_io_uring, int fd, int level, int optname,
10361068
return getsockopt(fd, level, optname, optval, optlen);
10371069
}
10381070

1039-
static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
1071+
static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring,
1072+
bool use_link)
10401073
{
1041-
int sock_fd, err, prog_fd;
1074+
int prog_type = BPF_PROG_TYPE_CGROUP_SOCKOPT;
1075+
int sock_fd, err, prog_fd, link_fd = -1;
10421076
void *optval = NULL;
10431077
int ret = 0;
10441078

1045-
prog_fd = load_prog(test->insns, test->expected_attach_type);
1079+
if (test->prog_type)
1080+
prog_type = test->prog_type;
1081+
1082+
prog_fd = load_prog(test->insns, prog_type, test->expected_attach_type);
10461083
if (prog_fd < 0) {
10471084
if (test->error == DENY_LOAD)
10481085
return 0;
@@ -1051,7 +1088,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
10511088
return -1;
10521089
}
10531090

1054-
err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
1091+
if (use_link) {
1092+
err = bpf_link_create(prog_fd, cgroup_fd, test->attach_type, NULL);
1093+
link_fd = err;
1094+
} else {
1095+
err = bpf_prog_attach(prog_fd, cgroup_fd, test->attach_type, 0);
1096+
}
10551097
if (err < 0) {
10561098
if (test->error == DENY_ATTACH)
10571099
goto close_prog_fd;
@@ -1142,7 +1184,12 @@ static int run_test(int cgroup_fd, struct sockopt_test *test, bool use_io_uring)
11421184
close_sock_fd:
11431185
close(sock_fd);
11441186
detach_prog:
1145-
bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
1187+
if (use_link) {
1188+
if (link_fd >= 0)
1189+
close(link_fd);
1190+
} else {
1191+
bpf_prog_detach2(prog_fd, cgroup_fd, test->attach_type);
1192+
}
11461193
close_prog_fd:
11471194
close(prog_fd);
11481195
return ret;
@@ -1160,10 +1207,12 @@ void test_sockopt(void)
11601207
if (!test__start_subtest(tests[i].descr))
11611208
continue;
11621209

1163-
ASSERT_OK(run_test(cgroup_fd, &tests[i], false),
1210+
ASSERT_OK(run_test(cgroup_fd, &tests[i], false, false),
1211+
tests[i].descr);
1212+
ASSERT_OK(run_test(cgroup_fd, &tests[i], false, true),
11641213
tests[i].descr);
11651214
if (tests[i].io_uring_support)
1166-
ASSERT_OK(run_test(cgroup_fd, &tests[i], true),
1215+
ASSERT_OK(run_test(cgroup_fd, &tests[i], true, false),
11671216
tests[i].descr);
11681217
}
11691218

0 commit comments

Comments
 (0)