From patchwork Thu May 3 16:04:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 134914 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp2103429lji; Thu, 3 May 2018 09:05:13 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrhMh0aHvki2QtIrlkb2YeurH+6fh+d9vKwAak1Edvm+Kix+9i5cO2jenHRSb1zuvUe7XXC X-Received: by 10.98.160.68 with SMTP id r65mr23041604pfe.235.1525363512907; Thu, 03 May 2018 09:05:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525363512; cv=none; d=google.com; s=arc-20160816; b=Smk8mcN1BUB4jZy6QKTPlRwqf5u/Z4autbW9dLzB8rQ7KCzCpaNah+XZwOUFwd0x5h FAujjpcAvyJ93EMfSKLr6KUNNTFN4ExrdZe0xlIWnDpOdtStU4uEy/YitgcUZIeypiSw DecQ6oa22cFrIDO0GrXluCa50H7I02Lhahjl6JbRrVSviaXyxfPmKJYDpaLNcE8CS//p 8+gx7lRbO24VTWecLC7YJurygB6f/xt1xRplQf2BWAruW0mNqQ6AIjF0pjhzeSKwpLBK kX7n+q/021+/mefqOrbShJtaviSKEJhJj5gf8HDkkUqdEjM6DH5LobfGpdpT5ytWThMY yUCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=QThRlevj/O9qIMl03TgnTvJ2Ebf8t5pZFPosoUMiIVc=; b=a8TbBr+PJ/XL7iHqPvn3Dsk1ly9O3GYFyRJQ3f2GHHpCWX++i0hItEm4X3U8Lj1OfC ggFBkZ76sBdIMkJEaVRxJ65iqkpEz19LOpkizTLCuDSfx2mtsIOTq/NE5WORl60a0F6g 6i+5UNltxLK6+Ae38zUPLtShpzN3Qg1aFsmaTFlkTYDDHFqIAbhfcRgL/T4XcpkVFkXg dk4jH/aRgvr4vLrVnaV0gZgDq2LSsfhRp+vlOraTDCafsDNOjuP8P/2TPbhmgv9nP1Qf hGq5RtBAkwXiAb/ev4OZAOrk2hcMVZjqet6oSdjHjsAY9W5PTzj4QPjKGKKrRfuXZnD7 OOfA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x4-v6si3524582pgr.301.2018.05.03.09.05.12; Thu, 03 May 2018 09:05:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751807AbeECQFI (ORCPT + 29 others); Thu, 3 May 2018 12:05:08 -0400 Received: from foss.arm.com ([217.140.101.70]:43958 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751455AbeECQFG (ORCPT ); Thu, 3 May 2018 12:05:06 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 00D921435; Thu, 3 May 2018 09:05:06 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9FA333F587; Thu, 3 May 2018 09:05:04 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: Mark Rutland , Alexei Starovoitov , Dan Carpenter , Daniel Borkmann , Peter Zijlstra , netdev@vger.kernel.org Subject: [PATCH] bpf: fix possible spectre-v1 in find_and_alloc_map() Date: Thu, 3 May 2018 17:04:59 +0100 Message-Id: <20180503160459.4111-1-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It's possible for userspace to control attr->map_type. Sanitize it when using it as an array index to prevent an out-of-bounds value being used under speculation. Found by smatch. Signed-off-by: Mark Rutland Cc: Alexei Starovoitov Cc: Dan Carpenter Cc: Daniel Borkmann Cc: Peter Zijlstra Cc: netdev@vger.kernel.org --- kernel/bpf/syscall.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) I found this when running smatch over a v4.17-rc2 arm64 allyesconfig kernel. IIUC this may allow for a speculative branch to an arbitrary gadget when we subsequently call ops->map_alloc_check(attr). Mark. -- 2.11.0 diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 4ca46df19c9a..8a7acd0dbeb6 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -26,6 +26,7 @@ #include #include #include +#include #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ @@ -102,12 +103,14 @@ const struct bpf_map_ops bpf_map_offload_ops = { static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) { const struct bpf_map_ops *ops; + u32 type = attr->map_type; struct bpf_map *map; int err; - if (attr->map_type >= ARRAY_SIZE(bpf_map_types)) + if (type >= ARRAY_SIZE(bpf_map_types)) return ERR_PTR(-EINVAL); - ops = bpf_map_types[attr->map_type]; + type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types)); + ops = bpf_map_types[type]; if (!ops) return ERR_PTR(-EINVAL); @@ -122,7 +125,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) if (IS_ERR(map)) return map; map->ops = ops; - map->map_type = attr->map_type; + map->map_type = type; return map; }