keystore.c 83 KB

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