History

Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next

Alexei Starovoitov says: ==================== pull-request: bpf-next 2021-12-30 The following pull-request contains BPF updates for your *net-next* tree. We've added 72 non-merge commits during the last 20 day(s) which contain a total of 223 files changed, 3510 insertions(+), 1591 deletions(-). The main changes are: 1) Automatic setrlimit in libbpf when bpf is memcg's in the kernel, from Andrii. 2) Beautify and de-verbose verifier logs, from Christy. 3) Composable verifier types, from Hao. 4) bpf_strncmp helper, from Hou. 5) bpf.h header dependency cleanup, from Jakub. 6) get_func_[arg|ret|arg_cnt] helpers, from Jiri. 7) Sleepable local storage, from KP. 8) Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support, from Kumar. ====================

David S. Miller committed 4 years ago

Commit e63a0232 parents ce2b6eb 9e6b19a221 files · +3506 −1591Browse files

Showing the changes to kernel (23 of 221 files). Show all

Unified · Split

23 files
  1. kernel/bpf/bloom_filter.c +6 −0
  2. kernel/bpf/bpf_inode_storage.c +5 −1
  3. kernel/bpf/bpf_local_storage.c +37 −13
  4. kernel/bpf/bpf_task_storage.c +5 −1
  5. kernel/bpf/btf.c +88 −36
  6. kernel/bpf/cgroup.c +1 −1
  7. kernel/bpf/cpumap.c +2 −2
  8. kernel/bpf/devmap.c +2 −2
  9. kernel/bpf/helpers.c +23 −6
  10. kernel/bpf/local_storage.c +1 −2
  11. kernel/bpf/lpm_trie.c +1 −1
  12. kernel/bpf/map_iter.c +2 −2
  13. kernel/bpf/net_namespace.c +1 −0
  14. kernel/bpf/reuseport_array.c +1 −5
  15. kernel/bpf/ringbuf.c +1 −1
  16. kernel/bpf/syscall.c +2 −1
  17. kernel/bpf/trampoline.c +8 −0
  18. kernel/bpf/verifier.c +415 −281
  19. kernel/cgroup/cgroup.c +1 −0
  20. kernel/sysctl.c +1 −0
  21. kernel/trace/bpf_trace.c +67 −14
  22. kernel/trace/trace_kprobe.c +1 −0
  23. kernel/trace/trace_uprobe.c +1 −0
kernel/bpf/bloom_filter.c+6−0
@@ -82,6 +82,11 @@ static int bloom_map_delete_elem(struct bpf_map *map, void *value) · 81 unmodified lines
8282 return -EOPNOTSUPP;
8383 }
8484
85+static int bloom_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
86+{
87+ return -EOPNOTSUPP;
88+}
89+
8590 static struct bpf_map *bloom_map_alloc(union bpf_attr *attr)
8691 {
8792 u32 bitset_bytes, bitset_mask, nr_hash_funcs, nr_bits;
@@ -192,6 +197,7 @@ const struct bpf_map_ops bloom_filter_map_ops = { · 104 unmodified lines
192197 .map_meta_equal = bpf_map_meta_equal,
193198 .map_alloc = bloom_map_alloc,
194199 .map_free = bloom_map_free,
200+ .map_get_next_key = bloom_map_get_next_key,
195201 .map_push_elem = bloom_map_push_elem,
196202 .map_peek_elem = bloom_map_peek_elem,
197203 .map_pop_elem = bloom_map_pop_elem,
kernel/bpf/bpf_inode_storage.c+5−1
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 #include <linux/bpf_lsm.h>
1818 #include <linux/btf_ids.h>
1919 #include <linux/fdtable.h>
20+#include <linux/rcupdate_trace.h>
2021
2122 DEFINE_BPF_STORAGE_CACHE(inode_cache);
2223
@@ -44,7 +45,8 @@ static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode, · 21 unmodified lines
4445 if (!bsb)
4546 return NULL;
4647
47− inode_storage = rcu_dereference(bsb->storage);
48+ inode_storage =
49+ rcu_dereference_check(bsb->storage, bpf_rcu_lock_held());
4850 if (!inode_storage)
4951 return NULL;
5052
@@ -172,6 +174,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · 121 unmodified lines
172174 {
173175 struct bpf_local_storage_data *sdata;
174176
177+ WARN_ON_ONCE(!bpf_rcu_lock_held());
175178 if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
176179 return (unsigned long)NULL;
177180
@@ -204,6 +207,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · 26 unmodified lines
204207 BPF_CALL_2(bpf_inode_storage_delete,
205208 struct bpf_map *, map, struct inode *, inode)
206209 {
210+ WARN_ON_ONCE(!bpf_rcu_lock_held());
207211 if (!inode)
208212 return -EINVAL;
209213
kernel/bpf/bpf_local_storage.c+37−13
@@ -11,6 +11,9 @@ · 10 unmodified lines
1111 #include <net/sock.h>
1212 #include <uapi/linux/sock_diag.h>
1313 #include <uapi/linux/btf.h>
14+#include <linux/rcupdate.h>
15+#include <linux/rcupdate_trace.h>
16+#include <linux/rcupdate_wait.h>
1417
1518 #define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE)
1619
@@ -81,6 +84,22 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, · 64 unmodified lines
8184 return NULL;
8285 }
8386
87+void bpf_local_storage_free_rcu(struct rcu_head *rcu)
88+{
89+ struct bpf_local_storage *local_storage;
90+
91+ local_storage = container_of(rcu, struct bpf_local_storage, rcu);
92+ kfree_rcu(local_storage, rcu);
93+}
94+
95+static void bpf_selem_free_rcu(struct rcu_head *rcu)
96+{
97+ struct bpf_local_storage_elem *selem;
98+
99+ selem = container_of(rcu, struct bpf_local_storage_elem, rcu);
100+ kfree_rcu(selem, rcu);
101+}
102+
84103 /* local_storage->lock must be held and selem->local_storage == local_storage.
85104 * The caller must ensure selem->smap is still valid to be
86105 * dereferenced for its smap->elem_size and smap->cache_idx.
@@ -93,7 +112,7 @@ bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage, · 6 unmodified lines
93112 bool free_local_storage;
94113 void *owner;
95114
96− smap = rcu_dereference(SDATA(selem)->smap);
115+ smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
97116 owner = local_storage->owner;
98117
99118 /* All uncharging on the owner must be done first.
@@ -118,12 +137,12 @@ bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage, · 18 unmodified lines
118137 *
119138 * Although the unlock will be done under
120139 * rcu_read_lock(), it is more intutivie to
121− * read if kfree_rcu(local_storage, rcu) is done
140+ * read if the freeing of the storage is done
122141 * after the raw_spin_unlock_bh(&local_storage->lock).
123142 *
124143 * Hence, a "bool free_local_storage" is returned
125− * to the caller which then calls the kfree_rcu()
126− * after unlock.
144+ * to the caller which then calls then frees the storage after
145+ * all the RCU grace periods have expired.
127146 */
128147 }
129148 hlist_del_init_rcu(&selem->snode);
@@ -131,8 +150,7 @@ bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage, · 1 unmodified line
131150 SDATA(selem))
132151 RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL);
133152
134− kfree_rcu(selem, rcu);
135−
153+ call_rcu_tasks_trace(&selem->rcu, bpf_selem_free_rcu);
136154 return free_local_storage;
137155 }
138156
@@ -146,7 +164,8 @@ static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem) · 7 unmodified lines
146164 /* selem has already been unlinked from sk */
147165 return;
148166
149− local_storage = rcu_dereference(selem->local_storage);
167+ local_storage = rcu_dereference_check(selem->local_storage,
168+ bpf_rcu_lock_held());
150169 raw_spin_lock_irqsave(&local_storage->lock, flags);
151170 if (likely(selem_linked_to_storage(selem)))
152171 free_local_storage = bpf_selem_unlink_storage_nolock(
@@ -154,7 +173,8 @@ static void __bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem) · 1 unmodified line
154173 raw_spin_unlock_irqrestore(&local_storage->lock, flags);
155174
156175 if (free_local_storage)
157− kfree_rcu(local_storage, rcu);
176+ call_rcu_tasks_trace(&local_storage->rcu,
177+ bpf_local_storage_free_rcu);
158178 }
159179
160180 void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
@@ -174,7 +194,7 @@ void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem) · 13 unmodified lines
174194 /* selem has already be unlinked from smap */
175195 return;
176196
177− smap = rcu_dereference(SDATA(selem)->smap);
197+ smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
178198 b = select_bucket(smap, selem);
179199 raw_spin_lock_irqsave(&b->lock, flags);
180200 if (likely(selem_linked_to_map(selem)))
@@ -213,12 +233,14 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage, · 32 unmodified lines
213233 struct bpf_local_storage_elem *selem;
214234
215235 /* Fast path (cache hit) */
216− sdata = rcu_dereference(local_storage->cache[smap->cache_idx]);
236+ sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx],
237+ bpf_rcu_lock_held());
217238 if (sdata && rcu_access_pointer(sdata->smap) == smap)
218239 return sdata;
219240
220241 /* Slow path (cache miss) */
221− hlist_for_each_entry_rcu(selem, &local_storage->list, snode)
242+ hlist_for_each_entry_rcu(selem, &local_storage->list, snode,
243+ rcu_read_lock_trace_held())
222244 if (rcu_access_pointer(SDATA(selem)->smap) == smap)
223245 break;
224246
@@ -306,7 +328,8 @@ int bpf_local_storage_alloc(void *owner, · 81 unmodified lines
306328 * bucket->list, first_selem can be freed immediately
307329 * (instead of kfree_rcu) because
308330 * bpf_local_storage_map_free() does a
309− * synchronize_rcu() before walking the bucket->list.
331+ * synchronize_rcu_mult (waiting for both sleepable and
332+ * normal programs) before walking the bucket->list.
310333 * Hence, no one is accessing selem from the
311334 * bucket->list under rcu_read_lock().
312335 */
@@ -342,7 +365,8 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, · 29 unmodified lines
342365 !map_value_has_spin_lock(&smap->map)))
343366 return ERR_PTR(-EINVAL);
344367
345− local_storage = rcu_dereference(*owner_storage(smap, owner));
368+ local_storage = rcu_dereference_check(*owner_storage(smap, owner),
369+ bpf_rcu_lock_held());
346370 if (!local_storage || hlist_empty(&local_storage->list)) {
347371 /* Very first elem for the owner */
348372 err = check_flags(NULL, map_flags);
kernel/bpf/bpf_task_storage.c+5−1
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 #include <uapi/linux/btf.h>
1818 #include <linux/btf_ids.h>
1919 #include <linux/fdtable.h>
20+#include <linux/rcupdate_trace.h>
2021
2122 DEFINE_BPF_STORAGE_CACHE(task_cache);
2223
@@ -59,7 +60,8 @@ task_storage_lookup(struct task_struct *task, struct bpf_map *map, · 36 unmodified lines
5960 struct bpf_local_storage *task_storage;
6061 struct bpf_local_storage_map *smap;
6162
62− task_storage = rcu_dereference(task->bpf_storage);
63+ task_storage =
64+ rcu_dereference_check(task->bpf_storage, bpf_rcu_lock_held());
6365 if (!task_storage)
6466 return NULL;
6567
@@ -229,6 +231,7 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, · 163 unmodified lines
229231 {
230232 struct bpf_local_storage_data *sdata;
231233
234+ WARN_ON_ONCE(!bpf_rcu_lock_held());
232235 if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
233236 return (unsigned long)NULL;
234237
@@ -260,6 +263,7 @@ BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *, · 25 unmodified lines
260263 {
261264 int ret;
262265
266+ WARN_ON_ONCE(!bpf_rcu_lock_held());
263267 if (!task)
264268 return -EINVAL;
265269
kernel/bpf/btf.c+88−36
@@ -4826,7 +4826,7 @@ struct btf *bpf_prog_get_target_btf(const struct bpf_prog *prog) · 4825 unmodified lines
48264826 return prog->aux->attach_btf;
48274827 }
48284828
4829−static bool is_string_ptr(struct btf *btf, const struct btf_type *t)
4829+static bool is_int_ptr(struct btf *btf, const struct btf_type *t)
48304830 {
48314831 /* t comes in already as a pointer */
48324832 t = btf_type_by_id(btf, t->type);
@@ -4835,8 +4835,7 @@ static bool is_string_ptr(struct btf *btf, const struct btf_type *t) · 2 unmodified lines
48354835 if (BTF_INFO_KIND(t->info) == BTF_KIND_CONST)
48364836 t = btf_type_by_id(btf, t->type);
48374837
4838− /* char, signed char, unsigned char */
4839− return btf_type_is_int(t) && t->size == 1;
4838+ return btf_type_is_int(t);
48404839 }
48414840
48424841 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
@@ -4941,10 +4940,12 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, · 98 unmodified lines
49414940 /* check for PTR_TO_RDONLY_BUF_OR_NULL or PTR_TO_RDWR_BUF_OR_NULL */
49424941 for (i = 0; i < prog->aux->ctx_arg_info_size; i++) {
49434942 const struct bpf_ctx_arg_aux *ctx_arg_info = &prog->aux->ctx_arg_info[i];
4943+ u32 type, flag;
49444944
4945− if (ctx_arg_info->offset == off &&
4946− (ctx_arg_info->reg_type == PTR_TO_RDONLY_BUF_OR_NULL ||
4947− ctx_arg_info->reg_type == PTR_TO_RDWR_BUF_OR_NULL)) {
4945+ type = base_type(ctx_arg_info->reg_type);
4946+ flag = type_flag(ctx_arg_info->reg_type);
4947+ if (ctx_arg_info->offset == off && type == PTR_TO_BUF &&
4948+ (flag & PTR_MAYBE_NULL)) {
49484949 info->reg_type = ctx_arg_info->reg_type;
49494950 return true;
49504951 }
@@ -4957,7 +4958,7 @@ bool btf_ctx_access(int off, int size, enum bpf_access_type type, · 6 unmodified lines
49574958 */
49584959 return true;
49594960
4960− if (is_string_ptr(btf, t))
4961+ if (is_int_ptr(btf, t))
49614962 return true;
49624963
49634964 /* this is a pointer to another type */
@@ -5575,12 +5576,53 @@ static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { · 611 unmodified lines
55755576 #endif
55765577 };
55775578
5579+/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */
5580+static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log,
5581+ const struct btf *btf,
5582+ const struct btf_type *t, int rec)
5583+{
5584+ const struct btf_type *member_type;
5585+ const struct btf_member *member;
5586+ u32 i;
5587+
5588+ if (!btf_type_is_struct(t))
5589+ return false;
5590+
5591+ for_each_member(i, t, member) {
5592+ const struct btf_array *array;
5593+
5594+ member_type = btf_type_skip_modifiers(btf, member->type, NULL);
5595+ if (btf_type_is_struct(member_type)) {
5596+ if (rec >= 3) {
5597+ bpf_log(log, "max struct nesting depth exceeded\n");
5598+ return false;
5599+ }
5600+ if (!__btf_type_is_scalar_struct(log, btf, member_type, rec + 1))
5601+ return false;
5602+ continue;
5603+ }
5604+ if (btf_type_is_array(member_type)) {
5605+ array = btf_type_array(member_type);
5606+ if (!array->nelems)
5607+ return false;
5608+ member_type = btf_type_skip_modifiers(btf, array->type, NULL);
5609+ if (!btf_type_is_scalar(member_type))
5610+ return false;
5611+ continue;
5612+ }
5613+ if (!btf_type_is_scalar(member_type))
5614+ return false;
5615+ }
5616+ return true;
5617+}
5618+
55785619 static int btf_check_func_arg_match(struct bpf_verifier_env *env,
55795620 const struct btf *btf, u32 func_id,
55805621 struct bpf_reg_state *regs,
55815622 bool ptr_to_mem_ok)
55825623 {
55835624 struct bpf_verifier_log *log = &env->log;
5625+ bool is_kfunc = btf_is_kernel(btf);
55845626 const char *func_name, *ref_tname;
55855627 const struct btf_type *t, *ref_t;
55865628 const struct btf_param *args;
@@ -5633,7 +5675,20 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, · 46 unmodified lines
56335675
56345676 ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id);
56355677 ref_tname = btf_name_by_offset(btf, ref_t->name_off);
5636− if (btf_is_kernel(btf)) {
5678+ if (btf_get_prog_ctx_type(log, btf, t,
5679+ env->prog->type, i)) {
5680+ /* If function expects ctx type in BTF check that caller
5681+ * is passing PTR_TO_CTX.
5682+ */
5683+ if (reg->type != PTR_TO_CTX) {
5684+ bpf_log(log,
5685+ "arg#%d expected pointer to ctx, but got %s\n",
5686+ i, btf_type_str(t));
5687+ return -EINVAL;
5688+ }
5689+ if (check_ctx_reg(env, reg, regno))
5690+ return -EINVAL;
5691+ } else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {
56375692 const struct btf_type *reg_ref_t;
56385693 const struct btf *reg_btf;
56395694 const char *reg_ref_tname;
@@ -5649,14 +5704,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, · 9 unmodified lines
56495704 if (reg->type == PTR_TO_BTF_ID) {
56505705 reg_btf = reg->btf;
56515706 reg_ref_id = reg->btf_id;
5652− } else if (reg2btf_ids[reg->type]) {
5707+ } else {
56535708 reg_btf = btf_vmlinux;
56545709 reg_ref_id = *reg2btf_ids[reg->type];
5655− } else {
5656− bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d is not a pointer to btf_id\n",
5657− func_name, i,
5658− btf_type_str(ref_t), ref_tname, regno);
5659− return -EINVAL;
56605710 }
56615711
56625712 reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id,
@@ -5672,23 +5722,24 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, · 9 unmodified lines
56725722 reg_ref_tname);
56735723 return -EINVAL;
56745724 }
5675− } else if (btf_get_prog_ctx_type(log, btf, t,
5676− env->prog->type, i)) {
5677− /* If function expects ctx type in BTF check that caller
5678− * is passing PTR_TO_CTX.
5679− */
5680− if (reg->type != PTR_TO_CTX) {
5681− bpf_log(log,
5682− "arg#%d expected pointer to ctx, but got %s\n",
5683− i, btf_type_str(t));
5684− return -EINVAL;
5685− }
5686− if (check_ctx_reg(env, reg, regno))
5687− return -EINVAL;
56885725 } else if (ptr_to_mem_ok) {
56895726 const struct btf_type *resolve_ret;
56905727 u32 type_size;
56915728
5729+ if (is_kfunc) {
5730+ /* Permit pointer to mem, but only when argument
5731+ * type is pointer to scalar, or struct composed
5732+ * (recursively) of scalars.
5733+ */
5734+ if (!btf_type_is_scalar(ref_t) &&
5735+ !__btf_type_is_scalar_struct(log, btf, ref_t, 0)) {
5736+ bpf_log(log,
5737+ "arg#%d pointer type %s %s must point to scalar or struct with scalar\n",
5738+ i, btf_type_str(ref_t), ref_tname);
5739+ return -EINVAL;
5740+ }
5741+ }
5742+
56925743 resolve_ret = btf_resolve_size(btf, ref_t, &type_size);
56935744 if (IS_ERR(resolve_ret)) {
56945745 bpf_log(log,
@@ -5701,6 +5752,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, · 6 unmodified lines
57015752 if (check_mem_reg(env, reg, regno, type_size))
57025753 return -EINVAL;
57035754 } else {
5755+ bpf_log(log, "reg type unsupported for arg#%d %sfunction %s#%d\n", i,
5756+ is_kfunc ? "kernel " : "", func_name, func_id);
57045757 return -EINVAL;
57055758 }
57065759 }
@@ -5750,7 +5803,7 @@ int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, · 43 unmodified lines
57505803 const struct btf *btf, u32 func_id,
57515804 struct bpf_reg_state *regs)
57525805 {
5753− return btf_check_func_arg_match(env, btf, func_id, regs, false);
5806+ return btf_check_func_arg_match(env, btf, func_id, regs, true);
57545807 }
57555808
57565809 /* Convert BTF of a function into bpf_reg_state if possible
@@ -5858,7 +5911,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog, · 101 unmodified lines
58585911 return -EINVAL;
58595912 }
58605913
5861− reg->type = PTR_TO_MEM_OR_NULL;
5914+ reg->type = PTR_TO_MEM | PTR_MAYBE_NULL;
58625915 reg->id = ++env->id_gen;
58635916
58645917 continue;
@@ -6352,7 +6405,7 @@ const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { · 487 unmodified lines
63526405 .func = bpf_btf_find_by_name_kind,
63536406 .gpl_only = false,
63546407 .ret_type = RET_INTEGER,
6355− .arg1_type = ARG_PTR_TO_MEM,
6408+ .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
63566409 .arg2_type = ARG_CONST_SIZE,
63576410 .arg3_type = ARG_ANYTHING,
63586411 .arg4_type = ARG_ANYTHING,
@@ -6534,12 +6587,11 @@ static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands, · 175 unmodified lines
65346587 bpf_free_cands_from_cache(*cc);
65356588 *cc = NULL;
65366589 }
6537− new_cands = kmalloc(sizeof_cands(cands->cnt), GFP_KERNEL);
6590+ new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL);
65386591 if (!new_cands) {
65396592 bpf_free_cands(cands);
65406593 return ERR_PTR(-ENOMEM);
65416594 }
6542− memcpy(new_cands, cands, sizeof_cands(cands->cnt));
65436595 /* strdup the name, since it will stay in cache.
65446596 * the cands->name points to strings in prog's BTF and the prog can be unloaded.
65456597 */
@@ -6657,7 +6709,7 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id) · 111 unmodified lines
66576709
66586710 main_btf = bpf_get_btf_vmlinux();
66596711 if (IS_ERR(main_btf))
6660− return (void *)main_btf;
6712+ return ERR_CAST(main_btf);
66616713
66626714 local_type = btf_type_by_id(local_btf, local_type_id);
66636715 if (!local_type)
@@ -6684,14 +6736,14 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id) · 20 unmodified lines
66846736 /* Attempt to find target candidates in vmlinux BTF first */
66856737 cands = bpf_core_add_cands(cands, main_btf, 1);
66866738 if (IS_ERR(cands))
6687− return cands;
6739+ return ERR_CAST(cands);
66886740
66896741 /* cands is a pointer to kmalloced memory here if cands->cnt > 0 */
66906742
66916743 /* populate cache even when cands->cnt == 0 */
66926744 cc = populate_cand_cache(cands, vmlinux_cand_cache, VMLINUX_CAND_CACHE_SIZE);
66936745 if (IS_ERR(cc))
6694− return cc;
6746+ return ERR_CAST(cc);
66956747
66966748 /* if vmlinux BTF has any candidate, don't go for module BTFs */
66976749 if (cc->cnt)
@@ -6717,7 +6769,7 @@ check_modules: · 19 unmodified lines
67176769 cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf));
67186770 if (IS_ERR(cands)) {
67196771 btf_put(mod_btf);
6720− return cands;
6772+ return ERR_CAST(cands);
67216773 }
67226774 spin_lock_bh(&btf_idr_lock);
67236775 btf_put(mod_btf);
kernel/bpf/cgroup.c+1−1
@@ -1789,7 +1789,7 @@ static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = { · 1788 unmodified lines
17891789 .gpl_only = false,
17901790 .ret_type = RET_INTEGER,
17911791 .arg1_type = ARG_PTR_TO_CTX,
1792− .arg2_type = ARG_PTR_TO_MEM,
1792+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
17931793 .arg3_type = ARG_CONST_SIZE,
17941794 };
17951795
kernel/bpf/cpumap.c+2−2
@@ -195,7 +195,7 @@ static void cpu_map_bpf_prog_run_skb(struct bpf_cpu_map_entry *rcpu, · 194 unmodified lines
195195 }
196196 return;
197197 default:
198− bpf_warn_invalid_xdp_action(act);
198+ bpf_warn_invalid_xdp_action(NULL, rcpu->prog, act);
199199 fallthrough;
200200 case XDP_ABORTED:
201201 trace_xdp_exception(skb->dev, rcpu->prog, act);
@@ -254,7 +254,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu, · 52 unmodified lines
254254 }
255255 break;
256256 default:
257− bpf_warn_invalid_xdp_action(act);
257+ bpf_warn_invalid_xdp_action(NULL, rcpu->prog, act);
258258 fallthrough;
259259 case XDP_DROP:
260260 xdp_return_frame(xdpf);
kernel/bpf/devmap.c+2−2
@@ -348,7 +348,7 @@ static int dev_map_bpf_prog_run(struct bpf_prog *xdp_prog, · 347 unmodified lines
348348 frames[nframes++] = xdpf;
349349 break;
350350 default:
351− bpf_warn_invalid_xdp_action(act);
351+ bpf_warn_invalid_xdp_action(NULL, xdp_prog, act);
352352 fallthrough;
353353 case XDP_ABORTED:
354354 trace_xdp_exception(dev, xdp_prog, act);
@@ -507,7 +507,7 @@ static u32 dev_map_bpf_prog_run_skb(struct sk_buff *skb, struct bpf_dtab_netdev · 152 unmodified lines
507507 __skb_push(skb, skb->mac_len);
508508 break;
509509 default:
510− bpf_warn_invalid_xdp_action(act);
510+ bpf_warn_invalid_xdp_action(NULL, dst->xdp_prog, act);
511511 fallthrough;
512512 case XDP_ABORTED:
513513 trace_xdp_exception(dst->dev, dst->xdp_prog, act);
kernel/bpf/helpers.c+23−6
@@ -2,6 +2,7 @@ · 1 unmodified line
22 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
33 */
44 #include <linux/bpf.h>
5+#include <linux/bpf-cgroup.h>
56 #include <linux/rcupdate.h>
67 #include <linux/random.h>
78 #include <linux/smp.h>
@@ -530,7 +531,7 @@ const struct bpf_func_proto bpf_strtol_proto = { · 522 unmodified lines
530531 .func = bpf_strtol,
531532 .gpl_only = false,
532533 .ret_type = RET_INTEGER,
533− .arg1_type = ARG_PTR_TO_MEM,
534+ .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
534535 .arg2_type = ARG_CONST_SIZE,
535536 .arg3_type = ARG_ANYTHING,
536537 .arg4_type = ARG_PTR_TO_LONG,
@@ -558,13 +559,27 @@ const struct bpf_func_proto bpf_strtoul_proto = { · 21 unmodified lines
558559 .func = bpf_strtoul,
559560 .gpl_only = false,
560561 .ret_type = RET_INTEGER,
561− .arg1_type = ARG_PTR_TO_MEM,
562+ .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
562563 .arg2_type = ARG_CONST_SIZE,
563564 .arg3_type = ARG_ANYTHING,
564565 .arg4_type = ARG_PTR_TO_LONG,
565566 };
566567 #endif
567568
569+BPF_CALL_3(bpf_strncmp, const char *, s1, u32, s1_sz, const char *, s2)
570+{
571+ return strncmp(s1, s2, s1_sz);
572+}
573+
574+const struct bpf_func_proto bpf_strncmp_proto = {
575+ .func = bpf_strncmp,
576+ .gpl_only = false,
577+ .ret_type = RET_INTEGER,
578+ .arg1_type = ARG_PTR_TO_MEM,
579+ .arg2_type = ARG_CONST_SIZE,
580+ .arg3_type = ARG_PTR_TO_CONST_STR,
581+};
582+
568583 BPF_CALL_4(bpf_get_ns_current_pid_tgid, u64, dev, u64, ino,
569584 struct bpf_pidns_info *, nsdata, u32, size)
570585 {
@@ -630,7 +645,7 @@ const struct bpf_func_proto bpf_event_output_data_proto = { · 59 unmodified lines
630645 .arg1_type = ARG_PTR_TO_CTX,
631646 .arg2_type = ARG_CONST_MAP_PTR,
632647 .arg3_type = ARG_ANYTHING,
633− .arg4_type = ARG_PTR_TO_MEM,
648+ .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
634649 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
635650 };
636651
@@ -667,7 +682,7 @@ BPF_CALL_2(bpf_per_cpu_ptr, const void *, ptr, u32, cpu) · 30 unmodified lines
667682 const struct bpf_func_proto bpf_per_cpu_ptr_proto = {
668683 .func = bpf_per_cpu_ptr,
669684 .gpl_only = false,
670− .ret_type = RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL,
685+ .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | PTR_MAYBE_NULL | MEM_RDONLY,
671686 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
672687 .arg2_type = ARG_ANYTHING,
673688 };
@@ -680,7 +695,7 @@ BPF_CALL_1(bpf_this_cpu_ptr, const void *, percpu_ptr) · 6 unmodified lines
680695 const struct bpf_func_proto bpf_this_cpu_ptr_proto = {
681696 .func = bpf_this_cpu_ptr,
682697 .gpl_only = false,
683− .ret_type = RET_PTR_TO_MEM_OR_BTF_ID,
698+ .ret_type = RET_PTR_TO_MEM_OR_BTF_ID | MEM_RDONLY,
684699 .arg1_type = ARG_PTR_TO_PERCPU_BTF_ID,
685700 };
686701
@@ -1011,7 +1026,7 @@ const struct bpf_func_proto bpf_snprintf_proto = { · 324 unmodified lines
10111026 .arg1_type = ARG_PTR_TO_MEM_OR_NULL,
10121027 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
10131028 .arg3_type = ARG_PTR_TO_CONST_STR,
1014− .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
1029+ .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
10151030 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
10161031 };
10171032
@@ -1378,6 +1393,8 @@ bpf_base_func_proto(enum bpf_func_id func_id) · 360 unmodified lines
13781393 return &bpf_for_each_map_elem_proto;
13791394 case BPF_FUNC_loop:
13801395 return &bpf_loop_proto;
1396+ case BPF_FUNC_strncmp:
1397+ return &bpf_strncmp_proto;
13811398 default:
13821399 break;
13831400 }
kernel/bpf/local_storage.c+1−2
@@ -163,8 +163,7 @@ static int cgroup_storage_update_elem(struct bpf_map *map, void *key, · 162 unmodified lines
163163 return 0;
164164 }
165165
166− new = bpf_map_kmalloc_node(map, sizeof(struct bpf_storage_buffer) +
167− map->value_size,
166+ new = bpf_map_kmalloc_node(map, struct_size(new, data, map->value_size),
168167 __GFP_ZERO | GFP_ATOMIC | __GFP_NOWARN,
169168 map->numa_node);
170169 if (!new)
kernel/bpf/lpm_trie.c+1−1
@@ -412,7 +412,7 @@ static int trie_update_elem(struct bpf_map *map, · 411 unmodified lines
412412 rcu_assign_pointer(im_node->child[1], node);
413413 }
414414
415− /* Finally, assign the intermediate node to the determined spot */
415+ /* Finally, assign the intermediate node to the determined slot */
416416 rcu_assign_pointer(*slot, im_node);
417417
418418 out:
kernel/bpf/map_iter.c+2−2
@@ -174,9 +174,9 @@ static const struct bpf_iter_reg bpf_map_elem_reg_info = { · 173 unmodified lines
174174 .ctx_arg_info_size = 2,
175175 .ctx_arg_info = {
176176 { offsetof(struct bpf_iter__bpf_map_elem, key),
177− PTR_TO_RDONLY_BUF_OR_NULL },
177+ PTR_TO_BUF | PTR_MAYBE_NULL | MEM_RDONLY },
178178 { offsetof(struct bpf_iter__bpf_map_elem, value),
179− PTR_TO_RDWR_BUF_OR_NULL },
179+ PTR_TO_BUF | PTR_MAYBE_NULL },
180180 },
181181 };
182182
kernel/bpf/net_namespace.c+1−0
@@ -1,6 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0
22
33 #include <linux/bpf.h>
4+#include <linux/bpf-netns.h>
45 #include <linux/filter.h>
56 #include <net/net_namespace.h>
67
kernel/bpf/reuseport_array.c+1−5
@@ -152,16 +152,12 @@ static struct bpf_map *reuseport_array_alloc(union bpf_attr *attr) · 151 unmodified lines
152152 {
153153 int numa_node = bpf_map_attr_numa_node(attr);
154154 struct reuseport_array *array;
155− u64 array_size;
156155
157156 if (!bpf_capable())
158157 return ERR_PTR(-EPERM);
159158
160− array_size = sizeof(*array);
161− array_size += (u64)attr->max_entries * sizeof(struct sock *);
162−
163159 /* allocate all map elements and zero-initialize them */
164− array = bpf_map_area_alloc(array_size, numa_node);
160+ array = bpf_map_area_alloc(struct_size(array, ptrs, attr->max_entries), numa_node);
165161 if (!array)
166162 return ERR_PTR(-ENOMEM);
167163
kernel/bpf/ringbuf.c+1−1
@@ -444,7 +444,7 @@ const struct bpf_func_proto bpf_ringbuf_output_proto = { · 443 unmodified lines
444444 .func = bpf_ringbuf_output,
445445 .ret_type = RET_INTEGER,
446446 .arg1_type = ARG_CONST_MAP_PTR,
447− .arg2_type = ARG_PTR_TO_MEM,
447+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
448448 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
449449 .arg4_type = ARG_ANYTHING,
450450 };
kernel/bpf/syscall.c+2−1
@@ -2,6 +2,7 @@ · 1 unmodified line
22 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
33 */
44 #include <linux/bpf.h>
5+#include <linux/bpf-cgroup.h>
56 #include <linux/bpf_trace.h>
67 #include <linux/bpf_lirc.h>
78 #include <linux/bpf_verifier.h>
@@ -4772,7 +4773,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = { · 4764 unmodified lines
47724773 .gpl_only = false,
47734774 .ret_type = RET_INTEGER,
47744775 .arg1_type = ARG_ANYTHING,
4775− .arg2_type = ARG_PTR_TO_MEM,
4776+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
47764777 .arg3_type = ARG_CONST_SIZE,
47774778 };
47784779
kernel/bpf/trampoline.c+8−0
@@ -27,6 +27,14 @@ static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE]; · 26 unmodified lines
2727 /* serializes access to trampoline_table */
2828 static DEFINE_MUTEX(trampoline_mutex);
2929
30+bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
31+{
32+ enum bpf_attach_type eatype = prog->expected_attach_type;
33+
34+ return eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT ||
35+ eatype == BPF_MODIFY_RETURN;
36+}
37+
3038 void *bpf_jit_alloc_exec_page(void)
3139 {
3240 void *image;
kernel/bpf/verifier.c+415−281
@@ -4,6 +4,7 @@ · 3 unmodified lines
44 * Copyright (c) 2018 Covalent IO, Inc. http://covalent.io
55 */
66 #include <uapi/linux/btf.h>
7+#include <linux/bpf-cgroup.h>
78 #include <linux/kernel.h>
89 #include <linux/types.h>
910 #include <linux/slab.h>
@@ -441,18 +442,6 @@ static bool reg_type_not_null(enum bpf_reg_type type) · 431 unmodified lines
441442 type == PTR_TO_SOCK_COMMON;
442443 }
443444
444−static bool reg_type_may_be_null(enum bpf_reg_type type)
445−{
446− return type == PTR_TO_MAP_VALUE_OR_NULL ||
447− type == PTR_TO_SOCKET_OR_NULL ||
448− type == PTR_TO_SOCK_COMMON_OR_NULL ||
449− type == PTR_TO_TCP_SOCK_OR_NULL ||
450− type == PTR_TO_BTF_ID_OR_NULL ||
451− type == PTR_TO_MEM_OR_NULL ||
452− type == PTR_TO_RDONLY_BUF_OR_NULL ||
453− type == PTR_TO_RDWR_BUF_OR_NULL;
454−}
455−
456445 static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
457446 {
458447 return reg->type == PTR_TO_MAP_VALUE &&
@@ -461,12 +450,14 @@ static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg) · 2 unmodified lines
461450
462451 static bool reg_type_may_be_refcounted_or_null(enum bpf_reg_type type)
463452 {
464− return type == PTR_TO_SOCKET ||
465− type == PTR_TO_SOCKET_OR_NULL ||
466− type == PTR_TO_TCP_SOCK ||
467− type == PTR_TO_TCP_SOCK_OR_NULL ||
468− type == PTR_TO_MEM ||
469− type == PTR_TO_MEM_OR_NULL;
453+ return base_type(type) == PTR_TO_SOCKET ||
454+ base_type(type) == PTR_TO_TCP_SOCK ||
455+ base_type(type) == PTR_TO_MEM;
456+}
457+
458+static bool type_is_rdonly_mem(u32 type)
459+{
460+ return type & MEM_RDONLY;
470461 }
471462
472463 static bool arg_type_may_be_refcounted(enum bpf_arg_type type)
@@ -474,14 +465,9 @@ static bool arg_type_may_be_refcounted(enum bpf_arg_type type) · 1 unmodified line
474465 return type == ARG_PTR_TO_SOCK_COMMON;
475466 }
476467
477−static bool arg_type_may_be_null(enum bpf_arg_type type)
468+static bool type_may_be_null(u32 type)
478469 {
479− return type == ARG_PTR_TO_MAP_VALUE_OR_NULL ||
480− type == ARG_PTR_TO_MEM_OR_NULL ||
481− type == ARG_PTR_TO_CTX_OR_NULL ||
482− type == ARG_PTR_TO_SOCKET_OR_NULL ||
483− type == ARG_PTR_TO_ALLOC_MEM_OR_NULL ||
484− type == ARG_PTR_TO_STACK_OR_NULL;
470+ return type & PTR_MAYBE_NULL;
485471 }
486472
487473 /* Determine whether the function releases some resources allocated by another
@@ -541,39 +527,54 @@ static bool is_cmpxchg_insn(const struct bpf_insn *insn) · 53 unmodified lines
541527 insn->imm == BPF_CMPXCHG;
542528 }
543529
544−/* string representation of 'enum bpf_reg_type' */
545−static const char * const reg_type_str[] = {
546− [NOT_INIT] = "?",
547− [SCALAR_VALUE] = "inv",
548− [PTR_TO_CTX] = "ctx",
549− [CONST_PTR_TO_MAP] = "map_ptr",
550− [PTR_TO_MAP_VALUE] = "map_value",
551− [PTR_TO_MAP_VALUE_OR_NULL] = "map_value_or_null",
552− [PTR_TO_STACK] = "fp",
553− [PTR_TO_PACKET] = "pkt",
554− [PTR_TO_PACKET_META] = "pkt_meta",
555− [PTR_TO_PACKET_END] = "pkt_end",
556− [PTR_TO_FLOW_KEYS] = "flow_keys",
557− [PTR_TO_SOCKET] = "sock",
558− [PTR_TO_SOCKET_OR_NULL] = "sock_or_null",
559− [PTR_TO_SOCK_COMMON] = "sock_common",
560− [PTR_TO_SOCK_COMMON_OR_NULL] = "sock_common_or_null",
561− [PTR_TO_TCP_SOCK] = "tcp_sock",
562− [PTR_TO_TCP_SOCK_OR_NULL] = "tcp_sock_or_null",
563− [PTR_TO_TP_BUFFER] = "tp_buffer",
564− [PTR_TO_XDP_SOCK] = "xdp_sock",
565− [PTR_TO_BTF_ID] = "ptr_",
566− [PTR_TO_BTF_ID_OR_NULL] = "ptr_or_null_",
567− [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_",
568− [PTR_TO_MEM] = "mem",
569− [PTR_TO_MEM_OR_NULL] = "mem_or_null",
570− [PTR_TO_RDONLY_BUF] = "rdonly_buf",
571− [PTR_TO_RDONLY_BUF_OR_NULL] = "rdonly_buf_or_null",
572− [PTR_TO_RDWR_BUF] = "rdwr_buf",
573− [PTR_TO_RDWR_BUF_OR_NULL] = "rdwr_buf_or_null",
574− [PTR_TO_FUNC] = "func",
575− [PTR_TO_MAP_KEY] = "map_key",
576−};
530+/* string representation of 'enum bpf_reg_type'
531+ *
532+ * Note that reg_type_str() can not appear more than once in a single verbose()
533+ * statement.
534+ */
535+static const char *reg_type_str(struct bpf_verifier_env *env,
536+ enum bpf_reg_type type)
537+{
538+ char postfix[16] = {0}, prefix[16] = {0};
539+ static const char * const str[] = {
540+ [NOT_INIT] = "?",
541+ [SCALAR_VALUE] = "inv",
542+ [PTR_TO_CTX] = "ctx",
543+ [CONST_PTR_TO_MAP] = "map_ptr",
544+ [PTR_TO_MAP_VALUE] = "map_value",
545+ [PTR_TO_STACK] = "fp",
546+ [PTR_TO_PACKET] = "pkt",
547+ [PTR_TO_PACKET_META] = "pkt_meta",
548+ [PTR_TO_PACKET_END] = "pkt_end",
549+ [PTR_TO_FLOW_KEYS] = "flow_keys",
550+ [PTR_TO_SOCKET] = "sock",
551+ [PTR_TO_SOCK_COMMON] = "sock_common",
552+ [PTR_TO_TCP_SOCK] = "tcp_sock",
553+ [PTR_TO_TP_BUFFER] = "tp_buffer",
554+ [PTR_TO_XDP_SOCK] = "xdp_sock",
555+ [PTR_TO_BTF_ID] = "ptr_",
556+ [PTR_TO_PERCPU_BTF_ID] = "percpu_ptr_",
557+ [PTR_TO_MEM] = "mem",
558+ [PTR_TO_BUF] = "buf",
559+ [PTR_TO_FUNC] = "func",
560+ [PTR_TO_MAP_KEY] = "map_key",
561+ };
562+
563+ if (type & PTR_MAYBE_NULL) {
564+ if (base_type(type) == PTR_TO_BTF_ID ||
565+ base_type(type) == PTR_TO_PERCPU_BTF_ID)
566+ strncpy(postfix, "or_null_", 16);
567+ else
568+ strncpy(postfix, "_or_null", 16);
569+ }
570+
571+ if (type & MEM_RDONLY)
572+ strncpy(prefix, "rdonly_", 16);
573+
574+ snprintf(env->type_str_buf, TYPE_STR_BUF_LEN, "%s%s%s",
575+ prefix, str[base_type(type)], postfix);
576+ return env->type_str_buf;
577+}
577578
578579 static char slot_type_char[] = {
579580 [STACK_INVALID] = '?',
@@ -608,6 +609,44 @@ static const char *kernel_type_name(const struct btf* btf, u32 id) · 28 unmodified lines
608609 return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off);
609610 }
610611
612+static void mark_reg_scratched(struct bpf_verifier_env *env, u32 regno)
613+{
614+ env->scratched_regs |= 1U << regno;
615+}
616+
617+static void mark_stack_slot_scratched(struct bpf_verifier_env *env, u32 spi)
618+{
619+ env->scratched_stack_slots |= 1UL << spi;
620+}
621+
622+static bool reg_scratched(const struct bpf_verifier_env *env, u32 regno)
623+{
624+ return (env->scratched_regs >> regno) & 1;
625+}
626+
627+static bool stack_slot_scratched(const struct bpf_verifier_env *env, u64 regno)
628+{
629+ return (env->scratched_stack_slots >> regno) & 1;
630+}
631+
632+static bool verifier_state_scratched(const struct bpf_verifier_env *env)
633+{
634+ return env->scratched_regs || env->scratched_stack_slots;
635+}
636+
637+static void mark_verifier_state_clean(struct bpf_verifier_env *env)
638+{
639+ env->scratched_regs = 0U;
640+ env->scratched_stack_slots = 0UL;
641+}
642+
643+/* Used for printing the entire verifier state. */
644+static void mark_verifier_state_scratched(struct bpf_verifier_env *env)
645+{
646+ env->scratched_regs = ~0U;
647+ env->scratched_stack_slots = ~0UL;
648+}
649+
611650 /* The reg state of a pointer or a bounded scalar was saved when
612651 * it was spilled to the stack.
613652 */
@@ -623,7 +662,8 @@ static void scrub_spilled_slot(u8 *stype) · 9 unmodified lines
623662 }
624663
625664 static void print_verifier_state(struct bpf_verifier_env *env,
626− const struct bpf_func_state *state)
665+ const struct bpf_func_state *state,
666+ bool print_all)
627667 {
628668 const struct bpf_reg_state *reg;
629669 enum bpf_reg_type t;
@@ -636,9 +676,11 @@ static void print_verifier_state(struct bpf_verifier_env *env, · 6 unmodified lines
636676 t = reg->type;
637677 if (t == NOT_INIT)
638678 continue;
679+ if (!print_all && !reg_scratched(env, i))
680+ continue;
639681 verbose(env, " R%d", i);
640682 print_liveness(env, reg->live);
641− verbose(env, "=%s", reg_type_str[t]);
683+ verbose(env, "=%s", reg_type_str(env, t));
642684 if (t == SCALAR_VALUE && reg->precise)
643685 verbose(env, "P");
644686 if ((t == SCALAR_VALUE || t == PTR_TO_STACK) &&
@@ -646,9 +688,8 @@ static void print_verifier_state(struct bpf_verifier_env *env, · 1 unmodified line
646688 /* reg->off should be 0 for SCALAR_VALUE */
647689 verbose(env, "%lld", reg->var_off.value + reg->off);
648690 } else {
649− if (t == PTR_TO_BTF_ID ||
650− t == PTR_TO_BTF_ID_OR_NULL ||
651− t == PTR_TO_PERCPU_BTF_ID)
691+ if (base_type(t) == PTR_TO_BTF_ID ||
692+ base_type(t) == PTR_TO_PERCPU_BTF_ID)
652693 verbose(env, "%s", kernel_type_name(reg->btf, reg->btf_id));
653694 verbose(env, "(id=%d", reg->id);
654695 if (reg_type_may_be_refcounted_or_null(t))
@@ -657,10 +698,9 @@ static void print_verifier_state(struct bpf_verifier_env *env, · 2 unmodified lines
657698 verbose(env, ",off=%d", reg->off);
658699 if (type_is_pkt_pointer(t))
659700 verbose(env, ",r=%d", reg->range);
660− else if (t == CONST_PTR_TO_MAP ||
661− t == PTR_TO_MAP_KEY ||
662− t == PTR_TO_MAP_VALUE ||
663− t == PTR_TO_MAP_VALUE_OR_NULL)
701+ else if (base_type(t) == CONST_PTR_TO_MAP ||
702+ base_type(t) == PTR_TO_MAP_KEY ||
703+ base_type(t) == PTR_TO_MAP_VALUE)
664704 verbose(env, ",ks=%d,vs=%d",
665705 reg->map_ptr->key_size,
666706 reg->map_ptr->value_size);
@@ -725,12 +765,14 @@ static void print_verifier_state(struct bpf_verifier_env *env, · 58 unmodified lines
725765 types_buf[BPF_REG_SIZE] = 0;
726766 if (!valid)
727767 continue;
768+ if (!print_all && !stack_slot_scratched(env, i))
769+ continue;
728770 verbose(env, " fp%d", (-i - 1) * BPF_REG_SIZE);
729771 print_liveness(env, state->stack[i].spilled_ptr.live);
730772 if (is_spilled_reg(&state->stack[i])) {
731773 reg = &state->stack[i].spilled_ptr;
732774 t = reg->type;
733− verbose(env, "=%s", reg_type_str[t]);
775+ verbose(env, "=%s", reg_type_str(env, t));
734776 if (t == SCALAR_VALUE && reg->precise)
735777 verbose(env, "P");
736778 if (t == SCALAR_VALUE && tnum_is_const(reg->var_off))
@@ -750,6 +792,26 @@ static void print_verifier_state(struct bpf_verifier_env *env, · 13 unmodified lines
750792 if (state->in_async_callback_fn)
751793 verbose(env, " async_cb");
752794 verbose(env, "\n");
795+ mark_verifier_state_clean(env);
796+}
797+
798+static inline u32 vlog_alignment(u32 pos)
799+{
800+ return round_up(max(pos + BPF_LOG_MIN_ALIGNMENT / 2, BPF_LOG_ALIGNMENT),
801+ BPF_LOG_MIN_ALIGNMENT) - pos - 1;
802+}
803+
804+static void print_insn_state(struct bpf_verifier_env *env,
805+ const struct bpf_func_state *state)
806+{
807+ if (env->prev_log_len && env->prev_log_len == env->log.len_used) {
808+ /* remove new line character */
809+ bpf_vlog_reset(&env->log, env->prev_log_len - 1);
810+ verbose(env, "%*c;", vlog_alignment(env->prev_insn_print_len), ' ');
811+ } else {
812+ verbose(env, "%d:", env->insn_idx);
813+ }
814+ print_verifier_state(env, state, false);
753815 }
754816
755817 /* copy array src of length n * size bytes to dst. dst is reallocated if it's too
@@ -1143,8 +1205,7 @@ static void mark_reg_known_zero(struct bpf_verifier_env *env, · 387 unmodified lines
11431205
11441206 static void mark_ptr_not_null_reg(struct bpf_reg_state *reg)
11451207 {
1146− switch (reg->type) {
1147− case PTR_TO_MAP_VALUE_OR_NULL: {
1208+ if (base_type(reg->type) == PTR_TO_MAP_VALUE) {
11481209 const struct bpf_map *map = reg->map_ptr;
11491210
11501211 if (map->inner_map_meta) {
@@ -1163,32 +1224,10 @@ static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) · 12 unmodified lines
11631224 } else {
11641225 reg->type = PTR_TO_MAP_VALUE;
11651226 }
1166− break;
1167− }
1168− case PTR_TO_SOCKET_OR_NULL:
1169− reg->type = PTR_TO_SOCKET;
1170− break;
1171− case PTR_TO_SOCK_COMMON_OR_NULL:
1172− reg->type = PTR_TO_SOCK_COMMON;
1173− break;
1174− case PTR_TO_TCP_SOCK_OR_NULL:
1175− reg->type = PTR_TO_TCP_SOCK;
1176− break;
1177− case PTR_TO_BTF_ID_OR_NULL:
1178− reg->type = PTR_TO_BTF_ID;
1179− break;
1180− case PTR_TO_MEM_OR_NULL:
1181− reg->type = PTR_TO_MEM;
1182− break;
1183− case PTR_TO_RDONLY_BUF_OR_NULL:
1184− reg->type = PTR_TO_RDONLY_BUF;
1185− break;
1186− case PTR_TO_RDWR_BUF_OR_NULL:
1187− reg->type = PTR_TO_RDWR_BUF;
1188− break;
1189− default:
1190− WARN_ONCE(1, "unknown nullable register type");
1227+ return;
11911228 }
1229+
1230+ reg->type &= ~PTR_MAYBE_NULL;
11921231 }
11931232
11941233 static bool reg_is_pkt_pointer(const struct bpf_reg_state *reg)
@@ -1546,6 +1585,7 @@ static void init_func_state(struct bpf_verifier_env *env, · 351 unmodified lines
15461585 state->frameno = frameno;
15471586 state->subprogno = subprogno;
15481587 init_reg_state(env, state);
1588+ mark_verifier_state_scratched(env);
15491589 }
15501590
15511591 /* Similar to push_stack(), but for async callbacks */
@@ -2049,7 +2089,7 @@ static int mark_reg_read(struct bpf_verifier_env *env, · 497 unmodified lines
20492089 break;
20502090 if (parent->live & REG_LIVE_DONE) {
20512091 verbose(env, "verifier BUG type %s var_off %lld off %d\n",
2052− reg_type_str[parent->type],
2092+ reg_type_str(env, parent->type),
20532093 parent->var_off.value, parent->off);
20542094 return -EFAULT;
20552095 }
@@ -2233,6 +2273,8 @@ static int check_reg_arg(struct bpf_verifier_env *env, u32 regno, · 177 unmodified lines
22332273 return -EINVAL;
22342274 }
22352275
2276+ mark_reg_scratched(env, regno);
2277+
22362278 reg = &regs[regno];
22372279 rw64 = is_reg64(env, insn, regno, reg, t);
22382280 if (t == SRC_OP) {
@@ -2337,7 +2379,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, · 98 unmodified lines
23372379
23382380 if (insn->code == 0)
23392381 return 0;
2340− if (env->log.level & BPF_LOG_LEVEL) {
2382+ if (env->log.level & BPF_LOG_LEVEL2) {
23412383 verbose(env, "regs=%x stack=%llx before ", *reg_mask, *stack_mask);
23422384 verbose(env, "%d: ", idx);
23432385 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
@@ -2591,7 +2633,7 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, · 247 unmodified lines
25912633 DECLARE_BITMAP(mask, 64);
25922634 u32 history = st->jmp_history_cnt;
25932635
2594− if (env->log.level & BPF_LOG_LEVEL)
2636+ if (env->log.level & BPF_LOG_LEVEL2)
25952637 verbose(env, "last_idx %d first_idx %d\n", last_idx, first_idx);
25962638 for (i = last_idx;;) {
25972639 if (skip_first) {
@@ -2678,11 +2720,11 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno, · 80 unmodified lines
26782720 new_marks = true;
26792721 reg->precise = true;
26802722 }
2681− if (env->log.level & BPF_LOG_LEVEL) {
2682− print_verifier_state(env, func);
2683− verbose(env, "parent %s regs=%x stack=%llx marks\n",
2723+ if (env->log.level & BPF_LOG_LEVEL2) {
2724+ verbose(env, "parent %s regs=%x stack=%llx marks:",
26842725 new_marks ? "didn't have" : "already had",
26852726 reg_mask, stack_mask);
2727+ print_verifier_state(env, func, true);
26862728 }
26872729
26882730 if (!reg_mask && !stack_mask)
@@ -2708,9 +2750,8 @@ static int mark_chain_precision_stack(struct bpf_verifier_env *env, int spi) · 19 unmodified lines
27082750
27092751 static bool is_spillable_regtype(enum bpf_reg_type type)
27102752 {
2711− switch (type) {
2753+ switch (base_type(type)) {
27122754 case PTR_TO_MAP_VALUE:
2713− case PTR_TO_MAP_VALUE_OR_NULL:
27142755 case PTR_TO_STACK:
27152756 case PTR_TO_CTX:
27162757 case PTR_TO_PACKET:
@@ -2719,21 +2760,13 @@ static bool is_spillable_regtype(enum bpf_reg_type type) · 2 unmodified lines
27192760 case PTR_TO_FLOW_KEYS:
27202761 case CONST_PTR_TO_MAP:
27212762 case PTR_TO_SOCKET:
2722− case PTR_TO_SOCKET_OR_NULL:
27232763 case PTR_TO_SOCK_COMMON:
2724− case PTR_TO_SOCK_COMMON_OR_NULL:
27252764 case PTR_TO_TCP_SOCK:
2726− case PTR_TO_TCP_SOCK_OR_NULL:
27272765 case PTR_TO_XDP_SOCK:
27282766 case PTR_TO_BTF_ID:
2729− case PTR_TO_BTF_ID_OR_NULL:
2730− case PTR_TO_RDONLY_BUF:
2731− case PTR_TO_RDONLY_BUF_OR_NULL:
2732− case PTR_TO_RDWR_BUF:
2733− case PTR_TO_RDWR_BUF_OR_NULL:
2767+ case PTR_TO_BUF:
27342768 case PTR_TO_PERCPU_BTF_ID:
27352769 case PTR_TO_MEM:
2736− case PTR_TO_MEM_OR_NULL:
27372770 case PTR_TO_FUNC:
27382771 case PTR_TO_MAP_KEY:
27392772 return true;
@@ -2838,6 +2871,7 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env, · 98 unmodified lines
28382871 env->insn_aux_data[insn_idx].sanitize_stack_spill = true;
28392872 }
28402873
2874+ mark_stack_slot_scratched(env, spi);
28412875 if (reg && !(off % BPF_REG_SIZE) && register_is_bounded(reg) &&
28422876 !register_is_null(reg) && env->bpf_capable) {
28432877 if (dst_reg != BPF_REG_FP) {
@@ -2959,6 +2993,7 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env, · 115 unmodified lines
29592993 slot = -i - 1;
29602994 spi = slot / BPF_REG_SIZE;
29612995 stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
2996+ mark_stack_slot_scratched(env, spi);
29622997
29632998 if (!env->allow_ptr_leaks
29642999 && *stype != NOT_INIT
@@ -3375,11 +3410,8 @@ static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, · 410 unmodified lines
33753410 /* We may have adjusted the register pointing to memory region, so we
33763411 * need to try adding each of min_value and max_value to off
33773412 * to make sure our theoretical access will be safe.
3378− */
3379− if (env->log.level & BPF_LOG_LEVEL)
3380− print_verifier_state(env, state);
3381−
3382− /* The minimum value is only important with signed
3413+ *
3414+ * The minimum value is only important with signed
33833415 * comparisons where we can't assume the floor of a
33843416 * value is 0. If we are using signed variables for our
33853417 * index'es we need to make sure that whatever we use
@@ -3574,7 +3606,7 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int off, · 188 unmodified lines
35743606 */
35753607 *reg_type = info.reg_type;
35763608
3577− if (*reg_type == PTR_TO_BTF_ID || *reg_type == PTR_TO_BTF_ID_OR_NULL) {
3609+ if (base_type(*reg_type) == PTR_TO_BTF_ID) {
35783610 *btf = info.btf;
35793611 *btf_id = info.btf_id;
35803612 } else {
@@ -3642,7 +3674,7 @@ static int check_sock_access(struct bpf_verifier_env *env, int insn_idx, · 61 unmodified lines
36423674 }
36433675
36443676 verbose(env, "R%d invalid %s access off=%d size=%d\n",
3645− regno, reg_type_str[reg->type], off, size);
3677+ regno, reg_type_str(env, reg->type), off, size);
36463678
36473679 return -EACCES;
36483680 }
@@ -4369,15 +4401,30 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 720 unmodified lines
43694401 mark_reg_unknown(env, regs, value_regno);
43704402 }
43714403 }
4372− } else if (reg->type == PTR_TO_MEM) {
4404+ } else if (base_type(reg->type) == PTR_TO_MEM) {
4405+ bool rdonly_mem = type_is_rdonly_mem(reg->type);
4406+
4407+ if (type_may_be_null(reg->type)) {
4408+ verbose(env, "R%d invalid mem access '%s'\n", regno,
4409+ reg_type_str(env, reg->type));
4410+ return -EACCES;
4411+ }
4412+
4413+ if (t == BPF_WRITE && rdonly_mem) {
4414+ verbose(env, "R%d cannot write into %s\n",
4415+ regno, reg_type_str(env, reg->type));
4416+ return -EACCES;
4417+ }
4418+
43734419 if (t == BPF_WRITE && value_regno >= 0 &&
43744420 is_pointer_value(env, value_regno)) {
43754421 verbose(env, "R%d leaks addr into mem\n", value_regno);
43764422 return -EACCES;
43774423 }
4424+
43784425 err = check_mem_region_access(env, regno, off, size,
43794426 reg->mem_size, false);
4380− if (!err && t == BPF_READ && value_regno >= 0)
4427+ if (!err && value_regno >= 0 && (t == BPF_READ || rdonly_mem))
43814428 mark_reg_unknown(env, regs, value_regno);
43824429 } else if (reg->type == PTR_TO_CTX) {
43834430 enum bpf_reg_type reg_type = SCALAR_VALUE;
@@ -4407,7 +4454,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 23 unmodified lines
44074454 } else {
44084455 mark_reg_known_zero(env, regs,
44094456 value_regno);
4410− if (reg_type_may_be_null(reg_type))
4457+ if (type_may_be_null(reg_type))
44114458 regs[value_regno].id = ++env->id_gen;
44124459 /* A load of ctx field could have different
44134460 * actual load size with the one encoded in the
@@ -4415,8 +4462,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 1 unmodified line
44154462 * a sub-register.
44164463 */
44174464 regs[value_regno].subreg_def = DEF_NOT_SUBREG;
4418− if (reg_type == PTR_TO_BTF_ID ||
4419− reg_type == PTR_TO_BTF_ID_OR_NULL) {
4465+ if (base_type(reg_type) == PTR_TO_BTF_ID) {
44204466 regs[value_regno].btf = btf;
44214467 regs[value_regno].btf_id = btf_id;
44224468 }
@@ -4469,7 +4515,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 46 unmodified lines
44694515 } else if (type_is_sk_pointer(reg->type)) {
44704516 if (t == BPF_WRITE) {
44714517 verbose(env, "R%d cannot write into %s\n",
4472− regno, reg_type_str[reg->type]);
4518+ regno, reg_type_str(env, reg->type));
44734519 return -EACCES;
44744520 }
44754521 err = check_sock_access(env, insn_idx, regno, off, size, t);
@@ -4485,26 +4531,32 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · 9 unmodified lines
44854531 } else if (reg->type == CONST_PTR_TO_MAP) {
44864532 err = check_ptr_to_map_access(env, regs, regno, off, size, t,
44874533 value_regno);
4488− } else if (reg->type == PTR_TO_RDONLY_BUF) {
4489− if (t == BPF_WRITE) {
4490− verbose(env, "R%d cannot write into %s\n",
4491− regno, reg_type_str[reg->type]);
4492− return -EACCES;
4534+ } else if (base_type(reg->type) == PTR_TO_BUF) {
4535+ bool rdonly_mem = type_is_rdonly_mem(reg->type);
4536+ const char *buf_info;
4537+ u32 *max_access;
4538+
4539+ if (rdonly_mem) {
4540+ if (t == BPF_WRITE) {
4541+ verbose(env, "R%d cannot write into %s\n",
4542+ regno, reg_type_str(env, reg->type));
4543+ return -EACCES;
4544+ }
4545+ buf_info = "rdonly";
4546+ max_access = &env->prog->aux->max_rdonly_access;
4547+ } else {
4548+ buf_info = "rdwr";
4549+ max_access = &env->prog->aux->max_rdwr_access;
44934550 }
4551+
44944552 err = check_buffer_access(env, reg, regno, off, size, false,
4495− "rdonly",
4496− &env->prog->aux->max_rdonly_access);
4497− if (!err && value_regno >= 0)
4498− mark_reg_unknown(env, regs, value_regno);
4499− } else if (reg->type == PTR_TO_RDWR_BUF) {
4500− err = check_buffer_access(env, reg, regno, off, size, false,
4501− "rdwr",
4502− &env->prog->aux->max_rdwr_access);
4503− if (!err && t == BPF_READ && value_regno >= 0)
4553+ buf_info, max_access);
4554+
4555+ if (!err && value_regno >= 0 && (rdonly_mem || t == BPF_READ))
45044556 mark_reg_unknown(env, regs, value_regno);
45054557 } else {
45064558 verbose(env, "R%d invalid mem access '%s'\n", regno,
4507− reg_type_str[reg->type]);
4559+ reg_type_str(env, reg->type));
45084560 return -EACCES;
45094561 }
45104562
@@ -4578,7 +4630,7 @@ static int check_atomic(struct bpf_verifier_env *env, int insn_idx, struct bpf_i · 67 unmodified lines
45784630 is_sk_reg(env, insn->dst_reg)) {
45794631 verbose(env, "BPF_ATOMIC stores into R%d %s is not allowed\n",
45804632 insn->dst_reg,
4581− reg_type_str[reg_state(env, insn->dst_reg)->type]);
4633+ reg_type_str(env, reg_state(env, insn->dst_reg)->type));
45824634 return -EACCES;
45834635 }
45844636
@@ -4761,8 +4813,10 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, · 176 unmodified lines
47614813 struct bpf_call_arg_meta *meta)
47624814 {
47634815 struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
4816+ const char *buf_info;
4817+ u32 *max_access;
47644818
4765− switch (reg->type) {
4819+ switch (base_type(reg->type)) {
47664820 case PTR_TO_PACKET:
47674821 case PTR_TO_PACKET_META:
47684822 return check_packet_access(env, regno, reg->off, access_size,
@@ -4781,18 +4835,20 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, · 12 unmodified lines
47814835 return check_mem_region_access(env, regno, reg->off,
47824836 access_size, reg->mem_size,
47834837 zero_size_allowed);
4784− case PTR_TO_RDONLY_BUF:
4785− if (meta && meta->raw_mode)
4786− return -EACCES;
4787− return check_buffer_access(env, reg, regno, reg->off,
4788− access_size, zero_size_allowed,
4789− "rdonly",
4790− &env->prog->aux->max_rdonly_access);
4791− case PTR_TO_RDWR_BUF:
4838+ case PTR_TO_BUF:
4839+ if (type_is_rdonly_mem(reg->type)) {
4840+ if (meta && meta->raw_mode)
4841+ return -EACCES;
4842+
4843+ buf_info = "rdonly";
4844+ max_access = &env->prog->aux->max_rdonly_access;
4845+ } else {
4846+ buf_info = "rdwr";
4847+ max_access = &env->prog->aux->max_rdwr_access;
4848+ }
47924849 return check_buffer_access(env, reg, regno, reg->off,
47934850 access_size, zero_size_allowed,
4794− "rdwr",
4795− &env->prog->aux->max_rdwr_access);
4851+ buf_info, max_access);
47964852 case PTR_TO_STACK:
47974853 return check_stack_range_initialized(
47984854 env,
@@ -4804,9 +4860,9 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, · 5 unmodified lines
48044860 register_is_null(reg))
48054861 return 0;
48064862
4807− verbose(env, "R%d type=%s expected=%s\n", regno,
4808− reg_type_str[reg->type],
4809− reg_type_str[PTR_TO_STACK]);
4863+ verbose(env, "R%d type=%s ", regno,
4864+ reg_type_str(env, reg->type));
4865+ verbose(env, "expected=%s\n", reg_type_str(env, PTR_TO_STACK));
48104866 return -EACCES;
48114867 }
48124868 }
@@ -4817,7 +4873,7 @@ int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg, · 4 unmodified lines
48174873 if (register_is_null(reg))
48184874 return 0;
48194875
4820− if (reg_type_may_be_null(reg->type)) {
4876+ if (type_may_be_null(reg->type)) {
48214877 /* Assuming that the register contains a value check if the memory
48224878 * access is safe. Temporarily save and restore the register's state as
48234879 * the conversion shouldn't be visible to a caller.
@@ -4965,9 +5021,8 @@ static int process_timer_func(struct bpf_verifier_env *env, int regno, · 141 unmodified lines
49655021
49665022 static bool arg_type_is_mem_ptr(enum bpf_arg_type type)
49675023 {
4968− return type == ARG_PTR_TO_MEM ||
4969− type == ARG_PTR_TO_MEM_OR_NULL ||
4970− type == ARG_PTR_TO_UNINIT_MEM;
5024+ return base_type(type) == ARG_PTR_TO_MEM ||
5025+ base_type(type) == ARG_PTR_TO_UNINIT_MEM;
49715026 }
49725027
49735028 static bool arg_type_is_mem_size(enum bpf_arg_type type)
@@ -5072,8 +5127,7 @@ static const struct bpf_reg_types mem_types = { · 98 unmodified lines
50725127 PTR_TO_MAP_KEY,
50735128 PTR_TO_MAP_VALUE,
50745129 PTR_TO_MEM,
5075− PTR_TO_RDONLY_BUF,
5076− PTR_TO_RDWR_BUF,
5130+ PTR_TO_BUF,
50775131 },
50785132 };
50795133
@@ -5104,31 +5158,26 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { · 24 unmodified lines
51045158 [ARG_PTR_TO_MAP_KEY] = &map_key_value_types,
51055159 [ARG_PTR_TO_MAP_VALUE] = &map_key_value_types,
51065160 [ARG_PTR_TO_UNINIT_MAP_VALUE] = &map_key_value_types,
5107− [ARG_PTR_TO_MAP_VALUE_OR_NULL] = &map_key_value_types,
51085161 [ARG_CONST_SIZE] = &scalar_types,
51095162 [ARG_CONST_SIZE_OR_ZERO] = &scalar_types,
51105163 [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types,
51115164 [ARG_CONST_MAP_PTR] = &const_map_ptr_types,
51125165 [ARG_PTR_TO_CTX] = &context_types,
5113− [ARG_PTR_TO_CTX_OR_NULL] = &context_types,
51145166 [ARG_PTR_TO_SOCK_COMMON] = &sock_types,
51155167 #ifdef CONFIG_NET
51165168 [ARG_PTR_TO_BTF_ID_SOCK_COMMON] = &btf_id_sock_common_types,
51175169 #endif
51185170 [ARG_PTR_TO_SOCKET] = &fullsock_types,
5119− [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types,
51205171 [ARG_PTR_TO_BTF_ID] = &btf_ptr_types,
51215172 [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types,
51225173 [ARG_PTR_TO_MEM] = &mem_types,
5123− [ARG_PTR_TO_MEM_OR_NULL] = &mem_types,
51245174 [ARG_PTR_TO_UNINIT_MEM] = &mem_types,
51255175 [ARG_PTR_TO_ALLOC_MEM] = &alloc_mem_types,
5126− [ARG_PTR_TO_ALLOC_MEM_OR_NULL] = &alloc_mem_types,
51275176 [ARG_PTR_TO_INT] = &int_ptr_types,
51285177 [ARG_PTR_TO_LONG] = &int_ptr_types,
51295178 [ARG_PTR_TO_PERCPU_BTF_ID] = &percpu_btf_ptr_types,
51305179 [ARG_PTR_TO_FUNC] = &func_ptr_types,
5131− [ARG_PTR_TO_STACK_OR_NULL] = &stack_ptr_types,
5180+ [ARG_PTR_TO_STACK] = &stack_ptr_types,
51325181 [ARG_PTR_TO_CONST_STR] = &const_str_ptr_types,
51335182 [ARG_PTR_TO_TIMER] = &timer_types,
51345183 };
@@ -5142,12 +5191,27 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · 7 unmodified lines
51425191 const struct bpf_reg_types *compatible;
51435192 int i, j;
51445193
5145− compatible = compatible_reg_types[arg_type];
5194+ compatible = compatible_reg_types[base_type(arg_type)];
51465195 if (!compatible) {
51475196 verbose(env, "verifier internal error: unsupported arg type %d\n", arg_type);
51485197 return -EFAULT;
51495198 }
51505199
5200+ /* ARG_PTR_TO_MEM + RDONLY is compatible with PTR_TO_MEM and PTR_TO_MEM + RDONLY,
5201+ * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM and NOT with PTR_TO_MEM + RDONLY
5202+ *
5203+ * Same for MAYBE_NULL:
5204+ *
5205+ * ARG_PTR_TO_MEM + MAYBE_NULL is compatible with PTR_TO_MEM and PTR_TO_MEM + MAYBE_NULL,
5206+ * but ARG_PTR_TO_MEM is compatible only with PTR_TO_MEM but NOT with PTR_TO_MEM + MAYBE_NULL
5207+ *
5208+ * Therefore we fold these flags depending on the arg_type before comparison.
5209+ */
5210+ if (arg_type & MEM_RDONLY)
5211+ type &= ~MEM_RDONLY;
5212+ if (arg_type & PTR_MAYBE_NULL)
5213+ type &= ~PTR_MAYBE_NULL;
5214+
51515215 for (i = 0; i < ARRAY_SIZE(compatible->types); i++) {
51525216 expected = compatible->types[i];
51535217 if (expected == NOT_INIT)
@@ -5157,14 +5221,14 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno, · 3 unmodified lines
51575221 goto found;
51585222 }
51595223
5160− verbose(env, "R%d type=%s expected=", regno, reg_type_str[type]);
5224+ verbose(env, "R%d type=%s expected=", regno, reg_type_str(env, reg->type));
51615225 for (j = 0; j + 1 < i; j++)
5162− verbose(env, "%s, ", reg_type_str[compatible->types[j]]);
5163− verbose(env, "%s\n", reg_type_str[compatible->types[j]]);
5226+ verbose(env, "%s, ", reg_type_str(env, compatible->types[j]));
5227+ verbose(env, "%s\n", reg_type_str(env, compatible->types[j]));
51645228 return -EACCES;
51655229
51665230 found:
5167− if (type == PTR_TO_BTF_ID) {
5231+ if (reg->type == PTR_TO_BTF_ID) {
51685232 if (!arg_btf_id) {
51695233 if (!compatible->btf_id) {
51705234 verbose(env, "verifier internal error: missing arg compatible BTF ID\n");
@@ -5223,15 +5287,14 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · 52 unmodified lines
52235287 return -EACCES;
52245288 }
52255289
5226− if (arg_type == ARG_PTR_TO_MAP_VALUE ||
5227− arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE ||
5228− arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL) {
5290+ if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
5291+ base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) {
52295292 err = resolve_map_arg_type(env, meta, &arg_type);
52305293 if (err)
52315294 return err;
52325295 }
52335296
5234− if (register_is_null(reg) && arg_type_may_be_null(arg_type))
5297+ if (register_is_null(reg) && type_may_be_null(arg_type))
52355298 /* A NULL register has a SCALAR_VALUE type, so skip
52365299 * type checking.
52375300 */
@@ -5300,10 +5363,11 @@ skip_type_check: · 62 unmodified lines
53005363 err = check_helper_mem_access(env, regno,
53015364 meta->map_ptr->key_size, false,
53025365 NULL);
5303− } else if (arg_type == ARG_PTR_TO_MAP_VALUE ||
5304− (arg_type == ARG_PTR_TO_MAP_VALUE_OR_NULL &&
5305− !register_is_null(reg)) ||
5306− arg_type == ARG_PTR_TO_UNINIT_MAP_VALUE) {
5366+ } else if (base_type(arg_type) == ARG_PTR_TO_MAP_VALUE ||
5367+ base_type(arg_type) == ARG_PTR_TO_UNINIT_MAP_VALUE) {
5368+ if (type_may_be_null(arg_type) && register_is_null(reg))
5369+ return 0;
5370+
53075371 /* bpf_map_xxx(..., map_ptr, ..., value) call:
53085372 * check [value, value + map->value_size) validity
53095373 */
@@ -6025,9 +6089,9 @@ static int __check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn · 715 unmodified lines
60256089
60266090 if (env->log.level & BPF_LOG_LEVEL) {
60276091 verbose(env, "caller:\n");
6028− print_verifier_state(env, caller);
6092+ print_verifier_state(env, caller, true);
60296093 verbose(env, "callee:\n");
6030− print_verifier_state(env, callee);
6094+ print_verifier_state(env, callee, true);
60316095 }
60326096 return 0;
60336097 }
@@ -6242,9 +6306,9 @@ static int prepare_func_exit(struct bpf_verifier_env *env, int *insn_idx) · 208 unmodified lines
62426306 *insn_idx = callee->callsite + 1;
62436307 if (env->log.level & BPF_LOG_LEVEL) {
62446308 verbose(env, "returning from callee:\n");
6245− print_verifier_state(env, callee);
6309+ print_verifier_state(env, callee, true);
62466310 verbose(env, "to caller at %d:\n", *insn_idx);
6247− print_verifier_state(env, caller);
6311+ print_verifier_state(env, caller, true);
62486312 }
62496313 /* clear everything in the callee */
62506314 free_func_state(callee);
@@ -6410,13 +6474,11 @@ static int check_bpf_snprintf_call(struct bpf_verifier_env *env, · 159 unmodified lines
64106474
64116475 static int check_get_func_ip(struct bpf_verifier_env *env)
64126476 {
6413− enum bpf_attach_type eatype = env->prog->expected_attach_type;
64146477 enum bpf_prog_type type = resolve_prog_type(env->prog);
64156478 int func_id = BPF_FUNC_get_func_ip;
64166479
64176480 if (type == BPF_PROG_TYPE_TRACING) {
6418− if (eatype != BPF_TRACE_FENTRY && eatype != BPF_TRACE_FEXIT &&
6419− eatype != BPF_MODIFY_RETURN) {
6481+ if (!bpf_prog_has_trampoline(env->prog)) {
64206482 verbose(env, "func %s#%d supported only for fentry/fexit/fmod_ret programs\n",
64216483 func_id_name(func_id), func_id);
64226484 return -ENOTSUPP;
@@ -6435,6 +6497,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · 12 unmodified lines
64356497 int *insn_idx_p)
64366498 {
64376499 const struct bpf_func_proto *fn = NULL;
6500+ enum bpf_return_type ret_type;
6501+ enum bpf_type_flag ret_flag;
64386502 struct bpf_reg_state *regs;
64396503 struct bpf_call_arg_meta meta;
64406504 int insn_idx = *insn_idx_p;
@@ -6574,13 +6638,14 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · 133 unmodified lines
65746638 regs[BPF_REG_0].subreg_def = DEF_NOT_SUBREG;
65756639
65766640 /* update return register (already marked as written above) */
6577− if (fn->ret_type == RET_INTEGER) {
6641+ ret_type = fn->ret_type;
6642+ ret_flag = type_flag(fn->ret_type);
6643+ if (ret_type == RET_INTEGER) {
65786644 /* sets type to SCALAR_VALUE */
65796645 mark_reg_unknown(env, regs, BPF_REG_0);
6580− } else if (fn->ret_type == RET_VOID) {
6646+ } else if (ret_type == RET_VOID) {
65816647 regs[BPF_REG_0].type = NOT_INIT;
6582− } else if (fn->ret_type == RET_PTR_TO_MAP_VALUE_OR_NULL ||
6583− fn->ret_type == RET_PTR_TO_MAP_VALUE) {
6648+ } else if (base_type(ret_type) == RET_PTR_TO_MAP_VALUE) {
65846649 /* There is no offset yet applied, variable or fixed */
65856650 mark_reg_known_zero(env, regs, BPF_REG_0);
65866651 /* remember map_ptr, so that check_map_access()
@@ -6594,28 +6659,25 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · 7 unmodified lines
65946659 }
65956660 regs[BPF_REG_0].map_ptr = meta.map_ptr;
65966661 regs[BPF_REG_0].map_uid = meta.map_uid;
6597− if (fn->ret_type == RET_PTR_TO_MAP_VALUE) {
6598− regs[BPF_REG_0].type = PTR_TO_MAP_VALUE;
6599− if (map_value_has_spin_lock(meta.map_ptr))
6600− regs[BPF_REG_0].id = ++env->id_gen;
6601− } else {
6602− regs[BPF_REG_0].type = PTR_TO_MAP_VALUE_OR_NULL;
6662+ regs[BPF_REG_0].type = PTR_TO_MAP_VALUE | ret_flag;
6663+ if (!type_may_be_null(ret_type) &&
6664+ map_value_has_spin_lock(meta.map_ptr)) {
6665+ regs[BPF_REG_0].id = ++env->id_gen;
66036666 }
6604− } else if (fn->ret_type == RET_PTR_TO_SOCKET_OR_NULL) {
6667+ } else if (base_type(ret_type) == RET_PTR_TO_SOCKET) {
66056668 mark_reg_known_zero(env, regs, BPF_REG_0);
6606− regs[BPF_REG_0].type = PTR_TO_SOCKET_OR_NULL;
6607− } else if (fn->ret_type == RET_PTR_TO_SOCK_COMMON_OR_NULL) {
6669+ regs[BPF_REG_0].type = PTR_TO_SOCKET | ret_flag;
6670+ } else if (base_type(ret_type) == RET_PTR_TO_SOCK_COMMON) {
66086671 mark_reg_known_zero(env, regs, BPF_REG_0);
6609− regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON_OR_NULL;
6610− } else if (fn->ret_type == RET_PTR_TO_TCP_SOCK_OR_NULL) {
6672+ regs[BPF_REG_0].type = PTR_TO_SOCK_COMMON | ret_flag;
6673+ } else if (base_type(ret_type) == RET_PTR_TO_TCP_SOCK) {
66116674 mark_reg_known_zero(env, regs, BPF_REG_0);
6612− regs[BPF_REG_0].type = PTR_TO_TCP_SOCK_OR_NULL;
6613− } else if (fn->ret_type == RET_PTR_TO_ALLOC_MEM_OR_NULL) {
6675+ regs[BPF_REG_0].type = PTR_TO_TCP_SOCK | ret_flag;
6676+ } else if (base_type(ret_type) == RET_PTR_TO_ALLOC_MEM) {
66146677 mark_reg_known_zero(env, regs, BPF_REG_0);
6615− regs[BPF_REG_0].type = PTR_TO_MEM_OR_NULL;
6678+ regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
66166679 regs[BPF_REG_0].mem_size = meta.mem_size;
6617− } else if (fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID_OR_NULL ||
6618− fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID) {
6680+ } else if (base_type(ret_type) == RET_PTR_TO_MEM_OR_BTF_ID) {
66196681 const struct btf_type *t;
66206682
66216683 mark_reg_known_zero(env, regs, BPF_REG_0);
@@ -6633,29 +6695,30 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · 11 unmodified lines
66336695 tname, PTR_ERR(ret));
66346696 return -EINVAL;
66356697 }
6636− regs[BPF_REG_0].type =
6637− fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ?
6638− PTR_TO_MEM : PTR_TO_MEM_OR_NULL;
6698+ regs[BPF_REG_0].type = PTR_TO_MEM | ret_flag;
66396699 regs[BPF_REG_0].mem_size = tsize;
66406700 } else {
6641− regs[BPF_REG_0].type =
6642− fn->ret_type == RET_PTR_TO_MEM_OR_BTF_ID ?
6643− PTR_TO_BTF_ID : PTR_TO_BTF_ID_OR_NULL;
6701+ /* MEM_RDONLY may be carried from ret_flag, but it
6702+ * doesn't apply on PTR_TO_BTF_ID. Fold it, otherwise
6703+ * it will confuse the check of PTR_TO_BTF_ID in
6704+ * check_mem_access().
6705+ */
6706+ ret_flag &= ~MEM_RDONLY;
6707+
6708+ regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag;
66446709 regs[BPF_REG_0].btf = meta.ret_btf;
66456710 regs[BPF_REG_0].btf_id = meta.ret_btf_id;
66466711 }
6647− } else if (fn->ret_type == RET_PTR_TO_BTF_ID_OR_NULL ||
6648− fn->ret_type == RET_PTR_TO_BTF_ID) {
6712+ } else if (base_type(ret_type) == RET_PTR_TO_BTF_ID) {
66496713 int ret_btf_id;
66506714
66516715 mark_reg_known_zero(env, regs, BPF_REG_0);
6652− regs[BPF_REG_0].type = fn->ret_type == RET_PTR_TO_BTF_ID ?
6653− PTR_TO_BTF_ID :
6654− PTR_TO_BTF_ID_OR_NULL;
6716+ regs[BPF_REG_0].type = PTR_TO_BTF_ID | ret_flag;
66556717 ret_btf_id = *fn->ret_btf_id;
66566718 if (ret_btf_id == 0) {
6657− verbose(env, "invalid return type %d of func %s#%d\n",
6658− fn->ret_type, func_id_name(func_id), func_id);
6719+ verbose(env, "invalid return type %u of func %s#%d\n",
6720+ base_type(ret_type), func_id_name(func_id),
6721+ func_id);
66596722 return -EINVAL;
66606723 }
66616724 /* current BPF helper definitions are only coming from
@@ -6664,12 +6727,12 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · 2 unmodified lines
66646727 regs[BPF_REG_0].btf = btf_vmlinux;
66656728 regs[BPF_REG_0].btf_id = ret_btf_id;
66666729 } else {
6667− verbose(env, "unknown return type %d of func %s#%d\n",
6668− fn->ret_type, func_id_name(func_id), func_id);
6730+ verbose(env, "unknown return type %u of func %s#%d\n",
6731+ base_type(ret_type), func_id_name(func_id), func_id);
66696732 return -EINVAL;
66706733 }
66716734
6672− if (reg_type_may_be_null(regs[BPF_REG_0].type))
6735+ if (type_may_be_null(regs[BPF_REG_0].type))
66736736 regs[BPF_REG_0].id = ++env->id_gen;
66746737
66756738 if (is_ptr_cast_function(func_id)) {
@@ -6878,25 +6941,25 @@ static bool check_reg_sane_offset(struct bpf_verifier_env *env, · 202 unmodified lines
68786941
68796942 if (known && (val >= BPF_MAX_VAR_OFF || val <= -BPF_MAX_VAR_OFF)) {
68806943 verbose(env, "math between %s pointer and %lld is not allowed\n",
6881− reg_type_str[type], val);
6944+ reg_type_str(env, type), val);
68826945 return false;
68836946 }
68846947
68856948 if (reg->off >= BPF_MAX_VAR_OFF || reg->off <= -BPF_MAX_VAR_OFF) {
68866949 verbose(env, "%s pointer offset %d is not allowed\n",
6887− reg_type_str[type], reg->off);
6950+ reg_type_str(env, type), reg->off);
68886951 return false;
68896952 }
68906953
68916954 if (smin == S64_MIN) {
68926955 verbose(env, "math between %s pointer and register with unbounded min value is not allowed\n",
6893− reg_type_str[type]);
6956+ reg_type_str(env, type));
68946957 return false;
68956958 }
68966959
68976960 if (smin >= BPF_MAX_VAR_OFF || smin <= -BPF_MAX_VAR_OFF) {
68986961 verbose(env, "value %lld makes %s pointer be out of bounds\n",
6899− smin, reg_type_str[type]);
6962+ smin, reg_type_str(env, type));
69006963 return false;
69016964 }
69026965
@@ -7273,11 +7336,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · 370 unmodified lines
72737336 return -EACCES;
72747337 }
72757338
7276− switch (ptr_reg->type) {
7277− case PTR_TO_MAP_VALUE_OR_NULL:
7339+ if (ptr_reg->type & PTR_MAYBE_NULL) {
72787340 verbose(env, "R%d pointer arithmetic on %s prohibited, null-check it first\n",
7279− dst, reg_type_str[ptr_reg->type]);
7341+ dst, reg_type_str(env, ptr_reg->type));
72807342 return -EACCES;
7343+ }
7344+
7345+ switch (base_type(ptr_reg->type)) {
72817346 case CONST_PTR_TO_MAP:
72827347 /* smin_val represents the known value */
72837348 if (known && smin_val == 0 && opcode == BPF_ADD)
@@ -7285,14 +7350,11 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · 1 unmodified line
72857350 fallthrough;
72867351 case PTR_TO_PACKET_END:
72877352 case PTR_TO_SOCKET:
7288− case PTR_TO_SOCKET_OR_NULL:
72897353 case PTR_TO_SOCK_COMMON:
7290− case PTR_TO_SOCK_COMMON_OR_NULL:
72917354 case PTR_TO_TCP_SOCK:
7292− case PTR_TO_TCP_SOCK_OR_NULL:
72937355 case PTR_TO_XDP_SOCK:
72947356 verbose(env, "R%d pointer arithmetic on %s prohibited\n",
7295− dst, reg_type_str[ptr_reg->type]);
7357+ dst, reg_type_str(env, ptr_reg->type));
72967358 return -EACCES;
72977359 default:
72987360 break;
@@ -8265,12 +8327,12 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, · 966 unmodified lines
82658327
82668328 /* Got here implies adding two SCALAR_VALUEs */
82678329 if (WARN_ON_ONCE(ptr_reg)) {
8268− print_verifier_state(env, state);
8330+ print_verifier_state(env, state, true);
82698331 verbose(env, "verifier internal error: unexpected ptr_reg\n");
82708332 return -EINVAL;
82718333 }
82728334 if (WARN_ON(!src_reg)) {
8273− print_verifier_state(env, state);
8335+ print_verifier_state(env, state, true);
82748336 verbose(env, "verifier internal error: no src_reg\n");
82758337 return -EINVAL;
82768338 }
@@ -9015,7 +9077,7 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state, · 738 unmodified lines
90159077 struct bpf_reg_state *reg, u32 id,
90169078 bool is_null)
90179079 {
9018− if (reg_type_may_be_null(reg->type) && reg->id == id &&
9080+ if (type_may_be_null(reg->type) && reg->id == id &&
90199081 !WARN_ON_ONCE(!reg->id)) {
90209082 /* Old offset (both fixed and variable parts) should
90219083 * have been known-zero, because we don't allow pointer
@@ -9393,7 +9455,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, · 371 unmodified lines
93939455 */
93949456 if (!is_jmp32 && BPF_SRC(insn->code) == BPF_K &&
93959457 insn->imm == 0 && (opcode == BPF_JEQ || opcode == BPF_JNE) &&
9396− reg_type_may_be_null(dst_reg->type)) {
9458+ type_may_be_null(dst_reg->type)) {
93979459 /* Mark all identical registers in each branch as either
93989460 * safe or unknown depending R == 0 or R != 0 conditional.
93999461 */
@@ -9409,7 +9471,7 @@ static int check_cond_jmp_op(struct bpf_verifier_env *env, · 9 unmodified lines
94099471 return -EACCES;
94109472 }
94119473 if (env->log.level & BPF_LOG_LEVEL)
9412− print_verifier_state(env, this_branch->frame[this_branch->curframe]);
9474+ print_insn_state(env, this_branch->frame[this_branch->curframe]);
94139475 return 0;
94149476 }
94159477
@@ -9448,7 +9510,7 @@ static int check_ld_imm(struct bpf_verifier_env *env, struct bpf_insn *insn) · 32 unmodified lines
94489510 mark_reg_known_zero(env, regs, insn->dst_reg);
94499511
94509512 dst_reg->type = aux->btf_var.reg_type;
9451− switch (dst_reg->type) {
9513+ switch (base_type(dst_reg->type)) {
94529514 case PTR_TO_MEM:
94539515 dst_reg->mem_size = aux->btf_var.mem_size;
94549516 break;
@@ -9647,7 +9709,7 @@ static int check_return_code(struct bpf_verifier_env *env) · 192 unmodified lines
96479709 /* enforce return zero from async callbacks like timer */
96489710 if (reg->type != SCALAR_VALUE) {
96499711 verbose(env, "In async callback the register R0 is not a known value (%s)\n",
9650− reg_type_str[reg->type]);
9712+ reg_type_str(env, reg->type));
96519713 return -EINVAL;
96529714 }
96539715
@@ -9661,7 +9723,7 @@ static int check_return_code(struct bpf_verifier_env *env) · 7 unmodified lines
96619723 if (is_subprog) {
96629724 if (reg->type != SCALAR_VALUE) {
96639725 verbose(env, "At subprogram exit the register R0 is not a scalar value (%s)\n",
9664− reg_type_str[reg->type]);
9726+ reg_type_str(env, reg->type));
96659727 return -EINVAL;
96669728 }
96679729 return 0;
@@ -9725,7 +9787,7 @@ static int check_return_code(struct bpf_verifier_env *env) · 57 unmodified lines
97259787
97269788 if (reg->type != SCALAR_VALUE) {
97279789 verbose(env, "At program exit the register R0 is not a known value (%s)\n",
9728− reg_type_str[reg->type]);
9790+ reg_type_str(env, reg->type));
97299791 return -EINVAL;
97309792 }
97319793
@@ -10582,7 +10644,7 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, · 850 unmodified lines
1058210644 return true;
1058310645 if (rcur->type == NOT_INIT)
1058410646 return false;
10585− switch (rold->type) {
10647+ switch (base_type(rold->type)) {
1058610648 case SCALAR_VALUE:
1058710649 if (env->explore_alu_limits)
1058810650 return false;
@@ -10604,6 +10666,22 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, · 15 unmodified lines
1060410666 }
1060510667 case PTR_TO_MAP_KEY:
1060610668 case PTR_TO_MAP_VALUE:
10669+ /* a PTR_TO_MAP_VALUE could be safe to use as a
10670+ * PTR_TO_MAP_VALUE_OR_NULL into the same map.
10671+ * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
10672+ * checked, doing so could have affected others with the same
10673+ * id, and we can't check for that because we lost the id when
10674+ * we converted to a PTR_TO_MAP_VALUE.
10675+ */
10676+ if (type_may_be_null(rold->type)) {
10677+ if (!type_may_be_null(rcur->type))
10678+ return false;
10679+ if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
10680+ return false;
10681+ /* Check our ids match any regs they're supposed to */
10682+ return check_ids(rold->id, rcur->id, idmap);
10683+ }
10684+
1060710685 /* If the new min/max/var_off satisfy the old ones and
1060810686 * everything else matches, we are OK.
1060910687 * 'id' is not compared, since it's only used for maps with
@@ -10615,20 +10693,6 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, · 5 unmodified lines
1061510693 return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
1061610694 range_within(rold, rcur) &&
1061710695 tnum_in(rold->var_off, rcur->var_off);
10618− case PTR_TO_MAP_VALUE_OR_NULL:
10619− /* a PTR_TO_MAP_VALUE could be safe to use as a
10620− * PTR_TO_MAP_VALUE_OR_NULL into the same map.
10621− * However, if the old PTR_TO_MAP_VALUE_OR_NULL then got NULL-
10622− * checked, doing so could have affected others with the same
10623− * id, and we can't check for that because we lost the id when
10624− * we converted to a PTR_TO_MAP_VALUE.
10625− */
10626− if (rcur->type != PTR_TO_MAP_VALUE_OR_NULL)
10627− return false;
10628− if (memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)))
10629− return false;
10630− /* Check our ids match any regs they're supposed to */
10631− return check_ids(rold->id, rcur->id, idmap);
1063210696 case PTR_TO_PACKET_META:
1063310697 case PTR_TO_PACKET:
1063410698 if (rcur->type != rold->type)
@@ -10657,11 +10721,8 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold, · 22 unmodified lines
1065710721 case PTR_TO_PACKET_END:
1065810722 case PTR_TO_FLOW_KEYS:
1065910723 case PTR_TO_SOCKET:
10660− case PTR_TO_SOCKET_OR_NULL:
1066110724 case PTR_TO_SOCK_COMMON:
10662− case PTR_TO_SOCK_COMMON_OR_NULL:
1066310725 case PTR_TO_TCP_SOCK:
10664− case PTR_TO_TCP_SOCK_OR_NULL:
1066510726 case PTR_TO_XDP_SOCK:
1066610727 /* Only valid matches are exact, which memcmp() above
1066710728 * would have accepted
@@ -11187,17 +11248,13 @@ next: · 519 unmodified lines
1118711248 /* Return true if it's OK to have the same insn return a different type. */
1118811249 static bool reg_type_mismatch_ok(enum bpf_reg_type type)
1118911250 {
11190− switch (type) {
11251+ switch (base_type(type)) {
1119111252 case PTR_TO_CTX:
1119211253 case PTR_TO_SOCKET:
11193− case PTR_TO_SOCKET_OR_NULL:
1119411254 case PTR_TO_SOCK_COMMON:
11195− case PTR_TO_SOCK_COMMON_OR_NULL:
1119611255 case PTR_TO_TCP_SOCK:
11197− case PTR_TO_TCP_SOCK_OR_NULL:
1119811256 case PTR_TO_XDP_SOCK:
1119911257 case PTR_TO_BTF_ID:
11200− case PTR_TO_BTF_ID_OR_NULL:
1120111258 return false;
1120211259 default:
1120311260 return true;
@@ -11277,16 +11334,12 @@ static int do_check(struct bpf_verifier_env *env) · 73 unmodified lines
1127711334 if (need_resched())
1127811335 cond_resched();
1127911336
11280− if (env->log.level & BPF_LOG_LEVEL2 ||
11281− (env->log.level & BPF_LOG_LEVEL && do_print_state)) {
11282− if (env->log.level & BPF_LOG_LEVEL2)
11283− verbose(env, "%d:", env->insn_idx);
11284− else
11285− verbose(env, "\nfrom %d to %d%s:",
11286− env->prev_insn_idx, env->insn_idx,
11287− env->cur_state->speculative ?
11288− " (speculative execution)" : "");
11289− print_verifier_state(env, state->frame[state->curframe]);
11337+ if (env->log.level & BPF_LOG_LEVEL2 && do_print_state) {
11338+ verbose(env, "\nfrom %d to %d%s:",
11339+ env->prev_insn_idx, env->insn_idx,
11340+ env->cur_state->speculative ?
11341+ " (speculative execution)" : "");
11342+ print_verifier_state(env, state->frame[state->curframe], true);
1129011343 do_print_state = false;
1129111344 }
1129211345
@@ -11297,9 +11350,15 @@ static int do_check(struct bpf_verifier_env *env) · 4 unmodified lines
1129711350 .private_data = env,
1129811351 };
1129911352
11353+ if (verifier_state_scratched(env))
11354+ print_insn_state(env, state->frame[state->curframe]);
11355+
1130011356 verbose_linfo(env, env->insn_idx, "; ");
11357+ env->prev_log_len = env->log.len_used;
1130111358 verbose(env, "%d: ", env->insn_idx);
1130211359 print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
11360+ env->prev_insn_print_len = env->log.len_used - env->prev_log_len;
11361+ env->prev_log_len = env->log.len_used;
1130311362 }
1130411363
1130511364 if (bpf_prog_is_dev_bound(env->prog->aux)) {
@@ -11421,7 +11480,7 @@ static int do_check(struct bpf_verifier_env *env) · 115 unmodified lines
1142111480 if (is_ctx_reg(env, insn->dst_reg)) {
1142211481 verbose(env, "BPF_ST stores into R%d %s is not allowed\n",
1142311482 insn->dst_reg,
11424− reg_type_str[reg_state(env, insn->dst_reg)->type]);
11483+ reg_type_str(env, reg_state(env, insn->dst_reg)->type));
1142511484 return -EACCES;
1142611485 }
1142711486
@@ -11508,6 +11567,7 @@ static int do_check(struct bpf_verifier_env *env) · 80 unmodified lines
1150811567 if (err)
1150911568 return err;
1151011569 process_bpf_exit:
11570+ mark_verifier_state_scratched(env);
1151111571 update_branch_counts(env, env->cur_state);
1151211572 err = pop_stack(env, &prev_insn_idx,
1151311573 &env->insn_idx, pop_log);
@@ -11673,7 +11733,7 @@ static int check_pseudo_btf_id(struct bpf_verifier_env *env, · 159 unmodified lines
1167311733 err = -EINVAL;
1167411734 goto err_put;
1167511735 }
11676− aux->btf_var.reg_type = PTR_TO_MEM;
11736+ aux->btf_var.reg_type = PTR_TO_MEM | MEM_RDONLY;
1167711737 aux->btf_var.mem_size = tsize;
1167811738 } else {
1167911739 aux->btf_var.reg_type = PTR_TO_BTF_ID;
@@ -11833,6 +11893,9 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env, · 153 unmodified lines
1183311893 }
1183411894 break;
1183511895 case BPF_MAP_TYPE_RINGBUF:
11896+ case BPF_MAP_TYPE_INODE_STORAGE:
11897+ case BPF_MAP_TYPE_SK_STORAGE:
11898+ case BPF_MAP_TYPE_TASK_STORAGE:
1183611899 break;
1183711900 default:
1183811901 verbose(env,
@@ -13016,6 +13079,7 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, · 1177 unmodified lines
1301613079 static int do_misc_fixups(struct bpf_verifier_env *env)
1301713080 {
1301813081 struct bpf_prog *prog = env->prog;
13082+ enum bpf_attach_type eatype = prog->expected_attach_type;
1301913083 bool expect_blinding = bpf_jit_blinding_enabled(prog);
1302013084 enum bpf_prog_type prog_type = resolve_prog_type(prog);
1302113085 struct bpf_insn *insn = prog->insnsi;
@@ -13386,11 +13450,79 @@ patch_map_ops_generic: · 364 unmodified lines
1338613450 continue;
1338713451 }
1338813452
13453+ /* Implement bpf_get_func_arg inline. */
13454+ if (prog_type == BPF_PROG_TYPE_TRACING &&
13455+ insn->imm == BPF_FUNC_get_func_arg) {
13456+ /* Load nr_args from ctx - 8 */
13457+ insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
13458+ insn_buf[1] = BPF_JMP32_REG(BPF_JGE, BPF_REG_2, BPF_REG_0, 6);
13459+ insn_buf[2] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_2, 3);
13460+ insn_buf[3] = BPF_ALU64_REG(BPF_ADD, BPF_REG_2, BPF_REG_1);
13461+ insn_buf[4] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_2, 0);
13462+ insn_buf[5] = BPF_STX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0);
13463+ insn_buf[6] = BPF_MOV64_IMM(BPF_REG_0, 0);
13464+ insn_buf[7] = BPF_JMP_A(1);
13465+ insn_buf[8] = BPF_MOV64_IMM(BPF_REG_0, -EINVAL);
13466+ cnt = 9;
13467+
13468+ new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
13469+ if (!new_prog)
13470+ return -ENOMEM;
13471+
13472+ delta += cnt - 1;
13473+ env->prog = prog = new_prog;
13474+ insn = new_prog->insnsi + i + delta;
13475+ continue;
13476+ }
13477+
13478+ /* Implement bpf_get_func_ret inline. */
13479+ if (prog_type == BPF_PROG_TYPE_TRACING &&
13480+ insn->imm == BPF_FUNC_get_func_ret) {
13481+ if (eatype == BPF_TRACE_FEXIT ||
13482+ eatype == BPF_MODIFY_RETURN) {
13483+ /* Load nr_args from ctx - 8 */
13484+ insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
13485+ insn_buf[1] = BPF_ALU64_IMM(BPF_LSH, BPF_REG_0, 3);
13486+ insn_buf[2] = BPF_ALU64_REG(BPF_ADD, BPF_REG_0, BPF_REG_1);
13487+ insn_buf[3] = BPF_LDX_MEM(BPF_DW, BPF_REG_3, BPF_REG_0, 0);
13488+ insn_buf[4] = BPF_STX_MEM(BPF_DW, BPF_REG_2, BPF_REG_3, 0);
13489+ insn_buf[5] = BPF_MOV64_IMM(BPF_REG_0, 0);
13490+ cnt = 6;
13491+ } else {
13492+ insn_buf[0] = BPF_MOV64_IMM(BPF_REG_0, -EOPNOTSUPP);
13493+ cnt = 1;
13494+ }
13495+
13496+ new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
13497+ if (!new_prog)
13498+ return -ENOMEM;
13499+
13500+ delta += cnt - 1;
13501+ env->prog = prog = new_prog;
13502+ insn = new_prog->insnsi + i + delta;
13503+ continue;
13504+ }
13505+
13506+ /* Implement get_func_arg_cnt inline. */
13507+ if (prog_type == BPF_PROG_TYPE_TRACING &&
13508+ insn->imm == BPF_FUNC_get_func_arg_cnt) {
13509+ /* Load nr_args from ctx - 8 */
13510+ insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
13511+
13512+ new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1);
13513+ if (!new_prog)
13514+ return -ENOMEM;
13515+
13516+ env->prog = prog = new_prog;
13517+ insn = new_prog->insnsi + i + delta;
13518+ continue;
13519+ }
13520+
1338913521 /* Implement bpf_get_func_ip inline. */
1339013522 if (prog_type == BPF_PROG_TYPE_TRACING &&
1339113523 insn->imm == BPF_FUNC_get_func_ip) {
13392− /* Load IP address from ctx - 8 */
13393− insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -8);
13524+ /* Load IP address from ctx - 16 */
13525+ insn_buf[0] = BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, -16);
1339413526
1339513527 new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, 1);
1339613528 if (!new_prog)
@@ -13504,7 +13636,7 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog) · 107 unmodified lines
1350413636 mark_reg_known_zero(env, regs, i);
1350513637 else if (regs[i].type == SCALAR_VALUE)
1350613638 mark_reg_unknown(env, regs, i);
13507− else if (regs[i].type == PTR_TO_MEM_OR_NULL) {
13639+ else if (base_type(regs[i].type) == PTR_TO_MEM) {
1350813640 const u32 mem_size = regs[i].mem_size;
1350913641
1351013642 mark_reg_known_zero(env, regs, i);
@@ -14099,6 +14231,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr) · 588 unmodified lines
1409914231 }
1410014232 }
1410114233
14234+ mark_verifier_state_clean(env);
14235+
1410214236 if (IS_ERR(btf_vmlinux)) {
1410314237 /* Either gcc or pahole or kernel are broken. */
1410414238 verbose(env, "in-kernel BTF is malformed\n");
kernel/cgroup/cgroup.c+1−0
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030
3131 #include "cgroup-internal.h"
3232
33+#include <linux/bpf-cgroup.h>
3334 #include <linux/cred.h>
3435 #include <linux/errno.h>
3536 #include <linux/init_task.h>
kernel/sysctl.c+1−0
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333 #include <linux/security.h>
3434 #include <linux/ctype.h>
3535 #include <linux/kmemleak.h>
36+#include <linux/filter.h>
3637 #include <linux/fs.h>
3738 #include <linux/init.h>
3839 #include <linux/kernel.h>
kernel/trace/bpf_trace.c+67−14
@@ -345,7 +345,7 @@ static const struct bpf_func_proto bpf_probe_write_user_proto = { · 344 unmodified lines
345345 .gpl_only = true,
346346 .ret_type = RET_INTEGER,
347347 .arg1_type = ARG_ANYTHING,
348− .arg2_type = ARG_PTR_TO_MEM,
348+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
349349 .arg3_type = ARG_CONST_SIZE,
350350 };
351351
@@ -394,7 +394,7 @@ static const struct bpf_func_proto bpf_trace_printk_proto = { · 42 unmodified lines
394394 .func = bpf_trace_printk,
395395 .gpl_only = true,
396396 .ret_type = RET_INTEGER,
397− .arg1_type = ARG_PTR_TO_MEM,
397+ .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
398398 .arg2_type = ARG_CONST_SIZE,
399399 };
400400
@@ -450,9 +450,9 @@ static const struct bpf_func_proto bpf_trace_vprintk_proto = { · 49 unmodified lines
450450 .func = bpf_trace_vprintk,
451451 .gpl_only = true,
452452 .ret_type = RET_INTEGER,
453− .arg1_type = ARG_PTR_TO_MEM,
453+ .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY,
454454 .arg2_type = ARG_CONST_SIZE,
455− .arg3_type = ARG_PTR_TO_MEM_OR_NULL,
455+ .arg3_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
456456 .arg4_type = ARG_CONST_SIZE_OR_ZERO,
457457 };
458458
@@ -492,9 +492,9 @@ static const struct bpf_func_proto bpf_seq_printf_proto = { · 33 unmodified lines
492492 .ret_type = RET_INTEGER,
493493 .arg1_type = ARG_PTR_TO_BTF_ID,
494494 .arg1_btf_id = &btf_seq_file_ids[0],
495− .arg2_type = ARG_PTR_TO_MEM,
495+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
496496 .arg3_type = ARG_CONST_SIZE,
497− .arg4_type = ARG_PTR_TO_MEM_OR_NULL,
497+ .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY,
498498 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
499499 };
500500
@@ -509,7 +509,7 @@ static const struct bpf_func_proto bpf_seq_write_proto = { · 8 unmodified lines
509509 .ret_type = RET_INTEGER,
510510 .arg1_type = ARG_PTR_TO_BTF_ID,
511511 .arg1_btf_id = &btf_seq_file_ids[0],
512− .arg2_type = ARG_PTR_TO_MEM,
512+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
513513 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
514514 };
515515
@@ -533,7 +533,7 @@ static const struct bpf_func_proto bpf_seq_printf_btf_proto = { · 17 unmodified lines
533533 .ret_type = RET_INTEGER,
534534 .arg1_type = ARG_PTR_TO_BTF_ID,
535535 .arg1_btf_id = &btf_seq_file_ids[0],
536− .arg2_type = ARG_PTR_TO_MEM,
536+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
537537 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
538538 .arg4_type = ARG_ANYTHING,
539539 };
@@ -694,7 +694,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto = { · 154 unmodified lines
694694 .arg1_type = ARG_PTR_TO_CTX,
695695 .arg2_type = ARG_CONST_MAP_PTR,
696696 .arg3_type = ARG_ANYTHING,
697− .arg4_type = ARG_PTR_TO_MEM,
697+ .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
698698 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
699699 };
700700
@@ -1004,7 +1004,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { · 303 unmodified lines
10041004 .ret_type = RET_INTEGER,
10051005 .arg1_type = ARG_PTR_TO_MEM,
10061006 .arg2_type = ARG_CONST_SIZE,
1007− .arg3_type = ARG_PTR_TO_MEM,
1007+ .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY,
10081008 .arg4_type = ARG_CONST_SIZE,
10091009 .arg5_type = ARG_ANYTHING,
10101010 };
@@ -1012,7 +1012,7 @@ const struct bpf_func_proto bpf_snprintf_btf_proto = { · 1 unmodified line
10121012 BPF_CALL_1(bpf_get_func_ip_tracing, void *, ctx)
10131013 {
10141014 /* This helper call is inlined by verifier. */
1015− return ((u64 *)ctx)[-1];
1015+ return ((u64 *)ctx)[-2];
10161016 }
10171017
10181018 static const struct bpf_func_proto bpf_get_func_ip_proto_tracing = {
@@ -1091,6 +1091,53 @@ static const struct bpf_func_proto bpf_get_branch_snapshot_proto = { · 72 unmodified lines
10911091 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
10921092 };
10931093
1094+BPF_CALL_3(get_func_arg, void *, ctx, u32, n, u64 *, value)
1095+{
1096+ /* This helper call is inlined by verifier. */
1097+ u64 nr_args = ((u64 *)ctx)[-1];
1098+
1099+ if ((u64) n >= nr_args)
1100+ return -EINVAL;
1101+ *value = ((u64 *)ctx)[n];
1102+ return 0;
1103+}
1104+
1105+static const struct bpf_func_proto bpf_get_func_arg_proto = {
1106+ .func = get_func_arg,
1107+ .ret_type = RET_INTEGER,
1108+ .arg1_type = ARG_PTR_TO_CTX,
1109+ .arg2_type = ARG_ANYTHING,
1110+ .arg3_type = ARG_PTR_TO_LONG,
1111+};
1112+
1113+BPF_CALL_2(get_func_ret, void *, ctx, u64 *, value)
1114+{
1115+ /* This helper call is inlined by verifier. */
1116+ u64 nr_args = ((u64 *)ctx)[-1];
1117+
1118+ *value = ((u64 *)ctx)[nr_args];
1119+ return 0;
1120+}
1121+
1122+static const struct bpf_func_proto bpf_get_func_ret_proto = {
1123+ .func = get_func_ret,
1124+ .ret_type = RET_INTEGER,
1125+ .arg1_type = ARG_PTR_TO_CTX,
1126+ .arg2_type = ARG_PTR_TO_LONG,
1127+};
1128+
1129+BPF_CALL_1(get_func_arg_cnt, void *, ctx)
1130+{
1131+ /* This helper call is inlined by verifier. */
1132+ return ((u64 *)ctx)[-1];
1133+}
1134+
1135+static const struct bpf_func_proto bpf_get_func_arg_cnt_proto = {
1136+ .func = get_func_arg_cnt,
1137+ .ret_type = RET_INTEGER,
1138+ .arg1_type = ARG_PTR_TO_CTX,
1139+};
1140+
10941141 static const struct bpf_func_proto *
10951142 bpf_tracing_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
10961143 {
@@ -1287,7 +1334,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_tp = { · 190 unmodified lines
12871334 .arg1_type = ARG_PTR_TO_CTX,
12881335 .arg2_type = ARG_CONST_MAP_PTR,
12891336 .arg3_type = ARG_ANYTHING,
1290− .arg4_type = ARG_PTR_TO_MEM,
1337+ .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
12911338 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
12921339 };
12931340
@@ -1509,7 +1556,7 @@ static const struct bpf_func_proto bpf_perf_event_output_proto_raw_tp = { · 215 unmodified lines
15091556 .arg1_type = ARG_PTR_TO_CTX,
15101557 .arg2_type = ARG_CONST_MAP_PTR,
15111558 .arg3_type = ARG_ANYTHING,
1512− .arg4_type = ARG_PTR_TO_MEM,
1559+ .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
15131560 .arg5_type = ARG_CONST_SIZE_OR_ZERO,
15141561 };
15151562
@@ -1563,7 +1610,7 @@ static const struct bpf_func_proto bpf_get_stack_proto_raw_tp = { · 47 unmodified lines
15631610 .gpl_only = true,
15641611 .ret_type = RET_INTEGER,
15651612 .arg1_type = ARG_PTR_TO_CTX,
1566− .arg2_type = ARG_PTR_TO_MEM,
1613+ .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
15671614 .arg3_type = ARG_CONST_SIZE_OR_ZERO,
15681615 .arg4_type = ARG_ANYTHING,
15691616 };
@@ -1629,6 +1676,12 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) · 59 unmodified lines
16291676 NULL;
16301677 case BPF_FUNC_d_path:
16311678 return &bpf_d_path_proto;
1679+ case BPF_FUNC_get_func_arg:
1680+ return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_proto : NULL;
1681+ case BPF_FUNC_get_func_ret:
1682+ return bpf_prog_has_trampoline(prog) ? &bpf_get_func_ret_proto : NULL;
1683+ case BPF_FUNC_get_func_arg_cnt:
1684+ return bpf_prog_has_trampoline(prog) ? &bpf_get_func_arg_cnt_proto : NULL;
16321685 default:
16331686 fn = raw_tp_prog_func_proto(func_id, prog);
16341687 if (!fn && prog->expected_attach_type == BPF_TRACE_ITER)
kernel/trace/trace_kprobe.c+1−0
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 */
88 #define pr_fmt(fmt) "trace_kprobe: " fmt
99
10+#include <linux/bpf-cgroup.h>
1011 #include <linux/security.h>
1112 #include <linux/module.h>
1213 #include <linux/uaccess.h>
kernel/trace/trace_uprobe.c+1−0
@@ -7,6 +7,7 @@ · 6 unmodified lines
77 */
88 #define pr_fmt(fmt) "trace_uprobe: " fmt
99
10+#include <linux/bpf-cgroup.h>
1011 #include <linux/security.h>
1112 #include <linux/ctype.h>
1213 #include <linux/module.h>