Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1264874rwn; Thu, 15 Sep 2022 12:58:15 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4pvJybbndj+/xQ3KV+QSBHVi9CUTcizz53fn1vVctGlRQZTjadHa3XhaTPXe8+bOGALAYJ X-Received: by 2002:a17:902:eb8c:b0:178:1762:eb0e with SMTP id q12-20020a170902eb8c00b001781762eb0emr1041373plg.125.1663271895251; Thu, 15 Sep 2022 12:58:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663271895; cv=none; d=google.com; s=arc-20160816; b=MxEW771y3juZOxvTHEoVLF8sbsKvUjpw5MHmZtDN6d42KO9iAVglJSTmdd7Lbft/tV +tkkrji6n3q7qtzQsUTdaZ5VyWdJQMvSrOiMZF5D7XNGeoftaQ5DVoKivrt5rhPc+NEN QgRxJHGHEfKGCR1Rbv0hNSQXAcmRe/Ti1iu99SD3OZ6tXiax2EgNxoSDgUMJ7c4aGuSn KziqY3WxdLhgcJ56v87nr+i1TdMFFK0NHEyY0wHzhtM6G/oYelnf6j/vzuesFRe6/V+N BYQsdpl6i8eMECwqPmNY6/MVSFnOqj3vn+qgpBuMiEzFgZedY+yWwjvi6LKfsiQTmH/7 GFWQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4WyQ/ZL524QAngfXmtC12dttd7WkDXMegYh+1r0hMIw=; b=oBedcfCt37m8HfOprH17VSMctpRouzRaPNAxO1vn4wUycC5pxIHfVUF7XU/72stehN Hpa7k8r7P/qgbSIfcoepQDV1eAf9h6BIF1eOdombTcNz8EnN0/v9B7z9imTKKitGu37X W4pN2pIFTqnBlfn+eqvSS926CnGQbDrKHmc+XliecwkT6GCOSJdf09KgOYB9l58Fq/iC uFWzuTjGZxyKx7m/ZHWcWfX8XIr4CRZV5njCJ00X4mBUzIcVQhYJUhFektO85y7TMJQh 5yKr+vWaPqg4sKWNPzaiapAocUgTFBbI7Bm7Zh/iepwluHRJz6fzuWGEKqliBvFR9NGb jByg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=fKhsGseW; 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=ibm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j17-20020a637a51000000b0041bc27b1bd5si1010026pgn.462.2022.09.15.12.58.02; Thu, 15 Sep 2022 12:58:15 -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=@ibm.com header.s=pp1 header.b=fKhsGseW; 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=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbiIOTdk (ORCPT + 99 others); Thu, 15 Sep 2022 15:33:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229515AbiIOTdS (ORCPT ); Thu, 15 Sep 2022 15:33:18 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54B859D13D; Thu, 15 Sep 2022 12:33:09 -0700 (PDT) Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 28FJVFb8001212; Thu, 15 Sep 2022 19:32:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=4WyQ/ZL524QAngfXmtC12dttd7WkDXMegYh+1r0hMIw=; b=fKhsGseW75V3FUgkeZCyPYFhrWpFCK7DZMyLDHNvd8ihsgpnKxYWZUvMuqpewUQOkXms nIDJYzKO+D574pZAsFRhfXk10vP0dFE4YVqBUhg4Afoakzyz46Ioj0YuHMt2wak7WYiN jLpSi5kx59nlWYj+o+zr4V4y52EMm6lnFFQJWp9AHSc0wEH+NsY/uY6UEnNiigo4bHgO uDeJFQfFAiotC6fdxWRvRfguYIzoUlhaVHJX3+nv9Gomdbp65BtWJGWOKwVaiL+kllUd 5Qts0mHwf6KH9TjKl1G83n1D8R3NmSo++2yzO2QxX9cjwskkcrNjcw8YOeePu3/vs5u4 TA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3jma9eg279-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Sep 2022 19:32:40 +0000 Received: from m0098404.ppops.net (m0098404.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 28FJVxV6009804; Thu, 15 Sep 2022 19:32:39 GMT Received: from ppma01wdc.us.ibm.com (fd.55.37a9.ip4.static.sl-reverse.com [169.55.85.253]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3jma9eg254-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Sep 2022 19:32:39 +0000 Received: from pps.filterd (ppma01wdc.us.ibm.com [127.0.0.1]) by ppma01wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 28FJJirH014193; Thu, 15 Sep 2022 19:32:37 GMT Received: from b01cxnp23033.gho.pok.ibm.com (b01cxnp23033.gho.pok.ibm.com [9.57.198.28]) by ppma01wdc.us.ibm.com with ESMTP id 3jm91m0d7h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Sep 2022 19:32:37 +0000 Received: from b01ledav005.gho.pok.ibm.com (b01ledav005.gho.pok.ibm.com [9.57.199.110]) by b01cxnp23033.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 28FJWaRs4653626 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 15 Sep 2022 19:32:36 GMT Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AE67BAE063; Thu, 15 Sep 2022 19:32:36 +0000 (GMT) Received: from b01ledav005.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9180DAE062; Thu, 15 Sep 2022 19:32:36 +0000 (GMT) Received: from sbct-3.pok.ibm.com (unknown [9.47.158.153]) by b01ledav005.gho.pok.ibm.com (Postfix) with ESMTP; Thu, 15 Sep 2022 19:32:36 +0000 (GMT) From: Stefan Berger To: linux-integrity@vger.kernel.org Cc: zohar@linux.ibm.com, serge@hallyn.com, brauner@kernel.org, containers@lists.linux.dev, dmitry.kasatkin@gmail.com, ebiederm@xmission.com, krzysztof.struczynski@huawei.com, roberto.sassu@huawei.com, mpeters@redhat.com, lhinds@redhat.com, lsturman@redhat.com, puiterwi@redhat.com, jejb@linux.ibm.com, jamjoom@us.ibm.com, linux-kernel@vger.kernel.org, paul@paul-moore.com, rgb@redhat.com, linux-security-module@vger.kernel.org, jmorris@namei.org, jpenumak@redhat.com, Stefan Berger , Alexander Viro , linux-fsdevel@vger.kernel.org, Denis Semakin Subject: [PATCH v14 11/26] ima: Define mac_admin_ns_capable() as a wrapper for ns_capable() Date: Thu, 15 Sep 2022 15:32:06 -0400 Message-Id: <20220915193221.1728029-12-stefanb@linux.ibm.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220915193221.1728029-1-stefanb@linux.ibm.com> References: <20220915193221.1728029-1-stefanb@linux.ibm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: r7kruqUoMrzyczrf1pNl6CM-l5Zx6fZh X-Proofpoint-ORIG-GUID: lRXdhi_gdUUjUAWXVsBSgZjs-_hCFNMm X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-15_10,2022-09-14_04,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 phishscore=0 suspectscore=0 clxscore=1011 priorityscore=1501 adultscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 lowpriorityscore=0 impostorscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2209150119 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,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 Define mac_admin_ns_capable() as a wrapper for the combined ns_capable() checks on CAP_MAC_ADMIN and CAP_SYS_ADMIN in a user namespace. Return true on the check if either capability or both are available. Use mac_admin_ns_capable() in place of capable(SYS_ADMIN). This will allow an IMA namespace to read the policy with only CAP_MAC_ADMIN, which has less privileges than CAP_SYS_ADMIN. Since CAP_MAC_ADMIN is an additional capability added to an existing gate avoid auditing in case it is not set. Cc: Alexander Viro Cc: linux-fsdevel@vger.kernel.org Signed-off-by: Denis Semakin Signed-off-by: Stefan Berger --- v13: - implemented file_sb_user_ns(const struct file *); const is needed so it can be called with seq_file's 'const struct file *file' v11: - use ns_capable_noaudit for CAP_MAC_ADMIN to avoid auditing in this case --- include/linux/capability.h | 6 ++++++ include/linux/fs.h | 5 +++++ security/integrity/ima/ima.h | 6 ++++++ security/integrity/ima/ima_fs.c | 5 ++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/include/linux/capability.h b/include/linux/capability.h index 65efb74c3585..dc3e1230b365 100644 --- a/include/linux/capability.h +++ b/include/linux/capability.h @@ -270,6 +270,12 @@ static inline bool checkpoint_restore_ns_capable(struct user_namespace *ns) ns_capable(ns, CAP_SYS_ADMIN); } +static inline bool mac_admin_ns_capable(struct user_namespace *ns) +{ + return ns_capable_noaudit(ns, CAP_MAC_ADMIN) || + ns_capable(ns, CAP_SYS_ADMIN); +} + /* audit system wants to get cap info from files as well */ int get_vfs_caps_from_disk(struct user_namespace *mnt_userns, const struct dentry *dentry, diff --git a/include/linux/fs.h b/include/linux/fs.h index 9eced4cc286e..ec24368b669e 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2727,6 +2727,11 @@ static inline struct user_namespace *file_mnt_user_ns(struct file *file) return mnt_user_ns(file->f_path.mnt); } +static inline struct user_namespace *file_sb_user_ns(const struct file *file) +{ + return i_user_ns(file_inode(file)); +} + /** * is_idmapped_mnt - check whether a mount is mapped * @mnt: the mount to check diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h index 5bf7f080c2be..28a9842c566f 100644 --- a/security/integrity/ima/ima.h +++ b/security/integrity/ima/ima.h @@ -491,4 +491,10 @@ static inline int ima_filter_rule_match(u32 secid, u32 field, u32 op, #define POLICY_FILE_FLAGS S_IWUSR #endif /* CONFIG_IMA_READ_POLICY */ +static inline +struct user_namespace *ima_user_ns_from_file(const struct file *filp) +{ + return file_sb_user_ns(filp); +} + #endif /* __LINUX_IMA_H */ diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c index 89d3113ceda1..c41aa61b7393 100644 --- a/security/integrity/ima/ima_fs.c +++ b/security/integrity/ima/ima_fs.c @@ -377,6 +377,9 @@ static const struct seq_operations ima_policy_seqops = { */ static int ima_open_policy(struct inode *inode, struct file *filp) { +#ifdef CONFIG_IMA_READ_POLICY + struct user_namespace *user_ns = ima_user_ns_from_file(filp); +#endif struct ima_namespace *ns = &init_ima_ns; if (!(filp->f_flags & O_WRONLY)) { @@ -385,7 +388,7 @@ static int ima_open_policy(struct inode *inode, struct file *filp) #else if ((filp->f_flags & O_ACCMODE) != O_RDONLY) return -EACCES; - if (!capable(CAP_SYS_ADMIN)) + if (!mac_admin_ns_capable(user_ns)) return -EPERM; return seq_open(filp, &ima_policy_seqops); #endif -- 2.36.1