keystore.c 79 KB

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