Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1710849rwb; Mon, 7 Nov 2022 05:03:22 -0800 (PST) X-Google-Smtp-Source: AMsMyM5V2ngCH6dlYKlhZv1/XxB0R2CJWEW9W7FTEERmYkgfWYuAzTdseOhjt3TZhYAIFqZjWDHI X-Received: by 2002:aa7:cd58:0:b0:462:d797:483d with SMTP id v24-20020aa7cd58000000b00462d797483dmr51691040edw.343.1667826202513; Mon, 07 Nov 2022 05:03:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667826202; cv=none; d=google.com; s=arc-20160816; b=dQDOBJXfaSFWDfvL9EEFLsISv6l8DeW7Rv/2QVAjKgR5WAXnld0aWl2x/Zzy1ZF2kU nai9Fi6HT4iRhMDCV6mYmIbvifVRRLXSWs6JVfgYq290g804kLWGlaX3c86j7nFxJuvy DdAapaCsgHyYyORhf4IJSSm1teiNTtte7syXSyIiMwrYp5okxM1GRFhx7KfsCk8TGBit kQa7iIsRIcl1nm5t3nrEwYwKIAy2GRl/0+wkK+JCdp99uEeWtvmlzD0OMwYfmBFSd+E1 HmywaeqzV9c96l9bTSOc7eEZYyKVRvwaRX/nolIbRjW69zFKj50/xBRyHlfytfejJx7r Alhw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=28e5/DOfFMwhuxXr1Kct3xaRsFDz1GuJ5MgEaCNN7Eo=; b=LbdTbCR+8juJk4yiRcGmfijkrrnmzLqjwZmJMfpx5ARJmJfXHsCElfDQhGoi8xDIDD SFFg/CSeTXX0Mvr47TVKtyXW81t7AXjZfUIKa9d1oOaNd7aFWNz+L5VXEBC+CcdF7x4r hDSrjLBP0B3fmwO759MlcYTVcVyOmALya9O5TAcKV8rhR1quW4ZHoEPK8R1M/1jtLRMa Dg0gHc0/qv16nytkhawqyFwGxVW8/d1tt+b+cxmRUObqK7kyEislVm+EwbQNtgEve9Z0 0gvczbn1gIBMIeRrpipDDr+uN0y5+Wa5Xuz7fXlHtJNSAV1QtSIKa2gzvKziGtyjBnHa VQYg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qb7-20020a1709077e8700b007ad6a0afbc6si9117311ejc.7.2022.11.07.05.02.55; Mon, 07 Nov 2022 05:03:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232208AbiKGMdr (ORCPT + 93 others); Mon, 7 Nov 2022 07:33:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232136AbiKGMdp (ORCPT ); Mon, 7 Nov 2022 07:33:45 -0500 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10BFF6251; Mon, 7 Nov 2022 04:33:42 -0800 (PST) Received: from mail02.huawei.com (unknown [172.18.147.228]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4N5Vpt0xBSz9v7Vw; Mon, 7 Nov 2022 20:27:02 +0800 (CST) Received: from roberto-ThinkStation-P620 (unknown [10.204.63.22]) by APP2 (Coremail) with SMTP id GxC2BwAX_vkA+2hjCL9FAA--.24967S2; Mon, 07 Nov 2022 13:33:17 +0100 (CET) Message-ID: <7ecbf4fff621bb3340422ff668452c0bbf4c4e71.camel@huaweicloud.com> Subject: Re: [RESEND][RFC][PATCH 2/3] bpf-lsm: Limit values that can be returned by security modules From: Roberto Sassu To: Alexei Starovoitov Cc: KP Singh , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Florent Revest , Brendan Jackman , Shuah Khan , Paul Moore , Casey Schaufler , Mimi Zohar , bpf , LSM List , linux-integrity , "open list:KERNEL SELFTEST FRAMEWORK" , LKML , nicolas.bouchinet@clip-os.org Date: Mon, 07 Nov 2022 13:32:57 +0100 In-Reply-To: References: <20221028165423.386151-1-roberto.sassu@huaweicloud.com> <20221028165423.386151-2-roberto.sassu@huaweicloud.com> <38c3ff70963de4a7a396c0fad84349c7c39c0f07.camel@huaweicloud.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5-0ubuntu1 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-CM-TRANSID: GxC2BwAX_vkA+2hjCL9FAA--.24967S2 X-Coremail-Antispam: 1UD129KBjvJXoW3Ar47JFW5uFW5Zr15ur1DAwb_yoW7KF4fpF WxGa4Ykr4vqrW3Ca4ava18Za1Fy3yftr4UWrnrJr1Yvwn0vrn5tr45Gr1Y9F93Gr40kr40 vr42qFW3u34DAFDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk0b4IE77IF4wAFF20E14v26ryj6rWUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIEc7CjxV AFwI0_Gr0_Gr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IYc2Ij 64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x 8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r4a6rW5MIIYrxkI7VAKI48JMIIF0xvE 2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42 xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv 6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUFDGOUUUUU X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQAJBF1jj4UjMwAEss X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2022-11-04 at 17:42 -0700, Alexei Starovoitov wrote: > On Fri, Nov 4, 2022 at 8:29 AM Roberto Sassu > wrote: > > On Thu, 2022-11-03 at 16:09 +0100, KP Singh wrote: > > > On Fri, Oct 28, 2022 at 6:55 PM Roberto Sassu > > > wrote: > > > > From: Roberto Sassu > > > > > > > > BPF LSM defines a bpf_lsm_*() function for each LSM hook, so that > > > > security modules can define their own implementation for the desired hooks. > > > > > > > > Unfortunately, BPF LSM does not restrict which values security modules can > > > > return (for non-void LSM hooks). Security modules might follow the > > > > conventions stated in include/linux/lsm_hooks.h, or put arbitrary values. > > > > > > > > This could cause big troubles, as the kernel is not ready to handle > > > > possibly malicious return values from LSMs. Until now, it was not the > > > > > > I am not sure I would call this malicious. This would be incorrect, if > > > someone is writing a BPF LSM program they already have the powers > > > to willingly do a lot of malicious stuff. > > > > > > It's about unknowingly returning values that can break the system. > > > > Maybe it is possible to return specific values that lead to acquire > > more information/do actions that the eBPF program is not supposed to > > cause. > > > > I don't have a concrete example, so I will use the word you suggested. > > > > > > case, as each LSM is carefully reviewed and it won't be accepted if it > > > > does not meet the return value conventions. > > > > > > > > The biggest problem is when an LSM returns a positive value, instead of a > > > > negative value, as it could be converted to a pointer. Since such pointer > > > > escapes the IS_ERR() check, its use later in the code can cause > > > > unpredictable consequences (e.g. invalid memory access). > > > > > > > > Another problem is returning zero when an LSM is supposed to have done some > > > > operations. For example, the inode_init_security hook expects that their > > > > implementations return zero only if they set the name and value of the new > > > > xattr to be added to the new inode. Otherwise, other kernel subsystems > > > > might encounter unexpected conditions leading to a crash (e.g. > > > > evm_protected_xattr_common() getting NULL as argument). > > > > > > > > Finally, there are LSM hooks which are supposed to return just one as > > > > positive value, or non-negative values. Also in these cases, although it > > > > seems less critical, it is safer to return to callers of the LSM > > > > infrastructure more precisely what they expect. > > > > > > > > As eBPF allows code outside the kernel to run, it is its responsibility > > > > to ensure that only expected values are returned to LSM infrastructure > > > > callers. > > > > > > > > Create four new BTF ID sets, respectively for hooks that can return > > > > positive values, only one as positive value, that cannot return zero, and > > > > that cannot return negative values. Create also corresponding functions to > > > > check if the hook a security module is attached to belongs to one of the > > > > defined sets. > > > > > > > > Finally, check in the eBPF verifier the value returned by security modules > > > > for each attached LSM hook, and return -EINVAL (the security module cannot > > > > run) if the hook implementation does not satisfy the hook return value > > > > policy. > > > > > > > > Cc: stable@vger.kernel.org > > > > Fixes: 9d3fdea789c8 ("bpf: lsm: Provide attachment points for BPF LSM programs") > > > > Signed-off-by: Roberto Sassu > > > > --- > > > > include/linux/bpf_lsm.h | 24 ++++++++++++++++++ > > > > kernel/bpf/bpf_lsm.c | 56 +++++++++++++++++++++++++++++++++++++++++ > > > > kernel/bpf/verifier.c | 35 +++++++++++++++++++++++--- > > > > 3 files changed, 112 insertions(+), 3 deletions(-) > > > > > > > > diff --git a/include/linux/bpf_lsm.h b/include/linux/bpf_lsm.h > > > > index 4bcf76a9bb06..cd38aca4cfc0 100644 > > > > --- a/include/linux/bpf_lsm.h > > > > +++ b/include/linux/bpf_lsm.h > > > > @@ -28,6 +28,10 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog, > > > > const struct bpf_prog *prog); > > > > > > > > bool bpf_lsm_is_sleepable_hook(u32 btf_id); > > > > +bool bpf_lsm_can_ret_pos_value(u32 btf_id); > > > > +bool bpf_lsm_can_ret_only_one_as_pos_value(u32 btf_id); > > > > +bool bpf_lsm_cannot_ret_zero(u32 btf_id); > > > > +bool bpf_lsm_cannot_ret_neg_value(u32 btf_id); > > > > > > > > > > This does not need to be exported to the rest of the kernel. Please > > > have this logic in bpf_lsm.c and export a single verify function. > > > > > > Also, these really don't need to be such scattered logic, Could we > > > somehow encode this into the LSM_HOOK definition? > > > > The problem is that a new LSM_HOOK definition would apply to every LSM > > hook, while we need the ability to select subsets. > > > > I was thinking, but I didn't check yet, what about using BTF_ID_FLAGS, > > introducing a flag for each interval (<0, 0, 1, >1) and setting the > > appropriate flags for each LSM hook? > > Before adding infra to all hooks, let's analyze all hooks first. > I thought the number of exceptions is very small. > 99% of hooks will be fine with IS_ERR. > If so, adding an extra flag to every hook will cause too much churn. If I counted them correctly, there are 12 hooks which can return a positive value. Among them, 6 can return only 1. 3 should not return a negative value. A reason for making this change in the LSM infrastructure would be that the return values provided there would be the official reference for anyone dealing with LSM hooks (e.g. redefining the LSM_HOOK macro). Another reason would be that for new hooks, the developer introducing them would have to provide the information. BPF LSM would use that automatically (otherwise it might get out of sync). The idea would be to use BTF_ID_FLAGS() with the flags coming from lsm_hook_defs.h, and to check if a flag is set depending on the interval of the return value provided by the eBPF program. Roberto