From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.231.134 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796086695; b=cdjGPrR3gnAuKInbJtcqZlQuzfPUHLVuOiGIa3exosll4dkWfRnMh2J8JT80Mwwj8I1R sLmeEKQzPOUVMPLKLRRFaYaUqpmq4BcvBFgieTYD5f/qP07rpZu8DhA5uuZbCOSedMwsV FbkN1wgvA0jWRROwxisTq6z0bjti970W0VcMKXZwkcezZcXwg5KeBEhiMJeuAWjhuYoY+ qaGsJHq2NsEM8CGC11aD7mol8V8rCEIJklY4OP0KD9NXruMxwx4JuV/Z1LBKsty1PURR0 TZm5AOimHXHhvZBDHKs0MHeNNc+x0m0Uw/u/NP4JQ0+mBagaN+dcx0SiiVqYb8FtoEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796086695; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:X-Developer-Signature:X-Developer-Key: Content-Transfer-Encoding; bh=sRZLyK5pemP2hiv18N1mAT71UIRd18keUqsb4FzlT8I=; b=zgmk+EbBtFc9i/6BW/jrceO9WPDRgMaeGyiZpdDzERutdT96qI0GMkrikirq50RYOVpn i8UgAwBTJHJRSE5qjcX/PRSDopYQ1OtJssnMk4Bi0coZ5OhEaIdruMjJV6rmGbJRbKuip bt80QZbH4HNYqnrsEzX4hKZaVaNOaVZ7KPmnbhU/Jk1ppgPcVWwe3hVwMBXpq+blGeGu3 GrLuHVPbekEESevjntu4bKA5V+A+XDTosiwxSi79y/yYVAIOywfCIlta0HPmWTDkZdjGF n6JCZZ807bOx3RkYaVsAqh91Yrbvaancr2HV+oMmqohy0JYKu8SkUhpity2mzPkITvw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fI5a6xvC; arc=none smtp.remote-ip=74.125.231.134 Received: from mail-oo2-f6.google.com (mail-oo2-f6.google.com [74.125.231.134]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE732606A33B for ; Tue, 1 Dec 2026 00:58:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AE732606A33B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fI5a6xvC Received: by mail-oo2-f6.google.com with SMTP id 46e09a7af769-862990b7bbbso3513196a34.0 for ; Mon, 30 Nov 2026 16:58:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796086694; x=1796691494; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=sRZLyK5pemP2hiv18N1mAT71UIRd18keUqsb4FzlT8I=; b=fI5a6xvCZ+txRsZUwGMeRP3iWBwrO45dhaPuEIVVCd8tKIUPKhklpQzwQLW/cQIWnk HWrARz1LRV1NaGl8xa3O8dlBhBezfBXGA6OzPRJcM5zkAzELGoTCY3b/gF4goChjOAq2 g2Cmz5aITw2L5spMNBrfdo6rEpsl//YwprEiJ1J40dxL6gjkfrSHWMOM/OXEORQHSGng wmM1O8nuy1J8SC2t3RhX8k3wUp46I5Dzpn2m56bbJ9KOWKhMnFwnFcZbx1vrfqGCq81q GM8+19XnZvUck04rZ49Ufbsf46MGlkYO5M6Q9epiILq/dPXzJDUikKOh5H8Rx8r63FxA P0IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796086694; x=1796691494; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=sRZLyK5pemP2hiv18N1mAT71UIRd18keUqsb4FzlT8I=; b=nm6BNVwrloDlnEdo2xw1rjgNB6XQQNoujB4ka+lfafeFmfOmKIuNtSZnn3xo2HFOCb 4SG1ak90X9fjaBjPe8B4x9l1gl3CBebxUT6ZVS3sQ689co0veKCcUhI/JCHLz2/5jleg xfsSf3tLud6fhGkArgQ9fa5IcJ17BwKwC4hQLfdyrThbEVOS2AlK82foZ141U3j/un9y vv9m4XHf6u54AIHeRsl4FnRFuAiEhEmBXgoYCN5GC/LnKtBDaMYn6H3IWmtcNh+xq9+c NYJK9Y3fjHt+5tzYViBMfk1rH7mMCQLkCTq2uMP/Fx9F/jZZRvkxnFtSWcKjCmq8WJIL 1mjA== X-Forwarded-Encrypted: i=1; AL+hErFzftFTHdg6PG1XXSEarNXsPu3rJbctSp4HIQLI3pQTJBhpdB56tbTJlH0bNknSaHuS9wFvwET3nns3+V0=@vger.kernel.org X-Gm-Message-State: APuU4Z/sOumedb/gxVlvFUwZaQOjUmCWXJM3WHu+QxE76PP3MxYii4yq SMhhXuRCI1Jxdm8ZJj9N6uMHouCHEjr/MpZsNNz1taMlEtXcL2UAQSbs X-Gm-Gg: AZFdTZdtMkDZpabSl2RdC5V2MFFALDF5LH3uu5zSW+xb2NO7ws18J9yhX8fdNaHLZXW HfHzGgtOaxc/Flx1UdMbrO0N6/oArN/4i0WUfH8CP4xjG0swl/NvupsoMbVruVsHLVEJGstYHbO iaC7sNqAHd8IEJydbv99+2Am0DzQkrea3wuJjkESpJm6JuG+ou5eeQdXhcGyUukdzpw6l/4+1s0 qOe42aHJ1YMVhFEulecGU6zmFgVgc6m4YL5HnfIXJY9L3KYPolR8p+tAuRndNHkoHiDr2F66kJu bgVylsqfkirkVFTo1TA8d5ISuaekpUEXP72WkuhUWki56keSmRMnxQ34Hdgz+eq8dkuOIieEHsW czdIj98WZtx6QZWhnkaiFuQWlp7Sxd4s5SwLCTgOtuEyxR9i73u80RO8Z8/KWapy54+NqcRRVUY mU+DIEIAWtNjfeRaT/6Xa01TToCW/8CQxxoTP3gXl1Gh+ECM+vWUpRkCk20CNI25vn8BWpSPuya pYbNgWfxBMVUMd+TLtCwSt1P8aapMxoYfOs2RWqsc4XxZV0NRXGGIPY4O6zItr7FFOQoUopQkvt m2hopXV73zQXBDExCZ3NIcMck46wguCuSnxCsIWbTIZdiBgOvFdbdZpKpmErdyISim3MFR5oFau lEceAfWJi6mRQsbJxADF+/4vYKiSwE41ZDywu X-Received: by 2002:a05:6830:448d:b0:862:a901:2202 with SMTP id 46e09a7af769-86315657d67mr36429390a34.17.1796086693619; Mon, 30 Nov 2026 16:58:13 -0800 (PST) Received: from localhost ([2a03:2880:10ff:58::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-8631670c44dsm24198239a34.0.2026.11.30.16.58.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 16:58:11 -0800 (PST) From: Kumar Kartikeya Dwivedi To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Emil Tsalapatis , Ihor Solodrai , kkd@meta.com, kernel-team@meta.com, Dave Hansen , Andy Lutomirski , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Puranjay Mohan , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v3 2/3] bpf, x86: Skip the PROBE_MEM address range check under SMAP Date: Tue, 1 Dec 2026 01:58:01 +0100 Message-ID: <857caa44a42b3af1cc262553464d138165a1a15c.1796086624.git.memxor@gmail.com> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=10177; h=from:subject:message-id; bh=OJhKDzucjGxOnwZDzXVRMdRGzDbcUK3WdU6Axk3hBBQ=; b=owGbwMvMwCXmrmtenRyi38x4Wi2JIZtPOnFNzzrucr7n313vlRx66ty5X3i2yz/fYq7ip7eL+d6s dbXpKGVhEONikBVTZCn5v4/J+ETl70DbZdwwc1iZQIYwcHEKwET+LmFk6PS5NsXhpG9/rt3h9uh7bO IvnY/f1L/aH95/ZVLdFPeD1gz/8+SFVOuNJ5+WtwzkfXv2rq/GG55Wq6gJEUd2nP5w/rYOOwA= X-Developer-Key: i=memxor@gmail.com; a=openpgp; fpr=B34BD741DE8494B76E2F717880EF20021D46C59B Content-Transfer-Encoding: 8bit The x86 JIT puts a range check in front of every PROBE_MEM load to keep user addresses, the guard page above them and the vsyscall page away from it. A fault on a user or vsyscall address used to oops under SMAP instead of using the load's exception table entry. The check is five instructions (lea, shr, jne, xor, jmp) and 17 or 18 bytes, for a load of three to eight bytes. do_user_addr_fault() now fixes up such a fault when the faulting instruction has an EX_TYPE_BPF exception table entry. With LASS the access raises #GP, which exc_general_protection() has always fixed up. So emit the bare load when SMAP is enabled. The feature cannot change after boot, so it is checked at JIT time. Kernels without SMAP, including those booted with clearcpuid=smap, keep the check. bpf_arch_uaddress_limit() still returns 0 on x86, so the verifier adds no check of its own. Programs loaded without CAP_BPF keep the check too. The fault handler finds a program's exception table through the BPF ksym tree, and bpf_prog_kallsyms_add() leaves such programs out of it. They can still have PROBE_MEM loads: with kernel.unprivileged_bpf_disabled=0, a socket filter loaded with only CAP_PERFMON can call bpf_skc_to_tcp_sock() and walk pointers from the result. bpf_prog_load() now records whether the loader had CAP_BPF, and the new bpf_prog_kallsyms_allowed() gives that answer to both the JIT and bpf_prog_kallsyms_add(). JITed code size from veristat, on one kernel booted with and without clearcpuid=smap, over the BPF selftests and 474 production objects from Meta: programs with PROBE_MEM reduction per program mean median max BPF selftests 5486 100 20.2% 19.9% 58.2% Meta production programs 1942 262 3.5% 0.8% 51.4% No program grows; dump_tcp6 of the selftests shrinks from 3142 to 2129 bytes. Most programs are unchanged, because loads through trusted pointers and the probe_read helpers do not use PROBE_MEM. flex_array_far_join in verifier_btf_array_access matched the lea of the check to see that a far load got a fault handler. Under SMAP such a load is now the bare load, so drop that expectation. flex_array_far_load still runs a far load on an unmapped address. Signed-off-by: Kumar Kartikeya Dwivedi --- arch/x86/net/bpf_jit_comp.c | 33 +++++++++++++++---- include/linux/bpf.h | 1 + include/linux/filter.h | 1 + kernel/bpf/core.c | 16 +++++++-- kernel/bpf/syscall.c | 1 + .../bpf/progs/verifier_btf_array_access.c | 3 +- 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 2b2a81e02cfc..daea13bcce5f 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -299,6 +299,9 @@ struct jit_context { */ int tail_call_direct_label; int tail_call_indirect_label; + + /* Emit the address range check in front of PROBE_MEM loads */ + bool probe_mem_check; }; /* Maximum number of bytes emitted while JITing one eBPF insn */ @@ -2573,6 +2576,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * const s32 imm32 = insn->imm; u32 dst_reg = insn->dst_reg; u32 src_reg = insn->src_reg; + bool probe_mem, bounds_check; bool accesses_stack_only; u8 b3 = 0; u8 *start_of_ldx; @@ -3117,6 +3121,9 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * case BPF_LDX | BPF_PROBE_MEMSX | BPF_B: case BPF_LDX | BPF_PROBE_MEMSX | BPF_H: case BPF_LDX | BPF_PROBE_MEMSX | BPF_W: + probe_mem = BPF_MODE(insn->code) == BPF_PROBE_MEM || + BPF_MODE(insn->code) == BPF_PROBE_MEMSX; + bounds_check = probe_mem && ctx->probe_mem_check; insn_off = insn->off; if (src_reg == BPF_REG_PARAMS) { if (insn_off == 8 && !arena_sp) { @@ -3134,8 +3141,7 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * */ } - if (BPF_MODE(insn->code) == BPF_PROBE_MEM || - BPF_MODE(insn->code) == BPF_PROBE_MEMSX) { + if (bounds_check) { /* Conservatively check that src_reg + insn->off is a kernel address: * src_reg + insn->off > TASK_SIZE_MAX + PAGE_SIZE * and @@ -3176,6 +3182,8 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * /* populate jmp_offset for JNE above to jump to start_of_ldx */ start_of_ldx = prog; end_of_jmp[-1] = start_of_ldx - end_of_jmp; + } else if (probe_mem) { + start_of_ldx = prog; } else if (!accesses_stack_only) { err = emit_kasan_check(env, &prog, src_reg, insn_off, @@ -3189,14 +3197,14 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * emit_ldsx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off); else emit_ldx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off); - if (BPF_MODE(insn->code) == BPF_PROBE_MEM || - BPF_MODE(insn->code) == BPF_PROBE_MEMSX) { + if (probe_mem) { struct exception_table_entry *ex; u8 *_insn = image + proglen + (start_of_ldx - temp); s64 delta; /* populate jmp_offset for JMP above */ - start_of_ldx[-1] = prog - start_of_ldx; + if (bounds_check) + start_of_ldx[-1] = prog - start_of_ldx; if (!bpf_prog->aux->extable) break; @@ -4893,6 +4901,15 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr addrs[i] = proglen; } ctx.cleanup_addr = proglen; + /* + * With SMAP enabled, a PROBE_MEM load of a user address faults (#PF, or + * #GP with LASS) and the fault handler resolves the exception table + * entry of the program, as for an unmapped kernel address. That lookup + * only finds programs in the BPF ksym tree, so programs loaded without + * CAP_BPF keep the address range check, as do kernels without SMAP. + */ + ctx.probe_mem_check = !cpu_feature_enabled(X86_FEATURE_SMAP) || + !bpf_prog_kallsyms_allowed(prog); skip_init_addrs: /* @@ -5235,7 +5252,11 @@ bool bpf_jit_supports_ptr_xchg(void) return true; } -/* x86-64 JIT emits its own code to filter user addresses so return 0 here */ +/* + * The x86-64 JIT keeps user addresses away from PROBE_MEM loads itself, with + * its own range check or by leaving the access to SMAP and the fault handler + * (see bpf_int_jit_compile()), so return 0 here. + */ u64 bpf_arch_uaddress_limit(void) { return 0; diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 4f9c2bf64643..db55849b7383 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1936,6 +1936,7 @@ struct bpf_prog_aux { enum bpf_attach_type saved_dst_attach_type; bool verifier_zext; /* Zero extensions has been inserted by verifier. */ bool dev_bound; /* Program is bound to the netdev. */ + bool bpf_capable; /* Program was loaded with CAP_BPF. */ bool offload_requested; /* Program is bound and offloaded to the netdev. */ bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */ bool attach_tracing_prog; /* true if tracing another tracing program */ diff --git a/include/linux/filter.h b/include/linux/filter.h index 06f128632d5c..40df54efb817 100644 --- a/include/linux/filter.h +++ b/include/linux/filter.h @@ -1686,6 +1686,7 @@ int bpf_get_kallsym(unsigned int symnum, unsigned long *value, char *type, char *sym); struct bpf_prog *bpf_prog_ksym_find(unsigned long addr); +bool bpf_prog_kallsyms_allowed(const struct bpf_prog *fp); void bpf_prog_kallsyms_add(struct bpf_prog *fp); void bpf_prog_kallsyms_del(struct bpf_prog *fp); diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index facb0d6d2e8d..f515c46a01ca 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -953,10 +953,22 @@ static bool bpf_prog_kallsyms_candidate(const struct bpf_prog *fp) return fp->jited && !bpf_prog_was_classic(fp); } +/* + * Programs loaded without CAP_BPF are kept out of the BPF ksym tree, so + * search_bpf_extables() does not find their exception tables. A JIT that + * relies on the exception table to recover from a faulting PROBE_MEM load needs + * this answer at JIT time, before bpf_prog_kallsyms_add() runs. Both get the + * answer that bpf_prog_load() recorded, so they cannot disagree, and the JIT + * makes no capability check of its own. + */ +bool bpf_prog_kallsyms_allowed(const struct bpf_prog *fp) +{ + return fp->aux->main_prog_aux->bpf_capable; +} + void bpf_prog_kallsyms_add(struct bpf_prog *fp) { - if (!bpf_prog_kallsyms_candidate(fp) || - !bpf_token_capable(fp->aux->token, CAP_BPF)) + if (!bpf_prog_kallsyms_candidate(fp) || !bpf_prog_kallsyms_allowed(fp)) return; bpf_prog_ksym_set_addr(fp); diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 198438b95849..539ac76a28cb 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -3104,6 +3104,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at /* move token into prog->aux, reuse taken refcnt */ prog->aux->token = token; token = NULL; + prog->aux->bpf_capable = bpf_cap; prog->aux->user = get_current_user(); prog->len = attr->insn_cnt; diff --git a/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c b/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c index 2d48abed9ad9..da8e6e958b9c 100644 --- a/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c +++ b/tools/testing/selftests/bpf/progs/verifier_btf_array_access.c @@ -569,8 +569,7 @@ int flex_array_far_load(void *ctx) /* The join has the offsets from the start of the array to 1 << 40. */ SEC("tp_btf/exit_mmap") -__success __arch_x86_64 -__jited(" leaq 0xa00000(%rbx), %r11") +__success __naked void flex_array_far_join(void) { asm volatile (" \ -- 2.53.1-Meta