keystore.c 80 KB

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