Subject: Re: [PATCH 3/4] dm-verity: size the hash retain target from the hash 
area
In-Reply-To: <[email protected]>

On Tue, 17 Nov 2026 at 01:44:55PM +0000, [email protected] wrote:


> > diff --git a/drivers/md/dm-verity-target.c b/drivers/md/dm-verity-target.c
> > index 6d589867623c0..d4fb06cb4e7ea 100644
> > --- a/drivers/md/dm-verity-target.c
> > +++ b/drivers/md/dm-verity-target.c
> > @@ -32,6 +32,8 @@
> >  #define DM_VERITY_DEFAULT_PREFETCH_SIZE    262144
> >  #define DM_VERITY_USE_BH_DEFAULT_BYTES     8192
> >  
> > +#define DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT      10
> > +
> >  #define DM_VERITY_MAX_CORRUPTED_ERRS       100
> >  
> >  #define DM_VERITY_OPT_LOGGING              "ignore_corruption"
> > @@ -50,6 +52,13 @@ static unsigned int dm_verity_prefetch_cluster = 
> > DM_VERITY_DEFAULT_PREFETCH_SIZE
> >  
> >  module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, 
> > 0644);
> >  
> > +/* Default fraction of the hash tree that dm-bufio is asked to keep 
> > cached. */
> > +static unsigned int dm_verity_hash_retain_percent = 
> > DM_VERITY_DEFAULT_HASH_RETAIN_PERCENT;
> > +
> > +module_param_named(hash_retain_percent, dm_verity_hash_retain_percent, 
> > uint, 0644);
> > +MODULE_PARM_DESC(hash_retain_percent,
> > +   "Percentage of the hash area to keep cached by default");
> [Severity: Low]
> Since Documentation/admin-guide/kernel-parameters.txt already documents
> dm_verity.keyring_unsealed=, should dm_verity.hash_retain_percent= be added
> there as well?

Will be done in v2.


> > @@ -1083,6 +1092,9 @@ static void verity_dtr(struct dm_target *ti)
> >  
> >     verity_fec_dtr(v);
> >  
> > +   if (v->bufio_kobj)
> > +           kobject_put(v->bufio_kobj);
> > +
> [Severity: Medium]
> Can exposing v->fec->data_bufio in sysfs cause a use-after-free of
> v->data_dev->bdev during teardown?
> verity_dtr() calls dm_put_device(ti, v->data_dev) before verity_fec_dtr(v)
> destroys and unregisters f->data_bufio:
> drivers/md/dm-verity-target.c:verity_dtr() {
>     ...
>       if (v->data_dev)
>               dm_put_device(ti, v->data_dev);
>       verity_fec_dtr(v);
>     ...
> }
> v->fec->data_bufio is created on v->data_dev->bdev, and
> dm_bufio_client_create() stores c->bdev without taking its own reference.
> When FEC uses a separate device from data_dev, dm_put_device(ti, v->data_dev)
> closes the underlying block device while /sys/block/dm-N/dm-bufio/fec-data is
> still registered in sysfs, so a concurrent read of the bdev or bdev_name
> attribute in dm_bufio_attr_show() can dereference c->bdev after it has been
> released:
> CPU0                                  CPU1
> verity_dtr()
>   dm_put_device(ti, v->data_dev)
>                                       dm_bufio_attr_show()
>                                         sysfs_emit(buf, "%pg\n", c->bdev)
>                                           <-- use-after-free
>   verity_fec_dtr(v)
>     dm_bufio_client_destroy(f->data_bufio)
> Calling verity_fec_dtr(v) before releasing v->data_dev and v->hash_dev would
> ensure the FEC bufio clients are unregistered before their backing devices
> are put.

Will be fixed in v2 exactly this way: verity_dtr() will call
verity_fec_dtr() before dm_put_device() of the hash and data devices,
so the FEC bufio clients (and their sysfs entries) are gone before the
bdev is released.


> [ ... ]
>
> > @@ -1629,6 +1642,13 @@ static int verity_ctr(struct dm_target *ti, unsigned 
> > int argc, char **argv)
> >             goto bad;
> >     }
> >  
> > +   hash_retain_percent = READ_ONCE(dm_verity_hash_retain_percent);
> > +   if (hash_retain_percent <= 100)
> > +           dm_bufio_set_retain_bytes(v->bufio,
> > +                   mult_frac((u64)(v->hash_end - v->hash_start) <<
> > +                             v->hash_dev_block_bits,
> > +                             hash_retain_percent, 100));
> > +
> [Severity: High]
> Does passing a u64 expression to mult_frac() break 32-bit builds and silently
> truncate the retain target above ULONG_MAX?
> mult_frac(x, n, d) declares its internal variables with typeof(x) and expands
> to raw '/' and '%' operations, which emit __udivdi3 and __umoddi3 libcalls on
> 32-bit architectures when x is u64 and n is a runtime variable.  In addition,
> dm_bufio_set_retain_bytes() takes an unsigned long, so on 32-bit kernels a
> u64 result at or above 4 GiB is truncated modulo 2^32 (and if truncated to 0,
> resets retain_bytes to the global default).  Using mul_u64_u32_div() and
> min_t(u64, ..., ULONG_MAX) would avoid both problems.

The build claim is a false positive: 64-bit division links fine on
32-bit, the kernel provides those libcalls.  The truncation is real
though, so v2 will compute the percentage with mul_u64_u32_div() and
clamp the result at ULONG_MAX -- the same way dm-bufio sizes its own
cache -- so a large area cannot truncate to near-zero on 32-bit.


> > @@ -1806,6 +1826,45 @@ static void verity_postsuspend(struct dm_target *ti)
> >     verity_security_clear_integrity(ti);
> >  }
> >  
> > +/*
> > + * Publish the bufio clients under /sys/block/dm-N/dm-bufio/.  Runs from
> > + * ->resume because the dm device's block device does not exist in ->ctr.
> > + */
> > +static void verity_register_bufio_sysfs(struct dm_verity *v)
> > +{
> > +   struct mapped_device *md = dm_table_get_md(v->ti->table);
> > +   struct kobject *parent;
> > +
> > +   if (v->bufio_kobj)
> > +           return;
> > +
> > +   parent = &disk_to_dev(dm_disk(md))->kobj;
> > +   /*
> > +    * On a table reload the old table's directory is still there; it is
> > +    * removed only after this resume returns.  We retry on the next one.
> > +    */
> > +   v->bufio_kobj = kobject_create_and_add("dm-bufio", parent);
> > +   if (!v->bufio_kobj) {
> > +           DMERR("cannot create dm-bufio sysfs directory, will retry on 
> > next resume");
> > +           return;
> > +   }
> [Severity: Medium]
> Does reloading a dm-verity table trigger a sysfs_warn_dup() stack trace and
> leave the device without a /sys/block/dm-N/dm-bufio directory?
> On table reload, do_resume() suspends the old table, swaps in the new table,
> and calls dm_resume(md) before calling dm_table_destroy(old_map):
> drivers/md/dm-ioctl.c:do_resume() {
>     ...
>       if (dm_suspended_md(md)) {
>               r = dm_resume(md);
>     ...
>       }
>     ...
>       if (old_map)
>               dm_table_destroy(old_map);
>     ...
> }
> Because the old table's v->bufio_kobj is only freed in verity_dtr(),
> /sys/block/dm-N/dm-bufio still exists when verity_resume() runs on the new
> table.  kobject_create_and_add("dm-bufio", parent) hits -EEXIST in
> sysfs_create_dir_ns() and calls sysfs_warn_dup(), which logs a duplicate
> filename warning and a full dump_stack() backtrace.  Then
> dm_table_destroy(old_map) runs verity_dtr() on the old table and removes the
> directory, and since no second resume occurs, the reloaded table is left with
> v->bufio_kobj == NULL and no dm-bufio sysfs directory.
> Unregistering and freeing v->bufio_kobj (and removing the bufio clients from
> sysfs) in verity_postsuspend() would release the old table's directory before
> the new table's ->resume hook runs.

Same fix as in patch 2: ->presuspend detaches the directories and
frees the names, keeping the kobjects until the table is destroyed, so
the reloaded table publishes its directories synchronously from
->resume.  The ->postsuspend variant deadlocks against udev probing
the frozen device, which is what prompted the pre-freeze detach.


Thanks for the review,
Yongpeng

Reply via email to