keystore.c 76 KB

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