keystore.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * eCryptfs: Linux filesystem encryption layer
  4. * In-kernel key management code. Includes functions to parse and
  5. * write authentication token-related packets with the underlying
  6. * file.
  7. *
  8. * Copyright (C) 2004-2006 International Business Machines Corp.
  9. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  10. * Michael C. Thompson <mcthomps@us.ibm.com>
  11. * Trevor S. Highland <trevor.highland@gmail.com>
  12. */
  13. #include <crypto/hash.h>
  14. #include <crypto/skcipher.h>
  15. #include <linux/string.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/key.h>
  18. #include <linux/random.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/slab.h>
  21. #include "ecryptfs_kernel.h"
  22. /*
  23. * request_key returned an error instead of a valid key address;
  24. * determine the type of error, make appropriate log entries, and
  25. * return an error code.
  26. */
  27. static int process_request_key_err(long err_code)
  28. {
  29. int rc = 0;
  30. switch (err_code) {
  31. case -ENOKEY:
  32. ecryptfs_printk(KERN_WARNING, "No key\n");
  33. rc = -ENOENT;
  34. break;
  35. case -EKEYEXPIRED:
  36. ecryptfs_printk(KERN_WARNING, "Key expired\n");
  37. rc = -ETIME;
  38. break;
  39. case -EKEYREVOKED:
  40. ecryptfs_printk(KERN_WARNING, "Key revoked\n");
  41. rc = -EINVAL;
  42. break;
  43. default:
  44. ecryptfs_printk(KERN_WARNING, "Unknown error code: "
  45. "[0x%.16lx]\n", err_code);
  46. rc = -EINVAL;
  47. }
  48. return rc;
  49. }
  50. static int process_find_global_auth_tok_for_sig_err(int err_code)
  51. {
  52. int rc = err_code;
  53. switch (err_code) {
  54. case -ENOENT:
  55. ecryptfs_printk(KERN_WARNING, "Missing auth tok\n");
  56. break;
  57. case -EINVAL:
  58. ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n");
  59. break;
  60. default:
  61. rc = process_request_key_err(err_code);
  62. break;
  63. }
  64. return rc;
  65. }
  66. /**
  67. * ecryptfs_parse_packet_length
  68. * @data: Pointer to memory containing length at offset
  69. * @size: This function writes the decoded size to this memory
  70. * address; zero on error
  71. * @length_size: The number of bytes occupied by the encoded length
  72. *
  73. * Returns zero on success; non-zero on error
  74. */
  75. int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
  76. size_t *length_size)
  77. {
  78. int rc = 0;
  79. (*length_size) = 0;
  80. (*size) = 0;
  81. if (data[0] < 192) {
  82. /* One-byte length */
  83. (*size) = data[0];
  84. (*length_size) = 1;
  85. } else if (data[0] < 224) {
  86. /* Two-byte length */
  87. (*size) = (data[0] - 192) * 256;
  88. (*size) += data[1] + 192;
  89. (*length_size) = 2;
  90. } else if (data[0] == 255) {
  91. /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */
  92. ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
  93. "supported\n");
  94. rc = -EINVAL;
  95. goto out;
  96. } else {
  97. ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
  98. rc = -EINVAL;
  99. goto out;
  100. }
  101. out:
  102. return rc;
  103. }
  104. /**
  105. * ecryptfs_write_packet_length
  106. * @dest: The byte array target into which to write the length. Must
  107. * have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated.
  108. * @size: The length to write.
  109. * @packet_size_length: The number of bytes used to encode the packet
  110. * length is written to this address.
  111. *
  112. * Returns zero on success; non-zero on error.
  113. */
  114. int ecryptfs_write_packet_length(char *dest, size_t size,
  115. size_t *packet_size_length)
  116. {
  117. int rc = 0;
  118. if (size < 192) {
  119. dest[0] = size;
  120. (*packet_size_length) = 1;
  121. } else if (size < 65536) {
  122. dest[0] = (((size - 192) / 256) + 192);
  123. dest[1] = ((size - 192) % 256);
  124. (*packet_size_length) = 2;
  125. } else {
  126. /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */
  127. rc = -EINVAL;
  128. ecryptfs_printk(KERN_WARNING,
  129. "Unsupported packet size: [%zd]\n", size);
  130. }
  131. return rc;
  132. }
  133. static int
  134. write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
  135. char **packet, size_t *packet_len)
  136. {
  137. size_t i = 0;
  138. size_t data_len;
  139. size_t packet_size_len;
  140. char *message;
  141. int rc;
  142. /*
  143. * ***** TAG 64 Packet Format *****
  144. * | Content Type | 1 byte |
  145. * | Key Identifier Size | 1 or 2 bytes |
  146. * | Key Identifier | arbitrary |
  147. * | Encrypted File Encryption Key Size | 1 or 2 bytes |
  148. * | Encrypted File Encryption Key | arbitrary |
  149. */
  150. data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
  151. + session_key->encrypted_key_size);
  152. *packet = kmalloc(data_len, GFP_KERNEL);
  153. message = *packet;
  154. if (!message) {
  155. ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
  156. rc = -ENOMEM;
  157. goto out;
  158. }
  159. message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
  160. rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
  161. &packet_size_len);
  162. if (rc) {
  163. ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
  164. "header; cannot generate packet length\n");
  165. goto out;
  166. }
  167. i += packet_size_len;
  168. memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
  169. i += ECRYPTFS_SIG_SIZE_HEX;
  170. rc = ecryptfs_write_packet_length(&message[i],
  171. session_key->encrypted_key_size,
  172. &packet_size_len);
  173. if (rc) {
  174. ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
  175. "header; cannot generate packet length\n");
  176. goto out;
  177. }
  178. i += packet_size_len;
  179. memcpy(&message[i], session_key->encrypted_key,
  180. session_key->encrypted_key_size);
  181. i += session_key->encrypted_key_size;
  182. *packet_len = i;
  183. out:
  184. return rc;
  185. }
  186. static int
  187. parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
  188. struct ecryptfs_message *msg)
  189. {
  190. size_t i = 0;
  191. char *data;
  192. size_t data_len;
  193. size_t m_size;
  194. size_t message_len;
  195. u16 checksum = 0;
  196. u16 expected_checksum = 0;
  197. int rc;
  198. /*
  199. * ***** TAG 65 Packet Format *****
  200. * | Content Type | 1 byte |
  201. * | Status Indicator | 1 byte |
  202. * | File Encryption Key Size | 1 or 2 bytes |
  203. * | File Encryption Key | arbitrary |
  204. */
  205. message_len = msg->data_len;
  206. data = msg->data;
  207. if (message_len < 4) {
  208. rc = -EIO;
  209. goto out;
  210. }
  211. if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
  212. ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
  213. rc = -EIO;
  214. goto out;
  215. }
  216. if (data[i++]) {
  217. ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
  218. "[%d]\n", data[i-1]);
  219. rc = -EIO;
  220. goto out;
  221. }
  222. rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
  223. if (rc) {
  224. ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  225. "rc = [%d]\n", rc);
  226. goto out;
  227. }
  228. i += data_len;
  229. if (message_len < (i + m_size)) {
  230. ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd "
  231. "is shorter than expected\n");
  232. rc = -EIO;
  233. goto out;
  234. }
  235. if (m_size < 3) {
  236. ecryptfs_printk(KERN_ERR,
  237. "The decrypted key is not long enough to "
  238. "include a cipher code and checksum\n");
  239. rc = -EIO;
  240. goto out;
  241. }
  242. *cipher_code = data[i++];
  243. /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
  244. session_key->decrypted_key_size = m_size - 3;
  245. if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
  246. ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
  247. "the maximum key size [%d]\n",
  248. session_key->decrypted_key_size,
  249. ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
  250. rc = -EIO;
  251. goto out;
  252. }
  253. memcpy(session_key->decrypted_key, &data[i],
  254. session_key->decrypted_key_size);
  255. i += session_key->decrypted_key_size;
  256. expected_checksum += (unsigned char)(data[i++]) << 8;
  257. expected_checksum += (unsigned char)(data[i++]);
  258. for (i = 0; i < session_key->decrypted_key_size; i++)
  259. checksum += session_key->decrypted_key[i];
  260. if (expected_checksum != checksum) {
  261. ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
  262. "encryption key; expected [%x]; calculated "
  263. "[%x]\n", expected_checksum, checksum);
  264. rc = -EIO;
  265. }
  266. out:
  267. return rc;
  268. }
  269. static int
  270. write_tag_66_packet(char *signature, u8 cipher_code,
  271. struct ecryptfs_crypt_stat *crypt_stat, char **packet,
  272. size_t *packet_len)
  273. {
  274. size_t i = 0;
  275. size_t j;
  276. size_t data_len;
  277. size_t checksum = 0;
  278. size_t packet_size_len;
  279. char *message;
  280. int rc;
  281. /*
  282. * ***** TAG 66 Packet Format *****
  283. * | Content Type | 1 byte |
  284. * | Key Identifier Size | 1 or 2 bytes |
  285. * | Key Identifier | arbitrary |
  286. * | File Encryption Key Size | 1 or 2 bytes |
  287. * | Cipher Code | 1 byte |
  288. * | File Encryption Key | arbitrary |
  289. * | Checksum | 2 bytes |
  290. */
  291. data_len = (8 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
  292. *packet = kmalloc(data_len, GFP_KERNEL);
  293. message = *packet;
  294. if (!message) {
  295. ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
  296. rc = -ENOMEM;
  297. goto out;
  298. }
  299. message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
  300. rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
  301. &packet_size_len);
  302. if (rc) {
  303. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
  304. "header; cannot generate packet length\n");
  305. goto out;
  306. }
  307. i += packet_size_len;
  308. memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
  309. i += ECRYPTFS_SIG_SIZE_HEX;
  310. /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
  311. rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
  312. &packet_size_len);
  313. if (rc) {
  314. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
  315. "header; cannot generate packet length\n");
  316. goto out;
  317. }
  318. i += packet_size_len;
  319. message[i++] = cipher_code;
  320. memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
  321. i += crypt_stat->key_size;
  322. for (j = 0; j < crypt_stat->key_size; j++)
  323. checksum += crypt_stat->key[j];
  324. message[i++] = (checksum / 256) % 256;
  325. message[i++] = (checksum % 256);
  326. *packet_len = i;
  327. out:
  328. return rc;
  329. }
  330. static int
  331. parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
  332. struct ecryptfs_message *msg)
  333. {
  334. size_t i = 0;
  335. char *data;
  336. size_t data_len;
  337. size_t message_len;
  338. int rc;
  339. /*
  340. * ***** TAG 65 Packet Format *****
  341. * | Content Type | 1 byte |
  342. * | Status Indicator | 1 byte |
  343. * | Encrypted File Encryption Key Size | 1 or 2 bytes |
  344. * | Encrypted File Encryption Key | arbitrary |
  345. */
  346. message_len = msg->data_len;
  347. data = msg->data;
  348. /* verify that everything through the encrypted FEK size is present */
  349. if (message_len < 4) {
  350. rc = -EIO;
  351. printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable "
  352. "message length is [%d]\n", __func__, message_len, 4);
  353. goto out;
  354. }
  355. if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
  356. rc = -EIO;
  357. printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
  358. __func__);
  359. goto out;
  360. }
  361. if (data[i++]) {
  362. rc = -EIO;
  363. printk(KERN_ERR "%s: Status indicator has non zero "
  364. "value [%d]\n", __func__, data[i-1]);
  365. goto out;
  366. }
  367. rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
  368. &data_len);
  369. if (rc) {
  370. ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  371. "rc = [%d]\n", rc);
  372. goto out;
  373. }
  374. i += data_len;
  375. if (message_len < (i + key_rec->enc_key_size)) {
  376. rc = -EIO;
  377. printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n",
  378. __func__, message_len, (i + key_rec->enc_key_size));
  379. goto out;
  380. }
  381. if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
  382. rc = -EIO;
  383. printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than "
  384. "the maximum key size [%d]\n", __func__,
  385. key_rec->enc_key_size,
  386. ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
  387. goto out;
  388. }
  389. memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
  390. out:
  391. return rc;
  392. }
  393. /**
  394. * ecryptfs_verify_version
  395. * @version: The version number to confirm
  396. *
  397. * Returns zero on good version; non-zero otherwise
  398. */
  399. static int ecryptfs_verify_version(u16 version)
  400. {
  401. int rc = 0;
  402. unsigned char major;
  403. unsigned char minor;
  404. major = ((version >> 8) & 0xFF);
  405. minor = (version & 0xFF);
  406. if (major != ECRYPTFS_VERSION_MAJOR) {
  407. ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
  408. "Expected [%d]; got [%d]\n",
  409. ECRYPTFS_VERSION_MAJOR, major);
  410. rc = -EINVAL;
  411. goto out;
  412. }
  413. if (minor != ECRYPTFS_VERSION_MINOR) {
  414. ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
  415. "Expected [%d]; got [%d]\n",
  416. ECRYPTFS_VERSION_MINOR, minor);
  417. rc = -EINVAL;
  418. goto out;
  419. }
  420. out:
  421. return rc;
  422. }
  423. /**
  424. * ecryptfs_verify_auth_tok_from_key
  425. * @auth_tok_key: key containing the authentication token
  426. * @auth_tok: authentication token
  427. *
  428. * Returns zero on valid auth tok; -EINVAL if the payload is invalid; or
  429. * -EKEYREVOKED if the key was revoked before we acquired its semaphore.
  430. */
  431. static int
  432. ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
  433. struct ecryptfs_auth_tok **auth_tok)
  434. {
  435. int rc = 0;
  436. (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
  437. if (IS_ERR(*auth_tok)) {
  438. rc = PTR_ERR(*auth_tok);
  439. *auth_tok = NULL;
  440. goto out;
  441. }
  442. if (ecryptfs_verify_version((*auth_tok)->version)) {
  443. printk(KERN_ERR "Data structure version mismatch. Userspace "
  444. "tools must match eCryptfs kernel module with major "
  445. "version [%d] and minor version [%d]\n",
  446. ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR);
  447. rc = -EINVAL;
  448. goto out;
  449. }
  450. if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
  451. && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
  452. printk(KERN_ERR "Invalid auth_tok structure "
  453. "returned from key query\n");
  454. rc = -EINVAL;
  455. goto out;
  456. }
  457. out:
  458. return rc;
  459. }
  460. static int
  461. ecryptfs_find_global_auth_tok_for_sig(
  462. struct key **auth_tok_key,
  463. struct ecryptfs_auth_tok **auth_tok,
  464. struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
  465. {
  466. struct ecryptfs_global_auth_tok *walker;
  467. int rc = 0;
  468. (*auth_tok_key) = NULL;
  469. (*auth_tok) = NULL;
  470. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  471. list_for_each_entry(walker,
  472. &mount_crypt_stat->global_auth_tok_list,
  473. mount_crypt_stat_list) {
  474. if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX))
  475. continue;
  476. if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) {
  477. rc = -EINVAL;
  478. goto out;
  479. }
  480. rc = key_validate(walker->global_auth_tok_key);
  481. if (rc) {
  482. if (rc == -EKEYEXPIRED)
  483. goto out;
  484. goto out_invalid_auth_tok;
  485. }
  486. down_write(&(walker->global_auth_tok_key->sem));
  487. rc = ecryptfs_verify_auth_tok_from_key(
  488. walker->global_auth_tok_key, auth_tok);
  489. if (rc)
  490. goto out_invalid_auth_tok_unlock;
  491. (*auth_tok_key) = walker->global_auth_tok_key;
  492. key_get(*auth_tok_key);
  493. goto out;
  494. }
  495. rc = -ENOENT;
  496. goto out;
  497. out_invalid_auth_tok_unlock:
  498. up_write(&(walker->global_auth_tok_key->sem));
  499. out_invalid_auth_tok:
  500. printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig);
  501. walker->flags |= ECRYPTFS_AUTH_TOK_INVALID;
  502. key_put(walker->global_auth_tok_key);
  503. walker->global_auth_tok_key = NULL;
  504. out:
  505. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  506. return rc;
  507. }
  508. /**
  509. * ecryptfs_find_auth_tok_for_sig
  510. * @auth_tok_key: key containing the authentication token
  511. * @auth_tok: Set to the matching auth_tok; NULL if not found
  512. * @mount_crypt_stat: inode crypt_stat crypto context
  513. * @sig: Sig of auth_tok to find
  514. *
  515. * For now, this function simply looks at the registered auth_tok's
  516. * linked off the mount_crypt_stat, so all the auth_toks that can be
  517. * used must be registered at mount time. This function could
  518. * potentially try a lot harder to find auth_tok's (e.g., by calling
  519. * out to ecryptfsd to dynamically retrieve an auth_tok object) so
  520. * that static registration of auth_tok's will no longer be necessary.
  521. *
  522. * Returns zero on no error; non-zero on error
  523. */
  524. static int
  525. ecryptfs_find_auth_tok_for_sig(
  526. struct key **auth_tok_key,
  527. struct ecryptfs_auth_tok **auth_tok,
  528. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  529. char *sig)
  530. {
  531. int rc = 0;
  532. rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok,
  533. mount_crypt_stat, sig);
  534. if (rc == -ENOENT) {
  535. /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the
  536. * mount_crypt_stat structure, we prevent to use auth toks that
  537. * are not inserted through the ecryptfs_add_global_auth_tok
  538. * function.
  539. */
  540. if (mount_crypt_stat->flags
  541. & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY)
  542. return -EINVAL;
  543. rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok,
  544. sig);
  545. }
  546. return rc;
  547. }
  548. /*
  549. * write_tag_70_packet can gobble a lot of stack space. We stuff most
  550. * of the function's parameters in a kmalloc'd struct to help reduce
  551. * eCryptfs' overall stack usage.
  552. */
  553. struct ecryptfs_write_tag_70_packet_silly_stack {
  554. u8 cipher_code;
  555. size_t max_packet_size;
  556. size_t packet_size_len;
  557. size_t block_aligned_filename_size;
  558. size_t block_size;
  559. size_t i;
  560. size_t j;
  561. size_t num_rand_bytes;
  562. struct mutex *tfm_mutex;
  563. char *block_aligned_filename;
  564. struct ecryptfs_auth_tok *auth_tok;
  565. struct scatterlist src_sg[2];
  566. struct scatterlist dst_sg[2];
  567. struct crypto_skcipher *skcipher_tfm;
  568. struct skcipher_request *skcipher_req;
  569. char iv[ECRYPTFS_MAX_IV_BYTES];
  570. char hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
  571. char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE];
  572. struct crypto_shash *hash_tfm;
  573. struct shash_desc *hash_desc;
  574. };
  575. /*
  576. * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK
  577. * @filename: NULL-terminated filename string
  578. *
  579. * This is the simplest mechanism for achieving filename encryption in
  580. * eCryptfs. It encrypts the given filename with the mount-wide
  581. * filename encryption key (FNEK) and stores it in a packet to @dest,
  582. * which the callee will encode and write directly into the dentry
  583. * name.
  584. */
  585. int
  586. ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
  587. size_t *packet_size,
  588. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  589. char *filename, size_t filename_size)
  590. {
  591. struct ecryptfs_write_tag_70_packet_silly_stack *s;
  592. struct key *auth_tok_key = NULL;
  593. int rc = 0;
  594. s = kzalloc(sizeof(*s), GFP_KERNEL);
  595. if (!s)
  596. return -ENOMEM;
  597. (*packet_size) = 0;
  598. rc = ecryptfs_find_auth_tok_for_sig(
  599. &auth_tok_key,
  600. &s->auth_tok, mount_crypt_stat,
  601. mount_crypt_stat->global_default_fnek_sig);
  602. if (rc) {
  603. printk(KERN_ERR "%s: Error attempting to find auth tok for "
  604. "fnek sig [%s]; rc = [%d]\n", __func__,
  605. mount_crypt_stat->global_default_fnek_sig, rc);
  606. goto out;
  607. }
  608. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
  609. &s->skcipher_tfm,
  610. &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
  611. if (unlikely(rc)) {
  612. printk(KERN_ERR "Internal error whilst attempting to get "
  613. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  614. mount_crypt_stat->global_default_fn_cipher_name, rc);
  615. goto out;
  616. }
  617. mutex_lock(s->tfm_mutex);
  618. s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm);
  619. /* Plus one for the \0 separator between the random prefix
  620. * and the plaintext filename */
  621. s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1);
  622. s->block_aligned_filename_size = (s->num_rand_bytes + filename_size);
  623. if ((s->block_aligned_filename_size % s->block_size) != 0) {
  624. s->num_rand_bytes += (s->block_size
  625. - (s->block_aligned_filename_size
  626. % s->block_size));
  627. s->block_aligned_filename_size = (s->num_rand_bytes
  628. + filename_size);
  629. }
  630. /* Octet 0: Tag 70 identifier
  631. * Octets 1-N1: Tag 70 packet size (includes cipher identifier
  632. * and block-aligned encrypted filename size)
  633. * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
  634. * Octet N2-N3: Cipher identifier (1 octet)
  635. * Octets N3-N4: Block-aligned encrypted filename
  636. * - Consists of a minimum number of random characters, a \0
  637. * separator, and then the filename */
  638. s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE
  639. + s->block_aligned_filename_size);
  640. if (!dest) {
  641. (*packet_size) = s->max_packet_size;
  642. goto out_unlock;
  643. }
  644. if (s->max_packet_size > (*remaining_bytes)) {
  645. printk(KERN_WARNING "%s: Require [%zd] bytes to write; only "
  646. "[%zd] available\n", __func__, s->max_packet_size,
  647. (*remaining_bytes));
  648. rc = -EINVAL;
  649. goto out_unlock;
  650. }
  651. s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL);
  652. if (!s->skcipher_req) {
  653. printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  654. "skcipher_request_alloc for %s\n", __func__,
  655. crypto_skcipher_driver_name(s->skcipher_tfm));
  656. rc = -ENOMEM;
  657. goto out_unlock;
  658. }
  659. skcipher_request_set_callback(s->skcipher_req,
  660. CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  661. s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
  662. GFP_KERNEL);
  663. if (!s->block_aligned_filename) {
  664. rc = -ENOMEM;
  665. goto out_unlock;
  666. }
  667. dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE;
  668. rc = ecryptfs_write_packet_length(&dest[s->i],
  669. (ECRYPTFS_SIG_SIZE
  670. + 1 /* Cipher code */
  671. + s->block_aligned_filename_size),
  672. &s->packet_size_len);
  673. if (rc) {
  674. printk(KERN_ERR "%s: Error generating tag 70 packet "
  675. "header; cannot generate packet length; rc = [%d]\n",
  676. __func__, rc);
  677. goto out_free_unlock;
  678. }
  679. s->i += s->packet_size_len;
  680. ecryptfs_from_hex(&dest[s->i],
  681. mount_crypt_stat->global_default_fnek_sig,
  682. ECRYPTFS_SIG_SIZE);
  683. s->i += ECRYPTFS_SIG_SIZE;
  684. s->cipher_code = ecryptfs_code_for_cipher_string(
  685. mount_crypt_stat->global_default_fn_cipher_name,
  686. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  687. if (s->cipher_code == 0) {
  688. printk(KERN_WARNING "%s: Unable to generate code for "
  689. "cipher [%s] with key bytes [%zd]\n", __func__,
  690. mount_crypt_stat->global_default_fn_cipher_name,
  691. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  692. rc = -EINVAL;
  693. goto out_free_unlock;
  694. }
  695. dest[s->i++] = s->cipher_code;
  696. /* TODO: Support other key modules than passphrase for
  697. * filename encryption */
  698. if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
  699. rc = -EOPNOTSUPP;
  700. printk(KERN_INFO "%s: Filename encryption only supports "
  701. "password tokens\n", __func__);
  702. goto out_free_unlock;
  703. }
  704. s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0);
  705. if (IS_ERR(s->hash_tfm)) {
  706. rc = PTR_ERR(s->hash_tfm);
  707. printk(KERN_ERR "%s: Error attempting to "
  708. "allocate hash crypto context; rc = [%d]\n",
  709. __func__, rc);
  710. goto out_free_unlock;
  711. }
  712. s->hash_desc = kmalloc(sizeof(*s->hash_desc) +
  713. crypto_shash_descsize(s->hash_tfm), GFP_KERNEL);
  714. if (!s->hash_desc) {
  715. rc = -ENOMEM;
  716. goto out_release_free_unlock;
  717. }
  718. s->hash_desc->tfm = s->hash_tfm;
  719. rc = crypto_shash_digest(s->hash_desc,
  720. (u8 *)s->auth_tok->token.password.session_key_encryption_key,
  721. s->auth_tok->token.password.session_key_encryption_key_bytes,
  722. s->hash);
  723. if (rc) {
  724. printk(KERN_ERR
  725. "%s: Error computing crypto hash; rc = [%d]\n",
  726. __func__, rc);
  727. goto out_release_free_unlock;
  728. }
  729. for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) {
  730. s->block_aligned_filename[s->j] =
  731. s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)];
  732. if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)
  733. == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) {
  734. rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash,
  735. ECRYPTFS_TAG_70_DIGEST_SIZE,
  736. s->tmp_hash);
  737. if (rc) {
  738. printk(KERN_ERR
  739. "%s: Error computing crypto hash; "
  740. "rc = [%d]\n", __func__, rc);
  741. goto out_release_free_unlock;
  742. }
  743. memcpy(s->hash, s->tmp_hash,
  744. ECRYPTFS_TAG_70_DIGEST_SIZE);
  745. }
  746. if (s->block_aligned_filename[s->j] == '\0')
  747. s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL;
  748. }
  749. memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename,
  750. filename_size);
  751. rc = virt_to_scatterlist(s->block_aligned_filename,
  752. s->block_aligned_filename_size, s->src_sg, 2);
  753. if (rc < 1) {
  754. printk(KERN_ERR "%s: Internal error whilst attempting to "
  755. "convert filename memory to scatterlist; rc = [%d]. "
  756. "block_aligned_filename_size = [%zd]\n", __func__, rc,
  757. s->block_aligned_filename_size);
  758. goto out_release_free_unlock;
  759. }
  760. rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size,
  761. s->dst_sg, 2);
  762. if (rc < 1) {
  763. printk(KERN_ERR "%s: Internal error whilst attempting to "
  764. "convert encrypted filename memory to scatterlist; "
  765. "rc = [%d]. block_aligned_filename_size = [%zd]\n",
  766. __func__, rc, s->block_aligned_filename_size);
  767. goto out_release_free_unlock;
  768. }
  769. /* The characters in the first block effectively do the job
  770. * of the IV here, so we just use 0's for the IV. Note the
  771. * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
  772. * >= ECRYPTFS_MAX_IV_BYTES. */
  773. rc = crypto_skcipher_setkey(
  774. s->skcipher_tfm,
  775. s->auth_tok->token.password.session_key_encryption_key,
  776. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  777. if (rc < 0) {
  778. printk(KERN_ERR "%s: Error setting key for crypto context; "
  779. "rc = [%d]. s->auth_tok->token.password.session_key_"
  780. "encryption_key = [0x%p]; mount_crypt_stat->"
  781. "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
  782. rc,
  783. s->auth_tok->token.password.session_key_encryption_key,
  784. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  785. goto out_release_free_unlock;
  786. }
  787. skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg,
  788. s->block_aligned_filename_size, s->iv);
  789. rc = crypto_skcipher_encrypt(s->skcipher_req);
  790. if (rc) {
  791. printk(KERN_ERR "%s: Error attempting to encrypt filename; "
  792. "rc = [%d]\n", __func__, rc);
  793. goto out_release_free_unlock;
  794. }
  795. s->i += s->block_aligned_filename_size;
  796. (*packet_size) = s->i;
  797. (*remaining_bytes) -= (*packet_size);
  798. out_release_free_unlock:
  799. crypto_free_shash(s->hash_tfm);
  800. out_free_unlock:
  801. kfree_sensitive(s->block_aligned_filename);
  802. out_unlock:
  803. mutex_unlock(s->tfm_mutex);
  804. out:
  805. if (auth_tok_key) {
  806. up_write(&(auth_tok_key->sem));
  807. key_put(auth_tok_key);
  808. }
  809. skcipher_request_free(s->skcipher_req);
  810. kfree_sensitive(s->hash_desc);
  811. kfree(s);
  812. return rc;
  813. }
  814. struct ecryptfs_parse_tag_70_packet_silly_stack {
  815. u8 cipher_code;
  816. size_t max_packet_size;
  817. size_t packet_size_len;
  818. size_t parsed_tag_70_packet_size;
  819. size_t block_aligned_filename_size;
  820. size_t block_size;
  821. size_t i;
  822. struct mutex *tfm_mutex;
  823. char *decrypted_filename;
  824. struct ecryptfs_auth_tok *auth_tok;
  825. struct scatterlist src_sg[2];
  826. struct scatterlist dst_sg[2];
  827. struct crypto_skcipher *skcipher_tfm;
  828. struct skcipher_request *skcipher_req;
  829. char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
  830. char iv[ECRYPTFS_MAX_IV_BYTES];
  831. char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1];
  832. };
  833. /**
  834. * ecryptfs_parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
  835. * @filename: This function kmalloc's the memory for the filename
  836. * @filename_size: This function sets this to the amount of memory
  837. * kmalloc'd for the filename
  838. * @packet_size: This function sets this to the the number of octets
  839. * in the packet parsed
  840. * @mount_crypt_stat: The mount-wide cryptographic context
  841. * @data: The memory location containing the start of the tag 70
  842. * packet
  843. * @max_packet_size: The maximum legal size of the packet to be parsed
  844. * from @data
  845. *
  846. * Returns zero on success; non-zero otherwise
  847. */
  848. int
  849. ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
  850. size_t *packet_size,
  851. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  852. char *data, size_t max_packet_size)
  853. {
  854. struct ecryptfs_parse_tag_70_packet_silly_stack *s;
  855. struct key *auth_tok_key = NULL;
  856. int rc = 0;
  857. (*packet_size) = 0;
  858. (*filename_size) = 0;
  859. (*filename) = NULL;
  860. s = kzalloc(sizeof(*s), GFP_KERNEL);
  861. if (!s)
  862. return -ENOMEM;
  863. if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) {
  864. printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
  865. "at least [%d]\n", __func__, max_packet_size,
  866. ECRYPTFS_TAG_70_MIN_METADATA_SIZE);
  867. rc = -EINVAL;
  868. goto out;
  869. }
  870. /* Octet 0: Tag 70 identifier
  871. * Octets 1-N1: Tag 70 packet size (includes cipher identifier
  872. * and block-aligned encrypted filename size)
  873. * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
  874. * Octet N2-N3: Cipher identifier (1 octet)
  875. * Octets N3-N4: Block-aligned encrypted filename
  876. * - Consists of a minimum number of random numbers, a \0
  877. * separator, and then the filename */
  878. if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
  879. printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
  880. "tag [0x%.2x]\n", __func__,
  881. data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
  882. rc = -EINVAL;
  883. goto out;
  884. }
  885. rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
  886. &s->parsed_tag_70_packet_size,
  887. &s->packet_size_len);
  888. if (rc) {
  889. printk(KERN_WARNING "%s: Error parsing packet length; "
  890. "rc = [%d]\n", __func__, rc);
  891. goto out;
  892. }
  893. s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
  894. - ECRYPTFS_SIG_SIZE - 1);
  895. if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
  896. > max_packet_size) {
  897. printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
  898. "size is [%zd]\n", __func__, max_packet_size,
  899. (1 + s->packet_size_len + 1
  900. + s->block_aligned_filename_size));
  901. rc = -EINVAL;
  902. goto out;
  903. }
  904. (*packet_size) += s->packet_size_len;
  905. ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
  906. ECRYPTFS_SIG_SIZE);
  907. s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  908. (*packet_size) += ECRYPTFS_SIG_SIZE;
  909. s->cipher_code = data[(*packet_size)++];
  910. rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
  911. if (rc) {
  912. printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
  913. __func__, s->cipher_code);
  914. goto out;
  915. }
  916. rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
  917. &s->auth_tok, mount_crypt_stat,
  918. s->fnek_sig_hex);
  919. if (rc) {
  920. printk(KERN_ERR "%s: Error attempting to find auth tok for "
  921. "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
  922. rc);
  923. goto out;
  924. }
  925. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm,
  926. &s->tfm_mutex,
  927. s->cipher_string);
  928. if (unlikely(rc)) {
  929. printk(KERN_ERR "Internal error whilst attempting to get "
  930. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  931. s->cipher_string, rc);
  932. goto out;
  933. }
  934. mutex_lock(s->tfm_mutex);
  935. rc = virt_to_scatterlist(&data[(*packet_size)],
  936. s->block_aligned_filename_size, s->src_sg, 2);
  937. if (rc < 1) {
  938. printk(KERN_ERR "%s: Internal error whilst attempting to "
  939. "convert encrypted filename memory to scatterlist; "
  940. "rc = [%d]. block_aligned_filename_size = [%zd]\n",
  941. __func__, rc, s->block_aligned_filename_size);
  942. goto out_unlock;
  943. }
  944. (*packet_size) += s->block_aligned_filename_size;
  945. s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
  946. GFP_KERNEL);
  947. if (!s->decrypted_filename) {
  948. rc = -ENOMEM;
  949. goto out_unlock;
  950. }
  951. rc = virt_to_scatterlist(s->decrypted_filename,
  952. s->block_aligned_filename_size, s->dst_sg, 2);
  953. if (rc < 1) {
  954. printk(KERN_ERR "%s: Internal error whilst attempting to "
  955. "convert decrypted filename memory to scatterlist; "
  956. "rc = [%d]. block_aligned_filename_size = [%zd]\n",
  957. __func__, rc, s->block_aligned_filename_size);
  958. goto out_free_unlock;
  959. }
  960. s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL);
  961. if (!s->skcipher_req) {
  962. printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  963. "skcipher_request_alloc for %s\n", __func__,
  964. crypto_skcipher_driver_name(s->skcipher_tfm));
  965. rc = -ENOMEM;
  966. goto out_free_unlock;
  967. }
  968. skcipher_request_set_callback(s->skcipher_req,
  969. CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
  970. /* The characters in the first block effectively do the job of
  971. * the IV here, so we just use 0's for the IV. Note the
  972. * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
  973. * >= ECRYPTFS_MAX_IV_BYTES. */
  974. /* TODO: Support other key modules than passphrase for
  975. * filename encryption */
  976. if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
  977. rc = -EOPNOTSUPP;
  978. printk(KERN_INFO "%s: Filename encryption only supports "
  979. "password tokens\n", __func__);
  980. goto out_free_unlock;
  981. }
  982. rc = crypto_skcipher_setkey(
  983. s->skcipher_tfm,
  984. s->auth_tok->token.password.session_key_encryption_key,
  985. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  986. if (rc < 0) {
  987. printk(KERN_ERR "%s: Error setting key for crypto context; "
  988. "rc = [%d]. s->auth_tok->token.password.session_key_"
  989. "encryption_key = [0x%p]; mount_crypt_stat->"
  990. "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
  991. rc,
  992. s->auth_tok->token.password.session_key_encryption_key,
  993. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  994. goto out_free_unlock;
  995. }
  996. skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg,
  997. s->block_aligned_filename_size, s->iv);
  998. rc = crypto_skcipher_decrypt(s->skcipher_req);
  999. if (rc) {
  1000. printk(KERN_ERR "%s: Error attempting to decrypt filename; "
  1001. "rc = [%d]\n", __func__, rc);
  1002. goto out_free_unlock;
  1003. }
  1004. while (s->i < s->block_aligned_filename_size &&
  1005. s->decrypted_filename[s->i] != '\0')
  1006. s->i++;
  1007. if (s->i == s->block_aligned_filename_size) {
  1008. printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
  1009. "find valid separator between random characters and "
  1010. "the filename\n", __func__);
  1011. rc = -EINVAL;
  1012. goto out_free_unlock;
  1013. }
  1014. s->i++;
  1015. (*filename_size) = (s->block_aligned_filename_size - s->i);
  1016. if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
  1017. printk(KERN_WARNING "%s: Filename size is [%zd], which is "
  1018. "invalid\n", __func__, (*filename_size));
  1019. rc = -EINVAL;
  1020. goto out_free_unlock;
  1021. }
  1022. (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
  1023. if (!(*filename)) {
  1024. rc = -ENOMEM;
  1025. goto out_free_unlock;
  1026. }
  1027. memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
  1028. (*filename)[(*filename_size)] = '\0';
  1029. out_free_unlock:
  1030. kfree(s->decrypted_filename);
  1031. out_unlock:
  1032. mutex_unlock(s->tfm_mutex);
  1033. out:
  1034. if (rc) {
  1035. (*packet_size) = 0;
  1036. (*filename_size) = 0;
  1037. (*filename) = NULL;
  1038. }
  1039. if (auth_tok_key) {
  1040. up_write(&(auth_tok_key->sem));
  1041. key_put(auth_tok_key);
  1042. }
  1043. skcipher_request_free(s->skcipher_req);
  1044. kfree(s);
  1045. return rc;
  1046. }
  1047. static int
  1048. ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
  1049. {
  1050. int rc = 0;
  1051. (*sig) = NULL;
  1052. switch (auth_tok->token_type) {
  1053. case ECRYPTFS_PASSWORD:
  1054. (*sig) = auth_tok->token.password.signature;
  1055. break;
  1056. case ECRYPTFS_PRIVATE_KEY:
  1057. (*sig) = auth_tok->token.private_key.signature;
  1058. break;
  1059. default:
  1060. printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
  1061. auth_tok->token_type);
  1062. rc = -EINVAL;
  1063. }
  1064. return rc;
  1065. }
  1066. /**
  1067. * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
  1068. * @auth_tok: The key authentication token used to decrypt the session key
  1069. * @crypt_stat: The cryptographic context
  1070. *
  1071. * Returns zero on success; non-zero error otherwise.
  1072. */
  1073. static int
  1074. decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  1075. struct ecryptfs_crypt_stat *crypt_stat)
  1076. {
  1077. u8 cipher_code = 0;
  1078. struct ecryptfs_msg_ctx *msg_ctx;
  1079. struct ecryptfs_message *msg = NULL;
  1080. char *auth_tok_sig;
  1081. char *payload = NULL;
  1082. size_t payload_len = 0;
  1083. int rc;
  1084. rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
  1085. if (rc) {
  1086. printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
  1087. auth_tok->token_type);
  1088. goto out;
  1089. }
  1090. rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
  1091. &payload, &payload_len);
  1092. if (rc) {
  1093. ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
  1094. goto out;
  1095. }
  1096. rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
  1097. if (rc) {
  1098. ecryptfs_printk(KERN_ERR, "Error sending message to "
  1099. "ecryptfsd: %d\n", rc);
  1100. goto out;
  1101. }
  1102. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  1103. if (rc) {
  1104. ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
  1105. "from the user space daemon\n");
  1106. rc = -EIO;
  1107. goto out;
  1108. }
  1109. rc = parse_tag_65_packet(&(auth_tok->session_key),
  1110. &cipher_code, msg);
  1111. if (rc) {
  1112. printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
  1113. rc);
  1114. goto out;
  1115. }
  1116. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1117. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  1118. auth_tok->session_key.decrypted_key_size);
  1119. crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
  1120. rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
  1121. if (rc) {
  1122. ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
  1123. cipher_code);
  1124. goto out;
  1125. }
  1126. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  1127. if (ecryptfs_verbosity > 0) {
  1128. ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
  1129. ecryptfs_dump_hex(crypt_stat->key,
  1130. crypt_stat->key_size);
  1131. }
  1132. out:
  1133. kfree(msg);
  1134. kfree(payload);
  1135. return rc;
  1136. }
  1137. static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
  1138. {
  1139. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1140. struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  1141. list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
  1142. auth_tok_list_head, list) {
  1143. list_del(&auth_tok_list_item->list);
  1144. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  1145. auth_tok_list_item);
  1146. }
  1147. }
  1148. struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
  1149. /**
  1150. * parse_tag_1_packet
  1151. * @crypt_stat: The cryptographic context to modify based on packet contents
  1152. * @data: The raw bytes of the packet.
  1153. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  1154. * a new authentication token will be placed at the
  1155. * end of this list for this packet.
  1156. * @new_auth_tok: Pointer to a pointer to memory that this function
  1157. * allocates; sets the memory address of the pointer to
  1158. * NULL on error. This object is added to the
  1159. * auth_tok_list.
  1160. * @packet_size: This function writes the size of the parsed packet
  1161. * into this memory location; zero on error.
  1162. * @max_packet_size: The maximum allowable packet size
  1163. *
  1164. * Returns zero on success; non-zero on error.
  1165. */
  1166. static int
  1167. parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
  1168. unsigned char *data, struct list_head *auth_tok_list,
  1169. struct ecryptfs_auth_tok **new_auth_tok,
  1170. size_t *packet_size, size_t max_packet_size)
  1171. {
  1172. size_t body_size;
  1173. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1174. size_t length_size;
  1175. int rc = 0;
  1176. (*packet_size) = 0;
  1177. (*new_auth_tok) = NULL;
  1178. /**
  1179. * This format is inspired by OpenPGP; see RFC 2440
  1180. * packet tag 1
  1181. *
  1182. * Tag 1 identifier (1 byte)
  1183. * Max Tag 1 packet size (max 3 bytes)
  1184. * Version (1 byte)
  1185. * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
  1186. * Cipher identifier (1 byte)
  1187. * Encrypted key size (arbitrary)
  1188. *
  1189. * 12 bytes minimum packet size
  1190. */
  1191. if (unlikely(max_packet_size < 12)) {
  1192. printk(KERN_ERR "Invalid max packet size; must be >=12\n");
  1193. rc = -EINVAL;
  1194. goto out;
  1195. }
  1196. if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
  1197. printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
  1198. ECRYPTFS_TAG_1_PACKET_TYPE);
  1199. rc = -EINVAL;
  1200. goto out;
  1201. }
  1202. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  1203. * at end of function upon failure */
  1204. auth_tok_list_item =
  1205. kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
  1206. GFP_KERNEL);
  1207. if (!auth_tok_list_item) {
  1208. printk(KERN_ERR "Unable to allocate memory\n");
  1209. rc = -ENOMEM;
  1210. goto out;
  1211. }
  1212. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  1213. rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  1214. &length_size);
  1215. if (rc) {
  1216. printk(KERN_WARNING "Error parsing packet length; "
  1217. "rc = [%d]\n", rc);
  1218. goto out_free;
  1219. }
  1220. if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
  1221. printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
  1222. rc = -EINVAL;
  1223. goto out_free;
  1224. }
  1225. (*packet_size) += length_size;
  1226. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  1227. printk(KERN_WARNING "Packet size exceeds max\n");
  1228. rc = -EINVAL;
  1229. goto out_free;
  1230. }
  1231. if (unlikely(data[(*packet_size)++] != 0x03)) {
  1232. printk(KERN_WARNING "Unknown version number [%d]\n",
  1233. data[(*packet_size) - 1]);
  1234. rc = -EINVAL;
  1235. goto out_free;
  1236. }
  1237. ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
  1238. &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
  1239. *packet_size += ECRYPTFS_SIG_SIZE;
  1240. /* This byte is skipped because the kernel does not need to
  1241. * know which public key encryption algorithm was used */
  1242. (*packet_size)++;
  1243. (*new_auth_tok)->session_key.encrypted_key_size =
  1244. body_size - (ECRYPTFS_SIG_SIZE + 2);
  1245. if ((*new_auth_tok)->session_key.encrypted_key_size
  1246. > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
  1247. printk(KERN_WARNING "Tag 1 packet contains key larger "
  1248. "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
  1249. rc = -EINVAL;
  1250. goto out_free;
  1251. }
  1252. memcpy((*new_auth_tok)->session_key.encrypted_key,
  1253. &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
  1254. (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
  1255. (*new_auth_tok)->session_key.flags &=
  1256. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1257. (*new_auth_tok)->session_key.flags |=
  1258. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  1259. (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
  1260. (*new_auth_tok)->flags = 0;
  1261. (*new_auth_tok)->session_key.flags &=
  1262. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  1263. (*new_auth_tok)->session_key.flags &=
  1264. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  1265. list_add(&auth_tok_list_item->list, auth_tok_list);
  1266. goto out;
  1267. out_free:
  1268. (*new_auth_tok) = NULL;
  1269. memset(auth_tok_list_item, 0,
  1270. sizeof(struct ecryptfs_auth_tok_list_item));
  1271. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  1272. auth_tok_list_item);
  1273. out:
  1274. if (rc)
  1275. (*packet_size) = 0;
  1276. return rc;
  1277. }
  1278. /**
  1279. * parse_tag_3_packet
  1280. * @crypt_stat: The cryptographic context to modify based on packet
  1281. * contents.
  1282. * @data: The raw bytes of the packet.
  1283. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  1284. * a new authentication token will be placed at the end
  1285. * of this list for this packet.
  1286. * @new_auth_tok: Pointer to a pointer to memory that this function
  1287. * allocates; sets the memory address of the pointer to
  1288. * NULL on error. This object is added to the
  1289. * auth_tok_list.
  1290. * @packet_size: This function writes the size of the parsed packet
  1291. * into this memory location; zero on error.
  1292. * @max_packet_size: maximum number of bytes to parse
  1293. *
  1294. * Returns zero on success; non-zero on error.
  1295. */
  1296. static int
  1297. parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
  1298. unsigned char *data, struct list_head *auth_tok_list,
  1299. struct ecryptfs_auth_tok **new_auth_tok,
  1300. size_t *packet_size, size_t max_packet_size)
  1301. {
  1302. size_t body_size;
  1303. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1304. size_t length_size;
  1305. int rc = 0;
  1306. (*packet_size) = 0;
  1307. (*new_auth_tok) = NULL;
  1308. /**
  1309. *This format is inspired by OpenPGP; see RFC 2440
  1310. * packet tag 3
  1311. *
  1312. * Tag 3 identifier (1 byte)
  1313. * Max Tag 3 packet size (max 3 bytes)
  1314. * Version (1 byte)
  1315. * Cipher code (1 byte)
  1316. * S2K specifier (1 byte)
  1317. * Hash identifier (1 byte)
  1318. * Salt (ECRYPTFS_SALT_SIZE)
  1319. * Hash iterations (1 byte)
  1320. * Encrypted key (arbitrary)
  1321. *
  1322. * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
  1323. */
  1324. if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
  1325. printk(KERN_ERR "Max packet size too large\n");
  1326. rc = -EINVAL;
  1327. goto out;
  1328. }
  1329. if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
  1330. printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
  1331. ECRYPTFS_TAG_3_PACKET_TYPE);
  1332. rc = -EINVAL;
  1333. goto out;
  1334. }
  1335. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  1336. * at end of function upon failure */
  1337. auth_tok_list_item =
  1338. kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
  1339. if (!auth_tok_list_item) {
  1340. printk(KERN_ERR "Unable to allocate memory\n");
  1341. rc = -ENOMEM;
  1342. goto out;
  1343. }
  1344. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  1345. rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  1346. &length_size);
  1347. if (rc) {
  1348. printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
  1349. rc);
  1350. goto out_free;
  1351. }
  1352. if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
  1353. printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
  1354. rc = -EINVAL;
  1355. goto out_free;
  1356. }
  1357. (*packet_size) += length_size;
  1358. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  1359. printk(KERN_ERR "Packet size exceeds max\n");
  1360. rc = -EINVAL;
  1361. goto out_free;
  1362. }
  1363. (*new_auth_tok)->session_key.encrypted_key_size =
  1364. (body_size - (ECRYPTFS_SALT_SIZE + 5));
  1365. if ((*new_auth_tok)->session_key.encrypted_key_size
  1366. > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
  1367. printk(KERN_WARNING "Tag 3 packet contains key larger "
  1368. "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
  1369. rc = -EINVAL;
  1370. goto out_free;
  1371. }
  1372. if (unlikely(data[(*packet_size)++] != 0x04)) {
  1373. printk(KERN_WARNING "Unknown version number [%d]\n",
  1374. data[(*packet_size) - 1]);
  1375. rc = -EINVAL;
  1376. goto out_free;
  1377. }
  1378. rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher,
  1379. (u16)data[(*packet_size)]);
  1380. if (rc)
  1381. goto out_free;
  1382. /* A little extra work to differentiate among the AES key
  1383. * sizes; see RFC2440 */
  1384. switch(data[(*packet_size)++]) {
  1385. case RFC2440_CIPHER_AES_192:
  1386. crypt_stat->key_size = 24;
  1387. break;
  1388. default:
  1389. crypt_stat->key_size =
  1390. (*new_auth_tok)->session_key.encrypted_key_size;
  1391. }
  1392. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  1393. if (rc)
  1394. goto out_free;
  1395. if (unlikely(data[(*packet_size)++] != 0x03)) {
  1396. printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
  1397. rc = -ENOSYS;
  1398. goto out_free;
  1399. }
  1400. /* TODO: finish the hash mapping */
  1401. switch (data[(*packet_size)++]) {
  1402. case 0x01: /* See RFC2440 for these numbers and their mappings */
  1403. /* Choose MD5 */
  1404. memcpy((*new_auth_tok)->token.password.salt,
  1405. &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
  1406. (*packet_size) += ECRYPTFS_SALT_SIZE;
  1407. /* This conversion was taken straight from RFC2440 */
  1408. (*new_auth_tok)->token.password.hash_iterations =
  1409. ((u32) 16 + (data[(*packet_size)] & 15))
  1410. << ((data[(*packet_size)] >> 4) + 6);
  1411. (*packet_size)++;
  1412. /* Friendly reminder:
  1413. * (*new_auth_tok)->session_key.encrypted_key_size =
  1414. * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
  1415. memcpy((*new_auth_tok)->session_key.encrypted_key,
  1416. &data[(*packet_size)],
  1417. (*new_auth_tok)->session_key.encrypted_key_size);
  1418. (*packet_size) +=
  1419. (*new_auth_tok)->session_key.encrypted_key_size;
  1420. (*new_auth_tok)->session_key.flags &=
  1421. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1422. (*new_auth_tok)->session_key.flags |=
  1423. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  1424. (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
  1425. break;
  1426. default:
  1427. ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
  1428. "[%d]\n", data[(*packet_size) - 1]);
  1429. rc = -ENOSYS;
  1430. goto out_free;
  1431. }
  1432. (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
  1433. /* TODO: Parametarize; we might actually want userspace to
  1434. * decrypt the session key. */
  1435. (*new_auth_tok)->session_key.flags &=
  1436. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  1437. (*new_auth_tok)->session_key.flags &=
  1438. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  1439. list_add(&auth_tok_list_item->list, auth_tok_list);
  1440. goto out;
  1441. out_free:
  1442. (*new_auth_tok) = NULL;
  1443. memset(auth_tok_list_item, 0,
  1444. sizeof(struct ecryptfs_auth_tok_list_item));
  1445. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  1446. auth_tok_list_item);
  1447. out:
  1448. if (rc)
  1449. (*packet_size) = 0;
  1450. return rc;
  1451. }
  1452. /**
  1453. * parse_tag_11_packet
  1454. * @data: The raw bytes of the packet
  1455. * @contents: This function writes the data contents of the literal
  1456. * packet into this memory location
  1457. * @max_contents_bytes: The maximum number of bytes that this function
  1458. * is allowed to write into contents
  1459. * @tag_11_contents_size: This function writes the size of the parsed
  1460. * contents into this memory location; zero on
  1461. * error
  1462. * @packet_size: This function writes the size of the parsed packet
  1463. * into this memory location; zero on error
  1464. * @max_packet_size: maximum number of bytes to parse
  1465. *
  1466. * Returns zero on success; non-zero on error.
  1467. */
  1468. static int
  1469. parse_tag_11_packet(unsigned char *data, unsigned char *contents,
  1470. size_t max_contents_bytes, size_t *tag_11_contents_size,
  1471. size_t *packet_size, size_t max_packet_size)
  1472. {
  1473. size_t body_size;
  1474. size_t length_size;
  1475. int rc = 0;
  1476. (*packet_size) = 0;
  1477. (*tag_11_contents_size) = 0;
  1478. /* This format is inspired by OpenPGP; see RFC 2440
  1479. * packet tag 11
  1480. *
  1481. * Tag 11 identifier (1 byte)
  1482. * Max Tag 11 packet size (max 3 bytes)
  1483. * Binary format specifier (1 byte)
  1484. * Filename length (1 byte)
  1485. * Filename ("_CONSOLE") (8 bytes)
  1486. * Modification date (4 bytes)
  1487. * Literal data (arbitrary)
  1488. *
  1489. * We need at least 16 bytes of data for the packet to even be
  1490. * valid.
  1491. */
  1492. if (max_packet_size < 16) {
  1493. printk(KERN_ERR "Maximum packet size too small\n");
  1494. rc = -EINVAL;
  1495. goto out;
  1496. }
  1497. if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
  1498. printk(KERN_WARNING "Invalid tag 11 packet format\n");
  1499. rc = -EINVAL;
  1500. goto out;
  1501. }
  1502. rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  1503. &length_size);
  1504. if (rc) {
  1505. printk(KERN_WARNING "Invalid tag 11 packet format\n");
  1506. goto out;
  1507. }
  1508. if (body_size < 14) {
  1509. printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
  1510. rc = -EINVAL;
  1511. goto out;
  1512. }
  1513. (*packet_size) += length_size;
  1514. (*tag_11_contents_size) = (body_size - 14);
  1515. if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
  1516. printk(KERN_ERR "Packet size exceeds max\n");
  1517. rc = -EINVAL;
  1518. goto out;
  1519. }
  1520. if (unlikely((*tag_11_contents_size) > max_contents_bytes)) {
  1521. printk(KERN_ERR "Literal data section in tag 11 packet exceeds "
  1522. "expected size\n");
  1523. rc = -EINVAL;
  1524. goto out;
  1525. }
  1526. if (data[(*packet_size)++] != 0x62) {
  1527. printk(KERN_WARNING "Unrecognizable packet\n");
  1528. rc = -EINVAL;
  1529. goto out;
  1530. }
  1531. if (data[(*packet_size)++] != 0x08) {
  1532. printk(KERN_WARNING "Unrecognizable packet\n");
  1533. rc = -EINVAL;
  1534. goto out;
  1535. }
  1536. (*packet_size) += 12; /* Ignore filename and modification date */
  1537. memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
  1538. (*packet_size) += (*tag_11_contents_size);
  1539. out:
  1540. if (rc) {
  1541. (*packet_size) = 0;
  1542. (*tag_11_contents_size) = 0;
  1543. }
  1544. return rc;
  1545. }
  1546. int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
  1547. struct ecryptfs_auth_tok **auth_tok,
  1548. char *sig)
  1549. {
  1550. int rc = 0;
  1551. (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
  1552. if (IS_ERR(*auth_tok_key)) {
  1553. (*auth_tok_key) = ecryptfs_get_encrypted_key(sig);
  1554. if (IS_ERR(*auth_tok_key)) {
  1555. printk(KERN_ERR "Could not find key with description: [%s]\n",
  1556. sig);
  1557. rc = process_request_key_err(PTR_ERR(*auth_tok_key));
  1558. (*auth_tok_key) = NULL;
  1559. goto out;
  1560. }
  1561. }
  1562. down_write(&(*auth_tok_key)->sem);
  1563. rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok);
  1564. if (rc) {
  1565. up_write(&(*auth_tok_key)->sem);
  1566. key_put(*auth_tok_key);
  1567. (*auth_tok_key) = NULL;
  1568. goto out;
  1569. }
  1570. out:
  1571. return rc;
  1572. }
  1573. /**
  1574. * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
  1575. * @auth_tok: The passphrase authentication token to use to encrypt the FEK
  1576. * @crypt_stat: The cryptographic context
  1577. *
  1578. * Returns zero on success; non-zero error otherwise
  1579. */
  1580. static int
  1581. decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  1582. struct ecryptfs_crypt_stat *crypt_stat)
  1583. {
  1584. struct scatterlist dst_sg[2];
  1585. struct scatterlist src_sg[2];
  1586. struct mutex *tfm_mutex;
  1587. struct crypto_skcipher *tfm;
  1588. struct skcipher_request *req = NULL;
  1589. int rc = 0;
  1590. if (unlikely(ecryptfs_verbosity > 0)) {
  1591. ecryptfs_printk(
  1592. KERN_DEBUG, "Session key encryption key (size [%d]):\n",
  1593. auth_tok->token.password.session_key_encryption_key_bytes);
  1594. ecryptfs_dump_hex(
  1595. auth_tok->token.password.session_key_encryption_key,
  1596. auth_tok->token.password.session_key_encryption_key_bytes);
  1597. }
  1598. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
  1599. crypt_stat->cipher);
  1600. if (unlikely(rc)) {
  1601. printk(KERN_ERR "Internal error whilst attempting to get "
  1602. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1603. crypt_stat->cipher, rc);
  1604. goto out;
  1605. }
  1606. rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
  1607. auth_tok->session_key.encrypted_key_size,
  1608. src_sg, 2);
  1609. if (rc < 1 || rc > 2) {
  1610. printk(KERN_ERR "Internal error whilst attempting to convert "
  1611. "auth_tok->session_key.encrypted_key to scatterlist; "
  1612. "expected rc = 1; got rc = [%d]. "
  1613. "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
  1614. auth_tok->session_key.encrypted_key_size);
  1615. goto out;
  1616. }
  1617. auth_tok->session_key.decrypted_key_size =
  1618. auth_tok->session_key.encrypted_key_size;
  1619. rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
  1620. auth_tok->session_key.decrypted_key_size,
  1621. dst_sg, 2);
  1622. if (rc < 1 || rc > 2) {
  1623. printk(KERN_ERR "Internal error whilst attempting to convert "
  1624. "auth_tok->session_key.decrypted_key to scatterlist; "
  1625. "expected rc = 1; got rc = [%d]\n", rc);
  1626. goto out;
  1627. }
  1628. mutex_lock(tfm_mutex);
  1629. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  1630. if (!req) {
  1631. mutex_unlock(tfm_mutex);
  1632. printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
  1633. "skcipher_request_alloc for %s\n", __func__,
  1634. crypto_skcipher_driver_name(tfm));
  1635. rc = -ENOMEM;
  1636. goto out;
  1637. }
  1638. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  1639. NULL, NULL);
  1640. rc = crypto_skcipher_setkey(
  1641. tfm, auth_tok->token.password.session_key_encryption_key,
  1642. crypt_stat->key_size);
  1643. if (unlikely(rc < 0)) {
  1644. mutex_unlock(tfm_mutex);
  1645. printk(KERN_ERR "Error setting key for crypto context\n");
  1646. rc = -EINVAL;
  1647. goto out;
  1648. }
  1649. skcipher_request_set_crypt(req, src_sg, dst_sg,
  1650. auth_tok->session_key.encrypted_key_size,
  1651. NULL);
  1652. rc = crypto_skcipher_decrypt(req);
  1653. mutex_unlock(tfm_mutex);
  1654. if (unlikely(rc)) {
  1655. printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
  1656. goto out;
  1657. }
  1658. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1659. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  1660. auth_tok->session_key.decrypted_key_size);
  1661. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  1662. if (unlikely(ecryptfs_verbosity > 0)) {
  1663. ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n",
  1664. crypt_stat->key_size);
  1665. ecryptfs_dump_hex(crypt_stat->key,
  1666. crypt_stat->key_size);
  1667. }
  1668. out:
  1669. skcipher_request_free(req);
  1670. return rc;
  1671. }
  1672. /**
  1673. * ecryptfs_parse_packet_set
  1674. * @crypt_stat: The cryptographic context
  1675. * @src: Virtual address of region of memory containing the packets
  1676. * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
  1677. *
  1678. * Get crypt_stat to have the file's session key if the requisite key
  1679. * is available to decrypt the session key.
  1680. *
  1681. * Returns Zero if a valid authentication token was retrieved and
  1682. * processed; negative value for file not encrypted or for error
  1683. * conditions.
  1684. */
  1685. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  1686. unsigned char *src,
  1687. struct dentry *ecryptfs_dentry)
  1688. {
  1689. size_t i = 0;
  1690. size_t found_auth_tok;
  1691. size_t next_packet_is_auth_tok_packet;
  1692. struct list_head auth_tok_list;
  1693. struct ecryptfs_auth_tok *matching_auth_tok;
  1694. struct ecryptfs_auth_tok *candidate_auth_tok;
  1695. char *candidate_auth_tok_sig;
  1696. size_t packet_size;
  1697. struct ecryptfs_auth_tok *new_auth_tok;
  1698. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  1699. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1700. size_t tag_11_contents_size;
  1701. size_t tag_11_packet_size;
  1702. struct key *auth_tok_key = NULL;
  1703. int rc = 0;
  1704. INIT_LIST_HEAD(&auth_tok_list);
  1705. /* Parse the header to find as many packets as we can; these will be
  1706. * added the our &auth_tok_list */
  1707. next_packet_is_auth_tok_packet = 1;
  1708. while (next_packet_is_auth_tok_packet) {
  1709. size_t max_packet_size = ((PAGE_SIZE - 8) - i);
  1710. switch (src[i]) {
  1711. case ECRYPTFS_TAG_3_PACKET_TYPE:
  1712. rc = parse_tag_3_packet(crypt_stat,
  1713. (unsigned char *)&src[i],
  1714. &auth_tok_list, &new_auth_tok,
  1715. &packet_size, max_packet_size);
  1716. if (rc) {
  1717. ecryptfs_printk(KERN_ERR, "Error parsing "
  1718. "tag 3 packet\n");
  1719. rc = -EIO;
  1720. goto out_wipe_list;
  1721. }
  1722. i += packet_size;
  1723. rc = parse_tag_11_packet((unsigned char *)&src[i],
  1724. sig_tmp_space,
  1725. ECRYPTFS_SIG_SIZE,
  1726. &tag_11_contents_size,
  1727. &tag_11_packet_size,
  1728. max_packet_size);
  1729. if (rc) {
  1730. ecryptfs_printk(KERN_ERR, "No valid "
  1731. "(ecryptfs-specific) literal "
  1732. "packet containing "
  1733. "authentication token "
  1734. "signature found after "
  1735. "tag 3 packet\n");
  1736. rc = -EIO;
  1737. goto out_wipe_list;
  1738. }
  1739. i += tag_11_packet_size;
  1740. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  1741. ecryptfs_printk(KERN_ERR, "Expected "
  1742. "signature of size [%d]; "
  1743. "read size [%zd]\n",
  1744. ECRYPTFS_SIG_SIZE,
  1745. tag_11_contents_size);
  1746. rc = -EIO;
  1747. goto out_wipe_list;
  1748. }
  1749. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  1750. sig_tmp_space, tag_11_contents_size);
  1751. new_auth_tok->token.password.signature[
  1752. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  1753. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1754. break;
  1755. case ECRYPTFS_TAG_1_PACKET_TYPE:
  1756. rc = parse_tag_1_packet(crypt_stat,
  1757. (unsigned char *)&src[i],
  1758. &auth_tok_list, &new_auth_tok,
  1759. &packet_size, max_packet_size);
  1760. if (rc) {
  1761. ecryptfs_printk(KERN_ERR, "Error parsing "
  1762. "tag 1 packet\n");
  1763. rc = -EIO;
  1764. goto out_wipe_list;
  1765. }
  1766. i += packet_size;
  1767. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1768. break;
  1769. case ECRYPTFS_TAG_11_PACKET_TYPE:
  1770. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  1771. "(Tag 11 not allowed by itself)\n");
  1772. rc = -EIO;
  1773. goto out_wipe_list;
  1774. default:
  1775. ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] "
  1776. "of the file header; hex value of "
  1777. "character is [0x%.2x]\n", i, src[i]);
  1778. next_packet_is_auth_tok_packet = 0;
  1779. }
  1780. }
  1781. if (list_empty(&auth_tok_list)) {
  1782. printk(KERN_ERR "The lower file appears to be a non-encrypted "
  1783. "eCryptfs file; this is not supported in this version "
  1784. "of the eCryptfs kernel module\n");
  1785. rc = -EINVAL;
  1786. goto out;
  1787. }
  1788. /* auth_tok_list contains the set of authentication tokens
  1789. * parsed from the metadata. We need to find a matching
  1790. * authentication token that has the secret component(s)
  1791. * necessary to decrypt the EFEK in the auth_tok parsed from
  1792. * the metadata. There may be several potential matches, but
  1793. * just one will be sufficient to decrypt to get the FEK. */
  1794. find_next_matching_auth_tok:
  1795. found_auth_tok = 0;
  1796. list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
  1797. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  1798. if (unlikely(ecryptfs_verbosity > 0)) {
  1799. ecryptfs_printk(KERN_DEBUG,
  1800. "Considering candidate auth tok:\n");
  1801. ecryptfs_dump_auth_tok(candidate_auth_tok);
  1802. }
  1803. rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
  1804. candidate_auth_tok);
  1805. if (rc) {
  1806. printk(KERN_ERR
  1807. "Unrecognized candidate auth tok type: [%d]\n",
  1808. candidate_auth_tok->token_type);
  1809. rc = -EINVAL;
  1810. goto out_wipe_list;
  1811. }
  1812. rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
  1813. &matching_auth_tok,
  1814. crypt_stat->mount_crypt_stat,
  1815. candidate_auth_tok_sig);
  1816. if (!rc) {
  1817. found_auth_tok = 1;
  1818. goto found_matching_auth_tok;
  1819. }
  1820. }
  1821. if (!found_auth_tok) {
  1822. ecryptfs_printk(KERN_ERR, "Could not find a usable "
  1823. "authentication token\n");
  1824. rc = -EIO;
  1825. goto out_wipe_list;
  1826. }
  1827. found_matching_auth_tok:
  1828. if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1829. memcpy(&(candidate_auth_tok->token.private_key),
  1830. &(matching_auth_tok->token.private_key),
  1831. sizeof(struct ecryptfs_private_key));
  1832. up_write(&(auth_tok_key->sem));
  1833. key_put(auth_tok_key);
  1834. rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
  1835. crypt_stat);
  1836. } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1837. memcpy(&(candidate_auth_tok->token.password),
  1838. &(matching_auth_tok->token.password),
  1839. sizeof(struct ecryptfs_password));
  1840. up_write(&(auth_tok_key->sem));
  1841. key_put(auth_tok_key);
  1842. rc = decrypt_passphrase_encrypted_session_key(
  1843. candidate_auth_tok, crypt_stat);
  1844. } else {
  1845. up_write(&(auth_tok_key->sem));
  1846. key_put(auth_tok_key);
  1847. rc = -EINVAL;
  1848. }
  1849. if (rc) {
  1850. struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  1851. ecryptfs_printk(KERN_WARNING, "Error decrypting the "
  1852. "session key for authentication token with sig "
  1853. "[%.*s]; rc = [%d]. Removing auth tok "
  1854. "candidate from the list and searching for "
  1855. "the next match.\n", ECRYPTFS_SIG_SIZE_HEX,
  1856. candidate_auth_tok_sig, rc);
  1857. list_for_each_entry_safe(auth_tok_list_item,
  1858. auth_tok_list_item_tmp,
  1859. &auth_tok_list, list) {
  1860. if (candidate_auth_tok
  1861. == &auth_tok_list_item->auth_tok) {
  1862. list_del(&auth_tok_list_item->list);
  1863. kmem_cache_free(
  1864. ecryptfs_auth_tok_list_item_cache,
  1865. auth_tok_list_item);
  1866. goto find_next_matching_auth_tok;
  1867. }
  1868. }
  1869. BUG();
  1870. }
  1871. rc = ecryptfs_compute_root_iv(crypt_stat);
  1872. if (rc) {
  1873. ecryptfs_printk(KERN_ERR, "Error computing "
  1874. "the root IV\n");
  1875. goto out_wipe_list;
  1876. }
  1877. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  1878. if (rc) {
  1879. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  1880. "context for cipher [%s]; rc = [%d]\n",
  1881. crypt_stat->cipher, rc);
  1882. }
  1883. out_wipe_list:
  1884. wipe_auth_tok_list(&auth_tok_list);
  1885. out:
  1886. return rc;
  1887. }
  1888. static int
  1889. pki_encrypt_session_key(struct key *auth_tok_key,
  1890. struct ecryptfs_auth_tok *auth_tok,
  1891. struct ecryptfs_crypt_stat *crypt_stat,
  1892. struct ecryptfs_key_record *key_rec)
  1893. {
  1894. struct ecryptfs_msg_ctx *msg_ctx = NULL;
  1895. char *payload = NULL;
  1896. size_t payload_len = 0;
  1897. struct ecryptfs_message *msg;
  1898. int rc;
  1899. rc = write_tag_66_packet(auth_tok->token.private_key.signature,
  1900. ecryptfs_code_for_cipher_string(
  1901. crypt_stat->cipher,
  1902. crypt_stat->key_size),
  1903. crypt_stat, &payload, &payload_len);
  1904. up_write(&(auth_tok_key->sem));
  1905. key_put(auth_tok_key);
  1906. if (rc) {
  1907. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
  1908. goto out;
  1909. }
  1910. rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
  1911. if (rc) {
  1912. ecryptfs_printk(KERN_ERR, "Error sending message to "
  1913. "ecryptfsd: %d\n", rc);
  1914. goto out;
  1915. }
  1916. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  1917. if (rc) {
  1918. ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
  1919. "from the user space daemon\n");
  1920. rc = -EIO;
  1921. goto out;
  1922. }
  1923. rc = parse_tag_67_packet(key_rec, msg);
  1924. if (rc)
  1925. ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
  1926. kfree(msg);
  1927. out:
  1928. kfree(payload);
  1929. return rc;
  1930. }
  1931. /**
  1932. * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
  1933. * @dest: Buffer into which to write the packet
  1934. * @remaining_bytes: Maximum number of bytes that can be writtn
  1935. * @auth_tok_key: The authentication token key to unlock and put when done with
  1936. * @auth_tok
  1937. * @auth_tok: The authentication token used for generating the tag 1 packet
  1938. * @crypt_stat: The cryptographic context
  1939. * @key_rec: The key record struct for the tag 1 packet
  1940. * @packet_size: This function will write the number of bytes that end
  1941. * up constituting the packet; set to zero on error
  1942. *
  1943. * Returns zero on success; non-zero on error.
  1944. */
  1945. static int
  1946. write_tag_1_packet(char *dest, size_t *remaining_bytes,
  1947. struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok,
  1948. struct ecryptfs_crypt_stat *crypt_stat,
  1949. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1950. {
  1951. size_t i;
  1952. size_t encrypted_session_key_valid = 0;
  1953. size_t packet_size_length;
  1954. size_t max_packet_size;
  1955. int rc = 0;
  1956. (*packet_size) = 0;
  1957. ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
  1958. ECRYPTFS_SIG_SIZE);
  1959. encrypted_session_key_valid = 0;
  1960. for (i = 0; i < crypt_stat->key_size; i++)
  1961. encrypted_session_key_valid |=
  1962. auth_tok->session_key.encrypted_key[i];
  1963. if (encrypted_session_key_valid) {
  1964. memcpy(key_rec->enc_key,
  1965. auth_tok->session_key.encrypted_key,
  1966. auth_tok->session_key.encrypted_key_size);
  1967. up_write(&(auth_tok_key->sem));
  1968. key_put(auth_tok_key);
  1969. goto encrypted_session_key_set;
  1970. }
  1971. if (auth_tok->session_key.encrypted_key_size == 0)
  1972. auth_tok->session_key.encrypted_key_size =
  1973. auth_tok->token.private_key.key_size;
  1974. rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat,
  1975. key_rec);
  1976. if (rc) {
  1977. printk(KERN_ERR "Failed to encrypt session key via a key "
  1978. "module; rc = [%d]\n", rc);
  1979. goto out;
  1980. }
  1981. if (ecryptfs_verbosity > 0) {
  1982. ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
  1983. ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
  1984. }
  1985. encrypted_session_key_set:
  1986. /* This format is inspired by OpenPGP; see RFC 2440
  1987. * packet tag 1 */
  1988. max_packet_size = (1 /* Tag 1 identifier */
  1989. + 3 /* Max Tag 1 packet size */
  1990. + 1 /* Version */
  1991. + ECRYPTFS_SIG_SIZE /* Key identifier */
  1992. + 1 /* Cipher identifier */
  1993. + key_rec->enc_key_size); /* Encrypted key size */
  1994. if (max_packet_size > (*remaining_bytes)) {
  1995. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1996. "need up to [%td] bytes, but there are only [%td] "
  1997. "available\n", max_packet_size, (*remaining_bytes));
  1998. rc = -EINVAL;
  1999. goto out;
  2000. }
  2001. dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
  2002. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  2003. (max_packet_size - 4),
  2004. &packet_size_length);
  2005. if (rc) {
  2006. ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
  2007. "header; cannot generate packet length\n");
  2008. goto out;
  2009. }
  2010. (*packet_size) += packet_size_length;
  2011. dest[(*packet_size)++] = 0x03; /* version 3 */
  2012. memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
  2013. (*packet_size) += ECRYPTFS_SIG_SIZE;
  2014. dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
  2015. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  2016. key_rec->enc_key_size);
  2017. (*packet_size) += key_rec->enc_key_size;
  2018. out:
  2019. if (rc)
  2020. (*packet_size) = 0;
  2021. else
  2022. (*remaining_bytes) -= (*packet_size);
  2023. return rc;
  2024. }
  2025. /**
  2026. * write_tag_11_packet
  2027. * @dest: Target into which Tag 11 packet is to be written
  2028. * @remaining_bytes: Maximum packet length
  2029. * @contents: Byte array of contents to copy in
  2030. * @contents_length: Number of bytes in contents
  2031. * @packet_length: Length of the Tag 11 packet written; zero on error
  2032. *
  2033. * Returns zero on success; non-zero on error.
  2034. */
  2035. static int
  2036. write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
  2037. size_t contents_length, size_t *packet_length)
  2038. {
  2039. size_t packet_size_length;
  2040. size_t max_packet_size;
  2041. int rc = 0;
  2042. (*packet_length) = 0;
  2043. /* This format is inspired by OpenPGP; see RFC 2440
  2044. * packet tag 11 */
  2045. max_packet_size = (1 /* Tag 11 identifier */
  2046. + 3 /* Max Tag 11 packet size */
  2047. + 1 /* Binary format specifier */
  2048. + 1 /* Filename length */
  2049. + 8 /* Filename ("_CONSOLE") */
  2050. + 4 /* Modification date */
  2051. + contents_length); /* Literal data */
  2052. if (max_packet_size > (*remaining_bytes)) {
  2053. printk(KERN_ERR "Packet length larger than maximum allowable; "
  2054. "need up to [%td] bytes, but there are only [%td] "
  2055. "available\n", max_packet_size, (*remaining_bytes));
  2056. rc = -EINVAL;
  2057. goto out;
  2058. }
  2059. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  2060. rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
  2061. (max_packet_size - 4),
  2062. &packet_size_length);
  2063. if (rc) {
  2064. printk(KERN_ERR "Error generating tag 11 packet header; cannot "
  2065. "generate packet length. rc = [%d]\n", rc);
  2066. goto out;
  2067. }
  2068. (*packet_length) += packet_size_length;
  2069. dest[(*packet_length)++] = 0x62; /* binary data format specifier */
  2070. dest[(*packet_length)++] = 8;
  2071. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  2072. (*packet_length) += 8;
  2073. memset(&dest[(*packet_length)], 0x00, 4);
  2074. (*packet_length) += 4;
  2075. memcpy(&dest[(*packet_length)], contents, contents_length);
  2076. (*packet_length) += contents_length;
  2077. out:
  2078. if (rc)
  2079. (*packet_length) = 0;
  2080. else
  2081. (*remaining_bytes) -= (*packet_length);
  2082. return rc;
  2083. }
  2084. /**
  2085. * write_tag_3_packet
  2086. * @dest: Buffer into which to write the packet
  2087. * @remaining_bytes: Maximum number of bytes that can be written
  2088. * @auth_tok: Authentication token
  2089. * @crypt_stat: The cryptographic context
  2090. * @key_rec: encrypted key
  2091. * @packet_size: This function will write the number of bytes that end
  2092. * up constituting the packet; set to zero on error
  2093. *
  2094. * Returns zero on success; non-zero on error.
  2095. */
  2096. static int
  2097. write_tag_3_packet(char *dest, size_t *remaining_bytes,
  2098. struct ecryptfs_auth_tok *auth_tok,
  2099. struct ecryptfs_crypt_stat *crypt_stat,
  2100. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  2101. {
  2102. size_t i;
  2103. size_t encrypted_session_key_valid = 0;
  2104. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  2105. struct scatterlist dst_sg[2];
  2106. struct scatterlist src_sg[2];
  2107. struct mutex *tfm_mutex = NULL;
  2108. u8 cipher_code;
  2109. size_t packet_size_length;
  2110. size_t max_packet_size;
  2111. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  2112. crypt_stat->mount_crypt_stat;
  2113. struct crypto_skcipher *tfm;
  2114. struct skcipher_request *req;
  2115. int rc = 0;
  2116. (*packet_size) = 0;
  2117. ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
  2118. ECRYPTFS_SIG_SIZE);
  2119. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex,
  2120. crypt_stat->cipher);
  2121. if (unlikely(rc)) {
  2122. printk(KERN_ERR "Internal error whilst attempting to get "
  2123. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  2124. crypt_stat->cipher, rc);
  2125. goto out;
  2126. }
  2127. if (mount_crypt_stat->global_default_cipher_key_size == 0) {
  2128. printk(KERN_WARNING "No key size specified at mount; "
  2129. "defaulting to [%d]\n",
  2130. crypto_skcipher_max_keysize(tfm));
  2131. mount_crypt_stat->global_default_cipher_key_size =
  2132. crypto_skcipher_max_keysize(tfm);
  2133. }
  2134. if (crypt_stat->key_size == 0)
  2135. crypt_stat->key_size =
  2136. mount_crypt_stat->global_default_cipher_key_size;
  2137. if (auth_tok->session_key.encrypted_key_size == 0)
  2138. auth_tok->session_key.encrypted_key_size =
  2139. crypt_stat->key_size;
  2140. if (crypt_stat->key_size == 24
  2141. && strcmp("aes", crypt_stat->cipher) == 0) {
  2142. memset((crypt_stat->key + 24), 0, 8);
  2143. auth_tok->session_key.encrypted_key_size = 32;
  2144. } else
  2145. auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
  2146. key_rec->enc_key_size =
  2147. auth_tok->session_key.encrypted_key_size;
  2148. encrypted_session_key_valid = 0;
  2149. for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
  2150. encrypted_session_key_valid |=
  2151. auth_tok->session_key.encrypted_key[i];
  2152. if (encrypted_session_key_valid) {
  2153. ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
  2154. "using auth_tok->session_key.encrypted_key, "
  2155. "where key_rec->enc_key_size = [%zd]\n",
  2156. key_rec->enc_key_size);
  2157. memcpy(key_rec->enc_key,
  2158. auth_tok->session_key.encrypted_key,
  2159. key_rec->enc_key_size);
  2160. goto encrypted_session_key_set;
  2161. }
  2162. if (auth_tok->token.password.flags &
  2163. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
  2164. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  2165. "session key encryption key of size [%d]\n",
  2166. auth_tok->token.password.
  2167. session_key_encryption_key_bytes);
  2168. memcpy(session_key_encryption_key,
  2169. auth_tok->token.password.session_key_encryption_key,
  2170. crypt_stat->key_size);
  2171. ecryptfs_printk(KERN_DEBUG,
  2172. "Cached session key encryption key:\n");
  2173. if (ecryptfs_verbosity > 0)
  2174. ecryptfs_dump_hex(session_key_encryption_key, 16);
  2175. }
  2176. if (unlikely(ecryptfs_verbosity > 0)) {
  2177. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  2178. ecryptfs_dump_hex(session_key_encryption_key, 16);
  2179. }
  2180. rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
  2181. src_sg, 2);
  2182. if (rc < 1 || rc > 2) {
  2183. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  2184. "for crypt_stat session key; expected rc = 1; "
  2185. "got rc = [%d]. key_rec->enc_key_size = [%zd]\n",
  2186. rc, key_rec->enc_key_size);
  2187. rc = -ENOMEM;
  2188. goto out;
  2189. }
  2190. rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
  2191. dst_sg, 2);
  2192. if (rc < 1 || rc > 2) {
  2193. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  2194. "for crypt_stat encrypted session key; "
  2195. "expected rc = 1; got rc = [%d]. "
  2196. "key_rec->enc_key_size = [%zd]\n", rc,
  2197. key_rec->enc_key_size);
  2198. rc = -ENOMEM;
  2199. goto out;
  2200. }
  2201. mutex_lock(tfm_mutex);
  2202. rc = crypto_skcipher_setkey(tfm, session_key_encryption_key,
  2203. crypt_stat->key_size);
  2204. if (rc < 0) {
  2205. mutex_unlock(tfm_mutex);
  2206. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  2207. "context; rc = [%d]\n", rc);
  2208. goto out;
  2209. }
  2210. req = skcipher_request_alloc(tfm, GFP_KERNEL);
  2211. if (!req) {
  2212. mutex_unlock(tfm_mutex);
  2213. ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst "
  2214. "attempting to skcipher_request_alloc for "
  2215. "%s\n", crypto_skcipher_driver_name(tfm));
  2216. rc = -ENOMEM;
  2217. goto out;
  2218. }
  2219. skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
  2220. NULL, NULL);
  2221. rc = 0;
  2222. ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n",
  2223. crypt_stat->key_size);
  2224. skcipher_request_set_crypt(req, src_sg, dst_sg,
  2225. (*key_rec).enc_key_size, NULL);
  2226. rc = crypto_skcipher_encrypt(req);
  2227. mutex_unlock(tfm_mutex);
  2228. skcipher_request_free(req);
  2229. if (rc) {
  2230. printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
  2231. goto out;
  2232. }
  2233. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  2234. if (ecryptfs_verbosity > 0) {
  2235. ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n",
  2236. key_rec->enc_key_size);
  2237. ecryptfs_dump_hex(key_rec->enc_key,
  2238. key_rec->enc_key_size);
  2239. }
  2240. encrypted_session_key_set:
  2241. /* This format is inspired by OpenPGP; see RFC 2440
  2242. * packet tag 3 */
  2243. max_packet_size = (1 /* Tag 3 identifier */
  2244. + 3 /* Max Tag 3 packet size */
  2245. + 1 /* Version */
  2246. + 1 /* Cipher code */
  2247. + 1 /* S2K specifier */
  2248. + 1 /* Hash identifier */
  2249. + ECRYPTFS_SALT_SIZE /* Salt */
  2250. + 1 /* Hash iterations */
  2251. + key_rec->enc_key_size); /* Encrypted key size */
  2252. if (max_packet_size > (*remaining_bytes)) {
  2253. printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
  2254. "there are only [%td] available\n", max_packet_size,
  2255. (*remaining_bytes));
  2256. rc = -EINVAL;
  2257. goto out;
  2258. }
  2259. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  2260. /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
  2261. * to get the number of octets in the actual Tag 3 packet */
  2262. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  2263. (max_packet_size - 4),
  2264. &packet_size_length);
  2265. if (rc) {
  2266. printk(KERN_ERR "Error generating tag 3 packet header; cannot "
  2267. "generate packet length. rc = [%d]\n", rc);
  2268. goto out;
  2269. }
  2270. (*packet_size) += packet_size_length;
  2271. dest[(*packet_size)++] = 0x04; /* version 4 */
  2272. /* TODO: Break from RFC2440 so that arbitrary ciphers can be
  2273. * specified with strings */
  2274. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
  2275. crypt_stat->key_size);
  2276. if (cipher_code == 0) {
  2277. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  2278. "cipher [%s]\n", crypt_stat->cipher);
  2279. rc = -EINVAL;
  2280. goto out;
  2281. }
  2282. dest[(*packet_size)++] = cipher_code;
  2283. dest[(*packet_size)++] = 0x03; /* S2K */
  2284. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  2285. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  2286. ECRYPTFS_SALT_SIZE);
  2287. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  2288. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  2289. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  2290. key_rec->enc_key_size);
  2291. (*packet_size) += key_rec->enc_key_size;
  2292. out:
  2293. if (rc)
  2294. (*packet_size) = 0;
  2295. else
  2296. (*remaining_bytes) -= (*packet_size);
  2297. return rc;
  2298. }
  2299. struct kmem_cache *ecryptfs_key_record_cache;
  2300. /**
  2301. * ecryptfs_generate_key_packet_set
  2302. * @dest_base: Virtual address from which to write the key record set
  2303. * @crypt_stat: The cryptographic context from which the
  2304. * authentication tokens will be retrieved
  2305. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  2306. * for the global parameters
  2307. * @len: The amount written
  2308. * @max: The maximum amount of data allowed to be written
  2309. *
  2310. * Generates a key packet set and writes it to the virtual address
  2311. * passed in.
  2312. *
  2313. * Returns zero on success; non-zero on error.
  2314. */
  2315. int
  2316. ecryptfs_generate_key_packet_set(char *dest_base,
  2317. struct ecryptfs_crypt_stat *crypt_stat,
  2318. struct dentry *ecryptfs_dentry, size_t *len,
  2319. size_t max)
  2320. {
  2321. struct ecryptfs_auth_tok *auth_tok;
  2322. struct key *auth_tok_key = NULL;
  2323. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  2324. &ecryptfs_superblock_to_private(
  2325. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  2326. size_t written;
  2327. struct ecryptfs_key_record *key_rec;
  2328. struct ecryptfs_key_sig *key_sig;
  2329. int rc = 0;
  2330. (*len) = 0;
  2331. mutex_lock(&crypt_stat->keysig_list_mutex);
  2332. key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
  2333. if (!key_rec) {
  2334. rc = -ENOMEM;
  2335. goto out;
  2336. }
  2337. list_for_each_entry(key_sig, &crypt_stat->keysig_list,
  2338. crypt_stat_list) {
  2339. memset(key_rec, 0, sizeof(*key_rec));
  2340. rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key,
  2341. &auth_tok,
  2342. mount_crypt_stat,
  2343. key_sig->keysig);
  2344. if (rc) {
  2345. printk(KERN_WARNING "Unable to retrieve auth tok with "
  2346. "sig = [%s]\n", key_sig->keysig);
  2347. rc = process_find_global_auth_tok_for_sig_err(rc);
  2348. goto out_free;
  2349. }
  2350. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  2351. rc = write_tag_3_packet((dest_base + (*len)),
  2352. &max, auth_tok,
  2353. crypt_stat, key_rec,
  2354. &written);
  2355. up_write(&(auth_tok_key->sem));
  2356. key_put(auth_tok_key);
  2357. if (rc) {
  2358. ecryptfs_printk(KERN_WARNING, "Error "
  2359. "writing tag 3 packet\n");
  2360. goto out_free;
  2361. }
  2362. (*len) += written;
  2363. /* Write auth tok signature packet */
  2364. rc = write_tag_11_packet((dest_base + (*len)), &max,
  2365. key_rec->sig,
  2366. ECRYPTFS_SIG_SIZE, &written);
  2367. if (rc) {
  2368. ecryptfs_printk(KERN_ERR, "Error writing "
  2369. "auth tok signature packet\n");
  2370. goto out_free;
  2371. }
  2372. (*len) += written;
  2373. } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  2374. rc = write_tag_1_packet(dest_base + (*len), &max,
  2375. auth_tok_key, auth_tok,
  2376. crypt_stat, key_rec, &written);
  2377. if (rc) {
  2378. ecryptfs_printk(KERN_WARNING, "Error "
  2379. "writing tag 1 packet\n");
  2380. goto out_free;
  2381. }
  2382. (*len) += written;
  2383. } else {
  2384. up_write(&(auth_tok_key->sem));
  2385. key_put(auth_tok_key);
  2386. ecryptfs_printk(KERN_WARNING, "Unsupported "
  2387. "authentication token type\n");
  2388. rc = -EINVAL;
  2389. goto out_free;
  2390. }
  2391. }
  2392. if (likely(max > 0)) {
  2393. dest_base[(*len)] = 0x00;
  2394. } else {
  2395. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  2396. rc = -EIO;
  2397. }
  2398. out_free:
  2399. kmem_cache_free(ecryptfs_key_record_cache, key_rec);
  2400. out:
  2401. if (rc)
  2402. (*len) = 0;
  2403. mutex_unlock(&crypt_stat->keysig_list_mutex);
  2404. return rc;
  2405. }
  2406. struct kmem_cache *ecryptfs_key_sig_cache;
  2407. int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  2408. {
  2409. struct ecryptfs_key_sig *new_key_sig;
  2410. new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
  2411. if (!new_key_sig)
  2412. return -ENOMEM;
  2413. memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
  2414. new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  2415. /* Caller must hold keysig_list_mutex */
  2416. list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
  2417. return 0;
  2418. }
  2419. struct kmem_cache *ecryptfs_global_auth_tok_cache;
  2420. int
  2421. ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  2422. char *sig, u32 global_auth_tok_flags)
  2423. {
  2424. struct ecryptfs_global_auth_tok *new_auth_tok;
  2425. new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
  2426. GFP_KERNEL);
  2427. if (!new_auth_tok)
  2428. return -ENOMEM;
  2429. memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
  2430. new_auth_tok->flags = global_auth_tok_flags;
  2431. new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  2432. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  2433. list_add(&new_auth_tok->mount_crypt_stat_list,
  2434. &mount_crypt_stat->global_auth_tok_list);
  2435. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  2436. return 0;
  2437. }