X-Received: by 2002:a17:907:c92:b0:6b5:c8ae:7918 with SMTP id gi18-20020a1709070c9200b006b5c8ae7918mr176909ejc.531.1645630133796; Wed, 23 Feb 2022 07:28:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645630133; cv=none; d=google.com; s=arc-20160816; b=VCNd5nWXmcyYnkHkOUAK4Y1KfqtT9H+BEJIZSHpjMel8g8DxBRN9q4nqisTAvfwg4t jvISO71ku5A6vWxR0QwKEd47W3M+mqNhlZ3PvuW7j+IPapkNjzjWeFLt4chyxPpoKHBb FjHvLzzuTjSCRTEL6lFfAkpf8dImXqW5xLdoes89zKCexqtsMcgDfDmrCJLNoZoDv3Mh Lhq2sngs/L6zr/cH27uIpKCRfaFfYWigl3n9JOEGnxYeN6TClKcZZmIDR8Q6UHdcVfv9 A9bkDQgSvrGZ3m4t9l+5LoHSyzp6y5ze999oBCL+REErTFGhZzr2y5bFDid0z0E7OQak GUNg== 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=WFaWIFIAWbCZZlUkAh2lkRliBgd1XRdRHOnZNx1Zc70=; b=pVar0mqSO+UcV+cp7hZ27exYLxWcWev7fjaOaoCz/qX+E7Dod1zZt3RLZ41y6NlGE1 2XbMTZEr0WtnJPaeuk4sXeBeqHsQtVYDaFQLZBA0pY1i+fIF5Jh7lN7cRJZxj+E9iwr4 AGqXVU0p0Z9z0L+1vT5O3u1nTr9O0G1utuvTHCkDXXHnptSD7xlBAyYVnUudbcv3ZSAn 9VdEhdN8GZN8whvbrE7zQIi5LCpTr/QuBTU3Yq68/EF4XwuWji4rFA9zYQRjPq/+WLY7 ojIEj4MJGyHKiMi9ewGgKkXsm0P5bFKX9kOeShMKmfhV56d/YS+V5LeV60CmSTSJPhsn D2IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=AE9KrSIy; spf=pass (google.com: domain of selinux-refpolicy-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=selinux-refpolicy-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ca9si9209572ejb.771.2022.02.23.07.28.45; Wed, 23 Feb 2022 07:28:53 -0800 (PST) Received-SPF: pass (google.com: domain of selinux-refpolicy-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=@redhat.com header.s=mimecast20190719 header.b=AE9KrSIy; spf=pass (google.com: domain of selinux-refpolicy-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=selinux-refpolicy-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238894AbiBWMNS (ORCPT + 22 others); Wed, 23 Feb 2022 07:13:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235867AbiBWMNR (ORCPT ); Wed, 23 Feb 2022 07:13:17 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8234A9A9B4 for ; Wed, 23 Feb 2022 04:12:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645618368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WFaWIFIAWbCZZlUkAh2lkRliBgd1XRdRHOnZNx1Zc70=; b=AE9KrSIydw79LaywNSwwATce0lXYpcLNCikAmn3rACf+5Vhwe+m4O6Z2YwpIgb0LH+RD8t KArjGbYjDVARh6378deNDXDqlPCaoMRGTD/bWGWlsPC1kHMjNkj8WLxw5iitS7SGkeTsPK 0OqMPDjSdDJVWFjyi9kOwkZdEEO7++A= Received: from mail-yb1-f199.google.com (mail-yb1-f199.google.com [209.85.219.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-621-A5egMnqKNfKd0OfeT_Qejg-1; Wed, 23 Feb 2022 07:12:47 -0500 X-MC-Unique: A5egMnqKNfKd0OfeT_Qejg-1 Received: by mail-yb1-f199.google.com with SMTP id j17-20020a25ec11000000b0061dabf74012so27378685ybh.15 for ; Wed, 23 Feb 2022 04:12:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=WFaWIFIAWbCZZlUkAh2lkRliBgd1XRdRHOnZNx1Zc70=; b=RklAyiGrfgsSr2o5jwQNurEJfa6XR9BREhvylo+JQPRjd8tEzObMCr4A73etaaqCRd hKTgv15arDo/dOX6Z+TFB3QK3jwumSvSRSAPnybif+tHQj5y+mDBtugNLJc5a2LKEnPB thFimpGCaK7QSIJaMZz4ckO9eqca9NbKiWY62OHidrw18EI0Oc+Nao0B9IkyHac/WJ3+ kuFHVvHldchZywKWdEnmZcix/XLHYnsAnVpqbt2tJnyMM0QtpysYs/flcvnb/xKK3XgK BEiPE6QWSHlE220HgGplshWSSWiCMAgVpwykgJjUsq0Cgyd2LXGW07LyJTZwof5B6aZE WmsQ== X-Gm-Message-State: AOAM532SVg0imOK1uEYU84yHaWqqbUFfMcj0YcsCfE5kAa1omiAcUTAR dy5PJryHK4xPZxcI4GNMVYGnaLD2QCHphp7ixoMmSJ+0nbKYphrdvsD7vZQMyuZWkLJgm9D4/Fe psqPsb4AWtJ1CaQeSGFQxoDVJeCNOhUrgPv94k6+WhiPJWM0= X-Received: by 2002:a25:374d:0:b0:611:a6c1:b948 with SMTP id e74-20020a25374d000000b00611a6c1b948mr27535683yba.21.1645618366850; Wed, 23 Feb 2022 04:12:46 -0800 (PST) X-Received: by 2002:a25:374d:0:b0:611:a6c1:b948 with SMTP id e74-20020a25374d000000b00611a6c1b948mr27535619yba.21.1645618365933; Wed, 23 Feb 2022 04:12:45 -0800 (PST) MIME-Version: 1.0 References: <20220221131533.74238-1-richard_c_haines@btinternet.com> <20396305e71619dbee4fa3c612925b57f4bb0a4b.camel@btinternet.com> In-Reply-To: <20396305e71619dbee4fa3c612925b57f4bb0a4b.camel@btinternet.com> From: Ondrej Mosnacek Date: Wed, 23 Feb 2022 13:12:36 +0100 Message-ID: Subject: Re: [PATCH V2] security/selinux: Always allow FIOCLEX and FIONCLEX To: Richard Haines Cc: Paul Moore , Stephen Smalley , Eric Paris , demiobenour@gmail.com, SElinux list , Linux kernel mailing list , selinux-refpolicy@vger.kernel.org, Jeff Vander Stoep Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: selinux-refpolicy@vger.kernel.org On Wed, Feb 23, 2022 at 12:58 PM Richard Haines wrote: > On Tue, 2022-02-22 at 18:28 -0500, Paul Moore wrote: > > On Mon, Feb 21, 2022 at 8:15 AM Richard Haines > > wrote: > > > > > > These ioctls are equivalent to fcntl(fd, F_SETFD, flags), which > > > SELinux > > > always allows too. Furthermore, a failed FIOCLEX could result in a > > > file > > > descriptor being leaked to a process that should not have access to > > > it. > > > > > > As this patch removes access controls, a policy capability needs to > > > be > > > enabled in policy to always allow these ioctls. > > > > > > Based-on-patch-by: Demi Marie Obenour > > > Signed-off-by: Richard Haines > > > --- > > > V2 Change: Control via a policy capability. See this thread for > > > discussion: > > > https://lore.kernel.org/selinux/CAHC9VhQEPxYP_KU56gAGNHKQaxucY8gSsHiUB42PVgADBAccRQ@mail.gmail.com/T/#t > > > > > > With this patch and the polcap enabled, the selinux-testsuite will > > > fail: > > > ioctl/test at line 47 - Will need a fix. > > > > > > security/selinux/hooks.c | 7 +++++++ > > > security/selinux/include/policycap.h | 1 + > > > security/selinux/include/policycap_names.h | 3 ++- > > > security/selinux/include/security.h | 7 +++++++ > > > 4 files changed, 17 insertions(+), 1 deletion(-) > > > > Thanks Richard for putting together the v2 of this patch. > > > > As far as the test is concerned, it seems like the quick-n-dirty fix > > is to simply remove the ioctl(FIOCLEX) test in test_noioctl.c; is > > everyone okay with that? At least that is what I'm going to do with > > my local copy that I use to validate the kernel-secnext builds unless > > someone has a better patch :) > > To fix this I was planning to submit a patch that would change the > ioctl(FIOCLEX) tests to ioctl(FS_IOC_GETFSLABEL) as that would continue > to test the xperms. That one seems to be implemented only by some filesystems. Is there any more generic one we could use? > > > > > > diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c > > > index 5b6895e4f..030c41652 100644 > > > --- a/security/selinux/hooks.c > > > +++ b/security/selinux/hooks.c > > > @@ -3745,6 +3745,13 @@ static int selinux_file_ioctl(struct file > > > *file, unsigned int cmd, > > > CAP_OPT_NONE, true); > > > break; > > > > > > + case FIOCLEX: > > > + case FIONCLEX: > > > + /* Must always succeed if polcap set, else default: > > > */ > > > + if (selinux_policycap_ioctl_skip_cloexec()) > > > + break; > > > + fallthrough; > > > + > > > > The break/fallthrough looks like it might be a little more fragile > > than necessary, how about something like this: > > > > case FIOCLEX: > > case FIONCLEX: > > if (!selinux_policycap_ioctl_skip_cloexec()) > > error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); > > break; > > > > Yes, it does duplicate the default ioctl_has_perm() call, but since > > we > > are effectively deprecating this and locking the FIOCLEX/FIONCLEX > > behavior with this policy capability it seems okay to me (and > > preferable to relying on the fallthrough). > > > > Thoughts? > > Yes I did ponder this and in my first attempt I had this before the > switch(): > > /* Must always succeed if polcap set */ > if (selinux_policycap_ioctl_skip_cloexec() && > (cmd == FIOCLEX || cmd == FIONCLEX)) > return 0; > > switch (cmd) { > case FIONREAD: > case FIBMAP: > > but changed to within the switch(), anyway I'm happy to resubmit a > patch either way. I agree with Paul's suggestion. Better to duplicate the simple call than to complicate the code flow. -- Ondrej Mosnacek Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.