From patchwork Mon May 10 17:22:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YiFei Zhu X-Patchwork-Id: 12248891 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=unavailable 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 D7984C43460 for ; Mon, 10 May 2021 17:23:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B658D61264 for ; Mon, 10 May 2021 17:23:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232799AbhEJRYp (ORCPT ); Mon, 10 May 2021 13:24:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232768AbhEJRYN (ORCPT ); Mon, 10 May 2021 13:24:13 -0400 Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D72E0C061574; Mon, 10 May 2021 10:23:07 -0700 (PDT) Received: by mail-qt1-x82c.google.com with SMTP id t20so8220819qtx.8; Mon, 10 May 2021 10:23:07 -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=c2CF6wWxSeaRukQXS9D3kYC7ceIBWXkvfZuqNiEOj+o=; b=WqAWHZMm6eAK0zTUbLWe79O9z67IcrQBSPAWAl2vBkdQq207NC+4fQfMOWBWf+9vC6 NWiN+gAnCu62A63Na/yIhG3LvYNpt8VOQcRpLjEWAes1TcFBe7lmtWPjxyWIofsY0sl9 1lb4wdJ11FOoeiFttrxWzTrKdKzx6KJ3EceY8CP8X8phzDkbfLoHxHogv5USjEDr+KTg eOt2NJmwWLCLob252L+Yyxg4ScpxnKVNgjpNohvwmK4jeFlV3ciUN7rGxn8/HL4Zhqxz yCaf20VZo3K9d5IQTzK4aBp3Ot+SB5ia8xQy36GzAWrYskbxHTg2oT6S5t6MoFJgbP+Q K/7g== 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=c2CF6wWxSeaRukQXS9D3kYC7ceIBWXkvfZuqNiEOj+o=; b=m+wc0qWqVnuNL+/B8iTAp6yiFnEXyn6KX2ywFXNttox+rD3EHGrncONHhcQUhh1DFg HtQZSLbx6J0hOqAROX9ymR9/yK94ckZOe6civU03guReFFHPGXWu4vVI9rKpU/uqxMlM mxAMm33Oljh3TYFiPLbZSs/SgjOKgvbG2118eHN5v/VJ+jr9BtIJ+rEtbLdb/M8osn7I JZighGeG6aqS5RShtZw92VtvM5Wc+E44uC1/MAawa+Kgp6NN6026tz4eFi++6QMRHdjd x3keHCSA8g0IWd8p/FFFo7Deq6D1OV3SdgcTnZST5uPgBVXrLDGrJE9ieERRmBJ2YQIs o9tA== X-Gm-Message-State: AOAM533ctzbjsaovPwpA979vuN7GsQ/W3CxbQC2bd04uUTRrXnkMBd+h 4SBTChlrk09I1CCfmQPufWo= X-Google-Smtp-Source: ABdhPJxRUeU5QumnSTZa/U9nbnrewiDGajrInXlBLwfuKZzvyJZ9ZIJGVPH4dJM4ssLBN7m0dy2cEw== X-Received: by 2002:a05:622a:486:: with SMTP id p6mr23934227qtx.98.1620667387145; Mon, 10 May 2021 10:23:07 -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.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 May 2021 10:23:06 -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 11/12] bpf/verifier: support NULL-able ptr to BTF ID as helper argument Date: Mon, 10 May 2021 12:22:48 -0500 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: From: YiFei Zhu This is to allow progs with no access to ptr to BPF ID still be able to call some helpers, with these arguments set as NULL, so the helper implementation may set a fallback when NULL is passed in. Signed-off-by: YiFei Zhu --- include/linux/bpf.h | 1 + kernel/bpf/verifier.c | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 2019c0893250..efa6444b88d3 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -303,6 +303,7 @@ enum bpf_arg_type { ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */ ARG_PTR_TO_SOCKET_OR_NULL, /* pointer to bpf_sock (fullsock) or NULL */ ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */ + ARG_PTR_TO_BTF_ID_OR_NULL, /* pointer to in-kernel struct or NULL */ ARG_PTR_TO_ALLOC_MEM, /* pointer to dynamically allocated memory */ ARG_PTR_TO_ALLOC_MEM_OR_NULL, /* pointer to dynamically allocated memory or NULL */ ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */ diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 8eec1796caaa..8a08a27e0abc 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -484,7 +484,8 @@ static bool arg_type_may_be_null(enum bpf_arg_type type) type == ARG_PTR_TO_CTX_OR_NULL || type == ARG_PTR_TO_SOCKET_OR_NULL || type == ARG_PTR_TO_ALLOC_MEM_OR_NULL || - type == ARG_PTR_TO_STACK_OR_NULL; + type == ARG_PTR_TO_STACK_OR_NULL || + type == ARG_PTR_TO_BTF_ID_OR_NULL; } /* Determine whether the function releases some resources allocated by another @@ -4808,6 +4809,7 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_SOCKET] = &fullsock_types, [ARG_PTR_TO_SOCKET_OR_NULL] = &fullsock_types, [ARG_PTR_TO_BTF_ID] = &btf_ptr_types, + [ARG_PTR_TO_BTF_ID_OR_NULL] = &btf_ptr_types, [ARG_PTR_TO_SPIN_LOCK] = &spin_lock_types, [ARG_PTR_TO_MEM] = &mem_types, [ARG_PTR_TO_MEM_OR_NULL] = &mem_types, @@ -5436,10 +5438,14 @@ static bool check_btf_id_ok(const struct bpf_func_proto *fn) int i; for (i = 0; i < ARRAY_SIZE(fn->arg_type); i++) { - if (fn->arg_type[i] == ARG_PTR_TO_BTF_ID && !fn->arg_btf_id[i]) + if ((fn->arg_type[i] == ARG_PTR_TO_BTF_ID || + fn->arg_type[i] == ARG_PTR_TO_BTF_ID_OR_NULL) && + !fn->arg_btf_id[i]) return false; - if (fn->arg_type[i] != ARG_PTR_TO_BTF_ID && fn->arg_btf_id[i]) + if ((fn->arg_type[i] != ARG_PTR_TO_BTF_ID && + fn->arg_type[i] != ARG_PTR_TO_BTF_ID_OR_NULL) && + fn->arg_btf_id[i]) return false; }