Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1926632rdb; Tue, 5 Sep 2023 09:02:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IELPnAUuQTXbt7wRR4d8LweJ2uHh3oLNCRLNE32ChF0Qz3ueINfxSwUv9HNCoMw5yfHn/bv X-Received: by 2002:a05:6a20:3d0:b0:14c:ca25:3b5f with SMTP id 16-20020a056a2003d000b0014cca253b5fmr10431983pzu.46.1693929757760; Tue, 05 Sep 2023 09:02:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693929757; cv=none; d=google.com; s=arc-20160816; b=NCMSDrW/1ZxjrSMwlA2ocH503R1GBIBJjctxwQ75WVio9ZIbzoNFHf4L2DYQTD0My6 /lJT4E+tUUo3utPJH1Kb6M0xQmV9lL2Ih/X9tAEajvoJII0jckLc50dySIumcRURsLa2 xE0jryPPx2s6dER+w9XY7MrJbu1asQ8DPrK+26gi3Ngkj+NY8RDwu2f/pViayUqxa0gl 9u2lAfPcz1Jk/95ZOvi8IOJQQhNdlyW9h8pcpUio0pX4aEWdFHBoCiYrPWMoaKF0IgyA VuIP+4H93U0fSi7nMYiiJc2CGXI1DHzKS/dOPc8aNAxjzG0++6ENHLTMiPbnHpj4J+5w 0Ahg== 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=Qv+wLPRrwBy+t1+3a7QYJfXNwXaVWXY/hwOP5s9690I=; fh=k32FWR+OEbSGPhlKvyeJu1pW9b0VDX/rgvS8LbsdBHU=; b=Yt2YtoKwnyMHR/caFwNRx43IdGLLXFWe+lAXxCVlxRam8Ut9r9vCxqb0oyhr4wQFdY FZKJqlgBMpgj8NkKX2LRX/0RzI61nzGM/+mOqP90gN4TTGsomH1jZpi03T0R3NeMPwE6 QL5jdTQvjG5X9XQYtJrvoFk8DbKDSGIosYi5cqtesGpTBXn89CgkmzoxoLKDtJpDcuL5 RuHldwNTbLITdm0yXcxuTi3AXG9u5E9/1cyO93CmeNs9iAfo7voiNVSKYYqsqtmK48Xj M41vIycK/MVVPW+RFGH44N8aVnIMTUk0f6AMAk/tpuskbJikNQGcBK9MdfWZ8pO3HAZp E6UQ== 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 m1-20020a656a01000000b00565403cf948si10571592pgu.495.2023.09.05.09.02.35; Tue, 05 Sep 2023 09:02:37 -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 S1353117AbjIDNjD (ORCPT + 13 others); Mon, 4 Sep 2023 09:39:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353110AbjIDNjD (ORCPT ); Mon, 4 Sep 2023 09:39:03 -0400 Received: from frasgout13.his.huawei.com (unknown [14.137.139.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC87910F5; Mon, 4 Sep 2023 06:38:36 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.18.147.229]) by frasgout13.his.huawei.com (SkyGuard) with ESMTP id 4RfTsy5ztgz9xtn0; Mon, 4 Sep 2023 21:25:58 +0800 (CST) Received: from huaweicloud.com (unknown [10.204.63.22]) by APP1 (Coremail) with SMTP id LxC2BwDHerrf3PVkUqceAg--.16511S17; Mon, 04 Sep 2023 14:37:37 +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 15/25] security: Introduce file_pre_free_security hook Date: Mon, 4 Sep 2023 15:34:05 +0200 Message-Id: <20230904133415.1799503-16-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--.16511S17 X-Coremail-Antispam: 1UD129KBjvJXoWxJF1fWrW5WryxtF15Ar1DKFg_yoW5uw4kpr Z8t3W5GFW5GFy7WrnxCanrua4fK393KrWDWFZ5u34rtFnrJr95KrZxCF15CF15JrWkJry0 vw12grW3GryDArJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 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/1tbiAQAKBF1jj5OBcwABse 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 file_pre_free_security hook. It is useful for IMA to calculate the digest of the file content, just before a file descriptor is closed, and update the security.ima xattr with the new value. LSMs should use this hook instead of file_free_security, if they still need to access the opened file, before it is closed. The new hook cannot return an error and cannot cause the operation to be canceled. Signed-off-by: Roberto Sassu --- fs/file_table.c | 1 + include/linux/lsm_hook_defs.h | 1 + include/linux/security.h | 4 ++++ security/security.c | 11 +++++++++++ 4 files changed, 17 insertions(+) diff --git a/fs/file_table.c b/fs/file_table.c index fc7d677ff5ad..964e24120684 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -375,6 +375,7 @@ static void __fput(struct file *file) eventpoll_release(file); locks_remove_file(file); + security_file_pre_free(file); ima_file_free(file); if (unlikely(file->f_flags & FASYNC)) { if (file->f_op->fasync) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 60ed33f0c80d..797f51da3f7d 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -172,6 +172,7 @@ LSM_HOOK(int, 0, kernfs_init_security, struct kernfs_node *kn_dir, struct kernfs_node *kn) LSM_HOOK(int, 0, file_permission, struct file *file, int mask) LSM_HOOK(int, 0, file_alloc_security, struct file *file) +LSM_HOOK(void, LSM_RET_VOID, file_pre_free_security, struct file *file) LSM_HOOK(void, LSM_RET_VOID, file_free_security, struct file *file) LSM_HOOK(int, 0, file_ioctl, struct file *file, unsigned int cmd, unsigned long arg) diff --git a/include/linux/security.h b/include/linux/security.h index a0f16511c059..7871009d59ae 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -389,6 +389,7 @@ int security_kernfs_init_security(struct kernfs_node *kn_dir, struct kernfs_node *kn); int security_file_permission(struct file *file, int mask); int security_file_alloc(struct file *file); +void security_file_pre_free(struct file *file); void security_file_free(struct file *file); int security_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg); int security_mmap_file(struct file *file, unsigned long prot, @@ -985,6 +986,9 @@ static inline int security_file_alloc(struct file *file) return 0; } +static inline void security_file_pre_free(struct file *file) +{ } + static inline void security_file_free(struct file *file) { } diff --git a/security/security.c b/security/security.c index 3e0078b51e46..fbb58eeeea02 100644 --- a/security/security.c +++ b/security/security.c @@ -2626,6 +2626,17 @@ int security_file_alloc(struct file *file) return rc; } +/** + * security_file_pre_free() - Perform actions before closing a file descriptor + * @file: the file + * + * Perform actions before the file descriptor is closed and freed. + */ +void security_file_pre_free(struct file *file) +{ + call_void_hook(file_pre_free_security, file); +} + /** * security_file_free() - Free a file's LSM blob * @file: the file -- 2.34.1