keystore.c 80 KB

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