keystore.c 76 KB

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