keystore.c 77 KB

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