mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ima: allocate the new hash table before detaching staged measurements
@ 2026-11-27  7:57 Cong Nguyen
  2026-12-02 12:16 ` Roberto Sassu
  0 siblings, 1 reply; 2+ messages in thread
From: Cong Nguyen @ 2026-11-27  7:57 UTC (permalink / raw)
  To: Mimi Zohar, Roberto Sassu
  Cc: Dmitry Kasatkin, Eric Snowberg, linux-integrity,
	linux-security-module, linux-kernel

ima_queue_staged_delete_all() moves the staged measurements to a local
list and clears the staged counters before it allocates the replacement
hash table with ima_alloc_replace_htable(). If that allocation fails, the
function returns -ENOMEM with the entries already detached from
ima_measurements_staged and never freed, because ima_queue_delete() is
only reached on success. The entries are then on no list, and a retry
finds nothing to delete.

Allocate the hash table first, so that the -ENOMEM return leaves the
staged state unchanged. Both steps run under ima_extend_list_mutex.

Fixes: c26d9d9246cc ("ima: Add support for flushing the hash table when staging measurements")
Signed-off-by: Cong Nguyen <congnt264@gmail.com>
---
 security/integrity/ima/ima_queue.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c
index 67782c004cdf..d58b33950bcc 100644
--- a/security/integrity/ima/ima_queue.c
+++ b/security/integrity/ima/ima_queue.c
@@ -377,14 +377,6 @@ int ima_queue_staged_delete_all(void)
 		return -ESTALE;
 	}
 
-	list_replace(&ima_measurements_staged, &ima_measurements_trim);
-	INIT_LIST_HEAD(&ima_measurements_staged);
-
-	atomic_long_set(&ima_num_records[BINARY_STAGED], 0);
-
-	if (IS_ENABLED(CONFIG_IMA_KEXEC))
-		binary_runtime_size[BINARY_STAGED] = 0;
-
 	if (ima_flush_htable) {
 		old_queue = ima_alloc_replace_htable();
 		if (IS_ERR(old_queue)) {
@@ -393,6 +385,14 @@ int ima_queue_staged_delete_all(void)
 		}
 	}
 
+	list_replace(&ima_measurements_staged, &ima_measurements_trim);
+	INIT_LIST_HEAD(&ima_measurements_staged);
+
+	atomic_long_set(&ima_num_records[BINARY_STAGED], 0);
+
+	if (IS_ENABLED(CONFIG_IMA_KEXEC))
+		binary_runtime_size[BINARY_STAGED] = 0;
+
 	mutex_unlock(&ima_extend_list_mutex);
 
 	if (ima_flush_htable) {
-- 
2.25.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] ima: allocate the new hash table before detaching staged measurements
  2026-11-27  7:57 [PATCH] ima: allocate the new hash table before detaching staged measurements Cong Nguyen
@ 2026-12-02 12:16 ` Roberto Sassu
  0 siblings, 0 replies; 2+ messages in thread
From: Roberto Sassu @ 2026-12-02 12:16 UTC (permalink / raw)
  To: Cong Nguyen, Mimi Zohar, Roberto Sassu
  Cc: Dmitry Kasatkin, Eric Snowberg, linux-integrity,
	linux-security-module, linux-kernel

On Fri, 2026-11-27 at 14:57 +0700, Cong Nguyen wrote:
> ima_queue_staged_delete_all() moves the staged measurements to a local
> list and clears the staged counters before it allocates the replacement
> hash table with ima_alloc_replace_htable(). If that allocation fails, the
> function returns -ENOMEM with the entries already detached from
> ima_measurements_staged and never freed, because ima_queue_delete() is
> only reached on success. The entries are then on no list, and a retry
> finds nothing to delete.
> 
> Allocate the hash table first, so that the -ENOMEM return leaves the
> staged state unchanged. Both steps run under ima_extend_list_mutex.
> 
> Fixes: c26d9d9246cc ("ima: Add support for flushing the hash table when staging measurements")
> Signed-off-by: Cong Nguyen <congnt264@gmail.com>

Reviewed-by: Roberto Sassu <roberto.sassu@huawei.com>

Thanks

Roberto

> ---
>  security/integrity/ima/ima_queue.c | 16 ++++++++--------
>  1 file changed, 8 insertions(+), 8 deletions(-)
> 
> diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c
> index 67782c004cdf..d58b33950bcc 100644
> --- a/security/integrity/ima/ima_queue.c
> +++ b/security/integrity/ima/ima_queue.c
> @@ -377,14 +377,6 @@ int ima_queue_staged_delete_all(void)
>  		return -ESTALE;
>  	}
>  
> -	list_replace(&ima_measurements_staged, &ima_measurements_trim);
> -	INIT_LIST_HEAD(&ima_measurements_staged);
> -
> -	atomic_long_set(&ima_num_records[BINARY_STAGED], 0);
> -
> -	if (IS_ENABLED(CONFIG_IMA_KEXEC))
> -		binary_runtime_size[BINARY_STAGED] = 0;
> -
>  	if (ima_flush_htable) {
>  		old_queue = ima_alloc_replace_htable();
>  		if (IS_ERR(old_queue)) {
> @@ -393,6 +385,14 @@ int ima_queue_staged_delete_all(void)
>  		}
>  	}
>  
> +	list_replace(&ima_measurements_staged, &ima_measurements_trim);
> +	INIT_LIST_HEAD(&ima_measurements_staged);
> +
> +	atomic_long_set(&ima_num_records[BINARY_STAGED], 0);
> +
> +	if (IS_ENABLED(CONFIG_IMA_KEXEC))
> +		binary_runtime_size[BINARY_STAGED] = 0;
> +
>  	mutex_unlock(&ima_extend_list_mutex);
>  
>  	if (ima_flush_htable) {


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02 12:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  7:57 [PATCH] ima: allocate the new hash table before detaching staged measurements Cong Nguyen
2026-12-02 12:16 ` Roberto Sassu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®