Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp6138510rdb; Thu, 14 Dec 2023 09:13:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IF0P1vUIeupcpnH9jHpCVRUZIBPC7WuiP2dBBnpH+67Xzc3i7b2259+5oEM3ceOlHEPLnBN X-Received: by 2002:a05:622a:13c8:b0:418:af7:e00a with SMTP id p8-20020a05622a13c800b004180af7e00amr14416256qtk.21.1702574018869; Thu, 14 Dec 2023 09:13:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702574018; cv=none; d=google.com; s=arc-20160816; b=UPB6FhQTUYTqReh6vwZn6ArgK2RKkENtFyEHQfej7xiPKgqfTtme4q7bK72wXv1Rp7 fwsqA24tc53pQ92F5Siah1HLoRknLmi2jABipAjG/XfIMS/H/ddzcdJK3ZHfNnP3LQGq WJqcblFz5iV/irAkZE8ZY8KuLb8/V7DhN+l6cokucEELkmGBYhYHslVqaNgw4bgUyTnK hTtlaee+PI5MldDJ7kIgFjZhBIG3gyAccsZc8e2jGq1GuCZSfv2DzuKyjb7wN2wIIID5 QrhNpi0XKBdnzjeH8oSA2oxb3m0Pl09KrmMoGNlYkXSsWINVxPkgsnqTt3YMD6sU1rR4 lmYw== 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=AOzjSWH4AOiCMGzl8Vfp3QV4LQ4b5swr3fyP1oESFoY=; fh=xqNCamZli/vIZcnQJdiAEaw/AGWtg7mtli5wTijKSX4=; b=GUlVi94TZG8/qXdepbG3YJMx3lCXmt480Dm+yZaNAy13PfUtYc7bB/apWnka4+hICS jggbW3W3W4kUhUJraM71mVMCDzRXYVxrD/9WgpqLMrfde4Iy3XgHNOALBmgafwBfTqPR w0g130/UQU1Uc695Pbq3QL2pbUJ6TZEy9L5qrnyns2gzQBcfWqcCHhX4tDBj6QpCBsAn 7gB6kDtjfYwQ7H9qGN4f8HDI+rTil6Qv6G23OKoUMpLNRAF3ubuvH3chZ8XmO1kwbgQ3 6QYeJcAXyN1fwJn7mpFam+rh0FbmdDh+UUOOWoz+ZQ7MPFP4vfYe5mr1YfEhtipgmGGx 9DHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-nfs+bounces-598-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-nfs+bounces-598-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id bq25-20020a05622a1c1900b004236e9c2deasi15937232qtb.114.2023.12.14.09.13.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 09:13:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-nfs+bounces-598-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-nfs+bounces-598-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-nfs+bounces-598-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 904EE1C20E22 for ; Thu, 14 Dec 2023 17:13:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C3886470A; Thu, 14 Dec 2023 17:13:37 +0000 (UTC) X-Original-To: linux-nfs@vger.kernel.org Received: from frasgout13.his.huawei.com (frasgout13.his.huawei.com [14.137.139.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B90191AD; Thu, 14 Dec 2023 09:13:32 -0800 (PST) Received: from mail.maildlp.com (unknown [172.18.186.51]) by frasgout13.his.huawei.com (SkyGuard) with ESMTP id 4Srdqg5pTGz9y7Z8; Fri, 15 Dec 2023 00:59:27 +0800 (CST) Received: from mail02.huawei.com (unknown [7.182.16.27]) by mail.maildlp.com (Postfix) with ESMTP id 893DC14064E; Fri, 15 Dec 2023 01:13:19 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP2 (Coremail) with SMTP id GxC2BwAHuGFtN3tlDa2DAg--.53313S6; Thu, 14 Dec 2023 18:13: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, shuah@kernel.org, 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, linux-kselftest@vger.kernel.org, Roberto Sassu Subject: [PATCH v8 14/24] security: Introduce path_post_mknod hook Date: Thu, 14 Dec 2023 18:08:24 +0100 Message-Id: <20231214170834.3324559-15-roberto.sassu@huaweicloud.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231214170834.3324559-1-roberto.sassu@huaweicloud.com> References: <20231214170834.3324559-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:GxC2BwAHuGFtN3tlDa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1UYxBIdaVFxhVjvjDU0xZFpf9x07UdfHUUUUUU= X-CM-SenderInfo: purev21wro2thvvxqx5xdzvxpfor3voofrz/1tbiAQALBF1jj5envAAGs1 From: Roberto Sassu In preparation for moving IMA and EVM to the LSM infrastructure, introduce the path_post_mknod hook. IMA-appraisal requires all existing files in policy to have a file hash/signature stored in security.ima. An exception is made for empty files created by mknod, by tagging them as new files. LSMs could also take some action after files are created. The new hook cannot return an error and cannot cause the operation to be reverted. Signed-off-by: Roberto Sassu Acked-by: Casey Schaufler Reviewed-by: Mimi Zohar --- fs/namei.c | 5 +++++ include/linux/lsm_hook_defs.h | 2 ++ include/linux/security.h | 5 +++++ security/security.c | 14 ++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index fb93d3e13df6..b7f433720b1e 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -4047,6 +4047,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, dentry); 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 175ca00a6b1d..ee5ab180a312 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -94,6 +94,8 @@ 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, + struct dentry *dentry) 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 4c3585e3dcb4..5f595135c8f2 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -1884,6 +1884,7 @@ 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, struct dentry *dentry); int security_path_truncate(const struct path *path); int security_path_symlink(const struct path *dir, struct dentry *dentry, const char *old_name); @@ -1918,6 +1919,10 @@ 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, + struct dentry *dentry) +{ } + static inline int security_path_truncate(const struct path *path) { return 0; diff --git a/security/security.c b/security/security.c index 9aa072ca5a19..b0203c488d17 100644 --- a/security/security.c +++ b/security/security.c @@ -1800,6 +1800,20 @@ 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 + * @dentry: new file + * + * Update inode security field after a file has been created. + */ +void security_path_post_mknod(struct mnt_idmap *idmap, struct dentry *dentry) +{ + if (unlikely(IS_PRIVATE(d_backing_inode(dentry)))) + return; + call_void_hook(path_post_mknod, idmap, dentry); +} + /** * security_path_mkdir() - Check if creating a new directory is allowed * @dir: parent directory -- 2.34.1