keystore.c 79 KB

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