Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2021 Microsoft Corporation
0004  *
0005  * Author: Tushar Sugandhi <tusharsu@linux.microsoft.com>
0006  *
0007  * File: dm-ima.c
0008  *       Enables IMA measurements for DM targets
0009  */
0010 
0011 #include "dm-core.h"
0012 #include "dm-ima.h"
0013 
0014 #include <linux/ima.h>
0015 #include <linux/sched/mm.h>
0016 #include <crypto/hash.h>
0017 #include <linux/crypto.h>
0018 #include <crypto/hash_info.h>
0019 
0020 #define DM_MSG_PREFIX "ima"
0021 
0022 /*
0023  * Internal function to prefix separator characters in input buffer with escape
0024  * character, so that they don't interfere with the construction of key-value pairs,
0025  * and clients can split the key1=val1,key2=val2,key3=val3; pairs properly.
0026  */
0027 static void fix_separator_chars(char **buf)
0028 {
0029     int l = strlen(*buf);
0030     int i, j, sp = 0;
0031 
0032     for (i = 0; i < l; i++)
0033         if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=' || (*buf)[i] == ',')
0034             sp++;
0035 
0036     if (!sp)
0037         return;
0038 
0039     for (i = l-1, j = i+sp; i >= 0; i--) {
0040         (*buf)[j--] = (*buf)[i];
0041         if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=' || (*buf)[i] == ',')
0042             (*buf)[j--] = '\\';
0043     }
0044 }
0045 
0046 /*
0047  * Internal function to allocate memory for IMA measurements.
0048  */
0049 static void *dm_ima_alloc(size_t len, gfp_t flags, bool noio)
0050 {
0051     unsigned int noio_flag;
0052     void *ptr;
0053 
0054     if (noio)
0055         noio_flag = memalloc_noio_save();
0056 
0057     ptr = kzalloc(len, flags);
0058 
0059     if (noio)
0060         memalloc_noio_restore(noio_flag);
0061 
0062     return ptr;
0063 }
0064 
0065 /*
0066  * Internal function to allocate and copy name and uuid for IMA measurements.
0067  */
0068 static int dm_ima_alloc_and_copy_name_uuid(struct mapped_device *md, char **dev_name,
0069                        char **dev_uuid, bool noio)
0070 {
0071     int r;
0072     *dev_name = dm_ima_alloc(DM_NAME_LEN*2, GFP_KERNEL, noio);
0073     if (!(*dev_name)) {
0074         r = -ENOMEM;
0075         goto error;
0076     }
0077 
0078     *dev_uuid = dm_ima_alloc(DM_UUID_LEN*2, GFP_KERNEL, noio);
0079     if (!(*dev_uuid)) {
0080         r = -ENOMEM;
0081         goto error;
0082     }
0083 
0084     r = dm_copy_name_and_uuid(md, *dev_name, *dev_uuid);
0085     if (r)
0086         goto error;
0087 
0088     fix_separator_chars(dev_name);
0089     fix_separator_chars(dev_uuid);
0090 
0091     return 0;
0092 error:
0093     kfree(*dev_name);
0094     kfree(*dev_uuid);
0095     *dev_name = NULL;
0096     *dev_uuid = NULL;
0097     return r;
0098 }
0099 
0100 /*
0101  * Internal function to allocate and copy device data for IMA measurements.
0102  */
0103 static int dm_ima_alloc_and_copy_device_data(struct mapped_device *md, char **device_data,
0104                          unsigned int num_targets, bool noio)
0105 {
0106     char *dev_name = NULL, *dev_uuid = NULL;
0107     int r;
0108 
0109     r = dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio);
0110     if (r)
0111         return r;
0112 
0113     *device_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
0114     if (!(*device_data)) {
0115         r = -ENOMEM;
0116         goto error;
0117     }
0118 
0119     scnprintf(*device_data, DM_IMA_DEVICE_BUF_LEN,
0120           "name=%s,uuid=%s,major=%d,minor=%d,minor_count=%d,num_targets=%u;",
0121           dev_name, dev_uuid, md->disk->major, md->disk->first_minor,
0122           md->disk->minors, num_targets);
0123 error:
0124     kfree(dev_name);
0125     kfree(dev_uuid);
0126     return r;
0127 }
0128 
0129 /*
0130  * Internal wrapper function to call IMA to measure DM data.
0131  */
0132 static void dm_ima_measure_data(const char *event_name, const void *buf, size_t buf_len,
0133                 bool noio)
0134 {
0135     unsigned int noio_flag;
0136 
0137     if (noio)
0138         noio_flag = memalloc_noio_save();
0139 
0140     ima_measure_critical_data(DM_NAME, event_name, buf, buf_len,
0141                   false, NULL, 0);
0142 
0143     if (noio)
0144         memalloc_noio_restore(noio_flag);
0145 }
0146 
0147 /*
0148  * Internal function to allocate and copy current device capacity for IMA measurements.
0149  */
0150 static int dm_ima_alloc_and_copy_capacity_str(struct mapped_device *md, char **capacity_str,
0151                           bool noio)
0152 {
0153     sector_t capacity;
0154 
0155     capacity = get_capacity(md->disk);
0156 
0157     *capacity_str = dm_ima_alloc(DM_IMA_DEVICE_CAPACITY_BUF_LEN, GFP_KERNEL, noio);
0158     if (!(*capacity_str))
0159         return -ENOMEM;
0160 
0161     scnprintf(*capacity_str, DM_IMA_DEVICE_BUF_LEN, "current_device_capacity=%llu;",
0162           capacity);
0163 
0164     return 0;
0165 }
0166 
0167 /*
0168  * Initialize/reset the dm ima related data structure variables.
0169  */
0170 void dm_ima_reset_data(struct mapped_device *md)
0171 {
0172     memset(&(md->ima), 0, sizeof(md->ima));
0173     md->ima.dm_version_str_len = strlen(DM_IMA_VERSION_STR);
0174 }
0175 
0176 /*
0177  * Build up the IMA data for each target, and finally measure.
0178  */
0179 void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flags)
0180 {
0181     size_t device_data_buf_len, target_metadata_buf_len, target_data_buf_len, l = 0;
0182     char *target_metadata_buf = NULL, *target_data_buf = NULL, *digest_buf = NULL;
0183     char *ima_buf = NULL, *device_data_buf = NULL;
0184     int digest_size, last_target_measured = -1, r;
0185     status_type_t type = STATUSTYPE_IMA;
0186     size_t cur_total_buf_len = 0;
0187     unsigned int num_targets, i;
0188     SHASH_DESC_ON_STACK(shash, NULL);
0189     struct crypto_shash *tfm = NULL;
0190     u8 *digest = NULL;
0191     bool noio = false;
0192     /*
0193      * In below hash_alg_prefix_len assignment +1 is for the additional char (':'),
0194      * when prefixing the hash value with the hash algorithm name. e.g. sha256:<hash_value>.
0195      */
0196     const size_t hash_alg_prefix_len = strlen(DM_IMA_TABLE_HASH_ALG) + 1;
0197     char table_load_event_name[] = "dm_table_load";
0198 
0199     ima_buf = dm_ima_alloc(DM_IMA_MEASUREMENT_BUF_LEN, GFP_KERNEL, noio);
0200     if (!ima_buf)
0201         return;
0202 
0203     target_metadata_buf = dm_ima_alloc(DM_IMA_TARGET_METADATA_BUF_LEN, GFP_KERNEL, noio);
0204     if (!target_metadata_buf)
0205         goto error;
0206 
0207     target_data_buf = dm_ima_alloc(DM_IMA_TARGET_DATA_BUF_LEN, GFP_KERNEL, noio);
0208     if (!target_data_buf)
0209         goto error;
0210 
0211     num_targets = table->num_targets;
0212 
0213     if (dm_ima_alloc_and_copy_device_data(table->md, &device_data_buf, num_targets, noio))
0214         goto error;
0215 
0216     tfm = crypto_alloc_shash(DM_IMA_TABLE_HASH_ALG, 0, 0);
0217     if (IS_ERR(tfm))
0218         goto error;
0219 
0220     shash->tfm = tfm;
0221     digest_size = crypto_shash_digestsize(tfm);
0222     digest = dm_ima_alloc(digest_size, GFP_KERNEL, noio);
0223     if (!digest)
0224         goto error;
0225 
0226     r = crypto_shash_init(shash);
0227     if (r)
0228         goto error;
0229 
0230     memcpy(ima_buf + l, DM_IMA_VERSION_STR, table->md->ima.dm_version_str_len);
0231     l += table->md->ima.dm_version_str_len;
0232 
0233     device_data_buf_len = strlen(device_data_buf);
0234     memcpy(ima_buf + l, device_data_buf, device_data_buf_len);
0235     l += device_data_buf_len;
0236 
0237     for (i = 0; i < num_targets; i++) {
0238         struct dm_target *ti = dm_table_get_target(table, i);
0239 
0240         last_target_measured = 0;
0241 
0242         /*
0243          * First retrieve the target metadata.
0244          */
0245         scnprintf(target_metadata_buf, DM_IMA_TARGET_METADATA_BUF_LEN,
0246               "target_index=%d,target_begin=%llu,target_len=%llu,",
0247               i, ti->begin, ti->len);
0248         target_metadata_buf_len = strlen(target_metadata_buf);
0249 
0250         /*
0251          * Then retrieve the actual target data.
0252          */
0253         if (ti->type->status)
0254             ti->type->status(ti, type, status_flags, target_data_buf,
0255                      DM_IMA_TARGET_DATA_BUF_LEN);
0256         else
0257             target_data_buf[0] = '\0';
0258 
0259         target_data_buf_len = strlen(target_data_buf);
0260 
0261         /*
0262          * Check if the total data can fit into the IMA buffer.
0263          */
0264         cur_total_buf_len = l + target_metadata_buf_len + target_data_buf_len;
0265 
0266         /*
0267          * IMA measurements for DM targets are best-effort.
0268          * If the total data buffered so far, including the current target,
0269          * is too large to fit into DM_IMA_MEASUREMENT_BUF_LEN, measure what
0270          * we have in the current buffer, and continue measuring the remaining
0271          * targets by prefixing the device metadata again.
0272          */
0273         if (unlikely(cur_total_buf_len >= DM_IMA_MEASUREMENT_BUF_LEN)) {
0274             dm_ima_measure_data(table_load_event_name, ima_buf, l, noio);
0275             r = crypto_shash_update(shash, (const u8 *)ima_buf, l);
0276             if (r < 0)
0277                 goto error;
0278 
0279             memset(ima_buf, 0, DM_IMA_MEASUREMENT_BUF_LEN);
0280             l = 0;
0281 
0282             /*
0283              * Each new "dm_table_load" entry in IMA log should have device data
0284              * prefix, so that multiple records from the same "dm_table_load" for
0285              * a given device can be linked together.
0286              */
0287             memcpy(ima_buf + l, DM_IMA_VERSION_STR, table->md->ima.dm_version_str_len);
0288             l += table->md->ima.dm_version_str_len;
0289 
0290             memcpy(ima_buf + l, device_data_buf, device_data_buf_len);
0291             l += device_data_buf_len;
0292 
0293             /*
0294              * If this iteration of the for loop turns out to be the last target
0295              * in the table, dm_ima_measure_data("dm_table_load", ...) doesn't need
0296              * to be called again, just the hash needs to be finalized.
0297              * "last_target_measured" tracks this state.
0298              */
0299             last_target_measured = 1;
0300         }
0301 
0302         /*
0303          * Fill-in all the target metadata, so that multiple targets for the same
0304          * device can be linked together.
0305          */
0306         memcpy(ima_buf + l, target_metadata_buf, target_metadata_buf_len);
0307         l += target_metadata_buf_len;
0308 
0309         memcpy(ima_buf + l, target_data_buf, target_data_buf_len);
0310         l += target_data_buf_len;
0311     }
0312 
0313     if (!last_target_measured) {
0314         dm_ima_measure_data(table_load_event_name, ima_buf, l, noio);
0315 
0316         r = crypto_shash_update(shash, (const u8 *)ima_buf, l);
0317         if (r < 0)
0318             goto error;
0319     }
0320 
0321     /*
0322      * Finalize the table hash, and store it in table->md->ima.inactive_table.hash,
0323      * so that the table data can be verified against the future device state change
0324      * events, e.g. resume, rename, remove, table-clear etc.
0325      */
0326     r = crypto_shash_final(shash, digest);
0327     if (r < 0)
0328         goto error;
0329 
0330     digest_buf = dm_ima_alloc((digest_size*2) + hash_alg_prefix_len + 1, GFP_KERNEL, noio);
0331 
0332     if (!digest_buf)
0333         goto error;
0334 
0335     snprintf(digest_buf, hash_alg_prefix_len + 1, "%s:", DM_IMA_TABLE_HASH_ALG);
0336 
0337     for (i = 0; i < digest_size; i++)
0338         snprintf((digest_buf + hash_alg_prefix_len + (i*2)), 3, "%02x", digest[i]);
0339 
0340     if (table->md->ima.active_table.hash != table->md->ima.inactive_table.hash)
0341         kfree(table->md->ima.inactive_table.hash);
0342 
0343     table->md->ima.inactive_table.hash = digest_buf;
0344     table->md->ima.inactive_table.hash_len = strlen(digest_buf);
0345     table->md->ima.inactive_table.num_targets = num_targets;
0346 
0347     if (table->md->ima.active_table.device_metadata !=
0348         table->md->ima.inactive_table.device_metadata)
0349         kfree(table->md->ima.inactive_table.device_metadata);
0350 
0351     table->md->ima.inactive_table.device_metadata = device_data_buf;
0352     table->md->ima.inactive_table.device_metadata_len = device_data_buf_len;
0353 
0354     goto exit;
0355 error:
0356     kfree(digest_buf);
0357     kfree(device_data_buf);
0358 exit:
0359     kfree(digest);
0360     if (tfm)
0361         crypto_free_shash(tfm);
0362     kfree(ima_buf);
0363     kfree(target_metadata_buf);
0364     kfree(target_data_buf);
0365 }
0366 
0367 /*
0368  * Measure IMA data on device resume.
0369  */
0370 void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)
0371 {
0372     char *device_table_data, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
0373     char active[] = "active_table_hash=";
0374     unsigned int active_len = strlen(active), capacity_len = 0;
0375     unsigned int l = 0;
0376     bool noio = true;
0377     bool nodata = true;
0378     int r;
0379 
0380     device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
0381     if (!device_table_data)
0382         return;
0383 
0384     r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
0385     if (r)
0386         goto error;
0387 
0388     memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
0389     l += md->ima.dm_version_str_len;
0390 
0391     if (swap) {
0392         if (md->ima.active_table.hash != md->ima.inactive_table.hash)
0393             kfree(md->ima.active_table.hash);
0394 
0395         md->ima.active_table.hash = NULL;
0396         md->ima.active_table.hash_len = 0;
0397 
0398         if (md->ima.active_table.device_metadata !=
0399             md->ima.inactive_table.device_metadata)
0400             kfree(md->ima.active_table.device_metadata);
0401 
0402         md->ima.active_table.device_metadata = NULL;
0403         md->ima.active_table.device_metadata_len = 0;
0404         md->ima.active_table.num_targets = 0;
0405 
0406         if (md->ima.inactive_table.hash) {
0407             md->ima.active_table.hash = md->ima.inactive_table.hash;
0408             md->ima.active_table.hash_len = md->ima.inactive_table.hash_len;
0409             md->ima.inactive_table.hash = NULL;
0410             md->ima.inactive_table.hash_len = 0;
0411         }
0412 
0413         if (md->ima.inactive_table.device_metadata) {
0414             md->ima.active_table.device_metadata =
0415                 md->ima.inactive_table.device_metadata;
0416             md->ima.active_table.device_metadata_len =
0417                 md->ima.inactive_table.device_metadata_len;
0418             md->ima.active_table.num_targets = md->ima.inactive_table.num_targets;
0419             md->ima.inactive_table.device_metadata = NULL;
0420             md->ima.inactive_table.device_metadata_len = 0;
0421             md->ima.inactive_table.num_targets = 0;
0422         }
0423     }
0424 
0425     if (md->ima.active_table.device_metadata) {
0426         memcpy(device_table_data + l, md->ima.active_table.device_metadata,
0427                md->ima.active_table.device_metadata_len);
0428         l += md->ima.active_table.device_metadata_len;
0429 
0430         nodata = false;
0431     }
0432 
0433     if (md->ima.active_table.hash) {
0434         memcpy(device_table_data + l, active, active_len);
0435         l += active_len;
0436 
0437         memcpy(device_table_data + l, md->ima.active_table.hash,
0438                md->ima.active_table.hash_len);
0439         l += md->ima.active_table.hash_len;
0440 
0441         memcpy(device_table_data + l, ";", 1);
0442         l++;
0443 
0444         nodata = false;
0445     }
0446 
0447     if (nodata) {
0448         r = dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio);
0449         if (r)
0450             goto error;
0451 
0452         scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
0453               "%sname=%s,uuid=%s;device_resume=no_data;",
0454               DM_IMA_VERSION_STR, dev_name, dev_uuid);
0455         l = strlen(device_table_data);
0456 
0457     }
0458 
0459     capacity_len = strlen(capacity_str);
0460     memcpy(device_table_data + l, capacity_str, capacity_len);
0461     l += capacity_len;
0462 
0463     dm_ima_measure_data("dm_device_resume", device_table_data, l, noio);
0464 
0465     kfree(dev_name);
0466     kfree(dev_uuid);
0467 error:
0468     kfree(capacity_str);
0469     kfree(device_table_data);
0470 }
0471 
0472 /*
0473  * Measure IMA data on remove.
0474  */
0475 void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)
0476 {
0477     char *device_table_data, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
0478     char active_table_str[] = "active_table_hash=";
0479     char inactive_table_str[] = "inactive_table_hash=";
0480     char device_active_str[] = "device_active_metadata=";
0481     char device_inactive_str[] = "device_inactive_metadata=";
0482     char remove_all_str[] = "remove_all=";
0483     unsigned int active_table_len = strlen(active_table_str);
0484     unsigned int inactive_table_len = strlen(inactive_table_str);
0485     unsigned int device_active_len = strlen(device_active_str);
0486     unsigned int device_inactive_len = strlen(device_inactive_str);
0487     unsigned int remove_all_len = strlen(remove_all_str);
0488     unsigned int capacity_len = 0;
0489     unsigned int l = 0;
0490     bool noio = true;
0491     bool nodata = true;
0492     int r;
0493 
0494     device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN*2, GFP_KERNEL, noio);
0495     if (!device_table_data)
0496         goto exit;
0497 
0498     r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
0499     if (r) {
0500         kfree(device_table_data);
0501         goto exit;
0502     }
0503 
0504     memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
0505     l += md->ima.dm_version_str_len;
0506 
0507     if (md->ima.active_table.device_metadata) {
0508         memcpy(device_table_data + l, device_active_str, device_active_len);
0509         l += device_active_len;
0510 
0511         memcpy(device_table_data + l, md->ima.active_table.device_metadata,
0512                md->ima.active_table.device_metadata_len);
0513         l += md->ima.active_table.device_metadata_len;
0514 
0515         nodata = false;
0516     }
0517 
0518     if (md->ima.inactive_table.device_metadata) {
0519         memcpy(device_table_data + l, device_inactive_str, device_inactive_len);
0520         l += device_inactive_len;
0521 
0522         memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
0523                md->ima.inactive_table.device_metadata_len);
0524         l += md->ima.inactive_table.device_metadata_len;
0525 
0526         nodata = false;
0527     }
0528 
0529     if (md->ima.active_table.hash) {
0530         memcpy(device_table_data + l, active_table_str, active_table_len);
0531         l += active_table_len;
0532 
0533         memcpy(device_table_data + l, md->ima.active_table.hash,
0534                md->ima.active_table.hash_len);
0535         l += md->ima.active_table.hash_len;
0536 
0537         memcpy(device_table_data + l, ",", 1);
0538         l++;
0539 
0540         nodata = false;
0541     }
0542 
0543     if (md->ima.inactive_table.hash) {
0544         memcpy(device_table_data + l, inactive_table_str, inactive_table_len);
0545         l += inactive_table_len;
0546 
0547         memcpy(device_table_data + l, md->ima.inactive_table.hash,
0548                md->ima.inactive_table.hash_len);
0549         l += md->ima.inactive_table.hash_len;
0550 
0551         memcpy(device_table_data + l, ",", 1);
0552         l++;
0553 
0554         nodata = false;
0555     }
0556     /*
0557      * In case both active and inactive tables, and corresponding
0558      * device metadata is cleared/missing - record the name and uuid
0559      * in IMA measurements.
0560      */
0561     if (nodata) {
0562         if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
0563             goto error;
0564 
0565         scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
0566               "%sname=%s,uuid=%s;device_remove=no_data;",
0567               DM_IMA_VERSION_STR, dev_name, dev_uuid);
0568         l = strlen(device_table_data);
0569     }
0570 
0571     memcpy(device_table_data + l, remove_all_str, remove_all_len);
0572     l += remove_all_len;
0573     memcpy(device_table_data + l, remove_all ? "y;" : "n;", 2);
0574     l += 2;
0575 
0576     capacity_len = strlen(capacity_str);
0577     memcpy(device_table_data + l, capacity_str, capacity_len);
0578     l += capacity_len;
0579 
0580     dm_ima_measure_data("dm_device_remove", device_table_data, l, noio);
0581 
0582 error:
0583     kfree(device_table_data);
0584     kfree(capacity_str);
0585 exit:
0586     kfree(md->ima.active_table.device_metadata);
0587 
0588     if (md->ima.active_table.device_metadata !=
0589         md->ima.inactive_table.device_metadata)
0590         kfree(md->ima.inactive_table.device_metadata);
0591 
0592     kfree(md->ima.active_table.hash);
0593 
0594     if (md->ima.active_table.hash != md->ima.inactive_table.hash)
0595         kfree(md->ima.inactive_table.hash);
0596 
0597     dm_ima_reset_data(md);
0598 
0599     kfree(dev_name);
0600     kfree(dev_uuid);
0601 }
0602 
0603 /*
0604  * Measure ima data on table clear.
0605  */
0606 void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)
0607 {
0608     unsigned int l = 0, capacity_len = 0;
0609     char *device_table_data = NULL, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
0610     char inactive_str[] = "inactive_table_hash=";
0611     unsigned int inactive_len = strlen(inactive_str);
0612     bool noio = true;
0613     bool nodata = true;
0614     int r;
0615 
0616     device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
0617     if (!device_table_data)
0618         return;
0619 
0620     r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
0621     if (r)
0622         goto error1;
0623 
0624     memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
0625     l += md->ima.dm_version_str_len;
0626 
0627     if (md->ima.inactive_table.device_metadata_len &&
0628         md->ima.inactive_table.hash_len) {
0629         memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
0630                md->ima.inactive_table.device_metadata_len);
0631         l += md->ima.inactive_table.device_metadata_len;
0632 
0633         memcpy(device_table_data + l, inactive_str, inactive_len);
0634         l += inactive_len;
0635 
0636         memcpy(device_table_data + l, md->ima.inactive_table.hash,
0637                md->ima.inactive_table.hash_len);
0638 
0639         l += md->ima.inactive_table.hash_len;
0640 
0641         memcpy(device_table_data + l, ";", 1);
0642         l++;
0643 
0644         nodata = false;
0645     }
0646 
0647     if (nodata) {
0648         if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
0649             goto error2;
0650 
0651         scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
0652               "%sname=%s,uuid=%s;table_clear=no_data;",
0653                DM_IMA_VERSION_STR, dev_name, dev_uuid);
0654         l = strlen(device_table_data);
0655     }
0656 
0657     capacity_len = strlen(capacity_str);
0658     memcpy(device_table_data + l, capacity_str, capacity_len);
0659     l += capacity_len;
0660 
0661     dm_ima_measure_data("dm_table_clear", device_table_data, l, noio);
0662 
0663     if (new_map) {
0664         if (md->ima.inactive_table.hash &&
0665             md->ima.inactive_table.hash != md->ima.active_table.hash)
0666             kfree(md->ima.inactive_table.hash);
0667 
0668         md->ima.inactive_table.hash = NULL;
0669         md->ima.inactive_table.hash_len = 0;
0670 
0671         if (md->ima.inactive_table.device_metadata &&
0672             md->ima.inactive_table.device_metadata != md->ima.active_table.device_metadata)
0673             kfree(md->ima.inactive_table.device_metadata);
0674 
0675         md->ima.inactive_table.device_metadata = NULL;
0676         md->ima.inactive_table.device_metadata_len = 0;
0677         md->ima.inactive_table.num_targets = 0;
0678 
0679         if (md->ima.active_table.hash) {
0680             md->ima.inactive_table.hash = md->ima.active_table.hash;
0681             md->ima.inactive_table.hash_len = md->ima.active_table.hash_len;
0682         }
0683 
0684         if (md->ima.active_table.device_metadata) {
0685             md->ima.inactive_table.device_metadata =
0686                 md->ima.active_table.device_metadata;
0687             md->ima.inactive_table.device_metadata_len =
0688                 md->ima.active_table.device_metadata_len;
0689             md->ima.inactive_table.num_targets =
0690                 md->ima.active_table.num_targets;
0691         }
0692     }
0693 
0694     kfree(dev_name);
0695     kfree(dev_uuid);
0696 error2:
0697     kfree(capacity_str);
0698 error1:
0699     kfree(device_table_data);
0700 }
0701 
0702 /*
0703  * Measure IMA data on device rename.
0704  */
0705 void dm_ima_measure_on_device_rename(struct mapped_device *md)
0706 {
0707     char *old_device_data = NULL, *new_device_data = NULL, *combined_device_data = NULL;
0708     char *new_dev_name = NULL, *new_dev_uuid = NULL, *capacity_str = NULL;
0709     bool noio = true;
0710     int r;
0711 
0712     if (dm_ima_alloc_and_copy_device_data(md, &new_device_data,
0713                           md->ima.active_table.num_targets, noio))
0714         return;
0715 
0716     if (dm_ima_alloc_and_copy_name_uuid(md, &new_dev_name, &new_dev_uuid, noio))
0717         goto error;
0718 
0719     combined_device_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN * 2, GFP_KERNEL, noio);
0720     if (!combined_device_data)
0721         goto error;
0722 
0723     r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
0724     if (r)
0725         goto error;
0726 
0727     old_device_data = md->ima.active_table.device_metadata;
0728 
0729     md->ima.active_table.device_metadata = new_device_data;
0730     md->ima.active_table.device_metadata_len = strlen(new_device_data);
0731 
0732     scnprintf(combined_device_data, DM_IMA_DEVICE_BUF_LEN * 2,
0733           "%s%snew_name=%s,new_uuid=%s;%s", DM_IMA_VERSION_STR, old_device_data,
0734           new_dev_name, new_dev_uuid, capacity_str);
0735 
0736     dm_ima_measure_data("dm_device_rename", combined_device_data, strlen(combined_device_data),
0737                 noio);
0738 
0739     goto exit;
0740 
0741 error:
0742     kfree(new_device_data);
0743 exit:
0744     kfree(capacity_str);
0745     kfree(combined_device_data);
0746     kfree(old_device_data);
0747     kfree(new_dev_name);
0748     kfree(new_dev_uuid);
0749 }