xref: /OK3568_Linux_fs/kernel/Documentation/bpf/bpf_lsm.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun.. Copyright (C) 2020 Google LLC.
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun================
5*4882a593SmuzhiyunLSM BPF Programs
6*4882a593Smuzhiyun================
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunThese BPF programs allow runtime instrumentation of the LSM hooks by privileged
9*4882a593Smuzhiyunusers to implement system-wide MAC (Mandatory Access Control) and Audit
10*4882a593Smuzhiyunpolicies using eBPF.
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunStructure
13*4882a593Smuzhiyun---------
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunThe example shows an eBPF program that can be attached to the ``file_mprotect``
16*4882a593SmuzhiyunLSM hook:
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun.. c:function:: int file_mprotect(struct vm_area_struct *vma, unsigned long reqprot, unsigned long prot);
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunOther LSM hooks which can be instrumented can be found in
21*4882a593Smuzhiyun``include/linux/lsm_hooks.h``.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyuneBPF programs that use :doc:`/bpf/btf` do not need to include kernel headers
24*4882a593Smuzhiyunfor accessing information from the attached eBPF program's context. They can
25*4882a593Smuzhiyunsimply declare the structures in the eBPF program and only specify the fields
26*4882a593Smuzhiyunthat need to be accessed.
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun.. code-block:: c
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun	struct mm_struct {
31*4882a593Smuzhiyun		unsigned long start_brk, brk, start_stack;
32*4882a593Smuzhiyun	} __attribute__((preserve_access_index));
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun	struct vm_area_struct {
35*4882a593Smuzhiyun		unsigned long start_brk, brk, start_stack;
36*4882a593Smuzhiyun		unsigned long vm_start, vm_end;
37*4882a593Smuzhiyun		struct mm_struct *vm_mm;
38*4882a593Smuzhiyun	} __attribute__((preserve_access_index));
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun.. note:: The order of the fields is irrelevant.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunThis can be further simplified (if one has access to the BTF information at
44*4882a593Smuzhiyunbuild time) by generating the ``vmlinux.h`` with:
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun.. code-block:: console
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun	# bpftool btf dump file <path-to-btf-vmlinux> format c > vmlinux.h
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun.. note:: ``path-to-btf-vmlinux`` can be ``/sys/kernel/btf/vmlinux`` if the
51*4882a593Smuzhiyun	  build environment matches the environment the BPF programs are
52*4882a593Smuzhiyun	  deployed in.
53*4882a593Smuzhiyun
54*4882a593SmuzhiyunThe ``vmlinux.h`` can then simply be included in the BPF programs without
55*4882a593Smuzhiyunrequiring the definition of the types.
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunThe eBPF programs can be declared using the``BPF_PROG``
58*4882a593Smuzhiyunmacros defined in `tools/lib/bpf/bpf_tracing.h`_. In this
59*4882a593Smuzhiyunexample:
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun	* ``"lsm/file_mprotect"`` indicates the LSM hook that the program must
62*4882a593Smuzhiyun	  be attached to
63*4882a593Smuzhiyun	* ``mprotect_audit`` is the name of the eBPF program
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun.. code-block:: c
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun	SEC("lsm/file_mprotect")
68*4882a593Smuzhiyun	int BPF_PROG(mprotect_audit, struct vm_area_struct *vma,
69*4882a593Smuzhiyun		     unsigned long reqprot, unsigned long prot, int ret)
70*4882a593Smuzhiyun	{
71*4882a593Smuzhiyun		/* ret is the return value from the previous BPF program
72*4882a593Smuzhiyun		 * or 0 if it's the first hook.
73*4882a593Smuzhiyun		 */
74*4882a593Smuzhiyun		if (ret != 0)
75*4882a593Smuzhiyun			return ret;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun		int is_heap;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun		is_heap = (vma->vm_start >= vma->vm_mm->start_brk &&
80*4882a593Smuzhiyun			   vma->vm_end <= vma->vm_mm->brk);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun		/* Return an -EPERM or write information to the perf events buffer
83*4882a593Smuzhiyun		 * for auditing
84*4882a593Smuzhiyun		 */
85*4882a593Smuzhiyun		if (is_heap)
86*4882a593Smuzhiyun			return -EPERM;
87*4882a593Smuzhiyun	}
88*4882a593Smuzhiyun
89*4882a593SmuzhiyunThe ``__attribute__((preserve_access_index))`` is a clang feature that allows
90*4882a593Smuzhiyunthe BPF verifier to update the offsets for the access at runtime using the
91*4882a593Smuzhiyun:doc:`/bpf/btf` information. Since the BPF verifier is aware of the types, it
92*4882a593Smuzhiyunalso validates all the accesses made to the various types in the eBPF program.
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunLoading
95*4882a593Smuzhiyun-------
96*4882a593Smuzhiyun
97*4882a593SmuzhiyuneBPF programs can be loaded with the :manpage:`bpf(2)` syscall's
98*4882a593Smuzhiyun``BPF_PROG_LOAD`` operation:
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun.. code-block:: c
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun	struct bpf_object *obj;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun	obj = bpf_object__open("./my_prog.o");
105*4882a593Smuzhiyun	bpf_object__load(obj);
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunThis can be simplified by using a skeleton header generated by ``bpftool``:
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun.. code-block:: console
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun	# bpftool gen skeleton my_prog.o > my_prog.skel.h
112*4882a593Smuzhiyun
113*4882a593Smuzhiyunand the program can be loaded by including ``my_prog.skel.h`` and using
114*4882a593Smuzhiyunthe generated helper, ``my_prog__open_and_load``.
115*4882a593Smuzhiyun
116*4882a593SmuzhiyunAttachment to LSM Hooks
117*4882a593Smuzhiyun-----------------------
118*4882a593Smuzhiyun
119*4882a593SmuzhiyunThe LSM allows attachment of eBPF programs as LSM hooks using :manpage:`bpf(2)`
120*4882a593Smuzhiyunsyscall's ``BPF_RAW_TRACEPOINT_OPEN`` operation or more simply by
121*4882a593Smuzhiyunusing the libbpf helper ``bpf_program__attach_lsm``.
122*4882a593Smuzhiyun
123*4882a593SmuzhiyunThe program can be detached from the LSM hook by *destroying* the ``link``
124*4882a593Smuzhiyunlink returned by ``bpf_program__attach_lsm`` using ``bpf_link__destroy``.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunOne can also use the helpers generated in ``my_prog.skel.h`` i.e.
127*4882a593Smuzhiyun``my_prog__attach`` for attachment and ``my_prog__destroy`` for cleaning up.
128*4882a593Smuzhiyun
129*4882a593SmuzhiyunExamples
130*4882a593Smuzhiyun--------
131*4882a593Smuzhiyun
132*4882a593SmuzhiyunAn example eBPF program can be found in
133*4882a593Smuzhiyun`tools/testing/selftests/bpf/progs/lsm.c`_ and the corresponding
134*4882a593Smuzhiyunuserspace code in `tools/testing/selftests/bpf/prog_tests/test_lsm.c`_
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun.. Links
137*4882a593Smuzhiyun.. _tools/lib/bpf/bpf_tracing.h:
138*4882a593Smuzhiyun   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/lib/bpf/bpf_tracing.h
139*4882a593Smuzhiyun.. _tools/testing/selftests/bpf/progs/lsm.c:
140*4882a593Smuzhiyun   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/progs/lsm.c
141*4882a593Smuzhiyun.. _tools/testing/selftests/bpf/prog_tests/test_lsm.c:
142*4882a593Smuzhiyun   https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/test_lsm.c
143