keystore.c 79 KB

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