keystore.c 77 KB

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