From patchwork Thu Aug 31 10:41:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roberto Sassu X-Patchwork-Id: 137356 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp542860vqu; Thu, 31 Aug 2023 15:43:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHkBUH5fUEdIxQsEZFgmluNtsMRpAAMcQki9xm/eL+1Kkxk+fSkQz3WFw3V0UBbsrmh3b8C X-Received: by 2002:a17:90a:a602:b0:267:faba:705 with SMTP id c2-20020a17090aa60200b00267faba0705mr704555pjq.10.1693521834531; Thu, 31 Aug 2023 15:43:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693521834; cv=none; d=google.com; s=arc-20160816; b=RwZ8nJZfnpC88iRvbai2zbTJo54iVNgsmVCPFyWjXPySGV/fWtNcFrmjRLG+7VaOQB nnoXss72vHFD32NZLMYevNk426MIE9MbQWBakaDAu5R2yv7LosaF63Ew9R4lJQt59tmc 8wleK/pfTPWDgbbrhuFTz71fBep87SVsP3s+/cn7sxMiMqdHb6Pj42evgsvFAr8joqOZ 0neo9nVwWpqbE/U58mk9G9Gs6PvTMNPXbhRcOkuk9B/VNPM0fqF4pzsgV9WXo0SV0c2t RmVmWZdZtyKuIVg5mQKQaEw6d8LKcNfsOz9q6dEZcVLZH03WqLGxAhMK1QEO7KmV7HV/ x/Gg== 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=xBSbJ06nRPp5TxIvNMdYqPLaFj4E15Kf9kiPZ8LR0LA=; fh=ZhQZvtUJr/vinStRe/fLjcmSrOCsze6H5x9tCaYQJfY=; b=Q9bwasXpbdZjM9XBNifEtVNTcel+Th0Q09F9DIFvpc/bNu20FJm5KS1x7wozZ15f2+ Sd0cW4lAYP1GMqzyf7+lqpa2tXBYxxfgY/0S/I+Hce8uF1Dm7ixWzIt9lIia/+bc4Con J96o7Q38oOeyBfbStavNr+lSViauWUijLgI++B2MPq2A2fRkyhyZ2RJa1dbu6mp2A/eT Jh9yeAZgu3E6fEf++N1Sn+4qKo0pe5gYRM9tPqOaH1SQT1t6yroHCQkYJsxy+aq1nL/a Bv2l4y53ZIfIfcVnBZldWrhkPS0tDQgHPt7nIq4DKnsCRuBZLWW6Z7lKyt7d7SM17tE4 2pug== ARC-Authentication-Results: i=1; mx.google.com; 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hi22-20020a17090b30d600b0026d46757b20si1872970pjb.149.2023.08.31.15.43.39; Thu, 31 Aug 2023 15:43:54 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345271AbjHaKrq (ORCPT + 99 others); Thu, 31 Aug 2023 06:47:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238639AbjHaKrm (ORCPT ); Thu, 31 Aug 2023 06:47:42 -0400 Received: from frasgout12.his.huawei.com (unknown [14.137.139.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A177BE57; Thu, 31 Aug 2023 03:47:03 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout12.his.huawei.com (SkyGuard) with ESMTP id 4RbyCC2FWgz9xtRp; Thu, 31 Aug 2023 18:32:07 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwBXC7t9bvBkiGfdAQ--.39787S19; Thu, 31 Aug 2023 11:45: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, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, dhowells@redhat.com, jarkko@kernel.org, stephen.smalley.work@gmail.com, eparis@parisplace.org, casey@schaufler-ca.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, selinux@vger.kernel.org, Roberto Sassu , Stefan Berger Subject: [PATCH v2 17/25] security: Introduce inode_post_create_tmpfile hook Date: Thu, 31 Aug 2023 12:41:28 +0200 Message-Id: <20230831104136.903180-18-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230831104136.903180-1-roberto.sassu@huaweicloud.com> References: <20230831104136.903180-1-roberto.sassu@huaweicloud.com> MIME-Version: 1.0 X-CM-TRANSID: LxC2BwBXC7t9bvBkiGfdAQ--.39787S19 X-Coremail-Antispam: 1UD129KBjvJXoWxGFy7tFWrtw18CFykKr17ZFb_yoWrGry5pF 4fK3W5Gws5XFy7Wr1kAF4Uuw1SgayagrWUJrZagwnIyFn7tr1ftF4Skr12kF1fJrW8W342 q3ZFkrZxGr17trJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBab4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVWxJr0_GcWl84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E 14v26F4UJVW0owAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I 80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFVCj c4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7MxAIw28Icx kI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2Iq xVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVW8ZVWrXwCIc40Y0x0EwIxGrwCI42 IY6xIIjxv20xvE14v26F1j6w1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVWxJr0_GcWlIxAI cVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2js IEc7CjxVAFwI0_Cr1j6rxdYxBIdaVFxhVjvjDU0xZFpf9x07UdfHUUUUUU= X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQAGBF1jj5Nc0wAAsu X-CFilter-Loop: Reflected X-Spam-Status: No, score=1.2 required=5.0 tests=BAYES_00,MAY_BE_FORGED, RCVD_IN_DNSWL_BLOCKED,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: * 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775786351245895841 X-GMAIL-MSGID: 1775786351245895841 From: Roberto Sassu In preparation for moving IMA and EVM to the LSM infrastructure, introduce the inode_post_create_tmpfile hook. Signed-off-by: Roberto Sassu Reviewed-by: Stefan Berger --- fs/namei.c | 1 + include/linux/lsm_hook_defs.h | 2 ++ include/linux/security.h | 8 ++++++++ security/security.c | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index c8c4ab26b52a..efed0e1e93f5 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3700,6 +3700,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, dir, file, mode); ima_post_create_tmpfile(idmap, dir, file, mode); return 0; } diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index b1634b5de98c..9ae573b83737 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 *dir, struct file *file, umode_t mode) 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 f210bd66e939..5f296761883f 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -338,6 +338,9 @@ 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 *dir, struct file *file, + umode_t mode); 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); @@ -788,6 +791,11 @@ 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 *dir, + struct file *file, umode_t mode) +{ } + 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 56c1c1e66fd1..e5acb11f6ebd 100644 --- a/security/security.c +++ b/security/security.c @@ -1920,6 +1920,24 @@ 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 + * @dir: the inode of the base directory + * @file: file descriptor of the new tmpfile + * @mode: the mode 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 *dir, + struct file *file, umode_t mode) +{ + if (unlikely(IS_PRIVATE(dir))) + return; + call_void_hook(inode_post_create_tmpfile, idmap, dir, file, mode); +} + /** * security_inode_link() - Check if creating a hard link is allowed * @old_dentry: existing file