bpf: Enable bpf cgroup hooks to retrieve cgroup v2 and ancestor id
authorDaniel Borkmann <daniel@iogearbox.net>
Fri, 27 Mar 2020 15:58:54 +0000 (16:58 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 28 Mar 2020 02:40:39 +0000 (19:40 -0700)
Enable the bpf_get_current_cgroup_id() helper for connect(), sendmsg(),
recvmsg() and bind-related hooks in order to retrieve the cgroup v2
context which can then be used as part of the key for BPF map lookups,
for example. Given these hooks operate in process context 'current' is
always valid and pointing to the app that is performing mentioned
syscalls if it's subject to a v2 cgroup. Also with same motivation of
commit 7723628101aa ("bpf: Introduce bpf_skb_ancestor_cgroup_id helper")
enable retrieval of ancestor from current so the cgroup id can be used
for policy lookups which can then forbid connect() / bind(), for example.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/d2a7ef42530ad299e3cbb245e6c12374b72145ef.1585323121.git.daniel@iogearbox.net
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/bpf/core.c
kernel/bpf/helpers.c
net/core/filter.c
tools/include/uapi/linux/bpf.h

index 78046c57059661e893e7eb80d298fe128de9647c..372708eeaecdd9b375eaec6d7d92c04729845158 100644 (file)
@@ -1501,6 +1501,7 @@ extern const struct bpf_func_proto bpf_get_stack_proto;
 extern const struct bpf_func_proto bpf_sock_map_update_proto;
 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
+extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
index bd81c4555206ea1a2a7ffda6cf7f2b6597cc74bd..222ba11966e3274ff414a2b0b51d52c027dad52f 100644 (file)
@@ -2963,6 +2963,24 @@ union bpf_attr {
  *             instead of sockets.
  *     Return
  *             A 8-byte long opaque number.
+ *
+ * u64 bpf_get_current_ancestor_cgroup_id(int ancestor_level)
+ *     Description
+ *             Return id of cgroup v2 that is ancestor of the cgroup associated
+ *             with the current task at the *ancestor_level*. The root cgroup
+ *             is at *ancestor_level* zero and each step down the hierarchy
+ *             increments the level. If *ancestor_level* == level of cgroup
+ *             associated with the current task, then return value will be the
+ *             same as that of **bpf_get_current_cgroup_id**\ ().
+ *
+ *             The helper is useful to implement policies based on cgroups
+ *             that are upper in hierarchy than immediate cgroup associated
+ *             with the current task.
+ *
+ *             The format of returned id and helper limitations are same as in
+ *             **bpf_get_current_cgroup_id**\ ().
+ *     Return
+ *             The id is returned or 0 in case the id could not be retrieved.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -3087,7 +3105,8 @@ union bpf_attr {
        FN(read_branch_records),        \
        FN(get_ns_current_pid_tgid),    \
        FN(xdp_output),                 \
-       FN(get_netns_cookie),
+       FN(get_netns_cookie),           \
+       FN(get_current_ancestor_cgroup_id),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
index 914f3463aa415740f31a0b68daf6a29574490bda..916f5132a9848dfc46982557a2fa126aad792c55 100644 (file)
@@ -2156,6 +2156,7 @@ const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
 const struct bpf_func_proto bpf_get_current_cgroup_id_proto __weak;
+const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto __weak;
 const struct bpf_func_proto bpf_get_local_storage_proto __weak;
 const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto __weak;
 
index 01878db15eaf87b931d32f4c7ffe00fb03989e5b..bafc53ddd3504f01416725104573d0dd23a453c2 100644 (file)
@@ -340,6 +340,24 @@ const struct bpf_func_proto bpf_get_current_cgroup_id_proto = {
        .ret_type       = RET_INTEGER,
 };
 
+BPF_CALL_1(bpf_get_current_ancestor_cgroup_id, int, ancestor_level)
+{
+       struct cgroup *cgrp = task_dfl_cgroup(current);
+       struct cgroup *ancestor;
+
+       ancestor = cgroup_ancestor(cgrp, ancestor_level);
+       if (!ancestor)
+               return 0;
+       return cgroup_id(ancestor);
+}
+
+const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto = {
+       .func           = bpf_get_current_ancestor_cgroup_id,
+       .gpl_only       = false,
+       .ret_type       = RET_INTEGER,
+       .arg1_type      = ARG_ANYTHING,
+};
+
 #ifdef CONFIG_CGROUP_BPF
 DECLARE_PER_CPU(struct bpf_cgroup_storage*,
                bpf_cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE]);
index 3083c7746ee07a559b0a94508ba3d837c972e27f..5cec3ac9e3dd837fa7066ef1792a0304d3fc8441 100644 (file)
@@ -6018,6 +6018,12 @@ sock_filter_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_get_netns_cookie_sock_proto;
        case BPF_FUNC_perf_event_output:
                return &bpf_event_output_data_proto;
+#ifdef CONFIG_CGROUPS
+       case BPF_FUNC_get_current_cgroup_id:
+               return &bpf_get_current_cgroup_id_proto;
+       case BPF_FUNC_get_current_ancestor_cgroup_id:
+               return &bpf_get_current_ancestor_cgroup_id_proto;
+#endif
 #ifdef CONFIG_CGROUP_NET_CLASSID
        case BPF_FUNC_get_cgroup_classid:
                return &bpf_get_cgroup_classid_curr_proto;
@@ -6052,6 +6058,12 @@ sock_addr_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_get_local_storage_proto;
        case BPF_FUNC_perf_event_output:
                return &bpf_event_output_data_proto;
+#ifdef CONFIG_CGROUPS
+       case BPF_FUNC_get_current_cgroup_id:
+               return &bpf_get_current_cgroup_id_proto;
+       case BPF_FUNC_get_current_ancestor_cgroup_id:
+               return &bpf_get_current_ancestor_cgroup_id_proto;
+#endif
 #ifdef CONFIG_CGROUP_NET_CLASSID
        case BPF_FUNC_get_cgroup_classid:
                return &bpf_get_cgroup_classid_curr_proto;
index bd81c4555206ea1a2a7ffda6cf7f2b6597cc74bd..222ba11966e3274ff414a2b0b51d52c027dad52f 100644 (file)
@@ -2963,6 +2963,24 @@ union bpf_attr {
  *             instead of sockets.
  *     Return
  *             A 8-byte long opaque number.
+ *
+ * u64 bpf_get_current_ancestor_cgroup_id(int ancestor_level)
+ *     Description
+ *             Return id of cgroup v2 that is ancestor of the cgroup associated
+ *             with the current task at the *ancestor_level*. The root cgroup
+ *             is at *ancestor_level* zero and each step down the hierarchy
+ *             increments the level. If *ancestor_level* == level of cgroup
+ *             associated with the current task, then return value will be the
+ *             same as that of **bpf_get_current_cgroup_id**\ ().
+ *
+ *             The helper is useful to implement policies based on cgroups
+ *             that are upper in hierarchy than immediate cgroup associated
+ *             with the current task.
+ *
+ *             The format of returned id and helper limitations are same as in
+ *             **bpf_get_current_cgroup_id**\ ().
+ *     Return
+ *             The id is returned or 0 in case the id could not be retrieved.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -3087,7 +3105,8 @@ union bpf_attr {
        FN(read_branch_records),        \
        FN(get_ns_current_pid_tgid),    \
        FN(xdp_output),                 \
-       FN(get_netns_cookie),
+       FN(get_netns_cookie),           \
+       FN(get_current_ancestor_cgroup_id),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call