Received: by 2002:a25:ef43:0:0:0:0:0 with SMTP id w3csp15405ybm; Tue, 26 May 2020 09:36:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw0DYIrpzHkoIMCE/l8WSjxjsj33zslRDSRkqYDx8cHjDEwiG0xRCA0UKQJbr0u5dA4HWG9 X-Received: by 2002:a50:fb1a:: with SMTP id d26mr20808003edq.83.1590510990006; Tue, 26 May 2020 09:36:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1590510989; cv=none; d=google.com; s=arc-20160816; b=AoZ4vHLmdruJRRREnDTS952pYGHlAPcj3yAqhNHyhD5dMZwfqteg28ZOrAEIhHVO/Q 1monjPtg7LjdOkq2gJOw0JJqyfJjhPvT2weIJraZdUV+U9q0Iw64Et8dbAL7sga8Ba/v ocS/C1AR1Rx1rj2gise7zT7wRLj/EHM4IuV3wTp5cXf13kUACuyMPry87gS9QG2zYRca O4xfD87OtJOr0lioD6OV8dhXyvshoLN4STwRJBY6dY3Z1UmAPHzXImMceRMNc4Xunf7X pRGSwv/Ib2Olu4rjf5qxS+6RPCbzIzwHSsEWwy+0MuLfPEuA+xWNsQy11lYv/upVQxda oqBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=sWEW3WlniXpG51wWswVmYGNyCPa8cDkW5Yfe1j0NFdU=; b=W4EFwHwLcDNmhtWEXYNvUGSos0eQxR3tdiMp8uozpZ9xl6z8AKiNaUwq0VBvwXm8Wy fW7T2iZxs+1f4qmgYMIdBjLAV/oU/mmlINtlChEiG/AyYoHHuoLjMwPyAgp86e68PmtE 7brGYZKG9+hAx1fgsavmDSLhzAxsY+ANI/W88D+iVZckRrtcrtRYOtRBW4Uk6aRYNALp 6t/1hARoscAcBDxRs41RWcFBNwKDC2zeTvS3f0AzePuOG2i65dzjh7RARtd14y4vsXcD EBwcDNRLC34siR+LAhhGXtNehUbNV3CB4aEEttKRXXDCGhToPgItL/NSDl/VM9Wz0lqx Byjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OJHulCff; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i11si182988edr.14.2020.05.26.09.36.05; Tue, 26 May 2020 09:36:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OJHulCff; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730078AbgEZQeA (ORCPT + 99 others); Tue, 26 May 2020 12:34:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37794 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388447AbgEZQdn (ORCPT ); Tue, 26 May 2020 12:33:43 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9DA5C03E979 for ; Tue, 26 May 2020 09:33:42 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id h4so152212wmb.4 for ; Tue, 26 May 2020 09:33:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sWEW3WlniXpG51wWswVmYGNyCPa8cDkW5Yfe1j0NFdU=; b=OJHulCffT3ruBfvHd9SEc+9Txcjt3lQLkYzB8VZ4Ly5FjtzUoowlahdPAXYFkbKNTP 3TKt/XeT4Ok+xHE/FND2NiTLG1RCx4FSVSG5MRFzhb2YLP9VqW5rsDnzNw3uYcRAUaJL KLOzC7W7DuCP6phTmYMhs4wZbJORByuv/Xx9w= 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=sWEW3WlniXpG51wWswVmYGNyCPa8cDkW5Yfe1j0NFdU=; b=SY0OzIfYqUPZ/b2zL4ah8v4AOl27aWEkOW1WQDdHYSqlgQ1a9B5It5rtQHJHXDDCqz P8fHNXAdWeriscuvKNCt120Xclo0NAj8eQmdm6WiYZluhBeG0PZFBlBJn0gbRetgpNJu O7hY2BzTw8nKnjlrlJloXkXQfGUjlXp7xurdW4WkddRoGbFG/VWF1OQQPraI83fVSk33 9mWY3NFDBO3VlF6uqLtfT112VQyLea/AVQeDLFcwOB1uzCAwwDmQs9Ugcvn3au5YL4bv mpoXpFbHkIuKWI77VndBM/xhk17wkCWMCzrobxiW0wUOWSwVRtn7zca91CFAA4o5+vcM 44Bw== X-Gm-Message-State: AOAM531/trc92xeGf4qLGvrl/UKCl5naTb29uvWfAfISArl2P5ocY5r7 LXkJNmS6gMgclRTdWrX9obWcO3/SQ9M= X-Received: by 2002:a1c:f712:: with SMTP id v18mr63514wmh.27.1590510821258; Tue, 26 May 2020 09:33:41 -0700 (PDT) Received: from kpsingh.zrh.corp.google.com ([81.6.44.51]) by smtp.gmail.com with ESMTPSA id k17sm48654wmj.15.2020.05.26.09.33.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2020 09:33:40 -0700 (PDT) From: KP Singh To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, bpf@vger.kernel.org, linux-security-module@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , James Morris , Alexander Viro , Martin KaFai Lau , Florent Revest Subject: [PATCH bpf-next 3/4] bpf: Allow local storage to be used from LSM programs Date: Tue, 26 May 2020 18:33:35 +0200 Message-Id: <20200526163336.63653-4-kpsingh@chromium.org> X-Mailer: git-send-email 2.27.0.rc0.183.gde8f92d652-goog In-Reply-To: <20200526163336.63653-1-kpsingh@chromium.org> References: <20200526163336.63653-1-kpsingh@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: KP Singh Adds support for both bpf_{sk, inode}_storage_{get, delete} to be used in LSM programs. These helpers are not used for tracing programs (currently) as their usage is tied to the life-cycle of the object and should only be used where the owning object won't be freed. Thus, they are safe to use in LSM hooks, but can only be enabled in tracing programs using a whitelist based approach. Since the UAPI helper signature for bpf_sk_storage expect a bpf_sock, it, leads to a compilation warning for LSM programs, it's also updated to accept a void * pointer instead. Signed-off-by: KP Singh --- include/linux/bpf_local_storage.h | 2 ++ kernel/bpf/bpf_local_storage.c | 22 ++++++++++++++++++++++ kernel/bpf/bpf_lsm.c | 20 +++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index c6837e7838fc..8982c0c69332 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -9,6 +9,8 @@ void bpf_sk_storage_free(struct sock *sk); extern const struct bpf_func_proto bpf_sk_storage_get_proto; extern const struct bpf_func_proto bpf_sk_storage_delete_proto; +extern const struct bpf_func_proto sk_storage_get_btf_proto; +extern const struct bpf_func_proto sk_storage_delete_btf_proto; extern const struct bpf_func_proto bpf_inode_storage_get_proto; extern const struct bpf_func_proto bpf_inode_storage_delete_proto; diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index bf807cfe3a73..07e02d32feb0 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -1305,6 +1305,28 @@ const struct bpf_func_proto bpf_inode_storage_delete_proto = { .btf_id = bpf_inode_storage_delete_btf_ids, }; +static int sk_storage_get_btf_ids[4]; +const struct bpf_func_proto sk_storage_get_btf_proto = { + .func = bpf_sk_storage_get, + .gpl_only = false, + .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_PTR_TO_BTF_ID, + .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL, + .arg4_type = ARG_ANYTHING, + .btf_id = sk_storage_get_btf_ids, +}; + +static int sk_storage_delete_btf_ids[2]; +const struct bpf_func_proto sk_storage_delete_btf_proto = { + .func = bpf_sk_storage_delete, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_CONST_MAP_PTR, + .arg2_type = ARG_PTR_TO_BTF_ID, + .btf_id = sk_storage_delete_btf_ids, +}; + struct bpf_sk_storage_diag { u32 nr_maps; struct bpf_map *maps[]; diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c index 19636703b24e..fce0a11b63ca 100644 --- a/kernel/bpf/bpf_lsm.c +++ b/kernel/bpf/bpf_lsm.c @@ -11,6 +11,7 @@ #include #include #include +#include /* For every LSM hook that allows attachment of BPF programs, declare a nop * function where a BPF program can be attached. @@ -45,10 +46,27 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, return 0; } +static const struct bpf_func_proto * +bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) +{ + switch (func_id) { + case BPF_FUNC_inode_storage_get: + return &bpf_inode_storage_get_proto; + case BPF_FUNC_inode_storage_delete: + return &bpf_inode_storage_delete_proto; + case BPF_FUNC_sk_storage_get: + return &sk_storage_get_btf_proto; + case BPF_FUNC_sk_storage_delete: + return &sk_storage_delete_btf_proto; + default: + return bpf_tracing_func_proto(func_id, prog); + } +} + const struct bpf_prog_ops lsm_prog_ops = { }; const struct bpf_verifier_ops lsm_verifier_ops = { - .get_func_proto = bpf_tracing_func_proto, + .get_func_proto = bpf_lsm_func_proto, .is_valid_access = btf_ctx_access, }; -- 2.27.0.rc0.183.gde8f92d652-goog