keystore.c 77 KB

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