Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] dm-verity: 64-bit division and 32-bit truncation in verity_ctr() - [Medium] dm-verity: duplicate sysfs dir warning and missing dir on table reload - [Medium] dm-verity: release of data_dev before verity_fec_dtr() in verity_dtr() - [Low] dm-verity: document dm_verity.hash_retain_percent in kernel-parameters.txt --
commit 6fe0fff5c789faa19fdefcb12c794d8f32e868cd Author: Yongpeng Yang <[email protected]> dm-verity: size the hash retain target from the hash area This commit configures the dm-verity hash bufio client's retain target as a configurable percentage of the hash area and exposes dm-verity's bufio clients under /sys/block/dm-N/dm-bufio/. > 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? > @@ -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. [ ... ] > @@ -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. > @@ -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. -- Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=3
