keystore.c 79 KB

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