keystore.c 79 KB

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