linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Yonghong Song <yhs@fb.com>, Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andriin@fb.com>, Sasha Levin <sashal@kernel.org>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	clang-built-linux@googlegroups.com
Subject: [PATCH AUTOSEL 5.4 32/80] bpf: Permit map_ptr arithmetic with opcode add and offset 0
Date: Mon, 26 Oct 2020 19:54:28 -0400	[thread overview]
Message-ID: <20201026235516.1025100-32-sashal@kernel.org> (raw)
In-Reply-To: <20201026235516.1025100-1-sashal@kernel.org>

From: Yonghong Song <yhs@fb.com>

[ Upstream commit 7c6967326267bd5c0dded0a99541357d70dd11ac ]

Commit 41c48f3a98231 ("bpf: Support access
to bpf map fields") added support to access map fields
with CORE support. For example,

            struct bpf_map {
                    __u32 max_entries;
            } __attribute__((preserve_access_index));

            struct bpf_array {
                    struct bpf_map map;
                    __u32 elem_size;
            } __attribute__((preserve_access_index));

            struct {
                    __uint(type, BPF_MAP_TYPE_ARRAY);
                    __uint(max_entries, 4);
                    __type(key, __u32);
                    __type(value, __u32);
            } m_array SEC(".maps");

            SEC("cgroup_skb/egress")
            int cg_skb(void *ctx)
            {
                    struct bpf_array *array = (struct bpf_array *)&m_array;

                    /* .. array->map.max_entries .. */
            }

In kernel, bpf_htab has similar structure,

	    struct bpf_htab {
		    struct bpf_map map;
                    ...
            }

In the above cg_skb(), to access array->map.max_entries, with CORE, the clang will
generate two builtin's.
            base = &m_array;
            /* access array.map */
            map_addr = __builtin_preserve_struct_access_info(base, 0, 0);
            /* access array.map.max_entries */
            max_entries_addr = __builtin_preserve_struct_access_info(map_addr, 0, 0);
	    max_entries = *max_entries_addr;

In the current llvm, if two builtin's are in the same function or
in the same function after inlining, the compiler is smart enough to chain
them together and generates like below:
            base = &m_array;
            max_entries = *(base + reloc_offset); /* reloc_offset = 0 in this case */
and we are fine.

But if we force no inlining for one of functions in test_map_ptr() selftest, e.g.,
check_default(), the above two __builtin_preserve_* will be in two different
functions. In this case, we will have code like:
   func check_hash():
            reloc_offset_map = 0;
            base = &m_array;
            map_base = base + reloc_offset_map;
            check_default(map_base, ...)
   func check_default(map_base, ...):
            max_entries = *(map_base + reloc_offset_max_entries);

In kernel, map_ptr (CONST_PTR_TO_MAP) does not allow any arithmetic.
The above "map_base = base + reloc_offset_map" will trigger a verifier failure.
  ; VERIFY(check_default(&hash->map, map));
  0: (18) r7 = 0xffffb4fe8018a004
  2: (b4) w1 = 110
  3: (63) *(u32 *)(r7 +0) = r1
   R1_w=invP110 R7_w=map_value(id=0,off=4,ks=4,vs=8,imm=0) R10=fp0
  ; VERIFY_TYPE(BPF_MAP_TYPE_HASH, check_hash);
  4: (18) r1 = 0xffffb4fe8018a000
  6: (b4) w2 = 1
  7: (63) *(u32 *)(r1 +0) = r2
   R1_w=map_value(id=0,off=0,ks=4,vs=8,imm=0) R2_w=invP1 R7_w=map_value(id=0,off=4,ks=4,vs=8,imm=0) R10=fp0
  8: (b7) r2 = 0
  9: (18) r8 = 0xffff90bcb500c000
  11: (18) r1 = 0xffff90bcb500c000
  13: (0f) r1 += r2
  R1 pointer arithmetic on map_ptr prohibited

To fix the issue, let us permit map_ptr + 0 arithmetic which will
result in exactly the same map_ptr.

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/20200908175702.2463625-1-yhs@fb.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 kernel/bpf/verifier.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index ae27dd77a73cb..bd0a5ead2af0c 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -4398,6 +4398,10 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 			dst, reg_type_str[ptr_reg->type]);
 		return -EACCES;
 	case CONST_PTR_TO_MAP:
+		/* smin_val represents the known value */
+		if (known && smin_val == 0 && opcode == BPF_ADD)
+			break;
+		/* fall-through */
 	case PTR_TO_PACKET_END:
 	case PTR_TO_SOCKET:
 	case PTR_TO_SOCKET_OR_NULL:
-- 
2.25.1


  parent reply	other threads:[~2020-10-27  0:21 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-26 23:53 [PATCH AUTOSEL 5.4 01/80] powerpc/powernv/smp: Fix spurious DBG() warning Sasha Levin
2020-10-26 23:53 ` [PATCH AUTOSEL 5.4 02/80] mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race Sasha Levin
2020-10-26 23:53 ` [PATCH AUTOSEL 5.4 03/80] powerpc: select ARCH_WANT_IRQS_OFF_ACTIVATE_MM Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 04/80] sparc64: remove mm_cpumask clearing to fix kthread_use_mm race Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 05/80] f2fs: add trace exit in exception path Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 06/80] f2fs: fix uninit-value in f2fs_lookup Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 07/80] f2fs: fix to check segment boundary during SIT page readahead Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 08/80] s390/startup: avoid save_area_sync overflow Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 09/80] um: change sigio_spinlock to a mutex Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 10/80] f2fs: handle errors of f2fs_get_meta_page_nofail Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 11/80] MIPS: ftrace: Remove redundant #ifdef CONFIG_DYNAMIC_FTRACE Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 12/80] ARM: 8997/2: hw_breakpoint: Handle inexact watchpoint addresses Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 13/80] NFS4: Fix oops when copy_file_range is attempted with NFS4.0 source Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 14/80] power: supply: bq27xxx: report "not charging" on all types Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 15/80] xfs: fix realtime bitmap/summary file truncation when growing rt volume Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 16/80] video: fbdev: pvr2fb: initialize variables Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 17/80] ath10k: start recovery process when payload length exceeds max htc length for sdio Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 18/80] ath10k: fix VHT NSS calculation when STBC is enabled Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 19/80] drm/brige/megachips: Add checking if ge_b850v3_lvds_init() is working correctly Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 20/80] selftests/x86/fsgsbase: Reap a forgotten child Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 21/80] media: videodev2.h: RGB BT2020 and HSV are always full range Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 22/80] media: platform: Improve queue set up flow for bug fixing Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 23/80] usb: typec: tcpm: During PR_SWAP, source caps should be sent only after tSwapSourceStart Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 24/80] media: tw5864: check status of tw5864_frameinterval_get Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 25/80] media: imx274: fix frame interval handling Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 26/80] mmc: via-sdmmc: Fix data race bug Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 27/80] drm/bridge/synopsys: dsi: add support for non-continuous HS clock Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 28/80] arm64: topology: Stop using MPIDR for topology information Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 29/80] printk: reduce LOG_BUF_SHIFT range for H8300 Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 30/80] ia64: kprobes: Use generic kretprobe trampoline handler Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 31/80] kgdb: Make "kgdbcon" work properly with "kgdb_earlycon" Sasha Levin
2020-10-27 12:01   ` Daniel Thompson
2020-10-26 23:54 ` Sasha Levin [this message]
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 33/80] media: uvcvideo: Fix dereference of out-of-bound list iterator Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 34/80] selftests/bpf: Define string const as global for test_sysctl_prog.c Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 35/80] samples/bpf: Fix possible deadlock in xdpsock Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 36/80] riscv: Define AT_VECTOR_SIZE_ARCH for ARCH_DLINFO Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 37/80] cpufreq: sti-cpufreq: add stih418 support Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 38/80] USB: adutux: fix debugging Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 39/80] uio: free uio id after uio file node is freed Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 40/80] coresight: Make sysfs functional on topologies with per core sink Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 41/80] usb: xhci: omit duplicate actions when suspending a runtime suspended host Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 42/80] SUNRPC: Mitigate cond_resched() in xprt_transmit() Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 43/80] arm64/mm: return cpu_all_mask when node is NUMA_NO_NODE Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 44/80] can: flexcan: disable clocks during stop mode Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 45/80] xfs: don't free rt blocks when we're doing a REMAP bunmapi call Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 46/80] ACPI: Add out of bounds and numa_off protections to pxm_to_node() Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 47/80] genirq: Add stub for set_handle_irq() when !GENERIC_IRQ_MULTI_HANDLER Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 48/80] dm: change max_io_len() to use blk_max_size_offset() Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 49/80] brcmfmac: Fix warning message after dongle setup failed Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 50/80] drivers/net/wan/hdlc_fr: Correctly handle special skb->protocol values Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 51/80] bus/fsl_mc: Do not rely on caller to provide non NULL mc_io Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 52/80] ACPI: HMAT: Fix handling of changes from ACPI 6.2 to ACPI 6.3 Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 53/80] power: supply: test_power: add missing newlines when printing parameters by sysfs Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 54/80] drm/amd/display: HDMI remote sink need mode validation for Linux Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 55/80] ARC: [dts] fix the errors detected by dtbs_check Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 56/80] btrfs: fix replace of seed device Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 57/80] md/bitmap: md_bitmap_get_counter returns wrong blocks Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 58/80] bnxt_en: Log unknown link speed appropriately Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 59/80] rpmsg: glink: Use complete_all for open states Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 60/80] clk: ti: clockdomain: fix static checker warning Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 61/80] phy: marvell: comphy: Convert internal SMCC firmware return codes to errno Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 62/80] asm-generic/io.h: Fix !CONFIG_GENERIC_IOMAP pci_iounmap() implementation Sasha Levin
2020-10-26 23:54 ` [PATCH AUTOSEL 5.4 63/80] net: 9p: initialize sun_server.sun_path to have addr's value only when addr is valid Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 64/80] drivers: watchdog: rdc321x_wdt: Fix race condition bugs Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 65/80] ext4: Detect already used quota file early Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 66/80] PCI: dwc: Add link up check in dw_child_pcie_ops.map_bus() Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 67/80] KVM: PPC: Book3S HV: Do not allocate HPT for a nested guest Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 68/80] gfs2: use-after-free in sysfs deregistration Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 69/80] gfs2: add validation checks for size of superblock Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 70/80] cifs: handle -EINTR in cifs_setattr Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 71/80] arm64: dts: renesas: ulcb: add full-pwr-cycle-in-suspend into eMMC nodes Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 72/80] ARM: dts: omap4: Fix sgx clock rate for 4430 Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 73/80] memory: emif: Remove bogus debugfs error handling Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 74/80] ARM: dts: s5pv210: remove DMA controller bus node name to fix dtschema warnings Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 75/80] ARM: dts: s5pv210: move fixed clocks under root node Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 76/80] ARM: dts: s5pv210: move PMU node out of clock controller Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 77/80] ARM: dts: s5pv210: remove dedicated 'audio-subsystem' node Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 78/80] nbd: make the config put is called before the notifying the waiter Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 79/80] sgl_alloc_order: fix memory leak Sasha Levin
2020-10-26 23:55 ` [PATCH AUTOSEL 5.4 80/80] nvme-rdma: fix crash when connect rejected Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201026235516.1025100-32-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=clang-built-linux@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).