LCOV - code coverage report
Current view: top level - kernel/bpf - syscall.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 349 1126 31.0 %
Date: 2020-09-30 20:25:40 Functions: 41 92 44.6 %
Branches: 145 920 15.8 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
       3                 :            :  */
       4                 :            : #include <linux/bpf.h>
       5                 :            : #include <linux/bpf_trace.h>
       6                 :            : #include <linux/bpf_lirc.h>
       7                 :            : #include <linux/btf.h>
       8                 :            : #include <linux/syscalls.h>
       9                 :            : #include <linux/slab.h>
      10                 :            : #include <linux/sched/signal.h>
      11                 :            : #include <linux/vmalloc.h>
      12                 :            : #include <linux/mmzone.h>
      13                 :            : #include <linux/anon_inodes.h>
      14                 :            : #include <linux/fdtable.h>
      15                 :            : #include <linux/file.h>
      16                 :            : #include <linux/fs.h>
      17                 :            : #include <linux/license.h>
      18                 :            : #include <linux/filter.h>
      19                 :            : #include <linux/version.h>
      20                 :            : #include <linux/kernel.h>
      21                 :            : #include <linux/idr.h>
      22                 :            : #include <linux/cred.h>
      23                 :            : #include <linux/timekeeping.h>
      24                 :            : #include <linux/ctype.h>
      25                 :            : #include <linux/nospec.h>
      26                 :            : 
      27                 :            : #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
      28                 :            :                            (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
      29                 :            :                            (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
      30                 :            :                            (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
      31                 :            : #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
      32                 :            : #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
      33                 :            : 
      34                 :            : #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
      35                 :            : 
      36                 :            : DEFINE_PER_CPU(int, bpf_prog_active);
      37                 :            : static DEFINE_IDR(prog_idr);
      38                 :            : static DEFINE_SPINLOCK(prog_idr_lock);
      39                 :            : static DEFINE_IDR(map_idr);
      40                 :            : static DEFINE_SPINLOCK(map_idr_lock);
      41                 :            : 
      42                 :            : int sysctl_unprivileged_bpf_disabled __read_mostly;
      43                 :            : 
      44                 :            : static const struct bpf_map_ops * const bpf_map_types[] = {
      45                 :            : #define BPF_PROG_TYPE(_id, _ops)
      46                 :            : #define BPF_MAP_TYPE(_id, _ops) \
      47                 :            :         [_id] = &_ops,
      48                 :            : #include <linux/bpf_types.h>
      49                 :            : #undef BPF_PROG_TYPE
      50                 :            : #undef BPF_MAP_TYPE
      51                 :            : };
      52                 :            : 
      53                 :            : /*
      54                 :            :  * If we're handed a bigger struct than we know of, ensure all the unknown bits
      55                 :            :  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
      56                 :            :  * we don't know about yet.
      57                 :            :  *
      58                 :            :  * There is a ToCToU between this function call and the following
      59                 :            :  * copy_from_user() call. However, this is not a concern since this function is
      60                 :            :  * meant to be a future-proofing of bits.
      61                 :            :  */
      62                 :       6003 : int bpf_check_uarg_tail_zero(void __user *uaddr,
      63                 :            :                              size_t expected_size,
      64                 :            :                              size_t actual_size)
      65                 :            : {
      66                 :            :         unsigned char __user *addr;
      67                 :            :         unsigned char __user *end;
      68                 :            :         unsigned char val;
      69                 :            :         int err;
      70                 :            : 
      71         [ +  - ]:       6003 :         if (unlikely(actual_size > PAGE_SIZE))       /* silly large */
      72                 :            :                 return -E2BIG;
      73                 :            : 
      74         [ +  - ]:       6003 :         if (unlikely(!access_ok(uaddr, actual_size)))
      75                 :            :                 return -EFAULT;
      76                 :            : 
      77         [ -  + ]:       6003 :         if (actual_size <= expected_size)
      78                 :            :                 return 0;
      79                 :            : 
      80                 :          0 :         addr = uaddr + expected_size;
      81                 :          0 :         end  = uaddr + actual_size;
      82                 :            : 
      83         [ #  # ]:          0 :         for (; addr < end; addr++) {
      84                 :          0 :                 err = get_user(val, addr);
      85         [ #  # ]:          0 :                 if (err)
      86                 :          0 :                         return err;
      87         [ #  # ]:          0 :                 if (val)
      88                 :            :                         return -E2BIG;
      89                 :            :         }
      90                 :            : 
      91                 :            :         return 0;
      92                 :            : }
      93                 :            : 
      94                 :            : const struct bpf_map_ops bpf_map_offload_ops = {
      95                 :            :         .map_alloc = bpf_map_offload_map_alloc,
      96                 :            :         .map_free = bpf_map_offload_map_free,
      97                 :            :         .map_check_btf = map_check_no_btf,
      98                 :            : };
      99                 :            : 
     100                 :       1449 : static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
     101                 :            : {
     102                 :            :         const struct bpf_map_ops *ops;
     103                 :       1449 :         u32 type = attr->map_type;
     104                 :            :         struct bpf_map *map;
     105                 :            :         int err;
     106                 :            : 
     107         [ +  - ]:       1449 :         if (type >= ARRAY_SIZE(bpf_map_types))
     108                 :            :                 return ERR_PTR(-EINVAL);
     109                 :       1449 :         type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
     110                 :       1449 :         ops = bpf_map_types[type];
     111         [ +  - ]:       1449 :         if (!ops)
     112                 :            :                 return ERR_PTR(-EINVAL);
     113                 :            : 
     114         [ -  + ]:       1449 :         if (ops->map_alloc_check) {
     115                 :          0 :                 err = ops->map_alloc_check(attr);
     116         [ #  # ]:          0 :                 if (err)
     117                 :          0 :                         return ERR_PTR(err);
     118                 :            :         }
     119         [ -  + ]:       1449 :         if (attr->map_ifindex)
     120                 :            :                 ops = &bpf_map_offload_ops;
     121                 :       1449 :         map = ops->map_alloc(attr);
     122         [ +  - ]:       1449 :         if (IS_ERR(map))
     123                 :            :                 return map;
     124                 :       1449 :         map->ops = ops;
     125                 :       1449 :         map->map_type = type;
     126                 :       1449 :         return map;
     127                 :            : }
     128                 :            : 
     129                 :          0 : void *bpf_map_area_alloc(u64 size, int numa_node)
     130                 :            : {
     131                 :            :         /* We really just want to fail instead of triggering OOM killer
     132                 :            :          * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
     133                 :            :          * which is used for lower order allocation requests.
     134                 :            :          *
     135                 :            :          * It has been observed that higher order allocation requests done by
     136                 :            :          * vmalloc with __GFP_NORETRY being set might fail due to not trying
     137                 :            :          * to reclaim memory from the page cache, thus we set
     138                 :            :          * __GFP_RETRY_MAYFAIL to avoid such situations.
     139                 :            :          */
     140                 :            : 
     141                 :            :         const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
     142                 :            :         void *area;
     143                 :            : 
     144         [ #  # ]:          0 :         if (size >= SIZE_MAX)
     145                 :            :                 return NULL;
     146                 :            : 
     147         [ #  # ]:          0 :         if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
     148                 :          0 :                 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
     149                 :            :                                     numa_node);
     150         [ #  # ]:          0 :                 if (area != NULL)
     151                 :            :                         return area;
     152                 :            :         }
     153                 :            : 
     154                 :          0 :         return __vmalloc_node_flags_caller(size, numa_node,
     155                 :            :                                            GFP_KERNEL | __GFP_RETRY_MAYFAIL |
     156                 :            :                                            flags, __builtin_return_address(0));
     157                 :            : }
     158                 :            : 
     159                 :          0 : void bpf_map_area_free(void *area)
     160                 :            : {
     161                 :          0 :         kvfree(area);
     162                 :          0 : }
     163                 :            : 
     164                 :            : static u32 bpf_map_flags_retain_permanent(u32 flags)
     165                 :            : {
     166                 :            :         /* Some map creation flags are not tied to the map object but
     167                 :            :          * rather to the map fd instead, so they have no meaning upon
     168                 :            :          * map object inspection since multiple file descriptors with
     169                 :            :          * different (access) properties can exist here. Thus, given
     170                 :            :          * this has zero meaning for the map itself, lets clear these
     171                 :            :          * from here.
     172                 :            :          */
     173                 :       1449 :         return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
     174                 :            : }
     175                 :            : 
     176                 :       1449 : void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
     177                 :            : {
     178                 :       1449 :         map->map_type = attr->map_type;
     179                 :       1449 :         map->key_size = attr->key_size;
     180                 :       1449 :         map->value_size = attr->value_size;
     181                 :       1449 :         map->max_entries = attr->max_entries;
     182                 :       2898 :         map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
     183                 :       1449 :         map->numa_node = bpf_map_attr_numa_node(attr);
     184                 :       1449 : }
     185                 :            : 
     186                 :       1449 : static int bpf_charge_memlock(struct user_struct *user, u32 pages)
     187                 :            : {
     188                 :       1449 :         unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
     189                 :            : 
     190         [ -  + ]:       2898 :         if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
     191                 :            :                 atomic_long_sub(pages, &user->locked_vm);
     192                 :          0 :                 return -EPERM;
     193                 :            :         }
     194                 :            :         return 0;
     195                 :            : }
     196                 :            : 
     197                 :        207 : static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
     198                 :            : {
     199         [ +  - ]:        207 :         if (user)
     200                 :        207 :                 atomic_long_sub(pages, &user->locked_vm);
     201                 :        207 : }
     202                 :            : 
     203                 :       1449 : int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
     204                 :            : {
     205                 :       1449 :         u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
     206                 :            :         struct user_struct *user;
     207                 :            :         int ret;
     208                 :            : 
     209         [ +  - ]:       1449 :         if (size >= U32_MAX - PAGE_SIZE)
     210                 :            :                 return -E2BIG;
     211                 :            : 
     212                 :       1449 :         user = get_current_user();
     213                 :       1449 :         ret = bpf_charge_memlock(user, pages);
     214         [ -  + ]:       1449 :         if (ret) {
     215                 :          0 :                 free_uid(user);
     216                 :          0 :                 return ret;
     217                 :            :         }
     218                 :            : 
     219                 :       1449 :         mem->pages = pages;
     220                 :       1449 :         mem->user = user;
     221                 :            : 
     222                 :       1449 :         return 0;
     223                 :            : }
     224                 :            : 
     225                 :        207 : void bpf_map_charge_finish(struct bpf_map_memory *mem)
     226                 :            : {
     227                 :        207 :         bpf_uncharge_memlock(mem->user, mem->pages);
     228                 :        207 :         free_uid(mem->user);
     229                 :        207 : }
     230                 :            : 
     231                 :          0 : void bpf_map_charge_move(struct bpf_map_memory *dst,
     232                 :            :                          struct bpf_map_memory *src)
     233                 :            : {
     234                 :        207 :         *dst = *src;
     235                 :            : 
     236                 :            :         /* Make sure src will not be used for the redundant uncharging. */
     237                 :        207 :         memset(src, 0, sizeof(struct bpf_map_memory));
     238                 :          0 : }
     239                 :            : 
     240                 :          0 : int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
     241                 :            : {
     242                 :            :         int ret;
     243                 :            : 
     244                 :          0 :         ret = bpf_charge_memlock(map->memory.user, pages);
     245         [ #  # ]:          0 :         if (ret)
     246                 :            :                 return ret;
     247                 :          0 :         map->memory.pages += pages;
     248                 :          0 :         return ret;
     249                 :            : }
     250                 :            : 
     251                 :          0 : void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
     252                 :            : {
     253                 :          0 :         bpf_uncharge_memlock(map->memory.user, pages);
     254                 :          0 :         map->memory.pages -= pages;
     255                 :          0 : }
     256                 :            : 
     257                 :       1449 : static int bpf_map_alloc_id(struct bpf_map *map)
     258                 :            : {
     259                 :            :         int id;
     260                 :            : 
     261                 :       1449 :         idr_preload(GFP_KERNEL);
     262                 :            :         spin_lock_bh(&map_idr_lock);
     263                 :       1449 :         id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
     264         [ +  - ]:       1449 :         if (id > 0)
     265                 :       1449 :                 map->id = id;
     266                 :            :         spin_unlock_bh(&map_idr_lock);
     267                 :            :         idr_preload_end();
     268                 :            : 
     269   [ -  +  #  #  :       1449 :         if (WARN_ON_ONCE(!id))
                   +  - ]
     270                 :            :                 return -ENOSPC;
     271                 :            : 
     272                 :       1449 :         return id > 0 ? 0 : id;
     273                 :            : }
     274                 :            : 
     275                 :        207 : void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
     276                 :            : {
     277                 :            :         unsigned long flags;
     278                 :            : 
     279                 :            :         /* Offloaded maps are removed from the IDR store when their device
     280                 :            :          * disappears - even if someone holds an fd to them they are unusable,
     281                 :            :          * the memory is gone, all ops will fail; they are simply waiting for
     282                 :            :          * refcnt to drop to be freed.
     283                 :            :          */
     284         [ +  - ]:        207 :         if (!map->id)
     285                 :        207 :                 return;
     286                 :            : 
     287         [ +  - ]:        207 :         if (do_idr_lock)
     288                 :        207 :                 spin_lock_irqsave(&map_idr_lock, flags);
     289                 :            :         else
     290                 :            :                 __acquire(&map_idr_lock);
     291                 :            : 
     292                 :        207 :         idr_remove(&map_idr, map->id);
     293                 :        207 :         map->id = 0;
     294                 :            : 
     295         [ +  - ]:        207 :         if (do_idr_lock)
     296                 :            :                 spin_unlock_irqrestore(&map_idr_lock, flags);
     297                 :            :         else
     298                 :            :                 __release(&map_idr_lock);
     299                 :            : }
     300                 :            : 
     301                 :            : /* called from workqueue */
     302                 :        207 : static void bpf_map_free_deferred(struct work_struct *work)
     303                 :            : {
     304                 :        207 :         struct bpf_map *map = container_of(work, struct bpf_map, work);
     305                 :            :         struct bpf_map_memory mem;
     306                 :            : 
     307                 :        207 :         bpf_map_charge_move(&mem, &map->memory);
     308                 :        207 :         security_bpf_map_free(map);
     309                 :            :         /* implementation dependent freeing */
     310                 :        207 :         map->ops->map_free(map);
     311                 :        207 :         bpf_map_charge_finish(&mem);
     312                 :        207 : }
     313                 :            : 
     314                 :        207 : static void bpf_map_put_uref(struct bpf_map *map)
     315                 :            : {
     316         [ +  - ]:        414 :         if (atomic_dec_and_test(&map->usercnt)) {
     317         [ -  + ]:        207 :                 if (map->ops->map_release_uref)
     318                 :          0 :                         map->ops->map_release_uref(map);
     319                 :            :         }
     320                 :        207 : }
     321                 :            : 
     322                 :            : /* decrement map refcnt and schedule it for freeing via workqueue
     323                 :            :  * (unrelying map implementation ops->map_free() might sleep)
     324                 :            :  */
     325                 :        207 : static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
     326                 :            : {
     327         [ +  - ]:        414 :         if (atomic_dec_and_test(&map->refcnt)) {
     328                 :            :                 /* bpf_map_free_id() must be called first */
     329                 :        207 :                 bpf_map_free_id(map, do_idr_lock);
     330                 :        207 :                 btf_put(map->btf);
     331                 :        414 :                 INIT_WORK(&map->work, bpf_map_free_deferred);
     332                 :        207 :                 schedule_work(&map->work);
     333                 :            :         }
     334                 :        207 : }
     335                 :            : 
     336                 :          0 : void bpf_map_put(struct bpf_map *map)
     337                 :            : {
     338                 :        207 :         __bpf_map_put(map, true);
     339                 :          0 : }
     340                 :            : EXPORT_SYMBOL_GPL(bpf_map_put);
     341                 :            : 
     342                 :          0 : void bpf_map_put_with_uref(struct bpf_map *map)
     343                 :            : {
     344                 :        207 :         bpf_map_put_uref(map);
     345                 :            :         bpf_map_put(map);
     346                 :          0 : }
     347                 :            : 
     348                 :        207 : static int bpf_map_release(struct inode *inode, struct file *filp)
     349                 :            : {
     350                 :        207 :         struct bpf_map *map = filp->private_data;
     351                 :            : 
     352         [ -  + ]:        207 :         if (map->ops->map_release)
     353                 :          0 :                 map->ops->map_release(map, filp);
     354                 :            : 
     355                 :            :         bpf_map_put_with_uref(map);
     356                 :        207 :         return 0;
     357                 :            : }
     358                 :            : 
     359                 :            : static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
     360                 :            : {
     361                 :       1242 :         fmode_t mode = f.file->f_mode;
     362                 :            : 
     363                 :            :         /* Our file permissions may have been overridden by global
     364                 :            :          * map permissions facing syscall side.
     365                 :            :          */
     366   [ #  #  #  #  :       1242 :         if (READ_ONCE(map->frozen))
          #  #  #  #  -  
                +  #  # ]
     367                 :          0 :                 mode &= ~FMODE_CAN_WRITE;
     368                 :            :         return mode;
     369                 :            : }
     370                 :            : 
     371                 :            : #ifdef CONFIG_PROC_FS
     372                 :          0 : static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
     373                 :            : {
     374                 :          0 :         const struct bpf_map *map = filp->private_data;
     375                 :            :         const struct bpf_array *array;
     376                 :            :         u32 owner_prog_type = 0;
     377                 :            :         u32 owner_jited = 0;
     378                 :            : 
     379         [ #  # ]:          0 :         if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
     380                 :            :                 array = container_of(map, struct bpf_array, map);
     381                 :          0 :                 owner_prog_type = array->owner_prog_type;
     382                 :          0 :                 owner_jited = array->owner_jited;
     383                 :            :         }
     384                 :            : 
     385                 :          0 :         seq_printf(m,
     386                 :            :                    "map_type:\t%u\n"
     387                 :            :                    "key_size:\t%u\n"
     388                 :            :                    "value_size:\t%u\n"
     389                 :            :                    "max_entries:\t%u\n"
     390                 :            :                    "map_flags:\t%#x\n"
     391                 :            :                    "memlock:\t%llu\n"
     392                 :            :                    "map_id:\t%u\n"
     393                 :            :                    "frozen:\t%u\n",
     394                 :            :                    map->map_type,
     395                 :            :                    map->key_size,
     396                 :            :                    map->value_size,
     397                 :            :                    map->max_entries,
     398                 :            :                    map->map_flags,
     399                 :          0 :                    map->memory.pages * 1ULL << PAGE_SHIFT,
     400                 :            :                    map->id,
     401                 :          0 :                    READ_ONCE(map->frozen));
     402                 :            : 
     403         [ #  # ]:          0 :         if (owner_prog_type) {
     404                 :          0 :                 seq_printf(m, "owner_prog_type:\t%u\n",
     405                 :            :                            owner_prog_type);
     406                 :          0 :                 seq_printf(m, "owner_jited:\t%u\n",
     407                 :            :                            owner_jited);
     408                 :            :         }
     409                 :          0 : }
     410                 :            : #endif
     411                 :            : 
     412                 :          0 : static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
     413                 :            :                               loff_t *ppos)
     414                 :            : {
     415                 :            :         /* We need this handler such that alloc_file() enables
     416                 :            :          * f_mode with FMODE_CAN_READ.
     417                 :            :          */
     418                 :          0 :         return -EINVAL;
     419                 :            : }
     420                 :            : 
     421                 :          0 : static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
     422                 :            :                                size_t siz, loff_t *ppos)
     423                 :            : {
     424                 :            :         /* We need this handler such that alloc_file() enables
     425                 :            :          * f_mode with FMODE_CAN_WRITE.
     426                 :            :          */
     427                 :          0 :         return -EINVAL;
     428                 :            : }
     429                 :            : 
     430                 :            : const struct file_operations bpf_map_fops = {
     431                 :            : #ifdef CONFIG_PROC_FS
     432                 :            :         .show_fdinfo    = bpf_map_show_fdinfo,
     433                 :            : #endif
     434                 :            :         .release        = bpf_map_release,
     435                 :            :         .read           = bpf_dummy_read,
     436                 :            :         .write          = bpf_dummy_write,
     437                 :            : };
     438                 :            : 
     439                 :       1449 : int bpf_map_new_fd(struct bpf_map *map, int flags)
     440                 :            : {
     441                 :            :         int ret;
     442                 :            : 
     443                 :       1449 :         ret = security_bpf_map(map, OPEN_FMODE(flags));
     444         [ +  - ]:       1449 :         if (ret < 0)
     445                 :            :                 return ret;
     446                 :            : 
     447                 :       1449 :         return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
     448                 :            :                                 flags | O_CLOEXEC);
     449                 :            : }
     450                 :            : 
     451                 :          0 : int bpf_get_file_flag(int flags)
     452                 :            : {
     453   [ #  #  +  -  :       1449 :         if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
                   #  # ]
     454                 :            :                 return -EINVAL;
     455   [ #  #  +  -  :       1449 :         if (flags & BPF_F_RDONLY)
                   #  # ]
     456                 :            :                 return O_RDONLY;
     457   [ #  #  +  -  :       1449 :         if (flags & BPF_F_WRONLY)
                   #  # ]
     458                 :            :                 return O_WRONLY;
     459                 :          0 :         return O_RDWR;
     460                 :            : }
     461                 :            : 
     462                 :            : /* helper macro to check that unused fields 'union bpf_attr' are zero */
     463                 :            : #define CHECK_ATTR(CMD) \
     464                 :            :         memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
     465                 :            :                    sizeof(attr->CMD##_LAST_FIELD), 0, \
     466                 :            :                    sizeof(*attr) - \
     467                 :            :                    offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
     468                 :            :                    sizeof(attr->CMD##_LAST_FIELD)) != NULL
     469                 :            : 
     470                 :            : /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
     471                 :            :  * Return 0 on success and < 0 on error.
     472                 :            :  */
     473                 :       3105 : static int bpf_obj_name_cpy(char *dst, const char *src)
     474                 :            : {
     475                 :       3105 :         const char *end = src + BPF_OBJ_NAME_LEN;
     476                 :            : 
     477                 :       3105 :         memset(dst, 0, BPF_OBJ_NAME_LEN);
     478                 :            :         /* Copy all isalnum(), '_' and '.' chars. */
     479   [ +  -  -  + ]:       6210 :         while (src < end && *src) {
     480   [ #  #  #  # ]:          0 :                 if (!isalnum(*src) &&
     481         [ #  # ]:          0 :                     *src != '_' && *src != '.')
     482                 :            :                         return -EINVAL;
     483                 :          0 :                 *dst++ = *src++;
     484                 :            :         }
     485                 :            : 
     486                 :            :         /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
     487         [ +  - ]:       3105 :         if (src == end)
     488                 :            :                 return -EINVAL;
     489                 :            : 
     490                 :       3105 :         return 0;
     491                 :            : }
     492                 :            : 
     493                 :          0 : int map_check_no_btf(const struct bpf_map *map,
     494                 :            :                      const struct btf *btf,
     495                 :            :                      const struct btf_type *key_type,
     496                 :            :                      const struct btf_type *value_type)
     497                 :            : {
     498                 :          0 :         return -ENOTSUPP;
     499                 :            : }
     500                 :            : 
     501                 :          0 : static int map_check_btf(struct bpf_map *map, const struct btf *btf,
     502                 :            :                          u32 btf_key_id, u32 btf_value_id)
     503                 :            : {
     504                 :            :         const struct btf_type *key_type, *value_type;
     505                 :            :         u32 key_size, value_size;
     506                 :            :         int ret = 0;
     507                 :            : 
     508                 :            :         /* Some maps allow key to be unspecified. */
     509         [ #  # ]:          0 :         if (btf_key_id) {
     510                 :          0 :                 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
     511   [ #  #  #  # ]:          0 :                 if (!key_type || key_size != map->key_size)
     512                 :            :                         return -EINVAL;
     513                 :            :         } else {
     514                 :          0 :                 key_type = btf_type_by_id(btf, 0);
     515         [ #  # ]:          0 :                 if (!map->ops->map_check_btf)
     516                 :            :                         return -EINVAL;
     517                 :            :         }
     518                 :            : 
     519                 :          0 :         value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
     520   [ #  #  #  # ]:          0 :         if (!value_type || value_size != map->value_size)
     521                 :            :                 return -EINVAL;
     522                 :            : 
     523                 :          0 :         map->spin_lock_off = btf_find_spin_lock(btf, value_type);
     524                 :            : 
     525         [ #  # ]:          0 :         if (map_value_has_spin_lock(map)) {
     526         [ #  # ]:          0 :                 if (map->map_flags & BPF_F_RDONLY_PROG)
     527                 :            :                         return -EACCES;
     528         [ #  # ]:          0 :                 if (map->map_type != BPF_MAP_TYPE_HASH &&
     529                 :          0 :                     map->map_type != BPF_MAP_TYPE_ARRAY &&
     530         [ #  # ]:          0 :                     map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
     531                 :            :                     map->map_type != BPF_MAP_TYPE_SK_STORAGE)
     532                 :            :                         return -ENOTSUPP;
     533         [ #  # ]:          0 :                 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
     534                 :          0 :                     map->value_size) {
     535         [ #  # ]:          0 :                         WARN_ONCE(1,
     536                 :            :                                   "verifier bug spin_lock_off %d value_size %d\n",
     537                 :            :                                   map->spin_lock_off, map->value_size);
     538                 :            :                         return -EFAULT;
     539                 :            :                 }
     540                 :            :         }
     541                 :            : 
     542         [ #  # ]:          0 :         if (map->ops->map_check_btf)
     543                 :          0 :                 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
     544                 :            : 
     545                 :          0 :         return ret;
     546                 :            : }
     547                 :            : 
     548                 :            : #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
     549                 :            : /* called via syscall */
     550                 :       1449 : static int map_create(union bpf_attr *attr)
     551                 :            : {
     552                 :            :         int numa_node = bpf_map_attr_numa_node(attr);
     553                 :            :         struct bpf_map_memory mem;
     554                 :            :         struct bpf_map *map;
     555                 :            :         int f_flags;
     556                 :            :         int err;
     557                 :            : 
     558                 :       1449 :         err = CHECK_ATTR(BPF_MAP_CREATE);
     559         [ +  - ]:       1449 :         if (err)
     560                 :            :                 return -EINVAL;
     561                 :            : 
     562                 :       1449 :         f_flags = bpf_get_file_flag(attr->map_flags);
     563         [ +  - ]:       1449 :         if (f_flags < 0)
     564                 :            :                 return f_flags;
     565                 :            : 
     566   [ -  +  #  # ]:       1449 :         if (numa_node != NUMA_NO_NODE &&
     567         [ #  # ]:          0 :             ((unsigned int)numa_node >= nr_node_ids ||
     568                 :            :              !node_online(numa_node)))
     569                 :            :                 return -EINVAL;
     570                 :            : 
     571                 :            :         /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
     572                 :       1449 :         map = find_and_alloc_map(attr);
     573         [ -  + ]:       1449 :         if (IS_ERR(map))
     574                 :          0 :                 return PTR_ERR(map);
     575                 :            : 
     576                 :       1449 :         err = bpf_obj_name_cpy(map->name, attr->map_name);
     577         [ +  - ]:       1449 :         if (err)
     578                 :            :                 goto free_map;
     579                 :            : 
     580                 :            :         atomic_set(&map->refcnt, 1);
     581                 :            :         atomic_set(&map->usercnt, 1);
     582                 :            : 
     583   [ +  -  -  + ]:       1449 :         if (attr->btf_key_type_id || attr->btf_value_type_id) {
     584                 :            :                 struct btf *btf;
     585                 :            : 
     586         [ #  # ]:          0 :                 if (!attr->btf_value_type_id) {
     587                 :            :                         err = -EINVAL;
     588                 :            :                         goto free_map;
     589                 :            :                 }
     590                 :            : 
     591                 :          0 :                 btf = btf_get_by_fd(attr->btf_fd);
     592         [ #  # ]:          0 :                 if (IS_ERR(btf)) {
     593                 :            :                         err = PTR_ERR(btf);
     594                 :          0 :                         goto free_map;
     595                 :            :                 }
     596                 :            : 
     597                 :          0 :                 err = map_check_btf(map, btf, attr->btf_key_type_id,
     598                 :            :                                     attr->btf_value_type_id);
     599         [ #  # ]:          0 :                 if (err) {
     600                 :          0 :                         btf_put(btf);
     601                 :          0 :                         goto free_map;
     602                 :            :                 }
     603                 :            : 
     604                 :          0 :                 map->btf = btf;
     605                 :          0 :                 map->btf_key_type_id = attr->btf_key_type_id;
     606                 :          0 :                 map->btf_value_type_id = attr->btf_value_type_id;
     607                 :            :         } else {
     608                 :       1449 :                 map->spin_lock_off = -EINVAL;
     609                 :            :         }
     610                 :            : 
     611                 :       1449 :         err = security_bpf_map_alloc(map);
     612         [ +  - ]:       1449 :         if (err)
     613                 :            :                 goto free_map;
     614                 :            : 
     615                 :       1449 :         err = bpf_map_alloc_id(map);
     616         [ +  - ]:       1449 :         if (err)
     617                 :            :                 goto free_map_sec;
     618                 :            : 
     619                 :       1449 :         err = bpf_map_new_fd(map, f_flags);
     620         [ -  + ]:       1449 :         if (err < 0) {
     621                 :            :                 /* failed to allocate fd.
     622                 :            :                  * bpf_map_put_with_uref() is needed because the above
     623                 :            :                  * bpf_map_alloc_id() has published the map
     624                 :            :                  * to the userspace and the userspace may
     625                 :            :                  * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
     626                 :            :                  */
     627                 :            :                 bpf_map_put_with_uref(map);
     628                 :          0 :                 return err;
     629                 :            :         }
     630                 :            : 
     631                 :            :         return err;
     632                 :            : 
     633                 :            : free_map_sec:
     634                 :          0 :         security_bpf_map_free(map);
     635                 :            : free_map:
     636                 :          0 :         btf_put(map->btf);
     637                 :          0 :         bpf_map_charge_move(&mem, &map->memory);
     638                 :          0 :         map->ops->map_free(map);
     639                 :          0 :         bpf_map_charge_finish(&mem);
     640                 :          0 :         return err;
     641                 :            : }
     642                 :            : 
     643                 :            : /* if error is returned, fd is released.
     644                 :            :  * On success caller should complete fd access with matching fdput()
     645                 :            :  */
     646                 :       3726 : struct bpf_map *__bpf_map_get(struct fd f)
     647                 :            : {
     648         [ +  - ]:       3726 :         if (!f.file)
     649                 :            :                 return ERR_PTR(-EBADF);
     650         [ -  + ]:       3726 :         if (f.file->f_op != &bpf_map_fops) {
     651                 :            :                 fdput(f);
     652                 :            :                 return ERR_PTR(-EINVAL);
     653                 :            :         }
     654                 :            : 
     655                 :       3726 :         return f.file->private_data;
     656                 :            : }
     657                 :            : 
     658                 :            : /* prog's and map's refcnt limit */
     659                 :            : #define BPF_MAX_REFCNT 32768
     660                 :            : 
     661                 :       2484 : struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
     662                 :            : {
     663         [ -  + ]:       2484 :         if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
     664                 :            :                 atomic_dec(&map->refcnt);
     665                 :          0 :                 return ERR_PTR(-EBUSY);
     666                 :            :         }
     667         [ -  + ]:       2484 :         if (uref)
     668                 :          0 :                 atomic_inc(&map->usercnt);
     669                 :       2484 :         return map;
     670                 :            : }
     671                 :            : EXPORT_SYMBOL_GPL(bpf_map_inc);
     672                 :            : 
     673                 :          0 : struct bpf_map *bpf_map_get_with_uref(u32 ufd)
     674                 :            : {
     675                 :            :         struct fd f = fdget(ufd);
     676                 :            :         struct bpf_map *map;
     677                 :            : 
     678                 :          0 :         map = __bpf_map_get(f);
     679         [ #  # ]:          0 :         if (IS_ERR(map))
     680                 :            :                 return map;
     681                 :            : 
     682                 :          0 :         map = bpf_map_inc(map, true);
     683                 :            :         fdput(f);
     684                 :            : 
     685                 :          0 :         return map;
     686                 :            : }
     687                 :            : 
     688                 :            : /* map_idr_lock should have been held */
     689                 :          0 : static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map,
     690                 :            :                                               bool uref)
     691                 :            : {
     692                 :            :         int refold;
     693                 :            : 
     694                 :          0 :         refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
     695                 :            : 
     696         [ #  # ]:          0 :         if (refold >= BPF_MAX_REFCNT) {
     697                 :          0 :                 __bpf_map_put(map, false);
     698                 :          0 :                 return ERR_PTR(-EBUSY);
     699                 :            :         }
     700                 :            : 
     701         [ #  # ]:          0 :         if (!refold)
     702                 :            :                 return ERR_PTR(-ENOENT);
     703                 :            : 
     704         [ #  # ]:          0 :         if (uref)
     705                 :          0 :                 atomic_inc(&map->usercnt);
     706                 :            : 
     707                 :          0 :         return map;
     708                 :            : }
     709                 :            : 
     710                 :          0 : struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
     711                 :            : {
     712                 :            :         spin_lock_bh(&map_idr_lock);
     713                 :          0 :         map = __bpf_map_inc_not_zero(map, uref);
     714                 :            :         spin_unlock_bh(&map_idr_lock);
     715                 :            : 
     716                 :          0 :         return map;
     717                 :            : }
     718                 :            : EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
     719                 :            : 
     720                 :          0 : int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
     721                 :            : {
     722                 :          0 :         return -ENOTSUPP;
     723                 :            : }
     724                 :            : 
     725                 :            : static void *__bpf_copy_key(void __user *ukey, u64 key_size)
     726                 :            : {
     727   [ #  #  #  #  :       1242 :         if (key_size)
          #  #  +  -  #  
                      # ]
     728                 :       1242 :                 return memdup_user(ukey, key_size);
     729                 :            : 
     730   [ #  #  #  #  :          0 :         if (ukey)
          #  #  #  #  #  
                      # ]
     731                 :            :                 return ERR_PTR(-EINVAL);
     732                 :            : 
     733                 :            :         return NULL;
     734                 :            : }
     735                 :            : 
     736                 :            : /* last field in 'union bpf_attr' used by this command */
     737                 :            : #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
     738                 :            : 
     739                 :          0 : static int map_lookup_elem(union bpf_attr *attr)
     740                 :            : {
     741                 :          0 :         void __user *ukey = u64_to_user_ptr(attr->key);
     742                 :          0 :         void __user *uvalue = u64_to_user_ptr(attr->value);
     743                 :          0 :         int ufd = attr->map_fd;
     744                 :            :         struct bpf_map *map;
     745                 :            :         void *key, *value, *ptr;
     746                 :            :         u32 value_size;
     747                 :            :         struct fd f;
     748                 :            :         int err;
     749                 :            : 
     750         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
     751                 :            :                 return -EINVAL;
     752                 :            : 
     753         [ #  # ]:          0 :         if (attr->flags & ~BPF_F_LOCK)
     754                 :            :                 return -EINVAL;
     755                 :            : 
     756                 :            :         f = fdget(ufd);
     757                 :          0 :         map = __bpf_map_get(f);
     758         [ #  # ]:          0 :         if (IS_ERR(map))
     759                 :          0 :                 return PTR_ERR(map);
     760         [ #  # ]:          0 :         if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
     761                 :            :                 err = -EPERM;
     762                 :            :                 goto err_put;
     763                 :            :         }
     764                 :            : 
     765   [ #  #  #  # ]:          0 :         if ((attr->flags & BPF_F_LOCK) &&
     766                 :            :             !map_value_has_spin_lock(map)) {
     767                 :            :                 err = -EINVAL;
     768                 :            :                 goto err_put;
     769                 :            :         }
     770                 :            : 
     771                 :          0 :         key = __bpf_copy_key(ukey, map->key_size);
     772         [ #  # ]:          0 :         if (IS_ERR(key)) {
     773                 :            :                 err = PTR_ERR(key);
     774                 :          0 :                 goto err_put;
     775                 :            :         }
     776                 :            : 
     777         [ #  # ]:          0 :         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
     778         [ #  # ]:          0 :             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
     779         [ #  # ]:          0 :             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
     780                 :            :             map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
     781                 :          0 :                 value_size = round_up(map->value_size, 8) * num_possible_cpus();
     782   [ #  #  #  #  :          0 :         else if (IS_FD_MAP(map))
                   #  # ]
     783                 :            :                 value_size = sizeof(u32);
     784                 :            :         else
     785                 :          0 :                 value_size = map->value_size;
     786                 :            : 
     787                 :            :         err = -ENOMEM;
     788                 :            :         value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
     789         [ #  # ]:          0 :         if (!value)
     790                 :            :                 goto free_key;
     791                 :            : 
     792         [ #  # ]:          0 :         if (bpf_map_is_dev_bound(map)) {
     793                 :          0 :                 err = bpf_map_offload_lookup_elem(map, key, value);
     794                 :          0 :                 goto done;
     795                 :            :         }
     796                 :            : 
     797                 :          0 :         preempt_disable();
     798                 :          0 :         this_cpu_inc(bpf_prog_active);
     799         [ #  # ]:          0 :         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
     800                 :            :             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
     801                 :          0 :                 err = bpf_percpu_hash_copy(map, key, value);
     802         [ #  # ]:          0 :         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
     803                 :          0 :                 err = bpf_percpu_array_copy(map, key, value);
     804         [ #  # ]:          0 :         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
     805                 :          0 :                 err = bpf_percpu_cgroup_storage_copy(map, key, value);
     806         [ #  # ]:          0 :         } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
     807                 :          0 :                 err = bpf_stackmap_copy(map, key, value);
     808   [ #  #  #  # ]:          0 :         } else if (IS_FD_ARRAY(map)) {
     809                 :          0 :                 err = bpf_fd_array_map_lookup_elem(map, key, value);
     810         [ #  # ]:          0 :         } else if (IS_FD_HASH(map)) {
     811                 :          0 :                 err = bpf_fd_htab_map_lookup_elem(map, key, value);
     812         [ #  # ]:          0 :         } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
     813                 :          0 :                 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
     814         [ #  # ]:          0 :         } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
     815                 :            :                    map->map_type == BPF_MAP_TYPE_STACK) {
     816                 :          0 :                 err = map->ops->map_peek_elem(map, value);
     817                 :            :         } else {
     818                 :            :                 rcu_read_lock();
     819         [ #  # ]:          0 :                 if (map->ops->map_lookup_elem_sys_only)
     820                 :          0 :                         ptr = map->ops->map_lookup_elem_sys_only(map, key);
     821                 :            :                 else
     822                 :          0 :                         ptr = map->ops->map_lookup_elem(map, key);
     823         [ #  # ]:          0 :                 if (IS_ERR(ptr)) {
     824                 :            :                         err = PTR_ERR(ptr);
     825         [ #  # ]:          0 :                 } else if (!ptr) {
     826                 :            :                         err = -ENOENT;
     827                 :            :                 } else {
     828                 :            :                         err = 0;
     829         [ #  # ]:          0 :                         if (attr->flags & BPF_F_LOCK)
     830                 :            :                                 /* lock 'ptr' and copy everything but lock */
     831                 :          0 :                                 copy_map_value_locked(map, value, ptr, true);
     832                 :            :                         else
     833                 :          0 :                                 copy_map_value(map, value, ptr);
     834                 :            :                         /* mask lock, since value wasn't zero inited */
     835                 :            :                         check_and_init_map_lock(map, value);
     836                 :            :                 }
     837                 :            :                 rcu_read_unlock();
     838                 :            :         }
     839                 :          0 :         this_cpu_dec(bpf_prog_active);
     840                 :          0 :         preempt_enable();
     841                 :            : 
     842                 :            : done:
     843         [ #  # ]:          0 :         if (err)
     844                 :            :                 goto free_value;
     845                 :            : 
     846                 :            :         err = -EFAULT;
     847         [ #  # ]:          0 :         if (copy_to_user(uvalue, value, value_size) != 0)
     848                 :            :                 goto free_value;
     849                 :            : 
     850                 :            :         err = 0;
     851                 :            : 
     852                 :            : free_value:
     853                 :          0 :         kfree(value);
     854                 :            : free_key:
     855                 :          0 :         kfree(key);
     856                 :            : err_put:
     857                 :            :         fdput(f);
     858                 :          0 :         return err;
     859                 :            : }
     860                 :            : 
     861                 :            : static void maybe_wait_bpf_programs(struct bpf_map *map)
     862                 :            : {
     863                 :            :         /* Wait for any running BPF programs to complete so that
     864                 :            :          * userspace, when we return to it, knows that all programs
     865                 :            :          * that could be running use the new map value.
     866                 :            :          */
     867   [ #  #  -  + ]:       1242 :         if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
     868                 :            :             map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
     869                 :          0 :                 synchronize_rcu();
     870                 :            : }
     871                 :            : 
     872                 :            : #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
     873                 :            : 
     874                 :       1242 : static int map_update_elem(union bpf_attr *attr)
     875                 :            : {
     876                 :       1242 :         void __user *ukey = u64_to_user_ptr(attr->key);
     877                 :       1242 :         void __user *uvalue = u64_to_user_ptr(attr->value);
     878                 :       1242 :         int ufd = attr->map_fd;
     879                 :            :         struct bpf_map *map;
     880                 :            :         void *key, *value;
     881                 :            :         u32 value_size;
     882                 :            :         struct fd f;
     883                 :            :         int err;
     884                 :            : 
     885         [ +  - ]:       1242 :         if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
     886                 :            :                 return -EINVAL;
     887                 :            : 
     888                 :            :         f = fdget(ufd);
     889                 :       1242 :         map = __bpf_map_get(f);
     890         [ -  + ]:       1242 :         if (IS_ERR(map))
     891                 :          0 :                 return PTR_ERR(map);
     892         [ +  - ]:       1242 :         if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
     893                 :            :                 err = -EPERM;
     894                 :            :                 goto err_put;
     895                 :            :         }
     896                 :            : 
     897   [ -  +  #  # ]:       1242 :         if ((attr->flags & BPF_F_LOCK) &&
     898                 :            :             !map_value_has_spin_lock(map)) {
     899                 :            :                 err = -EINVAL;
     900                 :            :                 goto err_put;
     901                 :            :         }
     902                 :            : 
     903                 :       1242 :         key = __bpf_copy_key(ukey, map->key_size);
     904         [ -  + ]:       1242 :         if (IS_ERR(key)) {
     905                 :            :                 err = PTR_ERR(key);
     906                 :          0 :                 goto err_put;
     907                 :            :         }
     908                 :            : 
     909         [ +  - ]:       1242 :         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
     910         [ +  - ]:       1242 :             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
     911         [ -  + ]:       1242 :             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
     912                 :            :             map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
     913                 :          0 :                 value_size = round_up(map->value_size, 8) * num_possible_cpus();
     914                 :            :         else
     915                 :       1242 :                 value_size = map->value_size;
     916                 :            : 
     917                 :            :         err = -ENOMEM;
     918                 :            :         value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
     919         [ +  - ]:       1242 :         if (!value)
     920                 :            :                 goto free_key;
     921                 :            : 
     922                 :            :         err = -EFAULT;
     923         [ +  - ]:       1242 :         if (copy_from_user(value, uvalue, value_size) != 0)
     924                 :            :                 goto free_value;
     925                 :            : 
     926                 :            :         /* Need to create a kthread, thus must support schedule */
     927         [ -  + ]:       1242 :         if (bpf_map_is_dev_bound(map)) {
     928                 :          0 :                 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
     929                 :          0 :                 goto out;
     930         [ +  - ]:       1242 :         } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
     931         [ -  + ]:       1242 :                    map->map_type == BPF_MAP_TYPE_SOCKHASH ||
     932                 :            :                    map->map_type == BPF_MAP_TYPE_SOCKMAP) {
     933                 :          0 :                 err = map->ops->map_update_elem(map, key, value, attr->flags);
     934                 :          0 :                 goto out;
     935                 :            :         }
     936                 :            : 
     937                 :            :         /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
     938                 :            :          * inside bpf map update or delete otherwise deadlocks are possible
     939                 :            :          */
     940                 :       1242 :         preempt_disable();
     941                 :       2484 :         __this_cpu_inc(bpf_prog_active);
     942         [ -  + ]:       1242 :         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
     943                 :            :             map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
     944                 :          0 :                 err = bpf_percpu_hash_update(map, key, value, attr->flags);
     945         [ -  + ]:       1242 :         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
     946                 :          0 :                 err = bpf_percpu_array_update(map, key, value, attr->flags);
     947         [ -  + ]:       1242 :         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
     948                 :          0 :                 err = bpf_percpu_cgroup_storage_update(map, key, value,
     949                 :            :                                                        attr->flags);
     950   [ +  -  -  + ]:       1242 :         } else if (IS_FD_ARRAY(map)) {
     951                 :            :                 rcu_read_lock();
     952                 :          0 :                 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
     953                 :            :                                                    attr->flags);
     954                 :            :                 rcu_read_unlock();
     955         [ -  + ]:       1242 :         } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
     956                 :            :                 rcu_read_lock();
     957                 :          0 :                 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
     958                 :            :                                                   attr->flags);
     959                 :            :                 rcu_read_unlock();
     960         [ -  + ]:       1242 :         } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
     961                 :            :                 /* rcu_read_lock() is not needed */
     962                 :          0 :                 err = bpf_fd_reuseport_array_update_elem(map, key, value,
     963                 :            :                                                          attr->flags);
     964         [ -  + ]:       1242 :         } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
     965                 :            :                    map->map_type == BPF_MAP_TYPE_STACK) {
     966                 :          0 :                 err = map->ops->map_push_elem(map, value, attr->flags);
     967                 :            :         } else {
     968                 :            :                 rcu_read_lock();
     969                 :       1242 :                 err = map->ops->map_update_elem(map, key, value, attr->flags);
     970                 :            :                 rcu_read_unlock();
     971                 :            :         }
     972                 :       2484 :         __this_cpu_dec(bpf_prog_active);
     973                 :       1242 :         preempt_enable();
     974                 :            :         maybe_wait_bpf_programs(map);
     975                 :            : out:
     976                 :            : free_value:
     977                 :       1242 :         kfree(value);
     978                 :            : free_key:
     979                 :       1242 :         kfree(key);
     980                 :            : err_put:
     981                 :            :         fdput(f);
     982                 :       1242 :         return err;
     983                 :            : }
     984                 :            : 
     985                 :            : #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
     986                 :            : 
     987                 :          0 : static int map_delete_elem(union bpf_attr *attr)
     988                 :            : {
     989                 :          0 :         void __user *ukey = u64_to_user_ptr(attr->key);
     990                 :          0 :         int ufd = attr->map_fd;
     991                 :            :         struct bpf_map *map;
     992                 :            :         struct fd f;
     993                 :            :         void *key;
     994                 :            :         int err;
     995                 :            : 
     996         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
     997                 :            :                 return -EINVAL;
     998                 :            : 
     999                 :            :         f = fdget(ufd);
    1000                 :          0 :         map = __bpf_map_get(f);
    1001         [ #  # ]:          0 :         if (IS_ERR(map))
    1002                 :          0 :                 return PTR_ERR(map);
    1003         [ #  # ]:          0 :         if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
    1004                 :            :                 err = -EPERM;
    1005                 :            :                 goto err_put;
    1006                 :            :         }
    1007                 :            : 
    1008                 :          0 :         key = __bpf_copy_key(ukey, map->key_size);
    1009         [ #  # ]:          0 :         if (IS_ERR(key)) {
    1010                 :            :                 err = PTR_ERR(key);
    1011                 :          0 :                 goto err_put;
    1012                 :            :         }
    1013                 :            : 
    1014         [ #  # ]:          0 :         if (bpf_map_is_dev_bound(map)) {
    1015                 :          0 :                 err = bpf_map_offload_delete_elem(map, key);
    1016                 :          0 :                 goto out;
    1017                 :            :         }
    1018                 :            : 
    1019                 :          0 :         preempt_disable();
    1020                 :          0 :         __this_cpu_inc(bpf_prog_active);
    1021                 :            :         rcu_read_lock();
    1022                 :          0 :         err = map->ops->map_delete_elem(map, key);
    1023                 :            :         rcu_read_unlock();
    1024                 :          0 :         __this_cpu_dec(bpf_prog_active);
    1025                 :          0 :         preempt_enable();
    1026                 :            :         maybe_wait_bpf_programs(map);
    1027                 :            : out:
    1028                 :          0 :         kfree(key);
    1029                 :            : err_put:
    1030                 :            :         fdput(f);
    1031                 :          0 :         return err;
    1032                 :            : }
    1033                 :            : 
    1034                 :            : /* last field in 'union bpf_attr' used by this command */
    1035                 :            : #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
    1036                 :            : 
    1037                 :          0 : static int map_get_next_key(union bpf_attr *attr)
    1038                 :            : {
    1039                 :          0 :         void __user *ukey = u64_to_user_ptr(attr->key);
    1040                 :          0 :         void __user *unext_key = u64_to_user_ptr(attr->next_key);
    1041                 :          0 :         int ufd = attr->map_fd;
    1042                 :            :         struct bpf_map *map;
    1043                 :            :         void *key, *next_key;
    1044                 :            :         struct fd f;
    1045                 :            :         int err;
    1046                 :            : 
    1047         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
    1048                 :            :                 return -EINVAL;
    1049                 :            : 
    1050                 :            :         f = fdget(ufd);
    1051                 :          0 :         map = __bpf_map_get(f);
    1052         [ #  # ]:          0 :         if (IS_ERR(map))
    1053                 :          0 :                 return PTR_ERR(map);
    1054         [ #  # ]:          0 :         if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
    1055                 :            :                 err = -EPERM;
    1056                 :            :                 goto err_put;
    1057                 :            :         }
    1058                 :            : 
    1059         [ #  # ]:          0 :         if (ukey) {
    1060                 :          0 :                 key = __bpf_copy_key(ukey, map->key_size);
    1061         [ #  # ]:          0 :                 if (IS_ERR(key)) {
    1062                 :            :                         err = PTR_ERR(key);
    1063                 :          0 :                         goto err_put;
    1064                 :            :                 }
    1065                 :            :         } else {
    1066                 :            :                 key = NULL;
    1067                 :            :         }
    1068                 :            : 
    1069                 :            :         err = -ENOMEM;
    1070                 :          0 :         next_key = kmalloc(map->key_size, GFP_USER);
    1071         [ #  # ]:          0 :         if (!next_key)
    1072                 :            :                 goto free_key;
    1073                 :            : 
    1074         [ #  # ]:          0 :         if (bpf_map_is_dev_bound(map)) {
    1075                 :          0 :                 err = bpf_map_offload_get_next_key(map, key, next_key);
    1076                 :          0 :                 goto out;
    1077                 :            :         }
    1078                 :            : 
    1079                 :            :         rcu_read_lock();
    1080                 :          0 :         err = map->ops->map_get_next_key(map, key, next_key);
    1081                 :            :         rcu_read_unlock();
    1082                 :            : out:
    1083         [ #  # ]:          0 :         if (err)
    1084                 :            :                 goto free_next_key;
    1085                 :            : 
    1086                 :            :         err = -EFAULT;
    1087         [ #  # ]:          0 :         if (copy_to_user(unext_key, next_key, map->key_size) != 0)
    1088                 :            :                 goto free_next_key;
    1089                 :            : 
    1090                 :            :         err = 0;
    1091                 :            : 
    1092                 :            : free_next_key:
    1093                 :          0 :         kfree(next_key);
    1094                 :            : free_key:
    1095                 :          0 :         kfree(key);
    1096                 :            : err_put:
    1097                 :            :         fdput(f);
    1098                 :          0 :         return err;
    1099                 :            : }
    1100                 :            : 
    1101                 :            : #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
    1102                 :            : 
    1103                 :          0 : static int map_lookup_and_delete_elem(union bpf_attr *attr)
    1104                 :            : {
    1105                 :          0 :         void __user *ukey = u64_to_user_ptr(attr->key);
    1106                 :          0 :         void __user *uvalue = u64_to_user_ptr(attr->value);
    1107                 :          0 :         int ufd = attr->map_fd;
    1108                 :            :         struct bpf_map *map;
    1109                 :            :         void *key, *value;
    1110                 :            :         u32 value_size;
    1111                 :            :         struct fd f;
    1112                 :            :         int err;
    1113                 :            : 
    1114         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
    1115                 :            :                 return -EINVAL;
    1116                 :            : 
    1117                 :            :         f = fdget(ufd);
    1118                 :          0 :         map = __bpf_map_get(f);
    1119         [ #  # ]:          0 :         if (IS_ERR(map))
    1120                 :          0 :                 return PTR_ERR(map);
    1121   [ #  #  #  # ]:          0 :         if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
    1122                 :          0 :             !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
    1123                 :            :                 err = -EPERM;
    1124                 :            :                 goto err_put;
    1125                 :            :         }
    1126                 :            : 
    1127                 :          0 :         key = __bpf_copy_key(ukey, map->key_size);
    1128         [ #  # ]:          0 :         if (IS_ERR(key)) {
    1129                 :            :                 err = PTR_ERR(key);
    1130                 :          0 :                 goto err_put;
    1131                 :            :         }
    1132                 :            : 
    1133                 :          0 :         value_size = map->value_size;
    1134                 :            : 
    1135                 :            :         err = -ENOMEM;
    1136                 :            :         value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
    1137         [ #  # ]:          0 :         if (!value)
    1138                 :            :                 goto free_key;
    1139                 :            : 
    1140         [ #  # ]:          0 :         if (map->map_type == BPF_MAP_TYPE_QUEUE ||
    1141                 :            :             map->map_type == BPF_MAP_TYPE_STACK) {
    1142                 :          0 :                 err = map->ops->map_pop_elem(map, value);
    1143                 :            :         } else {
    1144                 :            :                 err = -ENOTSUPP;
    1145                 :            :         }
    1146                 :            : 
    1147         [ #  # ]:          0 :         if (err)
    1148                 :            :                 goto free_value;
    1149                 :            : 
    1150         [ #  # ]:          0 :         if (copy_to_user(uvalue, value, value_size) != 0) {
    1151                 :            :                 err = -EFAULT;
    1152                 :            :                 goto free_value;
    1153                 :            :         }
    1154                 :            : 
    1155                 :            :         err = 0;
    1156                 :            : 
    1157                 :            : free_value:
    1158                 :          0 :         kfree(value);
    1159                 :            : free_key:
    1160                 :          0 :         kfree(key);
    1161                 :            : err_put:
    1162                 :            :         fdput(f);
    1163                 :          0 :         return err;
    1164                 :            : }
    1165                 :            : 
    1166                 :            : #define BPF_MAP_FREEZE_LAST_FIELD map_fd
    1167                 :            : 
    1168                 :          0 : static int map_freeze(const union bpf_attr *attr)
    1169                 :            : {
    1170                 :          0 :         int err = 0, ufd = attr->map_fd;
    1171                 :            :         struct bpf_map *map;
    1172                 :            :         struct fd f;
    1173                 :            : 
    1174         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_MAP_FREEZE))
    1175                 :            :                 return -EINVAL;
    1176                 :            : 
    1177                 :            :         f = fdget(ufd);
    1178                 :          0 :         map = __bpf_map_get(f);
    1179         [ #  # ]:          0 :         if (IS_ERR(map))
    1180                 :          0 :                 return PTR_ERR(map);
    1181         [ #  # ]:          0 :         if (READ_ONCE(map->frozen)) {
    1182                 :            :                 err = -EBUSY;
    1183                 :            :                 goto err_put;
    1184                 :            :         }
    1185         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN)) {
    1186                 :            :                 err = -EPERM;
    1187                 :            :                 goto err_put;
    1188                 :            :         }
    1189                 :            : 
    1190                 :            :         WRITE_ONCE(map->frozen, true);
    1191                 :            : err_put:
    1192                 :            :         fdput(f);
    1193                 :          0 :         return err;
    1194                 :            : }
    1195                 :            : 
    1196                 :            : static const struct bpf_prog_ops * const bpf_prog_types[] = {
    1197                 :            : #define BPF_PROG_TYPE(_id, _name) \
    1198                 :            :         [_id] = & _name ## _prog_ops,
    1199                 :            : #define BPF_MAP_TYPE(_id, _ops)
    1200                 :            : #include <linux/bpf_types.h>
    1201                 :            : #undef BPF_PROG_TYPE
    1202                 :            : #undef BPF_MAP_TYPE
    1203                 :            : };
    1204                 :            : 
    1205                 :            : static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
    1206                 :            : {
    1207                 :            :         const struct bpf_prog_ops *ops;
    1208                 :            : 
    1209         [ +  - ]:       1656 :         if (type >= ARRAY_SIZE(bpf_prog_types))
    1210                 :            :                 return -EINVAL;
    1211                 :       1656 :         type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
    1212                 :       1656 :         ops = bpf_prog_types[type];
    1213         [ +  - ]:       1656 :         if (!ops)
    1214                 :            :                 return -EINVAL;
    1215                 :            : 
    1216         [ +  - ]:       3312 :         if (!bpf_prog_is_dev_bound(prog->aux))
    1217                 :       1656 :                 prog->aux->ops = ops;
    1218                 :            :         else
    1219                 :          0 :                 prog->aux->ops = &bpf_offload_prog_ops;
    1220                 :       1656 :         prog->type = type;
    1221                 :            :         return 0;
    1222                 :            : }
    1223                 :            : 
    1224                 :            : /* drop refcnt on maps used by eBPF program and free auxilary data */
    1225                 :        414 : static void free_used_maps(struct bpf_prog_aux *aux)
    1226                 :            : {
    1227                 :            :         enum bpf_cgroup_storage_type stype;
    1228                 :            :         int i;
    1229                 :            : 
    1230         [ +  + ]:       1242 :         for_each_cgroup_storage_type(stype) {
    1231         [ +  - ]:        828 :                 if (!aux->cgroup_storage[stype])
    1232                 :        828 :                         continue;
    1233                 :          0 :                 bpf_cgroup_storage_release(aux->prog,
    1234                 :            :                                            aux->cgroup_storage[stype]);
    1235                 :            :         }
    1236                 :            : 
    1237         [ -  + ]:          0 :         for (i = 0; i < aux->used_map_cnt; i++)
    1238                 :          0 :                 bpf_map_put(aux->used_maps[i]);
    1239                 :            : 
    1240                 :        414 :         kfree(aux->used_maps);
    1241                 :        414 : }
    1242                 :            : 
    1243                 :       1656 : int __bpf_prog_charge(struct user_struct *user, u32 pages)
    1244                 :            : {
    1245                 :       1656 :         unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
    1246                 :            :         unsigned long user_bufs;
    1247                 :            : 
    1248         [ +  - ]:       1656 :         if (user) {
    1249                 :       3312 :                 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
    1250         [ -  + ]:       1656 :                 if (user_bufs > memlock_limit) {
    1251                 :            :                         atomic_long_sub(pages, &user->locked_vm);
    1252                 :          0 :                         return -EPERM;
    1253                 :            :                 }
    1254                 :            :         }
    1255                 :            : 
    1256                 :            :         return 0;
    1257                 :            : }
    1258                 :            : 
    1259                 :        414 : void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
    1260                 :            : {
    1261         [ +  - ]:        414 :         if (user)
    1262                 :        414 :                 atomic_long_sub(pages, &user->locked_vm);
    1263                 :        414 : }
    1264                 :            : 
    1265                 :       1656 : static int bpf_prog_charge_memlock(struct bpf_prog *prog)
    1266                 :            : {
    1267                 :       1656 :         struct user_struct *user = get_current_user();
    1268                 :            :         int ret;
    1269                 :            : 
    1270                 :       1656 :         ret = __bpf_prog_charge(user, prog->pages);
    1271         [ -  + ]:       1656 :         if (ret) {
    1272                 :          0 :                 free_uid(user);
    1273                 :          0 :                 return ret;
    1274                 :            :         }
    1275                 :            : 
    1276                 :       1656 :         prog->aux->user = user;
    1277                 :       1656 :         return 0;
    1278                 :            : }
    1279                 :            : 
    1280                 :        414 : static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
    1281                 :            : {
    1282                 :        414 :         struct user_struct *user = prog->aux->user;
    1283                 :            : 
    1284                 :        414 :         __bpf_prog_uncharge(user, prog->pages);
    1285                 :        414 :         free_uid(user);
    1286                 :        414 : }
    1287                 :            : 
    1288                 :       1656 : static int bpf_prog_alloc_id(struct bpf_prog *prog)
    1289                 :            : {
    1290                 :            :         int id;
    1291                 :            : 
    1292                 :       1656 :         idr_preload(GFP_KERNEL);
    1293                 :            :         spin_lock_bh(&prog_idr_lock);
    1294                 :       1656 :         id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
    1295         [ +  - ]:       1656 :         if (id > 0)
    1296                 :       1656 :                 prog->aux->id = id;
    1297                 :            :         spin_unlock_bh(&prog_idr_lock);
    1298                 :            :         idr_preload_end();
    1299                 :            : 
    1300                 :            :         /* id is in [1, INT_MAX) */
    1301   [ -  +  #  #  :       1656 :         if (WARN_ON_ONCE(!id))
                   +  - ]
    1302                 :            :                 return -ENOSPC;
    1303                 :            : 
    1304                 :       1656 :         return id > 0 ? 0 : id;
    1305                 :            : }
    1306                 :            : 
    1307                 :        414 : void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
    1308                 :            : {
    1309                 :            :         /* cBPF to eBPF migrations are currently not in the idr store.
    1310                 :            :          * Offloaded programs are removed from the store when their device
    1311                 :            :          * disappears - even if someone grabs an fd to them they are unusable,
    1312                 :            :          * simply waiting for refcnt to drop to be freed.
    1313                 :            :          */
    1314         [ +  - ]:        414 :         if (!prog->aux->id)
    1315                 :        414 :                 return;
    1316                 :            : 
    1317         [ +  - ]:        414 :         if (do_idr_lock)
    1318                 :            :                 spin_lock_bh(&prog_idr_lock);
    1319                 :            :         else
    1320                 :            :                 __acquire(&prog_idr_lock);
    1321                 :            : 
    1322                 :        414 :         idr_remove(&prog_idr, prog->aux->id);
    1323                 :        414 :         prog->aux->id = 0;
    1324                 :            : 
    1325         [ +  - ]:        414 :         if (do_idr_lock)
    1326                 :            :                 spin_unlock_bh(&prog_idr_lock);
    1327                 :            :         else
    1328                 :            :                 __release(&prog_idr_lock);
    1329                 :            : }
    1330                 :            : 
    1331                 :        414 : static void __bpf_prog_put_rcu(struct rcu_head *rcu)
    1332                 :            : {
    1333                 :        414 :         struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
    1334                 :            : 
    1335                 :        414 :         kvfree(aux->func_info);
    1336                 :        414 :         free_used_maps(aux);
    1337                 :        414 :         bpf_prog_uncharge_memlock(aux->prog);
    1338                 :        414 :         security_bpf_prog_free(aux);
    1339                 :        414 :         bpf_prog_free(aux->prog);
    1340                 :        414 : }
    1341                 :            : 
    1342                 :        414 : static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
    1343                 :            : {
    1344                 :        414 :         bpf_prog_kallsyms_del_all(prog);
    1345                 :        414 :         btf_put(prog->aux->btf);
    1346                 :        414 :         bpf_prog_free_linfo(prog);
    1347                 :            : 
    1348         [ +  - ]:        414 :         if (deferred)
    1349                 :        414 :                 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
    1350                 :            :         else
    1351                 :          0 :                 __bpf_prog_put_rcu(&prog->aux->rcu);
    1352                 :        414 : }
    1353                 :            : 
    1354                 :        414 : static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
    1355                 :            : {
    1356         [ +  - ]:        828 :         if (atomic_dec_and_test(&prog->aux->refcnt)) {
    1357                 :        414 :                 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
    1358                 :            :                 /* bpf_prog_free_id() must be called first */
    1359                 :        414 :                 bpf_prog_free_id(prog, do_idr_lock);
    1360                 :        414 :                 __bpf_prog_put_noref(prog, true);
    1361                 :            :         }
    1362                 :        414 : }
    1363                 :            : 
    1364                 :          0 : void bpf_prog_put(struct bpf_prog *prog)
    1365                 :            : {
    1366                 :        414 :         __bpf_prog_put(prog, true);
    1367                 :          0 : }
    1368                 :            : EXPORT_SYMBOL_GPL(bpf_prog_put);
    1369                 :            : 
    1370                 :        414 : static int bpf_prog_release(struct inode *inode, struct file *filp)
    1371                 :            : {
    1372                 :        414 :         struct bpf_prog *prog = filp->private_data;
    1373                 :            : 
    1374                 :            :         bpf_prog_put(prog);
    1375                 :        414 :         return 0;
    1376                 :            : }
    1377                 :            : 
    1378                 :          0 : static void bpf_prog_get_stats(const struct bpf_prog *prog,
    1379                 :            :                                struct bpf_prog_stats *stats)
    1380                 :            : {
    1381                 :            :         u64 nsecs = 0, cnt = 0;
    1382                 :            :         int cpu;
    1383                 :            : 
    1384         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
    1385                 :            :                 const struct bpf_prog_stats *st;
    1386                 :            :                 unsigned int start;
    1387                 :            :                 u64 tnsecs, tcnt;
    1388                 :            : 
    1389                 :          0 :                 st = per_cpu_ptr(prog->aux->stats, cpu);
    1390                 :            :                 do {
    1391                 :            :                         start = u64_stats_fetch_begin_irq(&st->syncp);
    1392                 :          0 :                         tnsecs = st->nsecs;
    1393                 :          0 :                         tcnt = st->cnt;
    1394         [ #  # ]:          0 :                 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
    1395                 :          0 :                 nsecs += tnsecs;
    1396                 :          0 :                 cnt += tcnt;
    1397                 :            :         }
    1398                 :          0 :         stats->nsecs = nsecs;
    1399                 :          0 :         stats->cnt = cnt;
    1400                 :          0 : }
    1401                 :            : 
    1402                 :            : #ifdef CONFIG_PROC_FS
    1403                 :          0 : static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
    1404                 :            : {
    1405                 :          0 :         const struct bpf_prog *prog = filp->private_data;
    1406                 :          0 :         char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
    1407                 :            :         struct bpf_prog_stats stats;
    1408                 :            : 
    1409                 :          0 :         bpf_prog_get_stats(prog, &stats);
    1410                 :          0 :         bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
    1411                 :          0 :         seq_printf(m,
    1412                 :            :                    "prog_type:\t%u\n"
    1413                 :            :                    "prog_jited:\t%u\n"
    1414                 :            :                    "prog_tag:\t%s\n"
    1415                 :            :                    "memlock:\t%llu\n"
    1416                 :            :                    "prog_id:\t%u\n"
    1417                 :            :                    "run_time_ns:\t%llu\n"
    1418                 :            :                    "run_cnt:\t%llu\n",
    1419                 :          0 :                    prog->type,
    1420                 :          0 :                    prog->jited,
    1421                 :            :                    prog_tag,
    1422                 :          0 :                    prog->pages * 1ULL << PAGE_SHIFT,
    1423                 :          0 :                    prog->aux->id,
    1424                 :            :                    stats.nsecs,
    1425                 :            :                    stats.cnt);
    1426                 :          0 : }
    1427                 :            : #endif
    1428                 :            : 
    1429                 :            : const struct file_operations bpf_prog_fops = {
    1430                 :            : #ifdef CONFIG_PROC_FS
    1431                 :            :         .show_fdinfo    = bpf_prog_show_fdinfo,
    1432                 :            : #endif
    1433                 :            :         .release        = bpf_prog_release,
    1434                 :            :         .read           = bpf_dummy_read,
    1435                 :            :         .write          = bpf_dummy_write,
    1436                 :            : };
    1437                 :            : 
    1438                 :       1656 : int bpf_prog_new_fd(struct bpf_prog *prog)
    1439                 :            : {
    1440                 :            :         int ret;
    1441                 :            : 
    1442                 :       1656 :         ret = security_bpf_prog(prog);
    1443         [ +  - ]:       1656 :         if (ret < 0)
    1444                 :            :                 return ret;
    1445                 :            : 
    1446                 :       1656 :         return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
    1447                 :            :                                 O_RDWR | O_CLOEXEC);
    1448                 :            : }
    1449                 :            : 
    1450                 :       1449 : static struct bpf_prog *____bpf_prog_get(struct fd f)
    1451                 :            : {
    1452         [ +  + ]:       1449 :         if (!f.file)
    1453                 :            :                 return ERR_PTR(-EBADF);
    1454         [ -  + ]:       1242 :         if (f.file->f_op != &bpf_prog_fops) {
    1455                 :            :                 fdput(f);
    1456                 :            :                 return ERR_PTR(-EINVAL);
    1457                 :            :         }
    1458                 :            : 
    1459                 :       1242 :         return f.file->private_data;
    1460                 :            : }
    1461                 :            : 
    1462                 :       1242 : struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
    1463                 :            : {
    1464         [ -  + ]:       2484 :         if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
    1465                 :          0 :                 atomic_sub(i, &prog->aux->refcnt);
    1466                 :          0 :                 return ERR_PTR(-EBUSY);
    1467                 :            :         }
    1468                 :            :         return prog;
    1469                 :            : }
    1470                 :            : EXPORT_SYMBOL_GPL(bpf_prog_add);
    1471                 :            : 
    1472                 :          0 : void bpf_prog_sub(struct bpf_prog *prog, int i)
    1473                 :            : {
    1474                 :            :         /* Only to be used for undoing previous bpf_prog_add() in some
    1475                 :            :          * error path. We still know that another entity in our call
    1476                 :            :          * path holds a reference to the program, thus atomic_sub() can
    1477                 :            :          * be safely used in such cases!
    1478                 :            :          */
    1479         [ #  # ]:          0 :         WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
    1480                 :          0 : }
    1481                 :            : EXPORT_SYMBOL_GPL(bpf_prog_sub);
    1482                 :            : 
    1483                 :          0 : struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
    1484                 :            : {
    1485                 :       1242 :         return bpf_prog_add(prog, 1);
    1486                 :            : }
    1487                 :            : EXPORT_SYMBOL_GPL(bpf_prog_inc);
    1488                 :            : 
    1489                 :            : /* prog_idr_lock should have been held */
    1490                 :          0 : struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
    1491                 :            : {
    1492                 :            :         int refold;
    1493                 :            : 
    1494                 :          0 :         refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
    1495                 :            : 
    1496         [ #  # ]:          0 :         if (refold >= BPF_MAX_REFCNT) {
    1497                 :          0 :                 __bpf_prog_put(prog, false);
    1498                 :          0 :                 return ERR_PTR(-EBUSY);
    1499                 :            :         }
    1500                 :            : 
    1501         [ #  # ]:          0 :         if (!refold)
    1502                 :            :                 return ERR_PTR(-ENOENT);
    1503                 :            : 
    1504                 :          0 :         return prog;
    1505                 :            : }
    1506                 :            : EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
    1507                 :            : 
    1508                 :          0 : bool bpf_prog_get_ok(struct bpf_prog *prog,
    1509                 :            :                             enum bpf_prog_type *attach_type, bool attach_drv)
    1510                 :            : {
    1511                 :            :         /* not an attachment, just a refcount inc, always allow */
    1512   [ +  -  #  # ]:       1242 :         if (!attach_type)
    1513                 :            :                 return true;
    1514                 :            : 
    1515   [ +  -  #  # ]:       1242 :         if (prog->type != *attach_type)
    1516                 :            :                 return false;
    1517   [ -  +  #  #  :       2484 :         if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
             #  #  #  # ]
    1518                 :            :                 return false;
    1519                 :            : 
    1520                 :          0 :         return true;
    1521                 :            : }
    1522                 :            : 
    1523                 :       1449 : static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
    1524                 :            :                                        bool attach_drv)
    1525                 :            : {
    1526                 :            :         struct fd f = fdget(ufd);
    1527                 :            :         struct bpf_prog *prog;
    1528                 :            : 
    1529                 :       1449 :         prog = ____bpf_prog_get(f);
    1530         [ +  + ]:       1449 :         if (IS_ERR(prog))
    1531                 :            :                 return prog;
    1532         [ +  - ]:       1242 :         if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
    1533                 :            :                 prog = ERR_PTR(-EINVAL);
    1534                 :            :                 goto out;
    1535                 :            :         }
    1536                 :            : 
    1537                 :            :         prog = bpf_prog_inc(prog);
    1538                 :            : out:
    1539                 :            :         fdput(f);
    1540                 :       1242 :         return prog;
    1541                 :            : }
    1542                 :            : 
    1543                 :          0 : struct bpf_prog *bpf_prog_get(u32 ufd)
    1544                 :            : {
    1545                 :          0 :         return __bpf_prog_get(ufd, NULL, false);
    1546                 :            : }
    1547                 :            : 
    1548                 :          0 : struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
    1549                 :            :                                        bool attach_drv)
    1550                 :            : {
    1551                 :       1449 :         return __bpf_prog_get(ufd, &type, attach_drv);
    1552                 :            : }
    1553                 :            : EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
    1554                 :            : 
    1555                 :            : /* Initially all BPF programs could be loaded w/o specifying
    1556                 :            :  * expected_attach_type. Later for some of them specifying expected_attach_type
    1557                 :            :  * at load time became required so that program could be validated properly.
    1558                 :            :  * Programs of types that are allowed to be loaded both w/ and w/o (for
    1559                 :            :  * backward compatibility) expected_attach_type, should have the default attach
    1560                 :            :  * type assigned to expected_attach_type for the latter case, so that it can be
    1561                 :            :  * validated later at attach time.
    1562                 :            :  *
    1563                 :            :  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
    1564                 :            :  * prog type requires it but has some attach types that have to be backward
    1565                 :            :  * compatible.
    1566                 :            :  */
    1567                 :            : static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
    1568                 :            : {
    1569         [ -  + ]:       1656 :         switch (attr->prog_type) {
    1570                 :            :         case BPF_PROG_TYPE_CGROUP_SOCK:
    1571                 :            :                 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
    1572                 :            :                  * exist so checking for non-zero is the way to go here.
    1573                 :            :                  */
    1574         [ #  # ]:          0 :                 if (!attr->expected_attach_type)
    1575                 :          0 :                         attr->expected_attach_type =
    1576                 :            :                                 BPF_CGROUP_INET_SOCK_CREATE;
    1577                 :            :                 break;
    1578                 :            :         }
    1579                 :            : }
    1580                 :            : 
    1581                 :            : static int
    1582                 :       1656 : bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
    1583                 :            :                                 enum bpf_attach_type expected_attach_type)
    1584                 :            : {
    1585   [ -  -  +  -  :       1656 :         switch (prog_type) {
                      + ]
    1586                 :            :         case BPF_PROG_TYPE_CGROUP_SOCK:
    1587         [ #  # ]:          0 :                 switch (expected_attach_type) {
    1588                 :            :                 case BPF_CGROUP_INET_SOCK_CREATE:
    1589                 :            :                 case BPF_CGROUP_INET4_POST_BIND:
    1590                 :            :                 case BPF_CGROUP_INET6_POST_BIND:
    1591                 :            :                         return 0;
    1592                 :            :                 default:
    1593                 :          0 :                         return -EINVAL;
    1594                 :            :                 }
    1595                 :            :         case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
    1596         [ #  # ]:          0 :                 switch (expected_attach_type) {
    1597                 :            :                 case BPF_CGROUP_INET4_BIND:
    1598                 :            :                 case BPF_CGROUP_INET6_BIND:
    1599                 :            :                 case BPF_CGROUP_INET4_CONNECT:
    1600                 :            :                 case BPF_CGROUP_INET6_CONNECT:
    1601                 :            :                 case BPF_CGROUP_UDP4_SENDMSG:
    1602                 :            :                 case BPF_CGROUP_UDP6_SENDMSG:
    1603                 :            :                 case BPF_CGROUP_UDP4_RECVMSG:
    1604                 :            :                 case BPF_CGROUP_UDP6_RECVMSG:
    1605                 :            :                         return 0;
    1606                 :            :                 default:
    1607                 :          0 :                         return -EINVAL;
    1608                 :            :                 }
    1609                 :            :         case BPF_PROG_TYPE_CGROUP_SKB:
    1610         [ -  + ]:       1449 :                 switch (expected_attach_type) {
    1611                 :            :                 case BPF_CGROUP_INET_INGRESS:
    1612                 :            :                 case BPF_CGROUP_INET_EGRESS:
    1613                 :            :                         return 0;
    1614                 :            :                 default:
    1615                 :          0 :                         return -EINVAL;
    1616                 :            :                 }
    1617                 :            :         case BPF_PROG_TYPE_CGROUP_SOCKOPT:
    1618         [ #  # ]:          0 :                 switch (expected_attach_type) {
    1619                 :            :                 case BPF_CGROUP_SETSOCKOPT:
    1620                 :            :                 case BPF_CGROUP_GETSOCKOPT:
    1621                 :            :                         return 0;
    1622                 :            :                 default:
    1623                 :          0 :                         return -EINVAL;
    1624                 :            :                 }
    1625                 :            :         default:
    1626                 :            :                 return 0;
    1627                 :            :         }
    1628                 :            : }
    1629                 :            : 
    1630                 :            : /* last field in 'union bpf_attr' used by this command */
    1631                 :            : #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
    1632                 :            : 
    1633                 :       1656 : static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
    1634                 :            : {
    1635                 :       1656 :         enum bpf_prog_type type = attr->prog_type;
    1636                 :            :         struct bpf_prog *prog;
    1637                 :            :         int err;
    1638                 :            :         char license[128];
    1639                 :            :         bool is_gpl;
    1640                 :            : 
    1641         [ +  - ]:       1656 :         if (CHECK_ATTR(BPF_PROG_LOAD))
    1642                 :            :                 return -EINVAL;
    1643                 :            : 
    1644         [ +  - ]:       1656 :         if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
    1645                 :            :                                  BPF_F_ANY_ALIGNMENT |
    1646                 :            :                                  BPF_F_TEST_STATE_FREQ |
    1647                 :            :                                  BPF_F_TEST_RND_HI32))
    1648                 :            :                 return -EINVAL;
    1649                 :            : 
    1650                 :            :         if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
    1651                 :            :             (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
    1652                 :            :             !capable(CAP_SYS_ADMIN))
    1653                 :            :                 return -EPERM;
    1654                 :            : 
    1655                 :            :         /* copy eBPF program license from user space */
    1656         [ +  - ]:       1656 :         if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
    1657                 :            :                               sizeof(license) - 1) < 0)
    1658                 :            :                 return -EFAULT;
    1659                 :       1656 :         license[sizeof(license) - 1] = 0;
    1660                 :            : 
    1661                 :            :         /* eBPF programs must be GPL compatible to use GPL-ed functions */
    1662                 :       1656 :         is_gpl = license_is_gpl_compatible(license);
    1663                 :            : 
    1664   [ +  -  +  - ]:       3312 :         if (attr->insn_cnt == 0 ||
    1665         [ -  + ]:       1656 :             attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
    1666                 :            :                 return -E2BIG;
    1667         [ +  + ]:       3312 :         if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
    1668         [ +  - ]:       1863 :             type != BPF_PROG_TYPE_CGROUP_SKB &&
    1669                 :        207 :             !capable(CAP_SYS_ADMIN))
    1670                 :            :                 return -EPERM;
    1671                 :            : 
    1672                 :            :         bpf_prog_load_fixup_attach_type(attr);
    1673         [ +  - ]:       1656 :         if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
    1674                 :            :                 return -EINVAL;
    1675                 :            : 
    1676                 :            :         /* plain bpf_prog allocation */
    1677                 :       3312 :         prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
    1678         [ +  - ]:       1656 :         if (!prog)
    1679                 :            :                 return -ENOMEM;
    1680                 :            : 
    1681                 :       1656 :         prog->expected_attach_type = attr->expected_attach_type;
    1682                 :            : 
    1683                 :       1656 :         prog->aux->offload_requested = !!attr->prog_ifindex;
    1684                 :            : 
    1685                 :       1656 :         err = security_bpf_prog_alloc(prog->aux);
    1686         [ +  - ]:       1656 :         if (err)
    1687                 :            :                 goto free_prog_nouncharge;
    1688                 :            : 
    1689                 :       1656 :         err = bpf_prog_charge_memlock(prog);
    1690         [ +  - ]:       1656 :         if (err)
    1691                 :            :                 goto free_prog_sec;
    1692                 :            : 
    1693                 :       1656 :         prog->len = attr->insn_cnt;
    1694                 :            : 
    1695                 :       1656 :         err = -EFAULT;
    1696         [ +  - ]:       3312 :         if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
    1697                 :            :                            bpf_prog_insn_size(prog)) != 0)
    1698                 :            :                 goto free_prog;
    1699                 :            : 
    1700                 :       1656 :         prog->orig_prog = NULL;
    1701                 :       1656 :         prog->jited = 0;
    1702                 :            : 
    1703                 :       1656 :         atomic_set(&prog->aux->refcnt, 1);
    1704                 :       1656 :         prog->gpl_compatible = is_gpl ? 1 : 0;
    1705                 :            : 
    1706         [ -  + ]:       3312 :         if (bpf_prog_is_dev_bound(prog->aux)) {
    1707                 :          0 :                 err = bpf_prog_offload_init(prog, attr);
    1708         [ #  # ]:          0 :                 if (err)
    1709                 :            :                         goto free_prog;
    1710                 :            :         }
    1711                 :            : 
    1712                 :            :         /* find program type: socket_filter vs tracing_filter */
    1713                 :       3312 :         err = find_prog_type(type, prog);
    1714         [ +  - ]:       1656 :         if (err < 0)
    1715                 :            :                 goto free_prog;
    1716                 :            : 
    1717                 :       3312 :         prog->aux->load_time = ktime_get_boottime_ns();
    1718                 :       1656 :         err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
    1719         [ +  - ]:       1656 :         if (err)
    1720                 :            :                 goto free_prog;
    1721                 :            : 
    1722                 :            :         /* run eBPF verifier */
    1723                 :       1656 :         err = bpf_check(&prog, attr, uattr);
    1724         [ +  - ]:       1656 :         if (err < 0)
    1725                 :            :                 goto free_used_maps;
    1726                 :            : 
    1727                 :       1656 :         prog = bpf_prog_select_runtime(prog, &err);
    1728         [ +  - ]:       1656 :         if (err < 0)
    1729                 :            :                 goto free_used_maps;
    1730                 :            : 
    1731                 :       1656 :         err = bpf_prog_alloc_id(prog);
    1732         [ +  - ]:       1656 :         if (err)
    1733                 :            :                 goto free_used_maps;
    1734                 :            : 
    1735                 :            :         /* Upon success of bpf_prog_alloc_id(), the BPF prog is
    1736                 :            :          * effectively publicly exposed. However, retrieving via
    1737                 :            :          * bpf_prog_get_fd_by_id() will take another reference,
    1738                 :            :          * therefore it cannot be gone underneath us.
    1739                 :            :          *
    1740                 :            :          * Only for the time /after/ successful bpf_prog_new_fd()
    1741                 :            :          * and before returning to userspace, we might just hold
    1742                 :            :          * one reference and any parallel close on that fd could
    1743                 :            :          * rip everything out. Hence, below notifications must
    1744                 :            :          * happen before bpf_prog_new_fd().
    1745                 :            :          *
    1746                 :            :          * Also, any failure handling from this point onwards must
    1747                 :            :          * be using bpf_prog_put() given the program is exposed.
    1748                 :            :          */
    1749                 :            :         bpf_prog_kallsyms_add(prog);
    1750                 :       1656 :         perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
    1751                 :            : 
    1752                 :       1656 :         err = bpf_prog_new_fd(prog);
    1753         [ -  + ]:       1656 :         if (err < 0)
    1754                 :          0 :                 bpf_prog_put(prog);
    1755                 :       1656 :         return err;
    1756                 :            : 
    1757                 :            : free_used_maps:
    1758                 :            :         /* In case we have subprogs, we need to wait for a grace
    1759                 :            :          * period before we can tear down JIT memory since symbols
    1760                 :            :          * are already exposed under kallsyms.
    1761                 :            :          */
    1762                 :          0 :         __bpf_prog_put_noref(prog, prog->aux->func_cnt);
    1763                 :          0 :         return err;
    1764                 :            : free_prog:
    1765                 :          0 :         bpf_prog_uncharge_memlock(prog);
    1766                 :            : free_prog_sec:
    1767                 :          0 :         security_bpf_prog_free(prog->aux);
    1768                 :            : free_prog_nouncharge:
    1769                 :          0 :         bpf_prog_free(prog);
    1770                 :          0 :         return err;
    1771                 :            : }
    1772                 :            : 
    1773                 :            : #define BPF_OBJ_LAST_FIELD file_flags
    1774                 :            : 
    1775                 :          0 : static int bpf_obj_pin(const union bpf_attr *attr)
    1776                 :            : {
    1777   [ #  #  #  # ]:          0 :         if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
    1778                 :            :                 return -EINVAL;
    1779                 :            : 
    1780                 :          0 :         return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
    1781                 :            : }
    1782                 :            : 
    1783                 :          0 : static int bpf_obj_get(const union bpf_attr *attr)
    1784                 :            : {
    1785   [ #  #  #  #  :          0 :         if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
                   #  # ]
    1786                 :          0 :             attr->file_flags & ~BPF_OBJ_FLAG_MASK)
    1787                 :            :                 return -EINVAL;
    1788                 :            : 
    1789                 :          0 :         return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
    1790                 :            :                                 attr->file_flags);
    1791                 :            : }
    1792                 :            : 
    1793                 :            : struct bpf_raw_tracepoint {
    1794                 :            :         struct bpf_raw_event_map *btp;
    1795                 :            :         struct bpf_prog *prog;
    1796                 :            : };
    1797                 :            : 
    1798                 :          0 : static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
    1799                 :            : {
    1800                 :          0 :         struct bpf_raw_tracepoint *raw_tp = filp->private_data;
    1801                 :            : 
    1802         [ #  # ]:          0 :         if (raw_tp->prog) {
    1803                 :          0 :                 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
    1804                 :          0 :                 bpf_prog_put(raw_tp->prog);
    1805                 :            :         }
    1806                 :          0 :         bpf_put_raw_tracepoint(raw_tp->btp);
    1807                 :          0 :         kfree(raw_tp);
    1808                 :          0 :         return 0;
    1809                 :            : }
    1810                 :            : 
    1811                 :            : static const struct file_operations bpf_raw_tp_fops = {
    1812                 :            :         .release        = bpf_raw_tracepoint_release,
    1813                 :            :         .read           = bpf_dummy_read,
    1814                 :            :         .write          = bpf_dummy_write,
    1815                 :            : };
    1816                 :            : 
    1817                 :            : #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
    1818                 :            : 
    1819                 :          0 : static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
    1820                 :            : {
    1821                 :            :         struct bpf_raw_tracepoint *raw_tp;
    1822                 :            :         struct bpf_raw_event_map *btp;
    1823                 :            :         struct bpf_prog *prog;
    1824                 :            :         char tp_name[128];
    1825                 :            :         int tp_fd, err;
    1826                 :            : 
    1827         [ #  # ]:          0 :         if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
    1828                 :            :                               sizeof(tp_name) - 1) < 0)
    1829                 :            :                 return -EFAULT;
    1830                 :          0 :         tp_name[sizeof(tp_name) - 1] = 0;
    1831                 :            : 
    1832                 :          0 :         btp = bpf_get_raw_tracepoint(tp_name);
    1833         [ #  # ]:          0 :         if (!btp)
    1834                 :            :                 return -ENOENT;
    1835                 :            : 
    1836                 :          0 :         raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
    1837         [ #  # ]:          0 :         if (!raw_tp) {
    1838                 :            :                 err = -ENOMEM;
    1839                 :            :                 goto out_put_btp;
    1840                 :            :         }
    1841                 :          0 :         raw_tp->btp = btp;
    1842                 :            : 
    1843                 :          0 :         prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
    1844         [ #  # ]:          0 :         if (IS_ERR(prog)) {
    1845                 :            :                 err = PTR_ERR(prog);
    1846                 :          0 :                 goto out_free_tp;
    1847                 :            :         }
    1848         [ #  # ]:          0 :         if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
    1849                 :            :             prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
    1850                 :            :                 err = -EINVAL;
    1851                 :            :                 goto out_put_prog;
    1852                 :            :         }
    1853                 :            : 
    1854                 :          0 :         err = bpf_probe_register(raw_tp->btp, prog);
    1855         [ #  # ]:          0 :         if (err)
    1856                 :            :                 goto out_put_prog;
    1857                 :            : 
    1858                 :          0 :         raw_tp->prog = prog;
    1859                 :          0 :         tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
    1860                 :            :                                  O_CLOEXEC);
    1861         [ #  # ]:          0 :         if (tp_fd < 0) {
    1862                 :          0 :                 bpf_probe_unregister(raw_tp->btp, prog);
    1863                 :            :                 err = tp_fd;
    1864                 :          0 :                 goto out_put_prog;
    1865                 :            :         }
    1866                 :            :         return tp_fd;
    1867                 :            : 
    1868                 :            : out_put_prog:
    1869                 :            :         bpf_prog_put(prog);
    1870                 :            : out_free_tp:
    1871                 :          0 :         kfree(raw_tp);
    1872                 :            : out_put_btp:
    1873                 :          0 :         bpf_put_raw_tracepoint(btp);
    1874                 :          0 :         return err;
    1875                 :            : }
    1876                 :            : 
    1877                 :       1242 : static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
    1878                 :            :                                              enum bpf_attach_type attach_type)
    1879                 :            : {
    1880      [ -  +  - ]:       1242 :         switch (prog->type) {
    1881                 :            :         case BPF_PROG_TYPE_CGROUP_SOCK:
    1882                 :            :         case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
    1883                 :            :         case BPF_PROG_TYPE_CGROUP_SOCKOPT:
    1884         [ #  # ]:          0 :                 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
    1885                 :            :         case BPF_PROG_TYPE_CGROUP_SKB:
    1886         [ #  # ]:       1242 :                 return prog->enforce_expected_attach_type &&
    1887                 :          0 :                         prog->expected_attach_type != attach_type ?
    1888         [ -  + ]:       1242 :                         -EINVAL : 0;
    1889                 :            :         default:
    1890                 :            :                 return 0;
    1891                 :            :         }
    1892                 :            : }
    1893                 :            : 
    1894                 :            : #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
    1895                 :            : 
    1896                 :            : #define BPF_F_ATTACH_MASK \
    1897                 :            :         (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
    1898                 :            : 
    1899                 :       1449 : static int bpf_prog_attach(const union bpf_attr *attr)
    1900                 :            : {
    1901                 :            :         enum bpf_prog_type ptype;
    1902                 :            :         struct bpf_prog *prog;
    1903                 :            :         int ret;
    1904                 :            : 
    1905         [ +  - ]:       1449 :         if (!capable(CAP_NET_ADMIN))
    1906                 :            :                 return -EPERM;
    1907                 :            : 
    1908         [ +  - ]:       1449 :         if (CHECK_ATTR(BPF_PROG_ATTACH))
    1909                 :            :                 return -EINVAL;
    1910                 :            : 
    1911         [ +  - ]:       1449 :         if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
    1912                 :            :                 return -EINVAL;
    1913                 :            : 
    1914   [ -  -  -  -  :       1449 :         switch (attr->attach_type) {
          -  -  -  -  -  
                -  +  - ]
    1915                 :            :         case BPF_CGROUP_INET_INGRESS:
    1916                 :            :         case BPF_CGROUP_INET_EGRESS:
    1917                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SKB;
    1918                 :            :                 break;
    1919                 :            :         case BPF_CGROUP_INET_SOCK_CREATE:
    1920                 :            :         case BPF_CGROUP_INET4_POST_BIND:
    1921                 :            :         case BPF_CGROUP_INET6_POST_BIND:
    1922                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
    1923                 :          0 :                 break;
    1924                 :            :         case BPF_CGROUP_INET4_BIND:
    1925                 :            :         case BPF_CGROUP_INET6_BIND:
    1926                 :            :         case BPF_CGROUP_INET4_CONNECT:
    1927                 :            :         case BPF_CGROUP_INET6_CONNECT:
    1928                 :            :         case BPF_CGROUP_UDP4_SENDMSG:
    1929                 :            :         case BPF_CGROUP_UDP6_SENDMSG:
    1930                 :            :         case BPF_CGROUP_UDP4_RECVMSG:
    1931                 :            :         case BPF_CGROUP_UDP6_RECVMSG:
    1932                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
    1933                 :          0 :                 break;
    1934                 :            :         case BPF_CGROUP_SOCK_OPS:
    1935                 :            :                 ptype = BPF_PROG_TYPE_SOCK_OPS;
    1936                 :          0 :                 break;
    1937                 :            :         case BPF_CGROUP_DEVICE:
    1938                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
    1939                 :          0 :                 break;
    1940                 :            :         case BPF_SK_MSG_VERDICT:
    1941                 :            :                 ptype = BPF_PROG_TYPE_SK_MSG;
    1942                 :          0 :                 break;
    1943                 :            :         case BPF_SK_SKB_STREAM_PARSER:
    1944                 :            :         case BPF_SK_SKB_STREAM_VERDICT:
    1945                 :            :                 ptype = BPF_PROG_TYPE_SK_SKB;
    1946                 :          0 :                 break;
    1947                 :            :         case BPF_LIRC_MODE2:
    1948                 :            :                 ptype = BPF_PROG_TYPE_LIRC_MODE2;
    1949                 :          0 :                 break;
    1950                 :            :         case BPF_FLOW_DISSECTOR:
    1951                 :            :                 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
    1952                 :          0 :                 break;
    1953                 :            :         case BPF_CGROUP_SYSCTL:
    1954                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
    1955                 :          0 :                 break;
    1956                 :            :         case BPF_CGROUP_GETSOCKOPT:
    1957                 :            :         case BPF_CGROUP_SETSOCKOPT:
    1958                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
    1959                 :          0 :                 break;
    1960                 :            :         default:
    1961                 :            :                 return -EINVAL;
    1962                 :            :         }
    1963                 :            : 
    1964                 :       1449 :         prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
    1965         [ +  + ]:       1449 :         if (IS_ERR(prog))
    1966                 :        207 :                 return PTR_ERR(prog);
    1967                 :            : 
    1968         [ -  + ]:       1242 :         if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
    1969                 :            :                 bpf_prog_put(prog);
    1970                 :          0 :                 return -EINVAL;
    1971                 :            :         }
    1972                 :            : 
    1973   [ -  -  +  - ]:       1242 :         switch (ptype) {
    1974                 :            :         case BPF_PROG_TYPE_SK_SKB:
    1975                 :            :         case BPF_PROG_TYPE_SK_MSG:
    1976                 :            :                 ret = sock_map_get_from_fd(attr, prog);
    1977                 :            :                 break;
    1978                 :            :         case BPF_PROG_TYPE_LIRC_MODE2:
    1979                 :          0 :                 ret = lirc_prog_attach(attr, prog);
    1980                 :          0 :                 break;
    1981                 :            :         case BPF_PROG_TYPE_FLOW_DISSECTOR:
    1982                 :          0 :                 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
    1983                 :          0 :                 break;
    1984                 :            :         default:
    1985                 :       1242 :                 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
    1986                 :            :         }
    1987                 :            : 
    1988         [ -  + ]:       1242 :         if (ret)
    1989                 :            :                 bpf_prog_put(prog);
    1990                 :       1242 :         return ret;
    1991                 :            : }
    1992                 :            : 
    1993                 :            : #define BPF_PROG_DETACH_LAST_FIELD attach_type
    1994                 :            : 
    1995                 :        207 : static int bpf_prog_detach(const union bpf_attr *attr)
    1996                 :            : {
    1997                 :            :         enum bpf_prog_type ptype;
    1998                 :            : 
    1999         [ +  - ]:        207 :         if (!capable(CAP_NET_ADMIN))
    2000                 :            :                 return -EPERM;
    2001                 :            : 
    2002         [ +  - ]:        207 :         if (CHECK_ATTR(BPF_PROG_DETACH))
    2003                 :            :                 return -EINVAL;
    2004                 :            : 
    2005   [ -  -  -  -  :        207 :         switch (attr->attach_type) {
          -  -  -  -  -  
                   +  - ]
    2006                 :            :         case BPF_CGROUP_INET_INGRESS:
    2007                 :            :         case BPF_CGROUP_INET_EGRESS:
    2008                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SKB;
    2009                 :            :                 break;
    2010                 :            :         case BPF_CGROUP_INET_SOCK_CREATE:
    2011                 :            :         case BPF_CGROUP_INET4_POST_BIND:
    2012                 :            :         case BPF_CGROUP_INET6_POST_BIND:
    2013                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
    2014                 :          0 :                 break;
    2015                 :            :         case BPF_CGROUP_INET4_BIND:
    2016                 :            :         case BPF_CGROUP_INET6_BIND:
    2017                 :            :         case BPF_CGROUP_INET4_CONNECT:
    2018                 :            :         case BPF_CGROUP_INET6_CONNECT:
    2019                 :            :         case BPF_CGROUP_UDP4_SENDMSG:
    2020                 :            :         case BPF_CGROUP_UDP6_SENDMSG:
    2021                 :            :         case BPF_CGROUP_UDP4_RECVMSG:
    2022                 :            :         case BPF_CGROUP_UDP6_RECVMSG:
    2023                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
    2024                 :          0 :                 break;
    2025                 :            :         case BPF_CGROUP_SOCK_OPS:
    2026                 :            :                 ptype = BPF_PROG_TYPE_SOCK_OPS;
    2027                 :          0 :                 break;
    2028                 :            :         case BPF_CGROUP_DEVICE:
    2029                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
    2030                 :          0 :                 break;
    2031                 :            :         case BPF_SK_MSG_VERDICT:
    2032                 :            :                 return sock_map_prog_detach(attr, BPF_PROG_TYPE_SK_MSG);
    2033                 :            :         case BPF_SK_SKB_STREAM_PARSER:
    2034                 :            :         case BPF_SK_SKB_STREAM_VERDICT:
    2035                 :            :                 return sock_map_prog_detach(attr, BPF_PROG_TYPE_SK_SKB);
    2036                 :            :         case BPF_LIRC_MODE2:
    2037                 :          0 :                 return lirc_prog_detach(attr);
    2038                 :            :         case BPF_FLOW_DISSECTOR:
    2039                 :          0 :                 return skb_flow_dissector_bpf_prog_detach(attr);
    2040                 :            :         case BPF_CGROUP_SYSCTL:
    2041                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
    2042                 :          0 :                 break;
    2043                 :            :         case BPF_CGROUP_GETSOCKOPT:
    2044                 :            :         case BPF_CGROUP_SETSOCKOPT:
    2045                 :            :                 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
    2046                 :          0 :                 break;
    2047                 :            :         default:
    2048                 :          0 :                 return -EINVAL;
    2049                 :            :         }
    2050                 :            : 
    2051                 :        207 :         return cgroup_bpf_prog_detach(attr, ptype);
    2052                 :            : }
    2053                 :            : 
    2054                 :            : #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
    2055                 :            : 
    2056                 :          0 : static int bpf_prog_query(const union bpf_attr *attr,
    2057                 :            :                           union bpf_attr __user *uattr)
    2058                 :            : {
    2059         [ #  # ]:          0 :         if (!capable(CAP_NET_ADMIN))
    2060                 :            :                 return -EPERM;
    2061         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_PROG_QUERY))
    2062                 :            :                 return -EINVAL;
    2063         [ #  # ]:          0 :         if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
    2064                 :            :                 return -EINVAL;
    2065                 :            : 
    2066   [ #  #  #  # ]:          0 :         switch (attr->query.attach_type) {
    2067                 :            :         case BPF_CGROUP_INET_INGRESS:
    2068                 :            :         case BPF_CGROUP_INET_EGRESS:
    2069                 :            :         case BPF_CGROUP_INET_SOCK_CREATE:
    2070                 :            :         case BPF_CGROUP_INET4_BIND:
    2071                 :            :         case BPF_CGROUP_INET6_BIND:
    2072                 :            :         case BPF_CGROUP_INET4_POST_BIND:
    2073                 :            :         case BPF_CGROUP_INET6_POST_BIND:
    2074                 :            :         case BPF_CGROUP_INET4_CONNECT:
    2075                 :            :         case BPF_CGROUP_INET6_CONNECT:
    2076                 :            :         case BPF_CGROUP_UDP4_SENDMSG:
    2077                 :            :         case BPF_CGROUP_UDP6_SENDMSG:
    2078                 :            :         case BPF_CGROUP_UDP4_RECVMSG:
    2079                 :            :         case BPF_CGROUP_UDP6_RECVMSG:
    2080                 :            :         case BPF_CGROUP_SOCK_OPS:
    2081                 :            :         case BPF_CGROUP_DEVICE:
    2082                 :            :         case BPF_CGROUP_SYSCTL:
    2083                 :            :         case BPF_CGROUP_GETSOCKOPT:
    2084                 :            :         case BPF_CGROUP_SETSOCKOPT:
    2085                 :            :                 break;
    2086                 :            :         case BPF_LIRC_MODE2:
    2087                 :          0 :                 return lirc_prog_query(attr, uattr);
    2088                 :            :         case BPF_FLOW_DISSECTOR:
    2089                 :          0 :                 return skb_flow_dissector_prog_query(attr, uattr);
    2090                 :            :         default:
    2091                 :            :                 return -EINVAL;
    2092                 :            :         }
    2093                 :            : 
    2094                 :          0 :         return cgroup_bpf_prog_query(attr, uattr);
    2095                 :            : }
    2096                 :            : 
    2097                 :            : #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
    2098                 :            : 
    2099                 :          0 : static int bpf_prog_test_run(const union bpf_attr *attr,
    2100                 :            :                              union bpf_attr __user *uattr)
    2101                 :            : {
    2102                 :            :         struct bpf_prog *prog;
    2103                 :            :         int ret = -ENOTSUPP;
    2104                 :            : 
    2105         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2106                 :            :                 return -EPERM;
    2107         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_PROG_TEST_RUN))
    2108                 :            :                 return -EINVAL;
    2109                 :            : 
    2110   [ #  #  #  #  :          0 :         if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
                   #  # ]
    2111         [ #  # ]:          0 :             (!attr->test.ctx_size_in && attr->test.ctx_in))
    2112                 :            :                 return -EINVAL;
    2113                 :            : 
    2114   [ #  #  #  #  :          0 :         if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
                   #  # ]
    2115         [ #  # ]:          0 :             (!attr->test.ctx_size_out && attr->test.ctx_out))
    2116                 :            :                 return -EINVAL;
    2117                 :            : 
    2118                 :          0 :         prog = bpf_prog_get(attr->test.prog_fd);
    2119         [ #  # ]:          0 :         if (IS_ERR(prog))
    2120                 :          0 :                 return PTR_ERR(prog);
    2121                 :            : 
    2122         [ #  # ]:          0 :         if (prog->aux->ops->test_run)
    2123                 :          0 :                 ret = prog->aux->ops->test_run(prog, attr, uattr);
    2124                 :            : 
    2125                 :            :         bpf_prog_put(prog);
    2126                 :          0 :         return ret;
    2127                 :            : }
    2128                 :            : 
    2129                 :            : #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
    2130                 :            : 
    2131                 :          0 : static int bpf_obj_get_next_id(const union bpf_attr *attr,
    2132                 :            :                                union bpf_attr __user *uattr,
    2133                 :            :                                struct idr *idr,
    2134                 :            :                                spinlock_t *lock)
    2135                 :            : {
    2136                 :          0 :         u32 next_id = attr->start_id;
    2137                 :            :         int err = 0;
    2138                 :            : 
    2139   [ #  #  #  # ]:          0 :         if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
    2140                 :            :                 return -EINVAL;
    2141                 :            : 
    2142         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2143                 :            :                 return -EPERM;
    2144                 :            : 
    2145                 :          0 :         next_id++;
    2146                 :            :         spin_lock_bh(lock);
    2147         [ #  # ]:          0 :         if (!idr_get_next(idr, &next_id))
    2148                 :            :                 err = -ENOENT;
    2149                 :            :         spin_unlock_bh(lock);
    2150                 :            : 
    2151         [ #  # ]:          0 :         if (!err)
    2152                 :          0 :                 err = put_user(next_id, &uattr->next_id);
    2153                 :            : 
    2154                 :          0 :         return err;
    2155                 :            : }
    2156                 :            : 
    2157                 :            : #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
    2158                 :            : 
    2159                 :          0 : static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
    2160                 :            : {
    2161                 :            :         struct bpf_prog *prog;
    2162                 :          0 :         u32 id = attr->prog_id;
    2163                 :            :         int fd;
    2164                 :            : 
    2165         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
    2166                 :            :                 return -EINVAL;
    2167                 :            : 
    2168         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2169                 :            :                 return -EPERM;
    2170                 :            : 
    2171                 :            :         spin_lock_bh(&prog_idr_lock);
    2172                 :          0 :         prog = idr_find(&prog_idr, id);
    2173         [ #  # ]:          0 :         if (prog)
    2174                 :          0 :                 prog = bpf_prog_inc_not_zero(prog);
    2175                 :            :         else
    2176                 :            :                 prog = ERR_PTR(-ENOENT);
    2177                 :            :         spin_unlock_bh(&prog_idr_lock);
    2178                 :            : 
    2179         [ #  # ]:          0 :         if (IS_ERR(prog))
    2180                 :          0 :                 return PTR_ERR(prog);
    2181                 :            : 
    2182                 :          0 :         fd = bpf_prog_new_fd(prog);
    2183         [ #  # ]:          0 :         if (fd < 0)
    2184                 :            :                 bpf_prog_put(prog);
    2185                 :            : 
    2186                 :          0 :         return fd;
    2187                 :            : }
    2188                 :            : 
    2189                 :            : #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
    2190                 :            : 
    2191                 :          0 : static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
    2192                 :            : {
    2193                 :            :         struct bpf_map *map;
    2194                 :          0 :         u32 id = attr->map_id;
    2195                 :            :         int f_flags;
    2196                 :            :         int fd;
    2197                 :            : 
    2198   [ #  #  #  # ]:          0 :         if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
    2199                 :          0 :             attr->open_flags & ~BPF_OBJ_FLAG_MASK)
    2200                 :            :                 return -EINVAL;
    2201                 :            : 
    2202         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2203                 :            :                 return -EPERM;
    2204                 :            : 
    2205                 :          0 :         f_flags = bpf_get_file_flag(attr->open_flags);
    2206         [ #  # ]:          0 :         if (f_flags < 0)
    2207                 :            :                 return f_flags;
    2208                 :            : 
    2209                 :            :         spin_lock_bh(&map_idr_lock);
    2210                 :          0 :         map = idr_find(&map_idr, id);
    2211         [ #  # ]:          0 :         if (map)
    2212                 :          0 :                 map = __bpf_map_inc_not_zero(map, true);
    2213                 :            :         else
    2214                 :            :                 map = ERR_PTR(-ENOENT);
    2215                 :            :         spin_unlock_bh(&map_idr_lock);
    2216                 :            : 
    2217         [ #  # ]:          0 :         if (IS_ERR(map))
    2218                 :          0 :                 return PTR_ERR(map);
    2219                 :            : 
    2220                 :          0 :         fd = bpf_map_new_fd(map, f_flags);
    2221         [ #  # ]:          0 :         if (fd < 0)
    2222                 :            :                 bpf_map_put_with_uref(map);
    2223                 :            : 
    2224                 :          0 :         return fd;
    2225                 :            : }
    2226                 :            : 
    2227                 :          0 : static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
    2228                 :            :                                               unsigned long addr, u32 *off,
    2229                 :            :                                               u32 *type)
    2230                 :            : {
    2231                 :            :         const struct bpf_map *map;
    2232                 :            :         int i;
    2233                 :            : 
    2234         [ #  # ]:          0 :         for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
    2235                 :          0 :                 map = prog->aux->used_maps[i];
    2236         [ #  # ]:          0 :                 if (map == (void *)addr) {
    2237                 :          0 :                         *type = BPF_PSEUDO_MAP_FD;
    2238                 :          0 :                         return map;
    2239                 :            :                 }
    2240         [ #  # ]:          0 :                 if (!map->ops->map_direct_value_meta)
    2241                 :          0 :                         continue;
    2242         [ #  # ]:          0 :                 if (!map->ops->map_direct_value_meta(map, addr, off)) {
    2243                 :          0 :                         *type = BPF_PSEUDO_MAP_VALUE;
    2244                 :          0 :                         return map;
    2245                 :            :                 }
    2246                 :            :         }
    2247                 :            : 
    2248                 :            :         return NULL;
    2249                 :            : }
    2250                 :            : 
    2251                 :          0 : static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
    2252                 :            :                                               const struct cred *f_cred)
    2253                 :            : {
    2254                 :            :         const struct bpf_map *map;
    2255                 :            :         struct bpf_insn *insns;
    2256                 :            :         u32 off, type;
    2257                 :            :         u64 imm;
    2258                 :            :         int i;
    2259                 :            : 
    2260                 :          0 :         insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
    2261                 :            :                         GFP_USER);
    2262         [ #  # ]:          0 :         if (!insns)
    2263                 :            :                 return insns;
    2264                 :            : 
    2265         [ #  # ]:          0 :         for (i = 0; i < prog->len; i++) {
    2266         [ #  # ]:          0 :                 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
    2267                 :          0 :                         insns[i].code = BPF_JMP | BPF_CALL;
    2268                 :          0 :                         insns[i].imm = BPF_FUNC_tail_call;
    2269                 :            :                         /* fall-through */
    2270                 :            :                 }
    2271         [ #  # ]:          0 :                 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
    2272                 :            :                     insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
    2273         [ #  # ]:          0 :                         if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
    2274                 :          0 :                                 insns[i].code = BPF_JMP | BPF_CALL;
    2275         [ #  # ]:          0 :                         if (!bpf_dump_raw_ok(f_cred))
    2276                 :          0 :                                 insns[i].imm = 0;
    2277                 :          0 :                         continue;
    2278                 :            :                 }
    2279                 :            : 
    2280         [ #  # ]:          0 :                 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
    2281                 :          0 :                         continue;
    2282                 :            : 
    2283                 :          0 :                 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
    2284                 :          0 :                 map = bpf_map_from_imm(prog, imm, &off, &type);
    2285         [ #  # ]:          0 :                 if (map) {
    2286                 :          0 :                         insns[i].src_reg = type;
    2287                 :          0 :                         insns[i].imm = map->id;
    2288                 :          0 :                         insns[i + 1].imm = off;
    2289                 :          0 :                         continue;
    2290                 :            :                 }
    2291                 :            :         }
    2292                 :            : 
    2293                 :            :         return insns;
    2294                 :            : }
    2295                 :            : 
    2296                 :          0 : static int set_info_rec_size(struct bpf_prog_info *info)
    2297                 :            : {
    2298                 :            :         /*
    2299                 :            :          * Ensure info.*_rec_size is the same as kernel expected size
    2300                 :            :          *
    2301                 :            :          * or
    2302                 :            :          *
    2303                 :            :          * Only allow zero *_rec_size if both _rec_size and _cnt are
    2304                 :            :          * zero.  In this case, the kernel will set the expected
    2305                 :            :          * _rec_size back to the info.
    2306                 :            :          */
    2307                 :            : 
    2308   [ #  #  #  #  :          0 :         if ((info->nr_func_info || info->func_info_rec_size) &&
                   #  # ]
    2309                 :          0 :             info->func_info_rec_size != sizeof(struct bpf_func_info))
    2310                 :            :                 return -EINVAL;
    2311                 :            : 
    2312   [ #  #  #  #  :          0 :         if ((info->nr_line_info || info->line_info_rec_size) &&
                   #  # ]
    2313                 :          0 :             info->line_info_rec_size != sizeof(struct bpf_line_info))
    2314                 :            :                 return -EINVAL;
    2315                 :            : 
    2316   [ #  #  #  #  :          0 :         if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
                   #  # ]
    2317                 :          0 :             info->jited_line_info_rec_size != sizeof(__u64))
    2318                 :            :                 return -EINVAL;
    2319                 :            : 
    2320                 :          0 :         info->func_info_rec_size = sizeof(struct bpf_func_info);
    2321                 :          0 :         info->line_info_rec_size = sizeof(struct bpf_line_info);
    2322                 :          0 :         info->jited_line_info_rec_size = sizeof(__u64);
    2323                 :            : 
    2324                 :          0 :         return 0;
    2325                 :            : }
    2326                 :            : 
    2327                 :          0 : static int bpf_prog_get_info_by_fd(struct file *file,
    2328                 :            :                                    struct bpf_prog *prog,
    2329                 :            :                                    const union bpf_attr *attr,
    2330                 :            :                                    union bpf_attr __user *uattr)
    2331                 :            : {
    2332                 :          0 :         struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
    2333                 :            :         struct bpf_prog_info info;
    2334                 :          0 :         u32 info_len = attr->info.info_len;
    2335                 :            :         struct bpf_prog_stats stats;
    2336                 :            :         char __user *uinsns;
    2337                 :            :         u32 ulen;
    2338                 :            :         int err;
    2339                 :            : 
    2340                 :          0 :         err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
    2341         [ #  # ]:          0 :         if (err)
    2342                 :            :                 return err;
    2343                 :          0 :         info_len = min_t(u32, sizeof(info), info_len);
    2344                 :            : 
    2345                 :          0 :         memset(&info, 0, sizeof(info));
    2346         [ #  # ]:          0 :         if (copy_from_user(&info, uinfo, info_len))
    2347                 :            :                 return -EFAULT;
    2348                 :            : 
    2349                 :          0 :         info.type = prog->type;
    2350                 :          0 :         info.id = prog->aux->id;
    2351                 :          0 :         info.load_time = prog->aux->load_time;
    2352                 :          0 :         info.created_by_uid = from_kuid_munged(current_user_ns(),
    2353                 :          0 :                                                prog->aux->user->uid);
    2354                 :          0 :         info.gpl_compatible = prog->gpl_compatible;
    2355                 :            : 
    2356                 :          0 :         memcpy(info.tag, prog->tag, sizeof(prog->tag));
    2357                 :          0 :         memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
    2358                 :            : 
    2359                 :          0 :         ulen = info.nr_map_ids;
    2360                 :          0 :         info.nr_map_ids = prog->aux->used_map_cnt;
    2361                 :          0 :         ulen = min_t(u32, info.nr_map_ids, ulen);
    2362         [ #  # ]:          0 :         if (ulen) {
    2363                 :          0 :                 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
    2364                 :            :                 u32 i;
    2365                 :            : 
    2366         [ #  # ]:          0 :                 for (i = 0; i < ulen; i++)
    2367         [ #  # ]:          0 :                         if (put_user(prog->aux->used_maps[i]->id,
    2368                 :            :                                      &user_map_ids[i]))
    2369                 :            :                                 return -EFAULT;
    2370                 :            :         }
    2371                 :            : 
    2372                 :          0 :         err = set_info_rec_size(&info);
    2373         [ #  # ]:          0 :         if (err)
    2374                 :            :                 return err;
    2375                 :            : 
    2376                 :          0 :         bpf_prog_get_stats(prog, &stats);
    2377                 :          0 :         info.run_time_ns = stats.nsecs;
    2378                 :          0 :         info.run_cnt = stats.cnt;
    2379                 :            : 
    2380         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN)) {
    2381                 :          0 :                 info.jited_prog_len = 0;
    2382                 :          0 :                 info.xlated_prog_len = 0;
    2383                 :          0 :                 info.nr_jited_ksyms = 0;
    2384                 :          0 :                 info.nr_jited_func_lens = 0;
    2385                 :          0 :                 info.nr_func_info = 0;
    2386                 :          0 :                 info.nr_line_info = 0;
    2387                 :          0 :                 info.nr_jited_line_info = 0;
    2388                 :          0 :                 goto done;
    2389                 :            :         }
    2390                 :            : 
    2391                 :          0 :         ulen = info.xlated_prog_len;
    2392                 :          0 :         info.xlated_prog_len = bpf_prog_insn_size(prog);
    2393   [ #  #  #  # ]:          0 :         if (info.xlated_prog_len && ulen) {
    2394                 :            :                 struct bpf_insn *insns_sanitized;
    2395                 :            :                 bool fault;
    2396                 :            : 
    2397   [ #  #  #  # ]:          0 :                 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
    2398                 :          0 :                         info.xlated_prog_insns = 0;
    2399                 :          0 :                         goto done;
    2400                 :            :                 }
    2401                 :          0 :                 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
    2402         [ #  # ]:          0 :                 if (!insns_sanitized)
    2403                 :            :                         return -ENOMEM;
    2404                 :          0 :                 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
    2405                 :          0 :                 ulen = min_t(u32, info.xlated_prog_len, ulen);
    2406                 :            :                 fault = copy_to_user(uinsns, insns_sanitized, ulen);
    2407                 :          0 :                 kfree(insns_sanitized);
    2408         [ #  # ]:          0 :                 if (fault)
    2409                 :            :                         return -EFAULT;
    2410                 :            :         }
    2411                 :            : 
    2412         [ #  # ]:          0 :         if (bpf_prog_is_dev_bound(prog->aux)) {
    2413                 :          0 :                 err = bpf_prog_offload_info_fill(&info, prog);
    2414         [ #  # ]:          0 :                 if (err)
    2415                 :            :                         return err;
    2416                 :            :                 goto done;
    2417                 :            :         }
    2418                 :            : 
    2419                 :            :         /* NOTE: the following code is supposed to be skipped for offload.
    2420                 :            :          * bpf_prog_offload_info_fill() is the place to fill similar fields
    2421                 :            :          * for offload.
    2422                 :            :          */
    2423                 :          0 :         ulen = info.jited_prog_len;
    2424         [ #  # ]:          0 :         if (prog->aux->func_cnt) {
    2425                 :            :                 u32 i;
    2426                 :            : 
    2427                 :          0 :                 info.jited_prog_len = 0;
    2428         [ #  # ]:          0 :                 for (i = 0; i < prog->aux->func_cnt; i++)
    2429                 :          0 :                         info.jited_prog_len += prog->aux->func[i]->jited_len;
    2430                 :            :         } else {
    2431                 :          0 :                 info.jited_prog_len = prog->jited_len;
    2432                 :            :         }
    2433                 :            : 
    2434   [ #  #  #  # ]:          0 :         if (info.jited_prog_len && ulen) {
    2435         [ #  # ]:          0 :                 if (bpf_dump_raw_ok(file->f_cred)) {
    2436                 :          0 :                         uinsns = u64_to_user_ptr(info.jited_prog_insns);
    2437                 :          0 :                         ulen = min_t(u32, info.jited_prog_len, ulen);
    2438                 :            : 
    2439                 :            :                         /* for multi-function programs, copy the JITed
    2440                 :            :                          * instructions for all the functions
    2441                 :            :                          */
    2442         [ #  # ]:          0 :                         if (prog->aux->func_cnt) {
    2443                 :            :                                 u32 len, free, i;
    2444                 :            :                                 u8 *img;
    2445                 :            : 
    2446                 :            :                                 free = ulen;
    2447         [ #  # ]:          0 :                                 for (i = 0; i < prog->aux->func_cnt; i++) {
    2448                 :          0 :                                         len = prog->aux->func[i]->jited_len;
    2449                 :          0 :                                         len = min_t(u32, len, free);
    2450                 :          0 :                                         img = (u8 *) prog->aux->func[i]->bpf_func;
    2451         [ #  # ]:          0 :                                         if (copy_to_user(uinsns, img, len))
    2452                 :            :                                                 return -EFAULT;
    2453                 :          0 :                                         uinsns += len;
    2454                 :          0 :                                         free -= len;
    2455         [ #  # ]:          0 :                                         if (!free)
    2456                 :            :                                                 break;
    2457                 :            :                                 }
    2458                 :            :                         } else {
    2459         [ #  # ]:          0 :                                 if (copy_to_user(uinsns, prog->bpf_func, ulen))
    2460                 :            :                                         return -EFAULT;
    2461                 :            :                         }
    2462                 :            :                 } else {
    2463                 :          0 :                         info.jited_prog_insns = 0;
    2464                 :            :                 }
    2465                 :            :         }
    2466                 :            : 
    2467                 :          0 :         ulen = info.nr_jited_ksyms;
    2468         [ #  # ]:          0 :         info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
    2469         [ #  # ]:          0 :         if (ulen) {
    2470         [ #  # ]:          0 :                 if (bpf_dump_raw_ok(file->f_cred)) {
    2471                 :            :                         unsigned long ksym_addr;
    2472                 :            :                         u64 __user *user_ksyms;
    2473                 :            :                         u32 i;
    2474                 :            : 
    2475                 :            :                         /* copy the address of the kernel symbol
    2476                 :            :                          * corresponding to each function
    2477                 :            :                          */
    2478                 :          0 :                         ulen = min_t(u32, info.nr_jited_ksyms, ulen);
    2479                 :          0 :                         user_ksyms = u64_to_user_ptr(info.jited_ksyms);
    2480         [ #  # ]:          0 :                         if (prog->aux->func_cnt) {
    2481         [ #  # ]:          0 :                                 for (i = 0; i < ulen; i++) {
    2482                 :          0 :                                         ksym_addr = (unsigned long)
    2483                 :          0 :                                                 prog->aux->func[i]->bpf_func;
    2484         [ #  # ]:          0 :                                         if (put_user((u64) ksym_addr,
    2485                 :            :                                                      &user_ksyms[i]))
    2486                 :            :                                                 return -EFAULT;
    2487                 :            :                                 }
    2488                 :            :                         } else {
    2489                 :          0 :                                 ksym_addr = (unsigned long) prog->bpf_func;
    2490         [ #  # ]:          0 :                                 if (put_user((u64) ksym_addr, &user_ksyms[0]))
    2491                 :            :                                         return -EFAULT;
    2492                 :            :                         }
    2493                 :            :                 } else {
    2494                 :          0 :                         info.jited_ksyms = 0;
    2495                 :            :                 }
    2496                 :            :         }
    2497                 :            : 
    2498                 :          0 :         ulen = info.nr_jited_func_lens;
    2499         [ #  # ]:          0 :         info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
    2500         [ #  # ]:          0 :         if (ulen) {
    2501         [ #  # ]:          0 :                 if (bpf_dump_raw_ok(file->f_cred)) {
    2502                 :            :                         u32 __user *user_lens;
    2503                 :            :                         u32 func_len, i;
    2504                 :            : 
    2505                 :            :                         /* copy the JITed image lengths for each function */
    2506                 :          0 :                         ulen = min_t(u32, info.nr_jited_func_lens, ulen);
    2507                 :          0 :                         user_lens = u64_to_user_ptr(info.jited_func_lens);
    2508         [ #  # ]:          0 :                         if (prog->aux->func_cnt) {
    2509         [ #  # ]:          0 :                                 for (i = 0; i < ulen; i++) {
    2510                 :          0 :                                         func_len =
    2511                 :          0 :                                                 prog->aux->func[i]->jited_len;
    2512         [ #  # ]:          0 :                                         if (put_user(func_len, &user_lens[i]))
    2513                 :            :                                                 return -EFAULT;
    2514                 :            :                                 }
    2515                 :            :                         } else {
    2516                 :          0 :                                 func_len = prog->jited_len;
    2517         [ #  # ]:          0 :                                 if (put_user(func_len, &user_lens[0]))
    2518                 :            :                                         return -EFAULT;
    2519                 :            :                         }
    2520                 :            :                 } else {
    2521                 :          0 :                         info.jited_func_lens = 0;
    2522                 :            :                 }
    2523                 :            :         }
    2524                 :            : 
    2525         [ #  # ]:          0 :         if (prog->aux->btf)
    2526                 :          0 :                 info.btf_id = btf_id(prog->aux->btf);
    2527                 :            : 
    2528                 :          0 :         ulen = info.nr_func_info;
    2529                 :          0 :         info.nr_func_info = prog->aux->func_info_cnt;
    2530   [ #  #  #  # ]:          0 :         if (info.nr_func_info && ulen) {
    2531                 :            :                 char __user *user_finfo;
    2532                 :            : 
    2533                 :          0 :                 user_finfo = u64_to_user_ptr(info.func_info);
    2534                 :          0 :                 ulen = min_t(u32, info.nr_func_info, ulen);
    2535         [ #  # ]:          0 :                 if (copy_to_user(user_finfo, prog->aux->func_info,
    2536                 :          0 :                                  info.func_info_rec_size * ulen))
    2537                 :            :                         return -EFAULT;
    2538                 :            :         }
    2539                 :            : 
    2540                 :          0 :         ulen = info.nr_line_info;
    2541                 :          0 :         info.nr_line_info = prog->aux->nr_linfo;
    2542   [ #  #  #  # ]:          0 :         if (info.nr_line_info && ulen) {
    2543                 :            :                 __u8 __user *user_linfo;
    2544                 :            : 
    2545                 :          0 :                 user_linfo = u64_to_user_ptr(info.line_info);
    2546                 :          0 :                 ulen = min_t(u32, info.nr_line_info, ulen);
    2547         [ #  # ]:          0 :                 if (copy_to_user(user_linfo, prog->aux->linfo,
    2548                 :          0 :                                  info.line_info_rec_size * ulen))
    2549                 :            :                         return -EFAULT;
    2550                 :            :         }
    2551                 :            : 
    2552                 :          0 :         ulen = info.nr_jited_line_info;
    2553         [ #  # ]:          0 :         if (prog->aux->jited_linfo)
    2554                 :          0 :                 info.nr_jited_line_info = prog->aux->nr_linfo;
    2555                 :            :         else
    2556                 :          0 :                 info.nr_jited_line_info = 0;
    2557   [ #  #  #  # ]:          0 :         if (info.nr_jited_line_info && ulen) {
    2558         [ #  # ]:          0 :                 if (bpf_dump_raw_ok(file->f_cred)) {
    2559                 :            :                         __u64 __user *user_linfo;
    2560                 :            :                         u32 i;
    2561                 :            : 
    2562                 :          0 :                         user_linfo = u64_to_user_ptr(info.jited_line_info);
    2563                 :          0 :                         ulen = min_t(u32, info.nr_jited_line_info, ulen);
    2564         [ #  # ]:          0 :                         for (i = 0; i < ulen; i++) {
    2565         [ #  # ]:          0 :                                 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
    2566                 :            :                                              &user_linfo[i]))
    2567                 :            :                                         return -EFAULT;
    2568                 :            :                         }
    2569                 :            :                 } else {
    2570                 :          0 :                         info.jited_line_info = 0;
    2571                 :            :                 }
    2572                 :            :         }
    2573                 :            : 
    2574                 :          0 :         ulen = info.nr_prog_tags;
    2575         [ #  # ]:          0 :         info.nr_prog_tags = prog->aux->func_cnt ? : 1;
    2576         [ #  # ]:          0 :         if (ulen) {
    2577                 :            :                 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
    2578                 :            :                 u32 i;
    2579                 :            : 
    2580                 :          0 :                 user_prog_tags = u64_to_user_ptr(info.prog_tags);
    2581                 :          0 :                 ulen = min_t(u32, info.nr_prog_tags, ulen);
    2582         [ #  # ]:          0 :                 if (prog->aux->func_cnt) {
    2583         [ #  # ]:          0 :                         for (i = 0; i < ulen; i++) {
    2584         [ #  # ]:          0 :                                 if (copy_to_user(user_prog_tags[i],
    2585                 :          0 :                                                  prog->aux->func[i]->tag,
    2586                 :            :                                                  BPF_TAG_SIZE))
    2587                 :            :                                         return -EFAULT;
    2588                 :            :                         }
    2589                 :            :                 } else {
    2590         [ #  # ]:          0 :                         if (copy_to_user(user_prog_tags[0],
    2591                 :          0 :                                          prog->tag, BPF_TAG_SIZE))
    2592                 :            :                                 return -EFAULT;
    2593                 :            :                 }
    2594                 :            :         }
    2595                 :            : 
    2596                 :            : done:
    2597   [ #  #  #  # ]:          0 :         if (copy_to_user(uinfo, &info, info_len) ||
    2598                 :          0 :             put_user(info_len, &uattr->info.info_len))
    2599                 :            :                 return -EFAULT;
    2600                 :            : 
    2601                 :            :         return 0;
    2602                 :            : }
    2603                 :            : 
    2604                 :          0 : static int bpf_map_get_info_by_fd(struct file *file,
    2605                 :            :                                   struct bpf_map *map,
    2606                 :            :                                   const union bpf_attr *attr,
    2607                 :            :                                   union bpf_attr __user *uattr)
    2608                 :            : {
    2609                 :          0 :         struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
    2610                 :            :         struct bpf_map_info info;
    2611                 :          0 :         u32 info_len = attr->info.info_len;
    2612                 :            :         int err;
    2613                 :            : 
    2614                 :          0 :         err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
    2615         [ #  # ]:          0 :         if (err)
    2616                 :            :                 return err;
    2617                 :          0 :         info_len = min_t(u32, sizeof(info), info_len);
    2618                 :            : 
    2619                 :          0 :         memset(&info, 0, sizeof(info));
    2620                 :          0 :         info.type = map->map_type;
    2621                 :          0 :         info.id = map->id;
    2622                 :          0 :         info.key_size = map->key_size;
    2623                 :          0 :         info.value_size = map->value_size;
    2624                 :          0 :         info.max_entries = map->max_entries;
    2625                 :          0 :         info.map_flags = map->map_flags;
    2626                 :          0 :         memcpy(info.name, map->name, sizeof(map->name));
    2627                 :            : 
    2628         [ #  # ]:          0 :         if (map->btf) {
    2629                 :          0 :                 info.btf_id = btf_id(map->btf);
    2630                 :          0 :                 info.btf_key_type_id = map->btf_key_type_id;
    2631                 :          0 :                 info.btf_value_type_id = map->btf_value_type_id;
    2632                 :            :         }
    2633                 :            : 
    2634         [ #  # ]:          0 :         if (bpf_map_is_dev_bound(map)) {
    2635                 :          0 :                 err = bpf_map_offload_info_fill(&info, map);
    2636         [ #  # ]:          0 :                 if (err)
    2637                 :            :                         return err;
    2638                 :            :         }
    2639                 :            : 
    2640   [ #  #  #  # ]:          0 :         if (copy_to_user(uinfo, &info, info_len) ||
    2641                 :          0 :             put_user(info_len, &uattr->info.info_len))
    2642                 :            :                 return -EFAULT;
    2643                 :            : 
    2644                 :            :         return 0;
    2645                 :            : }
    2646                 :            : 
    2647                 :          0 : static int bpf_btf_get_info_by_fd(struct file *file,
    2648                 :            :                                   struct btf *btf,
    2649                 :            :                                   const union bpf_attr *attr,
    2650                 :            :                                   union bpf_attr __user *uattr)
    2651                 :            : {
    2652                 :          0 :         struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
    2653                 :          0 :         u32 info_len = attr->info.info_len;
    2654                 :            :         int err;
    2655                 :            : 
    2656                 :          0 :         err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
    2657         [ #  # ]:          0 :         if (err)
    2658                 :            :                 return err;
    2659                 :            : 
    2660                 :          0 :         return btf_get_info_by_fd(btf, attr, uattr);
    2661                 :            : }
    2662                 :            : 
    2663                 :            : #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
    2664                 :            : 
    2665                 :          0 : static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
    2666                 :            :                                   union bpf_attr __user *uattr)
    2667                 :            : {
    2668                 :          0 :         int ufd = attr->info.bpf_fd;
    2669                 :            :         struct fd f;
    2670                 :            :         int err;
    2671                 :            : 
    2672         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
    2673                 :            :                 return -EINVAL;
    2674                 :            : 
    2675                 :            :         f = fdget(ufd);
    2676         [ #  # ]:          0 :         if (!f.file)
    2677                 :            :                 return -EBADFD;
    2678                 :            : 
    2679         [ #  # ]:          0 :         if (f.file->f_op == &bpf_prog_fops)
    2680                 :          0 :                 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
    2681                 :            :                                               uattr);
    2682         [ #  # ]:          0 :         else if (f.file->f_op == &bpf_map_fops)
    2683                 :          0 :                 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
    2684                 :            :                                              uattr);
    2685         [ #  # ]:          0 :         else if (f.file->f_op == &btf_fops)
    2686                 :          0 :                 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
    2687                 :            :         else
    2688                 :            :                 err = -EINVAL;
    2689                 :            : 
    2690                 :            :         fdput(f);
    2691                 :          0 :         return err;
    2692                 :            : }
    2693                 :            : 
    2694                 :            : #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
    2695                 :            : 
    2696                 :          0 : static int bpf_btf_load(const union bpf_attr *attr)
    2697                 :            : {
    2698         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_BTF_LOAD))
    2699                 :            :                 return -EINVAL;
    2700                 :            : 
    2701         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2702                 :            :                 return -EPERM;
    2703                 :            : 
    2704                 :          0 :         return btf_new_fd(attr);
    2705                 :            : }
    2706                 :            : 
    2707                 :            : #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
    2708                 :            : 
    2709                 :          0 : static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
    2710                 :            : {
    2711         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
    2712                 :            :                 return -EINVAL;
    2713                 :            : 
    2714         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2715                 :            :                 return -EPERM;
    2716                 :            : 
    2717                 :          0 :         return btf_get_fd_by_id(attr->btf_id);
    2718                 :            : }
    2719                 :            : 
    2720                 :          0 : static int bpf_task_fd_query_copy(const union bpf_attr *attr,
    2721                 :            :                                     union bpf_attr __user *uattr,
    2722                 :            :                                     u32 prog_id, u32 fd_type,
    2723                 :            :                                     const char *buf, u64 probe_offset,
    2724                 :            :                                     u64 probe_addr)
    2725                 :            : {
    2726                 :          0 :         char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
    2727         [ #  # ]:          0 :         u32 len = buf ? strlen(buf) : 0, input_len;
    2728                 :            :         int err = 0;
    2729                 :            : 
    2730         [ #  # ]:          0 :         if (put_user(len, &uattr->task_fd_query.buf_len))
    2731                 :            :                 return -EFAULT;
    2732                 :          0 :         input_len = attr->task_fd_query.buf_len;
    2733         [ #  # ]:          0 :         if (input_len && ubuf) {
    2734         [ #  # ]:          0 :                 if (!len) {
    2735                 :            :                         /* nothing to copy, just make ubuf NULL terminated */
    2736                 :            :                         char zero = '\0';
    2737                 :            : 
    2738         [ #  # ]:          0 :                         if (put_user(zero, ubuf))
    2739                 :            :                                 return -EFAULT;
    2740         [ #  # ]:          0 :                 } else if (input_len >= len + 1) {
    2741                 :            :                         /* ubuf can hold the string with NULL terminator */
    2742         [ #  # ]:          0 :                         if (copy_to_user(ubuf, buf, len + 1))
    2743                 :            :                                 return -EFAULT;
    2744                 :            :                 } else {
    2745                 :            :                         /* ubuf cannot hold the string with NULL terminator,
    2746                 :            :                          * do a partial copy with NULL terminator.
    2747                 :            :                          */
    2748                 :            :                         char zero = '\0';
    2749                 :            : 
    2750                 :            :                         err = -ENOSPC;
    2751         [ #  # ]:          0 :                         if (copy_to_user(ubuf, buf, input_len - 1))
    2752                 :            :                                 return -EFAULT;
    2753         [ #  # ]:          0 :                         if (put_user(zero, ubuf + input_len - 1))
    2754                 :            :                                 return -EFAULT;
    2755                 :            :                 }
    2756                 :            :         }
    2757                 :            : 
    2758   [ #  #  #  # ]:          0 :         if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
    2759         [ #  # ]:          0 :             put_user(fd_type, &uattr->task_fd_query.fd_type) ||
    2760         [ #  # ]:          0 :             put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
    2761                 :          0 :             put_user(probe_addr, &uattr->task_fd_query.probe_addr))
    2762                 :            :                 return -EFAULT;
    2763                 :            : 
    2764                 :          0 :         return err;
    2765                 :            : }
    2766                 :            : 
    2767                 :            : #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
    2768                 :            : 
    2769                 :          0 : static int bpf_task_fd_query(const union bpf_attr *attr,
    2770                 :            :                              union bpf_attr __user *uattr)
    2771                 :            : {
    2772                 :          0 :         pid_t pid = attr->task_fd_query.pid;
    2773                 :          0 :         u32 fd = attr->task_fd_query.fd;
    2774                 :            :         const struct perf_event *event;
    2775                 :            :         struct files_struct *files;
    2776                 :            :         struct task_struct *task;
    2777                 :            :         struct file *file;
    2778                 :            :         int err;
    2779                 :            : 
    2780         [ #  # ]:          0 :         if (CHECK_ATTR(BPF_TASK_FD_QUERY))
    2781                 :            :                 return -EINVAL;
    2782                 :            : 
    2783         [ #  # ]:          0 :         if (!capable(CAP_SYS_ADMIN))
    2784                 :            :                 return -EPERM;
    2785                 :            : 
    2786         [ #  # ]:          0 :         if (attr->task_fd_query.flags != 0)
    2787                 :            :                 return -EINVAL;
    2788                 :            : 
    2789                 :          0 :         task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
    2790         [ #  # ]:          0 :         if (!task)
    2791                 :            :                 return -ENOENT;
    2792                 :            : 
    2793                 :          0 :         files = get_files_struct(task);
    2794                 :          0 :         put_task_struct(task);
    2795         [ #  # ]:          0 :         if (!files)
    2796                 :            :                 return -ENOENT;
    2797                 :            : 
    2798                 :            :         err = 0;
    2799                 :            :         spin_lock(&files->file_lock);
    2800                 :            :         file = fcheck_files(files, fd);
    2801         [ #  # ]:          0 :         if (!file)
    2802                 :            :                 err = -EBADF;
    2803                 :            :         else
    2804                 :            :                 get_file(file);
    2805                 :            :         spin_unlock(&files->file_lock);
    2806                 :          0 :         put_files_struct(files);
    2807                 :            : 
    2808         [ #  # ]:          0 :         if (err)
    2809                 :            :                 goto out;
    2810                 :            : 
    2811         [ #  # ]:          0 :         if (file->f_op == &bpf_raw_tp_fops) {
    2812                 :          0 :                 struct bpf_raw_tracepoint *raw_tp = file->private_data;
    2813                 :          0 :                 struct bpf_raw_event_map *btp = raw_tp->btp;
    2814                 :            : 
    2815                 :          0 :                 err = bpf_task_fd_query_copy(attr, uattr,
    2816                 :          0 :                                              raw_tp->prog->aux->id,
    2817                 :            :                                              BPF_FD_TYPE_RAW_TRACEPOINT,
    2818                 :          0 :                                              btp->tp->name, 0, 0);
    2819                 :          0 :                 goto put_file;
    2820                 :            :         }
    2821                 :            : 
    2822                 :          0 :         event = perf_get_event(file);
    2823         [ #  # ]:          0 :         if (!IS_ERR(event)) {
    2824                 :            :                 u64 probe_offset, probe_addr;
    2825                 :            :                 u32 prog_id, fd_type;
    2826                 :            :                 const char *buf;
    2827                 :            : 
    2828                 :          0 :                 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
    2829                 :            :                                               &buf, &probe_offset,
    2830                 :            :                                               &probe_addr);
    2831         [ #  # ]:          0 :                 if (!err)
    2832                 :          0 :                         err = bpf_task_fd_query_copy(attr, uattr, prog_id,
    2833                 :            :                                                      fd_type, buf,
    2834                 :            :                                                      probe_offset,
    2835                 :            :                                                      probe_addr);
    2836                 :            :                 goto put_file;
    2837                 :            :         }
    2838                 :            : 
    2839                 :            :         err = -ENOTSUPP;
    2840                 :            : put_file:
    2841                 :          0 :         fput(file);
    2842                 :            : out:
    2843                 :          0 :         return err;
    2844                 :            : }
    2845                 :            : 
    2846                 :      12006 : SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
    2847                 :            : {
    2848                 :            :         union bpf_attr attr;
    2849                 :            :         int err;
    2850                 :            : 
    2851   [ -  +  #  # ]:       6003 :         if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
    2852                 :            :                 return -EPERM;
    2853                 :            : 
    2854                 :       6003 :         err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
    2855         [ +  - ]:       6003 :         if (err)
    2856                 :            :                 return err;
    2857                 :       6003 :         size = min_t(u32, size, sizeof(attr));
    2858                 :            : 
    2859                 :            :         /* copy attributes from user space, may be less than sizeof(bpf_attr) */
    2860                 :       6003 :         memset(&attr, 0, sizeof(attr));
    2861         [ +  - ]:       6003 :         if (copy_from_user(&attr, uattr, size) != 0)
    2862                 :            :                 return -EFAULT;
    2863                 :            : 
    2864                 :       6003 :         err = security_bpf(cmd, &attr, size);
    2865         [ +  - ]:       6003 :         if (err < 0)
    2866                 :            :                 return err;
    2867                 :            : 
    2868   [ +  -  +  -  :       6003 :         switch (cmd) {
          -  -  +  -  -  
          +  +  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
                      - ]
    2869                 :            :         case BPF_MAP_CREATE:
    2870                 :       1449 :                 err = map_create(&attr);
    2871                 :       1449 :                 break;
    2872                 :            :         case BPF_MAP_LOOKUP_ELEM:
    2873                 :          0 :                 err = map_lookup_elem(&attr);
    2874                 :          0 :                 break;
    2875                 :            :         case BPF_MAP_UPDATE_ELEM:
    2876                 :       1242 :                 err = map_update_elem(&attr);
    2877                 :       1242 :                 break;
    2878                 :            :         case BPF_MAP_DELETE_ELEM:
    2879                 :          0 :                 err = map_delete_elem(&attr);
    2880                 :          0 :                 break;
    2881                 :            :         case BPF_MAP_GET_NEXT_KEY:
    2882                 :          0 :                 err = map_get_next_key(&attr);
    2883                 :          0 :                 break;
    2884                 :            :         case BPF_MAP_FREEZE:
    2885                 :          0 :                 err = map_freeze(&attr);
    2886                 :          0 :                 break;
    2887                 :            :         case BPF_PROG_LOAD:
    2888                 :       1656 :                 err = bpf_prog_load(&attr, uattr);
    2889                 :       1656 :                 break;
    2890                 :            :         case BPF_OBJ_PIN:
    2891                 :          0 :                 err = bpf_obj_pin(&attr);
    2892                 :          0 :                 break;
    2893                 :            :         case BPF_OBJ_GET:
    2894                 :          0 :                 err = bpf_obj_get(&attr);
    2895                 :          0 :                 break;
    2896                 :            :         case BPF_PROG_ATTACH:
    2897                 :       1449 :                 err = bpf_prog_attach(&attr);
    2898                 :       1449 :                 break;
    2899                 :            :         case BPF_PROG_DETACH:
    2900                 :        207 :                 err = bpf_prog_detach(&attr);
    2901                 :        207 :                 break;
    2902                 :            :         case BPF_PROG_QUERY:
    2903                 :          0 :                 err = bpf_prog_query(&attr, uattr);
    2904                 :          0 :                 break;
    2905                 :            :         case BPF_PROG_TEST_RUN:
    2906                 :          0 :                 err = bpf_prog_test_run(&attr, uattr);
    2907                 :          0 :                 break;
    2908                 :            :         case BPF_PROG_GET_NEXT_ID:
    2909                 :          0 :                 err = bpf_obj_get_next_id(&attr, uattr,
    2910                 :            :                                           &prog_idr, &prog_idr_lock);
    2911                 :          0 :                 break;
    2912                 :            :         case BPF_MAP_GET_NEXT_ID:
    2913                 :          0 :                 err = bpf_obj_get_next_id(&attr, uattr,
    2914                 :            :                                           &map_idr, &map_idr_lock);
    2915                 :          0 :                 break;
    2916                 :            :         case BPF_BTF_GET_NEXT_ID:
    2917                 :          0 :                 err = bpf_obj_get_next_id(&attr, uattr,
    2918                 :            :                                           &btf_idr, &btf_idr_lock);
    2919                 :          0 :                 break;
    2920                 :            :         case BPF_PROG_GET_FD_BY_ID:
    2921                 :          0 :                 err = bpf_prog_get_fd_by_id(&attr);
    2922                 :          0 :                 break;
    2923                 :            :         case BPF_MAP_GET_FD_BY_ID:
    2924                 :          0 :                 err = bpf_map_get_fd_by_id(&attr);
    2925                 :          0 :                 break;
    2926                 :            :         case BPF_OBJ_GET_INFO_BY_FD:
    2927                 :          0 :                 err = bpf_obj_get_info_by_fd(&attr, uattr);
    2928                 :          0 :                 break;
    2929                 :            :         case BPF_RAW_TRACEPOINT_OPEN:
    2930                 :          0 :                 err = bpf_raw_tracepoint_open(&attr);
    2931                 :          0 :                 break;
    2932                 :            :         case BPF_BTF_LOAD:
    2933                 :          0 :                 err = bpf_btf_load(&attr);
    2934                 :          0 :                 break;
    2935                 :            :         case BPF_BTF_GET_FD_BY_ID:
    2936                 :          0 :                 err = bpf_btf_get_fd_by_id(&attr);
    2937                 :          0 :                 break;
    2938                 :            :         case BPF_TASK_FD_QUERY:
    2939                 :          0 :                 err = bpf_task_fd_query(&attr, uattr);
    2940                 :          0 :                 break;
    2941                 :            :         case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
    2942                 :          0 :                 err = map_lookup_and_delete_elem(&attr);
    2943                 :          0 :                 break;
    2944                 :            :         default:
    2945                 :            :                 err = -EINVAL;
    2946                 :            :                 break;
    2947                 :            :         }
    2948                 :            : 
    2949                 :       6003 :         return err;
    2950                 :            : }

Generated by: LCOV version 1.14