Skip to contentSkip to conversation

ima: Fall back to default kernel module signature verification

PATCH In review Coiby Xu 5 participants 42 messages Sep 28, 2025 – Nov 19, 2025

Subsystems (8)
lore
Share & tools
lore

Coiby Xu

author
#0 lore raw

PATCH 1/1 ima: Fall back to default kernel module signature verification

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

 security/integrity/ima/ima_appraise.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
security/integrity/ima/ima_appraise.c +19 -6
index f435eff4667f..fcc75dd1486f 100644
@@ -502,9 +502,10 @@ int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,
 	enum integrity_status status = INTEGRITY_UNKNOWN;
 	int rc = xattr_len;
 	bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
+	bool enforce_module_sig = iint->flags & IMA_DIGSIG_REQUIRED && func == MODULE_CHECK;
 
-	/* If not appraising a modsig, we need an xattr. */
-	if (!(inode->i_opflags & IOP_XATTR) && !try_modsig)
+	/* If not appraising a modsig or using default module verification, we need an xattr. */
+	if (!(inode->i_opflags & IOP_XATTR) && !try_modsig && !enforce_module_sig)
 		return INTEGRITY_UNKNOWN;
 
 	/*
@@ -517,8 +518,8 @@ int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,
 	if (is_bprm_creds_for_exec(func, file))
 		audit_msgno = AUDIT_INTEGRITY_USERSPACE;
 
-	/* If reading the xattr failed and there's no modsig, error out. */
-	if (rc <= 0 && !try_modsig) {
+	/* If reading the xattr failed and there's no modsig or module verification, error out. */
+	if (rc <= 0 && !try_modsig && !enforce_module_sig) {
 		if (rc && rc != -ENODATA)
 			goto out;
 
@@ -549,8 +550,8 @@ int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,
 	case INTEGRITY_UNKNOWN:
 		break;
 	case INTEGRITY_NOXATTRS:	/* No EVM protected xattrs. */
-		/* It's fine not to have xattrs when using a modsig. */
-		if (try_modsig)
+		/* Fine to not have xattrs when using a modsig or default module verification. */
+		if (try_modsig || enforce_module_sig)
 			break;
 		fallthrough;
 	case INTEGRITY_NOLABEL:		/* No security.evm xattr. */
@@ -580,6 +581,18 @@ int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,
 	     rc == -ENOKEY))
 		rc = modsig_verify(func, modsig, &status, &cause);
 
+	/* Fall back to default kernel module signature verification */
+	if (rc && enforce_module_sig) {
+		rc = 0;
+		set_module_sig_enforced();
+		/* CONFIG_MODULE_SIG may be disabled */
+		if (is_module_sig_enforced()) {
+			rc = 0;
+			status = INTEGRITY_PASS;
+			pr_debug("Fall back to default kernel module verification for %s\n", filename);
+		}
+	}
+
 out:
 	/*
 	 * File signatures on some filesystems can not be properly verified.

base-commit: cec1e6e5d1ab33403b809f79cd20d6aff124ccfe

Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

thanks,

Mimi

[1] [PATCH v4 0/3] initramfs: add support for xattrs in the initial ram disk
https://lore.kernel.org/linux-fsdevel/[email protected]/

maintainer
#2 lore raw
Quoted 60 lines · Mimi Zohar
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Quoted 5 lines

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

or perhaps not totally.

init_module_from_file() doesn't pass the flags variable to kernel_read_file(). You might want to consider defining a new kernel_read_file_id enumeration named READING_COMPRESSED_MODULE.

Mimi

Quoted 5 lines

[1] [PATCH v4 0/3] initramfs: add support for xattrs in the initial ram disk
https://lore.kernel.org/linux-fsdevel/[email protected]/

author
#3 lore raw
Quoted 32 lines · Mimi Zohar
Quoted 31 lines · Mimi Zohar
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

Hi Mimi,

Thanks for listing and framing the issues! Sorry, it took me a while to go through your feedback as I also had a 8-day holiday.

Quoted 6 lines
Quoted 6 lines

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

Since IMA only accesses a kernel module as a fd or struct file*, even if IMA hook is triggerd after kernel module is decompressed, IMA still doesn't know the default verification has passed or can't access the decompressed kernel buffer [2] to do the verification by itself.

[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/module/main.c?h=v6.17#n3689

Quoted 5 lines
Quoted 5 lines

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

Thanks you for pointing me to the work [1]. I'll take a more careful look at [1]. I think CPIO supporting xattr can be a long-term solution and also close a important security gap.

Quoted 4 lines
Quoted 4 lines

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

Yes, the issue is not limited to Power. And thanks for correcting me that Power arch specific IMA policy rules don't have this problem! Sorry I misread the code.

Quoted 15 lines
Quoted 9 lines

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Quoted 5 lines

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

Quoted 5 lines

or perhaps not totally.

init_module_from_file() doesn't pass the flags variable to kernel_read_file(). You might want to consider defining a new kernel_read_file_id enumeration named READING_COMPRESSED_MODULE.

Thanks for suggesting the solutions! I like the solution of CPIO supporting xattr but it seems it won't land in upstream soon. So I prefer the last approach. I've implemented one [3] by defining a new kernel_read_file_id enumeration, would you like me to post the patches to the mailing list directly?

[3] https://github.com/coiby/linux/tree/in_kernel_decompression_ima

Quoted 10 lines

Mimi

Quoted 5 lines

[1] [PATCH v4 0/3] initramfs: add support for xattrs in the initial ram disk
https://lore.kernel.org/linux-fsdevel/[email protected]/

Signature · 2 lines
Best regards,
Coiby
maintainer
#4 lore raw
Quoted 98 lines · Coiby Xu
Quoted 32 lines · Mimi Zohar
Quoted 31 lines · Mimi Zohar
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

Hi Mimi,

Thanks for listing and framing the issues! Sorry, it took me a while to go through your feedback as I also had a 8-day holiday.

Quoted 6 lines
Quoted 6 lines

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

Since IMA only accesses a kernel module as a fd or struct file*, even if IMA hook is triggerd after kernel module is decompressed, IMA still doesn't know the default verification has passed or can't access the decompressed kernel buffer [2] to do the verification by itself.

[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/module/main.c?h=v6.17#n3689

Quoted 5 lines
Quoted 5 lines

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

Thanks you for pointing me to the work [1]. I'll take a more careful look at [1]. I think CPIO supporting xattr can be a long-term solution and also close a important security gap.

Quoted 4 lines
Quoted 4 lines

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

Yes, the issue is not limited to Power. And thanks for correcting me that Power arch specific IMA policy rules don't have this problem! Sorry I misread the code.

Quoted 15 lines
Quoted 9 lines

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Quoted 5 lines

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

Quoted 5 lines

or perhaps not totally.

init_module_from_file() doesn't pass the flags variable to kernel_read_file(). You might want to consider defining a new kernel_read_file_id enumeration named READING_COMPRESSED_MODULE.

Quoted 7 lines

Thanks for suggesting the solutions! I like the solution of CPIO supporting xattr but it seems it won't land in upstream soon. So I prefer the last approach. I've implemented one [3] by defining a new kernel_read_file_id enumeration, would you like me to post the patches to the mailing list directly?

[3] https://github.com/coiby/linux/tree/in_kernel_decompression_ima

A few thoughts, before you post the patch.

1. In the general case, the kernel module could be compressed, but without an appended signature. The new code should only defer appended signature verification, if there isn't an xattr or appended signature.

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

3.  The patch title "ima: Use default kernel module signature verification for compressed ones" is a bit off. It should be something along the lines of "ima:
defer compressed kernel module appended signature verification".

4. Simplify the patch description.

Quoted 1 line
Quoted 1 line

Mimi

author
#5 lore raw
Quoted 105 lines · Mimi Zohar
Quoted 104 lines · Coiby Xu
Quoted 32 lines · Mimi Zohar
Quoted 31 lines · Mimi Zohar
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

Hi Mimi,

Thanks for listing and framing the issues! Sorry, it took me a while to go through your feedback as I also had a 8-day holiday.

Quoted 6 lines
Quoted 6 lines

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

Since IMA only accesses a kernel module as a fd or struct file*, even if IMA hook is triggerd after kernel module is decompressed, IMA still doesn't know the default verification has passed or can't access the decompressed kernel buffer [2] to do the verification by itself.

[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/module/main.c?h=v6.17#n3689

Quoted 5 lines
Quoted 5 lines

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

Thanks you for pointing me to the work [1]. I'll take a more careful look at [1]. I think CPIO supporting xattr can be a long-term solution and also close a important security gap.

Quoted 4 lines
Quoted 4 lines

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

Yes, the issue is not limited to Power. And thanks for correcting me that Power arch specific IMA policy rules don't have this problem! Sorry I misread the code.

Quoted 15 lines
Quoted 9 lines

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Quoted 5 lines

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

Quoted 5 lines

or perhaps not totally.

init_module_from_file() doesn't pass the flags variable to kernel_read_file(). You might want to consider defining a new kernel_read_file_id enumeration named READING_COMPRESSED_MODULE.

Thanks for suggesting the solutions! I like the solution of CPIO supporting xattr but it seems it won't land in upstream soon. So I prefer the last approach. I've implemented one [3] by defining a new kernel_read_file_id enumeration, would you like me to post the patches to the mailing list directly?

Quoted 3 lines
Quoted 1 line

[3] https://github.com/coiby/linux/tree/in_kernel_decompression_ima

A few thoughts, before you post the patch.

Thank you for sharing your thoughts!

Quoted 4 lines

1. In the general case, the kernel module could be compressed, but without an appended signature. The new code should only defer appended signature verification, if there isn't an xattr or appended signature.

I'll add these two condition checks, thanks!

Quoted 6 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

Quoted 7 lines

3.  The patch title "ima: Use default kernel module signature verification for compressed ones" is a bit off. It should be something along the lines of "ima:
defer compressed kernel module appended signature verification".

4. Simplify the patch description.

I'll rephrase the title and try simplifying it. Thanks!

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

Mimi

Signature · 2 lines
Best regards,
Coiby
maintainer
#6 lore raw
Quoted 124 lines · Coiby Xu
Quoted 105 lines · Mimi Zohar
Quoted 104 lines · Coiby Xu
Quoted 32 lines · Mimi Zohar
Quoted 31 lines · Mimi Zohar
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

Hi Mimi,

Thanks for listing and framing the issues! Sorry, it took me a while to go through your feedback as I also had a 8-day holiday.

Quoted 6 lines
Quoted 6 lines

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

Since IMA only accesses a kernel module as a fd or struct file*, even if IMA hook is triggerd after kernel module is decompressed, IMA still doesn't know the default verification has passed or can't access the decompressed kernel buffer [2] to do the verification by itself.

[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/module/main.c?h=v6.17#n3689

Quoted 5 lines
Quoted 5 lines

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

Thanks you for pointing me to the work [1]. I'll take a more careful look at [1]. I think CPIO supporting xattr can be a long-term solution and also close a important security gap.

Quoted 4 lines
Quoted 4 lines

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

Yes, the issue is not limited to Power. And thanks for correcting me that Power arch specific IMA policy rules don't have this problem! Sorry I misread the code.

Quoted 15 lines
Quoted 9 lines

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Quoted 5 lines

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

Quoted 5 lines

or perhaps not totally.

init_module_from_file() doesn't pass the flags variable to kernel_read_file(). You might want to consider defining a new kernel_read_file_id enumeration named READING_COMPRESSED_MODULE.

Thanks for suggesting the solutions! I like the solution of CPIO supporting xattr but it seems it won't land in upstream soon. So I prefer the last approach. I've implemented one [3] by defining a new kernel_read_file_id enumeration, would you like me to post the patches to the mailing list directly?

Quoted 3 lines
Quoted 1 line

[3] https://github.com/coiby/linux/tree/in_kernel_decompression_ima

A few thoughts, before you post the patch.

Thank you for sharing your thoughts!

Quoted 4 lines

1. In the general case, the kernel module could be compressed, but without an appended signature. The new code should only defer appended signature verification, if there isn't an xattr or appended signature.

I'll add these two condition checks, thanks!

Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

Quoted 5 lines
Quoted 1 line

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 10 lines
Quoted 8 lines

3.  The patch title "ima: Use default kernel module signature verification for compressed ones" is a bit off. It should be something along the lines of "ima:
defer compressed kernel module appended signature verification".

4. Simplify the patch description.

I'll rephrase the title and try simplifying it. Thanks!

Thank you.

author
#7 lore raw
Quoted 127 lines · Mimi Zohar
Quoted 126 lines · Coiby Xu
Quoted 105 lines · Mimi Zohar
Quoted 104 lines · Coiby Xu
Quoted 32 lines · Mimi Zohar
Quoted 31 lines · Mimi Zohar
Quoted 30 lines · Coiby Xu

Currently, for any IMA policy that requires appraisal for kernel modules e.g. ima_policy=secure_boot, PowerPC architecture specific policy, booting will fail because IMA will reject a kernel module which will be decompressed in the kernel space and then have its signature verified.

This happens because when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, kmod will use finit_module syscall instead of init_module to load a module. And IMA mandates IMA xattr verification for finit_module unless appraise_type=imasig|modsig is specified in the rule. However currently initramfs doesn't support xattr. And IMA rule "func=MODULE_CHECK appraise_type=imasig|modsig" doesn't work either because IMA will treat to-be-decompressed kernel module as not having module signature as it can't decompress kernel module to check if signature exists.

So fall back to default kernel module signature verification when we have no way to verify IMA xattr.

---
Another approach will be to make IMA decompress the kernel module to check the signature. This requires refactoring kernel module code to make the in-kernel module decompressing feature modular and seemingly more efforts are needed. A second disadvantage is it feels counter-intuitive to verify the same kernel module signature twice. And we still need to make ima_policy=secure_boot allow verifying appended module signature.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Anyways, I'm open to suggestions and can try the latter approach if there are some benefits I'm not aware of or a better approach.

Coiby, there are multiple issues being discussed here. Before deciding on an appropriate solution, let's frame the issues(s) properly.

Hi Mimi,

Thanks for listing and framing the issues! Sorry, it took me a while to go through your feedback as I also had a 8-day holiday.

Quoted 6 lines
Quoted 6 lines

1. The finit_module syscall eventually calls init_module_from_file() to read the module into memory and then decompress it. The problem is that the kernel module signature verification occurs during the kernel_read_file(), before the kernel module is decompressed. Thus, the appended kernel module signature cannot be verified.

Since IMA only accesses a kernel module as a fd or struct file*, even if IMA hook is triggerd after kernel module is decompressed, IMA still doesn't know the default verification has passed or can't access the decompressed kernel buffer [2] to do the verification by itself.

[2] https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/module/main.c?h=v6.17#n3689

Quoted 5 lines
Quoted 5 lines

2. CPIO doesn't have xattr support. There were multiple attempts at including xattrs in CPIO, but none were upstreamed [1]. If file signatures stored in security.ima were available in the initramfs, then finit_module() could verify them, as opposed to the appended kernel module signature.

Thanks you for pointing me to the work [1]. I'll take a more careful look at [1]. I think CPIO supporting xattr can be a long-term solution and also close a important security gap.

Quoted 4 lines
Quoted 4 lines

3. The issues described above are generic, not limited to Power. When CONFIG_MODULE_SIG is configured, the arch specific IMA policy rules do not include an "appraise func=MODULE_CHECK".

Yes, the issue is not limited to Power. And thanks for correcting me that Power arch specific IMA policy rules don't have this problem! Sorry I misread the code.

Quoted 15 lines
Quoted 9 lines

4. Unlike the arch specific IMA policy rules, the built-in secure boot IMA policy, specified on the boot command line as "ima_policy=secure_boot", always enforces the IMA signature stored in security.ima.

Partial solutions without kernel changes:
- Enable CONFIG_MODULE_SIG (Doesn't solve 4)
- Disable kernel module compression.

Quoted 5 lines

Complete solution:
- Pick up and upstream Roberto Sassu's last version of initramfs support [1].
- Somehow prevent kernel_read_file() from failing when the kernel_read_file_id enumeration is READING_MODULE and the kernel module is compressed. The change might be limited to ima_post_read_file().

Quoted 5 lines

or perhaps not totally.

init_module_from_file() doesn't pass the flags variable to kernel_read_file(). You might want to consider defining a new kernel_read_file_id enumeration named READING_COMPRESSED_MODULE.

Thanks for suggesting the solutions! I like the solution of CPIO supporting xattr but it seems it won't land in upstream soon. So I prefer the last approach. I've implemented one [3] by defining a new kernel_read_file_id enumeration, would you like me to post the patches to the mailing list directly?

Quoted 3 lines
Quoted 1 line

[3] https://github.com/coiby/linux/tree/in_kernel_decompression_ima

A few thoughts, before you post the patch.

Thank you for sharing your thoughts!

Quoted 4 lines

1. In the general case, the kernel module could be compressed, but without an appended signature. The new code should only defer appended signature verification, if there isn't an xattr or appended signature.

I'll add these two condition checks, thanks!

Quoted 6 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Another thought is to make CPIO support xattr. Today I realize that ima_policy=secure_boot can also cause failure of loading kdump kernel. So the issue this patch tries to resolves has much less impact than I thought. Maybe we can wait until CPIO xattr support is ready? I'll help review and test Roberto's patches if this is the best way forward.

Quoted 14 lines
Quoted 11 lines
Quoted 9 lines

3.  The patch title "ima: Use default kernel module signature verification for compressed ones" is a bit off. It should be something along the lines of "ima:
defer compressed kernel module appended signature verification".

4. Simplify the patch description.

I'll rephrase the title and try simplifying it. Thanks!

Thank you.

Signature · 2 lines
Best regards,
Coiby
maintainer
#8 lore raw
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Remember based on policy, IMA supports:
1. verifying the signature stored in security.ima xattr
2. verifying the appended signature (not for compressed kernel modules)
3. verifying both the xattr and appended signatures
4. none

To prevent 3 - verifying both types of signatures, the IMA arch specific policy rule only adds the "appraise func=MODULE_CHECK ..." rule if CONFIG_MODULE_SIG is NOT enabled. Calling set_module_sig_enforced() from ima_appraise_measurement() to set sig_enforce could inadvertently result in requiring both the xattr and the appended signature kernel module verification. To prevent this from happening, "sig_enforce" should not be set, only verified in ima_appraise_measurement().

Quoted 6 lines

Another thought is to make CPIO support xattr. Today I realize that ima_policy=secure_boot can also cause failure of loading kdump kernel. So the issue this patch tries to resolves has much less impact than I thought. Maybe we can wait until CPIO xattr support is ready? I'll help review and test Roberto's patches if this is the best way forward.

I'm not sure of the status of the CPIO patch set. Roberto?

Mimi

#9 lore raw
Quoted 44 lines · Mimi Zohar
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Remember based on policy, IMA supports:
1. verifying the signature stored in security.ima xattr
2. verifying the appended signature (not for compressed kernel modules)
3. verifying both the xattr and appended signatures
4. none

To prevent 3 - verifying both types of signatures, the IMA arch specific policy rule only adds the "appraise func=MODULE_CHECK ..." rule if CONFIG_MODULE_SIG is NOT enabled. Calling set_module_sig_enforced() from ima_appraise_measurement() to set sig_enforce could inadvertently result in requiring both the xattr and the appended signature kernel module verification. To prevent this from happening, "sig_enforce" should not be set, only verified in ima_appraise_measurement().

Quoted 1 line
Quoted 7 lines
Quoted 5 lines

Another thought is to make CPIO support xattr. Today I realize that ima_policy=secure_boot can also cause failure of loading kdump kernel. So the issue this patch tries to resolves has much less impact than I thought. Maybe we can wait until CPIO xattr support is ready? I'll help review and test Roberto's patches if this is the best way forward.

I'm not sure of the status of the CPIO patch set. Roberto?

I haven't had time to look at it recently. I can take the openEuler version, address the remaining comments and repost.

Roberto

maintainer
#10 lore raw
Quoted 51 lines · Roberto Sassu
Quoted 50 lines · Mimi Zohar
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Remember based on policy, IMA supports:
1. verifying the signature stored in security.ima xattr
2. verifying the appended signature (not for compressed kernel modules)
3. verifying both the xattr and appended signatures
4. none

To prevent 3 - verifying both types of signatures, the IMA arch specific policy rule only adds the "appraise func=MODULE_CHECK ..." rule if CONFIG_MODULE_SIG is NOT enabled. Calling set_module_sig_enforced() from ima_appraise_measurement() to set sig_enforce could inadvertently result in requiring both the xattr and the appended signature kernel module verification. To prevent this from happening, "sig_enforce" should not be set, only verified in ima_appraise_measurement().

Quoted 6 lines

Another thought is to make CPIO support xattr. Today I realize that ima_policy=secure_boot can also cause failure of loading kdump kernel. So the issue this patch tries to resolves has much less impact than I thought. Maybe we can wait until CPIO xattr support is ready? I'll help review and test Roberto's patches if this is the best way forward.

Quoted 4 lines
Quoted 1 line

I'm not sure of the status of the CPIO patch set. Roberto?

I haven't had time to look at it recently. I can take the openEuler version, address the remaining comments and repost.

Thank you!

author
#11 lore raw
Quoted 2 lines · Mimi Zohar
Quoted 1 line · Roberto Sassu

On Mon, 2025-10-20 at 08:21 -0400, Mimi Zohar wrote:

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

Another thought is to make CPIO support xattr. Today I realize that ima_policy=secure_boot can also cause failure of loading kdump kernel. So the issue this patch tries to resolves has much less impact than I thought. Maybe we can wait until CPIO xattr support is ready? I'll help review and test Roberto's patches if this is the best way forward.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I'm not sure of the status of the CPIO patch set. Roberto?

I haven't had time to look at it recently. I can take the openEuler version, address the remaining comments and repost.

Thank you!

+1, I'm looking forward to the reposted patch set!

Signature · 2 lines
Best regards,
Coiby
maintainer
#12 lore raw
In reply to Mimi Zohar
Quoted 23 lines · Mimi Zohar
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Quoted 5 lines

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Looking at the code further, decompressing the kernel module in IMA is redundant. Instead I think the best approach would be to:
- define DECOMPRESSED_MODULE, in addition to COMPRESSED_MODULE.

id(COMPRESSED_MODULE, compressed-kernel-module) \
id(DECOMPRESSED_MODULE, decompressed-kernel-module) \

- instead of passing a boolean indicating whether the module is compressed, pass the kernel_read_file_id enumeration to differentiate between the compressed and decompressed module.

- define a new IMA hook, probably LSM hook as well, named ima_decompressed_module().

- call the new ima_decompressed_module() from init_module_from_file() immediately after decompressing the kernel module. Something along the lines of:

err = ima_decompressed_module(f, (char *)info.hdr, info.len,

                              READING_DECOMPRESSED_MODULE);

For testing purposes to see the decompressed appended signature in the measurement list, modify the MODULE_CHECK measure rule to include "template=ima- modsig" in ima_efi.c.

Signature · 1 line
Mimi
author
#13 lore raw
Quoted 44 lines · Mimi Zohar
Quoted 23 lines · Mimi Zohar
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Quoted 5 lines

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Looking at the code further, decompressing the kernel module in IMA is redundant. Instead I think the best approach would be to:
- define DECOMPRESSED_MODULE, in addition to COMPRESSED_MODULE.

id(COMPRESSED_MODULE, compressed-kernel-module) \
id(DECOMPRESSED_MODULE, decompressed-kernel-module) \

- instead of passing a boolean indicating whether the module is compressed, pass the kernel_read_file_id enumeration to differentiate between the compressed and decompressed module.

- define a new IMA hook, probably LSM hook as well, named ima_decompressed_module().

Quoted 6 lines

- call the new ima_decompressed_module() from init_module_from_file() immediately after decompressing the kernel module. Something along the lines of:

err = ima_decompressed_module(f, (char *)info.hdr, info.len,

                             READING_DECOMPRESSED_MODULE);

Thanks for proposing a better solution! Yeah, decompressing the kernel module in IMA is unnecessary. Do you think we can further extend your idea to call one IMA hook only after kernel module decompression is done? If we call two IMA hooks in finit_module, we'll need coordination between two IMA hooks i.e. the 1st IMA hook shouldn't fail in order for the 2nd IMA hook to be executed. The new IMA hook will always have access to the decompressed kernel module buffer so there is no need to differentiate between compressed and decompressed module.

Another question is whether we should allow loading a kernel module with appended signature but misses IMA signature. Both IMA arch specific policy and init_module syscall only require appended signature verification. On the other hand, we only allow "appraise_type=imasig|modsig" but not appraise_type=modsig. How about we allow loading a kernel module with valid appended signature regardless of its IMA signature? We won't call set_module_sig_enforced but as long as we know is_module_sig_enforced() is true, we allow the module in IMA.

Quoted 4 lines

For testing purposes to see the decompressed appended signature in the measurement list, modify the MODULE_CHECK measure rule to include "template=ima- modsig" in ima_efi.c.

I haven't figured out why to include "template=ima-modsig" for testing purpose considering we can simply check if the kernel module has been loaded successfully. It it related to the design that "The d-modsig and modsig fields are only populated if both the measure and appraise rules trigger"? If so, can you also help me understand there is such a design?

[1] https://ima-doc.readthedocs.io/en/latest/event-log-format.html#ima-modsig

Quoted 3 lines
Signature · 1 line
Mimi
Signature · 2 lines
Best regards,
Coiby
maintainer
#14 lore raw
Quoted 52 lines · Coiby Xu
Quoted 44 lines · Mimi Zohar
Quoted 23 lines · Mimi Zohar
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Quoted 5 lines

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Looking at the code further, decompressing the kernel module in IMA is redundant. Instead I think the best approach would be to:
- define DECOMPRESSED_MODULE, in addition to COMPRESSED_MODULE.

id(COMPRESSED_MODULE, compressed-kernel-module) \
id(DECOMPRESSED_MODULE, decompressed-kernel-module) \

- instead of passing a boolean indicating whether the module is compressed, pass the kernel_read_file_id enumeration to differentiate between the compressed and decompressed module.

- define a new IMA hook, probably LSM hook as well, named ima_decompressed_module().

Quoted 6 lines

- call the new ima_decompressed_module() from init_module_from_file() immediately after decompressing the kernel module. Something along the lines of:

err = ima_decompressed_module(f, (char *)info.hdr, info.len,

                             READING_DECOMPRESSED_MODULE);
Quoted 8 lines

Thanks for proposing a better solution! Yeah, decompressing the kernel module in IMA is unnecessary. Do you think we can further extend your idea to call one IMA hook only after kernel module decompression is done? If we call two IMA hooks in finit_module, we'll need coordination between two IMA hooks i.e. the 1st IMA hook shouldn't fail in order for the 2nd IMA hook to be executed. The new IMA hook will always have access to the decompressed kernel module buffer so there is no need to differentiate between compressed and decompressed module.

Agreed instead of verifying the kernel module signature on the LSM kernel_post_read_file() hook, define and move it to a new IMA/LSM hook after it is decompressed, would be much simpler than coordinating two LSM hooks.

Quoted 9 lines

Another question is whether we should allow loading a kernel module with appended signature but misses IMA signature. Both IMA arch specific policy and init_module syscall only require appended signature verification. On the other hand, we only allow "appraise_type=imasig|modsig" but not appraise_type=modsig. How about we allow loading a kernel module with valid appended signature regardless of its IMA signature? We won't call set_module_sig_enforced but as long as we know is_module_sig_enforced() is true, we allow the module in IMA.

Based on the policy, IMA enforces signature verification. Only if CONFIG_MODULE_SIG is configured, the IMA arch specific policy does not define an IMA kernel module appraise rule. However, custom policies could still require both types of signatures, not necessarily signed by the same entity.

The option "appraise_type=imasig|modsig" allows either an IMA signature OR an appended signature.

Quoted 9 lines
Quoted 5 lines

For testing purposes to see the decompressed appended signature in the measurement list, modify the MODULE_CHECK measure rule to include "template=ima- modsig" in ima_efi.c.

I haven't figured out why to include "template=ima-modsig" for testing purpose considering we can simply check if the kernel module has been loaded successfully.

That's fine too.

Quoted 5 lines

It it related to the design that "The d-modsig and
modsig fields are only populated if both the measure and appraise rules trigger"? If so, can you also help me understand there is such a design?

[1] https://ima-doc.readthedocs.io/en/latest/event-log-format.html#ima-modsig

The "ima-sig" template contains the file data hash and file signature, allowing the attestation server to verify the signature based on the file data hash contained in the measurement list.

In addition to the file data hash and the file signature, the "ima-modsig" template contains the file data hash without the appended signature, allowing the attestation server to verify the appended signature against it.

Mimi

author
#15 lore raw
Quoted 62 lines · Mimi Zohar
Quoted 52 lines · Coiby Xu
Quoted 44 lines · Mimi Zohar
Quoted 23 lines · Mimi Zohar
Quoted 15 lines · Coiby Xu
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

2. Instead of defining an additional process_measurement() argument to identify compressed kernel modules, to simplify the code it might be possible to define a new "func" named COMPRESSED_MODULE_CHECK.

+ [READING_COMPRESSED_MODULE] = MODULE_CHECK, -> COMPRESSED_MODULE_CHECK

Quoted 8 lines
Quoted 3 lines

I also thought about this approach. But IMA rule maps kernel module loading to MODULE_CHECK. If we define a new rule and ask users to use this new rule, ima_policy=secure_boot still won't work.

I don't have a problem with extending the "secure-boot" policy to support uncompressed kernel modules appended signatures, based on whether CONFIG_MODULE_SIG is enabled. The new rule would be in addition to the existing MODULE_CHECK rule.

Quoted 6 lines

I assume once the new rule get added, we can't remove it for userspace backward compatibility, right? And with CPIO xattr supported, it seems there is no need to keep this rule. So if this concern is valid, do you think we shall switch to another approach i.e. to make IMA support verifying decompressed module and then make "secure-boot" to allow appended module signature?

Quoted 5 lines

Yes, once the rule is added, it wouldn't be removed. As for "to make IMA support verifying decompressed module", yes that might be a better solution, than relying on "sig_enforce" being enabled. IMA already supports verifying the appended signatures. A new IMA specific or LSM hook would need to be defined after module_decompress().

Looking at the code further, decompressing the kernel module in IMA is redundant. Instead I think the best approach would be to:
- define DECOMPRESSED_MODULE, in addition to COMPRESSED_MODULE.

id(COMPRESSED_MODULE, compressed-kernel-module) \
id(DECOMPRESSED_MODULE, decompressed-kernel-module) \

- instead of passing a boolean indicating whether the module is compressed, pass the kernel_read_file_id enumeration to differentiate between the compressed and decompressed module.

- define a new IMA hook, probably LSM hook as well, named ima_decompressed_module().

Quoted 6 lines

- call the new ima_decompressed_module() from init_module_from_file() immediately after decompressing the kernel module. Something along the lines of:

err = ima_decompressed_module(f, (char *)info.hdr, info.len,

                             READING_DECOMPRESSED_MODULE);
Quoted 8 lines

Thanks for proposing a better solution! Yeah, decompressing the kernel module in IMA is unnecessary. Do you think we can further extend your idea to call one IMA hook only after kernel module decompression is done? If we call two IMA hooks in finit_module, we'll need coordination between two IMA hooks i.e. the 1st IMA hook shouldn't fail in order for the 2nd IMA hook to be executed. The new IMA hook will always have access to the decompressed kernel module buffer so there is no need to differentiate between compressed and decompressed module.

Quoted 3 lines

Agreed instead of verifying the kernel module signature on the LSM kernel_post_read_file() hook, define and move it to a new IMA/LSM hook after it is decompressed, would be much simpler than coordinating two LSM hooks.

Thanks for confirming it! I'll send a new version once the testing is finished.

Quoted 11 lines
Quoted 10 lines

Another question is whether we should allow loading a kernel module with appended signature but misses IMA signature. Both IMA arch specific policy and init_module syscall only require appended signature verification. On the other hand, we only allow "appraise_type=imasig|modsig" but not appraise_type=modsig. How about we allow loading a kernel module with valid appended signature regardless of its IMA signature? We won't call set_module_sig_enforced but as long as we know is_module_sig_enforced() is true, we allow the module in IMA.

Quoted 7 lines

Based on the policy, IMA enforces signature verification. Only if CONFIG_MODULE_SIG is configured, the IMA arch specific policy does not define an IMA kernel module appraise rule. However, custom policies could still require both types of signatures, not necessarily signed by the same entity.

The option "appraise_type=imasig|modsig" allows either an IMA signature OR an appended signature.

Thanks for the clarification! If I understand you correctly, some users
may want to enforce IMA signature verification and we should provide
such flexibility. Then do you think it's a good idea to change the kernel
module rule in ima_policy=secure_boot to "appraise func=MODULE_CHECK appraise_type=imasig|modsig" so ima_policy=secure_boot can also work for in-kernel decompressing
modules?

Signature · 2 lines
Best regards,
Coiby
maintainer
#16 lore raw
Quoted 18 lines · Coiby Xu
Quoted 10 lines
Quoted 9 lines

Another question is whether we should allow loading a kernel module with appended signature but misses IMA signature. Both IMA arch specific policy and init_module syscall only require appended signature verification. On the other hand, we only allow "appraise_type=imasig|modsig" but not appraise_type=modsig. How about we allow loading a kernel module with valid appended signature regardless of its IMA signature? We won't call set_module_sig_enforced but as long as we know is_module_sig_enforced() is true, we allow the module in IMA.

Quoted 7 lines

Based on the policy, IMA enforces signature verification. Only if CONFIG_MODULE_SIG is configured, the IMA arch specific policy does not define an IMA kernel module appraise rule. However, custom policies could still require both types of signatures, not necessarily signed by the same entity.

The option "appraise_type=imasig|modsig" allows either an IMA signature OR an appended signature.

Quoted 7 lines

Thanks for the clarification! If I understand you correctly, some users may want to enforce IMA signature verification and we should provide such flexibility. Then do you think it's a good idea to change the kernel module rule in ima_policy=secure_boot to
"appraise func=MODULE_CHECK appraise_type=imasig|modsig" so ima_policy=secure_boot can also work for in-kernel decompressing modules?

Yes, that's fine. Unlike the arch specific policy rules and the Kconfig appraise rules, which persist after loading a custom policy, the builtin secure boot rules do not persist.

Mimi

author
#17 lore raw
Quoted 27 lines · Mimi Zohar
Quoted 18 lines · Coiby Xu
Quoted 10 lines
Quoted 9 lines

Another question is whether we should allow loading a kernel module with appended signature but misses IMA signature. Both IMA arch specific policy and init_module syscall only require appended signature verification. On the other hand, we only allow "appraise_type=imasig|modsig" but not appraise_type=modsig. How about we allow loading a kernel module with valid appended signature regardless of its IMA signature? We won't call set_module_sig_enforced but as long as we know is_module_sig_enforced() is true, we allow the module in IMA.

Quoted 7 lines

Based on the policy, IMA enforces signature verification. Only if CONFIG_MODULE_SIG is configured, the IMA arch specific policy does not define an IMA kernel module appraise rule. However, custom policies could still require both types of signatures, not necessarily signed by the same entity.

The option "appraise_type=imasig|modsig" allows either an IMA signature OR an appended signature.

Quoted 7 lines

Thanks for the clarification! If I understand you correctly, some users may want to enforce IMA signature verification and we should provide such flexibility. Then do you think it's a good idea to change the kernel module rule in ima_policy=secure_boot to
"appraise func=MODULE_CHECK appraise_type=imasig|modsig" so ima_policy=secure_boot can also work for in-kernel decompressing modules?

Quoted 3 lines

Yes, that's fine. Unlike the arch specific policy rules and the Kconfig appraise rules, which persist after loading a custom policy, the builtin secure boot rules do not persist.

Thanks for the confirmation! v2 has been posted.

Quoted 4 lines

Mimi

Signature · 2 lines
Best regards,
Coiby
#18 lore raw
In reply to Coiby Xu
kernel test robot noticed the following build errors:

Hi Coiby,

kernel test robot noticed the following build errors:

[auto build test ERROR on cec1e6e5d1ab33403b809f79cd20d6aff124ccfe]

url:    https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/ima-Fall-back-to-default-kernel-module-signature-verification/20250928-110501
base:   cec1e6e5d1ab33403b809f79cd20d6aff124ccfe
patch link:    https://lore.kernel.org/r/20250928030358.3873311-1-coxu%40redhat.com
patch subject: [PATCH] ima: Fall back to default kernel module signature verification
config: i386-randconfig-012-20251002 (https://download.01.org/0day-ci/archive/20251003/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251003/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: security/integrity/ima/ima_appraise.o: in function `ima_appraise_measurement':

>> security/integrity/ima/ima_appraise.c:587:(.text+0xbbb): undefined reference to `set_module_sig_enforced'

vim +587 security/integrity/ima/ima_appraise.c

483
484 /*
485 * ima_appraise_measurement - appraise file measurement
486 *
487 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
488 * Assuming success, compare the xattr hash with the collected measurement.
489 *
490 * Return 0 on success, error code otherwise
491 */
492 int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,

   493				     struct file *file, const unsigned char *filename,
   494				     struct evm_ima_xattr_data *xattr_value,
   495				     int xattr_len, const struct modsig *modsig)
   496	{
   497		static const char op[] = "appraise_data";
   498		int audit_msgno = AUDIT_INTEGRITY_DATA;
   499		const char *cause = "unknown";
   500		struct dentry *dentry = file_dentry(file);
   501		struct inode *inode = d_backing_inode(dentry);
   502		enum integrity_status status = INTEGRITY_UNKNOWN;
   503		int rc = xattr_len;
   504		bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
   505		bool enforce_module_sig = iint->flags & IMA_DIGSIG_REQUIRED && func == MODULE_CHECK;
   506	
   507		/* If not appraising a modsig or using default module verification, we need an xattr. */
   508		if (!(inode->i_opflags & IOP_XATTR) && !try_modsig && !enforce_module_sig)
   509			return INTEGRITY_UNKNOWN;
   510	
   511		/*
   512		 * Unlike any of the other LSM hooks where the kernel enforces file
   513		 * integrity, enforcing file integrity for the bprm_creds_for_exec()
   514		 * LSM hook with the AT_EXECVE_CHECK flag is left up to the discretion
   515		 * of the script interpreter(userspace). Differentiate kernel and
   516		 * userspace enforced integrity audit messages.
   517		 */
   518		if (is_bprm_creds_for_exec(func, file))
   519			audit_msgno = AUDIT_INTEGRITY_USERSPACE;
   520	
   521		/* If reading the xattr failed and there's no modsig or module verification, error out. */
   522		if (rc <= 0 && !try_modsig && !enforce_module_sig) {
   523			if (rc && rc != -ENODATA)
   524				goto out;
   525	
   526			if (iint->flags & IMA_DIGSIG_REQUIRED) {
   527				if (iint->flags & IMA_VERITY_REQUIRED)
   528					cause = "verity-signature-required";
   529				else
   530					cause = "IMA-signature-required";
   531			} else {
   532				cause = "missing-hash";
   533			}
   534	
   535			status = INTEGRITY_NOLABEL;
   536			if (file->f_mode & FMODE_CREATED)
   537				iint->flags |= IMA_NEW_FILE;
   538			if ((iint->flags & IMA_NEW_FILE) &&
   539			    (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
   540			     (inode->i_size == 0)))
   541				status = INTEGRITY_PASS;
   542			goto out;
   543		}
   544	
   545		status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
   546					 rc < 0 ? 0 : rc);
   547		switch (status) {
   548		case INTEGRITY_PASS:
   549		case INTEGRITY_PASS_IMMUTABLE:
   550		case INTEGRITY_UNKNOWN:
   551			break;
   552		case INTEGRITY_NOXATTRS:	/* No EVM protected xattrs. */
   553			/* Fine to not have xattrs when using a modsig or default module verification. */
   554			if (try_modsig || enforce_module_sig)
   555				break;
   556			fallthrough;
   557		case INTEGRITY_NOLABEL:		/* No security.evm xattr. */
   558			cause = "missing-HMAC";
   559			goto out;
   560		case INTEGRITY_FAIL_IMMUTABLE:
   561			set_bit(IMA_DIGSIG, &iint->atomic_flags);
   562			cause = "invalid-fail-immutable";
   563			goto out;
   564		case INTEGRITY_FAIL:		/* Invalid HMAC/signature. */
   565			cause = "invalid-HMAC";
   566			goto out;
   567		default:
   568			WARN_ONCE(true, "Unexpected integrity status %d\n", status);
   569		}
   570	
   571		if (xattr_value)
   572			rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
   573					  &cause);
   574	
   575		/*
   576		 * If we have a modsig and either no imasig or the imasig's key isn't
   577		 * known, then try verifying the modsig.
   578		 */
   579		if (try_modsig &&
   580		    (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
   581		     rc == -ENOKEY))
   582			rc = modsig_verify(func, modsig, &status, &cause);
   583	
   584		/* Fall back to default kernel module signature verification */
   585		if (rc && enforce_module_sig) {
   586			rc = 0;
 > 587			set_module_sig_enforced();

588 /* CONFIG_MODULE_SIG may be disabled */
589 if (is_module_sig_enforced()) {
590 rc = 0;
591 status = INTEGRITY_PASS;
592 pr_debug("Fall back to default kernel module verification for %s\n", filename);
593 }
594 }
595
596 out:
597 /*
598 * File signatures on some filesystems can not be properly verified.
599 * When such filesystems are mounted by an untrusted mounter or on a
600 * system not willing to accept such a risk, fail the file signature
601 * verification.
602 */
603 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
604 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
605 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
606 status = INTEGRITY_FAIL;
607 cause = "unverifiable-signature";
608 integrity_audit_msg(audit_msgno, inode, filename,
609 op, cause, rc, 0);
610 } else if (status != INTEGRITY_PASS) {
611 /* Fix mode, but don't replace file signatures. */
612 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
613 (!xattr_value ||
614 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
615 if (!ima_fix_xattr(dentry, iint))
616 status = INTEGRITY_PASS;
617 }
618
619 /*
620 * Permit new files with file/EVM portable signatures, but
621 * without data.
622 */
623 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
624 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
625 status = INTEGRITY_PASS;
626 }
627
628 integrity_audit_msg(audit_msgno, inode, filename,
629 op, cause, rc, 0);
630 } else {
631 ima_cache_flags(iint, func);
632 }
633
634 ima_set_cache_status(iint, func, status);
635 return status;
636 }
637

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#19 lore raw
Quoted 1 line · kernel test robot

Hi Coiby,

Hi,

Quoted 13 lines

kernel test robot noticed the following build errors:

[auto build test ERROR on cec1e6e5d1ab33403b809f79cd20d6aff124ccfe]

url:    https://github.com/intel-lab-lkp/linux/commits/Coiby-Xu/ima-Fall-back-to-default-kernel-module-signature-verification/20250928-110501
base:   cec1e6e5d1ab33403b809f79cd20d6aff124ccfe
patch link:    https://lore.kernel.org/r/20250928030358.3873311-1-coxu%40redhat.com
patch subject: [PATCH] ima: Fall back to default kernel module signature verification
config: i386-randconfig-012-20251002 (https://download.01.org/0day-ci/archive/20251003/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251003/[email protected]/reproduce)
Quoted 9 lines

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: security/integrity/ima/ima_appraise.o: in function `ima_appraise_measurement':

Quoted 1 line
Quoted 1 line

security/integrity/ima/ima_appraise.c:587:(.text+0xbbb): undefined reference to `set_module_sig_enforced'

Thanks for reporting the error! This happens when set_module_sig_enforced is called without CONFIG_MODULE_SIG not enabled. I'll address this issue by declaring set_module_sig_enforced only when CONFIG_MODULE_SIG is enabled.

Quoted 164 lines

vim +587 security/integrity/ima/ima_appraise.c

483
484 /*
485 * ima_appraise_measurement - appraise file measurement
486 *
487 * Call evm_verifyxattr() to verify the integrity of 'security.ima'.
488 * Assuming success, compare the xattr hash with the collected measurement.
489 *
490 * Return 0 on success, error code otherwise
491 */
492 int ima_appraise_measurement(enum ima_hooks func, struct ima_iint_cache *iint,

  493				     struct file *file, const unsigned char *filename,
  494				     struct evm_ima_xattr_data *xattr_value,
  495				     int xattr_len, const struct modsig *modsig)

496 {
497 static const char op[] = "appraise_data";
498 int audit_msgno = AUDIT_INTEGRITY_DATA;
499 const char *cause = "unknown";
500 struct dentry *dentry = file_dentry(file);
501 struct inode *inode = d_backing_inode(dentry);
502 enum integrity_status status = INTEGRITY_UNKNOWN;
503 int rc = xattr_len;
504 bool try_modsig = iint->flags & IMA_MODSIG_ALLOWED && modsig;
505 bool enforce_module_sig = iint->flags & IMA_DIGSIG_REQUIRED && func == MODULE_CHECK;
506
507 /* If not appraising a modsig or using default module verification, we need an xattr. */
508 if (!(inode->i_opflags & IOP_XATTR) && !try_modsig && !enforce_module_sig)
509 return INTEGRITY_UNKNOWN;
510
511 /*
512 * Unlike any of the other LSM hooks where the kernel enforces file
513 * integrity, enforcing file integrity for the bprm_creds_for_exec()
514 * LSM hook with the AT_EXECVE_CHECK flag is left up to the discretion
515 * of the script interpreter(userspace). Differentiate kernel and
516 * userspace enforced integrity audit messages.
517 */
518 if (is_bprm_creds_for_exec(func, file))
519 audit_msgno = AUDIT_INTEGRITY_USERSPACE;
520
521 /* If reading the xattr failed and there's no modsig or module verification, error out. */
522 if (rc <= 0 && !try_modsig && !enforce_module_sig) {
523 if (rc && rc != -ENODATA)
524 goto out;
525
526 if (iint->flags & IMA_DIGSIG_REQUIRED) {
527 if (iint->flags & IMA_VERITY_REQUIRED)
528 cause = "verity-signature-required";
529 else
530 cause = "IMA-signature-required";
531 } else {
532 cause = "missing-hash";
533 }
534
535 status = INTEGRITY_NOLABEL;
536 if (file->f_mode & FMODE_CREATED)
537 iint->flags |= IMA_NEW_FILE;
538 if ((iint->flags & IMA_NEW_FILE) &&
539 (!(iint->flags & IMA_DIGSIG_REQUIRED) ||
540 (inode->i_size == 0)))
541 status = INTEGRITY_PASS;
542 goto out;
543 }
544
545 status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
546 rc < 0 ? 0 : rc);
547 switch (status) {
548 case INTEGRITY_PASS:
549 case INTEGRITY_PASS_IMMUTABLE:
550 case INTEGRITY_UNKNOWN:
551 break;
552 case INTEGRITY_NOXATTRS: /* No EVM protected xattrs. */
553 /* Fine to not have xattrs when using a modsig or default module verification. */
554 if (try_modsig || enforce_module_sig)
555 break;
556 fallthrough;
557 case INTEGRITY_NOLABEL: /* No security.evm xattr. */
558 cause = "missing-HMAC";
559 goto out;
560 case INTEGRITY_FAIL_IMMUTABLE:
561 set_bit(IMA_DIGSIG, &iint->atomic_flags);
562 cause = "invalid-fail-immutable";
563 goto out;
564 case INTEGRITY_FAIL: /* Invalid HMAC/signature. */
565 cause = "invalid-HMAC";
566 goto out;
567 default:
568 WARN_ONCE(true, "Unexpected integrity status %d\n", status);
569 }
570
571 if (xattr_value)
572 rc = xattr_verify(func, iint, xattr_value, xattr_len, &status,
573 &cause);
574
575 /*
576 * If we have a modsig and either no imasig or the imasig's key isn't
577 * known, then try verifying the modsig.
578 */
579 if (try_modsig &&
580 (!xattr_value || xattr_value->type == IMA_XATTR_DIGEST_NG ||
581 rc == -ENOKEY))
582 rc = modsig_verify(func, modsig, &status, &cause);
583
584 /* Fall back to default kernel module signature verification */
585 if (rc && enforce_module_sig) {
586 rc = 0;

Quoted 1 line

587 set_module_sig_enforced();

588 /* CONFIG_MODULE_SIG may be disabled */
589 if (is_module_sig_enforced()) {
590 rc = 0;
591 status = INTEGRITY_PASS;
592 pr_debug("Fall back to default kernel module verification for %s\n", filename);
593 }
594 }
595
596 out:
597 /*
598 * File signatures on some filesystems can not be properly verified.
599 * When such filesystems are mounted by an untrusted mounter or on a
600 * system not willing to accept such a risk, fail the file signature
601 * verification.
602 */
603 if ((inode->i_sb->s_iflags & SB_I_IMA_UNVERIFIABLE_SIGNATURE) &&
604 ((inode->i_sb->s_iflags & SB_I_UNTRUSTED_MOUNTER) ||
605 (iint->flags & IMA_FAIL_UNVERIFIABLE_SIGS))) {
606 status = INTEGRITY_FAIL;
607 cause = "unverifiable-signature";
608 integrity_audit_msg(audit_msgno, inode, filename,
609 op, cause, rc, 0);
610 } else if (status != INTEGRITY_PASS) {
611 /* Fix mode, but don't replace file signatures. */
612 if ((ima_appraise & IMA_APPRAISE_FIX) && !try_modsig &&
613 (!xattr_value ||
614 xattr_value->type != EVM_IMA_XATTR_DIGSIG)) {
615 if (!ima_fix_xattr(dentry, iint))
616 status = INTEGRITY_PASS;
617 }
618
619 /*
620 * Permit new files with file/EVM portable signatures, but
621 * without data.
622 */
623 if (inode->i_size == 0 && iint->flags & IMA_NEW_FILE &&
624 test_bit(IMA_DIGSIG, &iint->atomic_flags)) {
625 status = INTEGRITY_PASS;
626 }
627
628 integrity_audit_msg(audit_msgno, inode, filename,
629 op, cause, rc, 0);
630 } else {
631 ima_cache_flags(iint, func);
632 }
633
634 ima_set_cache_status(iint, func, status);
635 return status;
636 }
637

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Signature · 2 lines
Best regards,
Coiby
author
#20 lore raw

PATCH v2 1/1 lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

In reply to Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)
include/linux/lsm_hook_defs.h +2 -0
index 8c42b4bde09c..ced42eb8b618 100644
@@ -232,6 +232,8 @@ LSM_HOOK(int, 0, kernel_read_file, struct file *file,
 	 enum kernel_read_file_id id, bool contents)
 LSM_HOOK(int, 0, kernel_post_read_file, struct file *file, char *buf,
 	 loff_t size, enum kernel_read_file_id id)
+LSM_HOOK(int, 0, kernel_module_read_file, struct file *file, char *buf,
+	 loff_t size)
 LSM_HOOK(int, 0, task_fix_setuid, struct cred *new, const struct cred *old,
 	 int flags)
 LSM_HOOK(int, 0, task_fix_setgid, struct cred *new, const struct cred * old,
include/linux/security.h +7 -0
index 92ac3f27b973..e47951292c73 100644
@@ -508,6 +508,7 @@ int security_kernel_read_file(struct file *file, enum kernel_read_file_id id,
 			      bool contents);
 int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
 				   enum kernel_read_file_id id);
+int security_kernel_module_read_file(struct file *file, char *buf, loff_t size);
 int security_task_fix_setuid(struct cred *new, const struct cred *old,
 			     int flags);
 int security_task_fix_setgid(struct cred *new, const struct cred *old,
@@ -1295,6 +1296,12 @@ static inline int security_kernel_post_read_file(struct file *file,
 	return 0;
 }
 
+static inline int security_kernel_module_read_file(struct file *file,
+						   char *buf, loff_t size)
+{
+	return 0;
+}
+
 static inline int security_task_fix_setuid(struct cred *new,
 					   const struct cred *old,
 					   int flags)
kernel/module/main.c +9 -1
index c66b26184936..40bc86fa7384 100644
@@ -3678,6 +3678,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 	struct load_info info = { };
 	void *buf = NULL;
 	int len;
+	int err;
 
 	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
 	if (len < 0) {
@@ -3686,7 +3687,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 	}
 
 	if (flags & MODULE_INIT_COMPRESSED_FILE) {
-		int err = module_decompress(&info, buf, len);
+		err = module_decompress(&info, buf, len);
 		vfree(buf); /* compressed data is no longer needed */
 		if (err) {
 			mod_stat_inc(&failed_decompress);
@@ -3698,6 +3699,13 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 		info.len = len;
 	}
 
+	err = security_kernel_module_read_file(f, (char *)info.hdr, info.len);
+	if (err) {
+		mod_stat_inc(&failed_kreads);
+		free_copy(&info, flags);
+		return err;
+	}
+
 	return load_module(&info, uargs, flags);
 }
 
security/integrity/ima/ima_main.c +26 -0
index cdd225f65a62..53d2e90176ea 100644
@@ -635,6 +635,27 @@ static int ima_file_check(struct file *file, int mask)
 					   MAY_APPEND), FILE_CHECK);
 }
 
+/**
+ * ima_read_kernel_module - collect/appraise/audit measurement
+ * @file: file pointer to the module.
+ * @buf: buffer containing module data (possibly decompressed).
+ * @size: size of the buffer.
+ *
+ * This IMA hook for kernel_module_read_file LSM hook is called after a kernel
+ * module has been read into memory and (if applicable) decompressed. It
+ * measures and/or appraises the module based on the IMA policy.
+ *
+ * Return: 0 on success, negative error code on failure.
+ */
+static int ima_read_kernel_module(struct file *file, char *buf, loff_t size)
+{
+	struct lsm_prop prop;
+
+	security_current_getlsmprop_subj(&prop);
+	return process_measurement(file, current_cred(), &prop, buf, size,
+				   MAY_READ, MODULE_CHECK);
+}
+
 static int __ima_inode_hash(struct inode *inode, struct file *file, char *buf,
 			    size_t buf_size)
 {
@@ -881,6 +902,10 @@ static int ima_post_read_file(struct file *file, char *buf, loff_t size,
 	enum ima_hooks func;
 	struct lsm_prop prop;
 
+	/* kernel module will be addressed in ima_read_kernel_module */
+	if (read_id == READING_MODULE)
+		return 0;
+
 	/* permit signed certs */
 	if (!file && read_id == READING_X509_CERTIFICATE)
 		return 0;
@@ -1250,6 +1275,7 @@ static struct security_hook_list ima_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(kernel_load_data, ima_load_data),
 	LSM_HOOK_INIT(kernel_post_load_data, ima_post_load_data),
 	LSM_HOOK_INIT(kernel_read_file, ima_read_file),
+	LSM_HOOK_INIT(kernel_module_read_file, ima_read_kernel_module),
 	LSM_HOOK_INIT(kernel_post_read_file, ima_post_read_file),
 	LSM_HOOK_INIT(path_post_mknod, ima_post_path_mknod),
 #ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS
security/integrity/ima/ima_policy.c +1 -1
index 128fab897930..2c9bdc618ac9 100644
@@ -241,7 +241,7 @@ static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
 
 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
 	{.action = APPRAISE, .func = MODULE_CHECK,
-	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
+	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED | IMA_CHECK_BLACKLIST},
 	{.action = APPRAISE, .func = FIRMWARE_CHECK,
 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
 	{.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
security/security.c +17 -0
index 4d3c03a4524c..311ba63a8889 100644
@@ -3442,6 +3442,23 @@ int security_kernel_post_read_file(struct file *file, char *buf, loff_t size,
 }
 EXPORT_SYMBOL_GPL(security_kernel_post_read_file);
 
+/**
+ * security_kernel_module_read_file() - Read a kernel module loaded by finit_module
+ * @file: file
+ * @buf: contents of decompressed kernel module
+ * @size: size of decompressed kernel module
+ *
+ * Read a kernel module loaded by the finit_module syscall. Unlike
+ * security_kernel_post_read_file, it has access to the decompressed kernel module.
+ *
+ * Return: Returns 0 if permission is granted.
+ */
+int security_kernel_module_read_file(struct file *file, char *buf, loff_t size)
+{
+	return call_int_hook(kernel_module_read_file, file, buf, size);
+}
+EXPORT_SYMBOL_GPL(security_kernel_module_read_file);
+
 /**
  * security_kernel_load_data() - Load data provided by userspace
  * @id: data identifier

base-commit: e53642b87a4f4b03a8d7e5f8507fc3cd0c595ea6

Signature · 1 line
2.51.0
maintainer
#21 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 25 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Quoted 7 lines
 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)

We don't really need a new LSM hook for this do we? Can't we just define a new file read type, e.g. READING_MODULE_DECOMPRESS, and do another call to security_kernel_post_read_file() after the module is unpacked? Something like the snippet below ...

kernel/module/main.c +0 -0
index c66b26184936..f127000d2e0a 100644
@@ -3693,6 +3693,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }

+
+ err = security_kernel_post_read_file(f,
+ (char *)info.hdr, info.len,
+ READING_MODULE_DECOMPRESS);
+ if (err) {
+ mod_stat_inc(&failed_kreads);
+ return err;
+ }

       } else {
               info.hdr = buf;
               info.len = len;
Signature · 1 line
paul-moore.com
maintainer
#22 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 34 lines · Paul Moore
Quoted 25 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Quoted 7 lines
 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)
Quoted 4 lines

We don't really need a new LSM hook for this do we? Can't we just define a new file read type, e.g. READING_MODULE_DECOMPRESS, and do another call to security_kernel_post_read_file() after the module is unpacked? Something like the snippet below ...

Yes, this is similar to my suggestion based on defining multiple enumerations:
READING_MODULE, READING_COMPRESSED_MODULE, and READING_DECOMPRESSED_MODULE. With this solution, IMA would need to make an exception in the post kernel module read for the READING_COMPRESSED_MODULE case, since the kernel module has not yet been decompressed.

Coiby suggested further simplification by moving the call later. At which point either there is or isn't an appended signature for non-compressed and decompressed kernel modules.

As long as you don't have a problem calling the security_kernel_post_read_file() hook again, could we move the call later and pass READING_MODULE_UNCOMPRESSED?

Quoted 11 lines
kernel/module/main.c +0 -0
index c66b26184936..f127000d2e0a 100644
@@ -3693,6 +3693,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
kernel/module/main.c +1 -0
+

kernel/module/main.c

Quoted 10 lines
kernel/module/main.c +7 -0
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE_DECOMPRESS);
+               if (err) {
+                       mod_stat_inc(&failed_kreads);
+                       return err;
+               }
       } else {
               info.hdr = buf;
               info.len = len;

== defer security_kernel_post_read_file() call to here ==

Mimi

maintainer
#23 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 46 lines · Mimi Zohar
Quoted 34 lines · Paul Moore
Quoted 25 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Quoted 7 lines
 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)
Quoted 4 lines

We don't really need a new LSM hook for this do we? Can't we just define a new file read type, e.g. READING_MODULE_DECOMPRESS, and do another call to security_kernel_post_read_file() after the module is unpacked? Something like the snippet below ...

Yes, this is similar to my suggestion based on defining multiple enumerations:
READING_MODULE, READING_COMPRESSED_MODULE, and READING_DECOMPRESSED_MODULE. With this solution, IMA would need to make an exception in the post kernel module read for the READING_COMPRESSED_MODULE case, since the kernel module has not yet been decompressed.

Quoted 6 lines

Coiby suggested further simplification by moving the call later. At which point either there is or isn't an appended signature for non-compressed and decompressed kernel modules.

As long as you don't have a problem calling the security_kernel_post_read_file() hook again, could we move the call later and pass READING_MODULE_UNCOMPRESSED?

It isn't clear from these comments if you are talking about moving only the second security_kernel_post_read_file() call that was proposed for init_module_from_file() to later in the function, leaving the call in kernel_read_file() intact, or something else?

I think we want to leave the hook calls in kernel_read_file() intact, in which case I'm not certain what advantage there is in moving the security_kernel_post_read_file() call to a location where it is called in init_module_from_file() regardless of if the module is compressed or not. In the uncompressed case you are calling the hook twice for no real benefit? It may be helpful to submit a patch with your proposal as a patch can be worth a thousand words ;)

Quoted 22 lines
Quoted 10 lines
kernel/module/main.c +0 -0
index c66b26184936..f127000d2e0a 100644
@@ -3693,6 +3693,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
kernel/module/main.c +1 -0
+

kernel/module/main.c

Quoted 10 lines
kernel/module/main.c +7 -0
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE_DECOMPRESS);
+               if (err) {
+                       mod_stat_inc(&failed_kreads);
+                       return err;
+               }
       } else {
               info.hdr = buf;
               info.len = len;

== defer security_kernel_post_read_file() call to here ==

Signature · 1 line
paul-moore.com
author
#24 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 51 lines · Paul Moore
Quoted 50 lines · Mimi Zohar
Quoted 34 lines · Paul Moore
Quoted 25 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Quoted 7 lines
 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)
Quoted 4 lines

We don't really need a new LSM hook for this do we? Can't we just define a new file read type, e.g. READING_MODULE_DECOMPRESS, and do another call to security_kernel_post_read_file() after the module is unpacked? Something like the snippet below ...

Yes, this is similar to my suggestion based on defining multiple enumerations:
READING_MODULE, READING_COMPRESSED_MODULE, and READING_DECOMPRESSED_MODULE. With this solution, IMA would need to make an exception in the post kernel module read for the READING_COMPRESSED_MODULE case, since the kernel module has not yet been decompressed.

Coiby suggested further simplification by moving the call later. At which point either there is or isn't an appended signature for non-compressed and decompressed kernel modules.

Quoted 7 lines
Quoted 2 lines

As long as you don't have a problem calling the security_kernel_post_read_file() hook again, could we move the call later and pass READING_MODULE_UNCOMPRESSED?

It isn't clear from these comments if you are talking about moving only the second security_kernel_post_read_file() call that was proposed for init_module_from_file() to later in the function, leaving the call in kernel_read_file() intact, or something else?

Hi Paul and Mimi,

Thanks for sharing your feedback! Yes, you are right, there is no need for a new LSM hook. Actually by not introducing a new LSM hook, we can have a much simpler solution!

Quoted 32 lines

I think we want to leave the hook calls in kernel_read_file() intact, in which case I'm not certain what advantage there is in moving the security_kernel_post_read_file() call to a location where it is called in init_module_from_file() regardless of if the module is compressed or not. In the uncompressed case you are calling the hook twice for no real benefit? It may be helpful to submit a patch with your proposal as a patch can be worth a thousand words ;)

Quoted 22 lines
Quoted 10 lines
kernel/module/main.c +0 -0
index c66b26184936..f127000d2e0a 100644
@@ -3693,6 +3693,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
kernel/module/main.c +1 -0
+

kernel/module/main.c

Quoted 10 lines
kernel/module/main.c +7 -0
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE_DECOMPRESS);
+               if (err) {
+                       mod_stat_inc(&failed_kreads);
+                       return err;
+               }
       } else {
               info.hdr = buf;
               info.len = len;

== defer security_kernel_post_read_file() call to here ==

By moving security_kernel_post_read_file, I think what Mimi means is to move security_kernel_post_read_file in init_module_from_file() to later in the function,

kernel/module/main.c +10 -1
index c66b261849362a..66725e53fef0c1 100644
@@ -3678,6 +3678,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 	struct load_info info = { };
 	void *buf = NULL;
 	int len;
+	int err;
 
 	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
 	if (len < 0) {
@@ -3686,7 +3687,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 	}
 
 	if (flags & MODULE_INIT_COMPRESSED_FILE) {
-		int err = module_decompress(&info, buf, len);
+		err = module_decompress(&info, buf, len);
 		vfree(buf); /* compressed data is no longer needed */
 		if (err) {
 			mod_stat_inc(&failed_decompress);
@@ -3698,6 +3699,14 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
 		info.len = len;
 	}
 
+	err = security_kernel_post_read_file(f, (char *)info.hdr, info.len,
+					     READING_MODULE);
+	if (err) {
+		mod_stat_inc(&failed_kreads);
+		free_copy(&info, flags);
+		return err;
+	}
+
 	return load_module(&info, uargs, flags);
 }
 

If we only call security_kernel_post_read_file the 2nd time for a decompressed kernel module, IMA won't be sure what to do when security_kernel_post_read_file is called for the 1st time because it can't distinguish between a compressed module with appended signature or a uncompressed module without appended signature. If it permits 1st calling security_kernel_post_read_file, a uncompressed module without appended signature can be loaded. If it doesn't permit 1st calling security_kernel_post_read_file, there is no change to call security_kernel_post_read_file again for decompressed module.

And you are right, there is no need to call security_kernel_post_read_file twice. And from the perspective of IMA, it simplifies reasoning if it is guaranteed that IMA will always access uncompressed kernel module regardless regardless of its original compression state.

So I think a better solution is to stop calling security_kernel_post_read_file in kernel_read_file for READING_MODULE. This can also avoiding introducing an unnecessary READING_MODULE_UNCOMPRESSED/READING_COMPRESSED_MODULE enumeration and can make the solution even simpler,

fs/kernel_read_file.c +6 -1
index de32c95d823dbd..7c78e84def6ec7 100644
@@ -107,7 +107,12 @@ ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
 			goto out_free;
 		}
 
-		ret = security_kernel_post_read_file(file, *buf, i_size, id);
+		/*
+		 * security_kernel_post_read_file will be called later after
+		 * a read kernel module is truly decompressed
+		 */
+		if (id != READING_MODULE)
+			ret = security_kernel_post_read_file(file, *buf, i_size, id);
 	}
 

Btw, I notice IMA is the only user of security_kernel_post_read_file so this change won't affect other LSMs. For a full patch, please visit
https://github.com/coiby/linux/commit/558d85779ab5d794874749ecfae0e48b890bf3e0.patch

If there are concerns that I'm unaware of and a new READING_MODULE_UNCOMPRESSED/READING_COMPRESSED_MODULE enumeration is necessary, here's another patch
https://github.com/coiby/linux/commit/cdd40317b6070f48ec871c6a89428084f38ca083.patch

Quoted 4 lines
Signature · 2 lines
-- 
paul-moore.com
Signature · 2 lines
Best regards,
Coiby
maintainer
#25 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 167 lines · Coiby Xu
Quoted 51 lines · Paul Moore
Quoted 50 lines · Mimi Zohar
Quoted 34 lines · Paul Moore
Quoted 25 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Quoted 7 lines
 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)
Quoted 4 lines

We don't really need a new LSM hook for this do we? Can't we just define a new file read type, e.g. READING_MODULE_DECOMPRESS, and do another call to security_kernel_post_read_file() after the module is unpacked? Something like the snippet below ...

Yes, this is similar to my suggestion based on defining multiple enumerations:
READING_MODULE, READING_COMPRESSED_MODULE, and READING_DECOMPRESSED_MODULE. With this solution, IMA would need to make an exception in the post kernel module read for the READING_COMPRESSED_MODULE case, since the kernel module has not yet been decompressed.

Coiby suggested further simplification by moving the call later. At which point either there is or isn't an appended signature for non-compressed and decompressed kernel modules.

Quoted 7 lines
Quoted 2 lines

As long as you don't have a problem calling the security_kernel_post_read_file() hook again, could we move the call later and pass READING_MODULE_UNCOMPRESSED?

It isn't clear from these comments if you are talking about moving only the second security_kernel_post_read_file() call that was proposed for init_module_from_file() to later in the function, leaving the call in kernel_read_file() intact, or something else?

Hi Paul and Mimi,

Thanks for sharing your feedback! Yes, you are right, there is no need for a new LSM hook. Actually by not introducing a new LSM hook, we can have a much simpler solution!

Quoted 32 lines

I think we want to leave the hook calls in kernel_read_file() intact, in which case I'm not certain what advantage there is in moving the security_kernel_post_read_file() call to a location where it is called in init_module_from_file() regardless of if the module is compressed or not. In the uncompressed case you are calling the hook twice for no real benefit? It may be helpful to submit a patch with your proposal as a patch can be worth a thousand words ;)

Quoted 22 lines
Quoted 10 lines
kernel/module/main.c +0 -0
index c66b26184936..f127000d2e0a 100644
@@ -3693,6 +3693,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
kernel/module/main.c +1 -0
+

kernel/module/main.c

Quoted 10 lines
kernel/module/main.c +7 -0
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE_DECOMPRESS);
+               if (err) {
+                       mod_stat_inc(&failed_kreads);
+                       return err;
+               }
       } else {
               info.hdr = buf;
               info.len = len;

== defer security_kernel_post_read_file() call to here ==

By moving security_kernel_post_read_file, I think what Mimi means is to move security_kernel_post_read_file in init_module_from_file() to later in the function,

kernel/module/main.c +10 -1
index c66b261849362a..66725e53fef0c1 100644
@@ -3678,6 +3678,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
        struct load_info info = { };
        void *buf = NULL;
        int len;
+       int err;
 
        len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
        if (len < 0) {
@@ -3686,7 +3687,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
        }
 
        if (flags & MODULE_INIT_COMPRESSED_FILE) {
-               int err = module_decompress(&info, buf, len);
+               err = module_decompress(&info, buf, len);
                vfree(buf); /* compressed data is no longer needed */
                if (err) {
                        mod_stat_inc(&failed_decompress);
@@ -3698,6 +3699,14 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
                info.len = len;
        }
 
+       err = security_kernel_post_read_file(f, (char *)info.hdr, info.len,
+                                            READING_MODULE);
+       if (err) {
+               mod_stat_inc(&failed_kreads);
+               free_copy(&info, flags);
+               return err;
+       }
+
        return load_module(&info, uargs, flags);
  }
 

If we only call security_kernel_post_read_file the 2nd time for a decompressed kernel module, IMA won't be sure what to do when security_kernel_post_read_file is called for the 1st time because it can't distinguish between a compressed module with appended signature or a uncompressed module without appended signature. If it permits 1st calling security_kernel_post_read_file, a uncompressed module without appended signature can be loaded. If it doesn't permit 1st calling security_kernel_post_read_file, there is no change to call security_kernel_post_read_file again for decompressed module.

And you are right, there is no need to call security_kernel_post_read_file twice. And from the perspective of IMA, it simplifies reasoning if it is guaranteed that IMA will always access uncompressed kernel module regardless regardless of its original compression state.

So I think a better solution is to stop calling security_kernel_post_read_file in kernel_read_file for READING_MODULE. This can also avoiding introducing an unnecessary READING_MODULE_UNCOMPRESSED/READING_COMPRESSED_MODULE enumeration and can make the solution even simpler,

fs/kernel_read_file.c +0 -0
index de32c95d823dbd..7c78e84def6ec7 100644
@@ -107,7 +107,12 @@ ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
                        goto out_free;

fs/kernel_read_file.c ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf, · line 116

Quoted 10 lines
fs/kernel_read_file.c +6 -1
                }
 
-               ret = security_kernel_post_read_file(file, *buf, i_size, id);
+               /*
+                * security_kernel_post_read_file will be called later after
+                * a read kernel module is truly decompressed
+                */
+               if (id != READING_MODULE)
+                       ret = security_kernel_post_read_file(file, *buf, i_size, id);
        }

Assuming I'm understanding the problem correctly, I think you're making this harder than it needs to be. I believe something like this should solve the problem without having to add more conditionals around the hooks in kernel_read_file(), and limiting the multiple security_kernel_post_read_file() calls to just the compressed case ... and honestly in each of the _post_read_file() calls in the compressed case, the buffer contents have changed so it somewhat makes sense.

Given the code below, IMA could simply ignore the READING_MODULE_COMPRESSED case (or whatever it is the IMA needs to do in that case) and focus on the READING_MODULE case as it does today. I expect the associated IMA patch would be both trivial and small.

kernel/module/main.c +0 -0
index c66b26184936..b435c498ec01 100644
@@ -3675,17 +3675,19 @@ static int idempotent_wait_for_completion(struct idempot

ent *u)

static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
{

+       bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
       struct load_info info = { };
       void *buf = NULL;
       int len;

- len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
+ len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+ compressed ? READING_MODULE_COMPRESSED : READING_

MODULE);
       if (len < 0) {
               mod_stat_inc(&failed_kreads);
               return len;
       }

- if (flags & MODULE_INIT_COMPRESSED_FILE) {

+       if (compressed) {
               int err = module_decompress(&info, buf, len);
               vfree(buf); /* compressed data is no longer needed */
               if (err) {
@@ -3693,6 +3695,14 @@ static int init_module_from_file(struct file *f, const ch
ar __user * uargs, int
                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }

+
+ err = security_kernel_post_read_file(f,
+ (char *)info.hdr, info.len,
+ READING_MODULE);
+ if (err) {
+ mod_stat_inc(&failed_kreads);
+ return err;
+ }

       } else {
               info.hdr = buf;
               info.len = len;
Signature · 1 line
paul-moore.com
maintainer
#26 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 43 lines · Paul Moore

Assuming I'm understanding the problem correctly, I think you're making this harder than it needs to be.  I believe something like this should solve the problem without having to add more conditionals around the hooks in kernel_read_file(), and limiting the multiple security_kernel_post_read_file() calls to just the compressed case ... and honestly in each of the _post_read_file() calls in the compressed case, the buffer contents have changed so it somewhat makes sense.

Given the code below, IMA could simply ignore the READING_MODULE_COMPRESSED case (or whatever it is the IMA needs to do in that case) and focus on the READING_MODULE case as it does today. I expect the associated IMA patch would be both trivial and small.

kernel/module/main.c +0 -0
index c66b26184936..b435c498ec01 100644
@@ -3675,17 +3675,19 @@ static int idempotent_wait_for_completion(struct idempot
 ent *u)
 

static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
{
+       bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE); struct load_info info = { };
       void *buf = NULL;
       int len;

-       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE); +       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+                              compressed ? READING_MODULE_COMPRESSED : READING_ MODULE);
       if (len < 0) {
               mod_stat_inc(&failed_kreads);
               return len;
       }

-       if (flags & MODULE_INIT_COMPRESSED_FILE) {
+       if (compressed) {
               int err = module_decompress(&info, buf, len); vfree(buf); /* compressed data is no longer needed */ if (err) {

kernel/module/main.c +0 -0
@@ -3693,6 +3695,14 @@ static int init_module_from_file(struct file *f, const ch
Quoted 8 lines

ar __user * uargs, int
                       mod_stat_add_long(len, &invalid_decompress_bytes); return err;
               }
+
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len, +                                                    READING_MODULE);

Without changing the enumeration here, IMA would not be able to differentiate the first call to security_kernel_post_read_file() and this one. The first call would result in unnecessary error messages.

Adding an additional call to security_kernel_post_read_file() here, would require defining 2 additional enumerations: READING_MODULE_COMPRESSED, READING_MODULE_DECOMPRESSED.

Quoted 7 lines

+               if (err) {
+                       mod_stat_inc(&failed_kreads); +                       return err;
+               }
       } else {
               info.hdr = buf;
               info.len = len;

Deferring the security_kernel_post_read_file() call to here, eliminates the need for defining additional enumerations. (Coiby's first link.)

Adding an additional call to security_kernel_post_read_file() here, requires 1 additional enumeration. (Coiby's 2nd link.)

Mimi

maintainer
#27 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 48 lines · Mimi Zohar
Quoted 47 lines · Paul Moore

Assuming I'm understanding the problem correctly, I think you're making this harder than it needs to be. I believe something like this should solve the problem without having to add more conditionals around the hooks in kernel_read_file(), and limiting the multiple security_kernel_post_read_file() calls to just the compressed case ... and honestly in each of the _post_read_file() calls in the compressed case, the buffer contents have changed so it somewhat makes sense.

Given the code below, IMA could simply ignore the READING_MODULE_COMPRESSED case (or whatever it is the IMA needs to do in that case) and focus on the READING_MODULE case as it does today. I expect the associated IMA patch would be both trivial and small.

kernel/module/main.c +0 -0
index c66b26184936..b435c498ec01 100644
@@ -3675,17 +3675,19 @@ static int idempotent_wait_for_completion(struct idempot
 ent *u)
 

static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
{

kernel/module/main.c +3 -1
+       bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
       struct load_info info = { };
       void *buf = NULL;
       int len;
 
-       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
+       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+                              compressed ? READING_MODULE_COMPRESSED : READING_
MODULE);
       if (len < 0) {
               mod_stat_inc(&failed_kreads);
               return len;
       }
kernel/module/main.c +1 -1
-       if (flags & MODULE_INIT_COMPRESSED_FILE) {
+       if (compressed) {
               int err = module_decompress(&info, buf, len);
               vfree(buf); /* compressed data is no longer needed */
               if (err) {
@@ -3693,6 +3695,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
Quoted 8 lines

kernel/module/main.c

Quoted 4 lines
kernel/module/main.c +4 -0
+
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE);

Without changing the enumeration here, IMA would not be able to differentiate the first call to security_kernel_post_read_file() and this one. The first call would result in unnecessary error messages.

Given the patch snippet above, in the case where an uncompressed module is passed into init_module_from_file() there would be the following checks, in this order:

* kernel_read_file()
-> security_kernel_read_file(READING_MODULE) -> security_kernel_post_read_file(READING_MODULE)
* init_module_from_file()
-> NONE

... this should be the same as the current behavior.

In the case where a compressed module is passed into init_module_from_file() there would be the following checks, in this order:

* kernel_read_file()
-> security_kernel_read_file(READING_MODULE_COMPRESSED) -> security_kernel_post_read_file(READING_MODULE_COMPRESSED)
* init_module_from_file()
-> security_kernel_post_read_file(READING_MODULE)

... the two differences being that the hook calls in kernel_read_file() use the READING_MODULE_COMPRESSED id, which seems appropriate as the data passed to the hook is the compressed representation, and the additional _post_read_file() hook call in init_module_from_file() using the READING_MODULE id, as the data passed to the hook is now uncompressed. Not only should IMA be able to easily differentiate between the two _post_read_file() calls, but it should have access to both the compressed and uncompressed data.

Signature · 1 line
paul-moore.com
maintainer
#28 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 80 lines · Paul Moore
Quoted 48 lines · Mimi Zohar
Quoted 47 lines · Paul Moore

Assuming I'm understanding the problem correctly, I think you're making this harder than it needs to be. I believe something like this should solve the problem without having to add more conditionals around the hooks in kernel_read_file(), and limiting the multiple security_kernel_post_read_file() calls to just the compressed case ... and honestly in each of the _post_read_file() calls in the compressed case, the buffer contents have changed so it somewhat makes sense.

Given the code below, IMA could simply ignore the READING_MODULE_COMPRESSED case (or whatever it is the IMA needs to do in that case) and focus on the READING_MODULE case as it does today. I expect the associated IMA patch would be both trivial and small.

kernel/module/main.c +0 -0
index c66b26184936..b435c498ec01 100644
@@ -3675,17 +3675,19 @@ static int idempotent_wait_for_completion(struct idempot
 ent *u)
 

static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
{

kernel/module/main.c +3 -1
+       bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
       struct load_info info = { };
       void *buf = NULL;
       int len;
 
-       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
+       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+                              compressed ? READING_MODULE_COMPRESSED : READING_
MODULE);
       if (len < 0) {
               mod_stat_inc(&failed_kreads);
               return len;
       }
kernel/module/main.c +1 -1
-       if (flags & MODULE_INIT_COMPRESSED_FILE) {
+       if (compressed) {
               int err = module_decompress(&info, buf, len);
               vfree(buf); /* compressed data is no longer needed */
               if (err) {
@@ -3693,6 +3695,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
Quoted 8 lines

kernel/module/main.c

Quoted 4 lines
kernel/module/main.c +4 -0
+
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE);

Without changing the enumeration here, IMA would not be able to differentiate the first call to security_kernel_post_read_file() and this one. The first call would result in unnecessary error messages.

Given the patch snippet above, in the case where an uncompressed module is passed into init_module_from_file() there would be the following checks, in this order:

* kernel_read_file()
-> security_kernel_read_file(READING_MODULE) -> security_kernel_post_read_file(READING_MODULE)
* init_module_from_file()
-> NONE

... this should be the same as the current behavior.

In the case where a compressed module is passed into init_module_from_file() there would be the following checks, in this order:

* kernel_read_file()
-> security_kernel_read_file(READING_MODULE_COMPRESSED) -> security_kernel_post_read_file(READING_MODULE_COMPRESSED)
* init_module_from_file()
-> security_kernel_post_read_file(READING_MODULE)

Quoted 8 lines

... the two differences being that the hook calls in kernel_read_file() use the READING_MODULE_COMPRESSED id, which seems appropriate as the data passed to the hook is the compressed representation, and the additional _post_read_file() hook call in init_module_from_file() using the READING_MODULE id, as the data passed to the hook is now uncompressed. Not only should IMA be able to easily differentiate between the two _post_read_file() calls, but it should have access to both the compressed and uncompressed data.

Thanks, Paul. Yes, a single additional enumeration is enough.

Mimi

author
#29 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 90 lines · Mimi Zohar
Quoted 80 lines · Paul Moore
Quoted 48 lines · Mimi Zohar
Quoted 47 lines · Paul Moore

Assuming I'm understanding the problem correctly, I think you're making this harder than it needs to be. I believe something like this should solve the problem without having to add more conditionals around the hooks in kernel_read_file(), and limiting the multiple security_kernel_post_read_file() calls to just the compressed case ... and honestly in each of the _post_read_file() calls in the compressed case, the buffer contents have changed so it somewhat makes sense.

Given the code below, IMA could simply ignore the READING_MODULE_COMPRESSED case (or whatever it is the IMA needs to do in that case) and focus on the READING_MODULE case as it does today. I expect the associated IMA patch would be both trivial and small.

kernel/module/main.c +0 -0
index c66b26184936..b435c498ec01 100644
@@ -3675,17 +3675,19 @@ static int idempotent_wait_for_completion(struct idempot
 ent *u)
 

static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
{

kernel/module/main.c +3 -1
+       bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
       struct load_info info = { };
       void *buf = NULL;
       int len;
 
-       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
+       len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+                              compressed ? READING_MODULE_COMPRESSED : READING_
MODULE);
       if (len < 0) {
               mod_stat_inc(&failed_kreads);
               return len;
       }
kernel/module/main.c +1 -1
-       if (flags & MODULE_INIT_COMPRESSED_FILE) {
+       if (compressed) {
               int err = module_decompress(&info, buf, len);
               vfree(buf); /* compressed data is no longer needed */
               if (err) {
@@ -3693,6 +3695,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
Quoted 8 lines

kernel/module/main.c

Quoted 4 lines
kernel/module/main.c +4 -0
+
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE);

Without changing the enumeration here, IMA would not be able to differentiate the first call to security_kernel_post_read_file() and this one. The first call would result in unnecessary error messages.

Given the patch snippet above, in the case where an uncompressed module is passed into init_module_from_file() there would be the following checks, in this order:

* kernel_read_file()
-> security_kernel_read_file(READING_MODULE) -> security_kernel_post_read_file(READING_MODULE)
* init_module_from_file()
-> NONE

... this should be the same as the current behavior.

In the case where a compressed module is passed into init_module_from_file() there would be the following checks, in this order:

* kernel_read_file()
-> security_kernel_read_file(READING_MODULE_COMPRESSED) -> security_kernel_post_read_file(READING_MODULE_COMPRESSED)
* init_module_from_file()
-> security_kernel_post_read_file(READING_MODULE)

Quoted 8 lines

... the two differences being that the hook calls in kernel_read_file() use the READING_MODULE_COMPRESSED id, which seems appropriate as the data passed to the hook is the compressed representation, and the additional _post_read_file() hook call in init_module_from_file() using the READING_MODULE id, as the data passed to the hook is now uncompressed. Not only should IMA be able to easily differentiate between the two _post_read_file() calls, but it should have access to both the compressed and uncompressed data.

Quoted 1 line

Thanks, Paul. Yes, a single additional enumeration is enough.

Yeah, thank Paul for elaborating on the solution!

Quoted 3 lines

Mimi

Signature · 2 lines
Best regards,
Coiby
maintainer
#30 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

In reply to Coiby Xu
Quoted 182 lines · Coiby Xu
Quoted 51 lines · Paul Moore
Quoted 50 lines · Mimi Zohar
Quoted 34 lines · Paul Moore
Quoted 25 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new LSM hook security_kernel_module_read_file which will be called after kernel module decompression is done so IMA can access the decompressed kernel module to verify the appended signature.

Since IMA can access both xattr and appended kernel module signature with the new LSM hook, it no longer uses the security_kernel_post_read_file LSM hook for kernel module loading.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
v1: https://lore.kernel.org/linux-integrity/[email protected]/

Quoted 7 lines
 include/linux/lsm_hook_defs.h       |  2 ++
 include/linux/security.h            |  7 +++++++
 kernel/module/main.c                | 10 +++++++++-
 security/integrity/ima/ima_main.c   | 26 ++++++++++++++++++++++++++
 security/integrity/ima/ima_policy.c |  2 +-
 security/security.c                 | 17 +++++++++++++++++
 6 files changed, 62 insertions(+), 2 deletions(-)
Quoted 4 lines

We don't really need a new LSM hook for this do we? Can't we just define a new file read type, e.g. READING_MODULE_DECOMPRESS, and do another call to security_kernel_post_read_file() after the module is unpacked? Something like the snippet below ...

Yes, this is similar to my suggestion based on defining multiple enumerations:
READING_MODULE, READING_COMPRESSED_MODULE, and READING_DECOMPRESSED_MODULE. With this solution, IMA would need to make an exception in the post kernel module read for the READING_COMPRESSED_MODULE case, since the kernel module has not yet been decompressed.

Coiby suggested further simplification by moving the call later. At which point either there is or isn't an appended signature for non-compressed and decompressed kernel modules.

Quoted 7 lines
Quoted 2 lines

As long as you don't have a problem calling the security_kernel_post_read_file() hook again, could we move the call later and pass READING_MODULE_UNCOMPRESSED?

It isn't clear from these comments if you are talking about moving only the second security_kernel_post_read_file() call that was proposed for init_module_from_file() to later in the function, leaving the call in kernel_read_file() intact, or something else?

Hi Paul and Mimi,

Thanks for sharing your feedback! Yes, you are right, there is no need for a new LSM hook. Actually by not introducing a new LSM hook, we can have a much simpler solution!

Quoted 32 lines

I think we want to leave the hook calls in kernel_read_file() intact, in which case I'm not certain what advantage there is in moving the security_kernel_post_read_file() call to a location where it is called in init_module_from_file() regardless of if the module is compressed or not. In the uncompressed case you are calling the hook twice for no real benefit? It may be helpful to submit a patch with your proposal as a patch can be worth a thousand words ;)

Quoted 22 lines
Quoted 10 lines
kernel/module/main.c +0 -0
index c66b26184936..f127000d2e0a 100644
@@ -3693,6 +3693,14 @@ static int init_module_from_file(struct file *f, const ch

ar __user * uargs, int

                       mod_stat_add_long(len, &invalid_decompress_bytes);
                       return err;
               }
kernel/module/main.c +1 -0
+

kernel/module/main.c

Quoted 10 lines
kernel/module/main.c +7 -0
+               err = security_kernel_post_read_file(f,
+                                                    (char *)info.hdr, info.len,
+                                                    READING_MODULE_DECOMPRESS);
+               if (err) {
+                       mod_stat_inc(&failed_kreads);
+                       return err;
+               }
       } else {
               info.hdr = buf;
               info.len = len;

== defer security_kernel_post_read_file() call to here ==

By moving security_kernel_post_read_file, I think what Mimi means is to move security_kernel_post_read_file in init_module_from_file() to later in the function,

kernel/module/main.c +10 -1
index c66b261849362a..66725e53fef0c1 100644
@@ -3678,6 +3678,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
  	struct load_info info = { };
  	void *buf = NULL;
  	int len;
+	int err;
 
  	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
  	if (len < 0) {
@@ -3686,7 +3687,7 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
  	}
 
  	if (flags & MODULE_INIT_COMPRESSED_FILE) {
-		int err = module_decompress(&info, buf, len);
+		err = module_decompress(&info, buf, len);
  		vfree(buf); /* compressed data is no longer needed */
  		if (err) {
  			mod_stat_inc(&failed_decompress);
@@ -3698,6 +3699,14 @@ static int init_module_from_file(struct file *f, const char __user * uargs, int
  		info.len = len;
  	}
 
+	err = security_kernel_post_read_file(f, (char *)info.hdr, info.len,
+					     READING_MODULE);
+	if (err) {
+		mod_stat_inc(&failed_kreads);
+		free_copy(&info, flags);
+		return err;
+	}
+
  	return load_module(&info, uargs, flags);
  }
 

If we only call security_kernel_post_read_file the 2nd time for a decompressed kernel module, IMA won't be sure what to do when security_kernel_post_read_file is called for the 1st time because it can't distinguish between a compressed module with appended signature or a uncompressed module without appended signature. If it permits 1st calling security_kernel_post_read_file, a uncompressed module without appended signature can be loaded. If it doesn't permit 1st calling security_kernel_post_read_file, there is no change to call security_kernel_post_read_file again for decompressed module.

And you are right, there is no need to call security_kernel_post_read_file twice. And from the perspective of IMA, it simplifies reasoning if it is guaranteed that IMA will always access uncompressed kernel module regardless regardless of its original compression state.

So I think a better solution is to stop calling security_kernel_post_read_file in kernel_read_file for READING_MODULE. This can also avoiding introducing an unnecessary READING_MODULE_UNCOMPRESSED/READING_COMPRESSED_MODULE enumeration and can make the solution even simpler,

fs/kernel_read_file.c +6 -1
index de32c95d823dbd..7c78e84def6ec7 100644
@@ -107,7 +107,12 @@ ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
  			goto out_free;
  		}
 
-		ret = security_kernel_post_read_file(file, *buf, i_size, id);
+		/*
+		 * security_kernel_post_read_file will be called later after
+		 * a read kernel module is truly decompressed
+		 */
+		if (id != READING_MODULE)
+			ret = security_kernel_post_read_file(file, *buf, i_size, id);
  	}
 

Btw, I notice IMA is the only user of security_kernel_post_read_file so this change won't affect other LSMs. For a full patch, please visit
https://github.com/coiby/linux/commit/558d85779ab5d794874749ecfae0e48b890bf3e0.patch

Quoted 4 lines

If there are concerns that I'm unaware of and a new READING_MODULE_UNCOMPRESSED/READING_COMPRESSED_MODULE enumeration is necessary, here's another patch
https://github.com/coiby/linux/commit/cdd40317b6070f48ec871c6a89428084f38ca083.patch

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

thanks,

Mimi

author
#31 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 1 line · Mimi Zohar

On Wed, 2025-11-05 at 08:18 +0800, Coiby Xu wrote:

[...]

Quoted 9 lines

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

Quoted 7 lines

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

Hi Mimi,

Thanks for summarizing your conversation with Paul! I can confirm Paul's approach works
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

While testing the patch today, I realized there is another issue/challenge introduced by in-kernel module decompression. IMA appraisal is to verify the digest of compressed kernel module but currently the passed buffer is uncompressed module. When IMA uses uncompressed module data to calculate the digest, xattr signature verification will fail. If we always make IMA read the original kernel module data again to calculate the digest, does it look like a quick-and-dirty fix? If we can assume people won't load kernel module so often, the performance impact is negligible. Otherwise we may have to introduce a new LSM hook so IMA can access uncompressed and original module data one time.

Quoted 5 lines

thanks,

Mimi

Signature · 2 lines
Best regards,
Coiby
maintainer
#32 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 26 lines · Coiby Xu
Quoted 1 line · Mimi Zohar

On Wed, 2025-11-05 at 08:18 +0800, Coiby Xu wrote:

[...]

Quoted 9 lines

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

Quoted 7 lines

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

Hi Mimi,

Thanks for summarizing your conversation with Paul! I can confirm Paul's approach works
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Quoted 11 lines

While testing the patch today, I realized there is another issue/challenge introduced by in-kernel module decompression. IMA appraisal is to verify the digest of compressed kernel module but currently the passed buffer is uncompressed module. When IMA uses uncompressed module data to calculate the digest, xattr signature verification will fail. If we always make IMA read the original kernel module data again to calculate the digest, does it look like a quick-and-dirty fix? If we can assume people won't load kernel module so often, the performance impact is negligible. Otherwise we may have to introduce a new LSM hook so IMA can access uncompressed and original module data one time.

ima_collect_measurement() stores the file hash info in the iint and uses that information to verify the signature as stored in the security xattr. Decompressing the kernel module shouldn't affect the xattr signature verification.

The patch with a few minor changes looks good:

- READDING_MODULE_CHECK -> READING_MODULE_CHECK
- Fix the enumeration name in ima_main.c
- scripts/checkpatch.pl code/comment line length has been relaxed to 100 chars, but the section "Breaking long lines and strings" in Documentation/process/coding-style.rst still recommends 80 characters.

There are cases where it is necessary to go over the 80 char line limit for readability, but in general both Roberto and I prefer, as much as possible, to limit the line length to 80 char. To detect where/when the line limit is greater than 80 chars, use the scripts/checkpatch.pl "--max-line-length=80" option.

After fixing the patch, please post it to linux-integrity mailing list.

Signature · 3 lines
thanks,

Mimi
maintainer
#33 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 39 lines · Mimi Zohar
Quoted 26 lines · Coiby Xu
Quoted 1 line · Mimi Zohar

On Wed, 2025-11-05 at 08:18 +0800, Coiby Xu wrote:

[...]

Quoted 9 lines

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

Quoted 7 lines

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

Hi Mimi,

Thanks for summarizing your conversation with Paul! I can confirm Paul's approach works
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Quoted 11 lines

While testing the patch today, I realized there is another issue/challenge introduced by in-kernel module decompression. IMA appraisal is to verify the digest of compressed kernel module but currently the passed buffer is uncompressed module. When IMA uses uncompressed module data to calculate the digest, xattr signature verification will fail. If we always make IMA read the original kernel module data again to calculate the digest, does it look like a quick-and-dirty fix? If we can assume people won't load kernel module so often, the performance impact is negligible. Otherwise we may have to introduce a new LSM hook so IMA can access uncompressed and original module data one time.

Quoted 4 lines

ima_collect_measurement() stores the file hash info in the iint and uses that information to verify the signature as stored in the security xattr. Decompressing the kernel module shouldn't affect the xattr signature verification.

In the case when the compressed kernel module hasn't previously been measured or appraised before loading the kernel module, we need to "collect" the file data hash on READING_MODULE_COMPRESSED, but defer appraising/measuring it.

An alternative to your suggestion of re-reading the original kernel module data to calculate the digest or defining a new hook, would be to define "collect" as a new "action" and pass the kernel_read_file_id enumeration to process_measurement(). IMA_COLLECTED already exists. Only IMA_COLLECT would need to be defined. The new collect "action" should be limited to func=MODULE_CHECK.

The downside of this alternative is that it requires a new collect rule:
collect func=MODULE_CHECK mask=MAY_READ uid=0
appraise func=MODULE_CHECK appraise_type=imasig|modsig

Signature · 3 lines
thanks,

Mimi
author
#34 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 56 lines · Mimi Zohar
Quoted 39 lines · Mimi Zohar
Quoted 26 lines · Coiby Xu
Quoted 1 line · Mimi Zohar

On Wed, 2025-11-05 at 08:18 +0800, Coiby Xu wrote:

[...]

Quoted 9 lines

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

Quoted 7 lines

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

Hi Mimi,

Thanks for summarizing your conversation with Paul! I can confirm Paul's approach works
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Quoted 11 lines

While testing the patch today, I realized there is another issue/challenge introduced by in-kernel module decompression. IMA appraisal is to verify the digest of compressed kernel module but currently the passed buffer is uncompressed module. When IMA uses uncompressed module data to calculate the digest, xattr signature verification will fail. If we always make IMA read the original kernel module data again to calculate the digest, does it look like a quick-and-dirty fix? If we can assume people won't load kernel module so often, the performance impact is negligible. Otherwise we may have to introduce a new LSM hook so IMA can access uncompressed and original module data one time.

Quoted 4 lines

ima_collect_measurement() stores the file hash info in the iint and uses that information to verify the signature as stored in the security xattr. Decompressing the kernel module shouldn't affect the xattr signature verification.

In the case when the compressed kernel module hasn't previously been measured or appraised before loading the kernel module, we need to "collect" the file data hash on READING_MODULE_COMPRESSED, but defer appraising/measuring it.

An alternative to your suggestion of re-reading the original kernel module data to calculate the digest or defining a new hook, would be to define "collect" as a new "action" and pass the kernel_read_file_id enumeration to process_measurement(). IMA_COLLECTED already exists. Only IMA_COLLECT would need to be defined. The new collect "action" should be limited to func=MODULE_CHECK.

Quoted 3 lines

The downside of this alternative is that it requires a new collect rule:
collect func=MODULE_CHECK mask=MAY_READ uid=0
appraise func=MODULE_CHECK appraise_type=imasig|modsig

Thank for suggesting an alternative! I've implemented the idea in
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_collect

Note besides a new collect rule, another change is needed. Currently, process_measurement only accepts enum ima_hooks thus it can't tell if it's READING_MODULE_COMPRESSED so to only do collect action. So I create a fake MODULE_COMPRESSED_CHECK func.

And for the idea of re-reading the original kernel module data, it has
been implemented in https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Both branches have applied your requested three changes including respecting the 80 char line limit. Additionally, I made a change to the IPE LSM because of the new READING_MODULE_COMPRESSED kernel_read_file_id enumerate.

After comparing the two implementations, personally I prefer re-reading the original kernel module data because the change is smaller and it's more user-friendly. But if there are other reasons I don't know, I'll post the patches of the new collect action approach to the mailing list.

Signature · 2 lines
Best regards,
Coiby
maintainer
#35 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 57 lines · Coiby Xu
Quoted 56 lines · Mimi Zohar
Quoted 39 lines · Mimi Zohar
Quoted 26 lines · Coiby Xu
Quoted 1 line · Mimi Zohar

On Wed, 2025-11-05 at 08:18 +0800, Coiby Xu wrote:

[...]

Quoted 9 lines

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

Quoted 7 lines

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

Hi Mimi,

Thanks for summarizing your conversation with Paul! I can confirm Paul's approach works
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Quoted 11 lines

While testing the patch today, I realized there is another issue/challenge introduced by in-kernel module decompression. IMA appraisal is to verify the digest of compressed kernel module but currently the passed buffer is uncompressed module. When IMA uses uncompressed module data to calculate the digest, xattr signature verification will fail. If we always make IMA read the original kernel module data again to calculate the digest, does it look like a quick-and-dirty fix? If we can assume people won't load kernel module so often, the performance impact is negligible. Otherwise we may have to introduce a new LSM hook so IMA can access uncompressed and original module data one time.

Quoted 4 lines

ima_collect_measurement() stores the file hash info in the iint and uses that information to verify the signature as stored in the security xattr. Decompressing the kernel module shouldn't affect the xattr signature verification.

In the case when the compressed kernel module hasn't previously been measured or appraised before loading the kernel module, we need to "collect" the file data hash on READING_MODULE_COMPRESSED, but defer appraising/measuring it.

An alternative to your suggestion of re-reading the original kernel module data to calculate the digest or defining a new hook, would be to define "collect" as a new "action" and pass the kernel_read_file_id enumeration to process_measurement(). IMA_COLLECTED already exists. Only IMA_COLLECT would need to be defined. The new collect "action" should be limited to func=MODULE_CHECK.

Quoted 3 lines
Quoted 3 lines

The downside of this alternative is that it requires a new collect rule:
collect func=MODULE_CHECK mask=MAY_READ uid=0
appraise func=MODULE_CHECK appraise_type=imasig|modsig

As it turns out, the "collect" rule is unnecessary. On READING_MODULE_COMPRESSED, process_measurement() should calculate the compressed file hash. Extending the IMA measurement list and verifying the signature can then be differed to READING_MODULE.

Quoted 8 lines

Thank for suggesting an alternative! I've implemented the idea in
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_collect

Note besides a new collect rule, another change is needed. Currently, process_measurement only accepts enum ima_hooks thus it can't tell if it's READING_MODULE_COMPRESSED so to only do collect action. So I create a fake MODULE_COMPRESSED_CHECK func.

Correct, either extending process_measurement() with the read_idmap enum or defining the fake hook would work.

Quoted 10 lines

And for the idea of re-reading the original kernel module data, it has been implemented in
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Both branches have applied your requested three changes including respecting the 80 char line limit. Additionally, I made a change to the IPE LSM because of the new READING_MODULE_COMPRESSED kernel_read_file_id enumerate.

Quoted 4 lines

After comparing the two implementations, personally I prefer re-reading the original kernel module data because the change is smaller and it's more user-friendly. But if there are other reasons I don't know, I'll post the patches of the new collect action approach to the mailing list.

The "re-reading" option fails some of the tests. As the "collect" rule isn't needed, let's stick with the first option.

Signature · 3 lines
thanks,

Mimi
author
#36 lore raw

Re: [PATCH v2] lsm,ima: new LSM hook security_kernel_module_read_file to access decompressed kernel module

Quoted 89 lines · Mimi Zohar
Quoted 57 lines · Coiby Xu
Quoted 56 lines · Mimi Zohar
Quoted 39 lines · Mimi Zohar
Quoted 26 lines · Coiby Xu
Quoted 1 line · Mimi Zohar

On Wed, 2025-11-05 at 08:18 +0800, Coiby Xu wrote:

[...]

Quoted 9 lines

Hi Coiby,

Based on the conversation with Paul, there is no reason to remove the existing security_kernel_post_read_file() call.

The changes are similar to the 2nd link, but a bit different.
- Define a single enumeration named READING_MODULE_COMPRESSED.

Quoted 7 lines

- In module/main.c add a new security_kernel_post_read_file() call immediately after decompressing the kernel module. Like a previous version of this patch, call kernel_read_file() with either READING_MODULE or READING_MODULE_COMPRESSED based on MODULE_INIT_COMPRESSED_FILE.

- In ima_post_read_file() defer verifying the signature when the enumeration is READING_MODULE_COMPRESSED. (No need for a new function ima_read_kernel_module.)

Hi Mimi,

Thanks for summarizing your conversation with Paul! I can confirm Paul's approach works
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Quoted 11 lines

While testing the patch today, I realized there is another issue/challenge introduced by in-kernel module decompression. IMA appraisal is to verify the digest of compressed kernel module but currently the passed buffer is uncompressed module. When IMA uses uncompressed module data to calculate the digest, xattr signature verification will fail. If we always make IMA read the original kernel module data again to calculate the digest, does it look like a quick-and-dirty fix? If we can assume people won't load kernel module so often, the performance impact is negligible. Otherwise we may have to introduce a new LSM hook so IMA can access uncompressed and original module data one time.

Quoted 4 lines

ima_collect_measurement() stores the file hash info in the iint and uses that information to verify the signature as stored in the security xattr. Decompressing the kernel module shouldn't affect the xattr signature verification.

In the case when the compressed kernel module hasn't previously been measured or appraised before loading the kernel module, we need to "collect" the file data hash on READING_MODULE_COMPRESSED, but defer appraising/measuring it.

An alternative to your suggestion of re-reading the original kernel module data to calculate the digest or defining a new hook, would be to define "collect" as a new "action" and pass the kernel_read_file_id enumeration to process_measurement(). IMA_COLLECTED already exists. Only IMA_COLLECT would need to be defined. The new collect "action" should be limited to func=MODULE_CHECK.

Quoted 3 lines
Quoted 3 lines

The downside of this alternative is that it requires a new collect rule:
collect func=MODULE_CHECK mask=MAY_READ uid=0
appraise func=MODULE_CHECK appraise_type=imasig|modsig

As it turns out, the "collect" rule is unnecessary. On READING_MODULE_COMPRESSED, process_measurement() should calculate the compressed file hash. Extending the IMA measurement list and verifying the signature can then be differed to READING_MODULE.

Quoted 8 lines

Thank for suggesting an alternative! I've implemented the idea in
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_collect

Note besides a new collect rule, another change is needed. Currently, process_measurement only accepts enum ima_hooks thus it can't tell if it's READING_MODULE_COMPRESSED so to only do collect action. So I create a fake MODULE_COMPRESSED_CHECK func.

Correct, either extending process_measurement() with the read_idmap enum or defining the fake hook would work.

Quoted 10 lines

And for the idea of re-reading the original kernel module data, it has been implemented in
https://github.com/coiby/linux/tree/in_kernel_decompression_ima_no_lsm_hook_paul

Both branches have applied your requested three changes including respecting the 80 char line limit. Additionally, I made a change to the IPE LSM because of the new READING_MODULE_COMPRESSED kernel_read_file_id enumerate.

Quoted 7 lines
Quoted 4 lines

After comparing the two implementations, personally I prefer re-reading the original kernel module data because the change is smaller and it's more user-friendly. But if there are other reasons I don't know, I'll post the patches of the new collect action approach to the mailing list.

The "re-reading" option fails some of the tests. As the "collect" rule isn't needed, let's stick with the first option.

Thanks for evaluating the two options! Yeah, without the "collect" rule, the 1st option is much better as it doesn't have the issue of re-reading the module.

Signature · 2 lines
Best regards,
Coiby
author
#37 lore raw

PATCH v3 1/1 ima: Access decompressed kernel module to verify appended signature

In reply to Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new kernel_read_file_id enumerate READING_MODULE_COMPRESSED so IMA can know only to collect original module data hash on READING_MODULE_COMPRESSED and defer appraising/measuring it until on READING_MODULE when the module has been decompressed.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
 include/linux/kernel_read_file.h    |  1 +
 kernel/module/main.c                | 17 ++++++++++++++---
 security/integrity/ima/ima_main.c   | 24 ++++++++++++++++--------
 security/integrity/ima/ima_policy.c |  3 ++-
 security/ipe/hooks.c                |  1 +
 security/selinux/hooks.c            |  5 +++--
 6 files changed, 37 insertions(+), 14 deletions(-)
include/linux/kernel_read_file.h +1 -0
index 90451e2e12bd..d613a7b4dd35 100644
@@ -14,6 +14,7 @@
 	id(KEXEC_INITRAMFS, kexec-initramfs)	\
 	id(POLICY, security-policy)		\
 	id(X509_CERTIFICATE, x509-certificate)	\
+	id(MODULE_COMPRESSED, kernel-module-compressed) \
 	id(MAX_ID, )
 
 #define __fid_enumify(ENUM, dummy) READING_ ## ENUM,
kernel/module/main.c +14 -3
index c66b26184936..7b3ec2fa6e7c 100644
@@ -3675,24 +3675,35 @@ static int idempotent_wait_for_completion(struct idempotent *u)
 
 static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
 {
+	bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
 	struct load_info info = { };
 	void *buf = NULL;
 	int len;
+	int err;
 
-	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
+	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+			       compressed ? READING_MODULE_COMPRESSED :
+					    READING_MODULE);
 	if (len < 0) {
 		mod_stat_inc(&failed_kreads);
 		return len;
 	}
 
-	if (flags & MODULE_INIT_COMPRESSED_FILE) {
-		int err = module_decompress(&info, buf, len);
+	if (compressed) {
+		err = module_decompress(&info, buf, len);
 		vfree(buf); /* compressed data is no longer needed */
 		if (err) {
 			mod_stat_inc(&failed_decompress);
 			mod_stat_add_long(len, &invalid_decompress_bytes);
 			return err;
 		}
+		err = security_kernel_post_read_file(f, (char *)info.hdr, info.len,
+						     READING_MODULE);
+		if (err) {
+			mod_stat_inc(&failed_kreads);
+			free_copy(&info, flags);
+			return err;
+		}
 	} else {
 		info.hdr = buf;
 		info.len = len;
security/integrity/ima/ima_main.c +16 -8
index cdd225f65a62..49f8b2b1a9af 100644
@@ -235,7 +235,8 @@ static void ima_file_free(struct file *file)
 
 static int process_measurement(struct file *file, const struct cred *cred,
 			       struct lsm_prop *prop, char *buf, loff_t size,
-			       int mask, enum ima_hooks func)
+			       int mask, enum ima_hooks func,
+			       enum kernel_read_file_id read_id)
 {
 	struct inode *real_inode, *inode = file_inode(file);
 	struct ima_iint_cache *iint = NULL;
@@ -406,6 +407,12 @@ static int process_measurement(struct file *file, const struct cred *cred,
 	if (rc != 0 && rc != -EBADF && rc != -EINVAL)
 		goto out_locked;
 
+	/* Defer measuring/appraising kernel modules to READING_MODULE */
+	if (read_id == READING_MODULE_COMPRESSED) {
+		must_appraise = 0;
+		goto out_locked;
+	}
+
 	if (!pathbuf)	/* ima_rdwr_violation possibly pre-fetched */
 		pathname = ima_d_path(&file->f_path, &pathbuf, filename);
 
@@ -486,14 +493,14 @@ static int ima_file_mmap(struct file *file, unsigned long reqprot,
 
 	if (reqprot & PROT_EXEC) {
 		ret = process_measurement(file, current_cred(), &prop, NULL,
-					  0, MAY_EXEC, MMAP_CHECK_REQPROT);
+					  0, MAY_EXEC, MMAP_CHECK_REQPROT, 0);
 		if (ret)
 			return ret;
 	}
 
 	if (prot & PROT_EXEC)
 		return process_measurement(file, current_cred(), &prop, NULL,
-					   0, MAY_EXEC, MMAP_CHECK);
+					   0, MAY_EXEC, MMAP_CHECK, 0);
 
 	return 0;
 }
@@ -578,13 +585,13 @@ static int ima_bprm_check(struct linux_binprm *bprm)
 
 	security_current_getlsmprop_subj(&prop);
 	ret = process_measurement(bprm->file, current_cred(),
-				  &prop, NULL, 0, MAY_EXEC, BPRM_CHECK);
+				  &prop, NULL, 0, MAY_EXEC, BPRM_CHECK, 0);
 	if (ret)
 		return ret;
 
 	security_cred_getlsmprop(bprm->cred, &prop);
 	return process_measurement(bprm->file, bprm->cred, &prop, NULL, 0,
-				   MAY_EXEC, CREDS_CHECK);
+				   MAY_EXEC, CREDS_CHECK, 0);
 }
 
 /**
@@ -632,7 +639,7 @@ static int ima_file_check(struct file *file, int mask)
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(file, current_cred(), &prop, NULL, 0,
 				   mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
-					   MAY_APPEND), FILE_CHECK);
+					   MAY_APPEND), FILE_CHECK, 0);
 }
 
 static int __ima_inode_hash(struct inode *inode, struct file *file, char *buf,
@@ -851,12 +858,13 @@ static int ima_read_file(struct file *file, enum kernel_read_file_id read_id,
 	func = read_idmap[read_id] ?: FILE_CHECK;
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(file, current_cred(), &prop, NULL, 0,
-				   MAY_READ, func);
+				   MAY_READ, func, 0);
 }
 
 const int read_idmap[READING_MAX_ID] = {
 	[READING_FIRMWARE] = FIRMWARE_CHECK,
 	[READING_MODULE] = MODULE_CHECK,
+	[READING_MODULE_COMPRESSED] = MODULE_CHECK,
 	[READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
 	[READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
 	[READING_POLICY] = POLICY_CHECK
@@ -894,7 +902,7 @@ static int ima_post_read_file(struct file *file, char *buf, loff_t size,
 	func = read_idmap[read_id] ?: FILE_CHECK;
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(file, current_cred(), &prop, buf, size,
-				   MAY_READ, func);
+				   MAY_READ, func, read_id);
 }
 
 /**
security/integrity/ima/ima_policy.c +2 -1
index 128fab897930..ae520e6bb1cf 100644
@@ -241,7 +241,8 @@ static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
 
 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
 	{.action = APPRAISE, .func = MODULE_CHECK,
-	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
+	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
+		  IMA_CHECK_BLACKLIST},
 	{.action = APPRAISE, .func = FIRMWARE_CHECK,
 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
 	{.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
security/ipe/hooks.c +1 -0
index d0323b81cd8f..1053a4acf589 100644
@@ -118,6 +118,7 @@ int ipe_kernel_read_file(struct file *file, enum kernel_read_file_id id,
 		op = IPE_OP_FIRMWARE;
 		break;
 	case READING_MODULE:
+	case READING_MODULE_COMPRESSED:
 		op = IPE_OP_KERNEL_MODULE;
 		break;
 	case READING_KEXEC_INITRAMFS:
security/selinux/hooks.c +3 -2
index dfc22da42f30..c1ff69d5d76e 100644
@@ -4275,7 +4275,7 @@ static int selinux_kernel_read_file(struct file *file,
 {
 	int rc = 0;
 
-	BUILD_BUG_ON_MSG(READING_MAX_ID > 7,
+	BUILD_BUG_ON_MSG(READING_MAX_ID > 8,
 			 "New kernel_read_file_id introduced; update SELinux!");
 
 	switch (id) {
@@ -4283,6 +4283,7 @@ static int selinux_kernel_read_file(struct file *file,
 		rc = selinux_kernel_load_from_file(file, SYSTEM__FIRMWARE_LOAD);
 		break;
 	case READING_MODULE:
+	case READING_MODULE_COMPRESSED:
 		rc = selinux_kernel_load_from_file(file, SYSTEM__MODULE_LOAD);
 		break;
 	case READING_KEXEC_IMAGE:
@@ -4311,7 +4312,7 @@ static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
 {
 	int rc = 0;
 
-	BUILD_BUG_ON_MSG(LOADING_MAX_ID > 7,
+	BUILD_BUG_ON_MSG(LOADING_MAX_ID > 8,
 			 "New kernel_load_data_id introduced; update SELinux!");
 
 	switch (id) {

base-commit: 6a23ae0a96a600d1d12557add110e0bb6e32730c

Signature · 1 line
2.51.1
maintainer
#38 lore raw

Re: [PATCH v3] ima: Access decompressed kernel module to verify appended signature

Hi Coiby,

Quoted 8 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new kernel_read_file_id enumerate READING_MODULE_COMPRESSED so IMA can know only to collect original module data hash on READING_MODULE_COMPRESSED and defer appraising/measuring it until on READING_MODULE when the module has been decompressed.

This paragraph is a bit awkward. Perhaps something like:

-> so IMA can calculate the compressed kernel module data hash and defer measuring/appraising ...

Quoted 11 lines

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

Thanks, Coiby!

The patch applies cleanly to linus' tree, but needs to be applied to next- integrity. Please re-base.

Signature · 3 lines
thanks,

Mimi
author
#39 lore raw

Re: [PATCH v3] ima: Access decompressed kernel module to verify appended signature

Quoted 1 line · Mimi Zohar

Hi Coiby,

Hi Mimi,

Quoted 28 lines · Coiby Xu
Quoted 8 lines

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new kernel_read_file_id enumerate READING_MODULE_COMPRESSED so IMA can know only to collect original module data hash on READING_MODULE_COMPRESSED and defer appraising/measuring it until on READING_MODULE when the module has been decompressed.

This paragraph is a bit awkward. Perhaps something like:

-> so IMA can calculate the compressed kernel module data hash and defer measuring/appraising ...

Quoted 11 lines

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

Quoted 4 lines

Thanks, Coiby!

The patch applies cleanly to linus' tree, but needs to be applied to next- integrity. Please re-base.

I've sent v4 which has been rebased onto next tree with improved wording as suggested.

Quoted 7 lines
Signature · 5 lines
-- 

thanks,

Mimi
Signature · 2 lines
Best regards,
Coiby
author
#40 lore raw

PATCH v4 1/1 ima: Access decompressed kernel module to verify appended signature

In reply to Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new kernel_read_file_id enumerate READING_MODULE_COMPRESSED so IMA can calculate the compressed kernel module data hash on READING_MODULE_COMPRESSED and defer appraising/measuring it until on READING_MODULE when the module has been decompressed.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

---
 include/linux/kernel_read_file.h    |  1 +
 kernel/module/main.c                | 17 ++++++++++++++---
 security/integrity/ima/ima_main.c   | 24 ++++++++++++++++--------
 security/integrity/ima/ima_policy.c |  3 ++-
 security/ipe/hooks.c                |  1 +
 security/selinux/hooks.c            |  5 +++--
 6 files changed, 37 insertions(+), 14 deletions(-)
include/linux/kernel_read_file.h +1 -0
index 90451e2e12bd..d613a7b4dd35 100644
@@ -14,6 +14,7 @@
 	id(KEXEC_INITRAMFS, kexec-initramfs)	\
 	id(POLICY, security-policy)		\
 	id(X509_CERTIFICATE, x509-certificate)	\
+	id(MODULE_COMPRESSED, kernel-module-compressed) \
 	id(MAX_ID, )
 
 #define __fid_enumify(ENUM, dummy) READING_ ## ENUM,
kernel/module/main.c +14 -3
index c66b26184936..7b3ec2fa6e7c 100644
@@ -3675,24 +3675,35 @@ static int idempotent_wait_for_completion(struct idempotent *u)
 
 static int init_module_from_file(struct file *f, const char __user * uargs, int flags)
 {
+	bool compressed = !!(flags & MODULE_INIT_COMPRESSED_FILE);
 	struct load_info info = { };
 	void *buf = NULL;
 	int len;
+	int err;
 
-	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL, READING_MODULE);
+	len = kernel_read_file(f, 0, &buf, INT_MAX, NULL,
+			       compressed ? READING_MODULE_COMPRESSED :
+					    READING_MODULE);
 	if (len < 0) {
 		mod_stat_inc(&failed_kreads);
 		return len;
 	}
 
-	if (flags & MODULE_INIT_COMPRESSED_FILE) {
-		int err = module_decompress(&info, buf, len);
+	if (compressed) {
+		err = module_decompress(&info, buf, len);
 		vfree(buf); /* compressed data is no longer needed */
 		if (err) {
 			mod_stat_inc(&failed_decompress);
 			mod_stat_add_long(len, &invalid_decompress_bytes);
 			return err;
 		}
+		err = security_kernel_post_read_file(f, (char *)info.hdr, info.len,
+						     READING_MODULE);
+		if (err) {
+			mod_stat_inc(&failed_kreads);
+			free_copy(&info, flags);
+			return err;
+		}
 	} else {
 		info.hdr = buf;
 		info.len = len;
security/integrity/ima/ima_main.c +16 -8
index ebaebccfbe9a..edd0fd3d77a0 100644
@@ -235,7 +235,8 @@ static void ima_file_free(struct file *file)
 
 static int process_measurement(struct file *file, const struct cred *cred,
 			       struct lsm_prop *prop, char *buf, loff_t size,
-			       int mask, enum ima_hooks func)
+			       int mask, enum ima_hooks func,
+			       enum kernel_read_file_id read_id)
 {
 	struct inode *real_inode, *inode = file_inode(file);
 	struct ima_iint_cache *iint = NULL;
@@ -406,6 +407,12 @@ static int process_measurement(struct file *file, const struct cred *cred,
 	if (rc != 0 && rc != -EBADF && rc != -EINVAL)
 		goto out_locked;
 
+	/* Defer measuring/appraising kernel modules to READING_MODULE */
+	if (read_id == READING_MODULE_COMPRESSED) {
+		must_appraise = 0;
+		goto out_locked;
+	}
+
 	if (!pathbuf)	/* ima_rdwr_violation possibly pre-fetched */
 		pathname = ima_d_path(&file->f_path, &pathbuf, filename);
 
@@ -486,14 +493,14 @@ static int ima_file_mmap(struct file *file, unsigned long reqprot,
 
 	if (reqprot & PROT_EXEC) {
 		ret = process_measurement(file, current_cred(), &prop, NULL,
-					  0, MAY_EXEC, MMAP_CHECK_REQPROT);
+					  0, MAY_EXEC, MMAP_CHECK_REQPROT, 0);
 		if (ret)
 			return ret;
 	}
 
 	if (prot & PROT_EXEC)
 		return process_measurement(file, current_cred(), &prop, NULL,
-					   0, MAY_EXEC, MMAP_CHECK);
+					   0, MAY_EXEC, MMAP_CHECK, 0);
 
 	return 0;
 }
@@ -577,7 +584,7 @@ static int ima_bprm_check(struct linux_binprm *bprm)
 
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(bprm->file, current_cred(),
-				   &prop, NULL, 0, MAY_EXEC, BPRM_CHECK);
+				   &prop, NULL, 0, MAY_EXEC, BPRM_CHECK, 0);
 }
 
 /**
@@ -607,7 +614,7 @@ static int ima_creds_check(struct linux_binprm *bprm, const struct file *file)
 
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement((struct file *)file, bprm->cred, &prop, NULL,
-				   0, MAY_EXEC, CREDS_CHECK);
+				   0, MAY_EXEC, CREDS_CHECK, 0);
 }
 
 /**
@@ -655,7 +662,7 @@ static int ima_file_check(struct file *file, int mask)
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(file, current_cred(), &prop, NULL, 0,
 				   mask & (MAY_READ | MAY_WRITE | MAY_EXEC |
-					   MAY_APPEND), FILE_CHECK);
+					   MAY_APPEND), FILE_CHECK, 0);
 }
 
 static int __ima_inode_hash(struct inode *inode, struct file *file, char *buf,
@@ -874,12 +881,13 @@ static int ima_read_file(struct file *file, enum kernel_read_file_id read_id,
 	func = read_idmap[read_id] ?: FILE_CHECK;
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(file, current_cred(), &prop, NULL, 0,
-				   MAY_READ, func);
+				   MAY_READ, func, 0);
 }
 
 const int read_idmap[READING_MAX_ID] = {
 	[READING_FIRMWARE] = FIRMWARE_CHECK,
 	[READING_MODULE] = MODULE_CHECK,
+	[READING_MODULE_COMPRESSED] = MODULE_CHECK,
 	[READING_KEXEC_IMAGE] = KEXEC_KERNEL_CHECK,
 	[READING_KEXEC_INITRAMFS] = KEXEC_INITRAMFS_CHECK,
 	[READING_POLICY] = POLICY_CHECK
@@ -917,7 +925,7 @@ static int ima_post_read_file(struct file *file, char *buf, loff_t size,
 	func = read_idmap[read_id] ?: FILE_CHECK;
 	security_current_getlsmprop_subj(&prop);
 	return process_measurement(file, current_cred(), &prop, buf, size,
-				   MAY_READ, func);
+				   MAY_READ, func, read_id);
 }
 
 /**
security/integrity/ima/ima_policy.c +2 -1
index 164d62832f8e..7468afaab686 100644
@@ -244,7 +244,8 @@ static struct ima_rule_entry build_appraise_rules[] __ro_after_init = {
 
 static struct ima_rule_entry secure_boot_rules[] __ro_after_init = {
 	{.action = APPRAISE, .func = MODULE_CHECK,
-	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
+	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED | IMA_MODSIG_ALLOWED |
+		  IMA_CHECK_BLACKLIST},
 	{.action = APPRAISE, .func = FIRMWARE_CHECK,
 	 .flags = IMA_FUNC | IMA_DIGSIG_REQUIRED},
 	{.action = APPRAISE, .func = KEXEC_KERNEL_CHECK,
security/ipe/hooks.c +1 -0
index d0323b81cd8f..1053a4acf589 100644
@@ -118,6 +118,7 @@ int ipe_kernel_read_file(struct file *file, enum kernel_read_file_id id,
 		op = IPE_OP_FIRMWARE;
 		break;
 	case READING_MODULE:
+	case READING_MODULE_COMPRESSED:
 		op = IPE_OP_KERNEL_MODULE;
 		break;
 	case READING_KEXEC_INITRAMFS:
security/selinux/hooks.c +3 -2
index dfc22da42f30..c1ff69d5d76e 100644
@@ -4275,7 +4275,7 @@ static int selinux_kernel_read_file(struct file *file,
 {
 	int rc = 0;
 
-	BUILD_BUG_ON_MSG(READING_MAX_ID > 7,
+	BUILD_BUG_ON_MSG(READING_MAX_ID > 8,
 			 "New kernel_read_file_id introduced; update SELinux!");
 
 	switch (id) {
@@ -4283,6 +4283,7 @@ static int selinux_kernel_read_file(struct file *file,
 		rc = selinux_kernel_load_from_file(file, SYSTEM__FIRMWARE_LOAD);
 		break;
 	case READING_MODULE:
+	case READING_MODULE_COMPRESSED:
 		rc = selinux_kernel_load_from_file(file, SYSTEM__MODULE_LOAD);
 		break;
 	case READING_KEXEC_IMAGE:
@@ -4311,7 +4312,7 @@ static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
 {
 	int rc = 0;
 
-	BUILD_BUG_ON_MSG(LOADING_MAX_ID > 7,
+	BUILD_BUG_ON_MSG(LOADING_MAX_ID > 8,
 			 "New kernel_load_data_id introduced; update SELinux!");
 
 	switch (id) {

base-commit: 43369273518f57b7d56c1cf12d636a809b7bd81b

Signature · 1 line
2.51.1
maintainer
#41 lore raw

Re: [PATCH v4] ima: Access decompressed kernel module to verify appended signature

Quoted 15 lines · Coiby Xu

Currently, when in-kernel module decompression (CONFIG_MODULE_DECOMPRESS) is enabled, IMA has no way to verify the appended module signature as it can't decompress the module.

Define a new kernel_read_file_id enumerate READING_MODULE_COMPRESSED so IMA can calculate the compressed kernel module data hash on READING_MODULE_COMPRESSED and defer appraising/measuring it until on READING_MODULE when the module has been decompressed.

Before enabling in-kernel module decompression, a kernel module in initramfs can still be loaded with ima_policy=secure_boot. So adjust the kernel module rule in secure_boot policy to allow either an IMA signature OR an appended signature i.e. to use
"appraise func=MODULE_CHECK appraise_type=imasig|modsig".

Quoted 4 lines

Thanks, Coiby! The patch is now queued in next-integrity.

Signature · 1 line
Mimi