1 // SPDX-License-Identifier: GPL-2.0-only
3 * Functions to manage eBPF programs attached to cgroups
5 * Copyright (c) 2016 Daniel Mack
8 #include <linux/kernel.h>
9 #include <linux/atomic.h>
10 #include <linux/cgroup.h>
11 #include <linux/filter.h>
12 #include <linux/slab.h>
13 #include <linux/sysctl.h>
14 #include <linux/string.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf-cgroup.h>
19 DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key
);
20 EXPORT_SYMBOL(cgroup_bpf_enabled_key
);
23 * cgroup_bpf_put() - put references of all bpf programs
24 * @cgrp: the cgroup to modify
26 void cgroup_bpf_put(struct cgroup
*cgrp
)
28 enum bpf_cgroup_storage_type stype
;
31 for (type
= 0; type
< ARRAY_SIZE(cgrp
->bpf
.progs
); type
++) {
32 struct list_head
*progs
= &cgrp
->bpf
.progs
[type
];
33 struct bpf_prog_list
*pl
, *tmp
;
35 list_for_each_entry_safe(pl
, tmp
, progs
, node
) {
37 bpf_prog_put(pl
->prog
);
38 for_each_cgroup_storage_type(stype
) {
39 bpf_cgroup_storage_unlink(pl
->storage
[stype
]);
40 bpf_cgroup_storage_free(pl
->storage
[stype
]);
43 static_branch_dec(&cgroup_bpf_enabled_key
);
45 bpf_prog_array_free(cgrp
->bpf
.effective
[type
]);
49 /* count number of elements in the list.
50 * it's slow but the list cannot be long
52 static u32
prog_list_length(struct list_head
*head
)
54 struct bpf_prog_list
*pl
;
57 list_for_each_entry(pl
, head
, node
) {
65 /* if parent has non-overridable prog attached,
66 * disallow attaching new programs to the descendent cgroup.
67 * if parent has overridable or multi-prog, allow attaching
69 static bool hierarchy_allows_attach(struct cgroup
*cgrp
,
70 enum bpf_attach_type type
,
75 p
= cgroup_parent(cgrp
);
79 u32 flags
= p
->bpf
.flags
[type
];
82 if (flags
& BPF_F_ALLOW_MULTI
)
84 cnt
= prog_list_length(&p
->bpf
.progs
[type
]);
85 WARN_ON_ONCE(cnt
> 1);
87 return !!(flags
& BPF_F_ALLOW_OVERRIDE
);
93 /* compute a chain of effective programs for a given cgroup:
94 * start from the list of programs in this cgroup and add
95 * all parent programs.
96 * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
97 * to programs in this cgroup
99 static int compute_effective_progs(struct cgroup
*cgrp
,
100 enum bpf_attach_type type
,
101 struct bpf_prog_array __rcu
**array
)
103 enum bpf_cgroup_storage_type stype
;
104 struct bpf_prog_array
*progs
;
105 struct bpf_prog_list
*pl
;
106 struct cgroup
*p
= cgrp
;
109 /* count number of effective programs by walking parents */
111 if (cnt
== 0 || (p
->bpf
.flags
[type
] & BPF_F_ALLOW_MULTI
))
112 cnt
+= prog_list_length(&p
->bpf
.progs
[type
]);
113 p
= cgroup_parent(p
);
116 progs
= bpf_prog_array_alloc(cnt
, GFP_KERNEL
);
120 /* populate the array with effective progs */
124 if (cnt
> 0 && !(p
->bpf
.flags
[type
] & BPF_F_ALLOW_MULTI
))
127 list_for_each_entry(pl
, &p
->bpf
.progs
[type
], node
) {
131 progs
->items
[cnt
].prog
= pl
->prog
;
132 for_each_cgroup_storage_type(stype
)
133 progs
->items
[cnt
].cgroup_storage
[stype
] =
137 } while ((p
= cgroup_parent(p
)));
139 rcu_assign_pointer(*array
, progs
);
143 static void activate_effective_progs(struct cgroup
*cgrp
,
144 enum bpf_attach_type type
,
145 struct bpf_prog_array __rcu
*array
)
147 struct bpf_prog_array __rcu
*old_array
;
149 old_array
= xchg(&cgrp
->bpf
.effective
[type
], array
);
150 /* free prog array after grace period, since __cgroup_bpf_run_*()
151 * might be still walking the array
153 bpf_prog_array_free(old_array
);
157 * cgroup_bpf_inherit() - inherit effective programs from parent
158 * @cgrp: the cgroup to modify
160 int cgroup_bpf_inherit(struct cgroup
*cgrp
)
162 /* has to use marco instead of const int, since compiler thinks
163 * that array below is variable length
165 #define NR ARRAY_SIZE(cgrp->bpf.effective)
166 struct bpf_prog_array __rcu
*arrays
[NR
] = {};
169 for (i
= 0; i
< NR
; i
++)
170 INIT_LIST_HEAD(&cgrp
->bpf
.progs
[i
]);
172 for (i
= 0; i
< NR
; i
++)
173 if (compute_effective_progs(cgrp
, i
, &arrays
[i
]))
176 for (i
= 0; i
< NR
; i
++)
177 activate_effective_progs(cgrp
, i
, arrays
[i
]);
181 for (i
= 0; i
< NR
; i
++)
182 bpf_prog_array_free(arrays
[i
]);
186 static int update_effective_progs(struct cgroup
*cgrp
,
187 enum bpf_attach_type type
)
189 struct cgroup_subsys_state
*css
;
192 /* allocate and recompute effective prog arrays */
193 css_for_each_descendant_pre(css
, &cgrp
->self
) {
194 struct cgroup
*desc
= container_of(css
, struct cgroup
, self
);
196 err
= compute_effective_progs(desc
, type
, &desc
->bpf
.inactive
);
201 /* all allocations were successful. Activate all prog arrays */
202 css_for_each_descendant_pre(css
, &cgrp
->self
) {
203 struct cgroup
*desc
= container_of(css
, struct cgroup
, self
);
205 activate_effective_progs(desc
, type
, desc
->bpf
.inactive
);
206 desc
->bpf
.inactive
= NULL
;
212 /* oom while computing effective. Free all computed effective arrays
213 * since they were not activated
215 css_for_each_descendant_pre(css
, &cgrp
->self
) {
216 struct cgroup
*desc
= container_of(css
, struct cgroup
, self
);
218 bpf_prog_array_free(desc
->bpf
.inactive
);
219 desc
->bpf
.inactive
= NULL
;
225 #define BPF_CGROUP_MAX_PROGS 64
228 * __cgroup_bpf_attach() - Attach the program to a cgroup, and
229 * propagate the change to descendants
230 * @cgrp: The cgroup which descendants to traverse
231 * @prog: A program to attach
232 * @type: Type of attach operation
233 * @flags: Option flags
235 * Must be called with cgroup_mutex held.
237 int __cgroup_bpf_attach(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
238 enum bpf_attach_type type
, u32 flags
)
240 struct list_head
*progs
= &cgrp
->bpf
.progs
[type
];
241 struct bpf_prog
*old_prog
= NULL
;
242 struct bpf_cgroup_storage
*storage
[MAX_BPF_CGROUP_STORAGE_TYPE
],
243 *old_storage
[MAX_BPF_CGROUP_STORAGE_TYPE
] = {NULL
};
244 enum bpf_cgroup_storage_type stype
;
245 struct bpf_prog_list
*pl
;
246 bool pl_was_allocated
;
249 if ((flags
& BPF_F_ALLOW_OVERRIDE
) && (flags
& BPF_F_ALLOW_MULTI
))
250 /* invalid combination */
253 if (!hierarchy_allows_attach(cgrp
, type
, flags
))
256 if (!list_empty(progs
) && cgrp
->bpf
.flags
[type
] != flags
)
257 /* Disallow attaching non-overridable on top
258 * of existing overridable in this cgroup.
259 * Disallow attaching multi-prog if overridable or none
263 if (prog_list_length(progs
) >= BPF_CGROUP_MAX_PROGS
)
266 for_each_cgroup_storage_type(stype
) {
267 storage
[stype
] = bpf_cgroup_storage_alloc(prog
, stype
);
268 if (IS_ERR(storage
[stype
])) {
269 storage
[stype
] = NULL
;
270 for_each_cgroup_storage_type(stype
)
271 bpf_cgroup_storage_free(storage
[stype
]);
276 if (flags
& BPF_F_ALLOW_MULTI
) {
277 list_for_each_entry(pl
, progs
, node
) {
278 if (pl
->prog
== prog
) {
279 /* disallow attaching the same prog twice */
280 for_each_cgroup_storage_type(stype
)
281 bpf_cgroup_storage_free(storage
[stype
]);
286 pl
= kmalloc(sizeof(*pl
), GFP_KERNEL
);
288 for_each_cgroup_storage_type(stype
)
289 bpf_cgroup_storage_free(storage
[stype
]);
293 pl_was_allocated
= true;
295 for_each_cgroup_storage_type(stype
)
296 pl
->storage
[stype
] = storage
[stype
];
297 list_add_tail(&pl
->node
, progs
);
299 if (list_empty(progs
)) {
300 pl
= kmalloc(sizeof(*pl
), GFP_KERNEL
);
302 for_each_cgroup_storage_type(stype
)
303 bpf_cgroup_storage_free(storage
[stype
]);
306 pl_was_allocated
= true;
307 list_add_tail(&pl
->node
, progs
);
309 pl
= list_first_entry(progs
, typeof(*pl
), node
);
311 for_each_cgroup_storage_type(stype
) {
312 old_storage
[stype
] = pl
->storage
[stype
];
313 bpf_cgroup_storage_unlink(old_storage
[stype
]);
315 pl_was_allocated
= false;
318 for_each_cgroup_storage_type(stype
)
319 pl
->storage
[stype
] = storage
[stype
];
322 cgrp
->bpf
.flags
[type
] = flags
;
324 err
= update_effective_progs(cgrp
, type
);
328 static_branch_inc(&cgroup_bpf_enabled_key
);
329 for_each_cgroup_storage_type(stype
) {
330 if (!old_storage
[stype
])
332 bpf_cgroup_storage_free(old_storage
[stype
]);
335 bpf_prog_put(old_prog
);
336 static_branch_dec(&cgroup_bpf_enabled_key
);
338 for_each_cgroup_storage_type(stype
)
339 bpf_cgroup_storage_link(storage
[stype
], cgrp
, type
);
343 /* and cleanup the prog list */
345 for_each_cgroup_storage_type(stype
) {
346 bpf_cgroup_storage_free(pl
->storage
[stype
]);
347 pl
->storage
[stype
] = old_storage
[stype
];
348 bpf_cgroup_storage_link(old_storage
[stype
], cgrp
, type
);
350 if (pl_was_allocated
) {
358 * __cgroup_bpf_detach() - Detach the program from a cgroup, and
359 * propagate the change to descendants
360 * @cgrp: The cgroup which descendants to traverse
361 * @prog: A program to detach or NULL
362 * @type: Type of detach operation
364 * Must be called with cgroup_mutex held.
366 int __cgroup_bpf_detach(struct cgroup
*cgrp
, struct bpf_prog
*prog
,
367 enum bpf_attach_type type
)
369 struct list_head
*progs
= &cgrp
->bpf
.progs
[type
];
370 enum bpf_cgroup_storage_type stype
;
371 u32 flags
= cgrp
->bpf
.flags
[type
];
372 struct bpf_prog
*old_prog
= NULL
;
373 struct bpf_prog_list
*pl
;
376 if (flags
& BPF_F_ALLOW_MULTI
) {
378 /* to detach MULTI prog the user has to specify valid FD
379 * of the program to be detached
383 if (list_empty(progs
))
384 /* report error when trying to detach and nothing is attached */
388 if (flags
& BPF_F_ALLOW_MULTI
) {
389 /* find the prog and detach it */
390 list_for_each_entry(pl
, progs
, node
) {
391 if (pl
->prog
!= prog
)
394 /* mark it deleted, so it's ignored while
395 * recomputing effective
403 /* to maintain backward compatibility NONE and OVERRIDE cgroups
404 * allow detaching with invalid FD (prog==NULL)
406 pl
= list_first_entry(progs
, typeof(*pl
), node
);
411 err
= update_effective_progs(cgrp
, type
);
415 /* now can actually delete it from this cgroup list */
417 for_each_cgroup_storage_type(stype
) {
418 bpf_cgroup_storage_unlink(pl
->storage
[stype
]);
419 bpf_cgroup_storage_free(pl
->storage
[stype
]);
422 if (list_empty(progs
))
423 /* last program was detached, reset flags to zero */
424 cgrp
->bpf
.flags
[type
] = 0;
426 bpf_prog_put(old_prog
);
427 static_branch_dec(&cgroup_bpf_enabled_key
);
431 /* and restore back old_prog */
436 /* Must be called with cgroup_mutex held to avoid races. */
437 int __cgroup_bpf_query(struct cgroup
*cgrp
, const union bpf_attr
*attr
,
438 union bpf_attr __user
*uattr
)
440 __u32 __user
*prog_ids
= u64_to_user_ptr(attr
->query
.prog_ids
);
441 enum bpf_attach_type type
= attr
->query
.attach_type
;
442 struct list_head
*progs
= &cgrp
->bpf
.progs
[type
];
443 u32 flags
= cgrp
->bpf
.flags
[type
];
446 if (attr
->query
.query_flags
& BPF_F_QUERY_EFFECTIVE
)
447 cnt
= bpf_prog_array_length(cgrp
->bpf
.effective
[type
]);
449 cnt
= prog_list_length(progs
);
451 if (copy_to_user(&uattr
->query
.attach_flags
, &flags
, sizeof(flags
)))
453 if (copy_to_user(&uattr
->query
.prog_cnt
, &cnt
, sizeof(cnt
)))
455 if (attr
->query
.prog_cnt
== 0 || !prog_ids
|| !cnt
)
456 /* return early if user requested only program count + flags */
458 if (attr
->query
.prog_cnt
< cnt
) {
459 cnt
= attr
->query
.prog_cnt
;
463 if (attr
->query
.query_flags
& BPF_F_QUERY_EFFECTIVE
) {
464 return bpf_prog_array_copy_to_user(cgrp
->bpf
.effective
[type
],
467 struct bpf_prog_list
*pl
;
471 list_for_each_entry(pl
, progs
, node
) {
472 id
= pl
->prog
->aux
->id
;
473 if (copy_to_user(prog_ids
+ i
, &id
, sizeof(id
)))
482 int cgroup_bpf_prog_attach(const union bpf_attr
*attr
,
483 enum bpf_prog_type ptype
, struct bpf_prog
*prog
)
488 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
490 return PTR_ERR(cgrp
);
492 ret
= cgroup_bpf_attach(cgrp
, prog
, attr
->attach_type
,
498 int cgroup_bpf_prog_detach(const union bpf_attr
*attr
, enum bpf_prog_type ptype
)
500 struct bpf_prog
*prog
;
504 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
506 return PTR_ERR(cgrp
);
508 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
512 ret
= cgroup_bpf_detach(cgrp
, prog
, attr
->attach_type
, 0);
520 int cgroup_bpf_prog_query(const union bpf_attr
*attr
,
521 union bpf_attr __user
*uattr
)
526 cgrp
= cgroup_get_from_fd(attr
->query
.target_fd
);
528 return PTR_ERR(cgrp
);
530 ret
= cgroup_bpf_query(cgrp
, attr
, uattr
);
537 * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
538 * @sk: The socket sending or receiving traffic
539 * @skb: The skb that is being sent or received
540 * @type: The type of program to be exectuted
542 * If no socket is passed, or the socket is not of type INET or INET6,
543 * this function does nothing and returns 0.
545 * The program type passed in via @type must be suitable for network
546 * filtering. No further check is performed to assert that.
548 * This function will return %-EPERM if any if an attached program was found
549 * and if it returned != 1 during execution. In all other cases, 0 is returned.
551 int __cgroup_bpf_run_filter_skb(struct sock
*sk
,
553 enum bpf_attach_type type
)
555 unsigned int offset
= skb
->data
- skb_network_header(skb
);
556 struct sock
*save_sk
;
557 void *saved_data_end
;
561 if (!sk
|| !sk_fullsock(sk
))
564 if (sk
->sk_family
!= AF_INET
&& sk
->sk_family
!= AF_INET6
)
567 cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
570 __skb_push(skb
, offset
);
572 /* compute pointers for the bpf prog */
573 bpf_compute_and_save_data_end(skb
, &saved_data_end
);
575 ret
= BPF_PROG_RUN_ARRAY(cgrp
->bpf
.effective
[type
], skb
,
576 __bpf_prog_run_save_cb
);
577 bpf_restore_data_end(skb
, saved_data_end
);
578 __skb_pull(skb
, offset
);
580 return ret
== 1 ? 0 : -EPERM
;
582 EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb
);
585 * __cgroup_bpf_run_filter_sk() - Run a program on a sock
586 * @sk: sock structure to manipulate
587 * @type: The type of program to be exectuted
589 * socket is passed is expected to be of type INET or INET6.
591 * The program type passed in via @type must be suitable for sock
592 * filtering. No further check is performed to assert that.
594 * This function will return %-EPERM if any if an attached program was found
595 * and if it returned != 1 during execution. In all other cases, 0 is returned.
597 int __cgroup_bpf_run_filter_sk(struct sock
*sk
,
598 enum bpf_attach_type type
)
600 struct cgroup
*cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
603 ret
= BPF_PROG_RUN_ARRAY(cgrp
->bpf
.effective
[type
], sk
, BPF_PROG_RUN
);
604 return ret
== 1 ? 0 : -EPERM
;
606 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk
);
609 * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
610 * provided by user sockaddr
611 * @sk: sock struct that will use sockaddr
612 * @uaddr: sockaddr struct provided by user
613 * @type: The type of program to be exectuted
614 * @t_ctx: Pointer to attach type specific context
616 * socket is expected to be of type INET or INET6.
618 * This function will return %-EPERM if an attached program is found and
619 * returned value != 1 during execution. In all other cases, 0 is returned.
621 int __cgroup_bpf_run_filter_sock_addr(struct sock
*sk
,
622 struct sockaddr
*uaddr
,
623 enum bpf_attach_type type
,
626 struct bpf_sock_addr_kern ctx
= {
631 struct sockaddr_storage unspec
;
635 /* Check socket family since not all sockets represent network
636 * endpoint (e.g. AF_UNIX).
638 if (sk
->sk_family
!= AF_INET
&& sk
->sk_family
!= AF_INET6
)
642 memset(&unspec
, 0, sizeof(unspec
));
643 ctx
.uaddr
= (struct sockaddr
*)&unspec
;
646 cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
647 ret
= BPF_PROG_RUN_ARRAY(cgrp
->bpf
.effective
[type
], &ctx
, BPF_PROG_RUN
);
649 return ret
== 1 ? 0 : -EPERM
;
651 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr
);
654 * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
655 * @sk: socket to get cgroup from
656 * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
657 * sk with connection information (IP addresses, etc.) May not contain
658 * cgroup info if it is a req sock.
659 * @type: The type of program to be exectuted
661 * socket passed is expected to be of type INET or INET6.
663 * The program type passed in via @type must be suitable for sock_ops
664 * filtering. No further check is performed to assert that.
666 * This function will return %-EPERM if any if an attached program was found
667 * and if it returned != 1 during execution. In all other cases, 0 is returned.
669 int __cgroup_bpf_run_filter_sock_ops(struct sock
*sk
,
670 struct bpf_sock_ops_kern
*sock_ops
,
671 enum bpf_attach_type type
)
673 struct cgroup
*cgrp
= sock_cgroup_ptr(&sk
->sk_cgrp_data
);
676 ret
= BPF_PROG_RUN_ARRAY(cgrp
->bpf
.effective
[type
], sock_ops
,
678 return ret
== 1 ? 0 : -EPERM
;
680 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops
);
682 int __cgroup_bpf_check_dev_permission(short dev_type
, u32 major
, u32 minor
,
683 short access
, enum bpf_attach_type type
)
686 struct bpf_cgroup_dev_ctx ctx
= {
687 .access_type
= (access
<< 16) | dev_type
,
694 cgrp
= task_dfl_cgroup(current
);
695 allow
= BPF_PROG_RUN_ARRAY(cgrp
->bpf
.effective
[type
], &ctx
,
701 EXPORT_SYMBOL(__cgroup_bpf_check_dev_permission
);
703 static const struct bpf_func_proto
*
704 cgroup_base_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
707 case BPF_FUNC_map_lookup_elem
:
708 return &bpf_map_lookup_elem_proto
;
709 case BPF_FUNC_map_update_elem
:
710 return &bpf_map_update_elem_proto
;
711 case BPF_FUNC_map_delete_elem
:
712 return &bpf_map_delete_elem_proto
;
713 case BPF_FUNC_map_push_elem
:
714 return &bpf_map_push_elem_proto
;
715 case BPF_FUNC_map_pop_elem
:
716 return &bpf_map_pop_elem_proto
;
717 case BPF_FUNC_map_peek_elem
:
718 return &bpf_map_peek_elem_proto
;
719 case BPF_FUNC_get_current_uid_gid
:
720 return &bpf_get_current_uid_gid_proto
;
721 case BPF_FUNC_get_local_storage
:
722 return &bpf_get_local_storage_proto
;
723 case BPF_FUNC_get_current_cgroup_id
:
724 return &bpf_get_current_cgroup_id_proto
;
725 case BPF_FUNC_trace_printk
:
726 if (capable(CAP_SYS_ADMIN
))
727 return bpf_get_trace_printk_proto();
734 static const struct bpf_func_proto
*
735 cgroup_dev_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
737 return cgroup_base_func_proto(func_id
, prog
);
740 static bool cgroup_dev_is_valid_access(int off
, int size
,
741 enum bpf_access_type type
,
742 const struct bpf_prog
*prog
,
743 struct bpf_insn_access_aux
*info
)
745 const int size_default
= sizeof(__u32
);
747 if (type
== BPF_WRITE
)
750 if (off
< 0 || off
+ size
> sizeof(struct bpf_cgroup_dev_ctx
))
752 /* The verifier guarantees that size > 0. */
757 case bpf_ctx_range(struct bpf_cgroup_dev_ctx
, access_type
):
758 bpf_ctx_record_field_size(info
, size_default
);
759 if (!bpf_ctx_narrow_access_ok(off
, size
, size_default
))
763 if (size
!= size_default
)
770 const struct bpf_prog_ops cg_dev_prog_ops
= {
773 const struct bpf_verifier_ops cg_dev_verifier_ops
= {
774 .get_func_proto
= cgroup_dev_func_proto
,
775 .is_valid_access
= cgroup_dev_is_valid_access
,
779 * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
781 * @head: sysctl table header
782 * @table: sysctl table
783 * @write: sysctl is being read (= 0) or written (= 1)
784 * @buf: pointer to buffer passed by user space
785 * @pcount: value-result argument: value is size of buffer pointed to by @buf,
786 * result is size of @new_buf if program set new value, initial value
788 * @ppos: value-result argument: value is position at which read from or write
789 * to sysctl is happening, result is new position if program overrode it,
790 * initial value otherwise
791 * @new_buf: pointer to pointer to new buffer that will be allocated if program
792 * overrides new value provided by user space on sysctl write
793 * NOTE: it's caller responsibility to free *new_buf if it was set
794 * @type: type of program to be executed
796 * Program is run when sysctl is being accessed, either read or written, and
797 * can allow or deny such access.
799 * This function will return %-EPERM if an attached program is found and
800 * returned value != 1 during execution. In all other cases 0 is returned.
802 int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header
*head
,
803 struct ctl_table
*table
, int write
,
804 void __user
*buf
, size_t *pcount
,
805 loff_t
*ppos
, void **new_buf
,
806 enum bpf_attach_type type
)
808 struct bpf_sysctl_kern ctx
= {
814 .cur_len
= PAGE_SIZE
,
822 ctx
.cur_val
= kmalloc_track_caller(ctx
.cur_len
, GFP_KERNEL
);
829 if (table
->proc_handler(table
, 0, (void __user
*)ctx
.cur_val
,
830 &ctx
.cur_len
, &pos
)) {
831 /* Let BPF program decide how to proceed. */
836 /* Let BPF program decide how to proceed. */
840 if (write
&& buf
&& *pcount
) {
841 /* BPF program should be able to override new value with a
842 * buffer bigger than provided by user.
844 ctx
.new_val
= kmalloc_track_caller(PAGE_SIZE
, GFP_KERNEL
);
845 ctx
.new_len
= min_t(size_t, PAGE_SIZE
, *pcount
);
847 copy_from_user(ctx
.new_val
, buf
, ctx
.new_len
))
848 /* Let BPF program decide how to proceed. */
853 cgrp
= task_dfl_cgroup(current
);
854 ret
= BPF_PROG_RUN_ARRAY(cgrp
->bpf
.effective
[type
], &ctx
, BPF_PROG_RUN
);
859 if (ret
== 1 && ctx
.new_updated
) {
860 *new_buf
= ctx
.new_val
;
861 *pcount
= ctx
.new_len
;
866 return ret
== 1 ? 0 : -EPERM
;
868 EXPORT_SYMBOL(__cgroup_bpf_run_filter_sysctl
);
870 static ssize_t
sysctl_cpy_dir(const struct ctl_dir
*dir
, char **bufp
,
873 ssize_t tmp_ret
= 0, ret
;
875 if (dir
->header
.parent
) {
876 tmp_ret
= sysctl_cpy_dir(dir
->header
.parent
, bufp
, lenp
);
881 ret
= strscpy(*bufp
, dir
->header
.ctl_table
[0].procname
, *lenp
);
888 /* Avoid leading slash. */
892 tmp_ret
= strscpy(*bufp
, "/", *lenp
);
898 return ret
+ tmp_ret
;
901 BPF_CALL_4(bpf_sysctl_get_name
, struct bpf_sysctl_kern
*, ctx
, char *, buf
,
902 size_t, buf_len
, u64
, flags
)
904 ssize_t tmp_ret
= 0, ret
;
909 if (!(flags
& BPF_F_SYSCTL_BASE_NAME
)) {
912 tmp_ret
= sysctl_cpy_dir(ctx
->head
->parent
, &buf
, &buf_len
);
917 ret
= strscpy(buf
, ctx
->table
->procname
, buf_len
);
919 return ret
< 0 ? ret
: tmp_ret
+ ret
;
922 static const struct bpf_func_proto bpf_sysctl_get_name_proto
= {
923 .func
= bpf_sysctl_get_name
,
925 .ret_type
= RET_INTEGER
,
926 .arg1_type
= ARG_PTR_TO_CTX
,
927 .arg2_type
= ARG_PTR_TO_MEM
,
928 .arg3_type
= ARG_CONST_SIZE
,
929 .arg4_type
= ARG_ANYTHING
,
932 static int copy_sysctl_value(char *dst
, size_t dst_len
, char *src
,
941 if (!src
|| !src_len
) {
942 memset(dst
, 0, dst_len
);
946 memcpy(dst
, src
, min(dst_len
, src_len
));
948 if (dst_len
> src_len
) {
949 memset(dst
+ src_len
, '\0', dst_len
- src_len
);
953 dst
[dst_len
- 1] = '\0';
958 BPF_CALL_3(bpf_sysctl_get_current_value
, struct bpf_sysctl_kern
*, ctx
,
959 char *, buf
, size_t, buf_len
)
961 return copy_sysctl_value(buf
, buf_len
, ctx
->cur_val
, ctx
->cur_len
);
964 static const struct bpf_func_proto bpf_sysctl_get_current_value_proto
= {
965 .func
= bpf_sysctl_get_current_value
,
967 .ret_type
= RET_INTEGER
,
968 .arg1_type
= ARG_PTR_TO_CTX
,
969 .arg2_type
= ARG_PTR_TO_UNINIT_MEM
,
970 .arg3_type
= ARG_CONST_SIZE
,
973 BPF_CALL_3(bpf_sysctl_get_new_value
, struct bpf_sysctl_kern
*, ctx
, char *, buf
,
978 memset(buf
, '\0', buf_len
);
981 return copy_sysctl_value(buf
, buf_len
, ctx
->new_val
, ctx
->new_len
);
984 static const struct bpf_func_proto bpf_sysctl_get_new_value_proto
= {
985 .func
= bpf_sysctl_get_new_value
,
987 .ret_type
= RET_INTEGER
,
988 .arg1_type
= ARG_PTR_TO_CTX
,
989 .arg2_type
= ARG_PTR_TO_UNINIT_MEM
,
990 .arg3_type
= ARG_CONST_SIZE
,
993 BPF_CALL_3(bpf_sysctl_set_new_value
, struct bpf_sysctl_kern
*, ctx
,
994 const char *, buf
, size_t, buf_len
)
996 if (!ctx
->write
|| !ctx
->new_val
|| !ctx
->new_len
|| !buf
|| !buf_len
)
999 if (buf_len
> PAGE_SIZE
- 1)
1002 memcpy(ctx
->new_val
, buf
, buf_len
);
1003 ctx
->new_len
= buf_len
;
1004 ctx
->new_updated
= 1;
1009 static const struct bpf_func_proto bpf_sysctl_set_new_value_proto
= {
1010 .func
= bpf_sysctl_set_new_value
,
1012 .ret_type
= RET_INTEGER
,
1013 .arg1_type
= ARG_PTR_TO_CTX
,
1014 .arg2_type
= ARG_PTR_TO_MEM
,
1015 .arg3_type
= ARG_CONST_SIZE
,
1018 static const struct bpf_func_proto
*
1019 sysctl_func_proto(enum bpf_func_id func_id
, const struct bpf_prog
*prog
)
1022 case BPF_FUNC_strtol
:
1023 return &bpf_strtol_proto
;
1024 case BPF_FUNC_strtoul
:
1025 return &bpf_strtoul_proto
;
1026 case BPF_FUNC_sysctl_get_name
:
1027 return &bpf_sysctl_get_name_proto
;
1028 case BPF_FUNC_sysctl_get_current_value
:
1029 return &bpf_sysctl_get_current_value_proto
;
1030 case BPF_FUNC_sysctl_get_new_value
:
1031 return &bpf_sysctl_get_new_value_proto
;
1032 case BPF_FUNC_sysctl_set_new_value
:
1033 return &bpf_sysctl_set_new_value_proto
;
1035 return cgroup_base_func_proto(func_id
, prog
);
1039 static bool sysctl_is_valid_access(int off
, int size
, enum bpf_access_type type
,
1040 const struct bpf_prog
*prog
,
1041 struct bpf_insn_access_aux
*info
)
1043 const int size_default
= sizeof(__u32
);
1045 if (off
< 0 || off
+ size
> sizeof(struct bpf_sysctl
) || off
% size
)
1049 case offsetof(struct bpf_sysctl
, write
):
1050 if (type
!= BPF_READ
)
1052 bpf_ctx_record_field_size(info
, size_default
);
1053 return bpf_ctx_narrow_access_ok(off
, size
, size_default
);
1054 case offsetof(struct bpf_sysctl
, file_pos
):
1055 if (type
== BPF_READ
) {
1056 bpf_ctx_record_field_size(info
, size_default
);
1057 return bpf_ctx_narrow_access_ok(off
, size
, size_default
);
1059 return size
== size_default
;
1066 static u32
sysctl_convert_ctx_access(enum bpf_access_type type
,
1067 const struct bpf_insn
*si
,
1068 struct bpf_insn
*insn_buf
,
1069 struct bpf_prog
*prog
, u32
*target_size
)
1071 struct bpf_insn
*insn
= insn_buf
;
1074 case offsetof(struct bpf_sysctl
, write
):
1075 *insn
++ = BPF_LDX_MEM(
1076 BPF_SIZE(si
->code
), si
->dst_reg
, si
->src_reg
,
1077 bpf_target_off(struct bpf_sysctl_kern
, write
,
1078 FIELD_SIZEOF(struct bpf_sysctl_kern
,
1082 case offsetof(struct bpf_sysctl
, file_pos
):
1083 /* ppos is a pointer so it should be accessed via indirect
1084 * loads and stores. Also for stores additional temporary
1085 * register is used since neither src_reg nor dst_reg can be
1088 if (type
== BPF_WRITE
) {
1089 int treg
= BPF_REG_9
;
1091 if (si
->src_reg
== treg
|| si
->dst_reg
== treg
)
1093 if (si
->src_reg
== treg
|| si
->dst_reg
== treg
)
1095 *insn
++ = BPF_STX_MEM(
1096 BPF_DW
, si
->dst_reg
, treg
,
1097 offsetof(struct bpf_sysctl_kern
, tmp_reg
));
1098 *insn
++ = BPF_LDX_MEM(
1099 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern
, ppos
),
1101 offsetof(struct bpf_sysctl_kern
, ppos
));
1102 *insn
++ = BPF_STX_MEM(
1103 BPF_SIZEOF(u32
), treg
, si
->src_reg
, 0);
1104 *insn
++ = BPF_LDX_MEM(
1105 BPF_DW
, treg
, si
->dst_reg
,
1106 offsetof(struct bpf_sysctl_kern
, tmp_reg
));
1108 *insn
++ = BPF_LDX_MEM(
1109 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern
, ppos
),
1110 si
->dst_reg
, si
->src_reg
,
1111 offsetof(struct bpf_sysctl_kern
, ppos
));
1112 *insn
++ = BPF_LDX_MEM(
1113 BPF_SIZE(si
->code
), si
->dst_reg
, si
->dst_reg
, 0);
1115 *target_size
= sizeof(u32
);
1119 return insn
- insn_buf
;
1122 const struct bpf_verifier_ops cg_sysctl_verifier_ops
= {
1123 .get_func_proto
= sysctl_func_proto
,
1124 .is_valid_access
= sysctl_is_valid_access
,
1125 .convert_ctx_access
= sysctl_convert_ctx_access
,
1128 const struct bpf_prog_ops cg_sysctl_prog_ops
= {