Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp850405pxb; Wed, 6 Apr 2022 01:54:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxMY18+jygt5BE0rwT/5hP/QboXdCcJDEdlGTZNffGgrQRtnbNtzOqWznktC/qPzbzj9onQ X-Received: by 2002:a63:4919:0:b0:382:6f3d:e48 with SMTP id w25-20020a634919000000b003826f3d0e48mr6235768pga.613.1649235293009; Wed, 06 Apr 2022 01:54:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649235293; cv=none; d=google.com; s=arc-20160816; b=1KUJqKvZrmJYAtzLsLaDYUgaJEEHbS60JkeL8H9SxTvKojUxaE0c5d0yyHIXnYQ6w7 fM+j+A+2JIOFsKs6ks41AeoJzfkbIjh4U/xzTSbNrJygbRXbj3au6Ym8D4JNI479Ebor sTqdcFMcXrYzjbkmIcMCliYXX351XFk/xsEWxFauqMGrR7yyz7gQuhYkHGBe7xzzSAnm xx9Mwe2vt+VeCi0lTLFV4RhMw2qtwP95Kl1JsBhut82JgU2HM2u8OCeBlQCViq6Ia19X 0/up3LtU2Coz0hQyOoRkMCcD6EaWGBBflmqGv8SR/pJs843t61EPsNdTg2rPoYFAwN2B x5+g== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=YISfyS2RLLoZTZskgc/5Lz4Oc7O9cB9Ve5/AhCwGAm0=; b=wGfdrBX3qnbg914Ee/IoYdH/8aU/o9WN7oDJO/3ok7rM9C6d3yNrohRvB7pRnm/Ujk fbC/wZk3etrVeuCLKPluMVN4GGgahVqdykdXIQOjU1+anW2CwYPCpqUHXvVmNcQ3zw/d 6KpFxyCpPejznipnyEMO1b3L9CcZjuWWkykHWkoptslwUCD5GEu47hzSi2xBhHLWn7WV pqEeWN3jYEb6bF6ltPCtdqb+fEa7PqQbhbiUGrHHt/tOspKAhsZcSYG/idn9UR46pZtg EjQRCjEriXjOEm3ZG/6h23Q3e6hhtWkOg5j5YlkNli65sj1P5j+lZqkPq96FPI5um75F wXJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aysHmXTU; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m30-20020a637d5e000000b0038265eb3545si15738158pgn.334.2022.04.06.01.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 01:54:52 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=aysHmXTU; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3FE13588374; Wed, 6 Apr 2022 01:46:50 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573879AbiDEWxh (ORCPT + 99 others); Tue, 5 Apr 2022 18:53:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349963AbiDEJwM (ORCPT ); Tue, 5 Apr 2022 05:52:12 -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 679B93466F; Tue, 5 Apr 2022 02:50:13 -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 1D0AFB81B7A; Tue, 5 Apr 2022 09:50:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 61627C385A1; Tue, 5 Apr 2022 09:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649152210; bh=2lkpK1T+wf6Ao5AxessnGv/w2OS1dIUjk/FEv6B0iUY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aysHmXTUnDJf0Vm4ia3nAgfm0Dmlfpmu0PGrzLdEv+S9QvzTKsWYodZilhdZQX3FT v1khR+eqMGEwGyUaRDl+LBr0iisScTti39J6x7oUHcn6kCnLYbqnqN0s1jq5dH5P7v FBA90MQZE0onOISAmT3UZCu62kpZDjZ7OXwYSByw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Richard Haines , Paul Moore , Sasha Levin , Demi Marie Obenour Subject: [PATCH 5.15 694/913] selinux: allow FIOCLEX and FIONCLEX with policy capability Date: Tue, 5 Apr 2022 09:29:16 +0200 Message-Id: <20220405070400.637793824@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: Richard Haines [ Upstream commit 65881e1db4e948614d9eb195b8e1197339822949 ] 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 [PM: subject line tweak] Signed-off-by: Paul Moore Signed-off-by: Sasha Levin --- security/selinux/hooks.c | 6 ++++++ security/selinux/include/policycap.h | 1 + security/selinux/include/policycap_names.h | 3 ++- security/selinux/include/security.h | 7 +++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c index 9a89e456d378..9ce029b2f226 100644 --- a/security/selinux/hooks.c +++ b/security/selinux/hooks.c @@ -3795,6 +3795,12 @@ static int selinux_file_ioctl(struct file *file, unsigned int cmd, CAP_OPT_NONE, true); break; + case FIOCLEX: + case FIONCLEX: + if (!selinux_policycap_ioctl_skip_cloexec()) + error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd); + break; + /* default case assumes that the command will go * to the file's ioctl() function. */ diff --git a/security/selinux/include/policycap.h b/security/selinux/include/policycap.h index 2ec038efbb03..a9e572ca4fd9 100644 --- a/security/selinux/include/policycap.h +++ b/security/selinux/include/policycap.h @@ -11,6 +11,7 @@ enum { POLICYDB_CAPABILITY_CGROUPSECLABEL, POLICYDB_CAPABILITY_NNP_NOSUID_TRANSITION, POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS, + POLICYDB_CAPABILITY_IOCTL_SKIP_CLOEXEC, __POLICYDB_CAPABILITY_MAX }; #define POLICYDB_CAPABILITY_MAX (__POLICYDB_CAPABILITY_MAX - 1) diff --git a/security/selinux/include/policycap_names.h b/security/selinux/include/policycap_names.h index b89289f092c9..ebd64afe1def 100644 --- a/security/selinux/include/policycap_names.h +++ b/security/selinux/include/policycap_names.h @@ -12,7 +12,8 @@ const char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = { "always_check_network", "cgroup_seclabel", "nnp_nosuid_transition", - "genfs_seclabel_symlinks" + "genfs_seclabel_symlinks", + "ioctl_skip_cloexec" }; #endif /* _SELINUX_POLICYCAP_NAMES_H_ */ diff --git a/security/selinux/include/security.h b/security/selinux/include/security.h index ac0ece01305a..c0d966020ebd 100644 --- a/security/selinux/include/security.h +++ b/security/selinux/include/security.h @@ -219,6 +219,13 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void) return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS]); } +static inline bool selinux_policycap_ioctl_skip_cloexec(void) +{ + struct selinux_state *state = &selinux_state; + + return READ_ONCE(state->policycap[POLICYDB_CAPABILITY_IOCTL_SKIP_CLOEXEC]); +} + struct selinux_policy_convert_data; struct selinux_load_state { -- 2.34.1