A signer whose algorithm signs the data directly (eg. ML-DSA) has no content digest unless it carries authenticated attributes. With authenticated attributes, which sign-file uses for ML-DSA when built against OpenSSL 3.x, 'd-modsig' holds the messageDigest attribute, while the signature covers the attributes and not that digest.
Record such a signer by the name of its signature algorithm instead of a hash algorithm, with an empty digest (e.g. "mldsa65:" '\0'), with or without authenticated attributes. A verifier needs to traverse the 'modsig' field to find out authattr's presence. Signers that sign a digest are recorded as before. Signed-off-by: Bruno Meneguele <[email protected]> --- Documentation/security/IMA-templates.rst | 4 +- security/integrity/ima/ima_template_lib.c | 113 ++++++++++++++-------- 2 files changed, 75 insertions(+), 42 deletions(-) diff --git a/Documentation/security/IMA-templates.rst b/Documentation/security/IMA-templates.rst index 15b4add314fc..4ec0440b27f1 100644 --- a/Documentation/security/IMA-templates.rst +++ b/Documentation/security/IMA-templates.rst @@ -69,7 +69,9 @@ descriptors by adding their identifier to the format string algorithm (field format: <hash algo>:digest); - 'd-ngv2': same as d-ng, but prefixed with the "ima" or "verity" digest type (field format: <digest type>:<hash algo>:digest); - - 'd-modsig': the digest of the event without the appended modsig; + - 'd-modsig': the digest of the event without the appended modsig or a signature + algorithm name followed by an empty digest (<signing algo>: \0) when direct data + algorithms are used, e.g. ML-DSA; - 'n-ng': the name of the event, without size limitations; - 'sig': the file signature, based on either the file's/fsverity's digest[1], or the EVM portable signature, if 'security.ima' contains a file hash. diff --git a/security/integrity/ima/ima_template_lib.c b/security/integrity/ima/ima_template_lib.c index 0609a8f7a127..a2aeaae83065 100644 --- a/security/integrity/ima/ima_template_lib.c +++ b/security/integrity/ima/ima_template_lib.c @@ -286,24 +286,27 @@ int ima_parse_buf(void *bufstartp, void *bufendp, void **bufcurp, return 0; } +/* + * digest formats: + * - DATA_FMT_DIGEST: digest + * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest, + * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO: + * <digest type> + ':' + <hash algo> + ':' + '\0' + digest, + * + * where 'DATA_FMT_DIGEST' is the original digest format ('d') + * with a hash size limitation of 20 bytes, + * where <digest type> is either "ima" or "verity", + * where <hash algo> is the hash_algo_name[] string. + */ +#define IMA_MAX_DIGEST_ENTRY_SIZE (DIGEST_TYPE_NAME_LEN_MAX + \ + CRYPTO_MAX_ALG_NAME + 2 + \ + IMA_MAX_DIGEST_SIZE) + static int ima_eventdigest_init_common(const u8 *digest, u32 digestsize, u8 digest_type, u8 hash_algo, struct ima_field_data *field_data) { - /* - * digest formats: - * - DATA_FMT_DIGEST: digest - * - DATA_FMT_DIGEST_WITH_ALGO: <hash algo> + ':' + '\0' + digest, - * - DATA_FMT_DIGEST_WITH_TYPE_AND_ALGO: - * <digest type> + ':' + <hash algo> + ':' + '\0' + digest, - * - * where 'DATA_FMT_DIGEST' is the original digest format ('d') - * with a hash size limitation of 20 bytes, - * where <digest type> is either "ima" or "verity", - * where <hash algo> is the hash_algo_name[] string. - */ - u8 buffer[DIGEST_TYPE_NAME_LEN_MAX + CRYPTO_MAX_ALG_NAME + 2 + - IMA_MAX_DIGEST_SIZE] = { 0 }; + u8 buffer[IMA_MAX_DIGEST_ENTRY_SIZE] = { 0 }; enum data_formats fmt = DATA_FMT_DIGEST; u32 offset = 0; @@ -451,6 +454,44 @@ int ima_eventdigest_ngv2_init(struct ima_event_data *event_data, field_data); } +/* + * ima_modsig_digest_entry - encode the content digest of one modsig signer + * @modsig: the appended signature + * @index: zero-based index of the signer + * @entry: output buffer + * + * The entry has the DATA_FMT_DIGEST_WITH_ALGO format. A signer whose algorithm + * signs the data directly (eg. ML-DSA) is recorded by the name of that + * algorithm instead, with empty digest (eg. "mldsa65:"), regardless of + * authenticated attributes being present or not. + * + * Return: the entry length on success, a negative error code otherwise. + */ +static int ima_modsig_digest_entry(const struct modsig *modsig, + unsigned int index, u8 *entry) +{ + const char *sig_algo; + enum hash_algo algo; + const u8 *digest; + u32 size; + int len, rc; + + rc = ima_get_modsig_digest(modsig, index, &algo, &digest, &size, + &sig_algo); + if (rc) + return rc; + + if (sig_algo) + return sprintf(entry, "%s:", sig_algo) + 1; + + if (algo >= HASH_ALGO__LAST || !size || size > IMA_MAX_DIGEST_SIZE) + return -EINVAL; + + len = sprintf(entry, "%s:", hash_algo_name[algo]) + 1; + memcpy(entry + len, digest, size); + return len + size; +} + /* * This function writes the digest of the file which is expected to match the * digest contained in the file's appended signature. @@ -458,39 +499,29 @@ int ima_eventdigest_ngv2_init(struct ima_event_data *event_data, int ima_eventdigest_modsig_init(struct ima_event_data *event_data, struct ima_field_data *field_data) { - enum hash_algo hash_algo; - const u8 *cur_digest; - u32 cur_digestsize; + /* 'd-modsig' (DATA_FMT_DIGEST_WITH_ALGO) doesn't contain digest type */ + u8 entry[IMA_MAX_DIGEST_ENTRY_SIZE - DIGEST_TYPE_NAME_LEN_MAX] = { 0 }; + int len; if (!event_data->modsig) return 0; - if (event_data->violation) { + if (event_data->violation) /* Recording a violation. */ - hash_algo = HASH_ALGO_SHA1; - cur_digest = NULL; - cur_digestsize = 0; - } else { - const char *sig_algo; - int rc; - - /* 'd-modsig' supports the digest of a single signer only. */ - if (ima_get_modsig_signer_count(event_data->modsig) != 1) - return -EINVAL; - - rc = ima_get_modsig_digest(event_data->modsig, 0, &hash_algo, - &cur_digest, &cur_digestsize, - &sig_algo); - if (rc) - return rc; - else if (hash_algo == HASH_ALGO__LAST || cur_digestsize == 0) - /* There was some error collecting the digest. */ - return -EINVAL; - } + return ima_eventdigest_init_common(NULL, 0, DIGEST_TYPE__LAST, + HASH_ALGO_SHA1, field_data); - return ima_eventdigest_init_common(cur_digest, cur_digestsize, - DIGEST_TYPE__LAST, hash_algo, - field_data); + /* 'd-modsig' supports the digest of a single signer only. */ + if (ima_get_modsig_signer_count(event_data->modsig) != 1) + return -EINVAL; + + len = ima_modsig_digest_entry(event_data->modsig, 0, entry); + if (len < 0) + return len; + + return ima_write_template_field_data(entry, len, + DATA_FMT_DIGEST_WITH_ALGO, + field_data); } static int ima_eventname_init_common(struct ima_event_data *event_data, -- 2.55.0
