dm-verity-target.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Red Hat, Inc.
  4. *
  5. * Author: Mikulas Patocka <mpatocka@redhat.com>
  6. *
  7. * Based on Chromium dm-verity driver (C) 2011 The Chromium OS Authors
  8. *
  9. * In the file "/sys/module/dm_verity/parameters/prefetch_cluster" you can set
  10. * default prefetch value. Data are read in "prefetch_cluster" chunks from the
  11. * hash device. Setting this greatly improves performance when data and hash
  12. * are on the same disk on different partitions on devices with poor random
  13. * access behavior.
  14. */
  15. #include "dm-verity.h"
  16. #include "dm-verity-fec.h"
  17. #include "dm-verity-verify-sig.h"
  18. #include "dm-audit.h"
  19. #include <linux/hex.h>
  20. #include <linux/module.h>
  21. #include <linux/reboot.h>
  22. #include <linux/string.h>
  23. #include <linux/jump_label.h>
  24. #include <linux/security.h>
  25. #define DM_MSG_PREFIX "verity"
  26. #define DM_VERITY_ENV_LENGTH 42
  27. #define DM_VERITY_ENV_VAR_NAME "DM_VERITY_ERR_BLOCK_NR"
  28. #define DM_VERITY_DEFAULT_PREFETCH_SIZE 262144
  29. #define DM_VERITY_USE_BH_DEFAULT_BYTES 8192
  30. #define DM_VERITY_MAX_CORRUPTED_ERRS 100
  31. #define DM_VERITY_OPT_LOGGING "ignore_corruption"
  32. #define DM_VERITY_OPT_RESTART "restart_on_corruption"
  33. #define DM_VERITY_OPT_PANIC "panic_on_corruption"
  34. #define DM_VERITY_OPT_ERROR_RESTART "restart_on_error"
  35. #define DM_VERITY_OPT_ERROR_PANIC "panic_on_error"
  36. #define DM_VERITY_OPT_IGN_ZEROES "ignore_zero_blocks"
  37. #define DM_VERITY_OPT_AT_MOST_ONCE "check_at_most_once"
  38. #define DM_VERITY_OPT_TASKLET_VERIFY "try_verify_in_tasklet"
  39. #define DM_VERITY_OPTS_MAX (5 + DM_VERITY_OPTS_FEC + \
  40. DM_VERITY_ROOT_HASH_VERIFICATION_OPTS)
  41. static unsigned int dm_verity_prefetch_cluster = DM_VERITY_DEFAULT_PREFETCH_SIZE;
  42. module_param_named(prefetch_cluster, dm_verity_prefetch_cluster, uint, 0644);
  43. static unsigned int dm_verity_use_bh_bytes[4] = {
  44. DM_VERITY_USE_BH_DEFAULT_BYTES, // IOPRIO_CLASS_NONE
  45. DM_VERITY_USE_BH_DEFAULT_BYTES, // IOPRIO_CLASS_RT
  46. DM_VERITY_USE_BH_DEFAULT_BYTES, // IOPRIO_CLASS_BE
  47. 0 // IOPRIO_CLASS_IDLE
  48. };
  49. module_param_array_named(use_bh_bytes, dm_verity_use_bh_bytes, uint, NULL, 0644);
  50. static DEFINE_STATIC_KEY_FALSE(use_bh_wq_enabled);
  51. struct dm_verity_prefetch_work {
  52. struct work_struct work;
  53. struct dm_verity *v;
  54. unsigned short ioprio;
  55. sector_t block;
  56. unsigned int n_blocks;
  57. };
  58. /*
  59. * Auxiliary structure appended to each dm-bufio buffer. If the value
  60. * hash_verified is nonzero, hash of the block has been verified.
  61. *
  62. * The variable hash_verified is set to 0 when allocating the buffer, then
  63. * it can be changed to 1 and it is never reset to 0 again.
  64. *
  65. * There is no lock around this value, a race condition can at worst cause
  66. * that multiple processes verify the hash of the same buffer simultaneously
  67. * and write 1 to hash_verified simultaneously.
  68. * This condition is harmless, so we don't need locking.
  69. */
  70. struct buffer_aux {
  71. int hash_verified;
  72. };
  73. /*
  74. * Initialize struct buffer_aux for a freshly created buffer.
  75. */
  76. static void dm_bufio_alloc_callback(struct dm_buffer *buf)
  77. {
  78. struct buffer_aux *aux = dm_bufio_get_aux_data(buf);
  79. aux->hash_verified = 0;
  80. }
  81. /*
  82. * Translate input sector number to the sector number on the target device.
  83. */
  84. static sector_t verity_map_sector(struct dm_verity *v, sector_t bi_sector)
  85. {
  86. return dm_target_offset(v->ti, bi_sector);
  87. }
  88. /*
  89. * Return hash position of a specified block at a specified tree level
  90. * (0 is the lowest level).
  91. * The lowest "hash_per_block_bits"-bits of the result denote hash position
  92. * inside a hash block. The remaining bits denote location of the hash block.
  93. */
  94. static sector_t verity_position_at_level(struct dm_verity *v, sector_t block,
  95. int level)
  96. {
  97. return block >> (level * v->hash_per_block_bits);
  98. }
  99. int verity_hash(struct dm_verity *v, struct dm_verity_io *io,
  100. const u8 *data, size_t len, u8 *digest)
  101. {
  102. struct shash_desc *desc;
  103. int r;
  104. if (likely(v->use_sha256_lib)) {
  105. struct sha256_ctx *ctx = &io->hash_ctx.sha256;
  106. /*
  107. * Fast path using SHA-256 library. This is enabled only for
  108. * verity version 1, where the salt is at the beginning.
  109. */
  110. *ctx = *v->initial_hashstate.sha256;
  111. sha256_update(ctx, data, len);
  112. sha256_final(ctx, digest);
  113. return 0;
  114. }
  115. desc = &io->hash_ctx.shash;
  116. desc->tfm = v->shash_tfm;
  117. if (unlikely(v->initial_hashstate.shash == NULL)) {
  118. /* Version 0: salt at end */
  119. r = crypto_shash_init(desc) ?:
  120. crypto_shash_update(desc, data, len) ?:
  121. crypto_shash_update(desc, v->salt, v->salt_size) ?:
  122. crypto_shash_final(desc, digest);
  123. } else {
  124. /* Version 1: salt at beginning */
  125. r = crypto_shash_import(desc, v->initial_hashstate.shash) ?:
  126. crypto_shash_finup(desc, data, len, digest);
  127. }
  128. if (unlikely(r))
  129. DMERR("Error hashing block: %d", r);
  130. return r;
  131. }
  132. static void verity_hash_at_level(struct dm_verity *v, sector_t block, int level,
  133. sector_t *hash_block, unsigned int *offset)
  134. {
  135. sector_t position = verity_position_at_level(v, block, level);
  136. unsigned int idx;
  137. *hash_block = v->hash_level_block[level] + (position >> v->hash_per_block_bits);
  138. if (!offset)
  139. return;
  140. idx = position & ((1 << v->hash_per_block_bits) - 1);
  141. if (!v->version)
  142. *offset = idx * v->digest_size;
  143. else
  144. *offset = idx << (v->hash_dev_block_bits - v->hash_per_block_bits);
  145. }
  146. /*
  147. * Handle verification errors.
  148. */
  149. static int verity_handle_err(struct dm_verity *v, enum verity_block_type type,
  150. unsigned long long block)
  151. {
  152. char verity_env[DM_VERITY_ENV_LENGTH];
  153. char *envp[] = { verity_env, NULL };
  154. const char *type_str = "";
  155. struct mapped_device *md = dm_table_get_md(v->ti->table);
  156. /* Corruption should be visible in device status in all modes */
  157. v->hash_failed = true;
  158. if (v->corrupted_errs >= DM_VERITY_MAX_CORRUPTED_ERRS)
  159. goto out;
  160. v->corrupted_errs++;
  161. switch (type) {
  162. case DM_VERITY_BLOCK_TYPE_DATA:
  163. type_str = "data";
  164. break;
  165. case DM_VERITY_BLOCK_TYPE_METADATA:
  166. type_str = "metadata";
  167. break;
  168. default:
  169. BUG();
  170. }
  171. DMERR_LIMIT("%s: %s block %llu is corrupted", v->data_dev->name,
  172. type_str, block);
  173. if (v->corrupted_errs == DM_VERITY_MAX_CORRUPTED_ERRS) {
  174. DMERR("%s: reached maximum errors", v->data_dev->name);
  175. dm_audit_log_target(DM_MSG_PREFIX, "max-corrupted-errors", v->ti, 0);
  176. }
  177. snprintf(verity_env, DM_VERITY_ENV_LENGTH, "%s=%d,%llu",
  178. DM_VERITY_ENV_VAR_NAME, type, block);
  179. kobject_uevent_env(&disk_to_dev(dm_disk(md))->kobj, KOBJ_CHANGE, envp);
  180. out:
  181. if (v->mode == DM_VERITY_MODE_LOGGING)
  182. return 0;
  183. if (v->mode == DM_VERITY_MODE_RESTART)
  184. kernel_restart("dm-verity device corrupted");
  185. if (v->mode == DM_VERITY_MODE_PANIC)
  186. panic("dm-verity device corrupted");
  187. return 1;
  188. }
  189. /*
  190. * Verify hash of a metadata block pertaining to the specified data block
  191. * ("block" argument) at a specified level ("level" argument).
  192. *
  193. * On successful return, want_digest contains the hash value for a lower tree
  194. * level or for the data block (if we're at the lowest level).
  195. *
  196. * If "skip_unverified" is true, unverified buffer is skipped and 1 is returned.
  197. * If "skip_unverified" is false, unverified buffer is hashed and verified
  198. * against current value of want_digest.
  199. */
  200. static int verity_verify_level(struct dm_verity *v, struct dm_verity_io *io,
  201. sector_t block, int level, bool skip_unverified,
  202. u8 *want_digest)
  203. {
  204. struct dm_buffer *buf;
  205. struct buffer_aux *aux;
  206. u8 *data;
  207. int r;
  208. sector_t hash_block;
  209. unsigned int offset;
  210. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  211. verity_hash_at_level(v, block, level, &hash_block, &offset);
  212. if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) {
  213. data = dm_bufio_get(v->bufio, hash_block, &buf);
  214. if (IS_ERR_OR_NULL(data)) {
  215. /*
  216. * In softirq and the hash was not in the bufio cache.
  217. * Return early and resume execution from a kworker to
  218. * read the hash from disk.
  219. */
  220. return -EAGAIN;
  221. }
  222. } else {
  223. data = dm_bufio_read_with_ioprio(v->bufio, hash_block,
  224. &buf, bio->bi_ioprio);
  225. }
  226. if (IS_ERR(data)) {
  227. if (skip_unverified)
  228. return 1;
  229. r = PTR_ERR(data);
  230. data = dm_bufio_new(v->bufio, hash_block, &buf);
  231. if (IS_ERR(data))
  232. return r;
  233. if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_METADATA,
  234. want_digest, hash_block, data) == 0) {
  235. aux = dm_bufio_get_aux_data(buf);
  236. aux->hash_verified = 1;
  237. goto release_ok;
  238. } else {
  239. dm_bufio_release(buf);
  240. dm_bufio_forget(v->bufio, hash_block);
  241. return r;
  242. }
  243. }
  244. aux = dm_bufio_get_aux_data(buf);
  245. if (!aux->hash_verified) {
  246. if (skip_unverified) {
  247. r = 1;
  248. goto release_ret_r;
  249. }
  250. r = verity_hash(v, io, data, 1 << v->hash_dev_block_bits,
  251. io->tmp_digest);
  252. if (unlikely(r < 0))
  253. goto release_ret_r;
  254. if (likely(memcmp(io->tmp_digest, want_digest,
  255. v->digest_size) == 0))
  256. aux->hash_verified = 1;
  257. else if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) {
  258. /*
  259. * Error handling code (FEC included) cannot be run in a
  260. * softirq since it may sleep, so fallback to a kworker.
  261. */
  262. r = -EAGAIN;
  263. goto release_ret_r;
  264. } else if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_METADATA,
  265. want_digest, hash_block, data) == 0)
  266. aux->hash_verified = 1;
  267. else if (verity_handle_err(v,
  268. DM_VERITY_BLOCK_TYPE_METADATA,
  269. hash_block)) {
  270. struct bio *bio;
  271. io->had_mismatch = true;
  272. bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  273. dm_audit_log_bio(DM_MSG_PREFIX, "verify-metadata", bio,
  274. block, 0);
  275. r = -EIO;
  276. goto release_ret_r;
  277. }
  278. }
  279. release_ok:
  280. data += offset;
  281. memcpy(want_digest, data, v->digest_size);
  282. r = 0;
  283. release_ret_r:
  284. dm_bufio_release(buf);
  285. return r;
  286. }
  287. /*
  288. * Find a hash for a given block, write it to digest and verify the integrity
  289. * of the hash tree if necessary.
  290. */
  291. int verity_hash_for_block(struct dm_verity *v, struct dm_verity_io *io,
  292. sector_t block, u8 *digest, bool *is_zero)
  293. {
  294. int r = 0, i;
  295. if (likely(v->levels)) {
  296. /*
  297. * First, we try to get the requested hash for
  298. * the current block. If the hash block itself is
  299. * verified, zero is returned. If it isn't, this
  300. * function returns 1 and we fall back to whole
  301. * chain verification.
  302. */
  303. r = verity_verify_level(v, io, block, 0, true, digest);
  304. if (likely(r <= 0))
  305. goto out;
  306. }
  307. memcpy(digest, v->root_digest, v->digest_size);
  308. for (i = v->levels - 1; i >= 0; i--) {
  309. r = verity_verify_level(v, io, block, i, false, digest);
  310. if (unlikely(r))
  311. goto out;
  312. }
  313. out:
  314. if (!r && v->zero_digest)
  315. *is_zero = !memcmp(v->zero_digest, digest, v->digest_size);
  316. else
  317. *is_zero = false;
  318. return r;
  319. }
  320. static noinline int verity_recheck(struct dm_verity *v, struct dm_verity_io *io,
  321. const u8 *want_digest, sector_t cur_block,
  322. u8 *dest)
  323. {
  324. struct page *page;
  325. void *buffer;
  326. int r;
  327. struct dm_io_request io_req;
  328. struct dm_io_region io_loc;
  329. page = mempool_alloc(&v->recheck_pool, GFP_NOIO);
  330. buffer = page_to_virt(page);
  331. io_req.bi_opf = REQ_OP_READ;
  332. io_req.mem.type = DM_IO_KMEM;
  333. io_req.mem.ptr.addr = buffer;
  334. io_req.notify.fn = NULL;
  335. io_req.client = v->io;
  336. io_loc.bdev = v->data_dev->bdev;
  337. io_loc.sector = cur_block << (v->data_dev_block_bits - SECTOR_SHIFT);
  338. io_loc.count = 1 << (v->data_dev_block_bits - SECTOR_SHIFT);
  339. r = dm_io(&io_req, 1, &io_loc, NULL, IOPRIO_DEFAULT);
  340. if (unlikely(r))
  341. goto free_ret;
  342. r = verity_hash(v, io, buffer, 1 << v->data_dev_block_bits,
  343. io->tmp_digest);
  344. if (unlikely(r))
  345. goto free_ret;
  346. if (memcmp(io->tmp_digest, want_digest, v->digest_size)) {
  347. r = -EIO;
  348. goto free_ret;
  349. }
  350. memcpy(dest, buffer, 1 << v->data_dev_block_bits);
  351. r = 0;
  352. free_ret:
  353. mempool_free(page, &v->recheck_pool);
  354. return r;
  355. }
  356. static int verity_handle_data_hash_mismatch(struct dm_verity *v,
  357. struct dm_verity_io *io,
  358. struct bio *bio,
  359. struct pending_block *block)
  360. {
  361. const u8 *want_digest = block->want_digest;
  362. sector_t blkno = block->blkno;
  363. u8 *data = block->data;
  364. if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) {
  365. /*
  366. * Error handling code (FEC included) cannot be run in a
  367. * softirq since it may sleep, so fallback to a kworker.
  368. */
  369. return -EAGAIN;
  370. }
  371. if (verity_recheck(v, io, want_digest, blkno, data) == 0) {
  372. if (v->validated_blocks)
  373. set_bit(blkno, v->validated_blocks);
  374. return 0;
  375. }
  376. if (verity_fec_decode(v, io, DM_VERITY_BLOCK_TYPE_DATA, want_digest,
  377. blkno, data) == 0)
  378. return 0;
  379. if (bio->bi_status)
  380. return -EIO; /* Error correction failed; Just return error */
  381. if (verity_handle_err(v, DM_VERITY_BLOCK_TYPE_DATA, blkno)) {
  382. io->had_mismatch = true;
  383. dm_audit_log_bio(DM_MSG_PREFIX, "verify-data", bio, blkno, 0);
  384. return -EIO;
  385. }
  386. return 0;
  387. }
  388. static void verity_clear_pending_blocks(struct dm_verity_io *io)
  389. {
  390. int i;
  391. for (i = io->num_pending - 1; i >= 0; i--) {
  392. kunmap_local(io->pending_blocks[i].data);
  393. io->pending_blocks[i].data = NULL;
  394. }
  395. io->num_pending = 0;
  396. }
  397. static int verity_verify_pending_blocks(struct dm_verity *v,
  398. struct dm_verity_io *io,
  399. struct bio *bio)
  400. {
  401. const unsigned int block_size = 1 << v->data_dev_block_bits;
  402. int i, r;
  403. if (io->num_pending == 2) {
  404. /* num_pending == 2 implies that the algorithm is SHA-256 */
  405. sha256_finup_2x(v->initial_hashstate.sha256,
  406. io->pending_blocks[0].data,
  407. io->pending_blocks[1].data, block_size,
  408. io->pending_blocks[0].real_digest,
  409. io->pending_blocks[1].real_digest);
  410. } else {
  411. for (i = 0; i < io->num_pending; i++) {
  412. r = verity_hash(v, io, io->pending_blocks[i].data,
  413. block_size,
  414. io->pending_blocks[i].real_digest);
  415. if (unlikely(r))
  416. return r;
  417. }
  418. }
  419. for (i = 0; i < io->num_pending; i++) {
  420. struct pending_block *block = &io->pending_blocks[i];
  421. if (likely(memcmp(block->real_digest, block->want_digest,
  422. v->digest_size) == 0)) {
  423. if (v->validated_blocks)
  424. set_bit(block->blkno, v->validated_blocks);
  425. } else {
  426. r = verity_handle_data_hash_mismatch(v, io, bio, block);
  427. if (unlikely(r))
  428. return r;
  429. }
  430. }
  431. verity_clear_pending_blocks(io);
  432. return 0;
  433. }
  434. /*
  435. * Verify one "dm_verity_io" structure.
  436. */
  437. static int verity_verify_io(struct dm_verity_io *io)
  438. {
  439. struct dm_verity *v = io->v;
  440. const unsigned int block_size = 1 << v->data_dev_block_bits;
  441. const int max_pending = v->use_sha256_finup_2x ? 2 : 1;
  442. struct bvec_iter iter_copy;
  443. struct bvec_iter *iter;
  444. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  445. unsigned int b;
  446. int r;
  447. io->num_pending = 0;
  448. if (static_branch_unlikely(&use_bh_wq_enabled) && io->in_bh) {
  449. /*
  450. * Copy the iterator in case we need to restart verification in
  451. * a kworker.
  452. */
  453. iter_copy = io->iter;
  454. iter = &iter_copy;
  455. } else
  456. iter = &io->iter;
  457. for (b = 0; b < io->n_blocks;
  458. b++, bio_advance_iter_single(bio, iter, block_size)) {
  459. sector_t blkno = io->block + b;
  460. struct pending_block *block;
  461. bool is_zero;
  462. struct bio_vec bv;
  463. void *data;
  464. if (v->validated_blocks && bio->bi_status == BLK_STS_OK &&
  465. likely(test_bit(blkno, v->validated_blocks)))
  466. continue;
  467. block = &io->pending_blocks[io->num_pending];
  468. r = verity_hash_for_block(v, io, blkno, block->want_digest,
  469. &is_zero);
  470. if (unlikely(r < 0))
  471. goto error;
  472. bv = bio_iter_iovec(bio, *iter);
  473. if (unlikely(bv.bv_len < block_size)) {
  474. /*
  475. * Data block spans pages. This should not happen,
  476. * since dm-verity sets dma_alignment to the data block
  477. * size minus 1, and dm-verity also doesn't allow the
  478. * data block size to be greater than PAGE_SIZE.
  479. */
  480. DMERR_LIMIT("unaligned io (data block spans pages)");
  481. r = -EIO;
  482. goto error;
  483. }
  484. data = bvec_kmap_local(&bv);
  485. if (is_zero) {
  486. /*
  487. * If we expect a zero block, don't validate, just
  488. * return zeros.
  489. */
  490. memset(data, 0, block_size);
  491. kunmap_local(data);
  492. continue;
  493. }
  494. block->data = data;
  495. block->blkno = blkno;
  496. if (++io->num_pending == max_pending) {
  497. r = verity_verify_pending_blocks(v, io, bio);
  498. if (unlikely(r))
  499. goto error;
  500. }
  501. }
  502. if (io->num_pending) {
  503. r = verity_verify_pending_blocks(v, io, bio);
  504. if (unlikely(r))
  505. goto error;
  506. }
  507. return 0;
  508. error:
  509. verity_clear_pending_blocks(io);
  510. return r;
  511. }
  512. /*
  513. * Skip verity work in response to I/O error when system is shutting down.
  514. */
  515. static inline bool verity_is_system_shutting_down(void)
  516. {
  517. return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
  518. || system_state == SYSTEM_RESTART;
  519. }
  520. static void restart_io_error(struct work_struct *w)
  521. {
  522. kernel_restart("dm-verity device has I/O error");
  523. }
  524. /*
  525. * End one "io" structure with a given error.
  526. */
  527. static void verity_finish_io(struct dm_verity_io *io, blk_status_t status)
  528. {
  529. struct dm_verity *v = io->v;
  530. struct bio *bio = dm_bio_from_per_bio_data(io, v->ti->per_io_data_size);
  531. bio->bi_end_io = io->orig_bi_end_io;
  532. bio->bi_status = status;
  533. verity_fec_finish_io(io);
  534. if (unlikely(status != BLK_STS_OK) &&
  535. unlikely(!(bio->bi_opf & REQ_RAHEAD)) &&
  536. !io->had_mismatch &&
  537. !verity_is_system_shutting_down()) {
  538. if (v->error_mode == DM_VERITY_MODE_PANIC) {
  539. panic("dm-verity device has I/O error");
  540. }
  541. if (v->error_mode == DM_VERITY_MODE_RESTART) {
  542. static DECLARE_WORK(restart_work, restart_io_error);
  543. queue_work(v->verify_wq, &restart_work);
  544. /*
  545. * We deliberately don't call bio_endio here, because
  546. * the machine will be restarted anyway.
  547. */
  548. return;
  549. }
  550. }
  551. bio_endio(bio);
  552. }
  553. static void verity_work(struct work_struct *w)
  554. {
  555. struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
  556. io->in_bh = false;
  557. verity_finish_io(io, errno_to_blk_status(verity_verify_io(io)));
  558. }
  559. static void verity_bh_work(struct work_struct *w)
  560. {
  561. struct dm_verity_io *io = container_of(w, struct dm_verity_io, work);
  562. int err;
  563. io->in_bh = true;
  564. err = verity_verify_io(io);
  565. if (err == -EAGAIN || err == -ENOMEM) {
  566. /* fallback to retrying in a kworker */
  567. INIT_WORK(&io->work, verity_work);
  568. queue_work(io->v->verify_wq, &io->work);
  569. return;
  570. }
  571. verity_finish_io(io, errno_to_blk_status(err));
  572. }
  573. static inline bool verity_use_bh(unsigned int bytes, unsigned short ioprio)
  574. {
  575. return ioprio <= IOPRIO_CLASS_IDLE &&
  576. bytes <= READ_ONCE(dm_verity_use_bh_bytes[ioprio]) &&
  577. !need_resched();
  578. }
  579. static void verity_end_io(struct bio *bio)
  580. {
  581. struct dm_verity_io *io = bio->bi_private;
  582. unsigned short ioprio = IOPRIO_PRIO_CLASS(bio->bi_ioprio);
  583. unsigned int bytes = io->n_blocks << io->v->data_dev_block_bits;
  584. if (bio->bi_status &&
  585. (!verity_fec_is_enabled(io->v) ||
  586. verity_is_system_shutting_down() ||
  587. (bio->bi_opf & REQ_RAHEAD))) {
  588. verity_finish_io(io, bio->bi_status);
  589. return;
  590. }
  591. if (static_branch_unlikely(&use_bh_wq_enabled) && io->v->use_bh_wq &&
  592. verity_use_bh(bytes, ioprio)) {
  593. if (in_hardirq() || irqs_disabled()) {
  594. INIT_WORK(&io->work, verity_bh_work);
  595. queue_work(system_bh_wq, &io->work);
  596. } else {
  597. verity_bh_work(&io->work);
  598. }
  599. } else {
  600. INIT_WORK(&io->work, verity_work);
  601. queue_work(io->v->verify_wq, &io->work);
  602. }
  603. }
  604. /*
  605. * Prefetch buffers for the specified io.
  606. * The root buffer is not prefetched, it is assumed that it will be cached
  607. * all the time.
  608. */
  609. static void verity_prefetch_io(struct work_struct *work)
  610. {
  611. struct dm_verity_prefetch_work *pw =
  612. container_of(work, struct dm_verity_prefetch_work, work);
  613. struct dm_verity *v = pw->v;
  614. int i;
  615. for (i = v->levels - 2; i >= 0; i--) {
  616. sector_t hash_block_start;
  617. sector_t hash_block_end;
  618. verity_hash_at_level(v, pw->block, i, &hash_block_start, NULL);
  619. verity_hash_at_level(v, pw->block + pw->n_blocks - 1, i, &hash_block_end, NULL);
  620. if (!i) {
  621. unsigned int cluster = READ_ONCE(dm_verity_prefetch_cluster);
  622. cluster >>= v->data_dev_block_bits;
  623. if (unlikely(!cluster))
  624. goto no_prefetch_cluster;
  625. if (unlikely(cluster & (cluster - 1)))
  626. cluster = 1 << __fls(cluster);
  627. hash_block_start &= ~(sector_t)(cluster - 1);
  628. hash_block_end |= cluster - 1;
  629. if (unlikely(hash_block_end >= v->hash_blocks))
  630. hash_block_end = v->hash_blocks - 1;
  631. }
  632. no_prefetch_cluster:
  633. dm_bufio_prefetch_with_ioprio(v->bufio, hash_block_start,
  634. hash_block_end - hash_block_start + 1,
  635. pw->ioprio);
  636. }
  637. kfree(pw);
  638. }
  639. static void verity_submit_prefetch(struct dm_verity *v, struct dm_verity_io *io,
  640. unsigned short ioprio)
  641. {
  642. sector_t block = io->block;
  643. unsigned int n_blocks = io->n_blocks;
  644. struct dm_verity_prefetch_work *pw;
  645. if (v->validated_blocks) {
  646. while (n_blocks && test_bit(block, v->validated_blocks)) {
  647. block++;
  648. n_blocks--;
  649. }
  650. while (n_blocks && test_bit(block + n_blocks - 1,
  651. v->validated_blocks))
  652. n_blocks--;
  653. if (!n_blocks)
  654. return;
  655. }
  656. pw = kmalloc_obj(struct dm_verity_prefetch_work,
  657. GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN);
  658. if (!pw)
  659. return;
  660. INIT_WORK(&pw->work, verity_prefetch_io);
  661. pw->v = v;
  662. pw->block = block;
  663. pw->n_blocks = n_blocks;
  664. pw->ioprio = ioprio;
  665. queue_work(v->verify_wq, &pw->work);
  666. }
  667. /*
  668. * Bio map function. It allocates dm_verity_io structure and bio vector and
  669. * fills them. Then it issues prefetches and the I/O.
  670. */
  671. static int verity_map(struct dm_target *ti, struct bio *bio)
  672. {
  673. struct dm_verity *v = ti->private;
  674. struct dm_verity_io *io;
  675. bio_set_dev(bio, v->data_dev->bdev);
  676. bio->bi_iter.bi_sector = verity_map_sector(v, bio->bi_iter.bi_sector);
  677. if (((unsigned int)bio->bi_iter.bi_sector | bio_sectors(bio)) &
  678. ((1 << (v->data_dev_block_bits - SECTOR_SHIFT)) - 1)) {
  679. DMERR_LIMIT("unaligned io");
  680. return DM_MAPIO_KILL;
  681. }
  682. if (bio_end_sector(bio) >>
  683. (v->data_dev_block_bits - SECTOR_SHIFT) > v->data_blocks) {
  684. DMERR_LIMIT("io out of range");
  685. return DM_MAPIO_KILL;
  686. }
  687. if (bio_data_dir(bio) == WRITE)
  688. return DM_MAPIO_KILL;
  689. io = dm_per_bio_data(bio, ti->per_io_data_size);
  690. io->v = v;
  691. io->orig_bi_end_io = bio->bi_end_io;
  692. io->block = bio->bi_iter.bi_sector >> (v->data_dev_block_bits - SECTOR_SHIFT);
  693. io->n_blocks = bio->bi_iter.bi_size >> v->data_dev_block_bits;
  694. io->had_mismatch = false;
  695. bio->bi_end_io = verity_end_io;
  696. bio->bi_private = io;
  697. io->iter = bio->bi_iter;
  698. verity_fec_init_io(io);
  699. verity_submit_prefetch(v, io, bio->bi_ioprio);
  700. submit_bio_noacct(bio);
  701. return DM_MAPIO_SUBMITTED;
  702. }
  703. static void verity_postsuspend(struct dm_target *ti)
  704. {
  705. struct dm_verity *v = ti->private;
  706. flush_workqueue(v->verify_wq);
  707. dm_bufio_client_reset(v->bufio);
  708. }
  709. /*
  710. * Status: V (valid) or C (corruption found)
  711. */
  712. static void verity_status(struct dm_target *ti, status_type_t type,
  713. unsigned int status_flags, char *result, unsigned int maxlen)
  714. {
  715. struct dm_verity *v = ti->private;
  716. unsigned int args = 0;
  717. unsigned int sz = 0;
  718. unsigned int x;
  719. switch (type) {
  720. case STATUSTYPE_INFO:
  721. DMEMIT("%c", v->hash_failed ? 'C' : 'V');
  722. if (verity_fec_is_enabled(v))
  723. DMEMIT(" %lld", atomic64_read(&v->fec->corrected));
  724. else
  725. DMEMIT(" -");
  726. break;
  727. case STATUSTYPE_TABLE:
  728. DMEMIT("%u %s %s %u %u %llu %llu %s ",
  729. v->version,
  730. v->data_dev->name,
  731. v->hash_dev->name,
  732. 1 << v->data_dev_block_bits,
  733. 1 << v->hash_dev_block_bits,
  734. (unsigned long long)v->data_blocks,
  735. (unsigned long long)v->hash_start,
  736. v->alg_name
  737. );
  738. for (x = 0; x < v->digest_size; x++)
  739. DMEMIT("%02x", v->root_digest[x]);
  740. DMEMIT(" ");
  741. if (!v->salt_size)
  742. DMEMIT("-");
  743. else
  744. for (x = 0; x < v->salt_size; x++)
  745. DMEMIT("%02x", v->salt[x]);
  746. if (v->mode != DM_VERITY_MODE_EIO)
  747. args++;
  748. if (v->error_mode != DM_VERITY_MODE_EIO)
  749. args++;
  750. if (verity_fec_is_enabled(v))
  751. args += DM_VERITY_OPTS_FEC;
  752. if (v->zero_digest)
  753. args++;
  754. if (v->validated_blocks)
  755. args++;
  756. if (v->use_bh_wq)
  757. args++;
  758. if (v->signature_key_desc)
  759. args += DM_VERITY_ROOT_HASH_VERIFICATION_OPTS;
  760. if (!args)
  761. return;
  762. DMEMIT(" %u", args);
  763. if (v->mode != DM_VERITY_MODE_EIO) {
  764. DMEMIT(" ");
  765. switch (v->mode) {
  766. case DM_VERITY_MODE_LOGGING:
  767. DMEMIT(DM_VERITY_OPT_LOGGING);
  768. break;
  769. case DM_VERITY_MODE_RESTART:
  770. DMEMIT(DM_VERITY_OPT_RESTART);
  771. break;
  772. case DM_VERITY_MODE_PANIC:
  773. DMEMIT(DM_VERITY_OPT_PANIC);
  774. break;
  775. default:
  776. BUG();
  777. }
  778. }
  779. if (v->error_mode != DM_VERITY_MODE_EIO) {
  780. DMEMIT(" ");
  781. switch (v->error_mode) {
  782. case DM_VERITY_MODE_RESTART:
  783. DMEMIT(DM_VERITY_OPT_ERROR_RESTART);
  784. break;
  785. case DM_VERITY_MODE_PANIC:
  786. DMEMIT(DM_VERITY_OPT_ERROR_PANIC);
  787. break;
  788. default:
  789. BUG();
  790. }
  791. }
  792. if (v->zero_digest)
  793. DMEMIT(" " DM_VERITY_OPT_IGN_ZEROES);
  794. if (v->validated_blocks)
  795. DMEMIT(" " DM_VERITY_OPT_AT_MOST_ONCE);
  796. if (v->use_bh_wq)
  797. DMEMIT(" " DM_VERITY_OPT_TASKLET_VERIFY);
  798. sz = verity_fec_status_table(v, sz, result, maxlen);
  799. if (v->signature_key_desc)
  800. DMEMIT(" " DM_VERITY_ROOT_HASH_VERIFICATION_OPT_SIG_KEY
  801. " %s", v->signature_key_desc);
  802. break;
  803. case STATUSTYPE_IMA:
  804. DMEMIT_TARGET_NAME_VERSION(ti->type);
  805. DMEMIT(",hash_failed=%c", v->hash_failed ? 'C' : 'V');
  806. DMEMIT(",verity_version=%u", v->version);
  807. DMEMIT(",data_device_name=%s", v->data_dev->name);
  808. DMEMIT(",hash_device_name=%s", v->hash_dev->name);
  809. DMEMIT(",verity_algorithm=%s", v->alg_name);
  810. DMEMIT(",root_digest=");
  811. for (x = 0; x < v->digest_size; x++)
  812. DMEMIT("%02x", v->root_digest[x]);
  813. DMEMIT(",salt=");
  814. if (!v->salt_size)
  815. DMEMIT("-");
  816. else
  817. for (x = 0; x < v->salt_size; x++)
  818. DMEMIT("%02x", v->salt[x]);
  819. DMEMIT(",ignore_zero_blocks=%c", v->zero_digest ? 'y' : 'n');
  820. DMEMIT(",check_at_most_once=%c", v->validated_blocks ? 'y' : 'n');
  821. if (v->signature_key_desc)
  822. DMEMIT(",root_hash_sig_key_desc=%s", v->signature_key_desc);
  823. if (v->mode != DM_VERITY_MODE_EIO) {
  824. DMEMIT(",verity_mode=");
  825. switch (v->mode) {
  826. case DM_VERITY_MODE_LOGGING:
  827. DMEMIT(DM_VERITY_OPT_LOGGING);
  828. break;
  829. case DM_VERITY_MODE_RESTART:
  830. DMEMIT(DM_VERITY_OPT_RESTART);
  831. break;
  832. case DM_VERITY_MODE_PANIC:
  833. DMEMIT(DM_VERITY_OPT_PANIC);
  834. break;
  835. default:
  836. DMEMIT("invalid");
  837. }
  838. }
  839. if (v->error_mode != DM_VERITY_MODE_EIO) {
  840. DMEMIT(",verity_error_mode=");
  841. switch (v->error_mode) {
  842. case DM_VERITY_MODE_RESTART:
  843. DMEMIT(DM_VERITY_OPT_ERROR_RESTART);
  844. break;
  845. case DM_VERITY_MODE_PANIC:
  846. DMEMIT(DM_VERITY_OPT_ERROR_PANIC);
  847. break;
  848. default:
  849. DMEMIT("invalid");
  850. }
  851. }
  852. DMEMIT(";");
  853. break;
  854. }
  855. }
  856. static int verity_prepare_ioctl(struct dm_target *ti, struct block_device **bdev,
  857. unsigned int cmd, unsigned long arg,
  858. bool *forward)
  859. {
  860. struct dm_verity *v = ti->private;
  861. *bdev = v->data_dev->bdev;
  862. if (ti->len != bdev_nr_sectors(v->data_dev->bdev))
  863. return 1;
  864. return 0;
  865. }
  866. static int verity_iterate_devices(struct dm_target *ti,
  867. iterate_devices_callout_fn fn, void *data)
  868. {
  869. struct dm_verity *v = ti->private;
  870. return fn(ti, v->data_dev, 0, ti->len, data);
  871. }
  872. static void verity_io_hints(struct dm_target *ti, struct queue_limits *limits)
  873. {
  874. struct dm_verity *v = ti->private;
  875. if (limits->logical_block_size < 1 << v->data_dev_block_bits)
  876. limits->logical_block_size = 1 << v->data_dev_block_bits;
  877. if (limits->physical_block_size < 1 << v->data_dev_block_bits)
  878. limits->physical_block_size = 1 << v->data_dev_block_bits;
  879. limits->io_min = limits->logical_block_size;
  880. /*
  881. * Similar to what dm-crypt does, opt dm-verity out of support for
  882. * direct I/O that is aligned to less than the traditional direct I/O
  883. * alignment requirement of logical_block_size. This prevents dm-verity
  884. * data blocks from crossing pages, eliminating various edge cases.
  885. */
  886. limits->dma_alignment = limits->logical_block_size - 1;
  887. }
  888. #ifdef CONFIG_SECURITY
  889. static int verity_init_sig(struct dm_verity *v, const void *sig,
  890. size_t sig_size)
  891. {
  892. v->sig_size = sig_size;
  893. if (sig) {
  894. v->root_digest_sig = kmemdup(sig, v->sig_size, GFP_KERNEL);
  895. if (!v->root_digest_sig)
  896. return -ENOMEM;
  897. }
  898. return 0;
  899. }
  900. static void verity_free_sig(struct dm_verity *v)
  901. {
  902. kfree(v->root_digest_sig);
  903. }
  904. #else
  905. static inline int verity_init_sig(struct dm_verity *v, const void *sig,
  906. size_t sig_size)
  907. {
  908. return 0;
  909. }
  910. static inline void verity_free_sig(struct dm_verity *v)
  911. {
  912. }
  913. #endif /* CONFIG_SECURITY */
  914. static void verity_dtr(struct dm_target *ti)
  915. {
  916. struct dm_verity *v = ti->private;
  917. if (v->verify_wq)
  918. destroy_workqueue(v->verify_wq);
  919. mempool_exit(&v->recheck_pool);
  920. if (v->io)
  921. dm_io_client_destroy(v->io);
  922. if (v->bufio)
  923. dm_bufio_client_destroy(v->bufio);
  924. kvfree(v->validated_blocks);
  925. kfree(v->salt);
  926. kfree(v->initial_hashstate.shash);
  927. kfree(v->root_digest);
  928. kfree(v->zero_digest);
  929. verity_free_sig(v);
  930. crypto_free_shash(v->shash_tfm);
  931. kfree(v->alg_name);
  932. if (v->hash_dev)
  933. dm_put_device(ti, v->hash_dev);
  934. if (v->data_dev)
  935. dm_put_device(ti, v->data_dev);
  936. verity_fec_dtr(v);
  937. kfree(v->signature_key_desc);
  938. if (v->use_bh_wq)
  939. static_branch_dec(&use_bh_wq_enabled);
  940. kfree(v);
  941. dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1);
  942. }
  943. static int verity_alloc_most_once(struct dm_verity *v)
  944. {
  945. struct dm_target *ti = v->ti;
  946. if (v->validated_blocks)
  947. return 0;
  948. /* the bitset can only handle INT_MAX blocks */
  949. if (v->data_blocks > INT_MAX) {
  950. ti->error = "device too large to use check_at_most_once";
  951. return -E2BIG;
  952. }
  953. v->validated_blocks = kvcalloc(BITS_TO_LONGS(v->data_blocks),
  954. sizeof(unsigned long),
  955. GFP_KERNEL);
  956. if (!v->validated_blocks) {
  957. ti->error = "failed to allocate bitset for check_at_most_once";
  958. return -ENOMEM;
  959. }
  960. return 0;
  961. }
  962. static int verity_alloc_zero_digest(struct dm_verity *v)
  963. {
  964. int r = -ENOMEM;
  965. struct dm_verity_io *io;
  966. u8 *zero_data;
  967. if (v->zero_digest)
  968. return 0;
  969. v->zero_digest = kmalloc(v->digest_size, GFP_KERNEL);
  970. if (!v->zero_digest)
  971. return r;
  972. io = kmalloc(v->ti->per_io_data_size, GFP_KERNEL);
  973. if (!io)
  974. return r; /* verity_dtr will free zero_digest */
  975. zero_data = kzalloc(1 << v->data_dev_block_bits, GFP_KERNEL);
  976. if (!zero_data)
  977. goto out;
  978. r = verity_hash(v, io, zero_data, 1 << v->data_dev_block_bits,
  979. v->zero_digest);
  980. out:
  981. kfree(io);
  982. kfree(zero_data);
  983. return r;
  984. }
  985. static inline bool verity_is_verity_mode(const char *arg_name)
  986. {
  987. return (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING) ||
  988. !strcasecmp(arg_name, DM_VERITY_OPT_RESTART) ||
  989. !strcasecmp(arg_name, DM_VERITY_OPT_PANIC));
  990. }
  991. static int verity_parse_verity_mode(struct dm_verity *v, const char *arg_name)
  992. {
  993. if (v->mode)
  994. return -EINVAL;
  995. if (!strcasecmp(arg_name, DM_VERITY_OPT_LOGGING))
  996. v->mode = DM_VERITY_MODE_LOGGING;
  997. else if (!strcasecmp(arg_name, DM_VERITY_OPT_RESTART))
  998. v->mode = DM_VERITY_MODE_RESTART;
  999. else if (!strcasecmp(arg_name, DM_VERITY_OPT_PANIC))
  1000. v->mode = DM_VERITY_MODE_PANIC;
  1001. return 0;
  1002. }
  1003. static inline bool verity_is_verity_error_mode(const char *arg_name)
  1004. {
  1005. return (!strcasecmp(arg_name, DM_VERITY_OPT_ERROR_RESTART) ||
  1006. !strcasecmp(arg_name, DM_VERITY_OPT_ERROR_PANIC));
  1007. }
  1008. static int verity_parse_verity_error_mode(struct dm_verity *v, const char *arg_name)
  1009. {
  1010. if (v->error_mode)
  1011. return -EINVAL;
  1012. if (!strcasecmp(arg_name, DM_VERITY_OPT_ERROR_RESTART))
  1013. v->error_mode = DM_VERITY_MODE_RESTART;
  1014. else if (!strcasecmp(arg_name, DM_VERITY_OPT_ERROR_PANIC))
  1015. v->error_mode = DM_VERITY_MODE_PANIC;
  1016. return 0;
  1017. }
  1018. static int verity_parse_opt_args(struct dm_arg_set *as, struct dm_verity *v,
  1019. struct dm_verity_sig_opts *verify_args,
  1020. bool only_modifier_opts)
  1021. {
  1022. int r = 0;
  1023. unsigned int argc;
  1024. struct dm_target *ti = v->ti;
  1025. const char *arg_name;
  1026. static const struct dm_arg _args[] = {
  1027. {0, DM_VERITY_OPTS_MAX, "Invalid number of feature args"},
  1028. };
  1029. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  1030. if (r)
  1031. return -EINVAL;
  1032. if (!argc)
  1033. return 0;
  1034. do {
  1035. arg_name = dm_shift_arg(as);
  1036. argc--;
  1037. if (verity_is_verity_mode(arg_name)) {
  1038. if (only_modifier_opts)
  1039. continue;
  1040. r = verity_parse_verity_mode(v, arg_name);
  1041. if (r) {
  1042. ti->error = "Conflicting error handling parameters";
  1043. return r;
  1044. }
  1045. continue;
  1046. } else if (verity_is_verity_error_mode(arg_name)) {
  1047. if (only_modifier_opts)
  1048. continue;
  1049. r = verity_parse_verity_error_mode(v, arg_name);
  1050. if (r) {
  1051. ti->error = "Conflicting error handling parameters";
  1052. return r;
  1053. }
  1054. continue;
  1055. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_IGN_ZEROES)) {
  1056. if (only_modifier_opts)
  1057. continue;
  1058. r = verity_alloc_zero_digest(v);
  1059. if (r) {
  1060. ti->error = "Cannot allocate zero digest";
  1061. return r;
  1062. }
  1063. continue;
  1064. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_AT_MOST_ONCE)) {
  1065. if (only_modifier_opts)
  1066. continue;
  1067. r = verity_alloc_most_once(v);
  1068. if (r)
  1069. return r;
  1070. continue;
  1071. } else if (!strcasecmp(arg_name, DM_VERITY_OPT_TASKLET_VERIFY)) {
  1072. v->use_bh_wq = true;
  1073. static_branch_inc(&use_bh_wq_enabled);
  1074. continue;
  1075. } else if (verity_is_fec_opt_arg(arg_name)) {
  1076. if (only_modifier_opts)
  1077. continue;
  1078. r = verity_fec_parse_opt_args(as, v, &argc, arg_name);
  1079. if (r)
  1080. return r;
  1081. continue;
  1082. } else if (verity_verify_is_sig_opt_arg(arg_name)) {
  1083. if (only_modifier_opts)
  1084. continue;
  1085. r = verity_verify_sig_parse_opt_args(as, v,
  1086. verify_args,
  1087. &argc, arg_name);
  1088. if (r)
  1089. return r;
  1090. continue;
  1091. } else if (only_modifier_opts) {
  1092. /*
  1093. * Ignore unrecognized opt, could easily be an extra
  1094. * argument to an option whose parsing was skipped.
  1095. * Normal parsing (@only_modifier_opts=false) will
  1096. * properly parse all options (and their extra args).
  1097. */
  1098. continue;
  1099. }
  1100. DMERR("Unrecognized verity feature request: %s", arg_name);
  1101. ti->error = "Unrecognized verity feature request";
  1102. return -EINVAL;
  1103. } while (argc && !r);
  1104. return r;
  1105. }
  1106. static int verity_setup_hash_alg(struct dm_verity *v, const char *alg_name)
  1107. {
  1108. struct dm_target *ti = v->ti;
  1109. struct crypto_shash *shash;
  1110. v->alg_name = kstrdup(alg_name, GFP_KERNEL);
  1111. if (!v->alg_name) {
  1112. ti->error = "Cannot allocate algorithm name";
  1113. return -ENOMEM;
  1114. }
  1115. shash = crypto_alloc_shash(alg_name, 0, 0);
  1116. if (IS_ERR(shash)) {
  1117. ti->error = "Cannot initialize hash function";
  1118. return PTR_ERR(shash);
  1119. }
  1120. v->shash_tfm = shash;
  1121. v->digest_size = crypto_shash_digestsize(shash);
  1122. if ((1 << v->hash_dev_block_bits) < v->digest_size * 2) {
  1123. ti->error = "Digest size too big";
  1124. return -EINVAL;
  1125. }
  1126. if (likely(v->version && strcmp(alg_name, "sha256") == 0)) {
  1127. /*
  1128. * Fast path: use the library API for reduced overhead and
  1129. * interleaved hashing support.
  1130. */
  1131. v->use_sha256_lib = true;
  1132. if (sha256_finup_2x_is_optimized())
  1133. v->use_sha256_finup_2x = true;
  1134. ti->per_io_data_size =
  1135. offsetofend(struct dm_verity_io, hash_ctx.sha256);
  1136. } else {
  1137. /* Fallback case: use the generic crypto API. */
  1138. ti->per_io_data_size =
  1139. offsetofend(struct dm_verity_io, hash_ctx.shash) +
  1140. crypto_shash_descsize(shash);
  1141. }
  1142. return 0;
  1143. }
  1144. static int verity_setup_salt_and_hashstate(struct dm_verity *v, const char *arg)
  1145. {
  1146. struct dm_target *ti = v->ti;
  1147. if (strcmp(arg, "-") != 0) {
  1148. v->salt_size = strlen(arg) / 2;
  1149. v->salt = kmalloc(v->salt_size, GFP_KERNEL);
  1150. if (!v->salt) {
  1151. ti->error = "Cannot allocate salt";
  1152. return -ENOMEM;
  1153. }
  1154. if (strlen(arg) != v->salt_size * 2 ||
  1155. hex2bin(v->salt, arg, v->salt_size)) {
  1156. ti->error = "Invalid salt";
  1157. return -EINVAL;
  1158. }
  1159. }
  1160. if (likely(v->use_sha256_lib)) {
  1161. /* Implies version 1: salt at beginning */
  1162. v->initial_hashstate.sha256 =
  1163. kmalloc_obj(struct sha256_ctx);
  1164. if (!v->initial_hashstate.sha256) {
  1165. ti->error = "Cannot allocate initial hash state";
  1166. return -ENOMEM;
  1167. }
  1168. sha256_init(v->initial_hashstate.sha256);
  1169. sha256_update(v->initial_hashstate.sha256,
  1170. v->salt, v->salt_size);
  1171. } else if (v->version) { /* Version 1: salt at beginning */
  1172. SHASH_DESC_ON_STACK(desc, v->shash_tfm);
  1173. int r;
  1174. /*
  1175. * Compute the pre-salted hash state that can be passed to
  1176. * crypto_shash_import() for each block later.
  1177. */
  1178. v->initial_hashstate.shash = kmalloc(
  1179. crypto_shash_statesize(v->shash_tfm), GFP_KERNEL);
  1180. if (!v->initial_hashstate.shash) {
  1181. ti->error = "Cannot allocate initial hash state";
  1182. return -ENOMEM;
  1183. }
  1184. desc->tfm = v->shash_tfm;
  1185. r = crypto_shash_init(desc) ?:
  1186. crypto_shash_update(desc, v->salt, v->salt_size) ?:
  1187. crypto_shash_export(desc, v->initial_hashstate.shash);
  1188. if (r) {
  1189. ti->error = "Cannot set up initial hash state";
  1190. return r;
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. /*
  1196. * Target parameters:
  1197. * <version> The current format is version 1.
  1198. * Vsn 0 is compatible with original Chromium OS releases.
  1199. * <data device>
  1200. * <hash device>
  1201. * <data block size>
  1202. * <hash block size>
  1203. * <the number of data blocks>
  1204. * <hash start block>
  1205. * <algorithm>
  1206. * <digest>
  1207. * <salt> Hex string or "-" if no salt.
  1208. */
  1209. static int verity_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1210. {
  1211. struct dm_verity *v;
  1212. struct dm_verity_sig_opts verify_args = {0};
  1213. struct dm_arg_set as;
  1214. unsigned int num;
  1215. unsigned long long num_ll;
  1216. int r;
  1217. int i;
  1218. sector_t hash_position;
  1219. char dummy;
  1220. char *root_hash_digest_to_validate;
  1221. v = kzalloc_obj(struct dm_verity);
  1222. if (!v) {
  1223. ti->error = "Cannot allocate verity structure";
  1224. return -ENOMEM;
  1225. }
  1226. ti->private = v;
  1227. v->ti = ti;
  1228. r = verity_fec_ctr_alloc(v);
  1229. if (r)
  1230. goto bad;
  1231. if ((dm_table_get_mode(ti->table) & ~BLK_OPEN_READ)) {
  1232. ti->error = "Device must be readonly";
  1233. r = -EINVAL;
  1234. goto bad;
  1235. }
  1236. if (argc < 10) {
  1237. ti->error = "Not enough arguments";
  1238. r = -EINVAL;
  1239. goto bad;
  1240. }
  1241. /* Parse optional parameters that modify primary args */
  1242. if (argc > 10) {
  1243. as.argc = argc - 10;
  1244. as.argv = argv + 10;
  1245. r = verity_parse_opt_args(&as, v, &verify_args, true);
  1246. if (r < 0)
  1247. goto bad;
  1248. }
  1249. if (sscanf(argv[0], "%u%c", &num, &dummy) != 1 ||
  1250. num > 1) {
  1251. ti->error = "Invalid version";
  1252. r = -EINVAL;
  1253. goto bad;
  1254. }
  1255. v->version = num;
  1256. r = dm_get_device(ti, argv[1], BLK_OPEN_READ, &v->data_dev);
  1257. if (r) {
  1258. ti->error = "Data device lookup failed";
  1259. goto bad;
  1260. }
  1261. r = dm_get_device(ti, argv[2], BLK_OPEN_READ, &v->hash_dev);
  1262. if (r) {
  1263. ti->error = "Hash device lookup failed";
  1264. goto bad;
  1265. }
  1266. if (sscanf(argv[3], "%u%c", &num, &dummy) != 1 ||
  1267. !num || (num & (num - 1)) ||
  1268. num < bdev_logical_block_size(v->data_dev->bdev) ||
  1269. num > PAGE_SIZE) {
  1270. ti->error = "Invalid data device block size";
  1271. r = -EINVAL;
  1272. goto bad;
  1273. }
  1274. v->data_dev_block_bits = __ffs(num);
  1275. if (sscanf(argv[4], "%u%c", &num, &dummy) != 1 ||
  1276. !num || (num & (num - 1)) ||
  1277. num < bdev_logical_block_size(v->hash_dev->bdev) ||
  1278. num > INT_MAX) {
  1279. ti->error = "Invalid hash device block size";
  1280. r = -EINVAL;
  1281. goto bad;
  1282. }
  1283. v->hash_dev_block_bits = __ffs(num);
  1284. if (sscanf(argv[5], "%llu%c", &num_ll, &dummy) != 1 ||
  1285. (sector_t)(num_ll << (v->data_dev_block_bits - SECTOR_SHIFT))
  1286. >> (v->data_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  1287. ti->error = "Invalid data blocks";
  1288. r = -EINVAL;
  1289. goto bad;
  1290. }
  1291. v->data_blocks = num_ll;
  1292. if (ti->len > (v->data_blocks << (v->data_dev_block_bits - SECTOR_SHIFT))) {
  1293. ti->error = "Data device is too small";
  1294. r = -EINVAL;
  1295. goto bad;
  1296. }
  1297. if (sscanf(argv[6], "%llu%c", &num_ll, &dummy) != 1 ||
  1298. (sector_t)(num_ll << (v->hash_dev_block_bits - SECTOR_SHIFT))
  1299. >> (v->hash_dev_block_bits - SECTOR_SHIFT) != num_ll) {
  1300. ti->error = "Invalid hash start";
  1301. r = -EINVAL;
  1302. goto bad;
  1303. }
  1304. v->hash_start = num_ll;
  1305. r = verity_setup_hash_alg(v, argv[7]);
  1306. if (r)
  1307. goto bad;
  1308. v->root_digest = kmalloc(v->digest_size, GFP_KERNEL);
  1309. if (!v->root_digest) {
  1310. ti->error = "Cannot allocate root digest";
  1311. r = -ENOMEM;
  1312. goto bad;
  1313. }
  1314. if (strlen(argv[8]) != v->digest_size * 2 ||
  1315. hex2bin(v->root_digest, argv[8], v->digest_size)) {
  1316. ti->error = "Invalid root digest";
  1317. r = -EINVAL;
  1318. goto bad;
  1319. }
  1320. root_hash_digest_to_validate = argv[8];
  1321. r = verity_setup_salt_and_hashstate(v, argv[9]);
  1322. if (r)
  1323. goto bad;
  1324. argv += 10;
  1325. argc -= 10;
  1326. /* Optional parameters */
  1327. if (argc) {
  1328. as.argc = argc;
  1329. as.argv = argv;
  1330. r = verity_parse_opt_args(&as, v, &verify_args, false);
  1331. if (r < 0)
  1332. goto bad;
  1333. }
  1334. /* Root hash signature is an optional parameter */
  1335. r = verity_verify_root_hash(root_hash_digest_to_validate,
  1336. strlen(root_hash_digest_to_validate),
  1337. verify_args.sig,
  1338. verify_args.sig_size);
  1339. if (r < 0) {
  1340. ti->error = "Root hash verification failed";
  1341. goto bad;
  1342. }
  1343. r = verity_init_sig(v, verify_args.sig, verify_args.sig_size);
  1344. if (r < 0) {
  1345. ti->error = "Cannot allocate root digest signature";
  1346. goto bad;
  1347. }
  1348. v->hash_per_block_bits =
  1349. __fls((1 << v->hash_dev_block_bits) / v->digest_size);
  1350. v->levels = 0;
  1351. if (v->data_blocks)
  1352. while (v->hash_per_block_bits * v->levels < 64 &&
  1353. (unsigned long long)(v->data_blocks - 1) >>
  1354. (v->hash_per_block_bits * v->levels))
  1355. v->levels++;
  1356. if (v->levels > DM_VERITY_MAX_LEVELS) {
  1357. ti->error = "Too many tree levels";
  1358. r = -E2BIG;
  1359. goto bad;
  1360. }
  1361. hash_position = v->hash_start;
  1362. for (i = v->levels - 1; i >= 0; i--) {
  1363. sector_t s;
  1364. v->hash_level_block[i] = hash_position;
  1365. s = (v->data_blocks + ((sector_t)1 << ((i + 1) * v->hash_per_block_bits)) - 1)
  1366. >> ((i + 1) * v->hash_per_block_bits);
  1367. if (hash_position + s < hash_position) {
  1368. ti->error = "Hash device offset overflow";
  1369. r = -E2BIG;
  1370. goto bad;
  1371. }
  1372. hash_position += s;
  1373. }
  1374. v->hash_blocks = hash_position;
  1375. r = mempool_init_page_pool(&v->recheck_pool, 1, 0);
  1376. if (unlikely(r)) {
  1377. ti->error = "Cannot allocate mempool";
  1378. goto bad;
  1379. }
  1380. v->io = dm_io_client_create();
  1381. if (IS_ERR(v->io)) {
  1382. r = PTR_ERR(v->io);
  1383. v->io = NULL;
  1384. ti->error = "Cannot allocate dm io";
  1385. goto bad;
  1386. }
  1387. v->bufio = dm_bufio_client_create(v->hash_dev->bdev,
  1388. 1 << v->hash_dev_block_bits, 1, sizeof(struct buffer_aux),
  1389. dm_bufio_alloc_callback, NULL,
  1390. v->use_bh_wq ? DM_BUFIO_CLIENT_NO_SLEEP : 0);
  1391. if (IS_ERR(v->bufio)) {
  1392. ti->error = "Cannot initialize dm-bufio";
  1393. r = PTR_ERR(v->bufio);
  1394. v->bufio = NULL;
  1395. goto bad;
  1396. }
  1397. if (dm_bufio_get_device_size(v->bufio) < v->hash_blocks) {
  1398. ti->error = "Hash device is too small";
  1399. r = -E2BIG;
  1400. goto bad;
  1401. }
  1402. /*
  1403. * Using WQ_HIGHPRI improves throughput and completion latency by
  1404. * reducing wait times when reading from a dm-verity device.
  1405. *
  1406. * Also as required for the "try_verify_in_tasklet" feature: WQ_HIGHPRI
  1407. * allows verify_wq to preempt softirq since verification in softirq
  1408. * will fall-back to using it for error handling (or if the bufio cache
  1409. * doesn't have required hashes).
  1410. */
  1411. v->verify_wq = alloc_workqueue("kverityd",
  1412. WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_PERCPU,
  1413. 0);
  1414. if (!v->verify_wq) {
  1415. ti->error = "Cannot allocate workqueue";
  1416. r = -ENOMEM;
  1417. goto bad;
  1418. }
  1419. r = verity_fec_ctr(v);
  1420. if (r)
  1421. goto bad;
  1422. ti->per_io_data_size = roundup(ti->per_io_data_size,
  1423. __alignof__(struct dm_verity_io));
  1424. verity_verify_sig_opts_cleanup(&verify_args);
  1425. dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1);
  1426. return 0;
  1427. bad:
  1428. verity_verify_sig_opts_cleanup(&verify_args);
  1429. dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0);
  1430. verity_dtr(ti);
  1431. return r;
  1432. }
  1433. /*
  1434. * Get the verity mode (error behavior) of a verity target.
  1435. *
  1436. * Returns the verity mode of the target, or -EINVAL if 'ti' is not a verity
  1437. * target.
  1438. */
  1439. int dm_verity_get_mode(struct dm_target *ti)
  1440. {
  1441. struct dm_verity *v = ti->private;
  1442. if (!dm_is_verity_target(ti))
  1443. return -EINVAL;
  1444. return v->mode;
  1445. }
  1446. /*
  1447. * Get the root digest of a verity target.
  1448. *
  1449. * Returns a copy of the root digest, the caller is responsible for
  1450. * freeing the memory of the digest.
  1451. */
  1452. int dm_verity_get_root_digest(struct dm_target *ti, u8 **root_digest, unsigned int *digest_size)
  1453. {
  1454. struct dm_verity *v = ti->private;
  1455. if (!dm_is_verity_target(ti))
  1456. return -EINVAL;
  1457. *root_digest = kmemdup(v->root_digest, v->digest_size, GFP_KERNEL);
  1458. if (*root_digest == NULL)
  1459. return -ENOMEM;
  1460. *digest_size = v->digest_size;
  1461. return 0;
  1462. }
  1463. #ifdef CONFIG_SECURITY
  1464. #ifdef CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG
  1465. static int verity_security_set_signature(struct block_device *bdev,
  1466. struct dm_verity *v)
  1467. {
  1468. /*
  1469. * if the dm-verity target is unsigned, v->root_digest_sig will
  1470. * be NULL, and the hook call is still required to let LSMs mark
  1471. * the device as unsigned. This information is crucial for LSMs to
  1472. * block operations such as execution on unsigned files
  1473. */
  1474. return security_bdev_setintegrity(bdev,
  1475. LSM_INT_DMVERITY_SIG_VALID,
  1476. v->root_digest_sig,
  1477. v->sig_size);
  1478. }
  1479. #else
  1480. static inline int verity_security_set_signature(struct block_device *bdev,
  1481. struct dm_verity *v)
  1482. {
  1483. return 0;
  1484. }
  1485. #endif /* CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG */
  1486. /*
  1487. * Expose verity target's root hash and signature data to LSMs before resume.
  1488. *
  1489. * Returns 0 on success, or -ENOMEM if the system is out of memory.
  1490. */
  1491. static int verity_preresume(struct dm_target *ti)
  1492. {
  1493. struct block_device *bdev;
  1494. struct dm_verity_digest root_digest;
  1495. struct dm_verity *v;
  1496. int r;
  1497. v = ti->private;
  1498. bdev = dm_disk(dm_table_get_md(ti->table))->part0;
  1499. root_digest.digest = v->root_digest;
  1500. root_digest.digest_len = v->digest_size;
  1501. root_digest.alg = crypto_shash_alg_name(v->shash_tfm);
  1502. r = security_bdev_setintegrity(bdev, LSM_INT_DMVERITY_ROOTHASH, &root_digest,
  1503. sizeof(root_digest));
  1504. if (r)
  1505. return r;
  1506. r = verity_security_set_signature(bdev, v);
  1507. if (r)
  1508. goto bad;
  1509. return 0;
  1510. bad:
  1511. security_bdev_setintegrity(bdev, LSM_INT_DMVERITY_ROOTHASH, NULL, 0);
  1512. return r;
  1513. }
  1514. #endif /* CONFIG_SECURITY */
  1515. static struct target_type verity_target = {
  1516. .name = "verity",
  1517. /* Note: the LSMs depend on the singleton and immutable features */
  1518. .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
  1519. .version = {1, 13, 0},
  1520. .module = THIS_MODULE,
  1521. .ctr = verity_ctr,
  1522. .dtr = verity_dtr,
  1523. .map = verity_map,
  1524. .postsuspend = verity_postsuspend,
  1525. .status = verity_status,
  1526. .prepare_ioctl = verity_prepare_ioctl,
  1527. .iterate_devices = verity_iterate_devices,
  1528. .io_hints = verity_io_hints,
  1529. #ifdef CONFIG_SECURITY
  1530. .preresume = verity_preresume,
  1531. #endif /* CONFIG_SECURITY */
  1532. };
  1533. static int __init dm_verity_init(void)
  1534. {
  1535. int r;
  1536. r = dm_verity_verify_sig_init();
  1537. if (r)
  1538. return r;
  1539. r = dm_register_target(&verity_target);
  1540. if (r) {
  1541. dm_verity_verify_sig_exit();
  1542. return r;
  1543. }
  1544. return 0;
  1545. }
  1546. module_init(dm_verity_init);
  1547. static void __exit dm_verity_exit(void)
  1548. {
  1549. dm_unregister_target(&verity_target);
  1550. dm_verity_verify_sig_exit();
  1551. }
  1552. module_exit(dm_verity_exit);
  1553. /*
  1554. * Check whether a DM target is a verity target.
  1555. */
  1556. bool dm_is_verity_target(struct dm_target *ti)
  1557. {
  1558. return ti->type == &verity_target;
  1559. }
  1560. MODULE_AUTHOR("Mikulas Patocka <mpatocka@redhat.com>");
  1561. MODULE_AUTHOR("Mandeep Baines <msb@chromium.org>");
  1562. MODULE_AUTHOR("Will Drewry <wad@chromium.org>");
  1563. MODULE_DESCRIPTION(DM_NAME " target for transparent disk integrity checking");
  1564. MODULE_LICENSE("GPL");