keystore.c 77 KB

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