Hi,

The bug this patch fixes is a kernel stack overflow, not only a wrong
digest, so I would like to see it merged and sent to stable soon.

With signed attributes and an ML-DSA signer, pkcs7_get_digest() returns
the whole attribute set and its length. IMA copies that into a 201-byte
stack buffer in ima_eventdigest_init_common(). The length comes from
the file, and no key is needed because the measurement is taken before
the signature is checked. I reproduced it on v7.3.7 and on mainline
0fbf78feb38a (7.4-rc5). With v2 applied the overflow is gone on both.

I also have an IMA-side bound, below. A reproducer is available to
maintainers on request.

Details
-------

Affected: v7.0 and later (f3eccecd782d "pkcs7: Allow the signing algo to
do whatever digestion it wants itself"). Tested: v7.3.7 and
0fbf78feb38a (7.4-rc5), ARCH=um, GCC 13.3, KASAN. Not affected: 6.18 and older.

Path: finit_module() or kexec_file_load() of a file with an appended
PKCS#7 signature -> ima_read_modsig() -> ima_collect_modsig() ->
pkcs7_get_digest() -> pkcs7_digest(), which for algo_takes_data with
authattrs sets sig->m_size = sinfo->authattrs_len
(crypto/asymmetric_keys/pkcs7_verify.c:118). m_free is true, so
pkcs7_get_digest() returns it. ima_collect_modsig() stores the size
unchecked (security/integrity/ima/ima_modsig.c:115) and
ima_eventdigest_init_common() does

        memcpy(buffer + offset, digest, digestsize);

into u8 buffer[201] (security/integrity/ima/ima_template_lib.c:322).
Unknown attribute types are accepted, so the set can be any length.

Needs: CONFIG_IMA_APPRAISE_MODSIG=y, an appraise rule with
appraise_type=imasig|modsig and a measure rule with a d-modsig template
for the same hook, for example "ima_policy=tcb|secure_boot
ima_template=ima-modsig" on the command line (the module path is what I
ran). The powerpc secure and trusted boot arch policy has the same pair
for KEXEC_KERNEL_CHECK, and for MODULE_CHECK when CONFIG_MODULE_SIG is
off; I have not run powerpc. The caller needs CAP_SYS_MODULE or
CAP_SYS_BOOT, so this only crosses a boundary where IMA appraisal or
lockdown is what stops root from running its own kernel code.

KASAN (0fbf78feb38a (7.4-rc5), 702 bytes of signed attributes):

  BUG: KASAN: stack-out-of-bounds in ima_eventdigest_init_common+0x1c2/0x25b
  Write of size 702 at addr 00000000610af408 by task init/1
   ima_eventdigest_init_common+0x1c2/0x25b
   ima_eventdigest_modsig_init+0x126/0x151
   ima_alloc_init_template+0x1fb/0x298
   ima_store_measurement+0x169/0x22c
   ima_post_read_file+0x179/0x19b
  [48, 249) 'buffer'

Not tested: a non-UML x86_64 or a powerpc kernel, kexec_file_load(),
and builds with FORTIFY_SOURCE or a stack protector.

Proposed follow-up (can be sent as a proper patch)
--------------------------------------------------

ima: do not trust the digest size returned by pkcs7_get_digest()

--- a/security/integrity/ima/ima_modsig.c
+++ b/security/integrity/ima/ima_modsig.c
@@ -114,6 +114,19 @@ void ima_collect_modsig(struct modsig *modsig,
const void *buf, loff_t size)
        /* Ask the PKCS7 code to calculate the file hash. */
        rc = pkcs7_get_digest(modsig->pkcs7_msg, &modsig->digest,
                              &modsig->digest_size, &modsig->hash_algo);
+
+       /*
+        * The 'd-modsig' template field and the blacklist check expect a real
+        * digest.  Do not trust the size reported by the PKCS#7 code: drop
+        * anything that is not exactly one digest of the reported algorithm.
+        */
+       if (rc || modsig->hash_algo >= HASH_ALGO__LAST ||
+           modsig->digest_size != hash_digest_size[modsig->hash_algo]) {
+               pr_notice_once("modsig digest unavailable or malformed\n");
+               modsig->digest = NULL;
+               modsig->digest_size = 0;
+               modsig->hash_algo = HASH_ALGO__LAST;
+       }
 }

This closes the overflow on its own and keeps IMA safe whatever
pkcs7_get_digest() returns in future, including after Bruno's RFC.

Found with the help of Mythos 5.2 (AI-assisted code analysis); every
claim, the PoC and the patch were verified by hand on real builds.

György Bolyki, Magrathean UK Ltd

Reply via email to