From patchwork Mon May 10 17:22:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YiFei Zhu X-Patchwork-Id: 12248909 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C787C43600 for ; Mon, 10 May 2021 17:23:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E6DA56147F for ; Mon, 10 May 2021 17:23:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232774AbhEJRYT (ORCPT ); Mon, 10 May 2021 13:24:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46562 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232770AbhEJRYI (ORCPT ); Mon, 10 May 2021 13:24:08 -0400 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB443C061760; Mon, 10 May 2021 10:23:02 -0700 (PDT) Received: by mail-qt1-x82e.google.com with SMTP id n22so12481762qtk.9; Mon, 10 May 2021 10:23:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=os+aNkIj11roEHDUU05sC6TNDVf0op+4DKUiI/YhlCI=; b=M5fRVYqrGRIo+zDt0ww9BZnPnThLcGUFnq1uBtyv9zZCFzq7kYM+ebANCjyQPR7JyW mojidBSveomAk3SZkuZyQQbm33GnzbuwSmiT7pRt5dreyQRnAiAGSLLIjdAkL3bXtXJW Kt/uQ0ZGpjx10L02Nyw2XJqcP6SjauIIlC6rOnlauWf0OQXsXChwyC2s3wUuAHjR+OBl 449/wDIHI8AY8vsyKGmxD4MQMG/lfaEFI0m2zRzxEkducKu0VsktQiFeN03xmZIiqgWq lzx6AAApP7SWNBNaW8VN5W5QrdTaJQYfZzWHM1ZZ8LY0g4pEyrOREnKw6c7bk3FvV3y6 4acQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=os+aNkIj11roEHDUU05sC6TNDVf0op+4DKUiI/YhlCI=; b=JPsUvG9zmpXYkpPF5pcSRPOAtJb15d4OAJZeF8RwmafOjcpqolSQXMCh6Ek/Pa7pe9 ZvlRJTYVMbPBv5jqagBiJVvc0DrB3GU00gZKH8EG/4vMBfICZwltfa8/vHqN5euGOrNI Wt0snOyRxZS7wYBn7SZhUZZilH/RoWUK88S6eQrsP13R/9U9XeLcDVnw/vMxoDceK/43 0i0EGPPzCz6JyCJCvU5NtospH+XuQqKC8liohpLuqoWn/BJEwU7DkPeGrqy08OewZtQg 8M0wcKCj5nbHoaG/1a91Y8Nli47/d4bdYYbswPoMFNNthty7P9iNtpqkgzBurwwg5OPh tKZw== X-Gm-Message-State: AOAM532fdVZfOL0ehB3GVCd+d0VNnw+wYUTa5wymccTcnxCekB9OnalG uSeC11XEv0pOOLNqjD1aplCFuYVmdDk2IeZ8 X-Google-Smtp-Source: ABdhPJzGzOZt96I0Zs7TdCghlnOJueKKaLB7JVqgkm1SC2dIxERLF1FLObKXXk8uUTGdiuh0MqkEEw== X-Received: by 2002:ac8:646:: with SMTP id e6mr22635080qth.285.1620667382103; Mon, 10 May 2021 10:23:02 -0700 (PDT) Received: from localhost.localdomain (host-173-230-99-154.tnkngak.clients.pavlovmedia.com. [173.230.99.154]) by smtp.gmail.com with ESMTPSA id q7sm11924367qki.17.2021.05.10.10.23.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 May 2021 10:23:01 -0700 (PDT) From: YiFei Zhu To: containers@lists.linux.dev, bpf@vger.kernel.org Cc: YiFei Zhu , linux-security-module@vger.kernel.org, Alexei Starovoitov , Andrea Arcangeli , Andy Lutomirski , Austin Kuo , Claudio Canella , Daniel Borkmann , Daniel Gruss , Dimitrios Skarlatos , Giuseppe Scrivano , Hubertus Franke , Jann Horn , Jinghao Jia , Josep Torrellas , Kees Cook , Sargun Dhillon , Tianyin Xu , Tobin Feldman-Fitzthum , Tom Hromatka , Will Drewry Subject: [RFC PATCH bpf-next seccomp 07/12] bpf/verifier: allow restricting direct map access Date: Mon, 10 May 2021 12:22:44 -0500 Message-Id: <08b306f207cc6c516500a58ee0bc506f09859d26.1620499942.git.yifeifz2@illinois.edu> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC From: YiFei Zhu Add a verifier hook that is able to reject direct map access that does not make use of eBPF helpers. These accesses mostly correspond to eBPF data section accesses. This allows a program type to disable maps altogether by resturing direct map accesses and not whitelisting helpers that perform map accesses. Signed-off-by: YiFei Zhu --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 02b02cb29ce2..86f3e8784e43 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -484,6 +484,7 @@ struct bpf_verifier_ops { enum bpf_access_type atype, u32 *next_btf_id); bool (*check_kfunc_call)(u32 kfunc_btf_id); + bool (*map_access)(enum bpf_access_type type); }; struct bpf_prog_offload_ops { diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8fd552c16763..8eec1796caaa 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3100,6 +3100,9 @@ static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, struct bpf_map *map = regs[regno].map_ptr; u32 cap = bpf_map_flags_to_cap(map); + if (env->ops->map_access && !env->ops->map_access(type)) + cap = 0; + if (type == BPF_WRITE && !(cap & BPF_MAP_CAN_WRITE)) { verbose(env, "write into map forbidden, value_size=%d off=%d size=%d\n", map->value_size, off, size);