aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorAndrey Ignatov <rdna@fb.com>2019-02-27 15:59:24 -0500
committerAlexei Starovoitov <ast@kernel.org>2019-04-12 16:54:58 -0400
commit7b146cebe30cb481b0f70d85779da938da818637 (patch)
tree11dbbeb42b32557d345e6dac2baf2881cbe5adb5 /include
parentb1cd609d9b517f01867c211bd520cc805db3068a (diff)
bpf: Sysctl hook
Containerized applications may run as root and it may create problems for whole host. Specifically such applications may change a sysctl and affect applications in other containers. Furthermore in existing infrastructure it may not be possible to just completely disable writing to sysctl, instead such a process should be gradual with ability to log what sysctl are being changed by a container, investigate, limit the set of writable sysctl to currently used ones (so that new ones can not be changed) and eventually reduce this set to zero. The patch introduces new program type BPF_PROG_TYPE_CGROUP_SYSCTL and attach type BPF_CGROUP_SYSCTL to solve these problems on cgroup basis. New program type has access to following minimal context: struct bpf_sysctl { __u32 write; }; Where @write indicates whether sysctl is being read (= 0) or written (= 1). Helpers to access sysctl name and value will be introduced separately. BPF_CGROUP_SYSCTL attach point is added to sysctl code right before passing control to ctl_table->proc_handler so that BPF program can either allow or deny access to sysctl. Suggested-by: Roman Gushchin <guro@fb.com> Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'include')
-rw-r--r--include/linux/bpf-cgroup.h18
-rw-r--r--include/linux/bpf_types.h1
-rw-r--r--include/linux/filter.h8
-rw-r--r--include/uapi/linux/bpf.h9
4 files changed, 36 insertions, 0 deletions
diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.h
index a4c644c1c091..b1c45da20a26 100644
--- a/include/linux/bpf-cgroup.h
+++ b/include/linux/bpf-cgroup.h
@@ -17,6 +17,8 @@ struct bpf_map;
17struct bpf_prog; 17struct bpf_prog;
18struct bpf_sock_ops_kern; 18struct bpf_sock_ops_kern;
19struct bpf_cgroup_storage; 19struct bpf_cgroup_storage;
20struct ctl_table;
21struct ctl_table_header;
20 22
21#ifdef CONFIG_CGROUP_BPF 23#ifdef CONFIG_CGROUP_BPF
22 24
@@ -109,6 +111,10 @@ int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
109int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor, 111int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
110 short access, enum bpf_attach_type type); 112 short access, enum bpf_attach_type type);
111 113
114int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
115 struct ctl_table *table, int write,
116 enum bpf_attach_type type);
117
112static inline enum bpf_cgroup_storage_type cgroup_storage_type( 118static inline enum bpf_cgroup_storage_type cgroup_storage_type(
113 struct bpf_map *map) 119 struct bpf_map *map)
114{ 120{
@@ -253,6 +259,17 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
253 \ 259 \
254 __ret; \ 260 __ret; \
255}) 261})
262
263
264#define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write) \
265({ \
266 int __ret = 0; \
267 if (cgroup_bpf_enabled) \
268 __ret = __cgroup_bpf_run_filter_sysctl(head, table, write, \
269 BPF_CGROUP_SYSCTL); \
270 __ret; \
271})
272
256int cgroup_bpf_prog_attach(const union bpf_attr *attr, 273int cgroup_bpf_prog_attach(const union bpf_attr *attr,
257 enum bpf_prog_type ptype, struct bpf_prog *prog); 274 enum bpf_prog_type ptype, struct bpf_prog *prog);
258int cgroup_bpf_prog_detach(const union bpf_attr *attr, 275int cgroup_bpf_prog_detach(const union bpf_attr *attr,
@@ -321,6 +338,7 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
321#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; }) 338#define BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk, uaddr, t_ctx) ({ 0; })
322#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; }) 339#define BPF_CGROUP_RUN_PROG_SOCK_OPS(sock_ops) ({ 0; })
323#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; }) 340#define BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type,major,minor,access) ({ 0; })
341#define BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write) ({ 0; })
324 342
325#define for_each_cgroup_storage_type(stype) for (; false; ) 343#define for_each_cgroup_storage_type(stype) for (; false; )
326 344
diff --git a/include/linux/bpf_types.h b/include/linux/bpf_types.h
index 08bf2f1fe553..d26991a16894 100644
--- a/include/linux/bpf_types.h
+++ b/include/linux/bpf_types.h
@@ -28,6 +28,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_RAW_TRACEPOINT, raw_tracepoint)
28#endif 28#endif
29#ifdef CONFIG_CGROUP_BPF 29#ifdef CONFIG_CGROUP_BPF
30BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_DEVICE, cg_dev) 30BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_DEVICE, cg_dev)
31BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SYSCTL, cg_sysctl)
31#endif 32#endif
32#ifdef CONFIG_BPF_LIRC_MODE2 33#ifdef CONFIG_BPF_LIRC_MODE2
33BPF_PROG_TYPE(BPF_PROG_TYPE_LIRC_MODE2, lirc_mode2) 34BPF_PROG_TYPE(BPF_PROG_TYPE_LIRC_MODE2, lirc_mode2)
diff --git a/include/linux/filter.h b/include/linux/filter.h
index 6074aa064b54..a17732057880 100644
--- a/include/linux/filter.h
+++ b/include/linux/filter.h
@@ -33,6 +33,8 @@ struct bpf_prog_aux;
33struct xdp_rxq_info; 33struct xdp_rxq_info;
34struct xdp_buff; 34struct xdp_buff;
35struct sock_reuseport; 35struct sock_reuseport;
36struct ctl_table;
37struct ctl_table_header;
36 38
37/* ArgX, context and stack frame pointer register positions. Note, 39/* ArgX, context and stack frame pointer register positions. Note,
38 * Arg1, Arg2, Arg3, etc are used as argument mappings of function 40 * Arg1, Arg2, Arg3, etc are used as argument mappings of function
@@ -1177,4 +1179,10 @@ struct bpf_sock_ops_kern {
1177 */ 1179 */
1178}; 1180};
1179 1181
1182struct bpf_sysctl_kern {
1183 struct ctl_table_header *head;
1184 struct ctl_table *table;
1185 int write;
1186};
1187
1180#endif /* __LINUX_FILTER_H__ */ 1188#endif /* __LINUX_FILTER_H__ */
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 2e96d0b4bf65..cc2a2466d5f3 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -167,6 +167,7 @@ enum bpf_prog_type {
167 BPF_PROG_TYPE_LIRC_MODE2, 167 BPF_PROG_TYPE_LIRC_MODE2,
168 BPF_PROG_TYPE_SK_REUSEPORT, 168 BPF_PROG_TYPE_SK_REUSEPORT,
169 BPF_PROG_TYPE_FLOW_DISSECTOR, 169 BPF_PROG_TYPE_FLOW_DISSECTOR,
170 BPF_PROG_TYPE_CGROUP_SYSCTL,
170}; 171};
171 172
172enum bpf_attach_type { 173enum bpf_attach_type {
@@ -188,6 +189,7 @@ enum bpf_attach_type {
188 BPF_CGROUP_UDP6_SENDMSG, 189 BPF_CGROUP_UDP6_SENDMSG,
189 BPF_LIRC_MODE2, 190 BPF_LIRC_MODE2,
190 BPF_FLOW_DISSECTOR, 191 BPF_FLOW_DISSECTOR,
192 BPF_CGROUP_SYSCTL,
191 __MAX_BPF_ATTACH_TYPE 193 __MAX_BPF_ATTACH_TYPE
192}; 194};
193 195
@@ -3308,4 +3310,11 @@ struct bpf_line_info {
3308struct bpf_spin_lock { 3310struct bpf_spin_lock {
3309 __u32 val; 3311 __u32 val;
3310}; 3312};
3313
3314struct bpf_sysctl {
3315 __u32 write; /* Sysctl is being read (= 0) or written (= 1).
3316 * Allows 1,2,4-byte read, but no write.
3317 */
3318};
3319
3311#endif /* _UAPI__LINUX_BPF_H__ */ 3320#endif /* _UAPI__LINUX_BPF_H__ */