dm-ima.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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, gfp_t flags, bool noio)
  41. {
  42. unsigned int noio_flag;
  43. void *ptr;
  44. if (noio)
  45. noio_flag = memalloc_noio_save();
  46. ptr = kzalloc(len, flags);
  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, GFP_KERNEL, noio);
  59. if (!(*dev_name)) {
  60. r = -ENOMEM;
  61. goto error;
  62. }
  63. *dev_uuid = dm_ima_alloc(DM_UUID_LEN*2, GFP_KERNEL, 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, GFP_KERNEL, 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, GFP_KERNEL, noio);
  129. if (!(*capacity_str))
  130. return -ENOMEM;
  131. scnprintf(*capacity_str, DM_IMA_DEVICE_BUF_LEN, "current_device_capacity=%llu;",
  132. capacity);
  133. return 0;
  134. }
  135. /*
  136. * Initialize/reset the dm ima related data structure variables.
  137. */
  138. void dm_ima_reset_data(struct mapped_device *md)
  139. {
  140. memset(&(md->ima), 0, sizeof(md->ima));
  141. md->ima.dm_version_str_len = strlen(DM_IMA_VERSION_STR);
  142. }
  143. /*
  144. * Build up the IMA data for each target, and finally measure.
  145. */
  146. void dm_ima_measure_on_table_load(struct dm_table *table, unsigned int status_flags)
  147. {
  148. size_t device_data_buf_len, target_metadata_buf_len, target_data_buf_len, l = 0;
  149. char *target_metadata_buf = NULL, *target_data_buf = NULL, *digest_buf = NULL;
  150. char *ima_buf = NULL, *device_data_buf = NULL;
  151. int digest_size, last_target_measured = -1, r;
  152. status_type_t type = STATUSTYPE_IMA;
  153. size_t cur_total_buf_len = 0;
  154. unsigned int num_targets, i;
  155. SHASH_DESC_ON_STACK(shash, NULL);
  156. struct crypto_shash *tfm = NULL;
  157. u8 *digest = NULL;
  158. bool noio = false;
  159. /*
  160. * In below hash_alg_prefix_len assignment +1 is for the additional char (':'),
  161. * when prefixing the hash value with the hash algorithm name. e.g. sha256:<hash_value>.
  162. */
  163. const size_t hash_alg_prefix_len = strlen(DM_IMA_TABLE_HASH_ALG) + 1;
  164. char table_load_event_name[] = "dm_table_load";
  165. ima_buf = dm_ima_alloc(DM_IMA_MEASUREMENT_BUF_LEN, GFP_KERNEL, noio);
  166. if (!ima_buf)
  167. return;
  168. target_metadata_buf = dm_ima_alloc(DM_IMA_TARGET_METADATA_BUF_LEN, GFP_KERNEL, noio);
  169. if (!target_metadata_buf)
  170. goto error;
  171. target_data_buf = dm_ima_alloc(DM_IMA_TARGET_DATA_BUF_LEN, GFP_KERNEL, noio);
  172. if (!target_data_buf)
  173. goto error;
  174. num_targets = table->num_targets;
  175. if (dm_ima_alloc_and_copy_device_data(table->md, &device_data_buf, num_targets, noio))
  176. goto error;
  177. tfm = crypto_alloc_shash(DM_IMA_TABLE_HASH_ALG, 0, 0);
  178. if (IS_ERR(tfm))
  179. goto error;
  180. shash->tfm = tfm;
  181. digest_size = crypto_shash_digestsize(tfm);
  182. digest = dm_ima_alloc(digest_size, GFP_KERNEL, noio);
  183. if (!digest)
  184. goto error;
  185. r = crypto_shash_init(shash);
  186. if (r)
  187. goto error;
  188. memcpy(ima_buf + l, DM_IMA_VERSION_STR, table->md->ima.dm_version_str_len);
  189. l += table->md->ima.dm_version_str_len;
  190. device_data_buf_len = strlen(device_data_buf);
  191. memcpy(ima_buf + l, device_data_buf, device_data_buf_len);
  192. l += device_data_buf_len;
  193. for (i = 0; i < num_targets; i++) {
  194. struct dm_target *ti = dm_table_get_target(table, i);
  195. last_target_measured = 0;
  196. /*
  197. * First retrieve the target metadata.
  198. */
  199. scnprintf(target_metadata_buf, DM_IMA_TARGET_METADATA_BUF_LEN,
  200. "target_index=%d,target_begin=%llu,target_len=%llu,",
  201. i, ti->begin, ti->len);
  202. target_metadata_buf_len = strlen(target_metadata_buf);
  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, GFP_KERNEL, 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), capacity_len = 0;
  306. unsigned int l = 0;
  307. bool noio = true;
  308. bool nodata = true;
  309. int r;
  310. device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
  311. if (!device_table_data)
  312. return;
  313. r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
  314. if (r)
  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. r = dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio);
  364. if (r)
  365. goto error;
  366. scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
  367. "%sname=%s,uuid=%s;device_resume=no_data;",
  368. DM_IMA_VERSION_STR, dev_name, dev_uuid);
  369. l = strlen(device_table_data);
  370. }
  371. capacity_len = strlen(capacity_str);
  372. memcpy(device_table_data + l, capacity_str, capacity_len);
  373. l += capacity_len;
  374. dm_ima_measure_data("dm_device_resume", device_table_data, l, noio);
  375. kfree(dev_name);
  376. kfree(dev_uuid);
  377. error:
  378. kfree(capacity_str);
  379. kfree(device_table_data);
  380. }
  381. /*
  382. * Measure IMA data on remove.
  383. */
  384. void dm_ima_measure_on_device_remove(struct mapped_device *md, bool remove_all)
  385. {
  386. char *device_table_data, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
  387. char active_table_str[] = "active_table_hash=";
  388. char inactive_table_str[] = "inactive_table_hash=";
  389. char device_active_str[] = "device_active_metadata=";
  390. char device_inactive_str[] = "device_inactive_metadata=";
  391. char remove_all_str[] = "remove_all=";
  392. unsigned int active_table_len = strlen(active_table_str);
  393. unsigned int inactive_table_len = strlen(inactive_table_str);
  394. unsigned int device_active_len = strlen(device_active_str);
  395. unsigned int device_inactive_len = strlen(device_inactive_str);
  396. unsigned int remove_all_len = strlen(remove_all_str);
  397. unsigned int capacity_len = 0;
  398. unsigned int l = 0;
  399. bool noio = true;
  400. bool nodata = true;
  401. int r;
  402. device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN*2, GFP_KERNEL, noio);
  403. if (!device_table_data)
  404. goto exit;
  405. r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
  406. if (r) {
  407. kfree(device_table_data);
  408. goto exit;
  409. }
  410. memcpy(device_table_data + l, DM_IMA_VERSION_STR, md->ima.dm_version_str_len);
  411. l += md->ima.dm_version_str_len;
  412. if (md->ima.active_table.device_metadata) {
  413. memcpy(device_table_data + l, device_active_str, device_active_len);
  414. l += device_active_len;
  415. memcpy(device_table_data + l, md->ima.active_table.device_metadata,
  416. md->ima.active_table.device_metadata_len);
  417. l += md->ima.active_table.device_metadata_len;
  418. nodata = false;
  419. }
  420. if (md->ima.inactive_table.device_metadata) {
  421. memcpy(device_table_data + l, device_inactive_str, device_inactive_len);
  422. l += device_inactive_len;
  423. memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
  424. md->ima.inactive_table.device_metadata_len);
  425. l += md->ima.inactive_table.device_metadata_len;
  426. nodata = false;
  427. }
  428. if (md->ima.active_table.hash) {
  429. memcpy(device_table_data + l, active_table_str, active_table_len);
  430. l += active_table_len;
  431. memcpy(device_table_data + l, md->ima.active_table.hash,
  432. md->ima.active_table.hash_len);
  433. l += md->ima.active_table.hash_len;
  434. memcpy(device_table_data + l, ",", 1);
  435. l++;
  436. nodata = false;
  437. }
  438. if (md->ima.inactive_table.hash) {
  439. memcpy(device_table_data + l, inactive_table_str, inactive_table_len);
  440. l += inactive_table_len;
  441. memcpy(device_table_data + l, md->ima.inactive_table.hash,
  442. md->ima.inactive_table.hash_len);
  443. l += md->ima.inactive_table.hash_len;
  444. memcpy(device_table_data + l, ",", 1);
  445. l++;
  446. nodata = false;
  447. }
  448. /*
  449. * In case both active and inactive tables, and corresponding
  450. * device metadata is cleared/missing - record the name and uuid
  451. * in IMA measurements.
  452. */
  453. if (nodata) {
  454. if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
  455. goto error;
  456. scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
  457. "%sname=%s,uuid=%s;device_remove=no_data;",
  458. DM_IMA_VERSION_STR, dev_name, dev_uuid);
  459. l = strlen(device_table_data);
  460. }
  461. memcpy(device_table_data + l, remove_all_str, remove_all_len);
  462. l += remove_all_len;
  463. memcpy(device_table_data + l, remove_all ? "y;" : "n;", 2);
  464. l += 2;
  465. capacity_len = strlen(capacity_str);
  466. memcpy(device_table_data + l, capacity_str, capacity_len);
  467. l += capacity_len;
  468. dm_ima_measure_data("dm_device_remove", device_table_data, l, noio);
  469. error:
  470. kfree(device_table_data);
  471. kfree(capacity_str);
  472. exit:
  473. kfree(md->ima.active_table.device_metadata);
  474. if (md->ima.active_table.device_metadata !=
  475. md->ima.inactive_table.device_metadata)
  476. kfree(md->ima.inactive_table.device_metadata);
  477. kfree(md->ima.active_table.hash);
  478. if (md->ima.active_table.hash != md->ima.inactive_table.hash)
  479. kfree(md->ima.inactive_table.hash);
  480. dm_ima_reset_data(md);
  481. kfree(dev_name);
  482. kfree(dev_uuid);
  483. }
  484. /*
  485. * Measure ima data on table clear.
  486. */
  487. void dm_ima_measure_on_table_clear(struct mapped_device *md, bool new_map)
  488. {
  489. unsigned int l = 0, capacity_len = 0;
  490. char *device_table_data = NULL, *dev_name = NULL, *dev_uuid = NULL, *capacity_str = NULL;
  491. char inactive_str[] = "inactive_table_hash=";
  492. unsigned int inactive_len = strlen(inactive_str);
  493. bool noio = true;
  494. bool nodata = true;
  495. int r;
  496. device_table_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN, GFP_KERNEL, noio);
  497. if (!device_table_data)
  498. return;
  499. r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
  500. if (r)
  501. goto error1;
  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. if (md->ima.inactive_table.device_metadata_len &&
  505. md->ima.inactive_table.hash_len) {
  506. memcpy(device_table_data + l, md->ima.inactive_table.device_metadata,
  507. md->ima.inactive_table.device_metadata_len);
  508. l += md->ima.inactive_table.device_metadata_len;
  509. memcpy(device_table_data + l, inactive_str, inactive_len);
  510. l += inactive_len;
  511. memcpy(device_table_data + l, md->ima.inactive_table.hash,
  512. md->ima.inactive_table.hash_len);
  513. l += md->ima.inactive_table.hash_len;
  514. memcpy(device_table_data + l, ";", 1);
  515. l++;
  516. nodata = false;
  517. }
  518. if (nodata) {
  519. if (dm_ima_alloc_and_copy_name_uuid(md, &dev_name, &dev_uuid, noio))
  520. goto error2;
  521. scnprintf(device_table_data, DM_IMA_DEVICE_BUF_LEN,
  522. "%sname=%s,uuid=%s;table_clear=no_data;",
  523. DM_IMA_VERSION_STR, dev_name, dev_uuid);
  524. l = strlen(device_table_data);
  525. }
  526. capacity_len = strlen(capacity_str);
  527. memcpy(device_table_data + l, capacity_str, capacity_len);
  528. l += capacity_len;
  529. dm_ima_measure_data("dm_table_clear", device_table_data, l, noio);
  530. if (new_map) {
  531. if (md->ima.inactive_table.hash &&
  532. md->ima.inactive_table.hash != md->ima.active_table.hash)
  533. kfree(md->ima.inactive_table.hash);
  534. md->ima.inactive_table.hash = NULL;
  535. md->ima.inactive_table.hash_len = 0;
  536. if (md->ima.inactive_table.device_metadata &&
  537. md->ima.inactive_table.device_metadata != md->ima.active_table.device_metadata)
  538. kfree(md->ima.inactive_table.device_metadata);
  539. md->ima.inactive_table.device_metadata = NULL;
  540. md->ima.inactive_table.device_metadata_len = 0;
  541. md->ima.inactive_table.num_targets = 0;
  542. if (md->ima.active_table.hash) {
  543. md->ima.inactive_table.hash = md->ima.active_table.hash;
  544. md->ima.inactive_table.hash_len = md->ima.active_table.hash_len;
  545. }
  546. if (md->ima.active_table.device_metadata) {
  547. md->ima.inactive_table.device_metadata =
  548. md->ima.active_table.device_metadata;
  549. md->ima.inactive_table.device_metadata_len =
  550. md->ima.active_table.device_metadata_len;
  551. md->ima.inactive_table.num_targets =
  552. md->ima.active_table.num_targets;
  553. }
  554. }
  555. kfree(dev_name);
  556. kfree(dev_uuid);
  557. error2:
  558. kfree(capacity_str);
  559. error1:
  560. kfree(device_table_data);
  561. }
  562. /*
  563. * Measure IMA data on device rename.
  564. */
  565. void dm_ima_measure_on_device_rename(struct mapped_device *md)
  566. {
  567. char *old_device_data = NULL, *new_device_data = NULL, *combined_device_data = NULL;
  568. char *new_dev_name = NULL, *new_dev_uuid = NULL, *capacity_str = NULL;
  569. bool noio = true;
  570. int r;
  571. if (dm_ima_alloc_and_copy_device_data(md, &new_device_data,
  572. md->ima.active_table.num_targets, noio))
  573. return;
  574. if (dm_ima_alloc_and_copy_name_uuid(md, &new_dev_name, &new_dev_uuid, noio))
  575. goto error;
  576. combined_device_data = dm_ima_alloc(DM_IMA_DEVICE_BUF_LEN * 2, GFP_KERNEL, noio);
  577. if (!combined_device_data)
  578. goto error;
  579. r = dm_ima_alloc_and_copy_capacity_str(md, &capacity_str, noio);
  580. if (r)
  581. goto error;
  582. old_device_data = md->ima.active_table.device_metadata;
  583. md->ima.active_table.device_metadata = new_device_data;
  584. md->ima.active_table.device_metadata_len = strlen(new_device_data);
  585. scnprintf(combined_device_data, DM_IMA_DEVICE_BUF_LEN * 2,
  586. "%s%snew_name=%s,new_uuid=%s;%s", DM_IMA_VERSION_STR, old_device_data,
  587. new_dev_name, new_dev_uuid, capacity_str);
  588. dm_ima_measure_data("dm_device_rename", combined_device_data, strlen(combined_device_data),
  589. noio);
  590. goto exit;
  591. error:
  592. kfree(new_device_data);
  593. exit:
  594. kfree(capacity_str);
  595. kfree(combined_device_data);
  596. kfree(old_device_data);
  597. kfree(new_dev_name);
  598. kfree(new_dev_uuid);
  599. }