Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] dm-verity: verity_prefetch_hash() races with suspend and breaks
  quiescence
--

commit d29b7b5d122e67887bc98258e3fe7f3b2d2db059
Author: Yongpeng Yang <[email protected]>

dm-verity: add messages to prefetch and drop the hash cache

This commit adds prefetch_hash and drop_hash target messages to warm or
drop hash blocks in the dm-verity bufio cache.

> diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> index 44b95dbdaebbc..622c427949a35 100644
> --- a/drivers/md/dm-verity-target.c
> +++ b/drivers/md/dm-verity-target.c
> @@ -1828,12 +1828,117 @@ static void verity_postsuspend(struct dm_target *ti)
>       verity_security_clear_integrity(ti);
>  }
>  
> +static int verity_prefetch_hash(struct dm_verity *v, unsigned int argc,
> +                             char **argv, char *result, unsigned int maxlen)
> +{
> +     sector_t total = v->hash_end - v->hash_start;
> +     sector_t start, n;
> +     unsigned long long value;
> +     unsigned int sz = 0;
> +
> +     /*
> +      * On a no_sleep client a buffer being read cannot be evicted or
> +      * waited for, so an unfinished prefetch would outlive a suspend or
> +      * teardown; drop_hash rejects this case for the same reason.
> +      */
> +     if (v->use_bh_wq) {
> +             DMERR("prefetch_hash unsupported on tasklet-verifying device");
> +             return -EOPNOTSUPP;
> +     }
> +
> +     if (argc == 1) {
> +             start = 0;
> +             n = total;
> +     } else if (argc == 3) {
> +             if (kstrtoull(argv[1], 0, &value) || (sector_t)value != value)
> +                     return -EINVAL;
> +             start = value;
> +
> +             if (kstrtoull(argv[2], 0, &value) || (sector_t)value != value)
> +                     return -EINVAL;
> +             n = value;
> +     } else {
> +             return -EINVAL;
> +     }
> +
> +     if (start >= total) {
> +             if (!total) {
> +                     n = 0;
> +                     goto out;
> +             }
> +             DMERR("prefetch_hash start %llu beyond hash area (%llu blocks)",
> +                   (unsigned long long)start, (unsigned long long)total);
> +             return -EINVAL;
> +     }
> +
> +     n = min(n, total - start);
> +     n = min_t(sector_t, n, UINT_MAX);
> +
> +out:
> +     dm_bufio_prefetch(v->bufio, v->hash_start + start, n);

[Severity: High]
Can verity_prefetch_hash() race with verity_postsuspend() or run while the
device is suspended?

target_message() in drivers/md/dm-ioctl.c only takes the SRCU read lock via
dm_get_live_table() and does not take md->suspend_lock or check whether the
device is suspending or suspended.  In dm_suspend(), __dm_suspend()
completes synchronize_srcu(&md->io_barrier) before calling
dm_table_postsuspend_targets() -> verity_postsuspend().

CPU0                                  CPU1
target_message()
  verity_message()
    verity_prefetch_hash()
      __dm_bufio_prefetch()
        dm_bufio_lock(c)
        b = __bufio_new(...)
          b->hold_count = 1
        dm_bufio_unlock(c)
                                      dm_suspend()
                                        __dm_suspend()
                                        verity_postsuspend()
                                          dm_bufio_client_reset(v->bufio)
                                            drop_buffers()
                                              cache_iterate(..., warn_leak)
                                              WARN_ON(cache_count(...))
        submit_io(b, ...)
        dm_bufio_release(b)  <-- use-after-free if
                                 CONFIG_DM_DEBUG_BLOCK_STACK_TRACING

While __dm_bufio_prefetch() drops dm_bufio_lock(c) across submit_io() and
dm_bufio_release(), b remains in c->cache with hold_count == 1.  A
concurrent dm_bufio_client_reset() skips b in __get_unclaimed_buffer(),
triggers the leak WARN_ONs in warn_leak() and drop_buffers(), and under
CONFIG_DM_DEBUG_BLOCK_STACK_TRACING zeroes b->hold_count and frees b while
CPU0 still holds it.

Also, contrary to the documentation note in verity.rst, v->bufio is bound
to v->hash_dev->bdev rather than the top-level DM device, so calling
prefetch_hash while suspended submits read bios directly to the underlying
hash device instead of queueing them until resume, breaking the suspend
quiescence fixed by commit 2de510fccbca ("dm-verity: fix
prefetch-vs-suspend race").

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=4

Reply via email to