1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2021 Microsoft Corporation
4 *
5 * Author: Tushar Sugandhi <tusharsu@linux.microsoft.com>
6 *
7 * Enables IMA measurements for DM targets
8 */
9
10 #include "dm-core.h"
11 #include "dm-ima.h"
12
13 #include <linux/ima.h>
14 #include <linux/sched/mm.h>
15 #include <crypto/hash.h>
16 #include <linux/crypto.h>
17 #include <crypto/hash_info.h>
18
19 #define DM_MSG_PREFIX "ima"
20
21 /*
22 * Internal function to prefix separator characters in input buffer with escape
23 * character, so that they don't interfere with the construction of key-value pairs,
24 * and clients can split the key1=val1,key2=val2,key3=val3; pairs properly.
25 */
fix_separator_chars(char ** buf)26 static void fix_separator_chars(char **buf)
27 {
28 int l = strlen(*buf);
29 int i, j, sp = 0;
30
31 for (i = 0; i < l; i++)
32 if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=' || (*buf)[i] == ',')
33 sp++;
34
35 if (!sp)
36 return;
37
38 for (i = l-1, j = i+sp; i >= 0; i--) {
39 (*buf)[j--] = (*buf)[i];
40 if ((*buf)[i] == '\\' || (*buf)[i] == ';' || (*buf)[i] == '=' || (*buf)[i] == ',')
41 (*buf)[j--] = '\\';
42 }
43 }
44
45 /*
46 * Internal function to allocate memory for IMA measurements.
47 */
dm_ima_alloc(size_t len,gfp_t flags,bool noio)48 static void *dm_ima_alloc(size_t len, gfp_t flags, bool noio)
49 {
50 unsigned int noio_flag;
51 void *ptr;
52
53 if (noio)
54 noio_flag = memalloc_noio_save();
55
56 ptr = kzalloc(len, flags);
57
58 if (noio)
59 memalloc_noio_restore(noio_flag);
60
61 return ptr;
62 }
63
64 /*
65 * Internal function to allocate and copy name and uuid for IMA measurements.
66 */
dm_ima_alloc_and_copy_name_uuid(struct mapped_device * md,char ** dev_name,char ** dev_uuid,bool noio)67 static int dm_ima_alloc_and_copy_name_uuid(struct mapped_device *md, char **dev_name,
68 char **dev_uuid, bool noio)
69 {
70 int r;
71 *dev_name = dm_ima_alloc(DM_NAME_LEN*2, GFP_KERNEL, noio);
72 if (!(*dev_name)) {
73 r = -ENOMEM;
74 goto error;
75 }
76
77 *dev_uuid = dm_ima_alloc(DM_UUID_LEN*2, GFP_KERNEL, noio);
78 if (!(*dev_uuid)) {
79 r = -ENOMEM;
80 goto error;
81 }
82
83 r = dm_copy_name_and_uuid(md, *dev_name, *dev_uuid);
84 if (r)
85 goto error;
86
87 fix_separator_chars(dev_name);
88 fix_separator_chars(dev_uuid);
89
90 return 0;
91 error:
92 kfree(*dev_name);
93 kfree(*dev_uuid);
94 *dev_name = NULL;
95 *dev_uuid = NULL;
96 return r;
97 }
98
99 /*
100 * Internal function to allocate and copy device data for IMA measurements.
101 */
dm_ima_alloc_and_copy_device_data(struct mapped_device * md,char ** device_data,unsigned int num_targets,bool noio)102 static int dm_ima_alloc_and_copy_device_data(struct mapped_device *md, char **device_data,
103 unsigned int num_targets, bool noio)
104 {
105 char *dev_name = NULL, *dev_uuid = NULL;
106 int r;
107
108 r = dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio);
109 if (r)
110 return r;
111
112 *device_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
113 if (!(*device_data)) {
114 r = -ENOMEM;
115 goto error;
116 }
117
118 scnprintf(*device_data, DM_IMA_DEVICE_BUF_LEN,
119 "name=%s,uuid=%s,major=%d,minor=%d,minor_count=%d,num_targets=%u;",
120 dev_name, dev_uuid, md->disk->major, md->disk->first_minor,
121 md->disk->minors, num_targets);
122 error:
123 kfree(dev_name);
124 kfree(dev_uuid);
125 return r;
126 }
127
128 /*
129 * Internal wrapper function to call IMA to measure DM data.
130 */
dm_ima_measure_data(const char * event_name,const void * buf,size_t buf_len,bool noio)131 static void dm_ima_measure_data(const char *event_name, const void *buf, size_t buf_len,
132 bool noio)
133 {
134 unsigned int noio_flag;
135
136 if (noio)
137 noio_flag = memalloc_noio_save();
138
139 ima_measure_critical_data(DM_NAME, event_name, buf, buf_len,
140 false, NULL, 0);
141
142 if (noio)
143 memalloc_noio_restore(noio_flag);
144 }
145
146 /*
147 * Internal function to allocate and copy current device capacity for IMA measurements.
148 */
dm_ima_alloc_and_copy_capacity_str(struct mapped_device * md,char ** capacity_str,bool noio)149 static int dm_ima_alloc_and_copy_capacity_str(struct mapped_device *md, char **capacity_str,
150 bool noio)
151 {
152 sector_t capacity;
153
154 capacity = get_capacity(md->disk);
155
156 *capacity_str = dm_ima_alloc(DM_IMA_DEVICE_CAPACITY_BUF_LEN, GFP_KERNEL, noio);
157 if (!(*capacity_str))
158 return -ENOMEM;
159
160 scnprintf(*capacity_str, DM_IMA_DEVICE_BUF_LEN, "current_device_capacity=%llu;",
161 capacity);
162
163 return 0;
164 }
165
166 /*
167 * Initialize/reset the dm ima related data structure variables.
168 */
dm_ima_reset_data(struct mapped_device * md)169 void dm_ima_reset_data(struct mapped_device *md)
170 {
171 memset(&(md->ima), 0, sizeof(md->ima));
172 md->ima.dm_version_str_len = strlen(DM_IMA_VERSION_STR);
173 }
174
175 /*
176 * Build up the IMA data for each target, and finally measure.
177 */
dm_ima_measure_on_table_load(struct dm_table * table,unsigned int status_flags)178 void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flags)
179 {
180 size_t device_data_buf_len, target_metadata_buf_len, target_data_buf_len, l = 0;
181 char *target_metadata_buf = NULL, *target_data_buf = NULL, *digest_buf = NULL;
182 char *ima_buf = NULL, *device_data_buf = NULL;
183 int digest_size, last_target_measured = -1, r;
184 status_type_t type = STATUSTYPE_IMA;
185 size_t cur_total_buf_len = 0;
186 unsigned int num_targets, i;
187 SHASH_DESC_ON_STACK(shash, NULL);
188 struct crypto_shash *tfm = NULL;
189 u8 *digest = NULL;
190 bool noio = false;
191 /*
192 * In below hash_alg_prefix_len assignment +1 is for the additional char (':'),
193 * when prefixing the hash value with the hash algorithm name. e.g. sha256:<hash_value>.
194 */
195 const size_t hash_alg_prefix_len = strlen(DM_IMA_TABLE_HASH_ALG) + 1;
196 char table_load_event_name[] = "dm_table_load";
197
198 ima_buf = dm_ima_alloc(DM_IMA_MEASUREMENT_BUF_LEN, GFP_KERNEL, noio);
199 if (!ima_buf)
200 return;
201
202 target_metadata_buf = dm_ima_alloc(DM_IMA_TARGET_METADATA_BUF_LEN, GFP_KERNEL, noio);
203 if (!target_metadata_buf)
204 goto error;
205
206 target_data_buf = dm_ima_alloc(DM_IMA_TARGET_DATA_BUF_LEN, GFP_KERNEL, noio);
207 if (!target_data_buf)
208 goto error;
209
210 num_targets = table->num_targets;
211
212 if (dm_ima_alloc_and_copy_device_data(table->md, &device_data_buf, num_targets, noio))
213 goto error;
214
215 tfm = crypto_alloc_shash(DM_IMA_TABLE_HASH_ALG, 0, 0);
216 if (IS_ERR(tfm))
217 goto error;
218
219 shash->tfm = tfm;
220 digest_size = crypto_shash_digestsize(tfm);
221 digest = dm_ima_alloc(digest_size, GFP_KERNEL, noio);
222 if (!digest)
223 goto error;
224
225 r = crypto_shash_init(shash);
226 if (r)
227 goto error;
228
229 memcpy(ima_buf + l, DM_IMA_VERSION_STR, table->md->ima.dm_version_str_len);
230 l += table->md->ima.dm_version_str_len;
231
232 device_data_buf_len = strlen(device_data_buf);
233 memcpy(ima_buf + l, device_data_buf, device_data_buf_len);
234 l += device_data_buf_len;
235
236 for (i = 0; i < num_targets; i++) {
237 struct dm_target *ti = dm_table_get_target(table, i);
238
239 last_target_measured = 0;
240
241 /*
242 * First retrieve the target metadata.
243 */
244 target_metadata_buf_len =
245 scnprintf(target_metadata_buf,
246 DM_IMA_TARGET_METADATA_BUF_LEN,
247 "target_index=%d,target_begin=%llu,target_len=%llu,",
248 i, ti->begin, ti->len);
249
250 /*
251 * Then retrieve the actual target data.
252 */
253 if (ti->type->status)
254 ti->type->status(ti, type, status_flags, target_data_buf,
255 DM_IMA_TARGET_DATA_BUF_LEN);
256 else
257 target_data_buf[0] = '\0';
258
259 target_data_buf_len = strlen(target_data_buf);
260
261 /*
262 * Check if the total data can fit into the IMA buffer.
263 */
264 cur_total_buf_len = l + target_metadata_buf_len + target_data_buf_len;
265
266 /*
267 * IMA measurements for DM targets are best-effort.
268 * If the total data buffered so far, including the current target,
269 * is too large to fit into DM_IMA_MEASUREMENT_BUF_LEN, measure what
270 * we have in the current buffer, and continue measuring the remaining
271 * targets by prefixing the device metadata again.
272 */
273 if (unlikely(cur_total_buf_len >= DM_IMA_MEASUREMENT_BUF_LEN)) {
274 dm_ima_measure_data(table_load_event_name, ima_buf, l, noio);
275 r = crypto_shash_update(shash, (const u8 *)ima_buf, l);
276 if (r < 0)
277 goto error;
278
279 memset(ima_buf, 0, DM_IMA_MEASUREMENT_BUF_LEN);
280 l = 0;
281
282 /*
283 * Each new "dm_table_load" entry in IMA log should have device data
284 * prefix, so that multiple records from the same "dm_table_load" for
285 * a given device can be linked together.
286 */
287 memcpy(ima_buf + l, DM_IMA_VERSION_STR, table->md->ima.dm_version_str_len);
288 l += table->md->ima.dm_version_str_len;
289
290 memcpy(ima_buf + l, device_data_buf, device_data_buf_len);
291 l += device_data_buf_len;
292
293 /*
294 * If this iteration of the for loop turns out to be the last target
295 * in the table, dm_ima_measure_data("dm_table_load", ...) doesn't need
296 * to be called again, just the hash needs to be finalized.
297 * "last_target_measured" tracks this state.
298 */
299 last_target_measured = 1;
300 }
301
302 /*
303 * Fill-in all the target metadata, so that multiple targets for the same
304 * device can be linked together.
305 */
306 memcpy(ima_buf + l, target_metadata_buf, target_metadata_buf_len);
307 l += target_metadata_buf_len;
308
309 memcpy(ima_buf + l, target_data_buf, target_data_buf_len);
310 l += target_data_buf_len;
311 }
312
313 if (!last_target_measured) {
314 dm_ima_measure_data(table_load_event_name, ima_buf, l, noio);
315
316 r = crypto_shash_update(shash, (const u8 *)ima_buf, l);
317 if (r < 0)
318 goto error;
319 }
320
321 /*
322 * Finalize the table hash, and store it in table->md->ima.inactive_table.hash,
323 * so that the table data can be verified against the future device state change
324 * events, e.g. resume, rename, remove, table-clear etc.
325 */
326 r = crypto_shash_final(shash, digest);
327 if (r < 0)
328 goto error;
329
330 digest_buf = dm_ima_alloc((digest_size*2) + hash_alg_prefix_len + 1, GFP_KERNEL, noio);
331
332 if (!digest_buf)
333 goto error;
334
335 snprintf(digest_buf, hash_alg_prefix_len + 1, "%s:", DM_IMA_TABLE_HASH_ALG);
336
337 for (i = 0; i < digest_size; i++)
338 snprintf((digest_buf + hash_alg_prefix_len + (i*2)), 3, "%02x", digest[i]);
339
340 if (table->md->ima.active_table.hash != table->md->ima.inactive_table.hash)
341 kfree(table->md->ima.inactive_table.hash);
342
343 table->md->ima.inactive_table.hash = digest_buf;
344 table->md->ima.inactive_table.hash_len = strlen(digest_buf);
345 table->md->ima.inactive_table.num_targets = num_targets;
346
347 if (table->md->ima.active_table.device_metadata !=
348 table->md->ima.inactive_table.device_metadata)
349 kfree(table->md->ima.inactive_table.device_metadata);
350
351 table->md->ima.inactive_table.device_metadata = device_data_buf;
352 table->md->ima.inactive_table.device_metadata_len = device_data_buf_len;
353
354 goto exit;
355 error:
356 kfree(digest_buf);
357 kfree(device_data_buf);
358 exit:
359 kfree(digest);
360 if (tfm)
361 crypto_free_shash(tfm);
362 kfree(ima_buf);
363 kfree(target_metadata_buf);
364 kfree(target_data_buf);
365 }
366
367 /*
368 * Measure IMA data on device resume.
369 */
dm_ima_measure_on_device_resume(struct mapped_device * md,bool swap)370 void dm_ima_measure_on_device_resume(struct mapped_device *md, bool swap)
371 {
372 char *device_table_data, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
373 char active[] = "active_table_hash=";
374 unsigned int active_len = strlen(active), capacity_len = 0;
375 unsigned int l = 0;
376 bool noio = true;
377 bool nodata = true;
378 int r;
379
380 device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
381 if (!device_table_data)
382 return;
383
384 r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
385 if (r)
386 goto error;
387
388 memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
389 l += md->ima.dm_version_str_len;
390
391 if (swap) {
392 if (md->ima.active_table.hash != md->ima.inactive_table.hash)
393 kfree(md->ima.active_table.hash);
394
395 md->ima.active_table.hash = NULL;
396 md->ima.active_table.hash_len = 0;
397
398 if (md->ima.active_table.device_metadata !=
399 md->ima.inactive_table.device_metadata)
400 kfree(md->ima.active_table.device_metadata);
401
402 md->ima.active_table.device_metadata = NULL;
403 md->ima.active_table.device_metadata_len = 0;
404 md->ima.active_table.num_targets = 0;
405
406 if (md->ima.inactive_table.hash) {
407 md->ima.active_table.hash = md->ima.inactive_table.hash;
408 md->ima.active_table.hash_len = md->ima.inactive_table.hash_len;
409 md->ima.inactive_table.hash = NULL;
410 md->ima.inactive_table.hash_len = 0;
411 }
412
413 if (md->ima.inactive_table.device_metadata) {
414 md->ima.active_table.device_metadata =
415 md->ima.inactive_table.device_metadata;
416 md->ima.active_table.device_metadata_len =
417 md->ima.inactive_table.device_metadata_len;
418 md->ima.active_table.num_targets = md->ima.inactive_table.num_targets;
419 md->ima.inactive_table.device_metadata = NULL;
420 md->ima.inactive_table.device_metadata_len = 0;
421 md->ima.inactive_table.num_targets = 0;
422 }
423 }
424
425 if (md->ima.active_table.device_metadata) {
426 memcpy(device_table_data + l, md->ima.active_table.device_metadata,
427 md->ima.active_table.device_metadata_len);
428 l += md->ima.active_table.device_metadata_len;
429
430 nodata = false;
431 }
432
433 if (md->ima.active_table.hash) {
434 memcpy(device_table_data + l, active, active_len);
435 l += active_len;
436
437 memcpy(device_table_data + l, md->ima.active_table.hash,
438 md->ima.active_table.hash_len);
439 l += md->ima.active_table.hash_len;
440
441 memcpy(device_table_data + l, ";", 1);
442 l++;
443
444 nodata = false;
445 }
446
447 if (nodata) {
448 r = dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio);
449 if (r)
450 goto error;
451
452 l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
453 "%sname=%s,uuid=%s;device_resume=no_data;",
454 DM_IMA_VERSION_STR, dev_name, dev_uuid);
455 }
456
457 capacity_len = strlen(capacity_str);
458 memcpy(device_table_data + l, capacity_str, capacity_len);
459 l += capacity_len;
460
461 dm_ima_measure_data("dm_device_resume", device_table_data, l, noio);
462
463 kfree(dev_name);
464 kfree(dev_uuid);
465 error:
466 kfree(capacity_str);
467 kfree(device_table_data);
468 }
469
470 /*
471 * Measure IMA data on remove.
472 */
dm_ima_measure_on_device_remove(struct mapped_device * md,bool remove_all)473 void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)
474 {
475 char *device_table_data, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
476 char active_table_str[] = "active_table_hash=";
477 char inactive_table_str[] = "inactive_table_hash=";
478 char device_active_str[] = "device_active_metadata=";
479 char device_inactive_str[] = "device_inactive_metadata=";
480 char remove_all_str[] = "remove_all=";
481 unsigned int active_table_len = strlen(active_table_str);
482 unsigned int inactive_table_len = strlen(inactive_table_str);
483 unsigned int device_active_len = strlen(device_active_str);
484 unsigned int device_inactive_len = strlen(device_inactive_str);
485 unsigned int remove_all_len = strlen(remove_all_str);
486 unsigned int capacity_len = 0;
487 unsigned int l = 0;
488 bool noio = true;
489 bool nodata = true;
490 int r;
491
492 device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN*2, GFP_KERNEL, noio);
493 if (!device_table_data)
494 goto exit;
495
496 r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
497 if (r) {
498 kfree(device_table_data);
499 goto exit;
500 }
501
502 memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
503 l += md->ima.dm_version_str_len;
504
505 if (md->ima.active_table.device_metadata) {
506 memcpy(device_table_data + l, device_active_str, device_active_len);
507 l += device_active_len;
508
509 memcpy(device_table_data + l, md->ima.active_table.device_metadata,
510 md->ima.active_table.device_metadata_len);
511 l += md->ima.active_table.device_metadata_len;
512
513 nodata = false;
514 }
515
516 if (md->ima.inactive_table.device_metadata) {
517 memcpy(device_table_data + l, device_inactive_str, device_inactive_len);
518 l += device_inactive_len;
519
520 memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
521 md->ima.inactive_table.device_metadata_len);
522 l += md->ima.inactive_table.device_metadata_len;
523
524 nodata = false;
525 }
526
527 if (md->ima.active_table.hash) {
528 memcpy(device_table_data + l, active_table_str, active_table_len);
529 l += active_table_len;
530
531 memcpy(device_table_data + l, md->ima.active_table.hash,
532 md->ima.active_table.hash_len);
533 l += md->ima.active_table.hash_len;
534
535 memcpy(device_table_data + l, ",", 1);
536 l++;
537
538 nodata = false;
539 }
540
541 if (md->ima.inactive_table.hash) {
542 memcpy(device_table_data + l, inactive_table_str, inactive_table_len);
543 l += inactive_table_len;
544
545 memcpy(device_table_data + l, md->ima.inactive_table.hash,
546 md->ima.inactive_table.hash_len);
547 l += md->ima.inactive_table.hash_len;
548
549 memcpy(device_table_data + l, ",", 1);
550 l++;
551
552 nodata = false;
553 }
554 /*
555 * In case both active and inactive tables, and corresponding
556 * device metadata is cleared/missing - record the name and uuid
557 * in IMA measurements.
558 */
559 if (nodata) {
560 if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
561 goto error;
562
563 l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
564 "%sname=%s,uuid=%s;device_remove=no_data;",
565 DM_IMA_VERSION_STR, dev_name, dev_uuid);
566 }
567
568 memcpy(device_table_data + l, remove_all_str, remove_all_len);
569 l += remove_all_len;
570 memcpy(device_table_data + l, remove_all ? "y;" : "n;", 2);
571 l += 2;
572
573 capacity_len = strlen(capacity_str);
574 memcpy(device_table_data + l, capacity_str, capacity_len);
575 l += capacity_len;
576
577 dm_ima_measure_data("dm_device_remove", device_table_data, l, noio);
578
579 error:
580 kfree(device_table_data);
581 kfree(capacity_str);
582 exit:
583 kfree(md->ima.active_table.device_metadata);
584
585 if (md->ima.active_table.device_metadata !=
586 md->ima.inactive_table.device_metadata)
587 kfree(md->ima.inactive_table.device_metadata);
588
589 kfree(md->ima.active_table.hash);
590
591 if (md->ima.active_table.hash != md->ima.inactive_table.hash)
592 kfree(md->ima.inactive_table.hash);
593
594 dm_ima_reset_data(md);
595
596 kfree(dev_name);
597 kfree(dev_uuid);
598 }
599
600 /*
601 * Measure ima data on table clear.
602 */
dm_ima_measure_on_table_clear(struct mapped_device * md,bool new_map)603 void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)
604 {
605 unsigned int l = 0, capacity_len = 0;
606 char *device_table_data = NULL, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
607 char inactive_str[] = "inactive_table_hash=";
608 unsigned int inactive_len = strlen(inactive_str);
609 bool noio = true;
610 bool nodata = true;
611 int r;
612
613 device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
614 if (!device_table_data)
615 return;
616
617 r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
618 if (r)
619 goto error1;
620
621 memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
622 l += md->ima.dm_version_str_len;
623
624 if (md->ima.inactive_table.device_metadata_len &&
625 md->ima.inactive_table.hash_len) {
626 memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
627 md->ima.inactive_table.device_metadata_len);
628 l += md->ima.inactive_table.device_metadata_len;
629
630 memcpy(device_table_data + l, inactive_str, inactive_len);
631 l += inactive_len;
632
633 memcpy(device_table_data + l, md->ima.inactive_table.hash,
634 md->ima.inactive_table.hash_len);
635
636 l += md->ima.inactive_table.hash_len;
637
638 memcpy(device_table_data + l, ";", 1);
639 l++;
640
641 nodata = false;
642 }
643
644 if (nodata) {
645 if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
646 goto error2;
647
648 l = scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
649 "%sname=%s,uuid=%s;table_clear=no_data;",
650 DM_IMA_VERSION_STR, dev_name, dev_uuid);
651 }
652
653 capacity_len = strlen(capacity_str);
654 memcpy(device_table_data + l, capacity_str, capacity_len);
655 l += capacity_len;
656
657 dm_ima_measure_data("dm_table_clear", device_table_data, l, noio);
658
659 if (new_map) {
660 if (md->ima.inactive_table.hash &&
661 md->ima.inactive_table.hash != md->ima.active_table.hash)
662 kfree(md->ima.inactive_table.hash);
663
664 md->ima.inactive_table.hash = NULL;
665 md->ima.inactive_table.hash_len = 0;
666
667 if (md->ima.inactive_table.device_metadata &&
668 md->ima.inactive_table.device_metadata != md->ima.active_table.device_metadata)
669 kfree(md->ima.inactive_table.device_metadata);
670
671 md->ima.inactive_table.device_metadata = NULL;
672 md->ima.inactive_table.device_metadata_len = 0;
673 md->ima.inactive_table.num_targets = 0;
674
675 if (md->ima.active_table.hash) {
676 md->ima.inactive_table.hash = md->ima.active_table.hash;
677 md->ima.inactive_table.hash_len = md->ima.active_table.hash_len;
678 }
679
680 if (md->ima.active_table.device_metadata) {
681 md->ima.inactive_table.device_metadata =
682 md->ima.active_table.device_metadata;
683 md->ima.inactive_table.device_metadata_len =
684 md->ima.active_table.device_metadata_len;
685 md->ima.inactive_table.num_targets =
686 md->ima.active_table.num_targets;
687 }
688 }
689
690 kfree(dev_name);
691 kfree(dev_uuid);
692 error2:
693 kfree(capacity_str);
694 error1:
695 kfree(device_table_data);
696 }
697
698 /*
699 * Measure IMA data on device rename.
700 */
dm_ima_measure_on_device_rename(struct mapped_device * md)701 void dm_ima_measure_on_device_rename(struct mapped_device *md)
702 {
703 char *old_device_data = NULL, *new_device_data = NULL, *combined_device_data = NULL;
704 char *new_dev_name = NULL, *new_dev_uuid = NULL, *capacity_str = NULL;
705 bool noio = true;
706 int r, len;
707
708 if (dm_ima_alloc_and_copy_device_data(md, &new_device_data,
709 md->ima.active_table.num_targets, noio))
710 return;
711
712 if (dm_ima_alloc_and_copy_name_uuid(md, &new_dev_name, &new_dev_uuid, noio))
713 goto error;
714
715 combined_device_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN * 2, GFP_KERNEL, noio);
716 if (!combined_device_data)
717 goto error;
718
719 r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
720 if (r)
721 goto error;
722
723 old_device_data = md->ima.active_table.device_metadata;
724
725 md->ima.active_table.device_metadata = new_device_data;
726 md->ima.active_table.device_metadata_len = strlen(new_device_data);
727
728 len = scnprintf(combined_device_data, DM_IMA_DEVICE_BUF_LEN * 2,
729 "%s%snew_name=%s,new_uuid=%s;%s", DM_IMA_VERSION_STR, old_device_data,
730 new_dev_name, new_dev_uuid, capacity_str);
731
732 dm_ima_measure_data("dm_device_rename", combined_device_data, len, noio);
733
734 goto exit;
735
736 error:
737 kfree(new_device_data);
738 exit:
739 kfree(capacity_str);
740 kfree(combined_device_data);
741 kfree(old_device_data);
742 kfree(new_dev_name);
743 kfree(new_dev_uuid);
744 }
745