Skip to content

Commit fc611f4

Browse files
sinkapborkmann
authored andcommitted
bpf: Introduce BPF_PROG_TYPE_LSM
Introduce types and configs for bpf programs that can be attached to LSM hooks. The programs can be enabled by the config option CONFIG_BPF_LSM. Signed-off-by: KP Singh <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Brendan Jackman <[email protected]> Reviewed-by: Florent Revest <[email protected]> Reviewed-by: Thomas Garnier <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Acked-by: James Morris <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent e5fb60e commit fc611f4

File tree

10 files changed

+49
-6
lines changed

10 files changed

+49
-6
lines changed

MAINTAINERS

+1
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,7 @@ R: Martin KaFai Lau <[email protected]>
31473147
R: Song Liu <[email protected]>
31483148
R: Yonghong Song <[email protected]>
31493149
R: Andrii Nakryiko <[email protected]>
3150+
R: KP Singh <[email protected]>
31503151
31513152
31523153
T: git git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git

include/linux/bpf.h

+3
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,9 @@ extern const struct bpf_func_proto bpf_tcp_sock_proto;
15151515
extern const struct bpf_func_proto bpf_jiffies64_proto;
15161516
extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
15171517

1518+
const struct bpf_func_proto *bpf_tracing_func_proto(
1519+
enum bpf_func_id func_id, const struct bpf_prog *prog);
1520+
15181521
/* Shared helpers among cBPF and eBPF. */
15191522
void bpf_user_rnd_init_once(void);
15201523
u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);

include/linux/bpf_types.h

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_STRUCT_OPS, bpf_struct_ops,
7070
void *, void *)
7171
BPF_PROG_TYPE(BPF_PROG_TYPE_EXT, bpf_extension,
7272
void *, void *)
73+
#ifdef CONFIG_BPF_LSM
74+
BPF_PROG_TYPE(BPF_PROG_TYPE_LSM, lsm,
75+
void *, void *)
76+
#endif /* CONFIG_BPF_LSM */
7377
#endif
7478

7579
BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)

include/uapi/linux/bpf.h

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ enum bpf_prog_type {
181181
BPF_PROG_TYPE_TRACING,
182182
BPF_PROG_TYPE_STRUCT_OPS,
183183
BPF_PROG_TYPE_EXT,
184+
BPF_PROG_TYPE_LSM,
184185
};
185186

186187
enum bpf_attach_type {
@@ -211,6 +212,7 @@ enum bpf_attach_type {
211212
BPF_TRACE_FENTRY,
212213
BPF_TRACE_FEXIT,
213214
BPF_MODIFY_RETURN,
215+
BPF_LSM_MAC,
214216
__MAX_BPF_ATTACH_TYPE
215217
};
216218

init/Kconfig

+12
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,18 @@ config KALLSYMS_BASE_RELATIVE
16161616
# end of the "standard kernel features (expert users)" menu
16171617

16181618
# syscall, maps, verifier
1619+
1620+
config BPF_LSM
1621+
bool "LSM Instrumentation with BPF"
1622+
depends on BPF_SYSCALL
1623+
depends on SECURITY
1624+
depends on BPF_JIT
1625+
help
1626+
Enables instrumentation of the security hooks with eBPF programs for
1627+
implementing dynamic MAC and Audit Policies.
1628+
1629+
If you are unsure how to answer this question, answer N.
1630+
16191631
config BPF_SYSCALL
16201632
bool "Enable bpf() system call"
16211633
select BPF

kernel/bpf/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ obj-$(CONFIG_DEBUG_INFO_BTF) += sysfs_btf.o
2929
endif
3030
ifeq ($(CONFIG_BPF_JIT),y)
3131
obj-$(CONFIG_BPF_SYSCALL) += bpf_struct_ops.o
32+
obj-${CONFIG_BPF_LSM} += bpf_lsm.o
3233
endif

kernel/bpf/bpf_lsm.c

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/*
4+
* Copyright (C) 2020 Google LLC.
5+
*/
6+
7+
#include <linux/filter.h>
8+
#include <linux/bpf.h>
9+
#include <linux/btf.h>
10+
11+
const struct bpf_prog_ops lsm_prog_ops = {
12+
};
13+
14+
const struct bpf_verifier_ops lsm_verifier_ops = {
15+
.get_func_proto = bpf_tracing_func_proto,
16+
.is_valid_access = btf_ctx_access,
17+
};

kernel/trace/bpf_trace.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ static const struct bpf_func_proto bpf_send_signal_thread_proto = {
779779
.arg1_type = ARG_ANYTHING,
780780
};
781781

782-
static const struct bpf_func_proto *
783-
tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
782+
const struct bpf_func_proto *
783+
bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
784784
{
785785
switch (func_id) {
786786
case BPF_FUNC_map_lookup_elem:
@@ -865,7 +865,7 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
865865
return &bpf_override_return_proto;
866866
#endif
867867
default:
868-
return tracing_func_proto(func_id, prog);
868+
return bpf_tracing_func_proto(func_id, prog);
869869
}
870870
}
871871

@@ -975,7 +975,7 @@ tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
975975
case BPF_FUNC_get_stack:
976976
return &bpf_get_stack_proto_tp;
977977
default:
978-
return tracing_func_proto(func_id, prog);
978+
return bpf_tracing_func_proto(func_id, prog);
979979
}
980980
}
981981

@@ -1082,7 +1082,7 @@ pe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10821082
case BPF_FUNC_read_branch_records:
10831083
return &bpf_read_branch_records_proto;
10841084
default:
1085-
return tracing_func_proto(func_id, prog);
1085+
return bpf_tracing_func_proto(func_id, prog);
10861086
}
10871087
}
10881088

@@ -1210,7 +1210,7 @@ raw_tp_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
12101210
case BPF_FUNC_get_stack:
12111211
return &bpf_get_stack_proto_raw_tp;
12121212
default:
1213-
return tracing_func_proto(func_id, prog);
1213+
return bpf_tracing_func_proto(func_id, prog);
12141214
}
12151215
}
12161216

tools/include/uapi/linux/bpf.h

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ enum bpf_prog_type {
181181
BPF_PROG_TYPE_TRACING,
182182
BPF_PROG_TYPE_STRUCT_OPS,
183183
BPF_PROG_TYPE_EXT,
184+
BPF_PROG_TYPE_LSM,
184185
};
185186

186187
enum bpf_attach_type {
@@ -211,6 +212,7 @@ enum bpf_attach_type {
211212
BPF_TRACE_FENTRY,
212213
BPF_TRACE_FEXIT,
213214
BPF_MODIFY_RETURN,
215+
BPF_LSM_MAC,
214216
__MAX_BPF_ATTACH_TYPE
215217
};
216218

tools/lib/bpf/libbpf_probes.c

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ probe_load(enum bpf_prog_type prog_type, const struct bpf_insn *insns,
108108
case BPF_PROG_TYPE_TRACING:
109109
case BPF_PROG_TYPE_STRUCT_OPS:
110110
case BPF_PROG_TYPE_EXT:
111+
case BPF_PROG_TYPE_LSM:
111112
default:
112113
break;
113114
}

0 commit comments

Comments
 (0)