]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
dm-verity: make error counter atomic
authorMikulas Patocka <mpatocka@redhat.com>
Thu, 9 Jul 2026 19:37:38 +0000 (21:37 +0200)
committerMikulas Patocka <mpatocka@redhat.com>
Fri, 10 Jul 2026 12:42:33 +0000 (14:42 +0200)
The error counter "v->corrupted_errs" was not atomic, thus it could be
subject to race conditions. The call to
dm_audit_log_target("max-corrupted-errors") may be skipped due to the
races.

Signed-off-by: Mikulas Patocka <mpatocka@redhat.com>
Assisted-by: Claude:claude-opus-4.6
Fixes: 65ff5b7ddf05 ("dm verity: add error handling modes for corrupted blocks")
Cc: stable@vger.kernel.org
drivers/md/dm-verity-target.c
drivers/md/dm-verity.h

index e63a8290bd3af4b0061cc82e12ad646fb3e9d595..1b076309125403946f5e1e9bda985e807a06401b 100644 (file)
@@ -180,14 +180,16 @@ static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
        char *envp[] = { verity_env, NULL };
        const char *type_str = "";
        struct mapped_device *md = dm_table_get_md(v->ti->table);
+       int ce;
 
        /* Corruption should be visible in device status in all modes */
        v->hash_failed = true;
 
-       if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
-               goto out;
-
-       v->corrupted_errs++;
+       ce = atomic_read(&v->corrupted_errs);
+       do {
+               if (ce >= DM_VERITY_MAX_CORRUPTED_ERRS)
+                       goto out;
+       } while (!atomic_try_cmpxchg(&v->corrupted_errs, &ce, ce + 1));
 
        switch (type) {
        case DM_VERITY_BLOCK_TYPE_DATA:
@@ -203,7 +205,7 @@ static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
        DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
                    type_str, block);
 
-       if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS) {
+       if (ce + 1 == DM_VERITY_MAX_CORRUPTED_ERRS) {
                DMERR("%s: reached maximum errors", v->data_dev->name);
                dm_audit_log_target(DM_MSG_PREFIX, "max-corrupted-errors", v->ti, 0);
        }
index 2922263501f68d6ef02f509ccd4a0bcf4e13663e..e104a651c6574cf6e478491c3e515e228172a7ab 100644 (file)
@@ -68,7 +68,7 @@ struct dm_verity {
        unsigned int digest_size;       /* digest size for the current hash algorithm */
        enum verity_mode mode;  /* mode for handling verification errors */
        enum verity_mode error_mode;/* mode for handling I/O errors */
-       unsigned int corrupted_errs;/* Number of errors for corrupted blocks */
+       atomic_t corrupted_errs;/* Number of errors for corrupted blocks */
 
        struct workqueue_struct *verify_wq;