Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1367191rdb; Mon, 4 Sep 2023 11:15:43 -0700 (PDT) X-Google-Smtp-Source: AGHT+IETP+z6uebJCMn8aPSibsKIZhOjJaqMjB4K+E+28awIkqpqj2CqLKFhTea6nGjYqSzcP8M8 X-Received: by 2002:a17:902:ea02:b0:1bd:ae9e:62ea with SMTP id s2-20020a170902ea0200b001bdae9e62eamr13423941plg.8.1693851343250; Mon, 04 Sep 2023 11:15:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693851343; cv=none; d=google.com; s=arc-20160816; b=PlEz1Uo+9WwYgN9mV6gLtEIuShH60ZAyKrkQSyKq7LvgppuvuLiorK2mZvqOT81eq/ P9s/qk65jndC0MNq7/d9NHL3PS0gB5ruSbHmBjgjWFjDm6jw9z66MbJHz5W0AKsv9BdP QGrogCTCSWbMzCy1SWX0xd1E/kq5L8EZpE8WQrF6U0jUn4rvPjaahuni78lkuPt07aqu 1B/UQGP0dPEtqrnbbGYAAoM6ZSp5bmNAcpX9eb5ohl3snNLDMzsT+/GnxeNtIfoi0Joj 0XdSNX5hYMCnhKL6ZXv/ZBmTwkS1FbtRN6pDqaXVHP5iWhTW+HCCreL0tWxTuojDM8w0 qlmA== 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=yNd6ju+9vT2V8m3GGFUsZS8yPlk0fhzAT+FkNM6tLqk=; fh=k32FWR+OEbSGPhlKvyeJu1pW9b0VDX/rgvS8LbsdBHU=; b=cecvd7tawrvAfv+rt48SIIy699rUNMYz5bJiHYU/czQYOUKuoXqVOy0Hdg3cNCkbgj BUv9+XB+ilqAoCeQvrpOxY8qQCjjvt03gdUxa5eBcSF/wIoI5yiVx99CLhTGKzTqDncz B1kNklv1hjjvVUIHqj1gLloIpiXb3DfwzwlmeW9HKyOZaAuWgdjo62PKvE/FF2QBsJVy jeKwJkR+xtCJLCd96ZFXPbN5++C6f6B8WLFIydkYJFm5cJLAylpXRGp2IXS++6KM8jV7 DSw9EYPujndxRW9p9opko9o9WdC+di2YlMf/bXdC1IEsOPG1v8wa+CjvimVz4+iHjinp Eqbg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f8-20020a170902684800b001c32fe6cdf9si3523124pln.386.2023.09.04.11.15.34; Mon, 04 Sep 2023 11:15:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-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-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353138AbjIDNjd (ORCPT + 43 others); Mon, 4 Sep 2023 09:39:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353129AbjIDNjc (ORCPT ); Mon, 4 Sep 2023 09:39:32 -0400 Received: from frasgout11.his.huawei.com (unknown [14.137.139.23]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C3BC1BCB; Mon, 4 Sep 2023 06:39:07 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.227]) by frasgout11.his.huawei.com (SkyGuard) with ESMTP id 4RfTt82552z9xqwq; Mon, 4 Sep 2023 21:26:08 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwDHerrf3PVkUqceAg--.16511S18; Mon, 04 Sep 2023 14:37:49 +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 Subject: [PATCH v3 16/25] security: Introduce path_post_mknod hook Date: Mon, 4 Sep 2023 15:34:06 +0200 Message-Id: <20230904133415.1799503-17-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230904133415.1799503-1-roberto.sassu@huaweicloud.com> References: <20230904133415.1799503-1-roberto.sassu@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: LxC2BwDHerrf3PVkUqceAg--.16511S18 X-Coremail-Antispam: 1UD129KBjvJXoWxWr4xWrW3GryfGFyUuF4xXrb_yoWrAr1Upa 18tFn3Kr4rGFyagr1kAFsrAa4SvrZ8u3y7JrZIgwnIyFnxtr1aqF4S9ryYkr93GrWqgryI q3W3tr43Gr4Utr7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUBvb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI8067AKxVWUAV Cq3wA2048vs2IY020Ec7CjxVAFwI0_Xr0E3s1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0 rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW8JVW5JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267 AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVAFwI0_Gr0_Cr1l84ACjcxK6I8E87Iv6xkF7I0E 14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I8CrV C2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE 7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwACI402YVCY1x02628vn2kIc2xKxwCF04k20x vY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E14v26r1j6r18MI8I 3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_GFv_WrylIxkGc2Ij64vIr41lIx AIcVC0I7IYx2IY67AKxVW8JVW5JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr1j6F4UJwCI 42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z2 80aVCY1x0267AKxVW8Jr0_Cr1UYxBIdaVFxhVjvjDU0xZFpf9x07UZo7tUUUUU= X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAgAKBF1jj4+BWQAAsq X-CFilter-Loop: Reflected X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, RDNS_DYNAMIC,SPF_HELO_NONE,SPF_NONE 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-nfs@vger.kernel.org From: Roberto Sassu In preparation for moving IMA and EVM to the LSM infrastructure, introduce the path_post_mknod hook. It is useful for IMA to let new empty files be subsequently opened for further modification. LSMs can benefit from this hook to update the inode state after a file has been successfully created. The new hook cannot return an error and cannot cause the operation to be canceled. Signed-off-by: Roberto Sassu --- fs/namei.c | 5 +++++ include/linux/lsm_hook_defs.h | 3 +++ include/linux/security.h | 9 +++++++++ security/security.c | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index 7dc4626859f0..c8c4ab26b52a 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4061,6 +4061,11 @@ static int do_mknodat(int dfd, struct filename *name, umode_t mode, dentry, mode, 0); break; } + + if (error) + goto out2; + + security_path_post_mknod(idmap, &path, dentry, mode_stripped, dev); out2: done_path_create(&path, dentry); if (retry_estale(error, lookup_flags)) { diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 797f51da3f7d..b1634b5de98c 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -93,6 +93,9 @@ LSM_HOOK(int, 0, path_mkdir, const struct path *dir, struct dentry *dentry, LSM_HOOK(int, 0, path_rmdir, const struct path *dir, struct dentry *dentry) LSM_HOOK(int, 0, path_mknod, const struct path *dir, struct dentry *dentry, umode_t mode, unsigned int dev) +LSM_HOOK(void, LSM_RET_VOID, path_post_mknod, struct mnt_idmap *idmap, + const struct path *dir, struct dentry *dentry, umode_t mode, + unsigned int dev) LSM_HOOK(int, 0, path_truncate, const struct path *path) LSM_HOOK(int, 0, path_symlink, const struct path *dir, struct dentry *dentry, const char *old_name) diff --git a/include/linux/security.h b/include/linux/security.h index 7871009d59ae..f210bd66e939 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -1842,6 +1842,9 @@ int security_path_mkdir(const struct path *dir, struct dentry *dentry, umode_t m int security_path_rmdir(const struct path *dir, struct dentry *dentry); int security_path_mknod(const struct path *dir, struct dentry *dentry, umode_t mode, unsigned int dev); +void security_path_post_mknod(struct mnt_idmap *idmap, const struct path *dir, + struct dentry *dentry, umode_t mode, + unsigned int dev); int security_path_truncate(const struct path *path); int security_path_symlink(const struct path *dir, struct dentry *dentry, const char *old_name); @@ -1876,6 +1879,12 @@ static inline int security_path_mknod(const struct path *dir, struct dentry *den return 0; } +static inline void security_path_post_mknod(struct mnt_idmap *idmap, + const struct path *dir, + struct dentry *dentry, umode_t mode, + unsigned int dev) +{ } + static inline int security_path_truncate(const struct path *path) { return 0; diff --git a/security/security.c b/security/security.c index fbb58eeeea02..78aeb24efedb 100644 --- a/security/security.c +++ b/security/security.c @@ -1702,6 +1702,25 @@ int security_path_mknod(const struct path *dir, struct dentry *dentry, } EXPORT_SYMBOL(security_path_mknod); +/** + * security_path_post_mknod() - Update inode security field after file creation + * @idmap: idmap of the mount + * @dir: parent directory + * @dentry: new file + * @mode: new file mode + * @dev: device number + * + * Update inode security field after a file has been created. + */ +void security_path_post_mknod(struct mnt_idmap *idmap, const struct path *dir, + struct dentry *dentry, umode_t mode, + unsigned int dev) +{ + if (unlikely(IS_PRIVATE(d_backing_inode(dir->dentry)))) + return; + call_void_hook(path_post_mknod, idmap, dir, dentry, mode, dev); +} + /** * security_path_mkdir() - Check if creating a new directory is allowed * @dir: parent directory -- 2.34.1