netdev.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: Andrii Nakryiko <andriin@fb.com>, Jann Horn <jannh@google.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Sasha Levin <sashal@kernel.org>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: [PATCH AUTOSEL 5.6 202/606] bpf: Prevent mmap()'ing read-only maps as writable
Date: Mon,  8 Jun 2020 19:05:27 -0400	[thread overview]
Message-ID: <20200608231211.3363633-202-sashal@kernel.org> (raw)
In-Reply-To: <20200608231211.3363633-1-sashal@kernel.org>

From: Andrii Nakryiko <andriin@fb.com>

[ Upstream commit dfeb376dd4cb2c5004aeb625e2475f58a5ff2ea7 ]

As discussed in [0], it's dangerous to allow mapping BPF map, that's meant to
be frozen and is read-only on BPF program side, because that allows user-space
to actually store a writable view to the page even after it is frozen. This is
exacerbated by BPF verifier making a strong assumption that contents of such
frozen map will remain unchanged. To prevent this, disallow mapping
BPF_F_RDONLY_PROG mmap()'able BPF maps as writable, ever.

  [0] https://lore.kernel.org/bpf/CAEf4BzYGWYhXdp6BJ7_=9OQPJxQpgug080MMjdSB72i9R+5c6g@mail.gmail.com/

Fixes: fc9702273e2e ("bpf: Add mmap() support for BPF_MAP_TYPE_ARRAY")
Suggested-by: Jann Horn <jannh@google.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Jann Horn <jannh@google.com>
Link: https://lore.kernel.org/bpf/20200519053824.1089415-1-andriin@fb.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 kernel/bpf/syscall.c                          | 17 ++++++++++++++---
 tools/testing/selftests/bpf/prog_tests/mmap.c | 13 ++++++++++++-
 tools/testing/selftests/bpf/progs/test_mmap.c |  8 ++++++++
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index e04ea4c8f935..c0ab9bfdf28a 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -629,9 +629,20 @@ static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
 
 	mutex_lock(&map->freeze_mutex);
 
-	if ((vma->vm_flags & VM_WRITE) && map->frozen) {
-		err = -EPERM;
-		goto out;
+	if (vma->vm_flags & VM_WRITE) {
+		if (map->frozen) {
+			err = -EPERM;
+			goto out;
+		}
+		/* map is meant to be read-only, so do not allow mapping as
+		 * writable, because it's possible to leak a writable page
+		 * reference and allows user-space to still modify it after
+		 * freezing, while verifier will assume contents do not change
+		 */
+		if (map->map_flags & BPF_F_RDONLY_PROG) {
+			err = -EACCES;
+			goto out;
+		}
 	}
 
 	/* set default open/close callbacks */
diff --git a/tools/testing/selftests/bpf/prog_tests/mmap.c b/tools/testing/selftests/bpf/prog_tests/mmap.c
index b0e789678aa4..5495b669fccc 100644
--- a/tools/testing/selftests/bpf/prog_tests/mmap.c
+++ b/tools/testing/selftests/bpf/prog_tests/mmap.c
@@ -19,7 +19,7 @@ void test_mmap(void)
 	const size_t map_sz = roundup_page(sizeof(struct map_data));
 	const int zero = 0, one = 1, two = 2, far = 1500;
 	const long page_size = sysconf(_SC_PAGE_SIZE);
-	int err, duration = 0, i, data_map_fd;
+	int err, duration = 0, i, data_map_fd, rdmap_fd;
 	struct bpf_map *data_map, *bss_map;
 	void *bss_mmaped = NULL, *map_mmaped = NULL, *tmp1, *tmp2;
 	struct test_mmap__bss *bss_data;
@@ -36,6 +36,17 @@ void test_mmap(void)
 	data_map = skel->maps.data_map;
 	data_map_fd = bpf_map__fd(data_map);
 
+	rdmap_fd = bpf_map__fd(skel->maps.rdonly_map);
+	tmp1 = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, rdmap_fd, 0);
+	if (CHECK(tmp1 != MAP_FAILED, "rdonly_write_mmap", "unexpected success\n")) {
+		munmap(tmp1, 4096);
+		goto cleanup;
+	}
+	/* now double-check if it's mmap()'able at all */
+	tmp1 = mmap(NULL, 4096, PROT_READ, MAP_SHARED, rdmap_fd, 0);
+	if (CHECK(tmp1 == MAP_FAILED, "rdonly_read_mmap", "failed: %d\n", errno))
+		goto cleanup;
+
 	bss_mmaped = mmap(NULL, bss_sz, PROT_READ | PROT_WRITE, MAP_SHARED,
 			  bpf_map__fd(bss_map), 0);
 	if (CHECK(bss_mmaped == MAP_FAILED, "bss_mmap",
diff --git a/tools/testing/selftests/bpf/progs/test_mmap.c b/tools/testing/selftests/bpf/progs/test_mmap.c
index 6239596cd14e..4eb42cff5fe9 100644
--- a/tools/testing/selftests/bpf/progs/test_mmap.c
+++ b/tools/testing/selftests/bpf/progs/test_mmap.c
@@ -7,6 +7,14 @@
 
 char _license[] SEC("license") = "GPL";
 
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 4096);
+	__uint(map_flags, BPF_F_MMAPABLE | BPF_F_RDONLY_PROG);
+	__type(key, __u32);
+	__type(value, char);
+} rdonly_map SEC(".maps");
+
 struct {
 	__uint(type, BPF_MAP_TYPE_ARRAY);
 	__uint(max_entries, 512 * 4); /* at least 4 pages of data */
-- 
2.25.1


  parent reply	other threads:[~2020-06-09  0:28 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20200608231211.3363633-1-sashal@kernel.org>
2020-06-08 23:02 ` [PATCH AUTOSEL 5.6 006/606] bpf: Fix bug in mmap() implementation for BPF array map Sasha Levin
2020-06-08 23:02 ` [PATCH AUTOSEL 5.6 009/606] net/rds: Use ERR_PTR for rds_message_alloc_sgs() Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 067/606] SUNRPC: Revert 241b1f419f0e ("SUNRPC: Remove xdr_buf_trim()") Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 068/606] bpf: Fix sk_psock refcnt leak when receiving message Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 075/606] bpf: Enforce returning 0 for fentry/fexit progs Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 076/606] selftests/bpf: Enforce returning 0 for fentry/fexit programs Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 077/606] bpf: Restrict bpf_trace_printk()'s %s usage and add %pks, %pus specifier Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 102/606] net: drop_monitor: use IS_REACHABLE() to guard net_dm_hw_report() Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 111/606] vhost/vsock: fix packet delivery order to monitoring devices Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 112/606] aquantia: Fix the media type of AQC100 ethernet controller in the driver Sasha Levin
2020-06-08 23:03 ` [PATCH AUTOSEL 5.6 114/606] net/ena: Fix build warning in ena_xdp_set() Sasha Levin
2020-06-08 23:04 ` [PATCH AUTOSEL 5.6 117/606] ibmvnic: Skip fatal error reset after passive init Sasha Levin
2020-06-08 23:04 ` [PATCH AUTOSEL 5.6 121/606] gtp: set NLM_F_MULTI flag in gtp_genl_dump_pdp() Sasha Levin
2020-06-08 23:04 ` [PATCH AUTOSEL 5.6 124/606] stmmac: fix pointer check after utilization in stmmac_interrupt Sasha Levin
2020-06-08 23:04 ` [PATCH AUTOSEL 5.6 141/606] bpf: Restrict bpf_probe_read{, str}() only to archs where they work Sasha Levin
2020-06-08 23:04 ` [PATCH AUTOSEL 5.6 142/606] bpf: Add bpf_probe_read_{user, kernel}_str() to do_refine_retval_range Sasha Levin
2020-06-08 23:04 ` [PATCH AUTOSEL 5.6 168/606] kbuild: Remove debug info from kallsyms linking Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 194/606] rxrpc: Fix the excessive initial retransmission timeout Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 195/606] rxrpc: Fix a memory leak in rxkad_verify_response() Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 198/606] flow_dissector: Drop BPF flow dissector prog ref on netns cleanup Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 200/606] rxrpc: Trace discarded ACKs Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 201/606] rxrpc: Fix ack discard Sasha Levin
2020-06-08 23:05 ` Sasha Levin [this message]
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 207/606] ax25: fix setsockopt(SO_BINDTODEVICE) Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 208/606] dpaa_eth: fix usage as DSA master, try 3 Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 209/606] ethtool: count header size in reply size estimate Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 210/606] felix: Fix initialization of ioremap resources Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 211/606] net: don't return invalid table id error when we fall back to PF_UNSPEC Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 212/606] net: dsa: mt7530: fix roaming from DSA user ports Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 213/606] net: ethernet: ti: cpsw: fix ASSERT_RTNL() warning during suspend Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 214/606] __netif_receive_skb_core: pass skb by reference Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 215/606] net: inet_csk: Fix so_reuseport bind-address cache in tb->fast* Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 216/606] net: ipip: fix wrong address family in init error path Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 217/606] net/mlx5: Add command entry handling completion Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 218/606] net: mvpp2: fix RX hashing for non-10G ports Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 219/606] net: nlmsg_cancel() if put fails for nhmsg Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 220/606] net: qrtr: Fix passing invalid reference to qrtr_local_enqueue() Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 221/606] net: revert "net: get rid of an signed integer overflow in ip_idents_reserve()" Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 222/606] net sched: fix reporting the first-time use timestamp Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 223/606] net/tls: fix race condition causing kernel panic Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 224/606] nexthop: Fix attribute checking for groups Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 225/606] r8152: support additional Microsoft Surface Ethernet Adapter variant Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 226/606] sctp: Don't add the shutdown timer if its already been added Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 227/606] sctp: Start shutdown on association restart if in SHUTDOWN-SENT state and socket is closed Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 228/606] tipc: block BH before using dst_cache Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 229/606] net/mlx5e: kTLS, Destroy key object after destroying the TIS Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 230/606] net/mlx5e: Fix inner tirs handling Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 231/606] net/mlx5: Fix memory leak in mlx5_events_init Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 232/606] net/mlx5e: Update netdev txq on completions during closure Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 233/606] net/mlx5: Fix error flow in case of function_setup failure Sasha Levin
2020-06-08 23:05 ` [PATCH AUTOSEL 5.6 234/606] wireguard: noise: read preshared key while taking lock Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 235/606] wireguard: queueing: preserve flow hash across packet scrubbing Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 236/606] wireguard: noise: separate receive counter from send counter Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 237/606] r8169: fix OCP access on RTL8117 Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 238/606] net/mlx5: Fix a race when moving command interface to events mode Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 239/606] net/mlx5: Fix cleaning unmanaged flow tables Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 241/606] net/mlx5: Avoid processing commands before cmdif is ready Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 242/606] net/mlx5: Annotate mutex destroy for root ns Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 243/606] net/tls: fix encryption error checking Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 244/606] net/tls: free record only on encryption error Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 245/606] net: sun: fix missing release regions in cas_init_one() Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 246/606] net/mlx4_core: fix a memory leak bug Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 247/606] net: sgi: ioc3-eth: Fix return value check in ioc3eth_probe() Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 248/606] mlxsw: spectrum: Fix use-after-free of split/unsplit/type_set in case reload fails Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 249/606] net: mscc: ocelot: fix address ageing time (again) Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 261/606] net: microchip: encx24j600: add missed kthread_stop Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 272/606] net: freescale: select CONFIG_FIXED_PHY where needed Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 287/606] samples: bpf: Fix build error Sasha Levin
2020-06-08 23:06 ` [PATCH AUTOSEL 5.6 288/606] drivers: net: hamradio: Fix suspicious RCU usage warning in bpqether.c Sasha Levin
2020-06-08 23:07 ` [PATCH AUTOSEL 5.6 325/606] libceph: ignore pool overlay and cache logic on redirects 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=20200608231211.3363633-202-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /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).