Linux Security Modules development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
Cc: linux-security-module@vger.kernel.org
Subject: Re: [PATCH] ima: allocate the new hash table before detaching staged measurements
Date: Fri, 27 Nov 2026 08:02:19 +0000	[thread overview]
Message-ID: <sashiko-outbox-236853@kernel.org> (raw)
In-Reply-To: <20261127075732.252471-1-congnt264@gmail.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] ima: race between seq_read() and ima_queue_delete_partial()
  causes UAF [1]
--

commit 45f3c9a66f0bc27f297aae05dcd53c20818ca3ef
Author: Cong Nguyen <congnt264@gmail.com>

ima: allocate the new hash table before detaching staged measurements

This commit moves the replacement hash table allocation in
ima_queue_staged_delete_all() ahead of detaching the staged measurements list
so that allocation failure leaves the staged entries intact.

> diff --git a/security/integrity/ima/ima_queue.c b/security/integrity/ima/ima_queue.c
> index 0f1b7e4113c4f..8225c35cf709b 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);

[Severity: Critical]
This problem wasn't introduced by this patch, but can [1]
ima_queue_staged_delete_all() and ima_queue_delete_partial() race with a
concurrent seq_read_iter() reader and cause a use-after-free?

CPU0                                  CPU1
_ima_measurements_write()             seq_read_iter()
  ima_queue_staged_delete_all()         _ima_measurements_start()
                                          rcu_read_lock()
                                          ...
                                          rcu_read_unlock()
                                          return qe;
    mutex_lock(&ima_extend_list_mutex)
    list_replace(...,
                 &ima_measurements_trim)
    mutex_unlock(&ima_extend_list_mutex)
    ima_queue_delete()
      kfree(template_data[i].data)
      list_del(&qe->later)
      kfree(qe->entry->digests)
      kfree(qe->entry)
      kfree(qe)
                                        ima_measurements_show()
                                          qe->entry  <-- use-after-free
                                        _ima_measurements_next()
                                          qe->later.next  <-- LIST_POISON1 or
                                                              stack list head

Once a task becomes measure_writer in ima_measure_lock(), it can issue
concurrent pread64() and write() calls on the same O_RDWR descriptor (or
across extra descriptors opened via measure_writer_extra_writes), because
seq_open() sets FMODE_PREAD and _ima_measurements_write() does not take
seq_file->lock.

_ima_measurements_start() and _ima_measurements_next() do not take
ima_extend_list_mutex and drop rcu_read_lock() before returning qe to
seq_read_iter().  Splicing entries onto the stack-local
ima_measurements_trim list (via list_replace() here or
__list_cut_position() in ima_queue_delete_partial()) and freeing them in
ima_queue_delete() with non-RCU list_del() and no RCU grace period leaves
concurrent readers accessing freed memory, LIST_POISON1, or the stack
list head.

[1] https://sashiko.dev/#/bug/linux-ba0e31a8-a909-4f9b-a571-5f4d11ef4c91

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127075732.252471-1-congnt264@gmail.com?part=1

      reply	other threads:[~2026-11-27  8:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  7:57 [PATCH] ima: allocate the new hash table before detaching staged measurements Cong Nguyen
2026-11-27  8:02 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-236853@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox