History

Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf

Pull BPF fixes from Daniel Borkmann: - Initialize task local storage before fork bails out to free the task (Jann Horn) - Fix insn_aux_data leak on verifier error path (KaFai Wan) - Reject BPF inode storage map creation when BPF LSM is uninitialized (Matt Bobrowski) - Mask pseudo pointer values in verifier logs when pointer leaks are not allowed (Nuoqi Gui) - Harden BPF JIT against spraying via IBPB flush (Pawan Gupta) - Reject a skb-modifying SK_SKB stream parser since the latter is only meant to measure the next message (Sechang Lim) - Fix bpf_refcount_acquire to reject refcounted allocation arguments with a non-zero fixed offset (Yiyang Chen) * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: bpf: Prefer dirty packs for eBPF allocations bpf: Prefer packs that won't trigger an IBPB flush on allocation bpf: Skip redundant IBPB in pack allocator bpf: Restrict JIT predictor flush to cBPF x86/bugs: Enable IBPB flush on BPF JIT allocation bpf: Support for hardening against JIT spraying bpf: Reject BPF_MAP_TYPE_INODE_STORAGE creation if BPF LSM is uninitialized bpf,fork: wipe ->bpf_storage before bailouts that access it bpf: Fix insn_aux_data leak on verifier err_free_env path selftests/bpf: Cover pseudo-BTF ksym log masking bpf: Mask pseudo pointer values in verifier logs selftests/bpf: Cover refcount acquire node offsets bpf: Reject offset refcount acquire arguments selftests/bpf: test rejection of a packet-modifying SK_SKB stream parser bpf, sockmap: reject a packet-modifying SK_SKB stream parser selftests/bpf: don't modify the skb in the strparser parser prog

Linus Torvalds committed 4 months ago

Commit 51512e22 parents 826eec5 b72e29e23 files · +297 −62Browse files

Unified · Split

23 files
  1. arch/arm64/net/bpf_jit_comp.c +2 −2
  2. arch/loongarch/net/bpf_jit.c +3 −2
  3. arch/powerpc/net/bpf_jit_comp.c +2 −2
  4. arch/riscv/net/bpf_jit_comp64.c +1 −1
  5. arch/riscv/net/bpf_jit_core.c +2 −1
  6. arch/x86/include/asm/nospec-branch.h +4 −0
  7. arch/x86/kernel/cpu/bugs.c +45 −5
  8. arch/x86/net/bpf_jit_comp.c +3 −2
  9. include/linux/bpf_lsm.h +4 −0
  10. include/linux/filter.h +13 −2
  11. kernel/bpf/bpf_inode_storage.c +9 −0
  12. kernel/bpf/core.c +62 −6
  13. kernel/bpf/disasm.c +4 −1
  14. kernel/bpf/dispatcher.c +1 −1
  15. kernel/bpf/verifier.c +23 −11
  16. kernel/fork.c +5 −4
  17. net/core/sock_map.c +20 −0
  18. security/bpf/hooks.c +3 −0
  19. tools/testing/selftests/bpf/prog_tests/sockmap_strp.c +31 −0
  20. tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c +34 −0
  21. tools/testing/selftests/bpf/progs/sockmap_parse_prog.c +0 −22
  22. tools/testing/selftests/bpf/progs/test_sockmap_strp.c +7 −0
  23. tools/testing/selftests/bpf/progs/verifier_unpriv.c +19 −0
arch/arm64/net/bpf_jit_comp.c+2−2
@@ -2177,7 +2177,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr · 2176 unmodified lines
21772177 image_size = extable_offset + extable_size;
21782178 ro_header = bpf_jit_binary_pack_alloc(image_size, &ro_image_ptr,
21792179 sizeof(u64), &header, &image_ptr,
2180− jit_fill_hole);
2180+ jit_fill_hole, was_classic);
21812181 if (!ro_header)
21822182 goto out_off;
21832183
@@ -2870,7 +2870,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, · 686 unmodified lines
28702870
28712871 void *arch_alloc_bpf_trampoline(unsigned int size)
28722872 {
2873− return bpf_prog_pack_alloc(size, jit_fill_hole);
2873+ return bpf_prog_pack_alloc(size, jit_fill_hole, false);
28742874 }
28752875
28762876 void arch_free_bpf_trampoline(void *image, unsigned int size)
arch/loongarch/net/bpf_jit.c+3−2
@@ -1790,7 +1790,7 @@ static int invoke_bpf(struct jit_ctx *ctx, struct bpf_tramp_nodes *tn, · 1789 unmodified lines
17901790
17911791 void *arch_alloc_bpf_trampoline(unsigned int size)
17921792 {
1793− return bpf_prog_pack_alloc(size, jit_fill_hole);
1793+ return bpf_prog_pack_alloc(size, jit_fill_hole, false);
17941794 }
17951795
17961796 void arch_free_bpf_trampoline(void *image, unsigned int size)
@@ -2256,7 +2256,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr · 459 unmodified lines
22562256 image_size = prog_size + extable_size;
22572257 /* Now we know the size of the structure to make */
22582258 ro_header = bpf_jit_binary_pack_alloc(image_size, &ro_image_ptr, sizeof(u32),
2259− &header, &image_ptr, jit_fill_hole);
2259+ &header, &image_ptr, jit_fill_hole,
2260+ bpf_prog_was_classic(prog));
22602261 if (!ro_header)
22612262 goto out_offset;
22622263
arch/powerpc/net/bpf_jit_comp.c+2−2
@@ -295,7 +295,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr · 294 unmodified lines
295295 alloclen = proglen + FUNCTION_DESCR_SIZE + fixup_len + extable_len;
296296
297297 fhdr = bpf_jit_binary_pack_alloc(alloclen, &fimage, 4, &hdr, &image,
298− bpf_jit_fill_ill_insns);
298+ bpf_jit_fill_ill_insns, bpf_prog_was_classic(fp));
299299 if (!fhdr)
300300 goto out_err;
301301
@@ -588,7 +588,7 @@ bool bpf_jit_inlines_helper_call(s32 imm) · 286 unmodified lines
588588
589589 void *arch_alloc_bpf_trampoline(unsigned int size)
590590 {
591− return bpf_prog_pack_alloc(size, bpf_jit_fill_ill_insns);
591+ return bpf_prog_pack_alloc(size, bpf_jit_fill_ill_insns, false);
592592 }
593593
594594 void arch_free_bpf_trampoline(void *image, unsigned int size)
arch/riscv/net/bpf_jit_comp64.c+1−1
@@ -1321,7 +1321,7 @@ int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, · 1320 unmodified lines
13211321
13221322 void *arch_alloc_bpf_trampoline(unsigned int size)
13231323 {
1324− return bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
1324+ return bpf_prog_pack_alloc(size, bpf_fill_ill_insns, false);
13251325 }
13261326
13271327 void arch_free_bpf_trampoline(void *image, unsigned int size)
arch/riscv/net/bpf_jit_core.c+2−1
@@ -109,7 +109,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr · 108 unmodified lines
109109 bpf_jit_binary_pack_alloc(prog_size + extable_size,
110110 &jit_data->ro_image, sizeof(u32),
111111 &jit_data->header, &jit_data->image,
112− bpf_fill_ill_insns);
112+ bpf_fill_ill_insns,
113+ bpf_prog_was_classic(prog));
113114 if (!jit_data->ro_header)
114115 goto out_offset;
115116
arch/x86/include/asm/nospec-branch.h+4−0
@@ -388,6 +388,10 @@ extern void srso_alias_return_thunk(void); · 387 unmodified lines
388388 extern void entry_untrain_ret(void);
389389 extern void write_ibpb(void);
390390
391+#ifdef CONFIG_BPF_JIT
392+extern void bpf_arch_ibpb(void);
393+#endif
394+
391395 #ifdef CONFIG_X86_64
392396 extern void clear_bhb_loop(void);
393397 #endif
arch/x86/kernel/cpu/bugs.c+45−5
@@ -16,6 +16,7 @@ · 15 unmodified lines
1616 #include <linux/sched/smt.h>
1717 #include <linux/pgtable.h>
1818 #include <linux/bpf.h>
19+#include <linux/filter.h>
1920 #include <linux/kvm_types.h>
2021
2122 #include <asm/spec-ctrl.h>
@@ -1651,8 +1652,21 @@ static inline const char *spectre_v2_module_string(void) · 1629 unmodified lines
16511652 {
16521653 return spectre_v2_bad_module ? " - vulnerable module loaded" : "";
16531654 }
1655+
1656+/*
1657+ * The "retpoline sequence" is the "call;mov;ret" sequence that
1658+ * replaces normal indirect branch instructions. Differentiate
1659+ * *the* retpoline sequence from the LFENCE-prefixed indirect
1660+ * branches that simply use the retpoline infrastructure.
1661+ */
1662+static inline bool retpoline_seq_enabled(void)
1663+{
1664+ return boot_cpu_has(X86_FEATURE_RETPOLINE) && !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE);
1665+}
1666+
16541667 #else
16551668 static inline const char *spectre_v2_module_string(void) { return ""; }
1669+static inline bool retpoline_seq_enabled(void) { return false; }
16561670 #endif
16571671
16581672 #define SPECTRE_V2_LFENCE_MSG "WARNING: LFENCE mitigation is not recommended for this CPU, data leaks possible!\n"
@@ -2095,8 +2109,7 @@ static void __init bhi_apply_mitigation(void) · 436 unmodified lines
20952109 return;
20962110
20972111 /* Retpoline mitigates against BHI unless the CPU has RRSBA behavior */
2098− if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
2099− !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
2112+ if (retpoline_seq_enabled()) {
21002113 spec_ctrl_disable_kernel_rrsba();
21012114 if (rrsba_disabled)
21022115 return;
@@ -2238,6 +2251,27 @@ static void __init spectre_v2_update_mitigation(void) · 135 unmodified lines
22382251 pr_info("%s\n", spectre_v2_strings[spectre_v2_enabled]);
22392252 }
22402253
2254+#ifdef CONFIG_BPF_JIT
2255+static void __bpf_arch_ibpb(void *unused)
2256+{
2257+ write_ibpb();
2258+}
2259+
2260+void bpf_arch_ibpb(void)
2261+{
2262+ on_each_cpu(__bpf_arch_ibpb, NULL, 1);
2263+}
2264+
2265+static bool __init cpu_wants_ibpb_bpf(void)
2266+{
2267+ /* A genuine retpoline already neutralizes ring0 indirect predictions */
2268+ if (retpoline_seq_enabled())
2269+ return false;
2270+
2271+ return boot_cpu_has(X86_FEATURE_IBPB);
2272+}
2273+#endif
2274+
22412275 static void __init spectre_v2_apply_mitigation(void)
22422276 {
22432277 if (spectre_v2_enabled == SPECTRE_V2_EIBRS && unprivileged_ebpf_enabled())
@@ -2314,6 +2348,14 @@ static void __init spectre_v2_apply_mitigation(void) · 70 unmodified lines
23142348 setup_force_cpu_cap(X86_FEATURE_USE_IBRS_FW);
23152349 pr_info("Enabling Restricted Speculation for firmware calls\n");
23162350 }
2351+
2352+#ifdef CONFIG_BPF_JIT
2353+ if (cpu_wants_ibpb_bpf()) {
2354+ static_call_update(bpf_arch_pred_flush, bpf_arch_ibpb);
2355+ static_branch_enable(&bpf_pred_flush_enabled);
2356+ pr_info("Enabling IBPB for BPF\n");
2357+ }
2358+#endif
23172359 }
23182360
23192361 static void update_stibp_msr(void * __unused)
@@ -3490,9 +3532,7 @@ static const char *spectre_bhi_state(void) · 1170 unmodified lines
34903532 return "; BHI: BHI_DIS_S";
34913533 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_LOOP))
34923534 return "; BHI: SW loop, KVM: SW loop";
3493− else if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
3494− !boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE) &&
3495− rrsba_disabled)
3535+ else if (retpoline_seq_enabled() && rrsba_disabled)
34963536 return "; BHI: Retpoline";
34973537 else if (boot_cpu_has(X86_FEATURE_CLEAR_BHB_VMEXIT))
34983538 return "; BHI: Vulnerable, KVM: SW loop";
arch/x86/net/bpf_jit_comp.c+3−2
@@ -3653,7 +3653,7 @@ cleanup: · 3652 unmodified lines
36533653
36543654 void *arch_alloc_bpf_trampoline(unsigned int size)
36553655 {
3656− return bpf_prog_pack_alloc(size, jit_fill_hole);
3656+ return bpf_prog_pack_alloc(size, jit_fill_hole, false);
36573657 }
36583658
36593659 void arch_free_bpf_trampoline(void *image, unsigned int size)
@@ -3965,7 +3965,8 @@ out_image: · 305 unmodified lines
39653965 /* allocate module memory for x86 insns and extable */
39663966 header = bpf_jit_binary_pack_alloc(roundup(proglen, align) + extable_size,
39673967 &image, align, &rw_header, &rw_image,
3968− jit_fill_hole);
3968+ jit_fill_hole,
3969+ bpf_prog_was_classic(prog));
39693970 if (!header)
39703971 goto out_addrs;
39713972 prog->aux->extable = (void *) image + roundup(proglen, align);
include/linux/bpf_lsm.h+4−0
@@ -14,6 +14,8 @@ · 13 unmodified lines
1414
1515 #ifdef CONFIG_BPF_LSM
1616
17+extern bool bpf_lsm_initialized __ro_after_init;
18+
1719 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
1820 RET bpf_lsm_##NAME(__VA_ARGS__);
1921 #include <linux/lsm_hook_defs.h>
@@ -56,6 +58,8 @@ bool bpf_lsm_hook_returns_errno(u32 btf_id); · 36 unmodified lines
5658
5759 #else /* !CONFIG_BPF_LSM */
5860
61+#define bpf_lsm_initialized false
62+
5963 static inline bool bpf_lsm_is_sleepable_hook(u32 btf_id)
6064 {
6165 return false;
include/linux/filter.h+13−2
@@ -21,6 +21,7 @@ · 20 unmodified lines
2121 #include <linux/if_vlan.h>
2222 #include <linux/vmalloc.h>
2323 #include <linux/sockptr.h>
24+#include <linux/static_call.h>
2425 #include <linux/u64_stats_sync.h>
2526
2627 #include <net/sch_generic.h>
@@ -1314,6 +1315,15 @@ extern long bpf_jit_limit_max; · 1287 unmodified lines
13141315
13151316 typedef void (*bpf_jit_fill_hole_t)(void *area, unsigned int size);
13161317
1318+/*
1319+ * Flush the indirect branch predictors before reusing JIT memory, so that
1320+ * indirect jumps into a newly written program don't reuse predictions left
1321+ * behind by an old program that occupied the same space.
1322+ */
1323+void bpf_arch_pred_flush(void);
1324+DECLARE_STATIC_CALL(bpf_arch_pred_flush, bpf_arch_pred_flush);
1325+DECLARE_STATIC_KEY_FALSE(bpf_pred_flush_enabled);
1326+
13171327 void bpf_jit_fill_hole_with_zero(void *area, unsigned int size);
13181328
13191329 struct bpf_binary_header *
@@ -1328,7 +1338,7 @@ void bpf_jit_free(struct bpf_prog *fp); · 8 unmodified lines
13281338 struct bpf_binary_header *
13291339 bpf_jit_binary_pack_hdr(const struct bpf_prog *fp);
13301340
1331−void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns);
1341+void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic);
13321342 void bpf_prog_pack_free(void *ptr, u32 size);
13331343
13341344 static inline bool bpf_prog_kallsyms_verify_off(const struct bpf_prog *fp)
@@ -1342,7 +1352,8 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **ro_image, · 7 unmodified lines
13421352 unsigned int alignment,
13431353 struct bpf_binary_header **rw_hdr,
13441354 u8 **rw_image,
1345− bpf_jit_fill_hole_t bpf_fill_ill_insns);
1355+ bpf_jit_fill_hole_t bpf_fill_ill_insns,
1356+ bool was_classic);
13461357 int bpf_jit_binary_pack_finalize(struct bpf_binary_header *ro_header,
13471358 struct bpf_binary_header *rw_header);
13481359 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
kernel/bpf/bpf_inode_storage.c+9−0
@@ -178,6 +178,15 @@ static int notsupp_get_next_key(struct bpf_map *map, void *key, · 177 unmodified lines
178178
179179 static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
180180 {
181+ /*
182+ * Do not allow allocation of BPF_MAP_TYPE_INODE_STORAGE if the BPF LSM
183+ * was not initialized by the LSM framework at boot. Without proper
184+ * initialization, the BPF inode security blob offset remains unprepared,
185+ * causing bpf_inode() to calculate an invalid memory offset and corrupt
186+ * inode->i_security.
187+ */
188+ if (!bpf_lsm_initialized)
189+ return ERR_PTR(-EOPNOTSUPP);
181190 return bpf_local_storage_map_alloc(attr, &inode_cache);
182191 }
183192
kernel/bpf/core.c+62−6
@@ -20,6 +20,7 @@ · 19 unmodified lines
2020 #include <uapi/linux/btf.h>
2121 #include <linux/filter.h>
2222 #include <linux/skbuff.h>
23+#include <linux/static_call.h>
2324 #include <linux/vmalloc.h>
2425 #include <linux/prandom.h>
2526 #include <linux/bpf.h>
@@ -875,6 +876,7 @@ int bpf_jit_add_poke_descriptor(struct bpf_prog *prog, · 849 unmodified lines
875876 struct bpf_prog_pack {
876877 struct list_head list;
877878 void *ptr;
879+ bool arch_flush_needed;
878880 unsigned long bitmap[];
879881 };
880882
@@ -883,6 +885,15 @@ void bpf_jit_fill_hole_with_zero(void *area, unsigned int size) · 2 unmodified lines
883885 memset(area, 0, size);
884886 }
885887
888+DEFINE_STATIC_CALL_NULL(bpf_arch_pred_flush, bpf_arch_pred_flush);
889+
890+/*
891+ * Enabled once bpf_arch_pred_flush points at a real flush routine. Lets the
892+ * pack allocator test "is a predictor flush wired up at all" with a cheap
893+ * static branch instead of repeatedly querying the static call target.
894+ */
895+DEFINE_STATIC_KEY_FALSE(bpf_pred_flush_enabled);
896+
886897 #define BPF_PROG_SIZE_TO_NBITS(size) (round_up(size, BPF_PROG_CHUNK_SIZE) / BPF_PROG_CHUNK_SIZE)
887898
888899 static DEFINE_MUTEX(pack_mutex);
@@ -918,6 +929,8 @@ static struct bpf_prog_pack *alloc_new_pack(bpf_jit_fill_hole_t bpf_fill_ill_ins · 29 unmodified lines
918929 bpf_fill_ill_insns(pack->ptr, BPF_PROG_PACK_SIZE);
919930 bitmap_zero(pack->bitmap, BPF_PROG_PACK_SIZE / BPF_PROG_CHUNK_SIZE);
920931
932+ if (static_branch_unlikely(&bpf_pred_flush_enabled))
933+ pack->arch_flush_needed = true;
921934 set_vm_flush_reset_perms(pack->ptr);
922935 err = set_memory_rox((unsigned long)pack->ptr,
923936 BPF_PROG_PACK_SIZE / PAGE_SIZE);
@@ -932,15 +945,23 @@ out: · 8 unmodified lines
932945 return NULL;
933946 }
934947
935−void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns)
948+void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns, bool was_classic)
936949 {
937950 unsigned int nbits = BPF_PROG_SIZE_TO_NBITS(size);
938− struct bpf_prog_pack *pack;
939− unsigned long pos;
951+ struct bpf_prog_pack *pack, *fallback_pack = NULL;
952+ unsigned long pos, fallback_pos = 0;
940953 void *ptr = NULL;
941954
942955 mutex_lock(&pack_mutex);
943956 if (size > BPF_PROG_PACK_SIZE) {
957+ /*
958+ * Allocations larger than a pack get their own pages, and
959+ * predictors are not flushed for such allocation. This is only
960+ * safe because cBPF programs (the unprivileged attack surface)
961+ * are bounded well below a pack size.
962+ */
963+ if (was_classic && static_branch_unlikely(&bpf_pred_flush_enabled))
964+ pr_warn_once("BPF: Predictors not flushed for allocations greater than BPF_PROG_PACK_SIZE\n");
944965 size = round_up(size, PAGE_SIZE);
945966 ptr = bpf_jit_alloc_exec(size);
946967 if (ptr) {
@@ -960,8 +981,29 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns) · 13 unmodified lines
960981 list_for_each_entry(pack, &pack_list, list) {
961982 pos = bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
962983 nbits, 0);
963− if (pos < BPF_PROG_CHUNK_COUNT)
984+ if (pos >= BPF_PROG_CHUNK_COUNT)
985+ continue;
986+ /* Flush not enabled, use any pack */
987+ if (!static_branch_unlikely(&bpf_pred_flush_enabled))
964988 goto found_free_area;
989+ /*
990+ * cBPF reuse of a dirty pack triggers a flush, so prefer a
991+ * clean pack for cBPF. eBPF never flushes, so steer it to a
992+ * dirty pack and keep clean packs free for cBPF.
993+ */
994+ if (was_classic ^ pack->arch_flush_needed)
995+ goto found_free_area;
996+ if (!fallback_pack) {
997+ fallback_pack = pack;
998+ fallback_pos = pos;
999+ }
1000+ }
1001+
1002+ /* No preferred pack found */
1003+ if (fallback_pack) {
1004+ pack = fallback_pack;
1005+ pos = fallback_pos;
1006+ goto found_free_area;
9651007 }
9661008
9671009 pack = alloc_new_pack(bpf_fill_ill_insns);
@@ -971,6 +1013,16 @@ void *bpf_prog_pack_alloc(u32 size, bpf_jit_fill_hole_t bpf_fill_ill_insns) · 3 unmodified lines
9711013 pos = 0;
9721014
9731015 found_free_area:
1016+ /* Flush only for cBPF as it may contain a crafted gadget */
1017+ if (static_branch_unlikely(&bpf_pred_flush_enabled) &&
1018+ pack->arch_flush_needed &&
1019+ was_classic) {
1020+ struct bpf_prog_pack *p;
1021+
1022+ static_call_cond(bpf_arch_pred_flush)();
1023+ list_for_each_entry(p, &pack_list, list)
1024+ p->arch_flush_needed = false;
1025+ }
9741026 bitmap_set(pack->bitmap, pos, nbits);
9751027 ptr = (void *)(pack->ptr) + (pos << BPF_PROG_CHUNK_SHIFT);
9761028
@@ -1008,6 +1060,9 @@ void bpf_prog_pack_free(void *ptr, u32 size) · 31 unmodified lines
10081060 "bpf_prog_pack bug: missing bpf_arch_text_invalidate?\n");
10091061
10101062 bitmap_clear(pack->bitmap, pos, nbits);
1063+
1064+ if (static_branch_unlikely(&bpf_pred_flush_enabled))
1065+ pack->arch_flush_needed = true;
10111066 if (bitmap_find_next_zero_area(pack->bitmap, BPF_PROG_CHUNK_COUNT, 0,
10121067 BPF_PROG_CHUNK_COUNT, 0) == 0) {
10131068 list_del(&pack->list);
@@ -1130,7 +1185,8 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr, · 116 unmodified lines
11301185 unsigned int alignment,
11311186 struct bpf_binary_header **rw_header,
11321187 u8 **rw_image,
1133− bpf_jit_fill_hole_t bpf_fill_ill_insns)
1188+ bpf_jit_fill_hole_t bpf_fill_ill_insns,
1189+ bool was_classic)
11341190 {
11351191 struct bpf_binary_header *ro_header;
11361192 u32 size, hole, start;
@@ -1143,7 +1199,7 @@ bpf_jit_binary_pack_alloc(unsigned int proglen, u8 **image_ptr, · 6 unmodified lines
11431199
11441200 if (bpf_jit_charge_modmem(size))
11451201 return NULL;
1146− ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns);
1202+ ro_header = bpf_prog_pack_alloc(size, bpf_fill_ill_insns, was_classic);
11471203 if (!ro_header) {
11481204 bpf_jit_uncharge_modmem(size);
11491205 return NULL;
kernel/bpf/disasm.c+4−1
@@ -323,7 +323,10 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs, · 322 unmodified lines
323323 */
324324 u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
325325 bool is_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD ||
326− insn->src_reg == BPF_PSEUDO_MAP_VALUE;
326+ insn->src_reg == BPF_PSEUDO_MAP_VALUE ||
327+ insn->src_reg == BPF_PSEUDO_MAP_IDX ||
328+ insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE ||
329+ insn->src_reg == BPF_PSEUDO_BTF_ID;
327330 char tmp[64];
328331
329332 if (is_ptr && !allow_ptr_leaks)
kernel/bpf/dispatcher.c+1−1
@@ -145,7 +145,7 @@ void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from, · 144 unmodified lines
145145
146146 mutex_lock(&d->mutex);
147147 if (!d->image) {
148− d->image = bpf_prog_pack_alloc(PAGE_SIZE, bpf_jit_fill_hole_with_zero);
148+ d->image = bpf_prog_pack_alloc(PAGE_SIZE, bpf_jit_fill_hole_with_zero, false);
149149 if (!d->image)
150150 goto out;
151151 d->rw_image = bpf_jit_alloc_exec(PAGE_SIZE);
kernel/bpf/verifier.c+23−11
@@ -7996,9 +7996,10 @@ reg_find_field_offset(const struct bpf_reg_state *reg, s32 off, u32 fields) · 7995 unmodified lines
79967996 return field;
79977997 }
79987998
7999−static int check_func_arg_reg_off(struct bpf_verifier_env *env,
8000− const struct bpf_reg_state *reg, argno_t argno,
8001− enum bpf_arg_type arg_type)
7999+static int __check_func_arg_reg_off(struct bpf_verifier_env *env,
8000+ const struct bpf_reg_state *reg, argno_t argno,
8001+ enum bpf_arg_type arg_type,
8002+ bool btf_id_fixed_off_ok)
80028003 {
80038004 u32 type = reg->type;
80048005
@@ -8055,12 +8056,11 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env, · 50 unmodified lines
80558056 case PTR_TO_BTF_ID | MEM_ALLOC | NON_OWN_REF | MEM_RCU:
80568057 /* When referenced PTR_TO_BTF_ID is passed to release function,
80578058 * its fixed offset must be 0. In the other cases, fixed offset
8058− * can be non-zero. This was already checked above. So pass
8059− * fixed_off_ok as true to allow fixed offset for all other
8060− * cases. var_off always must be 0 for PTR_TO_BTF_ID, hence we
8061− * still need to do checks instead of returning.
8059+ * can be non-zero unless the caller requires otherwise.
8060+ * var_off always must be 0 for PTR_TO_BTF_ID, hence we still
8061+ * need to do checks instead of returning.
80628062 */
8063− return __check_ptr_off_reg(env, reg, argno, true);
8063+ return __check_ptr_off_reg(env, reg, argno, btf_id_fixed_off_ok);
80648064 case PTR_TO_CTX:
80658065 /*
80668066 * Allow fixed and variable offsets for syscall context, but
@@ -8076,6 +8076,13 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env, · 9 unmodified lines
80768076 }
80778077 }
80788078
8079+static int check_func_arg_reg_off(struct bpf_verifier_env *env,
8080+ const struct bpf_reg_state *reg, argno_t argno,
8081+ enum bpf_arg_type arg_type)
8082+{
8083+ return __check_func_arg_reg_off(env, reg, argno, arg_type, true);
8084+}
8085+
80798086 static int check_arg_const_str(struct bpf_verifier_env *env,
80808087 struct bpf_reg_state *reg, argno_t argno)
80818088 {
@@ -11947,6 +11954,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 3865 unmodified lines
1194711954 enum bpf_arg_type arg_type = ARG_DONTCARE;
1194811955 argno_t argno = argno_from_arg(i + 1);
1194911956 int regno = reg_from_argno(argno);
11957+ bool btf_id_fixed_off_ok = true;
1195011958 u32 ref_id, type_size;
1195111959 bool is_ret_buf_sz = false;
1195211960 int kf_arg_type;
@@ -12120,7 +12128,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 167 unmodified lines
1212012128 case KF_ARG_PTR_TO_MEM:
1212112129 case KF_ARG_PTR_TO_MEM_SIZE:
1212212130 case KF_ARG_PTR_TO_CALLBACK:
12123− case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
1212412131 case KF_ARG_PTR_TO_CONST_STR:
1212512132 case KF_ARG_PTR_TO_WORKQUEUE:
1212612133 case KF_ARG_PTR_TO_TIMER:
@@ -12134,6 +12141,10 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 7 unmodified lines
1213412141 case KF_ARG_PTR_TO_CTX:
1213512142 arg_type = ARG_PTR_TO_CTX;
1213612143 break;
12144+ case KF_ARG_PTR_TO_REFCOUNTED_KPTR:
12145+ arg_type = ARG_PTR_TO_BTF_ID;
12146+ btf_id_fixed_off_ok = false;
12147+ break;
1213712148 default:
1213812149 verifier_bug(env, "unknown kfunc arg type %d", kf_arg_type);
1213912150 return -EFAULT;
@@ -12141,7 +12152,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · 1 unmodified line
1214112152
1214212153 if (regno == meta->release_regno)
1214312154 arg_type |= OBJ_RELEASE;
12144− ret = check_func_arg_reg_off(env, reg, argno, arg_type);
12155+ ret = __check_func_arg_reg_off(env, reg, argno, arg_type,
12156+ btf_id_fixed_off_ok);
1214512157 if (ret < 0)
1214612158 return ret;
1214712159
@@ -19994,13 +20006,13 @@ err_unlock: · 7846 unmodified lines
1999420006 if (!is_priv)
1999520007 mutex_unlock(&bpf_verifier_lock);
1999620008 bpf_clear_insn_aux_data(env, 0, env->prog->len);
19997− vfree(env->insn_aux_data);
1999820009 err_free_env:
1999920010 bpf_stack_liveness_free(env);
2000020011 kvfree(env->cfg.insn_postorder);
2000120012 kvfree(env->scc_info);
2000220013 kvfree(env->succ);
2000320014 kvfree(env->gotox_tmp_buf);
20015+ vfree(env->insn_aux_data);
2000420016 kvfree(env);
2000520017 return ret;
2000620018 }
kernel/fork.c+5−4
@@ -1009,6 +1009,11 @@ static struct task_struct *dup_task_struct(struct task_struct *orig, int node) · 1008 unmodified lines
10091009 tsk->mm_cid.active = 0;
10101010 INIT_HLIST_NODE(&tsk->mm_cid.node);
10111011 #endif
1012+
1013+#ifdef CONFIG_BPF_SYSCALL
1014+ RCU_INIT_POINTER(tsk->bpf_storage, NULL);
1015+ tsk->bpf_ctx = NULL;
1016+#endif
10121017 return tsk;
10131018
10141019 free_stack:
@@ -2247,10 +2252,6 @@ __latent_entropy struct task_struct *copy_process( · 1232 unmodified lines
22472252 p->sequential_io = 0;
22482253 p->sequential_io_avg = 0;
22492254 #endif
2250−#ifdef CONFIG_BPF_SYSCALL
2251− RCU_INIT_POINTER(p->bpf_storage, NULL);
2252− p->bpf_ctx = NULL;
2253−#endif
22542255
22552256 unwind_task_init(p);
22562257
net/core/sock_map.c+20−0
@@ -1515,6 +1515,17 @@ static int sock_map_prog_link_lookup(struct bpf_map *map, struct bpf_prog ***ppr · 1514 unmodified lines
15151515 return 0;
15161516 }
15171517
1518+static int sock_map_prog_attach_check(enum bpf_attach_type attach_type,
1519+ struct bpf_prog *prog)
1520+{
1521+ /* A stream parser must not modify the skb, only measure it. */
1522+ if (prog && attach_type == BPF_SK_SKB_STREAM_PARSER &&
1523+ prog->aux->changes_pkt_data)
1524+ return -EINVAL;
1525+
1526+ return 0;
1527+}
1528+
15181529 /* Handle the following four cases:
15191530 * prog_attach: prog != NULL, old == NULL, link == NULL
15201531 * prog_detach: prog == NULL, old != NULL, link == NULL
@@ -1533,6 +1544,10 @@ static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog, · 12 unmodified lines
15331544 if (ret)
15341545 return ret;
15351546
1547+ ret = sock_map_prog_attach_check(which, prog);
1548+ if (ret)
1549+ return ret;
1550+
15361551 /* for prog_attach/prog_detach/link_attach, return error if a bpf_link
15371552 * exists for that prog.
15381553 */
@@ -1776,6 +1791,11 @@ static int sock_map_link_update_prog(struct bpf_link *link, · 237 unmodified lines
17761791 ret = -EINVAL;
17771792 goto out;
17781793 }
1794+
1795+ ret = sock_map_prog_attach_check(link->attach_type, prog);
1796+ if (ret)
1797+ goto out;
1798+
17791799 if (!sockmap_link->map) {
17801800 ret = -ENOLINK;
17811801 goto out;
security/bpf/hooks.c+3−0
@@ -7,6 +7,8 @@ · 6 unmodified lines
77 #include <linux/bpf_lsm.h>
88 #include <uapi/linux/lsm.h>
99
10+bool bpf_lsm_initialized __ro_after_init;
11+
1012 static struct security_hook_list bpf_lsm_hooks[] __ro_after_init = {
1113 #define LSM_HOOK(RET, DEFAULT, NAME, ...) \
1214 LSM_HOOK_INIT(NAME, bpf_lsm_##NAME),
@@ -24,6 +26,7 @@ static int __init bpf_lsm_init(void) · 11 unmodified lines
2426 {
2527 security_add_hooks(bpf_lsm_hooks, ARRAY_SIZE(bpf_lsm_hooks),
2628 &bpf_lsmid);
29+ bpf_lsm_initialized = true;
2730 pr_info("LSM support for eBPF active\n");
2831 return 0;
2932 }
tools/testing/selftests/bpf/prog_tests/sockmap_strp.c+31−0
@@ -431,6 +431,35 @@ out: · 430 unmodified lines
431431 test_sockmap_strp__destroy(strp);
432432 }
433433
434+static void test_sockmap_strp_parser_reject(void)
435+{
436+ struct test_sockmap_strp *strp = NULL;
437+ int parser_mod, parser_ro, link;
438+ int err, map;
439+
440+ strp = test_sockmap_strp__open_and_load();
441+ if (!ASSERT_OK_PTR(strp, "test_sockmap_strp__open_and_load"))
442+ return;
443+
444+ map = bpf_map__fd(strp->maps.sock_map);
445+ parser_mod = bpf_program__fd(strp->progs.prog_skb_parser_resize);
446+ parser_ro = bpf_program__fd(strp->progs.prog_skb_parser);
447+
448+ err = bpf_prog_attach(parser_mod, map, BPF_SK_SKB_STREAM_PARSER, 0);
449+ ASSERT_ERR(err, "bpf_prog_attach parser_mod");
450+
451+ link = bpf_link_create(parser_ro, map, BPF_SK_SKB_STREAM_PARSER, NULL);
452+ if (!ASSERT_GE(link, 0, "bpf_link_create parser_ro"))
453+ goto out;
454+
455+ err = bpf_link_update(link, parser_mod, NULL);
456+ ASSERT_ERR(err, "bpf_link_update parser_mod");
457+out:
458+ if (link >= 0)
459+ close(link);
460+ test_sockmap_strp__destroy(strp);
461+}
462+
434463 void test_sockmap_strp(void)
435464 {
436465 if (test__start_subtest("sockmap strp tcp pass"))
@@ -451,4 +480,6 @@ void test_sockmap_strp(void) · 14 unmodified lines
451480 test_sockmap_strp_multiple_pkt(AF_INET, SOCK_STREAM);
452481 if (test__start_subtest("sockmap strp tcp dispatch"))
453482 test_sockmap_strp_dispatch_pkt(AF_INET, SOCK_STREAM);
483+ if (test__start_subtest("sockmap strp parser reject pkt mod"))
484+ test_sockmap_strp_parser_reject();
454485 }
tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c+34−0
@@ -13,12 +13,20 @@ struct node_acquire { · 12 unmodified lines
1313 struct bpf_refcount refcount;
1414 };
1515
16+struct node_refcounted {
17+ long key;
18+ struct bpf_list_node list;
19+ struct bpf_refcount refcount;
20+};
21+
1622 extern void bpf_rcu_read_lock(void) __ksym;
1723 extern void bpf_rcu_read_unlock(void) __ksym;
1824
1925 #define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
2026 private(A) struct bpf_spin_lock glock;
2127 private(A) struct bpf_rb_root groot __contains(node_acquire, node);
28+private(B) struct bpf_spin_lock lock;
29+private(B) struct bpf_list_head head __contains(node_refcounted, list);
2230
2331 static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
2432 {
@@ -93,6 +101,32 @@ long rbtree_refcounted_node_ref_escapes_owning_input(void *ctx) · 68 unmodified lines
93101 return 0;
94102 }
95103
104+SEC("?tc")
105+__failure __msg("dereference of modified ptr_ ptr R1")
106+long refcount_acquire_list_node_offset(void *ctx)
107+{
108+ struct node_refcounted *node, *base, *ref;
109+ struct bpf_list_node *list_node;
110+
111+ node = bpf_obj_new(typeof(*node));
112+ if (!node)
113+ return 1;
114+
115+ bpf_spin_lock(&lock);
116+ bpf_list_push_front(&head, &node->list);
117+ list_node = bpf_list_pop_front(&head);
118+ bpf_spin_unlock(&lock);
119+ if (!list_node)
120+ return 2;
121+
122+ base = container_of(list_node, struct node_refcounted, list);
123+ ref = bpf_refcount_acquire(list_node);
124+ if (ref)
125+ bpf_obj_drop(ref);
126+ bpf_obj_drop(base);
127+ return 0;
128+}
129+
96130 SEC("?fentry.s/" SYS_PREFIX "sys_getpgid")
97131 __failure __msg("function calls are not allowed while holding a lock")
98132 int BPF_PROG(rbtree_fail_sleepable_lock_across_rcu,
tools/testing/selftests/bpf/progs/sockmap_parse_prog.c+0−22
@@ -5,28 +5,6 @@ · 4 unmodified lines
55 SEC("sk_skb1")
66 int bpf_prog1(struct __sk_buff *skb)
77 {
8− void *data_end = (void *)(long) skb->data_end;
9− void *data = (void *)(long) skb->data;
10− __u8 *d = data;
11− int err;
12−
13− if (data + 10 > data_end) {
14− err = bpf_skb_pull_data(skb, 10);
15− if (err)
16− return SK_DROP;
17−
18− data_end = (void *)(long)skb->data_end;
19− data = (void *)(long)skb->data;
20− if (data + 10 > data_end)
21− return SK_DROP;
22− }
23−
24− /* This write/read is a bit pointless but tests the verifier and
25− * strparser handler for read/write pkt data and access into sk
26− * fields.
27− */
28− d = data;
29− d[7] = 1;
308 return skb->len;
319 }
3210
tools/testing/selftests/bpf/progs/test_sockmap_strp.c+7−0
@@ -50,4 +50,11 @@ int prog_skb_parser_partial(struct __sk_buff *skb) · 49 unmodified lines
5050 return 10;
5151 }
5252
53+SEC("sk_skb/stream_parser")
54+int prog_skb_parser_resize(struct __sk_buff *skb)
55+{
56+ bpf_skb_change_tail(skb, skb->len, 0);
57+ return skb->len;
58+}
59+
5360 char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_unpriv.c+19−0
@@ -6,6 +6,8 @@ · 5 unmodified lines
66 #include "../../../include/linux/filter.h"
77 #include "bpf_misc.h"
88
9+extern const int bpf_prog_active __ksym;
10+
911 #define BPF_SK_LOOKUP(func) \
1012 /* struct bpf_sock_tuple tuple = {} */ \
1113 "r2 = 0;" \
@@ -77,6 +79,23 @@ __naked void dummy_prog_loop1_socket(void) · 65 unmodified lines
7779 : __clobber_all);
7880 }
7981
82+SEC("socket")
83+__description("unpriv: pseudo btf id log masks address")
84+__success_unpriv
85+__msg_unpriv("0: (18) r1 = 0x0")
86+__not_msg_unpriv("0: (18) r1 = 0x{{[1-9a-f][0-9a-f]*}}")
87+__retval_unpriv(0)
88+__log_level(2)
89+__naked void pseudo_btf_id_log_masks_address(void)
90+{
91+ asm volatile ("r1 = %[bpf_prog_active] ll;"
92+ "r0 = 0;"
93+ "exit;"
94+ :
95+ : __imm_addr(bpf_prog_active)
96+ : __clobber_all);
97+}
98+
8099 SEC("socket")
81100 __description("unpriv: return pointer")
82101 __success __failure_unpriv __msg_unpriv("R0 leaks addr")

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t