On Tue, 2026-12-01 at 09:54 +0800, Gong Ruiqi wrote:
Hi Ruiqi
in general, I think it is a good practice to CC the linux-security-
module and linux-kernel mailing lists too.
> process_buffer_measurement() takes the target PCR as an int argument
> and treats 0 as "no PCR given", replacing it with
> CONFIG_IMA_MEASURE_PCR_IDX. PCR 0 is a legitimate index: it passes
> INVALID_PCR() and ima_parse_rule() accepts a rule with pcr=0, setting
> IMA_PCR on it. ima_match_policy() then writes that 0 back into the
> caller's variable, and the fallback immediately overwrites it with the
> default.
>
> As a result every buffer measurement governed by a pcr=0 rule lands in
> the default PCR instead: critical data, keys, kexec cmdline, raw policy
> writes, and the auxiliary blacklisted-hash record when the file's rule
> says pcr=0. File measurements take the other route:
> process_measurement() pre-seeds pcr with the default and lets a
> matching rule override it, so pcr=0 works there. The two paths disagree.
>
> Use a dedicated sentinel, IMA_PCR_UNSET (-1), for "no PCR given". A
> negative value is outside the range INVALID_PCR() accepts, so it cannot
> be mistaken for a real index, and it is resolved before
> ima_store_template(), which keeps it away from the u32 pcr_idx of
> tpm_pcr_extend() and from the shifts by pcr in
> ima_store_measurement(). Callers that used to pass 0 now pass the
> sentinel. ima_check_blacklist() passes the PCR of the file's rule on
> purpose and is left alone.
Wouldn't be better to pass the default PCR where the callers pass 0,
and remove the fallback?
Thanks
Roberto
> Fixes: e14555e3d0e9e ("ima: Make process_buffer_measurement() generic")
> Signed-off-by: Weng Jiaxuan <[email protected]>
> Signed-off-by: Gong Ruiqi <[email protected]>
> ---
> security/integrity/ima/ima.h | 3 +++
> security/integrity/ima/ima_asymmetric_keys.c | 2 +-
> security/integrity/ima/ima_main.c | 10 +++++-----
> security/integrity/ima/ima_queue_keys.c | 2 +-
> 4 files changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/security/integrity/ima/ima.h b/security/integrity/ima/ima.h
> index b502854f28ee..5040b8ebf501 100644
> --- a/security/integrity/ima/ima.h
> +++ b/security/integrity/ima/ima.h
> @@ -28,6 +28,9 @@ enum ima_show_type { IMA_SHOW_BINARY,
> IMA_SHOW_BINARY_NO_FIELD_LEN,
> IMA_SHOW_BINARY_OLD_STRING_FMT, IMA_SHOW_ASCII };
> enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8, TPM_PCR10 = 10 };
>
> +/* "no PCR given": never a valid index, see INVALID_PCR() in ima_policy.c */
> +#define IMA_PCR_UNSET (-1)
> +
> /*
> * BINARY: current binary measurements list
> * BINARY_STAGED: staged binary measurements list
> diff --git a/security/integrity/ima/ima_asymmetric_keys.c
> b/security/integrity/ima/ima_asymmetric_keys.c
> index caacfe6860b1..223de01703a7 100644
> --- a/security/integrity/ima/ima_asymmetric_keys.c
> +++ b/security/integrity/ima/ima_asymmetric_keys.c
> @@ -61,6 +61,6 @@ void ima_post_key_create_or_update(struct key *keyring,
> struct key *key,
> * to the given keyring.
> */
> process_buffer_measurement(&nop_mnt_idmap, NULL, payload, payload_len,
> - keyring->description, KEY_CHECK, 0,
> + keyring->description, KEY_CHECK,
> IMA_PCR_UNSET,
> keyring->description, false, NULL, 0);
> }
> diff --git a/security/integrity/ima/ima_main.c
> b/security/integrity/ima/ima_main.c
> index fed80da3ee09..707f4073d1b5 100644
> --- a/security/integrity/ima/ima_main.c
> +++ b/security/integrity/ima/ima_main.c
> @@ -1084,7 +1084,7 @@ static int ima_post_load_data(char *buf, loff_t size,
> * @size: size of buffer(in bytes).
> * @eventname: event name to be used for the buffer entry.
> * @func: IMA hook
> - * @pcr: pcr to extend the measurement
> + * @pcr: pcr to extend the measurement, or IMA_PCR_UNSET for the default PCR
> * @func_data: func specific data, may be NULL
> * @buf_hash: measure buffer data hash
> * @digest: buffer digest will be written to
> @@ -1149,7 +1149,7 @@ int process_buffer_measurement(const struct mnt_idmap
> *idmap,
> return -ENOENT;
> }
>
> - if (!pcr)
> + if (pcr == IMA_PCR_UNSET)
> pcr = CONFIG_IMA_MEASURE_PCR_IDX;
>
> iint.ima_hash = hash_hdr;
> @@ -1221,7 +1221,7 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf,
> int size)
> return;
>
> process_buffer_measurement(file_mnt_idmap(fd_file(f)),
> file_inode(fd_file(f)),
> - buf, size, "kexec-cmdline", KEXEC_CMDLINE, 0,
> + buf, size, "kexec-cmdline", KEXEC_CMDLINE,
> IMA_PCR_UNSET,
> NULL, false, NULL, 0);
> }
>
> @@ -1253,7 +1253,7 @@ int ima_measure_critical_data(const char *event_label,
> return -ENOPARAM;
>
> return process_buffer_measurement(&nop_mnt_idmap, NULL, buf, buf_len,
> - event_name, CRITICAL_DATA, 0,
> + event_name, CRITICAL_DATA,
> IMA_PCR_UNSET,
> event_label, hash, digest,
> digest_len);
> }
> @@ -1278,7 +1278,7 @@ int ima_measure_raw_policy(const char *buf, size_t
> buf_len)
>
> return process_buffer_measurement(&nop_mnt_idmap, NULL, buf, buf_len,
> "ima_policy_written", POLICY_CHECK,
> - 0, NULL, false, NULL, 0);
> + IMA_PCR_UNSET, NULL, false, NULL, 0);
> }
>
> #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
> diff --git a/security/integrity/ima/ima_queue_keys.c
> b/security/integrity/ima/ima_queue_keys.c
> index 8adc8863ad5a..c3077bd0e247 100644
> --- a/security/integrity/ima/ima_queue_keys.c
> +++ b/security/integrity/ima/ima_queue_keys.c
> @@ -163,7 +163,7 @@ void ima_process_queued_keys(void)
> entry->payload,
> entry->payload_len,
> entry->keyring_name,
> - KEY_CHECK, 0,
> + KEY_CHECK, IMA_PCR_UNSET,
> entry->keyring_name,
> false, NULL, 0);
> list_del(&entry->list);