keystore.c 79 KB

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