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