keystore.c 80 KB

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