dm-ima.c 20 KB

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