Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp534043rwi; Thu, 27 Oct 2022 04:44:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Bpn7R/u8GAVE8M4rt9OiKmrkgSq/tU5z6HfLR/mKBDb179n5ojqMnY/lqZ61C9kXiz/k0 X-Received: by 2002:a17:907:7805:b0:780:24fd:c4e8 with SMTP id la5-20020a170907780500b0078024fdc4e8mr42014213ejc.78.1666871082261; Thu, 27 Oct 2022 04:44:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666871082; cv=none; d=google.com; s=arc-20160816; b=qQ3RgNHmx6rLPFIe4D0TnodKOPVGhpwK0elqjZxXI7Lb7x2BpQL3zFpv2Gi7kQuGbO ZG7MD/v90j0no9EHuyN0v6qROcdnf5sQ82EtGSXeEB29BmkBldL1gd9Tpop0ycNxfNU2 mqcrjrPiPXjZYQt4BqA//hK8Rnz4UiYGeVGHCB2ed6UaYPOuQ6oo7l04zVAAiTuUSMce yFKeY5syGJe9Qrwn8N3DPEAFtldHdVCX2fM+zvH8cef1tLvnF2+qb5TMoRvuQrJuXylR wAy1ShAh8jq4KTaPS6s8bbU70MMOC3s/xOM4Jl576F9OF8SVvzJCxqQdh4PFmyZYhbCD 4pFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=62qpToplQpxP20fnEw3MlDWlg4s//K5EPdvnwtYIEFk=; b=l8wqW5/QgMfu+BMVWQwY6Hr7ZNr0WI21+T0VhbX04p/H9CUvMACTIgI2DkbszUMosA D3VidWVsKlIZQDMTlXAASmAulIUH25+1kkQ62jpWn3p8mnCRHkWIyJmef5+JqW96H6GO y/9+W6+yEmk6ij5IKhemT/p2yokScmyhEqyl0zsdAKajvUtwoeM9eIzWNQirBdDDsV6C /jmqevrh2rYYZDheTm7NwVhI0eRxVrKdr8VPFDz9gxNB8R205S/iInI7AnrkGgoPDypw 8QJbypCHk0WVzNml4FCEDIzfuvpAt/I/S3TRiLB+SCd2d4MtlIP+mlsZKKWyN6eeBfSR q8FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oKsgT1Pj; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id xf7-20020a17090731c700b007a6843971c0si1434121ejb.190.2022.10.27.04.44.16; Thu, 27 Oct 2022 04:44:42 -0700 (PDT) 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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=oKsgT1Pj; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235205AbiJ0Kjp (ORCPT + 99 others); Thu, 27 Oct 2022 06:39:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234928AbiJ0Kjl (ORCPT ); Thu, 27 Oct 2022 06:39:41 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E00164CD for ; Thu, 27 Oct 2022 03:39:34 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 4056FB8256D for ; Thu, 27 Oct 2022 10:39:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA425C43142 for ; Thu, 27 Oct 2022 10:39:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666867172; bh=Wfmr4jp4HWW9KkLYlH5RdoT/CI548D6X9rUf/WCpVQg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=oKsgT1PjuIzr/x0ppB0HfU/AtoiNBThShImmM38C5H93pDCWBAbimk9ZdSxe+v1Ao c5Za/d03Pe04V0A6ttVg3AQbrDvVHq2DI9aiKLGCYlJ0rbaI3UKLsI604/AuM29vns a53S8WjbgDiSJrVGMjyloI8yttBzQoIz1HZKwgjiaEtIFb6o/gtOhaxIMkisAaIH44 HUxrJfxrb4+O/jCIpxIC+sKyKsIjvcTuuwNMRFtxsCDUQfbysPRgeVMGgjLEiEHcGl MW+51kq5URuTrJaRT6sMR7hIqE2JTcPSI4C7ryCUplTQ/3d+cqpMh1s8Yly1cxHHiG +HjbAmS8YvrRw== Received: by mail-lj1-f173.google.com with SMTP id u2so1764219ljl.3 for ; Thu, 27 Oct 2022 03:39:31 -0700 (PDT) X-Gm-Message-State: ACrzQf0JJUWbUJs2aiV/CAAivawx9YcLS4TMokgdJdwSE5Gt3zujaB1E Sur+2tMtWu23KVZGvBJFJmTro0ZiMb4hAxSTLcm/9Q== X-Received: by 2002:a2e:9107:0:b0:277:2e85:79d4 with SMTP id m7-20020a2e9107000000b002772e8579d4mr455368ljg.425.1666867169899; Thu, 27 Oct 2022 03:39:29 -0700 (PDT) MIME-Version: 1.0 References: <20221021164626.3729012-1-roberto.sassu@huaweicloud.com> <98353f6c82d3dabdb0d434d7ecf0bfc5295015ad.camel@huaweicloud.com> <34357c96-fe58-ffe5-e464-4bded8f119d5@huaweicloud.com> In-Reply-To: From: KP Singh Date: Thu, 27 Oct 2022 12:39:19 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC][PATCH] bpf: Check xattr name/value pair from bpf_lsm_inode_init_security() To: Alexei Starovoitov Cc: Roberto Sassu , Casey Schaufler , Florent Revest , Brendan Jackman , Paul Moore , James Morris , "Serge E . Hallyn" , bpf , LSM List , LKML , nicolas.bouchinet@clip-os.org, Mimi Zohar , linux-integrity Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Wed, Oct 26, 2022 at 7:14 PM Alexei Starovoitov wrote: > > On Wed, Oct 26, 2022 at 1:42 AM Roberto Sassu > wrote: > > > > On 10/26/2022 8:37 AM, Alexei Starovoitov wrote: > > > On Tue, Oct 25, 2022 at 7:58 AM Casey Schaufler wrote: > > >> > > >> On 10/25/2022 12:43 AM, Roberto Sassu wrote: > > >>> On Mon, 2022-10-24 at 19:13 -0700, Alexei Starovoitov wrote: > > >>>> I'm looking at security_inode_init_security() and it is indeed messy. > > >>>> Per file system initxattrs callback that processes kmalloc-ed > > >>>> strings. > > >>>> Yikes. > > >>>> > > >>>> In the short term we should denylist inode_init_security hook to > > >>>> disallow attaching bpf-lsm there. set/getxattr should be done > > >>>> through kfuncs instead of such kmalloc-a-string hack. > > >>> Inode_init_security is an example. It could be that the other hooks are > > >>> affected too. What happens if they get arbitrary positive values too? > > >> > > >> TL;DR - Things will go cattywampus. > > >> > > >> The LSM infrastructure is an interface that has "grown organically", > > >> and isn't necessarily consistent in what it requires of the security > > >> module implementations. There are cases where it assumes that the > > >> security module hooks are well behaved, as you've discovered. I have > > >> no small amount of fear that someone is going to provide an eBPF > > >> program for security_secid_to_secctx(). There has been an assumption, > > >> oft stated, that all security modules are going to be reviewed as > > >> part of the upstream process. The review process ought to catch hooks > > >> that return unacceptable values. Alas, we've lost that with BPF. > > >> > > >> It would take a(nother) major overhaul of the LSM infrastructure to > > >> make it safe against hooks that are not well behaved. From what I have > > >> seen so far it wouldn't be easy/convenient/performant to do it in the > > >> BPF security module either. I personally think that BPF needs to > > >> ensure that the eBPF implementations don't return inappropriate values, > > >> but I understand why that is problematic. > > > > > > That's an accurate statement. Thank you. > > > > > > Going back to the original question... > > > We fix bugs when we discover them. > > > Regardless of the subsystem they belong to. > > > No finger pointing. > > > > I'm concerned about the following situation: > > > > struct *function() > > { > > > > ret = security_*(); > > if (ret) > > return ERR_PTR(ret); > > > > } > > > > int caller() > > { > > ptr = function() > > if (IS_ERR(ptr) > > goto out; > > > > > > } > > > > I quickly found an occurrence of this: > > > > static int lookup_one_common() > > { > > > > [...] > > > > return inode_permission(); > > } > > > > struct dentry *try_lookup_one_len() > > { > > > > [...] > > > > err = lookup_one_common(&init_user_ns, name, base, len, &this); > > if (err) > > return ERR_PTR(err); > > > > > > Unfortunately, attaching to inode_permission causes the kernel > > to crash immediately (it does not happen with negative return > > values). > > > > So, I think the fix should be broader, and not limited to the > > inode_init_security hook. Will try to see how it can be fixed. > > I see. Let's restrict bpf-lsm return values to IS_ERR_VALUE. > Trivial verifier change. Thanks, yes this indeed is an issue. We need to do a few things: 1. Restrict some hooks that we know the BPF LSM will never need. 2. A verifier function that checks return values of LSM hooks. For most LSK hooks IS_ERR_VALUE is fine, however, there are some hooks like *xattr hooks that use a return value of 1 to indicate a capability check is required which might need special handling.