LCOV - code coverage report
Current view: top level - kernel/bpf - cgroup.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 163 489 33.3 %
Date: 2020-09-30 20:25:40 Functions: 17 38 44.7 %
Branches: 105 471 22.3 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  * Functions to manage eBPF programs attached to cgroups
       4                 :            :  *
       5                 :            :  * Copyright (c) 2016 Daniel Mack
       6                 :            :  */
       7                 :            : 
       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>
      17                 :            : #include <net/sock.h>
      18                 :            : #include <net/bpf_sk_storage.h>
      19                 :            : 
      20                 :            : #include "../cgroup/cgroup-internal.h"
      21                 :            : 
      22                 :            : DEFINE_STATIC_KEY_FALSE(cgroup_bpf_enabled_key);
      23                 :            : EXPORT_SYMBOL(cgroup_bpf_enabled_key);
      24                 :            : 
      25                 :      25278 : void cgroup_bpf_offline(struct cgroup *cgrp)
      26                 :            : {
      27                 :            :         cgroup_get(cgrp);
      28                 :      25278 :         percpu_ref_kill(&cgrp->bpf.refcnt);
      29                 :      25278 : }
      30                 :            : 
      31                 :            : /**
      32                 :            :  * cgroup_bpf_release() - put references of all bpf programs and
      33                 :            :  *                        release all cgroup bpf data
      34                 :            :  * @work: work structure embedded into the cgroup to modify
      35                 :            :  */
      36                 :      25278 : static void cgroup_bpf_release(struct work_struct *work)
      37                 :            : {
      38                 :            :         struct cgroup *p, *cgrp = container_of(work, struct cgroup,
      39                 :            :                                                bpf.release_work);
      40                 :            :         enum bpf_cgroup_storage_type stype;
      41                 :            :         struct bpf_prog_array *old_array;
      42                 :            :         unsigned int type;
      43                 :            : 
      44                 :      25278 :         mutex_lock(&cgroup_mutex);
      45                 :            : 
      46         [ +  + ]:     606672 :         for (type = 0; type < ARRAY_SIZE(cgrp->bpf.progs); type++) {
      47                 :     581394 :                 struct list_head *progs = &cgrp->bpf.progs[type];
      48                 :            :                 struct bpf_prog_list *pl, *tmp;
      49                 :            : 
      50         [ -  + ]:     581394 :                 list_for_each_entry_safe(pl, tmp, progs, node) {
      51                 :            :                         list_del(&pl->node);
      52                 :          0 :                         bpf_prog_put(pl->prog);
      53         [ #  # ]:          0 :                         for_each_cgroup_storage_type(stype) {
      54                 :          0 :                                 bpf_cgroup_storage_unlink(pl->storage[stype]);
      55                 :          0 :                                 bpf_cgroup_storage_free(pl->storage[stype]);
      56                 :            :                         }
      57                 :          0 :                         kfree(pl);
      58                 :          0 :                         static_branch_dec(&cgroup_bpf_enabled_key);
      59                 :            :                 }
      60                 :     581394 :                 old_array = rcu_dereference_protected(
      61                 :            :                                 cgrp->bpf.effective[type],
      62                 :            :                                 lockdep_is_held(&cgroup_mutex));
      63                 :     581394 :                 bpf_prog_array_free(old_array);
      64                 :            :         }
      65                 :            : 
      66                 :      25278 :         mutex_unlock(&cgroup_mutex);
      67                 :            : 
      68         [ +  + ]:     102354 :         for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
      69                 :            :                 cgroup_bpf_put(p);
      70                 :            : 
      71                 :      25278 :         percpu_ref_exit(&cgrp->bpf.refcnt);
      72                 :            :         cgroup_put(cgrp);
      73                 :      25278 : }
      74                 :            : 
      75                 :            : /**
      76                 :            :  * cgroup_bpf_release_fn() - callback used to schedule releasing
      77                 :            :  *                           of bpf cgroup data
      78                 :            :  * @ref: percpu ref counter structure
      79                 :            :  */
      80                 :      25278 : static void cgroup_bpf_release_fn(struct percpu_ref *ref)
      81                 :            : {
      82                 :            :         struct cgroup *cgrp = container_of(ref, struct cgroup, bpf.refcnt);
      83                 :            : 
      84                 :      50556 :         INIT_WORK(&cgrp->bpf.release_work, cgroup_bpf_release);
      85                 :      25278 :         queue_work(system_wq, &cgrp->bpf.release_work);
      86                 :      25278 : }
      87                 :            : 
      88                 :            : /* count number of elements in the list.
      89                 :            :  * it's slow but the list cannot be long
      90                 :            :  */
      91                 :            : static u32 prog_list_length(struct list_head *head)
      92                 :            : {
      93                 :            :         struct bpf_prog_list *pl;
      94                 :            :         u32 cnt = 0;
      95                 :            : 
      96   [ #  #  -  +  :    4106811 :         list_for_each_entry(pl, head, node) {
             +  +  -  + ]
      97   [ #  #  #  #  :       1242 :                 if (!pl->prog)
             -  +  #  # ]
      98                 :          0 :                         continue;
      99                 :       1242 :                 cnt++;
     100                 :            :         }
     101                 :    4105569 :         return cnt;
     102                 :            : }
     103                 :            : 
     104                 :            : /* if parent has non-overridable prog attached,
     105                 :            :  * disallow attaching new programs to the descendent cgroup.
     106                 :            :  * if parent has overridable or multi-prog, allow attaching
     107                 :            :  */
     108                 :       1242 : static bool hierarchy_allows_attach(struct cgroup *cgrp,
     109                 :            :                                     enum bpf_attach_type type,
     110                 :            :                                     u32 new_flags)
     111                 :            : {
     112                 :            :         struct cgroup *p;
     113                 :            : 
     114                 :            :         p = cgroup_parent(cgrp);
     115         [ +  - ]:       1242 :         if (!p)
     116                 :            :                 return true;
     117                 :            :         do {
     118                 :       2484 :                 u32 flags = p->bpf.flags[type];
     119                 :            :                 u32 cnt;
     120                 :            : 
     121         [ +  - ]:       2484 :                 if (flags & BPF_F_ALLOW_MULTI)
     122                 :            :                         return true;
     123                 :       2484 :                 cnt = prog_list_length(&p->bpf.progs[type]);
     124   [ -  +  #  # ]:       2484 :                 WARN_ON_ONCE(cnt > 1);
     125         [ -  + ]:       2484 :                 if (cnt == 1)
     126                 :          0 :                         return !!(flags & BPF_F_ALLOW_OVERRIDE);
     127                 :            :                 p = cgroup_parent(p);
     128         [ +  + ]:       2484 :         } while (p);
     129                 :            :         return true;
     130                 :            : }
     131                 :            : 
     132                 :            : /* compute a chain of effective programs for a given cgroup:
     133                 :            :  * start from the list of programs in this cgroup and add
     134                 :            :  * all parent programs.
     135                 :            :  * Note that parent's F_ALLOW_OVERRIDE-type program is yielding
     136                 :            :  * to programs in this cgroup
     137                 :            :  */
     138                 :    1325260 : static int compute_effective_progs(struct cgroup *cgrp,
     139                 :            :                                    enum bpf_attach_type type,
     140                 :            :                                    struct bpf_prog_array **array)
     141                 :            : {
     142                 :            :         enum bpf_cgroup_storage_type stype;
     143                 :            :         struct bpf_prog_array *progs;
     144                 :            :         struct bpf_prog_list *pl;
     145                 :            :         struct cgroup *p = cgrp;
     146                 :            :         int cnt = 0;
     147                 :            : 
     148                 :            :         /* count number of effective programs by walking parents */
     149                 :            :         do {
     150   [ +  +  -  + ]:    4104327 :                 if (cnt == 0 || (p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
     151                 :    8203686 :                         cnt += prog_list_length(&p->bpf.progs[type]);
     152                 :            :                 p = cgroup_parent(p);
     153         [ +  + ]:    4104327 :         } while (p);
     154                 :            : 
     155                 :    1325260 :         progs = bpf_prog_array_alloc(cnt, GFP_KERNEL);
     156         [ +  - ]:    1325260 :         if (!progs)
     157                 :            :                 return -ENOMEM;
     158                 :            : 
     159                 :            :         /* populate the array with effective progs */
     160                 :            :         cnt = 0;
     161                 :            :         p = cgrp;
     162                 :            :         do {
     163   [ +  +  +  - ]:    4104327 :                 if (cnt > 0 && !(p->bpf.flags[type] & BPF_F_ALLOW_MULTI))
     164                 :       2484 :                         continue;
     165                 :            : 
     166         [ +  + ]:    4103085 :                 list_for_each_entry(pl, &p->bpf.progs[type], node) {
     167         [ -  + ]:       1242 :                         if (!pl->prog)
     168                 :          0 :                                 continue;
     169                 :            : 
     170                 :       1242 :                         progs->items[cnt].prog = pl->prog;
     171         [ +  + ]:       3726 :                         for_each_cgroup_storage_type(stype)
     172                 :       2484 :                                 progs->items[cnt].cgroup_storage[stype] =
     173                 :       2484 :                                         pl->storage[stype];
     174                 :       1242 :                         cnt++;
     175                 :            :                 }
     176         [ +  + ]:    4104327 :         } while ((p = cgroup_parent(p)));
     177                 :            : 
     178                 :    1325260 :         *array = progs;
     179                 :    1325260 :         return 0;
     180                 :            : }
     181                 :            : 
     182                 :            : static void activate_effective_progs(struct cgroup *cgrp,
     183                 :            :                                      enum bpf_attach_type type,
     184                 :            :                                      struct bpf_prog_array *old_array)
     185                 :            : {
     186                 :    1325260 :         rcu_swap_protected(cgrp->bpf.effective[type], old_array,
     187                 :            :                            lockdep_is_held(&cgroup_mutex));
     188                 :            :         /* free prog array after grace period, since __cgroup_bpf_run_*()
     189                 :            :          * might be still walking the array
     190                 :            :          */
     191                 :    1325260 :         bpf_prog_array_free(old_array);
     192                 :            : }
     193                 :            : 
     194                 :            : /**
     195                 :            :  * cgroup_bpf_inherit() - inherit effective programs from parent
     196                 :            :  * @cgrp: the cgroup to modify
     197                 :            :  */
     198                 :      57566 : int cgroup_bpf_inherit(struct cgroup *cgrp)
     199                 :            : {
     200                 :            : /* has to use marco instead of const int, since compiler thinks
     201                 :            :  * that array below is variable length
     202                 :            :  */
     203                 :            : #define NR ARRAY_SIZE(cgrp->bpf.effective)
     204                 :      57566 :         struct bpf_prog_array *arrays[NR] = {};
     205                 :            :         struct cgroup *p;
     206                 :            :         int ret, i;
     207                 :            : 
     208                 :      57566 :         ret = percpu_ref_init(&cgrp->bpf.refcnt, cgroup_bpf_release_fn, 0,
     209                 :            :                               GFP_KERNEL);
     210         [ +  - ]:      57566 :         if (ret)
     211                 :            :                 return ret;
     212                 :            : 
     213         [ +  + ]:     235853 :         for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
     214                 :            :                 cgroup_bpf_get(p);
     215                 :            : 
     216         [ +  + ]:    1324018 :         for (i = 0; i < NR; i++)
     217                 :    1324018 :                 INIT_LIST_HEAD(&cgrp->bpf.progs[i]);
     218                 :            : 
     219         [ +  + ]:    1324018 :         for (i = 0; i < NR; i++)
     220         [ -  + ]:    1324018 :                 if (compute_effective_progs(cgrp, i, &arrays[i]))
     221                 :            :                         goto cleanup;
     222                 :            : 
     223         [ +  + ]:    1324018 :         for (i = 0; i < NR; i++)
     224                 :    1324018 :                 activate_effective_progs(cgrp, i, arrays[i]);
     225                 :            : 
     226                 :            :         return 0;
     227                 :            : cleanup:
     228         [ #  # ]:          0 :         for (i = 0; i < NR; i++)
     229                 :          0 :                 bpf_prog_array_free(arrays[i]);
     230                 :            : 
     231         [ #  # ]:          0 :         for (p = cgroup_parent(cgrp); p; p = cgroup_parent(p))
     232                 :            :                 cgroup_bpf_put(p);
     233                 :            : 
     234                 :          0 :         percpu_ref_exit(&cgrp->bpf.refcnt);
     235                 :            : 
     236                 :          0 :         return -ENOMEM;
     237                 :            : }
     238                 :            : 
     239                 :       1242 : static int update_effective_progs(struct cgroup *cgrp,
     240                 :            :                                   enum bpf_attach_type type)
     241                 :            : {
     242                 :            :         struct cgroup_subsys_state *css;
     243                 :            :         int err;
     244                 :            : 
     245                 :            :         /* allocate and recompute effective prog arrays */
     246         [ +  + ]:       2484 :         css_for_each_descendant_pre(css, &cgrp->self) {
     247                 :            :                 struct cgroup *desc = container_of(css, struct cgroup, self);
     248                 :            : 
     249         [ -  + ]:       1242 :                 if (percpu_ref_is_zero(&desc->bpf.refcnt))
     250                 :          0 :                         continue;
     251                 :            : 
     252                 :       1242 :                 err = compute_effective_progs(desc, type, &desc->bpf.inactive);
     253         [ +  - ]:       1242 :                 if (err)
     254                 :            :                         goto cleanup;
     255                 :            :         }
     256                 :            : 
     257                 :            :         /* all allocations were successful. Activate all prog arrays */
     258         [ +  + ]:       2484 :         css_for_each_descendant_pre(css, &cgrp->self) {
     259                 :            :                 struct cgroup *desc = container_of(css, struct cgroup, self);
     260                 :            : 
     261         [ -  + ]:       1242 :                 if (percpu_ref_is_zero(&desc->bpf.refcnt)) {
     262         [ #  # ]:          0 :                         if (unlikely(desc->bpf.inactive)) {
     263                 :          0 :                                 bpf_prog_array_free(desc->bpf.inactive);
     264                 :          0 :                                 desc->bpf.inactive = NULL;
     265                 :            :                         }
     266                 :          0 :                         continue;
     267                 :            :                 }
     268                 :            : 
     269                 :       1242 :                 activate_effective_progs(desc, type, desc->bpf.inactive);
     270                 :       1242 :                 desc->bpf.inactive = NULL;
     271                 :            :         }
     272                 :            : 
     273                 :            :         return 0;
     274                 :            : 
     275                 :            : cleanup:
     276                 :            :         /* oom while computing effective. Free all computed effective arrays
     277                 :            :          * since they were not activated
     278                 :            :          */
     279         [ #  # ]:          0 :         css_for_each_descendant_pre(css, &cgrp->self) {
     280                 :            :                 struct cgroup *desc = container_of(css, struct cgroup, self);
     281                 :            : 
     282                 :          0 :                 bpf_prog_array_free(desc->bpf.inactive);
     283                 :          0 :                 desc->bpf.inactive = NULL;
     284                 :            :         }
     285                 :            : 
     286                 :          0 :         return err;
     287                 :            : }
     288                 :            : 
     289                 :            : #define BPF_CGROUP_MAX_PROGS 64
     290                 :            : 
     291                 :            : /**
     292                 :            :  * __cgroup_bpf_attach() - Attach the program to a cgroup, and
     293                 :            :  *                         propagate the change to descendants
     294                 :            :  * @cgrp: The cgroup which descendants to traverse
     295                 :            :  * @prog: A program to attach
     296                 :            :  * @type: Type of attach operation
     297                 :            :  * @flags: Option flags
     298                 :            :  *
     299                 :            :  * Must be called with cgroup_mutex held.
     300                 :            :  */
     301                 :       1242 : int __cgroup_bpf_attach(struct cgroup *cgrp, struct bpf_prog *prog,
     302                 :            :                         enum bpf_attach_type type, u32 flags)
     303                 :            : {
     304                 :       1242 :         struct list_head *progs = &cgrp->bpf.progs[type];
     305                 :            :         struct bpf_prog *old_prog = NULL;
     306                 :       1242 :         struct bpf_cgroup_storage *storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
     307                 :       1242 :         struct bpf_cgroup_storage *old_storage[MAX_BPF_CGROUP_STORAGE_TYPE] = {};
     308                 :            :         enum bpf_cgroup_storage_type stype;
     309                 :            :         struct bpf_prog_list *pl;
     310                 :            :         bool pl_was_allocated;
     311                 :            :         int err;
     312                 :            : 
     313         [ +  - ]:       1242 :         if ((flags & BPF_F_ALLOW_OVERRIDE) && (flags & BPF_F_ALLOW_MULTI))
     314                 :            :                 /* invalid combination */
     315                 :            :                 return -EINVAL;
     316                 :            : 
     317         [ +  - ]:       1242 :         if (!hierarchy_allows_attach(cgrp, type, flags))
     318                 :            :                 return -EPERM;
     319                 :            : 
     320   [ -  +  #  # ]:       1242 :         if (!list_empty(progs) && cgrp->bpf.flags[type] != flags)
     321                 :            :                 /* Disallow attaching non-overridable on top
     322                 :            :                  * of existing overridable in this cgroup.
     323                 :            :                  * Disallow attaching multi-prog if overridable or none
     324                 :            :                  */
     325                 :            :                 return -EPERM;
     326                 :            : 
     327         [ +  - ]:       1242 :         if (prog_list_length(progs) >= BPF_CGROUP_MAX_PROGS)
     328                 :            :                 return -E2BIG;
     329                 :            : 
     330         [ +  + ]:       2484 :         for_each_cgroup_storage_type(stype) {
     331                 :       2484 :                 storage[stype] = bpf_cgroup_storage_alloc(prog, stype);
     332         [ -  + ]:       2484 :                 if (IS_ERR(storage[stype])) {
     333                 :          0 :                         storage[stype] = NULL;
     334         [ #  # ]:          0 :                         for_each_cgroup_storage_type(stype)
     335                 :          0 :                                 bpf_cgroup_storage_free(storage[stype]);
     336                 :            :                         return -ENOMEM;
     337                 :            :                 }
     338                 :            :         }
     339                 :            : 
     340         [ -  + ]:       1242 :         if (flags & BPF_F_ALLOW_MULTI) {
     341         [ #  # ]:          0 :                 list_for_each_entry(pl, progs, node) {
     342         [ #  # ]:          0 :                         if (pl->prog == prog) {
     343                 :            :                                 /* disallow attaching the same prog twice */
     344         [ #  # ]:          0 :                                 for_each_cgroup_storage_type(stype)
     345                 :          0 :                                         bpf_cgroup_storage_free(storage[stype]);
     346                 :            :                                 return -EINVAL;
     347                 :            :                         }
     348                 :            :                 }
     349                 :            : 
     350                 :            :                 pl = kmalloc(sizeof(*pl), GFP_KERNEL);
     351         [ #  # ]:          0 :                 if (!pl) {
     352         [ #  # ]:          0 :                         for_each_cgroup_storage_type(stype)
     353                 :          0 :                                 bpf_cgroup_storage_free(storage[stype]);
     354                 :            :                         return -ENOMEM;
     355                 :            :                 }
     356                 :            : 
     357                 :            :                 pl_was_allocated = true;
     358                 :          0 :                 pl->prog = prog;
     359         [ #  # ]:          0 :                 for_each_cgroup_storage_type(stype)
     360                 :          0 :                         pl->storage[stype] = storage[stype];
     361                 :          0 :                 list_add_tail(&pl->node, progs);
     362                 :            :         } else {
     363         [ +  - ]:       1242 :                 if (list_empty(progs)) {
     364                 :            :                         pl = kmalloc(sizeof(*pl), GFP_KERNEL);
     365         [ -  + ]:       1242 :                         if (!pl) {
     366         [ #  # ]:          0 :                                 for_each_cgroup_storage_type(stype)
     367                 :          0 :                                         bpf_cgroup_storage_free(storage[stype]);
     368                 :            :                                 return -ENOMEM;
     369                 :            :                         }
     370                 :            :                         pl_was_allocated = true;
     371                 :       1242 :                         list_add_tail(&pl->node, progs);
     372                 :            :                 } else {
     373                 :          0 :                         pl = list_first_entry(progs, typeof(*pl), node);
     374                 :          0 :                         old_prog = pl->prog;
     375         [ #  # ]:          0 :                         for_each_cgroup_storage_type(stype) {
     376                 :          0 :                                 old_storage[stype] = pl->storage[stype];
     377                 :          0 :                                 bpf_cgroup_storage_unlink(old_storage[stype]);
     378                 :            :                         }
     379                 :            :                         pl_was_allocated = false;
     380                 :            :                 }
     381                 :       1242 :                 pl->prog = prog;
     382         [ +  + ]:       3726 :                 for_each_cgroup_storage_type(stype)
     383                 :       2484 :                         pl->storage[stype] = storage[stype];
     384                 :            :         }
     385                 :            : 
     386                 :       1242 :         cgrp->bpf.flags[type] = flags;
     387                 :            : 
     388                 :       1242 :         err = update_effective_progs(cgrp, type);
     389         [ +  - ]:       1242 :         if (err)
     390                 :            :                 goto cleanup;
     391                 :            : 
     392                 :       1242 :         static_branch_inc(&cgroup_bpf_enabled_key);
     393         [ +  + ]:       3726 :         for_each_cgroup_storage_type(stype) {
     394         [ +  - ]:       2484 :                 if (!old_storage[stype])
     395                 :       2484 :                         continue;
     396                 :          0 :                 bpf_cgroup_storage_free(old_storage[stype]);
     397                 :            :         }
     398         [ -  + ]:       1242 :         if (old_prog) {
     399                 :          0 :                 bpf_prog_put(old_prog);
     400                 :          0 :                 static_branch_dec(&cgroup_bpf_enabled_key);
     401                 :            :         }
     402         [ +  + ]:       2484 :         for_each_cgroup_storage_type(stype)
     403                 :       2484 :                 bpf_cgroup_storage_link(storage[stype], cgrp, type);
     404                 :            :         return 0;
     405                 :            : 
     406                 :            : cleanup:
     407                 :            :         /* and cleanup the prog list */
     408                 :          0 :         pl->prog = old_prog;
     409         [ #  # ]:          0 :         for_each_cgroup_storage_type(stype) {
     410                 :          0 :                 bpf_cgroup_storage_free(pl->storage[stype]);
     411                 :          0 :                 pl->storage[stype] = old_storage[stype];
     412                 :          0 :                 bpf_cgroup_storage_link(old_storage[stype], cgrp, type);
     413                 :            :         }
     414         [ #  # ]:          0 :         if (pl_was_allocated) {
     415                 :            :                 list_del(&pl->node);
     416                 :          0 :                 kfree(pl);
     417                 :            :         }
     418                 :          0 :         return err;
     419                 :            : }
     420                 :            : 
     421                 :            : /**
     422                 :            :  * __cgroup_bpf_detach() - Detach the program from a cgroup, and
     423                 :            :  *                         propagate the change to descendants
     424                 :            :  * @cgrp: The cgroup which descendants to traverse
     425                 :            :  * @prog: A program to detach or NULL
     426                 :            :  * @type: Type of detach operation
     427                 :            :  *
     428                 :            :  * Must be called with cgroup_mutex held.
     429                 :            :  */
     430                 :          0 : int __cgroup_bpf_detach(struct cgroup *cgrp, struct bpf_prog *prog,
     431                 :            :                         enum bpf_attach_type type)
     432                 :            : {
     433                 :          0 :         struct list_head *progs = &cgrp->bpf.progs[type];
     434                 :            :         enum bpf_cgroup_storage_type stype;
     435                 :          0 :         u32 flags = cgrp->bpf.flags[type];
     436                 :            :         struct bpf_prog *old_prog = NULL;
     437                 :            :         struct bpf_prog_list *pl;
     438                 :            :         int err;
     439                 :            : 
     440         [ #  # ]:          0 :         if (flags & BPF_F_ALLOW_MULTI) {
     441         [ #  # ]:          0 :                 if (!prog)
     442                 :            :                         /* to detach MULTI prog the user has to specify valid FD
     443                 :            :                          * of the program to be detached
     444                 :            :                          */
     445                 :            :                         return -EINVAL;
     446                 :            :         } else {
     447         [ #  # ]:          0 :                 if (list_empty(progs))
     448                 :            :                         /* report error when trying to detach and nothing is attached */
     449                 :            :                         return -ENOENT;
     450                 :            :         }
     451                 :            : 
     452         [ #  # ]:          0 :         if (flags & BPF_F_ALLOW_MULTI) {
     453                 :            :                 /* find the prog and detach it */
     454         [ #  # ]:          0 :                 list_for_each_entry(pl, progs, node) {
     455         [ #  # ]:          0 :                         if (pl->prog != prog)
     456                 :          0 :                                 continue;
     457                 :            :                         old_prog = prog;
     458                 :            :                         /* mark it deleted, so it's ignored while
     459                 :            :                          * recomputing effective
     460                 :            :                          */
     461                 :          0 :                         pl->prog = NULL;
     462                 :          0 :                         break;
     463                 :            :                 }
     464         [ #  # ]:          0 :                 if (!old_prog)
     465                 :            :                         return -ENOENT;
     466                 :            :         } else {
     467                 :            :                 /* to maintain backward compatibility NONE and OVERRIDE cgroups
     468                 :            :                  * allow detaching with invalid FD (prog==NULL)
     469                 :            :                  */
     470                 :          0 :                 pl = list_first_entry(progs, typeof(*pl), node);
     471                 :          0 :                 old_prog = pl->prog;
     472                 :          0 :                 pl->prog = NULL;
     473                 :            :         }
     474                 :            : 
     475                 :          0 :         err = update_effective_progs(cgrp, type);
     476         [ #  # ]:          0 :         if (err)
     477                 :            :                 goto cleanup;
     478                 :            : 
     479                 :            :         /* now can actually delete it from this cgroup list */
     480                 :            :         list_del(&pl->node);
     481         [ #  # ]:          0 :         for_each_cgroup_storage_type(stype) {
     482                 :          0 :                 bpf_cgroup_storage_unlink(pl->storage[stype]);
     483                 :          0 :                 bpf_cgroup_storage_free(pl->storage[stype]);
     484                 :            :         }
     485                 :          0 :         kfree(pl);
     486         [ #  # ]:          0 :         if (list_empty(progs))
     487                 :            :                 /* last program was detached, reset flags to zero */
     488                 :          0 :                 cgrp->bpf.flags[type] = 0;
     489                 :            : 
     490                 :          0 :         bpf_prog_put(old_prog);
     491                 :          0 :         static_branch_dec(&cgroup_bpf_enabled_key);
     492                 :          0 :         return 0;
     493                 :            : 
     494                 :            : cleanup:
     495                 :            :         /* and restore back old_prog */
     496                 :          0 :         pl->prog = old_prog;
     497                 :          0 :         return err;
     498                 :            : }
     499                 :            : 
     500                 :            : /* Must be called with cgroup_mutex held to avoid races. */
     501                 :          0 : int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
     502                 :            :                        union bpf_attr __user *uattr)
     503                 :            : {
     504                 :          0 :         __u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
     505                 :          0 :         enum bpf_attach_type type = attr->query.attach_type;
     506                 :          0 :         struct list_head *progs = &cgrp->bpf.progs[type];
     507                 :          0 :         u32 flags = cgrp->bpf.flags[type];
     508                 :            :         struct bpf_prog_array *effective;
     509                 :            :         int cnt, ret = 0, i;
     510                 :            : 
     511                 :          0 :         effective = rcu_dereference_protected(cgrp->bpf.effective[type],
     512                 :            :                                               lockdep_is_held(&cgroup_mutex));
     513                 :            : 
     514         [ #  # ]:          0 :         if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE)
     515                 :          0 :                 cnt = bpf_prog_array_length(effective);
     516                 :            :         else
     517                 :          0 :                 cnt = prog_list_length(progs);
     518                 :            : 
     519         [ #  # ]:          0 :         if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
     520                 :            :                 return -EFAULT;
     521         [ #  # ]:          0 :         if (copy_to_user(&uattr->query.prog_cnt, &cnt, sizeof(cnt)))
     522                 :            :                 return -EFAULT;
     523   [ #  #  #  #  :          0 :         if (attr->query.prog_cnt == 0 || !prog_ids || !cnt)
                   #  # ]
     524                 :            :                 /* return early if user requested only program count + flags */
     525                 :            :                 return 0;
     526         [ #  # ]:          0 :         if (attr->query.prog_cnt < cnt) {
     527                 :          0 :                 cnt = attr->query.prog_cnt;
     528                 :            :                 ret = -ENOSPC;
     529                 :            :         }
     530                 :            : 
     531         [ #  # ]:          0 :         if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
     532                 :          0 :                 return bpf_prog_array_copy_to_user(effective, prog_ids, cnt);
     533                 :            :         } else {
     534                 :            :                 struct bpf_prog_list *pl;
     535                 :            :                 u32 id;
     536                 :            : 
     537                 :            :                 i = 0;
     538         [ #  # ]:          0 :                 list_for_each_entry(pl, progs, node) {
     539                 :          0 :                         id = pl->prog->aux->id;
     540         [ #  # ]:          0 :                         if (copy_to_user(prog_ids + i, &id, sizeof(id)))
     541                 :          0 :                                 return -EFAULT;
     542         [ #  # ]:          0 :                         if (++i == cnt)
     543                 :            :                                 break;
     544                 :            :                 }
     545                 :            :         }
     546                 :          0 :         return ret;
     547                 :            : }
     548                 :            : 
     549                 :       1242 : int cgroup_bpf_prog_attach(const union bpf_attr *attr,
     550                 :            :                            enum bpf_prog_type ptype, struct bpf_prog *prog)
     551                 :            : {
     552                 :            :         struct cgroup *cgrp;
     553                 :            :         int ret;
     554                 :            : 
     555                 :       1242 :         cgrp = cgroup_get_from_fd(attr->target_fd);
     556         [ -  + ]:       1242 :         if (IS_ERR(cgrp))
     557                 :          0 :                 return PTR_ERR(cgrp);
     558                 :            : 
     559                 :       1242 :         ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
     560                 :            :                                 attr->attach_flags);
     561                 :            :         cgroup_put(cgrp);
     562                 :       1242 :         return ret;
     563                 :            : }
     564                 :            : 
     565                 :        207 : int cgroup_bpf_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype)
     566                 :            : {
     567                 :            :         struct bpf_prog *prog;
     568                 :            :         struct cgroup *cgrp;
     569                 :            :         int ret;
     570                 :            : 
     571                 :        207 :         cgrp = cgroup_get_from_fd(attr->target_fd);
     572         [ +  - ]:        207 :         if (IS_ERR(cgrp))
     573                 :        207 :                 return PTR_ERR(cgrp);
     574                 :            : 
     575                 :          0 :         prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
     576         [ #  # ]:          0 :         if (IS_ERR(prog))
     577                 :            :                 prog = NULL;
     578                 :            : 
     579                 :          0 :         ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
     580         [ #  # ]:          0 :         if (prog)
     581                 :          0 :                 bpf_prog_put(prog);
     582                 :            : 
     583                 :            :         cgroup_put(cgrp);
     584                 :          0 :         return ret;
     585                 :            : }
     586                 :            : 
     587                 :          0 : int cgroup_bpf_prog_query(const union bpf_attr *attr,
     588                 :            :                           union bpf_attr __user *uattr)
     589                 :            : {
     590                 :            :         struct cgroup *cgrp;
     591                 :            :         int ret;
     592                 :            : 
     593                 :          0 :         cgrp = cgroup_get_from_fd(attr->query.target_fd);
     594         [ #  # ]:          0 :         if (IS_ERR(cgrp))
     595                 :          0 :                 return PTR_ERR(cgrp);
     596                 :            : 
     597                 :          0 :         ret = cgroup_bpf_query(cgrp, attr, uattr);
     598                 :            : 
     599                 :            :         cgroup_put(cgrp);
     600                 :          0 :         return ret;
     601                 :            : }
     602                 :            : 
     603                 :            : /**
     604                 :            :  * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering
     605                 :            :  * @sk: The socket sending or receiving traffic
     606                 :            :  * @skb: The skb that is being sent or received
     607                 :            :  * @type: The type of program to be exectuted
     608                 :            :  *
     609                 :            :  * If no socket is passed, or the socket is not of type INET or INET6,
     610                 :            :  * this function does nothing and returns 0.
     611                 :            :  *
     612                 :            :  * The program type passed in via @type must be suitable for network
     613                 :            :  * filtering. No further check is performed to assert that.
     614                 :            :  *
     615                 :            :  * For egress packets, this function can return:
     616                 :            :  *   NET_XMIT_SUCCESS    (0)    - continue with packet output
     617                 :            :  *   NET_XMIT_DROP       (1)    - drop packet and notify TCP to call cwr
     618                 :            :  *   NET_XMIT_CN         (2)    - continue with packet output and notify TCP
     619                 :            :  *                                to call cwr
     620                 :            :  *   -EPERM                     - drop packet
     621                 :            :  *
     622                 :            :  * For ingress packets, this function will return -EPERM if any
     623                 :            :  * attached program was found and if it returned != 1 during execution.
     624                 :            :  * Otherwise 0 is returned.
     625                 :            :  */
     626                 :     736559 : int __cgroup_bpf_run_filter_skb(struct sock *sk,
     627                 :            :                                 struct sk_buff *skb,
     628                 :            :                                 enum bpf_attach_type type)
     629                 :            : {
     630                 :    1473118 :         unsigned int offset = skb->data - skb_network_header(skb);
     631                 :            :         struct sock *save_sk;
     632                 :            :         void *saved_data_end;
     633                 :            :         struct cgroup *cgrp;
     634                 :            :         int ret;
     635                 :            : 
     636   [ +  +  +  - ]:    1470915 :         if (!sk || !sk_fullsock(sk))
     637                 :            :                 return 0;
     638                 :            : 
     639         [ +  + ]:     737563 :         if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
     640                 :            :                 return 0;
     641                 :            : 
     642                 :            :         cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
     643                 :       9176 :         save_sk = skb->sk;
     644                 :       9176 :         skb->sk = sk;
     645                 :            :         __skb_push(skb, offset);
     646                 :            : 
     647                 :            :         /* compute pointers for the bpf prog */
     648                 :            :         bpf_compute_and_save_data_end(skb, &saved_data_end);
     649                 :            : 
     650         [ +  + ]:       9176 :         if (type == BPF_CGROUP_INET_EGRESS) {
     651   [ -  +  +  -  :      28650 :                 ret = BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY(
             +  -  #  # ]
     652                 :            :                         cgrp->bpf.effective[type], skb, __bpf_prog_run_save_cb);
     653                 :            :         } else {
     654         [ -  + ]:      17230 :                 ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], skb,
     655                 :            :                                           __bpf_prog_run_save_cb);
     656         [ -  + ]:       3446 :                 ret = (ret == 1 ? 0 : -EPERM);
     657                 :            :         }
     658                 :            :         bpf_restore_data_end(skb, saved_data_end);
     659                 :       9176 :         __skb_pull(skb, offset);
     660                 :       9176 :         skb->sk = save_sk;
     661                 :            : 
     662                 :       9176 :         return ret;
     663                 :            : }
     664                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_skb);
     665                 :            : 
     666                 :            : /**
     667                 :            :  * __cgroup_bpf_run_filter_sk() - Run a program on a sock
     668                 :            :  * @sk: sock structure to manipulate
     669                 :            :  * @type: The type of program to be exectuted
     670                 :            :  *
     671                 :            :  * socket is passed is expected to be of type INET or INET6.
     672                 :            :  *
     673                 :            :  * The program type passed in via @type must be suitable for sock
     674                 :            :  * filtering. No further check is performed to assert that.
     675                 :            :  *
     676                 :            :  * This function will return %-EPERM if any if an attached program was found
     677                 :            :  * and if it returned != 1 during execution. In all other cases, 0 is returned.
     678                 :            :  */
     679                 :       9067 : int __cgroup_bpf_run_filter_sk(struct sock *sk,
     680                 :            :                                enum bpf_attach_type type)
     681                 :            : {
     682                 :            :         struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
     683                 :            :         int ret;
     684                 :            : 
     685   [ #  #  -  + ]:      45335 :         ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sk, BPF_PROG_RUN);
     686         [ -  + ]:       9067 :         return ret == 1 ? 0 : -EPERM;
     687                 :            : }
     688                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_sk);
     689                 :            : 
     690                 :            : /**
     691                 :            :  * __cgroup_bpf_run_filter_sock_addr() - Run a program on a sock and
     692                 :            :  *                                       provided by user sockaddr
     693                 :            :  * @sk: sock struct that will use sockaddr
     694                 :            :  * @uaddr: sockaddr struct provided by user
     695                 :            :  * @type: The type of program to be exectuted
     696                 :            :  * @t_ctx: Pointer to attach type specific context
     697                 :            :  *
     698                 :            :  * socket is expected to be of type INET or INET6.
     699                 :            :  *
     700                 :            :  * This function will return %-EPERM if an attached program is found and
     701                 :            :  * returned value != 1 during execution. In all other cases, 0 is returned.
     702                 :            :  */
     703                 :      12962 : int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
     704                 :            :                                       struct sockaddr *uaddr,
     705                 :            :                                       enum bpf_attach_type type,
     706                 :            :                                       void *t_ctx)
     707                 :            : {
     708                 :      12962 :         struct bpf_sock_addr_kern ctx = {
     709                 :            :                 .sk = sk,
     710                 :            :                 .uaddr = uaddr,
     711                 :            :                 .t_ctx = t_ctx,
     712                 :            :         };
     713                 :            :         struct sockaddr_storage unspec;
     714                 :            :         struct cgroup *cgrp;
     715                 :            :         int ret;
     716                 :            : 
     717                 :            :         /* Check socket family since not all sockets represent network
     718                 :            :          * endpoint (e.g. AF_UNIX).
     719                 :            :          */
     720         [ +  - ]:      12962 :         if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)
     721                 :            :                 return 0;
     722                 :            : 
     723         [ -  + ]:      12962 :         if (!ctx.uaddr) {
     724                 :          0 :                 memset(&unspec, 0, sizeof(unspec));
     725                 :          0 :                 ctx.uaddr = (struct sockaddr *)&unspec;
     726                 :            :         }
     727                 :            : 
     728                 :            :         cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
     729   [ #  #  -  + ]:      64810 :         ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
     730                 :            : 
     731         [ -  + ]:      12962 :         return ret == 1 ? 0 : -EPERM;
     732                 :            : }
     733                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_addr);
     734                 :            : 
     735                 :            : /**
     736                 :            :  * __cgroup_bpf_run_filter_sock_ops() - Run a program on a sock
     737                 :            :  * @sk: socket to get cgroup from
     738                 :            :  * @sock_ops: bpf_sock_ops_kern struct to pass to program. Contains
     739                 :            :  * sk with connection information (IP addresses, etc.) May not contain
     740                 :            :  * cgroup info if it is a req sock.
     741                 :            :  * @type: The type of program to be exectuted
     742                 :            :  *
     743                 :            :  * socket passed is expected to be of type INET or INET6.
     744                 :            :  *
     745                 :            :  * The program type passed in via @type must be suitable for sock_ops
     746                 :            :  * filtering. No further check is performed to assert that.
     747                 :            :  *
     748                 :            :  * This function will return %-EPERM if any if an attached program was found
     749                 :            :  * and if it returned != 1 during execution. In all other cases, 0 is returned.
     750                 :            :  */
     751                 :          0 : int __cgroup_bpf_run_filter_sock_ops(struct sock *sk,
     752                 :            :                                      struct bpf_sock_ops_kern *sock_ops,
     753                 :            :                                      enum bpf_attach_type type)
     754                 :            : {
     755                 :            :         struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
     756                 :            :         int ret;
     757                 :            : 
     758   [ #  #  #  # ]:          0 :         ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], sock_ops,
     759                 :            :                                  BPF_PROG_RUN);
     760         [ #  # ]:          0 :         return ret == 1 ? 0 : -EPERM;
     761                 :            : }
     762                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_sock_ops);
     763                 :            : 
     764                 :     193446 : int __cgroup_bpf_check_dev_permission(short dev_type, u32 major, u32 minor,
     765                 :            :                                       short access, enum bpf_attach_type type)
     766                 :            : {
     767                 :            :         struct cgroup *cgrp;
     768                 :     193446 :         struct bpf_cgroup_dev_ctx ctx = {
     769                 :     193446 :                 .access_type = (access << 16) | dev_type,
     770                 :            :                 .major = major,
     771                 :            :                 .minor = minor,
     772                 :            :         };
     773                 :            :         int allow = 1;
     774                 :            : 
     775                 :            :         rcu_read_lock();
     776                 :     193451 :         cgrp = task_dfl_cgroup(current);
     777   [ #  #  -  + ]:     967324 :         allow = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx,
     778                 :            :                                    BPF_PROG_RUN);
     779                 :            :         rcu_read_unlock();
     780                 :            : 
     781                 :     193464 :         return !allow;
     782                 :            : }
     783                 :            : EXPORT_SYMBOL(__cgroup_bpf_check_dev_permission);
     784                 :            : 
     785                 :            : static const struct bpf_func_proto *
     786                 :          0 : cgroup_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
     787                 :            : {
     788   [ #  #  #  #  :          0 :         switch (func_id) {
          #  #  #  #  #  
                   #  # ]
     789                 :            :         case BPF_FUNC_map_lookup_elem:
     790                 :            :                 return &bpf_map_lookup_elem_proto;
     791                 :            :         case BPF_FUNC_map_update_elem:
     792                 :          0 :                 return &bpf_map_update_elem_proto;
     793                 :            :         case BPF_FUNC_map_delete_elem:
     794                 :          0 :                 return &bpf_map_delete_elem_proto;
     795                 :            :         case BPF_FUNC_map_push_elem:
     796                 :          0 :                 return &bpf_map_push_elem_proto;
     797                 :            :         case BPF_FUNC_map_pop_elem:
     798                 :          0 :                 return &bpf_map_pop_elem_proto;
     799                 :            :         case BPF_FUNC_map_peek_elem:
     800                 :          0 :                 return &bpf_map_peek_elem_proto;
     801                 :            :         case BPF_FUNC_get_current_uid_gid:
     802                 :          0 :                 return &bpf_get_current_uid_gid_proto;
     803                 :            :         case BPF_FUNC_get_local_storage:
     804                 :          0 :                 return &bpf_get_local_storage_proto;
     805                 :            :         case BPF_FUNC_get_current_cgroup_id:
     806                 :          0 :                 return &bpf_get_current_cgroup_id_proto;
     807                 :            :         case BPF_FUNC_trace_printk:
     808         [ #  # ]:          0 :                 if (capable(CAP_SYS_ADMIN))
     809                 :          0 :                         return bpf_get_trace_printk_proto();
     810                 :            :                 /* fall through */
     811                 :            :         default:
     812                 :            :                 return NULL;
     813                 :            :         }
     814                 :            : }
     815                 :            : 
     816                 :            : static const struct bpf_func_proto *
     817                 :          0 : cgroup_dev_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
     818                 :            : {
     819                 :          0 :         return cgroup_base_func_proto(func_id, prog);
     820                 :            : }
     821                 :            : 
     822                 :          0 : static bool cgroup_dev_is_valid_access(int off, int size,
     823                 :            :                                        enum bpf_access_type type,
     824                 :            :                                        const struct bpf_prog *prog,
     825                 :            :                                        struct bpf_insn_access_aux *info)
     826                 :            : {
     827                 :            :         const int size_default = sizeof(__u32);
     828                 :            : 
     829         [ #  # ]:          0 :         if (type == BPF_WRITE)
     830                 :            :                 return false;
     831                 :            : 
     832   [ #  #  #  # ]:          0 :         if (off < 0 || off + size > sizeof(struct bpf_cgroup_dev_ctx))
     833                 :            :                 return false;
     834                 :            :         /* The verifier guarantees that size > 0. */
     835         [ #  # ]:          0 :         if (off % size != 0)
     836                 :            :                 return false;
     837                 :            : 
     838         [ #  # ]:          0 :         switch (off) {
     839                 :            :         case bpf_ctx_range(struct bpf_cgroup_dev_ctx, access_type):
     840                 :            :                 bpf_ctx_record_field_size(info, size_default);
     841         [ #  # ]:          0 :                 if (!bpf_ctx_narrow_access_ok(off, size, size_default))
     842                 :            :                         return false;
     843                 :            :                 break;
     844                 :            :         default:
     845         [ #  # ]:          0 :                 if (size != size_default)
     846                 :            :                         return false;
     847                 :            :         }
     848                 :            : 
     849                 :            :         return true;
     850                 :            : }
     851                 :            : 
     852                 :            : const struct bpf_prog_ops cg_dev_prog_ops = {
     853                 :            : };
     854                 :            : 
     855                 :            : const struct bpf_verifier_ops cg_dev_verifier_ops = {
     856                 :            :         .get_func_proto         = cgroup_dev_func_proto,
     857                 :            :         .is_valid_access        = cgroup_dev_is_valid_access,
     858                 :            : };
     859                 :            : 
     860                 :            : /**
     861                 :            :  * __cgroup_bpf_run_filter_sysctl - Run a program on sysctl
     862                 :            :  *
     863                 :            :  * @head: sysctl table header
     864                 :            :  * @table: sysctl table
     865                 :            :  * @write: sysctl is being read (= 0) or written (= 1)
     866                 :            :  * @buf: pointer to buffer passed by user space
     867                 :            :  * @pcount: value-result argument: value is size of buffer pointed to by @buf,
     868                 :            :  *      result is size of @new_buf if program set new value, initial value
     869                 :            :  *      otherwise
     870                 :            :  * @ppos: value-result argument: value is position at which read from or write
     871                 :            :  *      to sysctl is happening, result is new position if program overrode it,
     872                 :            :  *      initial value otherwise
     873                 :            :  * @new_buf: pointer to pointer to new buffer that will be allocated if program
     874                 :            :  *      overrides new value provided by user space on sysctl write
     875                 :            :  *      NOTE: it's caller responsibility to free *new_buf if it was set
     876                 :            :  * @type: type of program to be executed
     877                 :            :  *
     878                 :            :  * Program is run when sysctl is being accessed, either read or written, and
     879                 :            :  * can allow or deny such access.
     880                 :            :  *
     881                 :            :  * This function will return %-EPERM if an attached program is found and
     882                 :            :  * returned value != 1 during execution. In all other cases 0 is returned.
     883                 :            :  */
     884                 :      17198 : int __cgroup_bpf_run_filter_sysctl(struct ctl_table_header *head,
     885                 :            :                                    struct ctl_table *table, int write,
     886                 :            :                                    void __user *buf, size_t *pcount,
     887                 :            :                                    loff_t *ppos, void **new_buf,
     888                 :            :                                    enum bpf_attach_type type)
     889                 :            : {
     890                 :      17198 :         struct bpf_sysctl_kern ctx = {
     891                 :            :                 .head = head,
     892                 :            :                 .table = table,
     893                 :            :                 .write = write,
     894                 :            :                 .ppos = ppos,
     895                 :            :                 .cur_val = NULL,
     896                 :            :                 .cur_len = PAGE_SIZE,
     897                 :            :                 .new_val = NULL,
     898                 :            :                 .new_len = 0,
     899                 :            :                 .new_updated = 0,
     900                 :            :         };
     901                 :            :         struct cgroup *cgrp;
     902                 :            :         int ret;
     903                 :            : 
     904                 :      17198 :         ctx.cur_val = kmalloc_track_caller(ctx.cur_len, GFP_KERNEL);
     905         [ +  - ]:      17198 :         if (ctx.cur_val) {
     906                 :            :                 mm_segment_t old_fs;
     907                 :      17198 :                 loff_t pos = 0;
     908                 :            : 
     909                 :      17198 :                 old_fs = get_fs();
     910                 :            :                 set_fs(KERNEL_DS);
     911         [ -  + ]:      17199 :                 if (table->proc_handler(table, 0, (void __user *)ctx.cur_val,
     912                 :            :                                         &ctx.cur_len, &pos)) {
     913                 :            :                         /* Let BPF program decide how to proceed. */
     914                 :          0 :                         ctx.cur_len = 0;
     915                 :            :                 }
     916                 :            :                 set_fs(old_fs);
     917                 :            :         } else {
     918                 :            :                 /* Let BPF program decide how to proceed. */
     919                 :          0 :                 ctx.cur_len = 0;
     920                 :            :         }
     921                 :            : 
     922   [ +  +  +  - ]:      17199 :         if (write && buf && *pcount) {
     923                 :            :                 /* BPF program should be able to override new value with a
     924                 :            :                  * buffer bigger than provided by user.
     925                 :            :                  */
     926                 :       1242 :                 ctx.new_val = kmalloc_track_caller(PAGE_SIZE, GFP_KERNEL);
     927                 :       1242 :                 ctx.new_len = min_t(size_t, PAGE_SIZE, *pcount);
     928   [ +  -  -  + ]:       2484 :                 if (!ctx.new_val ||
     929                 :            :                     copy_from_user(ctx.new_val, buf, ctx.new_len))
     930                 :            :                         /* Let BPF program decide how to proceed. */
     931                 :          0 :                         ctx.new_len = 0;
     932                 :            :         }
     933                 :            : 
     934                 :            :         rcu_read_lock();
     935                 :      17198 :         cgrp = task_dfl_cgroup(current);
     936   [ #  #  -  + ]:      85994 :         ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[type], &ctx, BPF_PROG_RUN);
     937                 :            :         rcu_read_unlock();
     938                 :            : 
     939                 :      17199 :         kfree(ctx.cur_val);
     940                 :            : 
     941   [ +  +  -  + ]:      17199 :         if (ret == 1 && ctx.new_updated) {
     942                 :          0 :                 *new_buf = ctx.new_val;
     943                 :          0 :                 *pcount = ctx.new_len;
     944                 :            :         } else {
     945                 :      17199 :                 kfree(ctx.new_val);
     946                 :            :         }
     947                 :            : 
     948         [ -  + ]:      17199 :         return ret == 1 ? 0 : -EPERM;
     949                 :            : }
     950                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_sysctl);
     951                 :            : 
     952                 :            : #ifdef CONFIG_NET
     953                 :            : static bool __cgroup_bpf_prog_array_is_empty(struct cgroup *cgrp,
     954                 :            :                                              enum bpf_attach_type attach_type)
     955                 :            : {
     956                 :            :         struct bpf_prog_array *prog_array;
     957                 :            :         bool empty;
     958                 :            : 
     959                 :            :         rcu_read_lock();
     960                 :     160854 :         prog_array = rcu_dereference(cgrp->bpf.effective[attach_type]);
     961                 :     160854 :         empty = bpf_prog_array_is_empty(prog_array);
     962                 :            :         rcu_read_unlock();
     963                 :            : 
     964                 :            :         return empty;
     965                 :            : }
     966                 :            : 
     967                 :          0 : static int sockopt_alloc_buf(struct bpf_sockopt_kern *ctx, int max_optlen)
     968                 :            : {
     969         [ #  # ]:          0 :         if (unlikely(max_optlen < 0))
     970                 :            :                 return -EINVAL;
     971                 :            : 
     972         [ #  # ]:          0 :         if (unlikely(max_optlen > PAGE_SIZE)) {
     973                 :            :                 /* We don't expose optvals that are greater than PAGE_SIZE
     974                 :            :                  * to the BPF program.
     975                 :            :                  */
     976                 :            :                 max_optlen = PAGE_SIZE;
     977                 :            :         }
     978                 :            : 
     979                 :          0 :         ctx->optval = kzalloc(max_optlen, GFP_USER);
     980         [ #  # ]:          0 :         if (!ctx->optval)
     981                 :            :                 return -ENOMEM;
     982                 :            : 
     983                 :          0 :         ctx->optval_end = ctx->optval + max_optlen;
     984                 :            : 
     985                 :          0 :         return max_optlen;
     986                 :            : }
     987                 :            : 
     988                 :            : static void sockopt_free_buf(struct bpf_sockopt_kern *ctx)
     989                 :            : {
     990                 :          0 :         kfree(ctx->optval);
     991                 :            : }
     992                 :            : 
     993                 :      63800 : int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
     994                 :            :                                        int *optname, char __user *optval,
     995                 :            :                                        int *optlen, char **kernel_optval)
     996                 :            : {
     997                 :            :         struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
     998                 :     191400 :         struct bpf_sockopt_kern ctx = {
     999                 :            :                 .sk = sk,
    1000                 :      63800 :                 .level = *level,
    1001                 :      63800 :                 .optname = *optname,
    1002                 :            :         };
    1003                 :            :         int ret, max_optlen;
    1004                 :            : 
    1005                 :            :         /* Opportunistic check to see whether we have any BPF program
    1006                 :            :          * attached to the hook so we don't waste time allocating
    1007                 :            :          * memory and locking the socket.
    1008                 :            :          */
    1009   [ +  -  -  + ]:     127547 :         if (!cgroup_bpf_enabled ||
    1010                 :            :             __cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_SETSOCKOPT))
    1011                 :            :                 return 0;
    1012                 :            : 
    1013                 :            :         /* Allocate a bit more than the initial user buffer for
    1014                 :            :          * BPF program. The canonical use case is overriding
    1015                 :            :          * TCP_CONGESTION(nv) to TCP_CONGESTION(cubic).
    1016                 :            :          */
    1017                 :          0 :         max_optlen = max_t(int, 16, *optlen);
    1018                 :            : 
    1019                 :          0 :         max_optlen = sockopt_alloc_buf(&ctx, max_optlen);
    1020         [ #  # ]:          0 :         if (max_optlen < 0)
    1021                 :            :                 return max_optlen;
    1022                 :            : 
    1023                 :          0 :         ctx.optlen = *optlen;
    1024                 :            : 
    1025         [ #  # ]:          0 :         if (copy_from_user(ctx.optval, optval, min(*optlen, max_optlen)) != 0) {
    1026                 :            :                 ret = -EFAULT;
    1027                 :            :                 goto out;
    1028                 :            :         }
    1029                 :            : 
    1030                 :            :         lock_sock(sk);
    1031   [ #  #  #  # ]:          0 :         ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_SETSOCKOPT],
    1032                 :            :                                  &ctx, BPF_PROG_RUN);
    1033                 :          0 :         release_sock(sk);
    1034                 :            : 
    1035         [ #  # ]:          0 :         if (!ret) {
    1036                 :            :                 ret = -EPERM;
    1037                 :            :                 goto out;
    1038                 :            :         }
    1039                 :            : 
    1040         [ #  # ]:          0 :         if (ctx.optlen == -1) {
    1041                 :            :                 /* optlen set to -1, bypass kernel */
    1042                 :            :                 ret = 1;
    1043   [ #  #  #  # ]:          0 :         } else if (ctx.optlen > max_optlen || ctx.optlen < -1) {
    1044                 :            :                 /* optlen is out of bounds */
    1045                 :            :                 ret = -EFAULT;
    1046                 :            :         } else {
    1047                 :            :                 /* optlen within bounds, run kernel handler */
    1048                 :            :                 ret = 0;
    1049                 :            : 
    1050                 :            :                 /* export any potential modifications */
    1051                 :          0 :                 *level = ctx.level;
    1052                 :          0 :                 *optname = ctx.optname;
    1053                 :            : 
    1054                 :            :                 /* optlen == 0 from BPF indicates that we should
    1055                 :            :                  * use original userspace data.
    1056                 :            :                  */
    1057         [ #  # ]:          0 :                 if (ctx.optlen != 0) {
    1058                 :          0 :                         *optlen = ctx.optlen;
    1059                 :          0 :                         *kernel_optval = ctx.optval;
    1060                 :            :                 }
    1061                 :            :         }
    1062                 :            : 
    1063                 :            : out:
    1064         [ #  # ]:          0 :         if (ret)
    1065                 :            :                 sockopt_free_buf(&ctx);
    1066                 :          0 :         return ret;
    1067                 :            : }
    1068                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_setsockopt);
    1069                 :            : 
    1070                 :      97138 : int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,
    1071                 :            :                                        int optname, char __user *optval,
    1072                 :            :                                        int __user *optlen, int max_optlen,
    1073                 :            :                                        int retval)
    1074                 :            : {
    1075                 :            :         struct cgroup *cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
    1076                 :      97138 :         struct bpf_sockopt_kern ctx = {
    1077                 :            :                 .sk = sk,
    1078                 :            :                 .level = level,
    1079                 :            :                 .optname = optname,
    1080                 :            :                 .retval = retval,
    1081                 :            :         };
    1082                 :            :         int ret;
    1083                 :            : 
    1084                 :            :         /* Opportunistic check to see whether we have any BPF program
    1085                 :            :          * attached to the hook so we don't waste time allocating
    1086                 :            :          * memory and locking the socket.
    1087                 :            :          */
    1088   [ +  -  +  - ]:     194225 :         if (!cgroup_bpf_enabled ||
    1089                 :            :             __cgroup_bpf_prog_array_is_empty(cgrp, BPF_CGROUP_GETSOCKOPT))
    1090                 :      97105 :                 return retval;
    1091                 :            : 
    1092                 :          0 :         ctx.optlen = max_optlen;
    1093                 :            : 
    1094                 :          0 :         max_optlen = sockopt_alloc_buf(&ctx, max_optlen);
    1095         [ #  # ]:          0 :         if (max_optlen < 0)
    1096                 :            :                 return max_optlen;
    1097                 :            : 
    1098         [ #  # ]:          0 :         if (!retval) {
    1099                 :            :                 /* If kernel getsockopt finished successfully,
    1100                 :            :                  * copy whatever was returned to the user back
    1101                 :            :                  * into our temporary buffer. Set optlen to the
    1102                 :            :                  * one that kernel returned as well to let
    1103                 :            :                  * BPF programs inspect the value.
    1104                 :            :                  */
    1105                 :            : 
    1106         [ #  # ]:          0 :                 if (get_user(ctx.optlen, optlen)) {
    1107                 :            :                         ret = -EFAULT;
    1108                 :            :                         goto out;
    1109                 :            :                 }
    1110                 :            : 
    1111         [ #  # ]:          0 :                 if (copy_from_user(ctx.optval, optval,
    1112                 :          0 :                                    min(ctx.optlen, max_optlen)) != 0) {
    1113                 :            :                         ret = -EFAULT;
    1114                 :            :                         goto out;
    1115                 :            :                 }
    1116                 :            :         }
    1117                 :            : 
    1118                 :            :         lock_sock(sk);
    1119   [ #  #  #  # ]:          0 :         ret = BPF_PROG_RUN_ARRAY(cgrp->bpf.effective[BPF_CGROUP_GETSOCKOPT],
    1120                 :            :                                  &ctx, BPF_PROG_RUN);
    1121                 :          0 :         release_sock(sk);
    1122                 :            : 
    1123         [ #  # ]:          0 :         if (!ret) {
    1124                 :            :                 ret = -EPERM;
    1125                 :            :                 goto out;
    1126                 :            :         }
    1127                 :            : 
    1128         [ #  # ]:          0 :         if (ctx.optlen > max_optlen) {
    1129                 :            :                 ret = -EFAULT;
    1130                 :            :                 goto out;
    1131                 :            :         }
    1132                 :            : 
    1133                 :            :         /* BPF programs only allowed to set retval to 0, not some
    1134                 :            :          * arbitrary value.
    1135                 :            :          */
    1136   [ #  #  #  # ]:          0 :         if (ctx.retval != 0 && ctx.retval != retval) {
    1137                 :            :                 ret = -EFAULT;
    1138                 :            :                 goto out;
    1139                 :            :         }
    1140                 :            : 
    1141         [ #  # ]:          0 :         if (ctx.optlen != 0) {
    1142   [ #  #  #  # ]:          0 :                 if (copy_to_user(optval, ctx.optval, ctx.optlen) ||
    1143                 :          0 :                     put_user(ctx.optlen, optlen)) {
    1144                 :            :                         ret = -EFAULT;
    1145                 :            :                         goto out;
    1146                 :            :                 }
    1147                 :            :         }
    1148                 :            : 
    1149                 :          0 :         ret = ctx.retval;
    1150                 :            : 
    1151                 :            : out:
    1152                 :            :         sockopt_free_buf(&ctx);
    1153                 :          0 :         return ret;
    1154                 :            : }
    1155                 :            : EXPORT_SYMBOL(__cgroup_bpf_run_filter_getsockopt);
    1156                 :            : #endif
    1157                 :            : 
    1158                 :          0 : static ssize_t sysctl_cpy_dir(const struct ctl_dir *dir, char **bufp,
    1159                 :            :                               size_t *lenp)
    1160                 :            : {
    1161                 :            :         ssize_t tmp_ret = 0, ret;
    1162                 :            : 
    1163         [ #  # ]:          0 :         if (dir->header.parent) {
    1164                 :          0 :                 tmp_ret = sysctl_cpy_dir(dir->header.parent, bufp, lenp);
    1165         [ #  # ]:          0 :                 if (tmp_ret < 0)
    1166                 :            :                         return tmp_ret;
    1167                 :            :         }
    1168                 :            : 
    1169                 :          0 :         ret = strscpy(*bufp, dir->header.ctl_table[0].procname, *lenp);
    1170         [ #  # ]:          0 :         if (ret < 0)
    1171                 :            :                 return ret;
    1172                 :          0 :         *bufp += ret;
    1173                 :          0 :         *lenp -= ret;
    1174                 :          0 :         ret += tmp_ret;
    1175                 :            : 
    1176                 :            :         /* Avoid leading slash. */
    1177         [ #  # ]:          0 :         if (!ret)
    1178                 :            :                 return ret;
    1179                 :            : 
    1180                 :          0 :         tmp_ret = strscpy(*bufp, "/", *lenp);
    1181         [ #  # ]:          0 :         if (tmp_ret < 0)
    1182                 :            :                 return tmp_ret;
    1183                 :          0 :         *bufp += tmp_ret;
    1184                 :          0 :         *lenp -= tmp_ret;
    1185                 :            : 
    1186                 :          0 :         return ret + tmp_ret;
    1187                 :            : }
    1188                 :            : 
    1189                 :          0 : BPF_CALL_4(bpf_sysctl_get_name, struct bpf_sysctl_kern *, ctx, char *, buf,
    1190                 :            :            size_t, buf_len, u64, flags)
    1191                 :            : {
    1192                 :            :         ssize_t tmp_ret = 0, ret;
    1193                 :            : 
    1194         [ #  # ]:          0 :         if (!buf)
    1195                 :            :                 return -EINVAL;
    1196                 :            : 
    1197         [ #  # ]:          0 :         if (!(flags & BPF_F_SYSCTL_BASE_NAME)) {
    1198         [ #  # ]:          0 :                 if (!ctx->head)
    1199                 :            :                         return -EINVAL;
    1200                 :          0 :                 tmp_ret = sysctl_cpy_dir(ctx->head->parent, &buf, &buf_len);
    1201         [ #  # ]:          0 :                 if (tmp_ret < 0)
    1202                 :          0 :                         return tmp_ret;
    1203                 :            :         }
    1204                 :            : 
    1205                 :          0 :         ret = strscpy(buf, ctx->table->procname, buf_len);
    1206                 :            : 
    1207         [ #  # ]:          0 :         return ret < 0 ? ret : tmp_ret + ret;
    1208                 :            : }
    1209                 :            : 
    1210                 :            : static const struct bpf_func_proto bpf_sysctl_get_name_proto = {
    1211                 :            :         .func           = bpf_sysctl_get_name,
    1212                 :            :         .gpl_only       = false,
    1213                 :            :         .ret_type       = RET_INTEGER,
    1214                 :            :         .arg1_type      = ARG_PTR_TO_CTX,
    1215                 :            :         .arg2_type      = ARG_PTR_TO_MEM,
    1216                 :            :         .arg3_type      = ARG_CONST_SIZE,
    1217                 :            :         .arg4_type      = ARG_ANYTHING,
    1218                 :            : };
    1219                 :            : 
    1220                 :          0 : static int copy_sysctl_value(char *dst, size_t dst_len, char *src,
    1221                 :            :                              size_t src_len)
    1222                 :            : {
    1223         [ #  # ]:          0 :         if (!dst)
    1224                 :            :                 return -EINVAL;
    1225                 :            : 
    1226         [ #  # ]:          0 :         if (!dst_len)
    1227                 :            :                 return -E2BIG;
    1228                 :            : 
    1229         [ #  # ]:          0 :         if (!src || !src_len) {
    1230                 :          0 :                 memset(dst, 0, dst_len);
    1231                 :          0 :                 return -EINVAL;
    1232                 :            :         }
    1233                 :            : 
    1234                 :          0 :         memcpy(dst, src, min(dst_len, src_len));
    1235                 :            : 
    1236         [ #  # ]:          0 :         if (dst_len > src_len) {
    1237                 :          0 :                 memset(dst + src_len, '\0', dst_len - src_len);
    1238                 :          0 :                 return src_len;
    1239                 :            :         }
    1240                 :            : 
    1241                 :          0 :         dst[dst_len - 1] = '\0';
    1242                 :            : 
    1243                 :          0 :         return -E2BIG;
    1244                 :            : }
    1245                 :            : 
    1246                 :          0 : BPF_CALL_3(bpf_sysctl_get_current_value, struct bpf_sysctl_kern *, ctx,
    1247                 :            :            char *, buf, size_t, buf_len)
    1248                 :            : {
    1249                 :          0 :         return copy_sysctl_value(buf, buf_len, ctx->cur_val, ctx->cur_len);
    1250                 :            : }
    1251                 :            : 
    1252                 :            : static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = {
    1253                 :            :         .func           = bpf_sysctl_get_current_value,
    1254                 :            :         .gpl_only       = false,
    1255                 :            :         .ret_type       = RET_INTEGER,
    1256                 :            :         .arg1_type      = ARG_PTR_TO_CTX,
    1257                 :            :         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
    1258                 :            :         .arg3_type      = ARG_CONST_SIZE,
    1259                 :            : };
    1260                 :            : 
    1261                 :          0 : BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
    1262                 :            :            size_t, buf_len)
    1263                 :            : {
    1264         [ #  # ]:          0 :         if (!ctx->write) {
    1265         [ #  # ]:          0 :                 if (buf && buf_len)
    1266                 :          0 :                         memset(buf, '\0', buf_len);
    1267                 :            :                 return -EINVAL;
    1268                 :            :         }
    1269                 :          0 :         return copy_sysctl_value(buf, buf_len, ctx->new_val, ctx->new_len);
    1270                 :            : }
    1271                 :            : 
    1272                 :            : static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = {
    1273                 :            :         .func           = bpf_sysctl_get_new_value,
    1274                 :            :         .gpl_only       = false,
    1275                 :            :         .ret_type       = RET_INTEGER,
    1276                 :            :         .arg1_type      = ARG_PTR_TO_CTX,
    1277                 :            :         .arg2_type      = ARG_PTR_TO_UNINIT_MEM,
    1278                 :            :         .arg3_type      = ARG_CONST_SIZE,
    1279                 :            : };
    1280                 :            : 
    1281                 :          0 : BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
    1282                 :            :            const char *, buf, size_t, buf_len)
    1283                 :            : {
    1284   [ #  #  #  #  :          0 :         if (!ctx->write || !ctx->new_val || !ctx->new_len || !buf || !buf_len)
             #  #  #  # ]
    1285                 :            :                 return -EINVAL;
    1286                 :            : 
    1287         [ #  # ]:          0 :         if (buf_len > PAGE_SIZE - 1)
    1288                 :            :                 return -E2BIG;
    1289                 :            : 
    1290                 :          0 :         memcpy(ctx->new_val, buf, buf_len);
    1291                 :          0 :         ctx->new_len = buf_len;
    1292                 :          0 :         ctx->new_updated = 1;
    1293                 :            : 
    1294                 :            :         return 0;
    1295                 :            : }
    1296                 :            : 
    1297                 :            : static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = {
    1298                 :            :         .func           = bpf_sysctl_set_new_value,
    1299                 :            :         .gpl_only       = false,
    1300                 :            :         .ret_type       = RET_INTEGER,
    1301                 :            :         .arg1_type      = ARG_PTR_TO_CTX,
    1302                 :            :         .arg2_type      = ARG_PTR_TO_MEM,
    1303                 :            :         .arg3_type      = ARG_CONST_SIZE,
    1304                 :            : };
    1305                 :            : 
    1306                 :            : static const struct bpf_func_proto *
    1307                 :          0 : sysctl_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
    1308                 :            : {
    1309   [ #  #  #  #  :          0 :         switch (func_id) {
                #  #  # ]
    1310                 :            :         case BPF_FUNC_strtol:
    1311                 :            :                 return &bpf_strtol_proto;
    1312                 :            :         case BPF_FUNC_strtoul:
    1313                 :          0 :                 return &bpf_strtoul_proto;
    1314                 :            :         case BPF_FUNC_sysctl_get_name:
    1315                 :          0 :                 return &bpf_sysctl_get_name_proto;
    1316                 :            :         case BPF_FUNC_sysctl_get_current_value:
    1317                 :          0 :                 return &bpf_sysctl_get_current_value_proto;
    1318                 :            :         case BPF_FUNC_sysctl_get_new_value:
    1319                 :          0 :                 return &bpf_sysctl_get_new_value_proto;
    1320                 :            :         case BPF_FUNC_sysctl_set_new_value:
    1321                 :          0 :                 return &bpf_sysctl_set_new_value_proto;
    1322                 :            :         default:
    1323                 :          0 :                 return cgroup_base_func_proto(func_id, prog);
    1324                 :            :         }
    1325                 :            : }
    1326                 :            : 
    1327                 :          0 : static bool sysctl_is_valid_access(int off, int size, enum bpf_access_type type,
    1328                 :            :                                    const struct bpf_prog *prog,
    1329                 :            :                                    struct bpf_insn_access_aux *info)
    1330                 :            : {
    1331                 :            :         const int size_default = sizeof(__u32);
    1332                 :            : 
    1333   [ #  #  #  #  :          0 :         if (off < 0 || off + size > sizeof(struct bpf_sysctl) || off % size)
                   #  # ]
    1334                 :            :                 return false;
    1335                 :            : 
    1336      [ #  #  # ]:          0 :         switch (off) {
    1337                 :            :         case bpf_ctx_range(struct bpf_sysctl, write):
    1338         [ #  # ]:          0 :                 if (type != BPF_READ)
    1339                 :            :                         return false;
    1340                 :            :                 bpf_ctx_record_field_size(info, size_default);
    1341                 :          0 :                 return bpf_ctx_narrow_access_ok(off, size, size_default);
    1342                 :            :         case bpf_ctx_range(struct bpf_sysctl, file_pos):
    1343         [ #  # ]:          0 :                 if (type == BPF_READ) {
    1344                 :            :                         bpf_ctx_record_field_size(info, size_default);
    1345                 :          0 :                         return bpf_ctx_narrow_access_ok(off, size, size_default);
    1346                 :            :                 } else {
    1347                 :          0 :                         return size == size_default;
    1348                 :            :                 }
    1349                 :            :         default:
    1350                 :            :                 return false;
    1351                 :            :         }
    1352                 :            : }
    1353                 :            : 
    1354                 :          0 : static u32 sysctl_convert_ctx_access(enum bpf_access_type type,
    1355                 :            :                                      const struct bpf_insn *si,
    1356                 :            :                                      struct bpf_insn *insn_buf,
    1357                 :            :                                      struct bpf_prog *prog, u32 *target_size)
    1358                 :            : {
    1359                 :            :         struct bpf_insn *insn = insn_buf;
    1360                 :            :         u32 read_size;
    1361                 :            : 
    1362      [ #  #  # ]:          0 :         switch (si->off) {
    1363                 :            :         case offsetof(struct bpf_sysctl, write):
    1364                 :          0 :                 *insn++ = BPF_LDX_MEM(
    1365                 :            :                         BPF_SIZE(si->code), si->dst_reg, si->src_reg,
    1366                 :            :                         bpf_target_off(struct bpf_sysctl_kern, write,
    1367                 :            :                                        FIELD_SIZEOF(struct bpf_sysctl_kern,
    1368                 :            :                                                     write),
    1369                 :            :                                        target_size));
    1370                 :          0 :                 break;
    1371                 :            :         case offsetof(struct bpf_sysctl, file_pos):
    1372                 :            :                 /* ppos is a pointer so it should be accessed via indirect
    1373                 :            :                  * loads and stores. Also for stores additional temporary
    1374                 :            :                  * register is used since neither src_reg nor dst_reg can be
    1375                 :            :                  * overridden.
    1376                 :            :                  */
    1377         [ #  # ]:          0 :                 if (type == BPF_WRITE) {
    1378                 :            :                         int treg = BPF_REG_9;
    1379                 :            : 
    1380   [ #  #  #  # ]:          0 :                         if (si->src_reg == treg || si->dst_reg == treg)
    1381                 :            :                                 --treg;
    1382   [ #  #  #  # ]:          0 :                         if (si->src_reg == treg || si->dst_reg == treg)
    1383                 :          0 :                                 --treg;
    1384                 :          0 :                         *insn++ = BPF_STX_MEM(
    1385                 :            :                                 BPF_DW, si->dst_reg, treg,
    1386                 :            :                                 offsetof(struct bpf_sysctl_kern, tmp_reg));
    1387                 :          0 :                         *insn++ = BPF_LDX_MEM(
    1388                 :            :                                 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
    1389                 :            :                                 treg, si->dst_reg,
    1390                 :            :                                 offsetof(struct bpf_sysctl_kern, ppos));
    1391                 :          0 :                         *insn++ = BPF_STX_MEM(
    1392                 :            :                                 BPF_SIZEOF(u32), treg, si->src_reg,
    1393                 :            :                                 bpf_ctx_narrow_access_offset(
    1394                 :            :                                         0, sizeof(u32), sizeof(loff_t)));
    1395                 :          0 :                         *insn++ = BPF_LDX_MEM(
    1396                 :            :                                 BPF_DW, treg, si->dst_reg,
    1397                 :            :                                 offsetof(struct bpf_sysctl_kern, tmp_reg));
    1398                 :            :                 } else {
    1399                 :          0 :                         *insn++ = BPF_LDX_MEM(
    1400                 :            :                                 BPF_FIELD_SIZEOF(struct bpf_sysctl_kern, ppos),
    1401                 :            :                                 si->dst_reg, si->src_reg,
    1402                 :            :                                 offsetof(struct bpf_sysctl_kern, ppos));
    1403                 :          0 :                         read_size = bpf_size_to_bytes(BPF_SIZE(si->code));
    1404                 :          0 :                         *insn++ = BPF_LDX_MEM(
    1405                 :            :                                 BPF_SIZE(si->code), si->dst_reg, si->dst_reg,
    1406                 :            :                                 bpf_ctx_narrow_access_offset(
    1407                 :            :                                         0, read_size, sizeof(loff_t)));
    1408                 :            :                 }
    1409                 :          0 :                 *target_size = sizeof(u32);
    1410                 :          0 :                 break;
    1411                 :            :         }
    1412                 :            : 
    1413                 :          0 :         return insn - insn_buf;
    1414                 :            : }
    1415                 :            : 
    1416                 :            : const struct bpf_verifier_ops cg_sysctl_verifier_ops = {
    1417                 :            :         .get_func_proto         = sysctl_func_proto,
    1418                 :            :         .is_valid_access        = sysctl_is_valid_access,
    1419                 :            :         .convert_ctx_access     = sysctl_convert_ctx_access,
    1420                 :            : };
    1421                 :            : 
    1422                 :            : const struct bpf_prog_ops cg_sysctl_prog_ops = {
    1423                 :            : };
    1424                 :            : 
    1425                 :            : static const struct bpf_func_proto *
    1426                 :          0 : cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
    1427                 :            : {
    1428   [ #  #  #  # ]:          0 :         switch (func_id) {
    1429                 :            : #ifdef CONFIG_NET
    1430                 :            :         case BPF_FUNC_sk_storage_get:
    1431                 :            :                 return &bpf_sk_storage_get_proto;
    1432                 :            :         case BPF_FUNC_sk_storage_delete:
    1433                 :          0 :                 return &bpf_sk_storage_delete_proto;
    1434                 :            : #endif
    1435                 :            : #ifdef CONFIG_INET
    1436                 :            :         case BPF_FUNC_tcp_sock:
    1437                 :          0 :                 return &bpf_tcp_sock_proto;
    1438                 :            : #endif
    1439                 :            :         default:
    1440                 :          0 :                 return cgroup_base_func_proto(func_id, prog);
    1441                 :            :         }
    1442                 :            : }
    1443                 :            : 
    1444                 :          0 : static bool cg_sockopt_is_valid_access(int off, int size,
    1445                 :            :                                        enum bpf_access_type type,
    1446                 :            :                                        const struct bpf_prog *prog,
    1447                 :            :                                        struct bpf_insn_access_aux *info)
    1448                 :            : {
    1449                 :            :         const int size_default = sizeof(__u32);
    1450                 :            : 
    1451         [ #  # ]:          0 :         if (off < 0 || off >= sizeof(struct bpf_sockopt))
    1452                 :            :                 return false;
    1453                 :            : 
    1454         [ #  # ]:          0 :         if (off % size != 0)
    1455                 :            :                 return false;
    1456                 :            : 
    1457         [ #  # ]:          0 :         if (type == BPF_WRITE) {
    1458   [ #  #  #  # ]:          0 :                 switch (off) {
    1459                 :            :                 case offsetof(struct bpf_sockopt, retval):
    1460         [ #  # ]:          0 :                         if (size != size_default)
    1461                 :            :                                 return false;
    1462                 :          0 :                         return prog->expected_attach_type ==
    1463                 :            :                                 BPF_CGROUP_GETSOCKOPT;
    1464                 :            :                 case offsetof(struct bpf_sockopt, optname):
    1465                 :            :                         /* fallthrough */
    1466                 :            :                 case offsetof(struct bpf_sockopt, level):
    1467         [ #  # ]:          0 :                         if (size != size_default)
    1468                 :            :                                 return false;
    1469                 :          0 :                         return prog->expected_attach_type ==
    1470                 :            :                                 BPF_CGROUP_SETSOCKOPT;
    1471                 :            :                 case offsetof(struct bpf_sockopt, optlen):
    1472                 :          0 :                         return size == size_default;
    1473                 :            :                 default:
    1474                 :            :                         return false;
    1475                 :            :                 }
    1476                 :            :         }
    1477                 :            : 
    1478   [ #  #  #  #  :          0 :         switch (off) {
                      # ]
    1479                 :            :         case offsetof(struct bpf_sockopt, sk):
    1480         [ #  # ]:          0 :                 if (size != sizeof(__u64))
    1481                 :            :                         return false;
    1482                 :          0 :                 info->reg_type = PTR_TO_SOCKET;
    1483                 :          0 :                 break;
    1484                 :            :         case offsetof(struct bpf_sockopt, optval):
    1485         [ #  # ]:          0 :                 if (size != sizeof(__u64))
    1486                 :            :                         return false;
    1487                 :          0 :                 info->reg_type = PTR_TO_PACKET;
    1488                 :          0 :                 break;
    1489                 :            :         case offsetof(struct bpf_sockopt, optval_end):
    1490         [ #  # ]:          0 :                 if (size != sizeof(__u64))
    1491                 :            :                         return false;
    1492                 :          0 :                 info->reg_type = PTR_TO_PACKET_END;
    1493                 :          0 :                 break;
    1494                 :            :         case offsetof(struct bpf_sockopt, retval):
    1495         [ #  # ]:          0 :                 if (size != size_default)
    1496                 :            :                         return false;
    1497                 :          0 :                 return prog->expected_attach_type == BPF_CGROUP_GETSOCKOPT;
    1498                 :            :         default:
    1499         [ #  # ]:          0 :                 if (size != size_default)
    1500                 :            :                         return false;
    1501                 :            :                 break;
    1502                 :            :         }
    1503                 :            :         return true;
    1504                 :            : }
    1505                 :            : 
    1506                 :            : #define CG_SOCKOPT_ACCESS_FIELD(T, F)                                   \
    1507                 :            :         T(BPF_FIELD_SIZEOF(struct bpf_sockopt_kern, F),                 \
    1508                 :            :           si->dst_reg, si->src_reg,                                       \
    1509                 :            :           offsetof(struct bpf_sockopt_kern, F))
    1510                 :            : 
    1511                 :          0 : static u32 cg_sockopt_convert_ctx_access(enum bpf_access_type type,
    1512                 :            :                                          const struct bpf_insn *si,
    1513                 :            :                                          struct bpf_insn *insn_buf,
    1514                 :            :                                          struct bpf_prog *prog,
    1515                 :            :                                          u32 *target_size)
    1516                 :            : {
    1517                 :            :         struct bpf_insn *insn = insn_buf;
    1518                 :            : 
    1519   [ #  #  #  #  :          0 :         switch (si->off) {
             #  #  #  # ]
    1520                 :            :         case offsetof(struct bpf_sockopt, sk):
    1521                 :          0 :                 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, sk);
    1522                 :          0 :                 break;
    1523                 :            :         case offsetof(struct bpf_sockopt, level):
    1524         [ #  # ]:          0 :                 if (type == BPF_WRITE)
    1525                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, level);
    1526                 :            :                 else
    1527                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, level);
    1528                 :            :                 break;
    1529                 :            :         case offsetof(struct bpf_sockopt, optname):
    1530         [ #  # ]:          0 :                 if (type == BPF_WRITE)
    1531                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optname);
    1532                 :            :                 else
    1533                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optname);
    1534                 :            :                 break;
    1535                 :            :         case offsetof(struct bpf_sockopt, optlen):
    1536         [ #  # ]:          0 :                 if (type == BPF_WRITE)
    1537                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, optlen);
    1538                 :            :                 else
    1539                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optlen);
    1540                 :            :                 break;
    1541                 :            :         case offsetof(struct bpf_sockopt, retval):
    1542         [ #  # ]:          0 :                 if (type == BPF_WRITE)
    1543                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_STX_MEM, retval);
    1544                 :            :                 else
    1545                 :          0 :                         *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, retval);
    1546                 :            :                 break;
    1547                 :            :         case offsetof(struct bpf_sockopt, optval):
    1548                 :          0 :                 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval);
    1549                 :          0 :                 break;
    1550                 :            :         case offsetof(struct bpf_sockopt, optval_end):
    1551                 :          0 :                 *insn++ = CG_SOCKOPT_ACCESS_FIELD(BPF_LDX_MEM, optval_end);
    1552                 :          0 :                 break;
    1553                 :            :         }
    1554                 :            : 
    1555                 :          0 :         return insn - insn_buf;
    1556                 :            : }
    1557                 :            : 
    1558                 :          0 : static int cg_sockopt_get_prologue(struct bpf_insn *insn_buf,
    1559                 :            :                                    bool direct_write,
    1560                 :            :                                    const struct bpf_prog *prog)
    1561                 :            : {
    1562                 :            :         /* Nothing to do for sockopt argument. The data is kzalloc'ated.
    1563                 :            :          */
    1564                 :          0 :         return 0;
    1565                 :            : }
    1566                 :            : 
    1567                 :            : const struct bpf_verifier_ops cg_sockopt_verifier_ops = {
    1568                 :            :         .get_func_proto         = cg_sockopt_func_proto,
    1569                 :            :         .is_valid_access        = cg_sockopt_is_valid_access,
    1570                 :            :         .convert_ctx_access     = cg_sockopt_convert_ctx_access,
    1571                 :            :         .gen_prologue           = cg_sockopt_get_prologue,
    1572                 :            : };
    1573                 :            : 
    1574                 :            : const struct bpf_prog_ops cg_sockopt_prog_ops = {
    1575                 :            : };

Generated by: LCOV version 1.14