On Fri, 2026-11-13 at 22:56 +0800, Weibin Liu wrote:
> Without this patch, ima_queue_delete_partial() looks up the queue entry
> at which the measurement list has to be cut without holding
> ima_extend_list_mutex, and only takes the mutex afterwards for
> __list_cut_position().  The comment above the cut justifies this with
> "ima_fs.c enforces single-writer exclusion".
> 
> That exclusion only covers cross-process opens and is only enforced at
> open() and release() time.  ima_measure_lock() registers measure_writer
> at open() time and even lets the same task open the file again by
> incrementing measure_writer_extra_writes, while
> _ima_measurements_write() never checks the caller's identity.  A
> privileged task can therefore open several descriptors for the
> measurement list and use them concurrently from its own threads.  Each
> open() creates a separate struct file, so the f_pos_lock taken by
> fdget_pos() serializes neither these writers nor their readers.
> 
> Two racing writers can then resolve the same, or an overlapping, trim
> position, and the second one dereferences a queue entry that the first
> one has already detached and handed to ima_queue_delete().  Depending
> on the interleaving this corrupts the measurement list, reads freed
> memory in __list_cut_position(), or frees the same entry twice.
> 
> Reproducer: generate some tens of thousands of measurement records,
> open() the measurement list for writing several times from one task,
> then write "20000\n" from that many threads using one descriptor each.
> KASAN reports a stack-out-of-bounds access in ima_queue_delete(),
> called from ima_queue_delete_partial().
> 
> The extend mutex alone cannot protect readers: the binary and ASCII
> file operations call seq_read()/seq_lseek(), which traverse the list
> without taking ima_extend_list_mutex.  Readers from other processes
> are already excluded at open time, but an independent O_RDWR descriptor
> lets a reader thread of the writer process race with deletion from
> another thread.  Adding a concurrent reader to the writer reproducer
> triggers a general protection fault in ima_measurements_show(), reached
> from seq_read().
> 
> Introduce ima_measure_rwsem at the file-operation layer.  Writers use
> down_write_killable() around _ima_measurements_write(); readers use
> down_read_killable() around seq_read() and seq_lseek().  If a fatal
> signal interrupts lock acquisition, return -EINTR without entering the
> protected operation.  Cover the binary/ASCII and current/staged file
> operations, since they all expose the same list traversal and deletion
> commands.  The resulting lock order is rwsem followed by seq_file's
> lock on the read side, or ima_extend_list_mutex on the write side.
> Freeing the private trimmed list remains outside the extend mutex but
> inside the rwsem write-side critical section.  The existing open-time
> exclusion remains unchanged, so one task cannot delete a staged list
> belonging to another.
> 
> Keep the existing list_for_each_entry_rcu() traversal outside
> ima_extend_list_mutex.  The rwsem serializes staging/delete writers, so
> only ima_add_template_entry() can race with the traversal by appending
> an entry.  The selected entry stays valid until the cut.  Take the
> extend mutex only for the suspended check, the cut and the accounting
> updates.  Preserve the error precedence: a short list returns -ENOENT
> before the suspended check.
> 
> Suggested-by: Roberto Sassu <[email protected]>
> Fixes: e9b491e27bf6 ("ima: Add support for staging measurements with prompt")
> Fixes: fcb0318a2969 ("ima: Support staging and deleting N measurements 
> records")
> Cc: [email protected] # v7.2+
> Reviewed-by: Lin Qiaojie <[email protected]>
> Signed-off-by: Weibin Liu <[email protected]>

Reviewed-by: Roberto Sassu <[email protected]>

Thanks

Roberto

> ---
> Changes in v4:
> - Rename ima_measurements_rwsem to ima_measure_rwsem.
> - Use killable rwsem acquisition in all four read/lseek/write wrappers,
>   returning -EINTR if a fatal signal interrupts the wait.
> - Clarify the open()/release() exclusion, reader threads and RCU list
>   traversal in the commit message; fix the append function name.
> - Add a Fixes tag for the staging-with-prompt change.
> - Rebuild and rerun the current and staged KASAN+LOCKDEP QEMU tests.
>   Add a deterministic userfaultfd/SIGKILL test covering all four
>   interrupted lock-acquisition paths.
> 
> Link: 
> https://lore.kernel.org/all/[email protected]/ [v3]
> 
>  security/integrity/ima/ima_fs.c    | 78 
> ++++++++++++++++++++++++++++++++------
>  security/integrity/ima/ima_queue.c |  7 ++--
>  2 files changed, 70 insertions(+), 15 deletions(-)
> 
> diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
> index 3b8174a91a34..5c2919e35332 100644
> --- a/security/integrity/ima/ima_fs.c
> +++ b/security/integrity/ima/ima_fs.c
> @@ -39,6 +39,8 @@ static DEFINE_MUTEX(ima_measure_mutex);
>  static long ima_measure_users;
>  static struct task_struct *measure_writer;
>  static long measure_writer_extra_writes;
> +/* Lock order: ima_measure_rwsem -> ima_extend_list_mutex. */
> +static DECLARE_RWSEM(ima_measure_rwsem);
>  
>  bool ima_canonical_fmt;
>  static int __init default_canonical_fmt_setup(char *str)
> @@ -161,7 +163,11 @@ void ima_putc(struct seq_file *m, void *data, int 
> datalen)
>   */
>  int ima_measurements_show(struct seq_file *m, void *v)
>  {
> -     /* the list never shrinks, so we don't need a lock here */
> +     /*
> +      * seq_file callers hold ima_measure_rwsem.  The kexec dump caller
> +      * relies on reboot notifier ordering: measurements are suspended and
> +      * staging/deletion are forbidden while it walks the lists locklessly.
> +      */
>       struct ima_queue_entry *qe = v;
>       struct ima_template_entry *e;
>       char *template_name;
> @@ -400,32 +406,80 @@ static ssize_t _ima_measurements_write(struct file 
> *file,
>       return datalen;
>  }
>  
> +static ssize_t ima_measurements_read(struct file *file, char __user *buf,
> +                                  size_t datalen, loff_t *ppos)
> +{
> +     ssize_t ret;
> +
> +     ret = down_read_killable(&ima_measure_rwsem);
> +     if (ret)
> +             return ret;
> +
> +     ret = seq_read(file, buf, datalen, ppos);
> +     up_read(&ima_measure_rwsem);
> +
> +     return ret;
> +}
> +
> +static loff_t ima_measurements_llseek(struct file *file, loff_t offset,
> +                                   int whence)
> +{
> +     loff_t ret;
> +
> +     ret = down_read_killable(&ima_measure_rwsem);
> +     if (ret)
> +             return ret;
> +
> +     ret = seq_lseek(file, offset, whence);
> +     up_read(&ima_measure_rwsem);
> +
> +     return ret;
> +}
> +
>  static ssize_t ima_measurements_write(struct file *file, const char __user 
> *buf,
>                                     size_t datalen, loff_t *ppos)
>  {
> -     return _ima_measurements_write(file, buf, datalen, ppos, false);
> +     ssize_t ret;
> +
> +     ret = down_write_killable(&ima_measure_rwsem);
> +     if (ret)
> +             return ret;
> +
> +     ret = _ima_measurements_write(file, buf, datalen, ppos, false);
> +     up_write(&ima_measure_rwsem);
> +
> +     return ret;
>  }
>  
>  static ssize_t ima_measurements_staged_write(struct file *file,
>                                            const char __user *buf,
>                                            size_t datalen, loff_t *ppos)
>  {
> -     return _ima_measurements_write(file, buf, datalen, ppos, true);
> +     ssize_t ret;
> +
> +     ret = down_write_killable(&ima_measure_rwsem);
> +     if (ret)
> +             return ret;
> +
> +     ret = _ima_measurements_write(file, buf, datalen, ppos, true);
> +     up_write(&ima_measure_rwsem);
> +
> +     return ret;
>  }
>  
>  static const struct file_operations ima_measurements_ops = {
>       .open = ima_measurements_open,
> -     .read = seq_read,
> +     .read = ima_measurements_read,
>       .write = ima_measurements_write,
> -     .llseek = seq_lseek,
> +     .llseek = ima_measurements_llseek,
>       .release = ima_measurements_release,
>  };
>  
>  static const struct file_operations ima_measurements_staged_ops = {
>       .open = ima_measurements_staged_open,
> -     .read = seq_read,
> +     .read = ima_measurements_read,
>       .write = ima_measurements_staged_write,
> -     .llseek = seq_lseek,
> +     .llseek = ima_measurements_llseek,
>       .release = ima_measurements_release,
>  };
>  
> @@ -440,7 +494,7 @@ void ima_print_digest(struct seq_file *m, u8 *digest, u32 
> size)
>  /* print in ascii */
>  static int ima_ascii_measurements_show(struct seq_file *m, void *v)
>  {
> -     /* the list never shrinks, so we don't need a lock here */
> +     /* The caller holds ima_measure_rwsem. */
>       struct ima_queue_entry *qe = v;
>       struct ima_template_entry *e;
>       char *template_name;
> @@ -497,9 +551,9 @@ static int ima_ascii_measurements_open(struct inode 
> *inode, struct file *file)
>  
>  static const struct file_operations ima_ascii_measurements_ops = {
>       .open = ima_ascii_measurements_open,
> -     .read = seq_read,
> +     .read = ima_measurements_read,
>       .write = ima_measurements_write,
> -     .llseek = seq_lseek,
> +     .llseek = ima_measurements_llseek,
>       .release = ima_measurements_release,
>  };
>  
> @@ -519,9 +573,9 @@ static int ima_ascii_measurements_staged_open(struct 
> inode *inode,
>  
>  static const struct file_operations ima_ascii_measurements_staged_ops = {
>       .open = ima_ascii_measurements_staged_open,
> -     .read = seq_read,
> +     .read = ima_measurements_read,
>       .write = ima_measurements_staged_write,
> -     .llseek = seq_lseek,
> +     .llseek = ima_measurements_llseek,
>       .release = ima_measurements_release,
>  };
>  
> diff --git a/security/integrity/ima/ima_queue.c 
> b/security/integrity/ima/ima_queue.c
> index 67782c004cdf..b35aa0c42f8a 100644
> --- a/security/integrity/ima/ima_queue.c
> +++ b/security/integrity/ima/ima_queue.c
> @@ -429,8 +429,8 @@ int ima_queue_delete_partial(unsigned long req_value)
>  
>       /*
>        * list_for_each_entry_rcu() without rcu_read_lock() is fine because
> -      * only list append can happen concurrently. No list replace due to the
> -      * staging/delete writers mutual exclusion.
> +      * only list append can happen concurrently: staging/delete writers are
> +      * serialized by ima_measure_rwsem.
>        */
>       list_for_each_entry_rcu(qe, &ima_measurements, later, true) {
>               size_to_remove += get_binary_runtime_size(qe->entry);
> @@ -451,7 +451,8 @@ int ima_queue_delete_partial(unsigned long req_value)
>       }
>  
>       /*
> -      * qe remains valid because ima_fs.c enforces single-writer exclusion.
> +      * qe remains valid because staging/delete writers are serialized by
> +      * ima_measure_rwsem.
>        */
>       __list_cut_position(&ima_measurements_trim, &ima_measurements,
>                           &qe->later);


Reply via email to