Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp671747rdb; Thu, 30 Nov 2023 15:21:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFLGedcF9U8aLcpBxURNrRvdu8xJaqKW6VxbEsBlWNI6JLwQ2RiMW1qD0L+Y8qQiUI+Tgrj X-Received: by 2002:a05:6e02:13eb:b0:35c:c47d:f58e with SMTP id w11-20020a056e0213eb00b0035cc47df58emr15638819ilj.5.1701386481012; Thu, 30 Nov 2023 15:21:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701386480; cv=none; d=google.com; s=arc-20160816; b=jBUcaSBeA76fW3IzqDQSIsTGlJ27OW0z3XX1GamiVVVdFubiATK149RRIxYjVsJQPy P0DN9MSXJ8By/Jy/+E5CmZkIF4NoRmeS287pRNnRaIum7CnQKE848YM8b1Og4kz5+ezi IIn5d8ItvbSHQjhQjL1pWPgnd5rwThdZhtvwMOHeprVgIp/UcVA4qnSbbi4ZEa/5/kXe 7H750xW00lU50RvXJq0E4oqpC8OHSRxSF5Uw4/TzZVf/bAGHikCsO2Ypwk1vsL4BqtEH T+GndWNEy0iibySxcpxdeqNAEivl/6MDcQ+4KocN18b/4qfnSq6pCzfoLK+ypdJUigr6 bg3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=qJ1SUuoC+/HZD/tv+wRzkc7OCvV4kDDki2NMV3IiTa4=; fh=2uEWnGGvEpdqFtUqKQh3Y6uaGNgTGNOI0L2cxn3xouc=; b=LD9OKMExxdzrLkaaVJyp7kTRefjKHef+gU6/SFjooLMaP4eeLq/hweir1RJKxE0MLL ZGvtDXWesNCjtkSZYn2RgRUitOh/4rdI2xQR8g2iri2kFDQB1yT/0r2NTPWRMHkauCqI owfPCab46EaboUDmFyyLHBmwRIH8dwuWPVkxOEabTXerqq53F309cMiNXyB6/j3hAd46 8mpwsM29EkpsMDJLakT2yguweuUBRJWX2mhHi4YyPD3bhwPyVQY/sCpStLKNdnTcMFiN XKwtcaX036x3LMOQ6Cv+0TyrK7CAyOP0tWnNv+24a15bLq9UEMN7stau5FcruaRzZvX6 zymg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-nfs+bounces-238-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-nfs+bounces-238-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s6-20020a656446000000b005c212bdc7d2si2135217pgv.611.2023.11.30.15.21.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 15:21:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs+bounces-238-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-nfs+bounces-238-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-nfs+bounces-238-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 3B38E281A6D for ; Thu, 30 Nov 2023 23:21:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D058359521; Thu, 30 Nov 2023 23:21:18 +0000 (UTC) X-Original-To: linux-nfs@vger.kernel.org Received: from frasgout12.his.huawei.com (frasgout12.his.huawei.com [14.137.139.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8FA7171E; Thu, 30 Nov 2023 15:21:08 -0800 (PST) Received: from mail.maildlp.com (unknown [172.18.186.51]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4ShBZx2clnz9xFr4; Fri, 1 Dec 2023 07:04:09 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.47]) by mail.maildlp.com (Postfix) with ESMTP id 7D90B14068D; Fri, 1 Dec 2023 07:21:00 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwAXU3OdGGllXRuuAQ--.2350S6; Fri, 01 Dec 2023 00:20:59 +0100 (CET) From: Roberto Sassu To: viro@zeniv.linux.org.uk, brauner@kernel.org, chuck.lever@oracle.com, jlayton@kernel.org, neilb@suse.de, kolga@netapp.com, Dai.Ngo@oracle.com, tom@talpey.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, dhowells@redhat.com, jarkko@kernel.org, stephen.smalley.work@gmail.com, eparis@parisplace.org, casey@schaufler-ca.com, mic@digikod.net Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-security-module@vger.kernel.org, linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, selinux@vger.kernel.org, Roberto Sassu Subject: [PATCH v7 23/23] integrity: Switch from rbtree to LSM-managed blob for integrity_iint_cache Date: Fri, 1 Dec 2023 00:19:48 +0100 Message-Id: <20231130231948.2545638-5-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130231948.2545638-1-roberto.sassu@huaweicloud.com> References: <20231130231948.2545638-1-roberto.sassu@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-nfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:LxC2BwAXU3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VW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x07UdfHUUUUUU= X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAgARBF1jj5MfaQABsC From: Roberto Sassu Before the security field of kernel objects could be shared among LSMs with the LSM stacking feature, IMA and EVM had to rely on an alternative storage of inode metadata. The association between inode metadata and inode is maintained through an rbtree. With the reservation mechanism offered by the LSM infrastructure, the rbtree is no longer necessary, as each LSM could reserve a space in the security blob for each inode. With the 'integrity' LSM removed, and with the 'ima' LSM taking its role, reserve space from the 'ima' LSM for a pointer to the integrity_iint_cache structure directly, rather than embedding the whole structure in the inode security blob, to minimize the changes and to avoid waste of memory. If IMA is disabled, EVM faces the same problems as before making it an LSM, metadata verification fails for new files due to not setting the IMA_NEW_FILE flag in ima_post_path_mknod(), and evm_verifyxattr() returns INTEGRITY_UNKNOWN since IMA didn't call integrity_inode_get(). The only difference caused to moving the integrity metadata management to the 'ima' LSM is the fact that EVM cannot take advantage of cached verification results, and has to do the verification again. However, this case should never happen, since the only public API available to all kernel components, evm_verifyxattr(), does not work if IMA is disabled. Introduce two primitives for getting and setting the pointer of integrity_iint_cache in the security blob, respectively integrity_inode_get_iint() and integrity_inode_set_iint(). This would make the code more understandable, as they directly replace rbtree operations. Locking is not needed, as access to inode metadata is not shared, it is per inode. Keep the blob size and the new primitives definition at the common level in security/integrity rather than moving them in IMA itself, so that EVM can still call integrity_inode_get() and integrity_iint_find() while IMA is disabled. Just add an extra check in integrity_inode_get() to return NULL if that is the case. Signed-off-by: Roberto Sassu --- security/integrity/iint.c | 70 ++++--------------------------- security/integrity/ima/ima_main.c | 1 + security/integrity/integrity.h | 20 ++++++++- 3 files changed, 29 insertions(+), 62 deletions(-) diff --git a/security/integrity/iint.c b/security/integrity/iint.c index c36054041b84..8fc9455dda11 100644 --- a/security/integrity/iint.c +++ b/security/integrity/iint.c @@ -14,56 +14,25 @@ #include #include #include -#include #include #include #include #include #include "integrity.h" -static struct rb_root integrity_iint_tree = RB_ROOT; -static DEFINE_RWLOCK(integrity_iint_lock); static struct kmem_cache *iint_cache __ro_after_init; struct dentry *integrity_dir; -/* - * __integrity_iint_find - return the iint associated with an inode - */ -static struct integrity_iint_cache *__integrity_iint_find(struct inode *inode) -{ - struct integrity_iint_cache *iint; - struct rb_node *n = integrity_iint_tree.rb_node; - - while (n) { - iint = rb_entry(n, struct integrity_iint_cache, rb_node); - - if (inode < iint->inode) - n = n->rb_left; - else if (inode > iint->inode) - n = n->rb_right; - else - return iint; - } - - return NULL; -} - /* * integrity_iint_find - return the iint associated with an inode */ struct integrity_iint_cache *integrity_iint_find(struct inode *inode) { - struct integrity_iint_cache *iint; - if (!IS_IMA(inode)) return NULL; - read_lock(&integrity_iint_lock); - iint = __integrity_iint_find(inode); - read_unlock(&integrity_iint_lock); - - return iint; + return integrity_inode_get_iint(inode); } #define IMA_MAX_NESTING (FILESYSTEM_MAX_STACK_DEPTH+1) @@ -123,9 +92,7 @@ static void iint_free(struct integrity_iint_cache *iint) */ struct integrity_iint_cache *integrity_inode_get(struct inode *inode) { - struct rb_node **p; - struct rb_node *node, *parent = NULL; - struct integrity_iint_cache *iint, *test_iint; + struct integrity_iint_cache *iint; /* * After removing the 'integrity' LSM, the 'ima' LSM calls @@ -144,31 +111,10 @@ struct integrity_iint_cache *integrity_inode_get(struct inode *inode) iint_init_always(iint, inode); - write_lock(&integrity_iint_lock); - - p = &integrity_iint_tree.rb_node; - while (*p) { - parent = *p; - test_iint = rb_entry(parent, struct integrity_iint_cache, - rb_node); - if (inode < test_iint->inode) { - p = &(*p)->rb_left; - } else if (inode > test_iint->inode) { - p = &(*p)->rb_right; - } else { - write_unlock(&integrity_iint_lock); - kmem_cache_free(iint_cache, iint); - return test_iint; - } - } - iint->inode = inode; - node = &iint->rb_node; inode->i_flags |= S_IMA; - rb_link_node(node, parent, p); - rb_insert_color(node, &integrity_iint_tree); + integrity_inode_set_iint(inode, iint); - write_unlock(&integrity_iint_lock); return iint; } @@ -185,10 +131,8 @@ void integrity_inode_free(struct inode *inode) if (!IS_IMA(inode)) return; - write_lock(&integrity_iint_lock); - iint = __integrity_iint_find(inode); - rb_erase(&iint->rb_node, &integrity_iint_tree); - write_unlock(&integrity_iint_lock); + iint = integrity_iint_find(inode); + integrity_inode_set_iint(inode, NULL); iint_free(iint); } @@ -212,6 +156,10 @@ int __init integrity_iintcache_init(void) return 0; } +struct lsm_blob_sizes integrity_blob_sizes __ro_after_init = { + .lbs_inode = sizeof(struct integrity_iint_cache *), +}; + /* * integrity_kernel_read - read data from the file * diff --git a/security/integrity/ima/ima_main.c b/security/integrity/ima/ima_main.c index 3f59cce3fa02..52b4a3bba45a 100644 --- a/security/integrity/ima/ima_main.c +++ b/security/integrity/ima/ima_main.c @@ -1162,6 +1162,7 @@ DEFINE_LSM(ima) = { .name = "ima", .init = init_ima_lsm, .order = LSM_ORDER_LAST, + .blobs = &integrity_blob_sizes, }; late_initcall(init_ima); /* Start IMA after the TPM is available */ diff --git a/security/integrity/integrity.h b/security/integrity/integrity.h index 26d3b08dca1c..2fb35c67d64d 100644 --- a/security/integrity/integrity.h +++ b/security/integrity/integrity.h @@ -158,7 +158,6 @@ struct ima_file_id { /* integrity data associated with an inode */ struct integrity_iint_cache { - struct rb_node rb_node; /* rooted in integrity_iint_tree */ struct mutex mutex; /* protects: version, flags, digest */ struct inode *inode; /* back pointer to inode in question */ u64 version; /* track inode changes */ @@ -194,6 +193,25 @@ int integrity_kernel_read(struct file *file, loff_t offset, #define INTEGRITY_KEYRING_MAX 4 extern struct dentry *integrity_dir; +extern struct lsm_blob_sizes integrity_blob_sizes; + +static inline struct integrity_iint_cache * +integrity_inode_get_iint(const struct inode *inode) +{ + struct integrity_iint_cache **iint_sec; + + iint_sec = inode->i_security + integrity_blob_sizes.lbs_inode; + return *iint_sec; +} + +static inline void integrity_inode_set_iint(const struct inode *inode, + struct integrity_iint_cache *iint) +{ + struct integrity_iint_cache **iint_sec; + + iint_sec = inode->i_security + integrity_blob_sizes.lbs_inode; + *iint_sec = iint; +} struct modsig; -- 2.34.1