From patchwork Fri Oct 27 08:35:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 158844 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp451916vqb; Fri, 27 Oct 2023 01:42:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFPhKeIT3aBwikaVhaE/8W41L+YdUUeclqyMhNqAHgPSyFHEs0GFLjwcXpwb7YUxleQ+Elk X-Received: by 2002:a25:ca0d:0:b0:d9a:5ab4:9403 with SMTP id a13-20020a25ca0d000000b00d9a5ab49403mr2033137ybg.23.1698396143512; Fri, 27 Oct 2023 01:42:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698396143; cv=none; d=google.com; s=arc-20160816; b=KOTME/bFZdNin4KeygrIUCCItasU+7Fc+ksm2s0AAzmBILSbzg4RYbZAy/4WT0Q18D 5jib0gPre+f0hujQEiKJSxG2V911jVlNp2Q8OHIjl6Djkr1Xwt5Kn61C/SqHZLd6Ybls y1O/fhsMWXjCc1JdscZJuPw7a+rsz9eMDDglKvXNxp2uMvPJQwSNinoseAURdnD5UdAa luyC1S19L/Md2PVkAQTpTddPQGGSjMBD3INcw0vx3rV/v728uuVBlHueSAYil5oOOJco VlfJ6gz1hq1hEq9/ovq95GgzM77BqIqL4DeJUKAtOE/9bM506eOLbIRab383u+fd8DrC 6N4w== 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; bh=knoJMz0B5Uavg2uZpMl4f9fTbOPTNypqOu5ND01rhnY=; fh=2uEWnGGvEpdqFtUqKQh3Y6uaGNgTGNOI0L2cxn3xouc=; b=pIAHNn9nm1a3OFALIx2D1FBvQSNh2coHMtF8XgcGoITvywfMTHS+iB2l9A7GHTlcrp UWldCy8PrQ0rkddF8vXSP3ytmhrMDXRI+7b4Szp3Dm9jpf+2JJIXohJ5PbC1Y3MLmwwp CyzxxR0ID330TBlKtEPNGBXLYswOg8gUKd57rZFly7jd4O0Pss/7iJEZtQ6QjPKRrImu m7Ps6CdLq0or/KRoBPziOvl0Vy3VvK/k+qOEFE25dh3iWNxCnDVkSyUCWKo/WqtW+W7M UoAJAaIZT9wQOF+vkA4zqdhQ8eqX0ba0CTTeTyn4EpaBULBdGjfV8leQWAw8nFHm5CNO Z3kw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id r3-20020a258283000000b00da0c49a5fdesi1696992ybk.688.2023.10.27.01.42.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 01:42:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id DF75D826ECB1; Fri, 27 Oct 2023 01:42:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345724AbjJ0Il4 (ORCPT + 26 others); Fri, 27 Oct 2023 04:41:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345613AbjJ0Il2 (ORCPT ); Fri, 27 Oct 2023 04:41:28 -0400 Received: from frasgout11.his.huawei.com (frasgout11.his.huawei.com [14.137.139.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A8F311FC9; Fri, 27 Oct 2023 01:40:47 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4SGwlD1V08z9xqdL; Fri, 27 Oct 2023 16:27:36 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwCX8JGqdjtlDvIBAw--.29710S17; Fri, 27 Oct 2023 09:40:18 +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 v4 15/23] security: Introduce inode_post_create_tmpfile hook Date: Fri, 27 Oct 2023 10:35:50 +0200 Message-Id: <20231027083558.484911-16-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231027083558.484911-1-roberto.sassu@huaweicloud.com> References: <20231027083558.484911-1-roberto.sassu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: LxC2BwCX8JGqdjtlDvIBAw--.29710S17 X-Coremail-Antispam: 1UD129KBjvJXoWxWFyUZF4fKw43AFyxJry3CFg_yoWrGr4DpF WxK3W5Gws5XFy7WryvyF4Uuw1SgayYgrWUJrZ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op: Reflected X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 27 Oct 2023 01:42:18 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780897434349520856 X-GMAIL-MSGID: 1780897434349520856 From: Roberto Sassu In preparation for moving IMA and EVM to the LSM infrastructure, introduce the inode_post_create_tmpfile hook. As temp files can be made persistent, treat new temp files like other new files, so that the file hash is calculated and stored in the security xattr. LSMs could also take some action after temp files have been created. The new hook cannot return an error and cannot cause the operation to be canceled. Signed-off-by: Roberto Sassu --- fs/namei.c | 1 + include/linux/lsm_hook_defs.h | 2 ++ include/linux/security.h | 6 ++++++ security/security.c | 15 +++++++++++++++ 4 files changed, 24 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index 955a03aa51b7..ef08053185a2 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3703,6 +3703,7 @@ static int vfs_tmpfile(struct mnt_idmap *idmap, inode->i_state |= I_LINKABLE; spin_unlock(&inode->i_lock); } + security_inode_post_create_tmpfile(idmap, inode); ima_post_create_tmpfile(idmap, inode); return 0; } diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 0163c781f950..945ed6078890 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -121,6 +121,8 @@ LSM_HOOK(int, 0, inode_init_security_anon, struct inode *inode, const struct qstr *name, const struct inode *context_inode) LSM_HOOK(int, 0, inode_create, struct inode *dir, struct dentry *dentry, umode_t mode) +LSM_HOOK(void, LSM_RET_VOID, inode_post_create_tmpfile, struct mnt_idmap *idmap, + struct inode *inode) LSM_HOOK(int, 0, inode_link, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) LSM_HOOK(int, 0, inode_unlink, struct inode *dir, struct dentry *dentry) diff --git a/include/linux/security.h b/include/linux/security.h index 9673a7f45bf0..fbc57f2fa158 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -344,6 +344,8 @@ int security_inode_init_security_anon(struct inode *inode, const struct qstr *name, const struct inode *context_inode); int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode); +void security_inode_post_create_tmpfile(struct mnt_idmap *idmap, + struct inode *inode); int security_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry); int security_inode_unlink(struct inode *dir, struct dentry *dentry); @@ -809,6 +811,10 @@ static inline int security_inode_create(struct inode *dir, return 0; } +static inline void +security_inode_post_create_tmpfile(struct mnt_idmap *idmap, struct inode *inode) +{ } + static inline int security_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) diff --git a/security/security.c b/security/security.c index 35e5b5d901ac..fcd16e853c42 100644 --- a/security/security.c +++ b/security/security.c @@ -2006,6 +2006,21 @@ int security_inode_create(struct inode *dir, struct dentry *dentry, } EXPORT_SYMBOL_GPL(security_inode_create); +/** + * security_inode_post_create_tmpfile() - Update inode security field after creation of tmpfile + * @idmap: idmap of the mount + * @inode: inode of the new tmpfile + * + * Update inode security field after a tmpfile has been created. + */ +void security_inode_post_create_tmpfile(struct mnt_idmap *idmap, + struct inode *inode) +{ + if (unlikely(IS_PRIVATE(inode))) + return; + call_void_hook(inode_post_create_tmpfile, idmap, inode); +} + /** * security_inode_link() - Check if creating a hard link is allowed * @old_dentry: existing file