From patchwork Wed Mar 6 23:34:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fan Wu X-Patchwork-Id: 13584831 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 66E4A1CABF; Wed, 6 Mar 2024 23:34:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709768096; cv=none; b=NGtDMlj46xbbT6604QDgSkesYn/ty6bgnevRsscjHwics6CkQPr5Tse2aPdttjbXBCVbRAtuXkfA5B4chWJ1xcpDYmqoc95llAiVyjEZktcqnolEhkJu41/QlRWc+FCBpNJdEiXSZxk03JEnEP9d7QarQx3re4JfQu/tvGivJSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709768096; c=relaxed/simple; bh=L25Oy6FnDnOXDI+S90VkDgc9hM+j0sW2Jeamc5OQnVc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=AAwF5UOAdR0ICm46HWCtPW4k9aN+8jQF0kaE4LlPvzQNqoL/QQhOFdo+pIWWSplmWg8HJHNyOQsJmqSuJltIYvjOcoLqdUlqqDYx5f7FOnMQOUVcFncs7wBvMB1B2oHKb0z1PN3/uIMNgTid0hTkmVRLhsx580dO0MNcT2mM+uc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=jl4o6WFQ; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="jl4o6WFQ" Received: by linux.microsoft.com (Postfix, from userid 1052) id 36DF920B74C9; Wed, 6 Mar 2024 15:34:52 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 36DF920B74C9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1709768092; bh=vGLfT6QYN/cpclyLFsjJynIUa6xGDoXeixbbbyzhDVE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jl4o6WFQtfPGwqItIGSIPifBUzkx43krp6X9PJGun5dWClgxJn31rMgeaOD8j4r6v WcurnMGCjWnE3xTGoIUl92A7tWxgdDA/DK+T6MlC3T3Y3qvswDpsCHp26ES6Dahe/r ACU/ia2WjZI8qriS8CCjFqAnecw0Zw1FzZzzAYyc= From: Fan Wu To: corbet@lwn.net, zohar@linux.ibm.com, jmorris@namei.org, serge@hallyn.com, tytso@mit.edu, ebiggers@kernel.org, axboe@kernel.dk, agk@redhat.com, snitzer@kernel.org, eparis@redhat.com, paul@paul-moore.com Cc: linux-doc@vger.kernel.org, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fscrypt@vger.kernel.org, linux-block@vger.kernel.org, dm-devel@lists.linux.dev, audit@vger.kernel.org, linux-kernel@vger.kernel.org, Fan Wu Subject: [RFC PATCH v14 05/19] initramfs|security: Add a security hook to do_populate_rootfs() Date: Wed, 6 Mar 2024 15:34:30 -0800 Message-Id: <1709768084-22539-6-git-send-email-wufan@linux.microsoft.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1709768084-22539-1-git-send-email-wufan@linux.microsoft.com> References: <1709768084-22539-1-git-send-email-wufan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: This patch introduces a new hook to notify security system that the content of initramfs has been unpacked into the rootfs. Upon receiving this notification, the security system can activate a policy to allow only files that originated from the initramfs to execute or load into kernel during the early stages of booting. This approach is crucial for minimizing the attack surface by ensuring that only trusted files from the initramfs are operational in the critical boot phase. Signed-off-by: Fan Wu --- v1-v11: + Not present v12: + Introduced v13: + Rename the hook name to initramfs_populated() v14: + No changes --- include/linux/lsm_hook_defs.h | 2 ++ include/linux/security.h | 8 ++++++++ init/initramfs.c | 3 +++ security/security.c | 10 ++++++++++ 4 files changed, 23 insertions(+) diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h index 76458b6d53da..e0f50789a18f 100644 --- a/include/linux/lsm_hook_defs.h +++ b/include/linux/lsm_hook_defs.h @@ -425,3 +425,5 @@ LSM_HOOK(int, 0, uring_override_creds, const struct cred *new) LSM_HOOK(int, 0, uring_sqpoll, void) LSM_HOOK(int, 0, uring_cmd, struct io_uring_cmd *ioucmd) #endif /* CONFIG_IO_URING */ + +LSM_HOOK(void, LSM_RET_VOID, initramfs_populated, void) diff --git a/include/linux/security.h b/include/linux/security.h index d0eb20f90b26..619e17e59532 100644 --- a/include/linux/security.h +++ b/include/linux/security.h @@ -2167,4 +2167,12 @@ static inline int security_uring_cmd(struct io_uring_cmd *ioucmd) #endif /* CONFIG_SECURITY */ #endif /* CONFIG_IO_URING */ +#ifdef CONFIG_SECURITY +extern void security_initramfs_populated(void); +#else +static inline void security_initramfs_populated(void) +{ +} +#endif /* CONFIG_SECURITY */ + #endif /* ! __LINUX_SECURITY_H */ diff --git a/init/initramfs.c b/init/initramfs.c index 76deb48c38cb..140619a583ff 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -18,6 +18,7 @@ #include #include #include +#include static __initdata bool csum_present; static __initdata u32 io_csum; @@ -720,6 +721,8 @@ static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) #endif } + security_initramfs_populated(); + done: /* * If the initrd region is overlapped with crashkernel reserved region, diff --git a/security/security.c b/security/security.c index f168bc30a60d..26c28db211fd 100644 --- a/security/security.c +++ b/security/security.c @@ -5619,3 +5619,13 @@ int security_uring_cmd(struct io_uring_cmd *ioucmd) return call_int_hook(uring_cmd, 0, ioucmd); } #endif /* CONFIG_IO_URING */ + +/** + * security_initramfs_populated() - Notify LSMs that initramfs has been loaded + * + * Tells the LSMs the initramfs has been unpacked into the rootfs. + */ +void security_initramfs_populated(void) +{ + call_void_hook(initramfs_populated); +}