dm-crypt.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010
  1. /*
  2. * Copyright (C) 2003 Jana Saout <jana@saout.de>
  3. * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
  4. * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com>
  6. *
  7. * This file is released under the GPL.
  8. */
  9. #include <linux/completion.h>
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/key.h>
  15. #include <linux/bio.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/mempool.h>
  18. #include <linux/slab.h>
  19. #include <linux/crypto.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/kthread.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/atomic.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/rbtree.h>
  26. #include <linux/ctype.h>
  27. #include <asm/page.h>
  28. #include <asm/unaligned.h>
  29. #include <crypto/hash.h>
  30. #include <crypto/md5.h>
  31. #include <crypto/algapi.h>
  32. #include <crypto/skcipher.h>
  33. #include <crypto/aead.h>
  34. #include <crypto/authenc.h>
  35. #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
  36. #include <keys/user-type.h>
  37. #include <linux/device-mapper.h>
  38. #define DM_MSG_PREFIX "crypt"
  39. /*
  40. * context holding the current state of a multi-part conversion
  41. */
  42. struct convert_context {
  43. struct completion restart;
  44. struct bio *bio_in;
  45. struct bio *bio_out;
  46. struct bvec_iter iter_in;
  47. struct bvec_iter iter_out;
  48. u64 cc_sector;
  49. atomic_t cc_pending;
  50. union {
  51. struct skcipher_request *req;
  52. struct aead_request *req_aead;
  53. } r;
  54. };
  55. /*
  56. * per bio private data
  57. */
  58. struct dm_crypt_io {
  59. struct crypt_config *cc;
  60. struct bio *base_bio;
  61. u8 *integrity_metadata;
  62. bool integrity_metadata_from_pool;
  63. struct work_struct work;
  64. struct convert_context ctx;
  65. atomic_t io_pending;
  66. blk_status_t error;
  67. sector_t sector;
  68. struct rb_node rb_node;
  69. } CRYPTO_MINALIGN_ATTR;
  70. struct dm_crypt_request {
  71. struct convert_context *ctx;
  72. struct scatterlist sg_in[4];
  73. struct scatterlist sg_out[4];
  74. u64 iv_sector;
  75. };
  76. struct crypt_config;
  77. struct crypt_iv_operations {
  78. int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
  79. const char *opts);
  80. void (*dtr)(struct crypt_config *cc);
  81. int (*init)(struct crypt_config *cc);
  82. int (*wipe)(struct crypt_config *cc);
  83. int (*generator)(struct crypt_config *cc, u8 *iv,
  84. struct dm_crypt_request *dmreq);
  85. int (*post)(struct crypt_config *cc, u8 *iv,
  86. struct dm_crypt_request *dmreq);
  87. };
  88. struct iv_benbi_private {
  89. int shift;
  90. };
  91. #define LMK_SEED_SIZE 64 /* hash + 0 */
  92. struct iv_lmk_private {
  93. struct crypto_shash *hash_tfm;
  94. u8 *seed;
  95. };
  96. #define TCW_WHITENING_SIZE 16
  97. struct iv_tcw_private {
  98. struct crypto_shash *crc32_tfm;
  99. u8 *iv_seed;
  100. u8 *whitening;
  101. };
  102. /*
  103. * Crypt: maps a linear range of a block device
  104. * and encrypts / decrypts at the same time.
  105. */
  106. enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
  107. DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
  108. enum cipher_flags {
  109. CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
  110. CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
  111. };
  112. /*
  113. * The fields in here must be read only after initialization.
  114. */
  115. struct crypt_config {
  116. struct dm_dev *dev;
  117. sector_t start;
  118. struct percpu_counter n_allocated_pages;
  119. struct workqueue_struct *io_queue;
  120. struct workqueue_struct *crypt_queue;
  121. spinlock_t write_thread_lock;
  122. struct task_struct *write_thread;
  123. struct rb_root write_tree;
  124. char *cipher_string;
  125. char *cipher_auth;
  126. char *key_string;
  127. const struct crypt_iv_operations *iv_gen_ops;
  128. union {
  129. struct iv_benbi_private benbi;
  130. struct iv_lmk_private lmk;
  131. struct iv_tcw_private tcw;
  132. } iv_gen_private;
  133. u64 iv_offset;
  134. unsigned int iv_size;
  135. unsigned short int sector_size;
  136. unsigned char sector_shift;
  137. union {
  138. struct crypto_skcipher **tfms;
  139. struct crypto_aead **tfms_aead;
  140. } cipher_tfm;
  141. unsigned tfms_count;
  142. unsigned long cipher_flags;
  143. /*
  144. * Layout of each crypto request:
  145. *
  146. * struct skcipher_request
  147. * context
  148. * padding
  149. * struct dm_crypt_request
  150. * padding
  151. * IV
  152. *
  153. * The padding is added so that dm_crypt_request and the IV are
  154. * correctly aligned.
  155. */
  156. unsigned int dmreq_start;
  157. unsigned int per_bio_data_size;
  158. unsigned long flags;
  159. unsigned int key_size;
  160. unsigned int key_parts; /* independent parts in key buffer */
  161. unsigned int key_extra_size; /* additional keys length */
  162. unsigned int key_mac_size; /* MAC key size for authenc(...) */
  163. unsigned int integrity_tag_size;
  164. unsigned int integrity_iv_size;
  165. unsigned int on_disk_tag_size;
  166. /*
  167. * pool for per bio private data, crypto requests,
  168. * encryption requeusts/buffer pages and integrity tags
  169. */
  170. unsigned tag_pool_max_sectors;
  171. mempool_t tag_pool;
  172. mempool_t req_pool;
  173. mempool_t page_pool;
  174. struct bio_set bs;
  175. struct mutex bio_alloc_lock;
  176. u8 *authenc_key; /* space for keys in authenc() format (if used) */
  177. u8 key[0];
  178. };
  179. #define MIN_IOS 64
  180. #define MAX_TAG_SIZE 480
  181. #define POOL_ENTRY_SIZE 512
  182. static DEFINE_SPINLOCK(dm_crypt_clients_lock);
  183. static unsigned dm_crypt_clients_n = 0;
  184. static volatile unsigned long dm_crypt_pages_per_client;
  185. #define DM_CRYPT_MEMORY_PERCENT 2
  186. #define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
  187. static void clone_init(struct dm_crypt_io *, struct bio *);
  188. static void kcryptd_queue_crypt(struct dm_crypt_io *io);
  189. static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
  190. struct scatterlist *sg);
  191. /*
  192. * Use this to access cipher attributes that are independent of the key.
  193. */
  194. static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
  195. {
  196. return cc->cipher_tfm.tfms[0];
  197. }
  198. static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
  199. {
  200. return cc->cipher_tfm.tfms_aead[0];
  201. }
  202. /*
  203. * Different IV generation algorithms:
  204. *
  205. * plain: the initial vector is the 32-bit little-endian version of the sector
  206. * number, padded with zeros if necessary.
  207. *
  208. * plain64: the initial vector is the 64-bit little-endian version of the sector
  209. * number, padded with zeros if necessary.
  210. *
  211. * plain64be: the initial vector is the 64-bit big-endian version of the sector
  212. * number, padded with zeros if necessary.
  213. *
  214. * essiv: "encrypted sector|salt initial vector", the sector number is
  215. * encrypted with the bulk cipher using a salt as key. The salt
  216. * should be derived from the bulk cipher's key via hashing.
  217. *
  218. * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
  219. * (needed for LRW-32-AES and possible other narrow block modes)
  220. *
  221. * null: the initial vector is always zero. Provides compatibility with
  222. * obsolete loop_fish2 devices. Do not use for new devices.
  223. *
  224. * lmk: Compatible implementation of the block chaining mode used
  225. * by the Loop-AES block device encryption system
  226. * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
  227. * It operates on full 512 byte sectors and uses CBC
  228. * with an IV derived from the sector number, the data and
  229. * optionally extra IV seed.
  230. * This means that after decryption the first block
  231. * of sector must be tweaked according to decrypted data.
  232. * Loop-AES can use three encryption schemes:
  233. * version 1: is plain aes-cbc mode
  234. * version 2: uses 64 multikey scheme with lmk IV generator
  235. * version 3: the same as version 2 with additional IV seed
  236. * (it uses 65 keys, last key is used as IV seed)
  237. *
  238. * tcw: Compatible implementation of the block chaining mode used
  239. * by the TrueCrypt device encryption system (prior to version 4.1).
  240. * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
  241. * It operates on full 512 byte sectors and uses CBC
  242. * with an IV derived from initial key and the sector number.
  243. * In addition, whitening value is applied on every sector, whitening
  244. * is calculated from initial key, sector number and mixed using CRC32.
  245. * Note that this encryption scheme is vulnerable to watermarking attacks
  246. * and should be used for old compatible containers access only.
  247. *
  248. * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode)
  249. * The IV is encrypted little-endian byte-offset (with the same key
  250. * and cipher as the volume).
  251. */
  252. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
  253. struct dm_crypt_request *dmreq)
  254. {
  255. memset(iv, 0, cc->iv_size);
  256. *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
  257. return 0;
  258. }
  259. static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
  260. struct dm_crypt_request *dmreq)
  261. {
  262. memset(iv, 0, cc->iv_size);
  263. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  264. return 0;
  265. }
  266. static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
  267. struct dm_crypt_request *dmreq)
  268. {
  269. memset(iv, 0, cc->iv_size);
  270. /* iv_size is at least of size u64; usually it is 16 bytes */
  271. *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
  272. return 0;
  273. }
  274. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
  275. struct dm_crypt_request *dmreq)
  276. {
  277. /*
  278. * ESSIV encryption of the IV is now handled by the crypto API,
  279. * so just pass the plain sector number here.
  280. */
  281. memset(iv, 0, cc->iv_size);
  282. *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
  283. return 0;
  284. }
  285. static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
  286. const char *opts)
  287. {
  288. unsigned bs;
  289. int log;
  290. if (test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags))
  291. bs = crypto_aead_blocksize(any_tfm_aead(cc));
  292. else
  293. bs = crypto_skcipher_blocksize(any_tfm(cc));
  294. log = ilog2(bs);
  295. /* we need to calculate how far we must shift the sector count
  296. * to get the cipher block count, we use this shift in _gen */
  297. if (1 << log != bs) {
  298. ti->error = "cypher blocksize is not a power of 2";
  299. return -EINVAL;
  300. }
  301. if (log > 9) {
  302. ti->error = "cypher blocksize is > 512";
  303. return -EINVAL;
  304. }
  305. cc->iv_gen_private.benbi.shift = 9 - log;
  306. return 0;
  307. }
  308. static void crypt_iv_benbi_dtr(struct crypt_config *cc)
  309. {
  310. }
  311. static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
  312. struct dm_crypt_request *dmreq)
  313. {
  314. __be64 val;
  315. memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
  316. val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
  317. put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
  318. return 0;
  319. }
  320. static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
  321. struct dm_crypt_request *dmreq)
  322. {
  323. memset(iv, 0, cc->iv_size);
  324. return 0;
  325. }
  326. static void crypt_iv_lmk_dtr(struct crypt_config *cc)
  327. {
  328. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  329. if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
  330. crypto_free_shash(lmk->hash_tfm);
  331. lmk->hash_tfm = NULL;
  332. kzfree(lmk->seed);
  333. lmk->seed = NULL;
  334. }
  335. static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
  336. const char *opts)
  337. {
  338. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  339. if (cc->sector_size != (1 << SECTOR_SHIFT)) {
  340. ti->error = "Unsupported sector size for LMK";
  341. return -EINVAL;
  342. }
  343. lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
  344. if (IS_ERR(lmk->hash_tfm)) {
  345. ti->error = "Error initializing LMK hash";
  346. return PTR_ERR(lmk->hash_tfm);
  347. }
  348. /* No seed in LMK version 2 */
  349. if (cc->key_parts == cc->tfms_count) {
  350. lmk->seed = NULL;
  351. return 0;
  352. }
  353. lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
  354. if (!lmk->seed) {
  355. crypt_iv_lmk_dtr(cc);
  356. ti->error = "Error kmallocing seed storage in LMK";
  357. return -ENOMEM;
  358. }
  359. return 0;
  360. }
  361. static int crypt_iv_lmk_init(struct crypt_config *cc)
  362. {
  363. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  364. int subkey_size = cc->key_size / cc->key_parts;
  365. /* LMK seed is on the position of LMK_KEYS + 1 key */
  366. if (lmk->seed)
  367. memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
  368. crypto_shash_digestsize(lmk->hash_tfm));
  369. return 0;
  370. }
  371. static int crypt_iv_lmk_wipe(struct crypt_config *cc)
  372. {
  373. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  374. if (lmk->seed)
  375. memset(lmk->seed, 0, LMK_SEED_SIZE);
  376. return 0;
  377. }
  378. static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
  379. struct dm_crypt_request *dmreq,
  380. u8 *data)
  381. {
  382. struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
  383. SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
  384. struct md5_state md5state;
  385. __le32 buf[4];
  386. int i, r;
  387. desc->tfm = lmk->hash_tfm;
  388. r = crypto_shash_init(desc);
  389. if (r)
  390. return r;
  391. if (lmk->seed) {
  392. r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
  393. if (r)
  394. return r;
  395. }
  396. /* Sector is always 512B, block size 16, add data of blocks 1-31 */
  397. r = crypto_shash_update(desc, data + 16, 16 * 31);
  398. if (r)
  399. return r;
  400. /* Sector is cropped to 56 bits here */
  401. buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
  402. buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
  403. buf[2] = cpu_to_le32(4024);
  404. buf[3] = 0;
  405. r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
  406. if (r)
  407. return r;
  408. /* No MD5 padding here */
  409. r = crypto_shash_export(desc, &md5state);
  410. if (r)
  411. return r;
  412. for (i = 0; i < MD5_HASH_WORDS; i++)
  413. __cpu_to_le32s(&md5state.hash[i]);
  414. memcpy(iv, &md5state.hash, cc->iv_size);
  415. return 0;
  416. }
  417. static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
  418. struct dm_crypt_request *dmreq)
  419. {
  420. struct scatterlist *sg;
  421. u8 *src;
  422. int r = 0;
  423. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
  424. sg = crypt_get_sg_data(cc, dmreq->sg_in);
  425. src = kmap_atomic(sg_page(sg));
  426. r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
  427. kunmap_atomic(src);
  428. } else
  429. memset(iv, 0, cc->iv_size);
  430. return r;
  431. }
  432. static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
  433. struct dm_crypt_request *dmreq)
  434. {
  435. struct scatterlist *sg;
  436. u8 *dst;
  437. int r;
  438. if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
  439. return 0;
  440. sg = crypt_get_sg_data(cc, dmreq->sg_out);
  441. dst = kmap_atomic(sg_page(sg));
  442. r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
  443. /* Tweak the first block of plaintext sector */
  444. if (!r)
  445. crypto_xor(dst + sg->offset, iv, cc->iv_size);
  446. kunmap_atomic(dst);
  447. return r;
  448. }
  449. static void crypt_iv_tcw_dtr(struct crypt_config *cc)
  450. {
  451. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  452. kzfree(tcw->iv_seed);
  453. tcw->iv_seed = NULL;
  454. kzfree(tcw->whitening);
  455. tcw->whitening = NULL;
  456. if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
  457. crypto_free_shash(tcw->crc32_tfm);
  458. tcw->crc32_tfm = NULL;
  459. }
  460. static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
  461. const char *opts)
  462. {
  463. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  464. if (cc->sector_size != (1 << SECTOR_SHIFT)) {
  465. ti->error = "Unsupported sector size for TCW";
  466. return -EINVAL;
  467. }
  468. if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
  469. ti->error = "Wrong key size for TCW";
  470. return -EINVAL;
  471. }
  472. tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
  473. if (IS_ERR(tcw->crc32_tfm)) {
  474. ti->error = "Error initializing CRC32 in TCW";
  475. return PTR_ERR(tcw->crc32_tfm);
  476. }
  477. tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
  478. tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
  479. if (!tcw->iv_seed || !tcw->whitening) {
  480. crypt_iv_tcw_dtr(cc);
  481. ti->error = "Error allocating seed storage in TCW";
  482. return -ENOMEM;
  483. }
  484. return 0;
  485. }
  486. static int crypt_iv_tcw_init(struct crypt_config *cc)
  487. {
  488. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  489. int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
  490. memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
  491. memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
  492. TCW_WHITENING_SIZE);
  493. return 0;
  494. }
  495. static int crypt_iv_tcw_wipe(struct crypt_config *cc)
  496. {
  497. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  498. memset(tcw->iv_seed, 0, cc->iv_size);
  499. memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
  500. return 0;
  501. }
  502. static int crypt_iv_tcw_whitening(struct crypt_config *cc,
  503. struct dm_crypt_request *dmreq,
  504. u8 *data)
  505. {
  506. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  507. __le64 sector = cpu_to_le64(dmreq->iv_sector);
  508. u8 buf[TCW_WHITENING_SIZE];
  509. SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
  510. int i, r;
  511. /* xor whitening with sector number */
  512. crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
  513. crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
  514. /* calculate crc32 for every 32bit part and xor it */
  515. desc->tfm = tcw->crc32_tfm;
  516. for (i = 0; i < 4; i++) {
  517. r = crypto_shash_init(desc);
  518. if (r)
  519. goto out;
  520. r = crypto_shash_update(desc, &buf[i * 4], 4);
  521. if (r)
  522. goto out;
  523. r = crypto_shash_final(desc, &buf[i * 4]);
  524. if (r)
  525. goto out;
  526. }
  527. crypto_xor(&buf[0], &buf[12], 4);
  528. crypto_xor(&buf[4], &buf[8], 4);
  529. /* apply whitening (8 bytes) to whole sector */
  530. for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
  531. crypto_xor(data + i * 8, buf, 8);
  532. out:
  533. memzero_explicit(buf, sizeof(buf));
  534. return r;
  535. }
  536. static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
  537. struct dm_crypt_request *dmreq)
  538. {
  539. struct scatterlist *sg;
  540. struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
  541. __le64 sector = cpu_to_le64(dmreq->iv_sector);
  542. u8 *src;
  543. int r = 0;
  544. /* Remove whitening from ciphertext */
  545. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
  546. sg = crypt_get_sg_data(cc, dmreq->sg_in);
  547. src = kmap_atomic(sg_page(sg));
  548. r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
  549. kunmap_atomic(src);
  550. }
  551. /* Calculate IV */
  552. crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
  553. if (cc->iv_size > 8)
  554. crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
  555. cc->iv_size - 8);
  556. return r;
  557. }
  558. static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
  559. struct dm_crypt_request *dmreq)
  560. {
  561. struct scatterlist *sg;
  562. u8 *dst;
  563. int r;
  564. if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
  565. return 0;
  566. /* Apply whitening on ciphertext */
  567. sg = crypt_get_sg_data(cc, dmreq->sg_out);
  568. dst = kmap_atomic(sg_page(sg));
  569. r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
  570. kunmap_atomic(dst);
  571. return r;
  572. }
  573. static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
  574. struct dm_crypt_request *dmreq)
  575. {
  576. /* Used only for writes, there must be an additional space to store IV */
  577. get_random_bytes(iv, cc->iv_size);
  578. return 0;
  579. }
  580. static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  581. const char *opts)
  582. {
  583. if (test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags)) {
  584. ti->error = "AEAD transforms not supported for EBOIV";
  585. return -EINVAL;
  586. }
  587. if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) {
  588. ti->error = "Block size of EBOIV cipher does "
  589. "not match IV size of block cipher";
  590. return -EINVAL;
  591. }
  592. return 0;
  593. }
  594. static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv,
  595. struct dm_crypt_request *dmreq)
  596. {
  597. u8 buf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(__le64));
  598. struct skcipher_request *req;
  599. struct scatterlist src, dst;
  600. DECLARE_CRYPTO_WAIT(wait);
  601. int err;
  602. req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO);
  603. if (!req)
  604. return -ENOMEM;
  605. memset(buf, 0, cc->iv_size);
  606. *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
  607. sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size);
  608. sg_init_one(&dst, iv, cc->iv_size);
  609. skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf);
  610. skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
  611. err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  612. skcipher_request_free(req);
  613. return err;
  614. }
  615. static const struct crypt_iv_operations crypt_iv_plain_ops = {
  616. .generator = crypt_iv_plain_gen
  617. };
  618. static const struct crypt_iv_operations crypt_iv_plain64_ops = {
  619. .generator = crypt_iv_plain64_gen
  620. };
  621. static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
  622. .generator = crypt_iv_plain64be_gen
  623. };
  624. static const struct crypt_iv_operations crypt_iv_essiv_ops = {
  625. .generator = crypt_iv_essiv_gen
  626. };
  627. static const struct crypt_iv_operations crypt_iv_benbi_ops = {
  628. .ctr = crypt_iv_benbi_ctr,
  629. .dtr = crypt_iv_benbi_dtr,
  630. .generator = crypt_iv_benbi_gen
  631. };
  632. static const struct crypt_iv_operations crypt_iv_null_ops = {
  633. .generator = crypt_iv_null_gen
  634. };
  635. static const struct crypt_iv_operations crypt_iv_lmk_ops = {
  636. .ctr = crypt_iv_lmk_ctr,
  637. .dtr = crypt_iv_lmk_dtr,
  638. .init = crypt_iv_lmk_init,
  639. .wipe = crypt_iv_lmk_wipe,
  640. .generator = crypt_iv_lmk_gen,
  641. .post = crypt_iv_lmk_post
  642. };
  643. static const struct crypt_iv_operations crypt_iv_tcw_ops = {
  644. .ctr = crypt_iv_tcw_ctr,
  645. .dtr = crypt_iv_tcw_dtr,
  646. .init = crypt_iv_tcw_init,
  647. .wipe = crypt_iv_tcw_wipe,
  648. .generator = crypt_iv_tcw_gen,
  649. .post = crypt_iv_tcw_post
  650. };
  651. static struct crypt_iv_operations crypt_iv_random_ops = {
  652. .generator = crypt_iv_random_gen
  653. };
  654. static struct crypt_iv_operations crypt_iv_eboiv_ops = {
  655. .ctr = crypt_iv_eboiv_ctr,
  656. .generator = crypt_iv_eboiv_gen
  657. };
  658. /*
  659. * Integrity extensions
  660. */
  661. static bool crypt_integrity_aead(struct crypt_config *cc)
  662. {
  663. return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
  664. }
  665. static bool crypt_integrity_hmac(struct crypt_config *cc)
  666. {
  667. return crypt_integrity_aead(cc) && cc->key_mac_size;
  668. }
  669. /* Get sg containing data */
  670. static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
  671. struct scatterlist *sg)
  672. {
  673. if (unlikely(crypt_integrity_aead(cc)))
  674. return &sg[2];
  675. return sg;
  676. }
  677. static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
  678. {
  679. struct bio_integrity_payload *bip;
  680. unsigned int tag_len;
  681. int ret;
  682. if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
  683. return 0;
  684. bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
  685. if (IS_ERR(bip))
  686. return PTR_ERR(bip);
  687. tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift);
  688. bip->bip_iter.bi_size = tag_len;
  689. bip->bip_iter.bi_sector = io->cc->start + io->sector;
  690. ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
  691. tag_len, offset_in_page(io->integrity_metadata));
  692. if (unlikely(ret != tag_len))
  693. return -ENOMEM;
  694. return 0;
  695. }
  696. static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
  697. {
  698. #ifdef CONFIG_BLK_DEV_INTEGRITY
  699. struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
  700. struct mapped_device *md = dm_table_get_md(ti->table);
  701. /* From now we require underlying device with our integrity profile */
  702. if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
  703. ti->error = "Integrity profile not supported.";
  704. return -EINVAL;
  705. }
  706. if (bi->tag_size != cc->on_disk_tag_size ||
  707. bi->tuple_size != cc->on_disk_tag_size) {
  708. ti->error = "Integrity profile tag size mismatch.";
  709. return -EINVAL;
  710. }
  711. if (1 << bi->interval_exp != cc->sector_size) {
  712. ti->error = "Integrity profile sector size mismatch.";
  713. return -EINVAL;
  714. }
  715. if (crypt_integrity_aead(cc)) {
  716. cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
  717. DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md),
  718. cc->integrity_tag_size, cc->integrity_iv_size);
  719. if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
  720. ti->error = "Integrity AEAD auth tag size is not supported.";
  721. return -EINVAL;
  722. }
  723. } else if (cc->integrity_iv_size)
  724. DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md),
  725. cc->integrity_iv_size);
  726. if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
  727. ti->error = "Not enough space for integrity tag in the profile.";
  728. return -EINVAL;
  729. }
  730. return 0;
  731. #else
  732. ti->error = "Integrity profile not supported.";
  733. return -EINVAL;
  734. #endif
  735. }
  736. static void crypt_convert_init(struct crypt_config *cc,
  737. struct convert_context *ctx,
  738. struct bio *bio_out, struct bio *bio_in,
  739. sector_t sector)
  740. {
  741. ctx->bio_in = bio_in;
  742. ctx->bio_out = bio_out;
  743. if (bio_in)
  744. ctx->iter_in = bio_in->bi_iter;
  745. if (bio_out)
  746. ctx->iter_out = bio_out->bi_iter;
  747. ctx->cc_sector = sector + cc->iv_offset;
  748. init_completion(&ctx->restart);
  749. }
  750. static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
  751. void *req)
  752. {
  753. return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
  754. }
  755. static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
  756. {
  757. return (void *)((char *)dmreq - cc->dmreq_start);
  758. }
  759. static u8 *iv_of_dmreq(struct crypt_config *cc,
  760. struct dm_crypt_request *dmreq)
  761. {
  762. if (crypt_integrity_aead(cc))
  763. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  764. crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
  765. else
  766. return (u8 *)ALIGN((unsigned long)(dmreq + 1),
  767. crypto_skcipher_alignmask(any_tfm(cc)) + 1);
  768. }
  769. static u8 *org_iv_of_dmreq(struct crypt_config *cc,
  770. struct dm_crypt_request *dmreq)
  771. {
  772. return iv_of_dmreq(cc, dmreq) + cc->iv_size;
  773. }
  774. static __le64 *org_sector_of_dmreq(struct crypt_config *cc,
  775. struct dm_crypt_request *dmreq)
  776. {
  777. u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
  778. return (__le64 *) ptr;
  779. }
  780. static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
  781. struct dm_crypt_request *dmreq)
  782. {
  783. u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
  784. cc->iv_size + sizeof(uint64_t);
  785. return (unsigned int*)ptr;
  786. }
  787. static void *tag_from_dmreq(struct crypt_config *cc,
  788. struct dm_crypt_request *dmreq)
  789. {
  790. struct convert_context *ctx = dmreq->ctx;
  791. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  792. return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
  793. cc->on_disk_tag_size];
  794. }
  795. static void *iv_tag_from_dmreq(struct crypt_config *cc,
  796. struct dm_crypt_request *dmreq)
  797. {
  798. return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
  799. }
  800. static int crypt_convert_block_aead(struct crypt_config *cc,
  801. struct convert_context *ctx,
  802. struct aead_request *req,
  803. unsigned int tag_offset)
  804. {
  805. struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
  806. struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
  807. struct dm_crypt_request *dmreq;
  808. u8 *iv, *org_iv, *tag_iv, *tag;
  809. __le64 *sector;
  810. int r = 0;
  811. BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
  812. /* Reject unexpected unaligned bio. */
  813. if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
  814. return -EIO;
  815. dmreq = dmreq_of_req(cc, req);
  816. dmreq->iv_sector = ctx->cc_sector;
  817. if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
  818. dmreq->iv_sector >>= cc->sector_shift;
  819. dmreq->ctx = ctx;
  820. *org_tag_of_dmreq(cc, dmreq) = tag_offset;
  821. sector = org_sector_of_dmreq(cc, dmreq);
  822. *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
  823. iv = iv_of_dmreq(cc, dmreq);
  824. org_iv = org_iv_of_dmreq(cc, dmreq);
  825. tag = tag_from_dmreq(cc, dmreq);
  826. tag_iv = iv_tag_from_dmreq(cc, dmreq);
  827. /* AEAD request:
  828. * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
  829. * | (authenticated) | (auth+encryption) | |
  830. * | sector_LE | IV | sector in/out | tag in/out |
  831. */
  832. sg_init_table(dmreq->sg_in, 4);
  833. sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
  834. sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
  835. sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
  836. sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
  837. sg_init_table(dmreq->sg_out, 4);
  838. sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
  839. sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
  840. sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
  841. sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
  842. if (cc->iv_gen_ops) {
  843. /* For READs use IV stored in integrity metadata */
  844. if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
  845. memcpy(org_iv, tag_iv, cc->iv_size);
  846. } else {
  847. r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
  848. if (r < 0)
  849. return r;
  850. /* Store generated IV in integrity metadata */
  851. if (cc->integrity_iv_size)
  852. memcpy(tag_iv, org_iv, cc->iv_size);
  853. }
  854. /* Working copy of IV, to be modified in crypto API */
  855. memcpy(iv, org_iv, cc->iv_size);
  856. }
  857. aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
  858. if (bio_data_dir(ctx->bio_in) == WRITE) {
  859. aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
  860. cc->sector_size, iv);
  861. r = crypto_aead_encrypt(req);
  862. if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
  863. memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
  864. cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
  865. } else {
  866. aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
  867. cc->sector_size + cc->integrity_tag_size, iv);
  868. r = crypto_aead_decrypt(req);
  869. }
  870. if (r == -EBADMSG) {
  871. char b[BDEVNAME_SIZE];
  872. DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
  873. (unsigned long long)le64_to_cpu(*sector));
  874. }
  875. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  876. r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
  877. bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
  878. bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
  879. return r;
  880. }
  881. static int crypt_convert_block_skcipher(struct crypt_config *cc,
  882. struct convert_context *ctx,
  883. struct skcipher_request *req,
  884. unsigned int tag_offset)
  885. {
  886. struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
  887. struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
  888. struct scatterlist *sg_in, *sg_out;
  889. struct dm_crypt_request *dmreq;
  890. u8 *iv, *org_iv, *tag_iv;
  891. __le64 *sector;
  892. int r = 0;
  893. /* Reject unexpected unaligned bio. */
  894. if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
  895. return -EIO;
  896. dmreq = dmreq_of_req(cc, req);
  897. dmreq->iv_sector = ctx->cc_sector;
  898. if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
  899. dmreq->iv_sector >>= cc->sector_shift;
  900. dmreq->ctx = ctx;
  901. *org_tag_of_dmreq(cc, dmreq) = tag_offset;
  902. iv = iv_of_dmreq(cc, dmreq);
  903. org_iv = org_iv_of_dmreq(cc, dmreq);
  904. tag_iv = iv_tag_from_dmreq(cc, dmreq);
  905. sector = org_sector_of_dmreq(cc, dmreq);
  906. *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
  907. /* For skcipher we use only the first sg item */
  908. sg_in = &dmreq->sg_in[0];
  909. sg_out = &dmreq->sg_out[0];
  910. sg_init_table(sg_in, 1);
  911. sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
  912. sg_init_table(sg_out, 1);
  913. sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
  914. if (cc->iv_gen_ops) {
  915. /* For READs use IV stored in integrity metadata */
  916. if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
  917. memcpy(org_iv, tag_iv, cc->integrity_iv_size);
  918. } else {
  919. r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
  920. if (r < 0)
  921. return r;
  922. /* Store generated IV in integrity metadata */
  923. if (cc->integrity_iv_size)
  924. memcpy(tag_iv, org_iv, cc->integrity_iv_size);
  925. }
  926. /* Working copy of IV, to be modified in crypto API */
  927. memcpy(iv, org_iv, cc->iv_size);
  928. }
  929. skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
  930. if (bio_data_dir(ctx->bio_in) == WRITE)
  931. r = crypto_skcipher_encrypt(req);
  932. else
  933. r = crypto_skcipher_decrypt(req);
  934. if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
  935. r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
  936. bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
  937. bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
  938. return r;
  939. }
  940. static void kcryptd_async_done(struct crypto_async_request *async_req,
  941. int error);
  942. static void crypt_alloc_req_skcipher(struct crypt_config *cc,
  943. struct convert_context *ctx)
  944. {
  945. unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
  946. if (!ctx->r.req)
  947. ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
  948. skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
  949. /*
  950. * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
  951. * requests if driver request queue is full.
  952. */
  953. skcipher_request_set_callback(ctx->r.req,
  954. CRYPTO_TFM_REQ_MAY_BACKLOG,
  955. kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
  956. }
  957. static void crypt_alloc_req_aead(struct crypt_config *cc,
  958. struct convert_context *ctx)
  959. {
  960. if (!ctx->r.req_aead)
  961. ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
  962. aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
  963. /*
  964. * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
  965. * requests if driver request queue is full.
  966. */
  967. aead_request_set_callback(ctx->r.req_aead,
  968. CRYPTO_TFM_REQ_MAY_BACKLOG,
  969. kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
  970. }
  971. static void crypt_alloc_req(struct crypt_config *cc,
  972. struct convert_context *ctx)
  973. {
  974. if (crypt_integrity_aead(cc))
  975. crypt_alloc_req_aead(cc, ctx);
  976. else
  977. crypt_alloc_req_skcipher(cc, ctx);
  978. }
  979. static void crypt_free_req_skcipher(struct crypt_config *cc,
  980. struct skcipher_request *req, struct bio *base_bio)
  981. {
  982. struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
  983. if ((struct skcipher_request *)(io + 1) != req)
  984. mempool_free(req, &cc->req_pool);
  985. }
  986. static void crypt_free_req_aead(struct crypt_config *cc,
  987. struct aead_request *req, struct bio *base_bio)
  988. {
  989. struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
  990. if ((struct aead_request *)(io + 1) != req)
  991. mempool_free(req, &cc->req_pool);
  992. }
  993. static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
  994. {
  995. if (crypt_integrity_aead(cc))
  996. crypt_free_req_aead(cc, req, base_bio);
  997. else
  998. crypt_free_req_skcipher(cc, req, base_bio);
  999. }
  1000. /*
  1001. * Encrypt / decrypt data from one bio to another one (can be the same one)
  1002. */
  1003. static blk_status_t crypt_convert(struct crypt_config *cc,
  1004. struct convert_context *ctx)
  1005. {
  1006. unsigned int tag_offset = 0;
  1007. unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
  1008. int r;
  1009. atomic_set(&ctx->cc_pending, 1);
  1010. while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
  1011. crypt_alloc_req(cc, ctx);
  1012. atomic_inc(&ctx->cc_pending);
  1013. if (crypt_integrity_aead(cc))
  1014. r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
  1015. else
  1016. r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
  1017. switch (r) {
  1018. /*
  1019. * The request was queued by a crypto driver
  1020. * but the driver request queue is full, let's wait.
  1021. */
  1022. case -EBUSY:
  1023. wait_for_completion(&ctx->restart);
  1024. reinit_completion(&ctx->restart);
  1025. /* fall through */
  1026. /*
  1027. * The request is queued and processed asynchronously,
  1028. * completion function kcryptd_async_done() will be called.
  1029. */
  1030. case -EINPROGRESS:
  1031. ctx->r.req = NULL;
  1032. ctx->cc_sector += sector_step;
  1033. tag_offset++;
  1034. continue;
  1035. /*
  1036. * The request was already processed (synchronously).
  1037. */
  1038. case 0:
  1039. atomic_dec(&ctx->cc_pending);
  1040. ctx->cc_sector += sector_step;
  1041. tag_offset++;
  1042. cond_resched();
  1043. continue;
  1044. /*
  1045. * There was a data integrity error.
  1046. */
  1047. case -EBADMSG:
  1048. atomic_dec(&ctx->cc_pending);
  1049. return BLK_STS_PROTECTION;
  1050. /*
  1051. * There was an error while processing the request.
  1052. */
  1053. default:
  1054. atomic_dec(&ctx->cc_pending);
  1055. return BLK_STS_IOERR;
  1056. }
  1057. }
  1058. return 0;
  1059. }
  1060. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
  1061. /*
  1062. * Generate a new unfragmented bio with the given size
  1063. * This should never violate the device limitations (but only because
  1064. * max_segment_size is being constrained to PAGE_SIZE).
  1065. *
  1066. * This function may be called concurrently. If we allocate from the mempool
  1067. * concurrently, there is a possibility of deadlock. For example, if we have
  1068. * mempool of 256 pages, two processes, each wanting 256, pages allocate from
  1069. * the mempool concurrently, it may deadlock in a situation where both processes
  1070. * have allocated 128 pages and the mempool is exhausted.
  1071. *
  1072. * In order to avoid this scenario we allocate the pages under a mutex.
  1073. *
  1074. * In order to not degrade performance with excessive locking, we try
  1075. * non-blocking allocations without a mutex first but on failure we fallback
  1076. * to blocking allocations with a mutex.
  1077. */
  1078. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
  1079. {
  1080. struct crypt_config *cc = io->cc;
  1081. struct bio *clone;
  1082. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1083. gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
  1084. unsigned i, len, remaining_size;
  1085. struct page *page;
  1086. retry:
  1087. if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
  1088. mutex_lock(&cc->bio_alloc_lock);
  1089. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
  1090. if (!clone)
  1091. goto out;
  1092. clone_init(io, clone);
  1093. remaining_size = size;
  1094. for (i = 0; i < nr_iovecs; i++) {
  1095. page = mempool_alloc(&cc->page_pool, gfp_mask);
  1096. if (!page) {
  1097. crypt_free_buffer_pages(cc, clone);
  1098. bio_put(clone);
  1099. gfp_mask |= __GFP_DIRECT_RECLAIM;
  1100. goto retry;
  1101. }
  1102. len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
  1103. bio_add_page(clone, page, len, 0);
  1104. remaining_size -= len;
  1105. }
  1106. /* Allocate space for integrity tags */
  1107. if (dm_crypt_integrity_io_alloc(io, clone)) {
  1108. crypt_free_buffer_pages(cc, clone);
  1109. bio_put(clone);
  1110. clone = NULL;
  1111. }
  1112. out:
  1113. if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
  1114. mutex_unlock(&cc->bio_alloc_lock);
  1115. return clone;
  1116. }
  1117. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  1118. {
  1119. struct bio_vec *bv;
  1120. struct bvec_iter_all iter_all;
  1121. bio_for_each_segment_all(bv, clone, iter_all) {
  1122. BUG_ON(!bv->bv_page);
  1123. mempool_free(bv->bv_page, &cc->page_pool);
  1124. }
  1125. }
  1126. static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
  1127. struct bio *bio, sector_t sector)
  1128. {
  1129. io->cc = cc;
  1130. io->base_bio = bio;
  1131. io->sector = sector;
  1132. io->error = 0;
  1133. io->ctx.r.req = NULL;
  1134. io->integrity_metadata = NULL;
  1135. io->integrity_metadata_from_pool = false;
  1136. atomic_set(&io->io_pending, 0);
  1137. }
  1138. static void crypt_inc_pending(struct dm_crypt_io *io)
  1139. {
  1140. atomic_inc(&io->io_pending);
  1141. }
  1142. /*
  1143. * One of the bios was finished. Check for completion of
  1144. * the whole request and correctly clean up the buffer.
  1145. */
  1146. static void crypt_dec_pending(struct dm_crypt_io *io)
  1147. {
  1148. struct crypt_config *cc = io->cc;
  1149. struct bio *base_bio = io->base_bio;
  1150. blk_status_t error = io->error;
  1151. if (!atomic_dec_and_test(&io->io_pending))
  1152. return;
  1153. if (io->ctx.r.req)
  1154. crypt_free_req(cc, io->ctx.r.req, base_bio);
  1155. if (unlikely(io->integrity_metadata_from_pool))
  1156. mempool_free(io->integrity_metadata, &io->cc->tag_pool);
  1157. else
  1158. kfree(io->integrity_metadata);
  1159. base_bio->bi_status = error;
  1160. bio_endio(base_bio);
  1161. }
  1162. /*
  1163. * kcryptd/kcryptd_io:
  1164. *
  1165. * Needed because it would be very unwise to do decryption in an
  1166. * interrupt context.
  1167. *
  1168. * kcryptd performs the actual encryption or decryption.
  1169. *
  1170. * kcryptd_io performs the IO submission.
  1171. *
  1172. * They must be separated as otherwise the final stages could be
  1173. * starved by new requests which can block in the first stages due
  1174. * to memory allocation.
  1175. *
  1176. * The work is done per CPU global for all dm-crypt instances.
  1177. * They should not depend on each other and do not block.
  1178. */
  1179. static void crypt_endio(struct bio *clone)
  1180. {
  1181. struct dm_crypt_io *io = clone->bi_private;
  1182. struct crypt_config *cc = io->cc;
  1183. unsigned rw = bio_data_dir(clone);
  1184. blk_status_t error;
  1185. /*
  1186. * free the processed pages
  1187. */
  1188. if (rw == WRITE)
  1189. crypt_free_buffer_pages(cc, clone);
  1190. error = clone->bi_status;
  1191. bio_put(clone);
  1192. if (rw == READ && !error) {
  1193. kcryptd_queue_crypt(io);
  1194. return;
  1195. }
  1196. if (unlikely(error))
  1197. io->error = error;
  1198. crypt_dec_pending(io);
  1199. }
  1200. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  1201. {
  1202. struct crypt_config *cc = io->cc;
  1203. clone->bi_private = io;
  1204. clone->bi_end_io = crypt_endio;
  1205. bio_set_dev(clone, cc->dev->bdev);
  1206. clone->bi_opf = io->base_bio->bi_opf;
  1207. }
  1208. static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
  1209. {
  1210. struct crypt_config *cc = io->cc;
  1211. struct bio *clone;
  1212. /*
  1213. * We need the original biovec array in order to decrypt
  1214. * the whole bio data *afterwards* -- thanks to immutable
  1215. * biovecs we don't need to worry about the block layer
  1216. * modifying the biovec array; so leverage bio_clone_fast().
  1217. */
  1218. clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
  1219. if (!clone)
  1220. return 1;
  1221. crypt_inc_pending(io);
  1222. clone_init(io, clone);
  1223. clone->bi_iter.bi_sector = cc->start + io->sector;
  1224. if (dm_crypt_integrity_io_alloc(io, clone)) {
  1225. crypt_dec_pending(io);
  1226. bio_put(clone);
  1227. return 1;
  1228. }
  1229. generic_make_request(clone);
  1230. return 0;
  1231. }
  1232. static void kcryptd_io_read_work(struct work_struct *work)
  1233. {
  1234. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  1235. crypt_inc_pending(io);
  1236. if (kcryptd_io_read(io, GFP_NOIO))
  1237. io->error = BLK_STS_RESOURCE;
  1238. crypt_dec_pending(io);
  1239. }
  1240. static void kcryptd_queue_read(struct dm_crypt_io *io)
  1241. {
  1242. struct crypt_config *cc = io->cc;
  1243. INIT_WORK(&io->work, kcryptd_io_read_work);
  1244. queue_work(cc->io_queue, &io->work);
  1245. }
  1246. static void kcryptd_io_write(struct dm_crypt_io *io)
  1247. {
  1248. struct bio *clone = io->ctx.bio_out;
  1249. generic_make_request(clone);
  1250. }
  1251. #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
  1252. static int dmcrypt_write(void *data)
  1253. {
  1254. struct crypt_config *cc = data;
  1255. struct dm_crypt_io *io;
  1256. while (1) {
  1257. struct rb_root write_tree;
  1258. struct blk_plug plug;
  1259. spin_lock_irq(&cc->write_thread_lock);
  1260. continue_locked:
  1261. if (!RB_EMPTY_ROOT(&cc->write_tree))
  1262. goto pop_from_list;
  1263. set_current_state(TASK_INTERRUPTIBLE);
  1264. spin_unlock_irq(&cc->write_thread_lock);
  1265. if (unlikely(kthread_should_stop())) {
  1266. set_current_state(TASK_RUNNING);
  1267. break;
  1268. }
  1269. schedule();
  1270. set_current_state(TASK_RUNNING);
  1271. spin_lock_irq(&cc->write_thread_lock);
  1272. goto continue_locked;
  1273. pop_from_list:
  1274. write_tree = cc->write_tree;
  1275. cc->write_tree = RB_ROOT;
  1276. spin_unlock_irq(&cc->write_thread_lock);
  1277. BUG_ON(rb_parent(write_tree.rb_node));
  1278. /*
  1279. * Note: we cannot walk the tree here with rb_next because
  1280. * the structures may be freed when kcryptd_io_write is called.
  1281. */
  1282. blk_start_plug(&plug);
  1283. do {
  1284. io = crypt_io_from_node(rb_first(&write_tree));
  1285. rb_erase(&io->rb_node, &write_tree);
  1286. kcryptd_io_write(io);
  1287. } while (!RB_EMPTY_ROOT(&write_tree));
  1288. blk_finish_plug(&plug);
  1289. }
  1290. return 0;
  1291. }
  1292. static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
  1293. {
  1294. struct bio *clone = io->ctx.bio_out;
  1295. struct crypt_config *cc = io->cc;
  1296. unsigned long flags;
  1297. sector_t sector;
  1298. struct rb_node **rbp, *parent;
  1299. if (unlikely(io->error)) {
  1300. crypt_free_buffer_pages(cc, clone);
  1301. bio_put(clone);
  1302. crypt_dec_pending(io);
  1303. return;
  1304. }
  1305. /* crypt_convert should have filled the clone bio */
  1306. BUG_ON(io->ctx.iter_out.bi_size);
  1307. clone->bi_iter.bi_sector = cc->start + io->sector;
  1308. if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
  1309. generic_make_request(clone);
  1310. return;
  1311. }
  1312. spin_lock_irqsave(&cc->write_thread_lock, flags);
  1313. if (RB_EMPTY_ROOT(&cc->write_tree))
  1314. wake_up_process(cc->write_thread);
  1315. rbp = &cc->write_tree.rb_node;
  1316. parent = NULL;
  1317. sector = io->sector;
  1318. while (*rbp) {
  1319. parent = *rbp;
  1320. if (sector < crypt_io_from_node(parent)->sector)
  1321. rbp = &(*rbp)->rb_left;
  1322. else
  1323. rbp = &(*rbp)->rb_right;
  1324. }
  1325. rb_link_node(&io->rb_node, parent, rbp);
  1326. rb_insert_color(&io->rb_node, &cc->write_tree);
  1327. spin_unlock_irqrestore(&cc->write_thread_lock, flags);
  1328. }
  1329. static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
  1330. {
  1331. struct crypt_config *cc = io->cc;
  1332. struct bio *clone;
  1333. int crypt_finished;
  1334. sector_t sector = io->sector;
  1335. blk_status_t r;
  1336. /*
  1337. * Prevent io from disappearing until this function completes.
  1338. */
  1339. crypt_inc_pending(io);
  1340. crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
  1341. clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
  1342. if (unlikely(!clone)) {
  1343. io->error = BLK_STS_IOERR;
  1344. goto dec;
  1345. }
  1346. io->ctx.bio_out = clone;
  1347. io->ctx.iter_out = clone->bi_iter;
  1348. sector += bio_sectors(clone);
  1349. crypt_inc_pending(io);
  1350. r = crypt_convert(cc, &io->ctx);
  1351. if (r)
  1352. io->error = r;
  1353. crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
  1354. /* Encryption was already finished, submit io now */
  1355. if (crypt_finished) {
  1356. kcryptd_crypt_write_io_submit(io, 0);
  1357. io->sector = sector;
  1358. }
  1359. dec:
  1360. crypt_dec_pending(io);
  1361. }
  1362. static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
  1363. {
  1364. crypt_dec_pending(io);
  1365. }
  1366. static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
  1367. {
  1368. struct crypt_config *cc = io->cc;
  1369. blk_status_t r;
  1370. crypt_inc_pending(io);
  1371. crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
  1372. io->sector);
  1373. r = crypt_convert(cc, &io->ctx);
  1374. if (r)
  1375. io->error = r;
  1376. if (atomic_dec_and_test(&io->ctx.cc_pending))
  1377. kcryptd_crypt_read_done(io);
  1378. crypt_dec_pending(io);
  1379. }
  1380. static void kcryptd_async_done(struct crypto_async_request *async_req,
  1381. int error)
  1382. {
  1383. struct dm_crypt_request *dmreq = async_req->data;
  1384. struct convert_context *ctx = dmreq->ctx;
  1385. struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
  1386. struct crypt_config *cc = io->cc;
  1387. /*
  1388. * A request from crypto driver backlog is going to be processed now,
  1389. * finish the completion and continue in crypt_convert().
  1390. * (Callback will be called for the second time for this request.)
  1391. */
  1392. if (error == -EINPROGRESS) {
  1393. complete(&ctx->restart);
  1394. return;
  1395. }
  1396. if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
  1397. error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
  1398. if (error == -EBADMSG) {
  1399. char b[BDEVNAME_SIZE];
  1400. DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
  1401. (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
  1402. io->error = BLK_STS_PROTECTION;
  1403. } else if (error < 0)
  1404. io->error = BLK_STS_IOERR;
  1405. crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
  1406. if (!atomic_dec_and_test(&ctx->cc_pending))
  1407. return;
  1408. if (bio_data_dir(io->base_bio) == READ)
  1409. kcryptd_crypt_read_done(io);
  1410. else
  1411. kcryptd_crypt_write_io_submit(io, 1);
  1412. }
  1413. static void kcryptd_crypt(struct work_struct *work)
  1414. {
  1415. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  1416. if (bio_data_dir(io->base_bio) == READ)
  1417. kcryptd_crypt_read_convert(io);
  1418. else
  1419. kcryptd_crypt_write_convert(io);
  1420. }
  1421. static void kcryptd_queue_crypt(struct dm_crypt_io *io)
  1422. {
  1423. struct crypt_config *cc = io->cc;
  1424. INIT_WORK(&io->work, kcryptd_crypt);
  1425. queue_work(cc->crypt_queue, &io->work);
  1426. }
  1427. static void crypt_free_tfms_aead(struct crypt_config *cc)
  1428. {
  1429. if (!cc->cipher_tfm.tfms_aead)
  1430. return;
  1431. if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
  1432. crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
  1433. cc->cipher_tfm.tfms_aead[0] = NULL;
  1434. }
  1435. kfree(cc->cipher_tfm.tfms_aead);
  1436. cc->cipher_tfm.tfms_aead = NULL;
  1437. }
  1438. static void crypt_free_tfms_skcipher(struct crypt_config *cc)
  1439. {
  1440. unsigned i;
  1441. if (!cc->cipher_tfm.tfms)
  1442. return;
  1443. for (i = 0; i < cc->tfms_count; i++)
  1444. if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
  1445. crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
  1446. cc->cipher_tfm.tfms[i] = NULL;
  1447. }
  1448. kfree(cc->cipher_tfm.tfms);
  1449. cc->cipher_tfm.tfms = NULL;
  1450. }
  1451. static void crypt_free_tfms(struct crypt_config *cc)
  1452. {
  1453. if (crypt_integrity_aead(cc))
  1454. crypt_free_tfms_aead(cc);
  1455. else
  1456. crypt_free_tfms_skcipher(cc);
  1457. }
  1458. static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
  1459. {
  1460. unsigned i;
  1461. int err;
  1462. cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
  1463. sizeof(struct crypto_skcipher *),
  1464. GFP_KERNEL);
  1465. if (!cc->cipher_tfm.tfms)
  1466. return -ENOMEM;
  1467. for (i = 0; i < cc->tfms_count; i++) {
  1468. cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
  1469. if (IS_ERR(cc->cipher_tfm.tfms[i])) {
  1470. err = PTR_ERR(cc->cipher_tfm.tfms[i]);
  1471. crypt_free_tfms(cc);
  1472. return err;
  1473. }
  1474. }
  1475. /*
  1476. * dm-crypt performance can vary greatly depending on which crypto
  1477. * algorithm implementation is used. Help people debug performance
  1478. * problems by logging the ->cra_driver_name.
  1479. */
  1480. DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
  1481. crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
  1482. return 0;
  1483. }
  1484. static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
  1485. {
  1486. int err;
  1487. cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
  1488. if (!cc->cipher_tfm.tfms)
  1489. return -ENOMEM;
  1490. cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
  1491. if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
  1492. err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
  1493. crypt_free_tfms(cc);
  1494. return err;
  1495. }
  1496. DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
  1497. crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name);
  1498. return 0;
  1499. }
  1500. static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
  1501. {
  1502. if (crypt_integrity_aead(cc))
  1503. return crypt_alloc_tfms_aead(cc, ciphermode);
  1504. else
  1505. return crypt_alloc_tfms_skcipher(cc, ciphermode);
  1506. }
  1507. static unsigned crypt_subkey_size(struct crypt_config *cc)
  1508. {
  1509. return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
  1510. }
  1511. static unsigned crypt_authenckey_size(struct crypt_config *cc)
  1512. {
  1513. return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
  1514. }
  1515. /*
  1516. * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
  1517. * the key must be for some reason in special format.
  1518. * This funcion converts cc->key to this special format.
  1519. */
  1520. static void crypt_copy_authenckey(char *p, const void *key,
  1521. unsigned enckeylen, unsigned authkeylen)
  1522. {
  1523. struct crypto_authenc_key_param *param;
  1524. struct rtattr *rta;
  1525. rta = (struct rtattr *)p;
  1526. param = RTA_DATA(rta);
  1527. param->enckeylen = cpu_to_be32(enckeylen);
  1528. rta->rta_len = RTA_LENGTH(sizeof(*param));
  1529. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  1530. p += RTA_SPACE(sizeof(*param));
  1531. memcpy(p, key + enckeylen, authkeylen);
  1532. p += authkeylen;
  1533. memcpy(p, key, enckeylen);
  1534. }
  1535. static int crypt_setkey(struct crypt_config *cc)
  1536. {
  1537. unsigned subkey_size;
  1538. int err = 0, i, r;
  1539. /* Ignore extra keys (which are used for IV etc) */
  1540. subkey_size = crypt_subkey_size(cc);
  1541. if (crypt_integrity_hmac(cc)) {
  1542. if (subkey_size < cc->key_mac_size)
  1543. return -EINVAL;
  1544. crypt_copy_authenckey(cc->authenc_key, cc->key,
  1545. subkey_size - cc->key_mac_size,
  1546. cc->key_mac_size);
  1547. }
  1548. for (i = 0; i < cc->tfms_count; i++) {
  1549. if (crypt_integrity_hmac(cc))
  1550. r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
  1551. cc->authenc_key, crypt_authenckey_size(cc));
  1552. else if (crypt_integrity_aead(cc))
  1553. r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
  1554. cc->key + (i * subkey_size),
  1555. subkey_size);
  1556. else
  1557. r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
  1558. cc->key + (i * subkey_size),
  1559. subkey_size);
  1560. if (r)
  1561. err = r;
  1562. }
  1563. if (crypt_integrity_hmac(cc))
  1564. memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
  1565. return err;
  1566. }
  1567. #ifdef CONFIG_KEYS
  1568. static bool contains_whitespace(const char *str)
  1569. {
  1570. while (*str)
  1571. if (isspace(*str++))
  1572. return true;
  1573. return false;
  1574. }
  1575. static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
  1576. {
  1577. char *new_key_string, *key_desc;
  1578. int ret;
  1579. struct key *key;
  1580. const struct user_key_payload *ukp;
  1581. /*
  1582. * Reject key_string with whitespace. dm core currently lacks code for
  1583. * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
  1584. */
  1585. if (contains_whitespace(key_string)) {
  1586. DMERR("whitespace chars not allowed in key string");
  1587. return -EINVAL;
  1588. }
  1589. /* look for next ':' separating key_type from key_description */
  1590. key_desc = strpbrk(key_string, ":");
  1591. if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
  1592. return -EINVAL;
  1593. if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
  1594. strncmp(key_string, "user:", key_desc - key_string + 1))
  1595. return -EINVAL;
  1596. new_key_string = kstrdup(key_string, GFP_KERNEL);
  1597. if (!new_key_string)
  1598. return -ENOMEM;
  1599. key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
  1600. key_desc + 1, NULL);
  1601. if (IS_ERR(key)) {
  1602. kzfree(new_key_string);
  1603. return PTR_ERR(key);
  1604. }
  1605. down_read(&key->sem);
  1606. ukp = user_key_payload_locked(key);
  1607. if (!ukp) {
  1608. up_read(&key->sem);
  1609. key_put(key);
  1610. kzfree(new_key_string);
  1611. return -EKEYREVOKED;
  1612. }
  1613. if (cc->key_size != ukp->datalen) {
  1614. up_read(&key->sem);
  1615. key_put(key);
  1616. kzfree(new_key_string);
  1617. return -EINVAL;
  1618. }
  1619. memcpy(cc->key, ukp->data, cc->key_size);
  1620. up_read(&key->sem);
  1621. key_put(key);
  1622. /* clear the flag since following operations may invalidate previously valid key */
  1623. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1624. ret = crypt_setkey(cc);
  1625. if (!ret) {
  1626. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1627. kzfree(cc->key_string);
  1628. cc->key_string = new_key_string;
  1629. } else
  1630. kzfree(new_key_string);
  1631. return ret;
  1632. }
  1633. static int get_key_size(char **key_string)
  1634. {
  1635. char *colon, dummy;
  1636. int ret;
  1637. if (*key_string[0] != ':')
  1638. return strlen(*key_string) >> 1;
  1639. /* look for next ':' in key string */
  1640. colon = strpbrk(*key_string + 1, ":");
  1641. if (!colon)
  1642. return -EINVAL;
  1643. if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
  1644. return -EINVAL;
  1645. *key_string = colon;
  1646. /* remaining key string should be :<logon|user>:<key_desc> */
  1647. return ret;
  1648. }
  1649. #else
  1650. static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
  1651. {
  1652. return -EINVAL;
  1653. }
  1654. static int get_key_size(char **key_string)
  1655. {
  1656. return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
  1657. }
  1658. #endif
  1659. static int crypt_set_key(struct crypt_config *cc, char *key)
  1660. {
  1661. int r = -EINVAL;
  1662. int key_string_len = strlen(key);
  1663. /* Hyphen (which gives a key_size of zero) means there is no key. */
  1664. if (!cc->key_size && strcmp(key, "-"))
  1665. goto out;
  1666. /* ':' means the key is in kernel keyring, short-circuit normal key processing */
  1667. if (key[0] == ':') {
  1668. r = crypt_set_keyring_key(cc, key + 1);
  1669. goto out;
  1670. }
  1671. /* clear the flag since following operations may invalidate previously valid key */
  1672. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1673. /* wipe references to any kernel keyring key */
  1674. kzfree(cc->key_string);
  1675. cc->key_string = NULL;
  1676. /* Decode key from its hex representation. */
  1677. if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
  1678. goto out;
  1679. r = crypt_setkey(cc);
  1680. if (!r)
  1681. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1682. out:
  1683. /* Hex key string not needed after here, so wipe it. */
  1684. memset(key, '0', key_string_len);
  1685. return r;
  1686. }
  1687. static int crypt_wipe_key(struct crypt_config *cc)
  1688. {
  1689. int r;
  1690. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  1691. get_random_bytes(&cc->key, cc->key_size);
  1692. /* Wipe IV private keys */
  1693. if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
  1694. r = cc->iv_gen_ops->wipe(cc);
  1695. if (r)
  1696. return r;
  1697. }
  1698. kzfree(cc->key_string);
  1699. cc->key_string = NULL;
  1700. r = crypt_setkey(cc);
  1701. memset(&cc->key, 0, cc->key_size * sizeof(u8));
  1702. return r;
  1703. }
  1704. static void crypt_calculate_pages_per_client(void)
  1705. {
  1706. unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;
  1707. if (!dm_crypt_clients_n)
  1708. return;
  1709. pages /= dm_crypt_clients_n;
  1710. if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
  1711. pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
  1712. dm_crypt_pages_per_client = pages;
  1713. }
  1714. static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
  1715. {
  1716. struct crypt_config *cc = pool_data;
  1717. struct page *page;
  1718. /*
  1719. * Note, percpu_counter_read_positive() may over (and under) estimate
  1720. * the current usage by at most (batch - 1) * num_online_cpus() pages,
  1721. * but avoids potential spinlock contention of an exact result.
  1722. */
  1723. if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) &&
  1724. likely(gfp_mask & __GFP_NORETRY))
  1725. return NULL;
  1726. page = alloc_page(gfp_mask);
  1727. if (likely(page != NULL))
  1728. percpu_counter_add(&cc->n_allocated_pages, 1);
  1729. return page;
  1730. }
  1731. static void crypt_page_free(void *page, void *pool_data)
  1732. {
  1733. struct crypt_config *cc = pool_data;
  1734. __free_page(page);
  1735. percpu_counter_sub(&cc->n_allocated_pages, 1);
  1736. }
  1737. static void crypt_dtr(struct dm_target *ti)
  1738. {
  1739. struct crypt_config *cc = ti->private;
  1740. ti->private = NULL;
  1741. if (!cc)
  1742. return;
  1743. if (cc->write_thread)
  1744. kthread_stop(cc->write_thread);
  1745. if (cc->io_queue)
  1746. destroy_workqueue(cc->io_queue);
  1747. if (cc->crypt_queue)
  1748. destroy_workqueue(cc->crypt_queue);
  1749. crypt_free_tfms(cc);
  1750. bioset_exit(&cc->bs);
  1751. mempool_exit(&cc->page_pool);
  1752. mempool_exit(&cc->req_pool);
  1753. mempool_exit(&cc->tag_pool);
  1754. WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
  1755. percpu_counter_destroy(&cc->n_allocated_pages);
  1756. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  1757. cc->iv_gen_ops->dtr(cc);
  1758. if (cc->dev)
  1759. dm_put_device(ti, cc->dev);
  1760. kzfree(cc->cipher_string);
  1761. kzfree(cc->key_string);
  1762. kzfree(cc->cipher_auth);
  1763. kzfree(cc->authenc_key);
  1764. mutex_destroy(&cc->bio_alloc_lock);
  1765. /* Must zero key material before freeing */
  1766. kzfree(cc);
  1767. spin_lock(&dm_crypt_clients_lock);
  1768. WARN_ON(!dm_crypt_clients_n);
  1769. dm_crypt_clients_n--;
  1770. crypt_calculate_pages_per_client();
  1771. spin_unlock(&dm_crypt_clients_lock);
  1772. }
  1773. static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
  1774. {
  1775. struct crypt_config *cc = ti->private;
  1776. if (crypt_integrity_aead(cc))
  1777. cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
  1778. else
  1779. cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
  1780. if (cc->iv_size)
  1781. /* at least a 64 bit sector number should fit in our buffer */
  1782. cc->iv_size = max(cc->iv_size,
  1783. (unsigned int)(sizeof(u64) / sizeof(u8)));
  1784. else if (ivmode) {
  1785. DMWARN("Selected cipher does not support IVs");
  1786. ivmode = NULL;
  1787. }
  1788. /* Choose ivmode, see comments at iv code. */
  1789. if (ivmode == NULL)
  1790. cc->iv_gen_ops = NULL;
  1791. else if (strcmp(ivmode, "plain") == 0)
  1792. cc->iv_gen_ops = &crypt_iv_plain_ops;
  1793. else if (strcmp(ivmode, "plain64") == 0)
  1794. cc->iv_gen_ops = &crypt_iv_plain64_ops;
  1795. else if (strcmp(ivmode, "plain64be") == 0)
  1796. cc->iv_gen_ops = &crypt_iv_plain64be_ops;
  1797. else if (strcmp(ivmode, "essiv") == 0)
  1798. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  1799. else if (strcmp(ivmode, "benbi") == 0)
  1800. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  1801. else if (strcmp(ivmode, "null") == 0)
  1802. cc->iv_gen_ops = &crypt_iv_null_ops;
  1803. else if (strcmp(ivmode, "eboiv") == 0)
  1804. cc->iv_gen_ops = &crypt_iv_eboiv_ops;
  1805. else if (strcmp(ivmode, "lmk") == 0) {
  1806. cc->iv_gen_ops = &crypt_iv_lmk_ops;
  1807. /*
  1808. * Version 2 and 3 is recognised according
  1809. * to length of provided multi-key string.
  1810. * If present (version 3), last key is used as IV seed.
  1811. * All keys (including IV seed) are always the same size.
  1812. */
  1813. if (cc->key_size % cc->key_parts) {
  1814. cc->key_parts++;
  1815. cc->key_extra_size = cc->key_size / cc->key_parts;
  1816. }
  1817. } else if (strcmp(ivmode, "tcw") == 0) {
  1818. cc->iv_gen_ops = &crypt_iv_tcw_ops;
  1819. cc->key_parts += 2; /* IV + whitening */
  1820. cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
  1821. } else if (strcmp(ivmode, "random") == 0) {
  1822. cc->iv_gen_ops = &crypt_iv_random_ops;
  1823. /* Need storage space in integrity fields. */
  1824. cc->integrity_iv_size = cc->iv_size;
  1825. } else {
  1826. ti->error = "Invalid IV mode";
  1827. return -EINVAL;
  1828. }
  1829. return 0;
  1830. }
  1831. /*
  1832. * Workaround to parse HMAC algorithm from AEAD crypto API spec.
  1833. * The HMAC is needed to calculate tag size (HMAC digest size).
  1834. * This should be probably done by crypto-api calls (once available...)
  1835. */
  1836. static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
  1837. {
  1838. char *start, *end, *mac_alg = NULL;
  1839. struct crypto_ahash *mac;
  1840. if (!strstarts(cipher_api, "authenc("))
  1841. return 0;
  1842. start = strchr(cipher_api, '(');
  1843. end = strchr(cipher_api, ',');
  1844. if (!start || !end || ++start > end)
  1845. return -EINVAL;
  1846. mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
  1847. if (!mac_alg)
  1848. return -ENOMEM;
  1849. strncpy(mac_alg, start, end - start);
  1850. mac = crypto_alloc_ahash(mac_alg, 0, 0);
  1851. kfree(mac_alg);
  1852. if (IS_ERR(mac))
  1853. return PTR_ERR(mac);
  1854. cc->key_mac_size = crypto_ahash_digestsize(mac);
  1855. crypto_free_ahash(mac);
  1856. cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
  1857. if (!cc->authenc_key)
  1858. return -ENOMEM;
  1859. return 0;
  1860. }
  1861. static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
  1862. char **ivmode, char **ivopts)
  1863. {
  1864. struct crypt_config *cc = ti->private;
  1865. char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME];
  1866. int ret = -EINVAL;
  1867. cc->tfms_count = 1;
  1868. /*
  1869. * New format (capi: prefix)
  1870. * capi:cipher_api_spec-iv:ivopts
  1871. */
  1872. tmp = &cipher_in[strlen("capi:")];
  1873. /* Separate IV options if present, it can contain another '-' in hash name */
  1874. *ivopts = strrchr(tmp, ':');
  1875. if (*ivopts) {
  1876. **ivopts = '\0';
  1877. (*ivopts)++;
  1878. }
  1879. /* Parse IV mode */
  1880. *ivmode = strrchr(tmp, '-');
  1881. if (*ivmode) {
  1882. **ivmode = '\0';
  1883. (*ivmode)++;
  1884. }
  1885. /* The rest is crypto API spec */
  1886. cipher_api = tmp;
  1887. /* Alloc AEAD, can be used only in new format. */
  1888. if (crypt_integrity_aead(cc)) {
  1889. ret = crypt_ctr_auth_cipher(cc, cipher_api);
  1890. if (ret < 0) {
  1891. ti->error = "Invalid AEAD cipher spec";
  1892. return -ENOMEM;
  1893. }
  1894. }
  1895. if (*ivmode && !strcmp(*ivmode, "lmk"))
  1896. cc->tfms_count = 64;
  1897. if (*ivmode && !strcmp(*ivmode, "essiv")) {
  1898. if (!*ivopts) {
  1899. ti->error = "Digest algorithm missing for ESSIV mode";
  1900. return -EINVAL;
  1901. }
  1902. ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s)",
  1903. cipher_api, *ivopts);
  1904. if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
  1905. ti->error = "Cannot allocate cipher string";
  1906. return -ENOMEM;
  1907. }
  1908. cipher_api = buf;
  1909. }
  1910. cc->key_parts = cc->tfms_count;
  1911. /* Allocate cipher */
  1912. ret = crypt_alloc_tfms(cc, cipher_api);
  1913. if (ret < 0) {
  1914. ti->error = "Error allocating crypto tfm";
  1915. return ret;
  1916. }
  1917. if (crypt_integrity_aead(cc))
  1918. cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
  1919. else
  1920. cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
  1921. return 0;
  1922. }
  1923. static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
  1924. char **ivmode, char **ivopts)
  1925. {
  1926. struct crypt_config *cc = ti->private;
  1927. char *tmp, *cipher, *chainmode, *keycount;
  1928. char *cipher_api = NULL;
  1929. int ret = -EINVAL;
  1930. char dummy;
  1931. if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
  1932. ti->error = "Bad cipher specification";
  1933. return -EINVAL;
  1934. }
  1935. /*
  1936. * Legacy dm-crypt cipher specification
  1937. * cipher[:keycount]-mode-iv:ivopts
  1938. */
  1939. tmp = cipher_in;
  1940. keycount = strsep(&tmp, "-");
  1941. cipher = strsep(&keycount, ":");
  1942. if (!keycount)
  1943. cc->tfms_count = 1;
  1944. else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
  1945. !is_power_of_2(cc->tfms_count)) {
  1946. ti->error = "Bad cipher key count specification";
  1947. return -EINVAL;
  1948. }
  1949. cc->key_parts = cc->tfms_count;
  1950. chainmode = strsep(&tmp, "-");
  1951. *ivmode = strsep(&tmp, ":");
  1952. *ivopts = tmp;
  1953. /*
  1954. * For compatibility with the original dm-crypt mapping format, if
  1955. * only the cipher name is supplied, use cbc-plain.
  1956. */
  1957. if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
  1958. chainmode = "cbc";
  1959. *ivmode = "plain";
  1960. }
  1961. if (strcmp(chainmode, "ecb") && !*ivmode) {
  1962. ti->error = "IV mechanism required";
  1963. return -EINVAL;
  1964. }
  1965. cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
  1966. if (!cipher_api)
  1967. goto bad_mem;
  1968. if (*ivmode && !strcmp(*ivmode, "essiv")) {
  1969. if (!*ivopts) {
  1970. ti->error = "Digest algorithm missing for ESSIV mode";
  1971. kfree(cipher_api);
  1972. return -EINVAL;
  1973. }
  1974. ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
  1975. "essiv(%s(%s),%s)", chainmode, cipher, *ivopts);
  1976. } else {
  1977. ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
  1978. "%s(%s)", chainmode, cipher);
  1979. }
  1980. if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
  1981. kfree(cipher_api);
  1982. goto bad_mem;
  1983. }
  1984. /* Allocate cipher */
  1985. ret = crypt_alloc_tfms(cc, cipher_api);
  1986. if (ret < 0) {
  1987. ti->error = "Error allocating crypto tfm";
  1988. kfree(cipher_api);
  1989. return ret;
  1990. }
  1991. kfree(cipher_api);
  1992. return 0;
  1993. bad_mem:
  1994. ti->error = "Cannot allocate cipher strings";
  1995. return -ENOMEM;
  1996. }
  1997. static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
  1998. {
  1999. struct crypt_config *cc = ti->private;
  2000. char *ivmode = NULL, *ivopts = NULL;
  2001. int ret;
  2002. cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
  2003. if (!cc->cipher_string) {
  2004. ti->error = "Cannot allocate cipher strings";
  2005. return -ENOMEM;
  2006. }
  2007. if (strstarts(cipher_in, "capi:"))
  2008. ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
  2009. else
  2010. ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
  2011. if (ret)
  2012. return ret;
  2013. /* Initialize IV */
  2014. ret = crypt_ctr_ivmode(ti, ivmode);
  2015. if (ret < 0)
  2016. return ret;
  2017. /* Initialize and set key */
  2018. ret = crypt_set_key(cc, key);
  2019. if (ret < 0) {
  2020. ti->error = "Error decoding and setting key";
  2021. return ret;
  2022. }
  2023. /* Allocate IV */
  2024. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
  2025. ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
  2026. if (ret < 0) {
  2027. ti->error = "Error creating IV";
  2028. return ret;
  2029. }
  2030. }
  2031. /* Initialize IV (set keys for ESSIV etc) */
  2032. if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
  2033. ret = cc->iv_gen_ops->init(cc);
  2034. if (ret < 0) {
  2035. ti->error = "Error initialising IV";
  2036. return ret;
  2037. }
  2038. }
  2039. /* wipe the kernel key payload copy */
  2040. if (cc->key_string)
  2041. memset(cc->key, 0, cc->key_size * sizeof(u8));
  2042. return ret;
  2043. }
  2044. static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
  2045. {
  2046. struct crypt_config *cc = ti->private;
  2047. struct dm_arg_set as;
  2048. static const struct dm_arg _args[] = {
  2049. {0, 6, "Invalid number of feature args"},
  2050. };
  2051. unsigned int opt_params, val;
  2052. const char *opt_string, *sval;
  2053. char dummy;
  2054. int ret;
  2055. /* Optional parameters */
  2056. as.argc = argc;
  2057. as.argv = argv;
  2058. ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
  2059. if (ret)
  2060. return ret;
  2061. while (opt_params--) {
  2062. opt_string = dm_shift_arg(&as);
  2063. if (!opt_string) {
  2064. ti->error = "Not enough feature arguments";
  2065. return -EINVAL;
  2066. }
  2067. if (!strcasecmp(opt_string, "allow_discards"))
  2068. ti->num_discard_bios = 1;
  2069. else if (!strcasecmp(opt_string, "same_cpu_crypt"))
  2070. set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
  2071. else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
  2072. set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
  2073. else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
  2074. if (val == 0 || val > MAX_TAG_SIZE) {
  2075. ti->error = "Invalid integrity arguments";
  2076. return -EINVAL;
  2077. }
  2078. cc->on_disk_tag_size = val;
  2079. sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
  2080. if (!strcasecmp(sval, "aead")) {
  2081. set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
  2082. } else if (strcasecmp(sval, "none")) {
  2083. ti->error = "Unknown integrity profile";
  2084. return -EINVAL;
  2085. }
  2086. cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
  2087. if (!cc->cipher_auth)
  2088. return -ENOMEM;
  2089. } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
  2090. if (cc->sector_size < (1 << SECTOR_SHIFT) ||
  2091. cc->sector_size > 4096 ||
  2092. (cc->sector_size & (cc->sector_size - 1))) {
  2093. ti->error = "Invalid feature value for sector_size";
  2094. return -EINVAL;
  2095. }
  2096. if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
  2097. ti->error = "Device size is not multiple of sector_size feature";
  2098. return -EINVAL;
  2099. }
  2100. cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
  2101. } else if (!strcasecmp(opt_string, "iv_large_sectors"))
  2102. set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
  2103. else {
  2104. ti->error = "Invalid feature arguments";
  2105. return -EINVAL;
  2106. }
  2107. }
  2108. return 0;
  2109. }
  2110. /*
  2111. * Construct an encryption mapping:
  2112. * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
  2113. */
  2114. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  2115. {
  2116. struct crypt_config *cc;
  2117. const char *devname = dm_table_device_name(ti->table);
  2118. int key_size;
  2119. unsigned int align_mask;
  2120. unsigned long long tmpll;
  2121. int ret;
  2122. size_t iv_size_padding, additional_req_size;
  2123. char dummy;
  2124. if (argc < 5) {
  2125. ti->error = "Not enough arguments";
  2126. return -EINVAL;
  2127. }
  2128. key_size = get_key_size(&argv[1]);
  2129. if (key_size < 0) {
  2130. ti->error = "Cannot parse key size";
  2131. return -EINVAL;
  2132. }
  2133. cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL);
  2134. if (!cc) {
  2135. ti->error = "Cannot allocate encryption context";
  2136. return -ENOMEM;
  2137. }
  2138. cc->key_size = key_size;
  2139. cc->sector_size = (1 << SECTOR_SHIFT);
  2140. cc->sector_shift = 0;
  2141. ti->private = cc;
  2142. spin_lock(&dm_crypt_clients_lock);
  2143. dm_crypt_clients_n++;
  2144. crypt_calculate_pages_per_client();
  2145. spin_unlock(&dm_crypt_clients_lock);
  2146. ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
  2147. if (ret < 0)
  2148. goto bad;
  2149. /* Optional parameters need to be read before cipher constructor */
  2150. if (argc > 5) {
  2151. ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
  2152. if (ret)
  2153. goto bad;
  2154. }
  2155. ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
  2156. if (ret < 0)
  2157. goto bad;
  2158. if (crypt_integrity_aead(cc)) {
  2159. cc->dmreq_start = sizeof(struct aead_request);
  2160. cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
  2161. align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
  2162. } else {
  2163. cc->dmreq_start = sizeof(struct skcipher_request);
  2164. cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
  2165. align_mask = crypto_skcipher_alignmask(any_tfm(cc));
  2166. }
  2167. cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
  2168. if (align_mask < CRYPTO_MINALIGN) {
  2169. /* Allocate the padding exactly */
  2170. iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
  2171. & align_mask;
  2172. } else {
  2173. /*
  2174. * If the cipher requires greater alignment than kmalloc
  2175. * alignment, we don't know the exact position of the
  2176. * initialization vector. We must assume worst case.
  2177. */
  2178. iv_size_padding = align_mask;
  2179. }
  2180. /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
  2181. additional_req_size = sizeof(struct dm_crypt_request) +
  2182. iv_size_padding + cc->iv_size +
  2183. cc->iv_size +
  2184. sizeof(uint64_t) +
  2185. sizeof(unsigned int);
  2186. ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
  2187. if (ret) {
  2188. ti->error = "Cannot allocate crypt request mempool";
  2189. goto bad;
  2190. }
  2191. cc->per_bio_data_size = ti->per_io_data_size =
  2192. ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
  2193. ARCH_KMALLOC_MINALIGN);
  2194. ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
  2195. if (ret) {
  2196. ti->error = "Cannot allocate page mempool";
  2197. goto bad;
  2198. }
  2199. ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
  2200. if (ret) {
  2201. ti->error = "Cannot allocate crypt bioset";
  2202. goto bad;
  2203. }
  2204. mutex_init(&cc->bio_alloc_lock);
  2205. ret = -EINVAL;
  2206. if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
  2207. (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
  2208. ti->error = "Invalid iv_offset sector";
  2209. goto bad;
  2210. }
  2211. cc->iv_offset = tmpll;
  2212. ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
  2213. if (ret) {
  2214. ti->error = "Device lookup failed";
  2215. goto bad;
  2216. }
  2217. ret = -EINVAL;
  2218. if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
  2219. ti->error = "Invalid device sector";
  2220. goto bad;
  2221. }
  2222. cc->start = tmpll;
  2223. if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
  2224. ret = crypt_integrity_ctr(cc, ti);
  2225. if (ret)
  2226. goto bad;
  2227. cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
  2228. if (!cc->tag_pool_max_sectors)
  2229. cc->tag_pool_max_sectors = 1;
  2230. ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
  2231. cc->tag_pool_max_sectors * cc->on_disk_tag_size);
  2232. if (ret) {
  2233. ti->error = "Cannot allocate integrity tags mempool";
  2234. goto bad;
  2235. }
  2236. cc->tag_pool_max_sectors <<= cc->sector_shift;
  2237. }
  2238. ret = -ENOMEM;
  2239. cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname);
  2240. if (!cc->io_queue) {
  2241. ti->error = "Couldn't create kcryptd io queue";
  2242. goto bad;
  2243. }
  2244. if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
  2245. cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
  2246. 1, devname);
  2247. else
  2248. cc->crypt_queue = alloc_workqueue("kcryptd/%s",
  2249. WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
  2250. num_online_cpus(), devname);
  2251. if (!cc->crypt_queue) {
  2252. ti->error = "Couldn't create kcryptd queue";
  2253. goto bad;
  2254. }
  2255. spin_lock_init(&cc->write_thread_lock);
  2256. cc->write_tree = RB_ROOT;
  2257. cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
  2258. if (IS_ERR(cc->write_thread)) {
  2259. ret = PTR_ERR(cc->write_thread);
  2260. cc->write_thread = NULL;
  2261. ti->error = "Couldn't spawn write thread";
  2262. goto bad;
  2263. }
  2264. wake_up_process(cc->write_thread);
  2265. ti->num_flush_bios = 1;
  2266. ti->limit_swap_bios = true;
  2267. return 0;
  2268. bad:
  2269. crypt_dtr(ti);
  2270. return ret;
  2271. }
  2272. static int crypt_map(struct dm_target *ti, struct bio *bio)
  2273. {
  2274. struct dm_crypt_io *io;
  2275. struct crypt_config *cc = ti->private;
  2276. /*
  2277. * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
  2278. * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
  2279. * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
  2280. */
  2281. if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
  2282. bio_op(bio) == REQ_OP_DISCARD)) {
  2283. bio_set_dev(bio, cc->dev->bdev);
  2284. if (bio_sectors(bio))
  2285. bio->bi_iter.bi_sector = cc->start +
  2286. dm_target_offset(ti, bio->bi_iter.bi_sector);
  2287. return DM_MAPIO_REMAPPED;
  2288. }
  2289. /*
  2290. * Check if bio is too large, split as needed.
  2291. */
  2292. if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
  2293. (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
  2294. dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
  2295. /*
  2296. * Ensure that bio is a multiple of internal sector encryption size
  2297. * and is aligned to this size as defined in IO hints.
  2298. */
  2299. if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
  2300. return DM_MAPIO_KILL;
  2301. if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
  2302. return DM_MAPIO_KILL;
  2303. io = dm_per_bio_data(bio, cc->per_bio_data_size);
  2304. crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
  2305. if (cc->on_disk_tag_size) {
  2306. unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
  2307. if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
  2308. unlikely(!(io->integrity_metadata = kmalloc(tag_len,
  2309. GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
  2310. if (bio_sectors(bio) > cc->tag_pool_max_sectors)
  2311. dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
  2312. io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
  2313. io->integrity_metadata_from_pool = true;
  2314. }
  2315. }
  2316. if (crypt_integrity_aead(cc))
  2317. io->ctx.r.req_aead = (struct aead_request *)(io + 1);
  2318. else
  2319. io->ctx.r.req = (struct skcipher_request *)(io + 1);
  2320. if (bio_data_dir(io->base_bio) == READ) {
  2321. if (kcryptd_io_read(io, GFP_NOWAIT))
  2322. kcryptd_queue_read(io);
  2323. } else
  2324. kcryptd_queue_crypt(io);
  2325. return DM_MAPIO_SUBMITTED;
  2326. }
  2327. static void crypt_status(struct dm_target *ti, status_type_t type,
  2328. unsigned status_flags, char *result, unsigned maxlen)
  2329. {
  2330. struct crypt_config *cc = ti->private;
  2331. unsigned i, sz = 0;
  2332. int num_feature_args = 0;
  2333. switch (type) {
  2334. case STATUSTYPE_INFO:
  2335. result[0] = '\0';
  2336. break;
  2337. case STATUSTYPE_TABLE:
  2338. DMEMIT("%s ", cc->cipher_string);
  2339. if (cc->key_size > 0) {
  2340. if (cc->key_string)
  2341. DMEMIT(":%u:%s", cc->key_size, cc->key_string);
  2342. else
  2343. for (i = 0; i < cc->key_size; i++)
  2344. DMEMIT("%02x", cc->key[i]);
  2345. } else
  2346. DMEMIT("-");
  2347. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  2348. cc->dev->name, (unsigned long long)cc->start);
  2349. num_feature_args += !!ti->num_discard_bios;
  2350. num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
  2351. num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
  2352. num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
  2353. num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
  2354. if (cc->on_disk_tag_size)
  2355. num_feature_args++;
  2356. if (num_feature_args) {
  2357. DMEMIT(" %d", num_feature_args);
  2358. if (ti->num_discard_bios)
  2359. DMEMIT(" allow_discards");
  2360. if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
  2361. DMEMIT(" same_cpu_crypt");
  2362. if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
  2363. DMEMIT(" submit_from_crypt_cpus");
  2364. if (cc->on_disk_tag_size)
  2365. DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
  2366. if (cc->sector_size != (1 << SECTOR_SHIFT))
  2367. DMEMIT(" sector_size:%d", cc->sector_size);
  2368. if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
  2369. DMEMIT(" iv_large_sectors");
  2370. }
  2371. break;
  2372. }
  2373. }
  2374. static void crypt_postsuspend(struct dm_target *ti)
  2375. {
  2376. struct crypt_config *cc = ti->private;
  2377. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  2378. }
  2379. static int crypt_preresume(struct dm_target *ti)
  2380. {
  2381. struct crypt_config *cc = ti->private;
  2382. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  2383. DMERR("aborting resume - crypt key is not set.");
  2384. return -EAGAIN;
  2385. }
  2386. return 0;
  2387. }
  2388. static void crypt_resume(struct dm_target *ti)
  2389. {
  2390. struct crypt_config *cc = ti->private;
  2391. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  2392. }
  2393. /* Message interface
  2394. * key set <key>
  2395. * key wipe
  2396. */
  2397. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
  2398. char *result, unsigned maxlen)
  2399. {
  2400. struct crypt_config *cc = ti->private;
  2401. int key_size, ret = -EINVAL;
  2402. if (argc < 2)
  2403. goto error;
  2404. if (!strcasecmp(argv[0], "key")) {
  2405. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  2406. DMWARN("not suspended during key manipulation.");
  2407. return -EINVAL;
  2408. }
  2409. if (argc == 3 && !strcasecmp(argv[1], "set")) {
  2410. /* The key size may not be changed. */
  2411. key_size = get_key_size(&argv[2]);
  2412. if (key_size < 0 || cc->key_size != key_size) {
  2413. memset(argv[2], '0', strlen(argv[2]));
  2414. return -EINVAL;
  2415. }
  2416. ret = crypt_set_key(cc, argv[2]);
  2417. if (ret)
  2418. return ret;
  2419. if (cc->iv_gen_ops && cc->iv_gen_ops->init)
  2420. ret = cc->iv_gen_ops->init(cc);
  2421. /* wipe the kernel key payload copy */
  2422. if (cc->key_string)
  2423. memset(cc->key, 0, cc->key_size * sizeof(u8));
  2424. return ret;
  2425. }
  2426. if (argc == 2 && !strcasecmp(argv[1], "wipe"))
  2427. return crypt_wipe_key(cc);
  2428. }
  2429. error:
  2430. DMWARN("unrecognised message received.");
  2431. return -EINVAL;
  2432. }
  2433. static int crypt_iterate_devices(struct dm_target *ti,
  2434. iterate_devices_callout_fn fn, void *data)
  2435. {
  2436. struct crypt_config *cc = ti->private;
  2437. return fn(ti, cc->dev, cc->start, ti->len, data);
  2438. }
  2439. static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2440. {
  2441. struct crypt_config *cc = ti->private;
  2442. /*
  2443. * Unfortunate constraint that is required to avoid the potential
  2444. * for exceeding underlying device's max_segments limits -- due to
  2445. * crypt_alloc_buffer() possibly allocating pages for the encryption
  2446. * bio that are not as physically contiguous as the original bio.
  2447. */
  2448. limits->max_segment_size = PAGE_SIZE;
  2449. limits->logical_block_size =
  2450. max_t(unsigned, limits->logical_block_size, cc->sector_size);
  2451. limits->physical_block_size =
  2452. max_t(unsigned, limits->physical_block_size, cc->sector_size);
  2453. limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
  2454. }
  2455. static struct target_type crypt_target = {
  2456. .name = "crypt",
  2457. .version = {1, 19, 0},
  2458. .module = THIS_MODULE,
  2459. .ctr = crypt_ctr,
  2460. .dtr = crypt_dtr,
  2461. .map = crypt_map,
  2462. .status = crypt_status,
  2463. .postsuspend = crypt_postsuspend,
  2464. .preresume = crypt_preresume,
  2465. .resume = crypt_resume,
  2466. .message = crypt_message,
  2467. .iterate_devices = crypt_iterate_devices,
  2468. .io_hints = crypt_io_hints,
  2469. };
  2470. static int __init dm_crypt_init(void)
  2471. {
  2472. int r;
  2473. r = dm_register_target(&crypt_target);
  2474. if (r < 0)
  2475. DMERR("register failed %d", r);
  2476. return r;
  2477. }
  2478. static void __exit dm_crypt_exit(void)
  2479. {
  2480. dm_unregister_target(&crypt_target);
  2481. }
  2482. module_init(dm_crypt_init);
  2483. module_exit(dm_crypt_exit);
  2484. MODULE_AUTHOR("Jana Saout <jana@saout.de>");
  2485. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  2486. MODULE_LICENSE("GPL");