keystore.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390
  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. memset(s->block_aligned_filename, 0, s->block_aligned_filename_size);
  719. kfree(s->block_aligned_filename);
  720. out_unlock:
  721. mutex_unlock(s->tfm_mutex);
  722. out:
  723. kfree(s);
  724. return rc;
  725. }
  726. struct ecryptfs_parse_tag_70_packet_silly_stack {
  727. u8 cipher_code;
  728. size_t max_packet_size;
  729. size_t packet_size_len;
  730. size_t parsed_tag_70_packet_size;
  731. size_t block_aligned_filename_size;
  732. size_t block_size;
  733. size_t i;
  734. struct mutex *tfm_mutex;
  735. char *decrypted_filename;
  736. struct ecryptfs_auth_tok *auth_tok;
  737. struct scatterlist src_sg;
  738. struct scatterlist dst_sg;
  739. struct blkcipher_desc desc;
  740. char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1];
  741. char iv[ECRYPTFS_MAX_IV_BYTES];
  742. char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE];
  743. };
  744. /**
  745. * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet
  746. * @filename: This function kmalloc's the memory for the filename
  747. */
  748. int
  749. ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
  750. size_t *packet_size,
  751. struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  752. char *data, size_t max_packet_size)
  753. {
  754. struct ecryptfs_parse_tag_70_packet_silly_stack *s;
  755. int rc = 0;
  756. (*packet_size) = 0;
  757. (*filename_size) = 0;
  758. (*filename) = NULL;
  759. s = kmalloc(sizeof(*s), GFP_KERNEL);
  760. if (!s) {
  761. printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
  762. "[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
  763. goto out;
  764. }
  765. s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  766. if (max_packet_size < (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1)) {
  767. printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
  768. "at least [%d]\n", __func__, max_packet_size,
  769. (1 + 1 + ECRYPTFS_SIG_SIZE + 1 + 1));
  770. rc = -EINVAL;
  771. goto out;
  772. }
  773. /* Octet 0: Tag 70 identifier
  774. * Octets 1-N1: Tag 70 packet size (includes cipher identifier
  775. * and block-aligned encrypted filename size)
  776. * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE)
  777. * Octet N2-N3: Cipher identifier (1 octet)
  778. * Octets N3-N4: Block-aligned encrypted filename
  779. * - Consists of a minimum number of random numbers, a \0
  780. * separator, and then the filename */
  781. if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) {
  782. printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be "
  783. "tag [0x%.2x]\n", __func__,
  784. data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE);
  785. rc = -EINVAL;
  786. goto out;
  787. }
  788. rc = ecryptfs_parse_packet_length(&data[(*packet_size)],
  789. &s->parsed_tag_70_packet_size,
  790. &s->packet_size_len);
  791. if (rc) {
  792. printk(KERN_WARNING "%s: Error parsing packet length; "
  793. "rc = [%d]\n", __func__, rc);
  794. goto out;
  795. }
  796. s->block_aligned_filename_size = (s->parsed_tag_70_packet_size
  797. - ECRYPTFS_SIG_SIZE - 1);
  798. if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size)
  799. > max_packet_size) {
  800. printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet "
  801. "size is [%zd]\n", __func__, max_packet_size,
  802. (1 + s->packet_size_len + 1
  803. + s->block_aligned_filename_size));
  804. rc = -EINVAL;
  805. goto out;
  806. }
  807. (*packet_size) += s->packet_size_len;
  808. ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)],
  809. ECRYPTFS_SIG_SIZE);
  810. s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  811. (*packet_size) += ECRYPTFS_SIG_SIZE;
  812. s->cipher_code = data[(*packet_size)++];
  813. rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code);
  814. if (rc) {
  815. printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n",
  816. __func__, s->cipher_code);
  817. goto out;
  818. }
  819. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm,
  820. &s->tfm_mutex,
  821. s->cipher_string);
  822. if (unlikely(rc)) {
  823. printk(KERN_ERR "Internal error whilst attempting to get "
  824. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  825. s->cipher_string, rc);
  826. goto out;
  827. }
  828. mutex_lock(s->tfm_mutex);
  829. rc = virt_to_scatterlist(&data[(*packet_size)],
  830. s->block_aligned_filename_size, &s->src_sg, 1);
  831. if (rc != 1) {
  832. printk(KERN_ERR "%s: Internal error whilst attempting to "
  833. "convert encrypted filename memory to scatterlist; "
  834. "expected rc = 1; got rc = [%d]. "
  835. "block_aligned_filename_size = [%zd]\n", __func__, rc,
  836. s->block_aligned_filename_size);
  837. goto out_unlock;
  838. }
  839. (*packet_size) += s->block_aligned_filename_size;
  840. s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
  841. GFP_KERNEL);
  842. if (!s->decrypted_filename) {
  843. printk(KERN_ERR "%s: Out of memory whilst attempting to "
  844. "kmalloc [%zd] bytes\n", __func__,
  845. s->block_aligned_filename_size);
  846. rc = -ENOMEM;
  847. goto out_unlock;
  848. }
  849. rc = virt_to_scatterlist(s->decrypted_filename,
  850. s->block_aligned_filename_size, &s->dst_sg, 1);
  851. if (rc != 1) {
  852. printk(KERN_ERR "%s: Internal error whilst attempting to "
  853. "convert decrypted filename memory to scatterlist; "
  854. "expected rc = 1; got rc = [%d]. "
  855. "block_aligned_filename_size = [%zd]\n", __func__, rc,
  856. s->block_aligned_filename_size);
  857. goto out_free_unlock;
  858. }
  859. /* The characters in the first block effectively do the job of
  860. * the IV here, so we just use 0's for the IV. Note the
  861. * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES
  862. * >= ECRYPTFS_MAX_IV_BYTES. */
  863. memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
  864. s->desc.info = s->iv;
  865. rc = ecryptfs_find_auth_tok_for_sig(&s->auth_tok, mount_crypt_stat,
  866. s->fnek_sig_hex);
  867. if (rc) {
  868. printk(KERN_ERR "%s: Error attempting to find auth tok for "
  869. "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
  870. rc);
  871. goto out_free_unlock;
  872. }
  873. /* TODO: Support other key modules than passphrase for
  874. * filename encryption */
  875. BUG_ON(s->auth_tok->token_type != ECRYPTFS_PASSWORD);
  876. rc = crypto_blkcipher_setkey(
  877. s->desc.tfm,
  878. s->auth_tok->token.password.session_key_encryption_key,
  879. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  880. if (rc < 0) {
  881. printk(KERN_ERR "%s: Error setting key for crypto context; "
  882. "rc = [%d]. s->auth_tok->token.password.session_key_"
  883. "encryption_key = [0x%p]; mount_crypt_stat->"
  884. "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
  885. rc,
  886. s->auth_tok->token.password.session_key_encryption_key,
  887. mount_crypt_stat->global_default_fn_cipher_key_bytes);
  888. goto out_free_unlock;
  889. }
  890. rc = crypto_blkcipher_decrypt_iv(&s->desc, &s->dst_sg, &s->src_sg,
  891. s->block_aligned_filename_size);
  892. if (rc) {
  893. printk(KERN_ERR "%s: Error attempting to decrypt filename; "
  894. "rc = [%d]\n", __func__, rc);
  895. goto out_free_unlock;
  896. }
  897. s->i = 0;
  898. while (s->decrypted_filename[s->i] != '\0'
  899. && s->i < s->block_aligned_filename_size)
  900. s->i++;
  901. if (s->i == s->block_aligned_filename_size) {
  902. printk(KERN_WARNING "%s: Invalid tag 70 packet; could not "
  903. "find valid separator between random characters and "
  904. "the filename\n", __func__);
  905. rc = -EINVAL;
  906. goto out_free_unlock;
  907. }
  908. s->i++;
  909. (*filename_size) = (s->block_aligned_filename_size - s->i);
  910. if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) {
  911. printk(KERN_WARNING "%s: Filename size is [%zd], which is "
  912. "invalid\n", __func__, (*filename_size));
  913. rc = -EINVAL;
  914. goto out_free_unlock;
  915. }
  916. (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
  917. if (!(*filename)) {
  918. printk(KERN_ERR "%s: Out of memory whilst attempting to "
  919. "kmalloc [%zd] bytes\n", __func__,
  920. ((*filename_size) + 1));
  921. rc = -ENOMEM;
  922. goto out_free_unlock;
  923. }
  924. memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size));
  925. (*filename)[(*filename_size)] = '\0';
  926. out_free_unlock:
  927. kfree(s->decrypted_filename);
  928. out_unlock:
  929. mutex_unlock(s->tfm_mutex);
  930. out:
  931. if (rc) {
  932. (*packet_size) = 0;
  933. (*filename_size) = 0;
  934. (*filename) = NULL;
  935. }
  936. kfree(s);
  937. return rc;
  938. }
  939. static int
  940. ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
  941. {
  942. int rc = 0;
  943. (*sig) = NULL;
  944. switch (auth_tok->token_type) {
  945. case ECRYPTFS_PASSWORD:
  946. (*sig) = auth_tok->token.password.signature;
  947. break;
  948. case ECRYPTFS_PRIVATE_KEY:
  949. (*sig) = auth_tok->token.private_key.signature;
  950. break;
  951. default:
  952. printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
  953. auth_tok->token_type);
  954. rc = -EINVAL;
  955. }
  956. return rc;
  957. }
  958. /**
  959. * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
  960. * @auth_tok: The key authentication token used to decrypt the session key
  961. * @crypt_stat: The cryptographic context
  962. *
  963. * Returns zero on success; non-zero error otherwise.
  964. */
  965. static int
  966. decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  967. struct ecryptfs_crypt_stat *crypt_stat)
  968. {
  969. u8 cipher_code = 0;
  970. struct ecryptfs_msg_ctx *msg_ctx;
  971. struct ecryptfs_message *msg = NULL;
  972. char *auth_tok_sig;
  973. char *payload;
  974. size_t payload_len;
  975. int rc;
  976. rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
  977. if (rc) {
  978. printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
  979. auth_tok->token_type);
  980. goto out;
  981. }
  982. rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
  983. &payload, &payload_len);
  984. if (rc) {
  985. ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
  986. goto out;
  987. }
  988. rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
  989. if (rc) {
  990. ecryptfs_printk(KERN_ERR, "Error sending message to "
  991. "ecryptfsd\n");
  992. goto out;
  993. }
  994. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  995. if (rc) {
  996. ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
  997. "from the user space daemon\n");
  998. rc = -EIO;
  999. goto out;
  1000. }
  1001. rc = parse_tag_65_packet(&(auth_tok->session_key),
  1002. &cipher_code, msg);
  1003. if (rc) {
  1004. printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
  1005. rc);
  1006. goto out;
  1007. }
  1008. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1009. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  1010. auth_tok->session_key.decrypted_key_size);
  1011. crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
  1012. rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
  1013. if (rc) {
  1014. ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
  1015. cipher_code)
  1016. goto out;
  1017. }
  1018. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  1019. if (ecryptfs_verbosity > 0) {
  1020. ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
  1021. ecryptfs_dump_hex(crypt_stat->key,
  1022. crypt_stat->key_size);
  1023. }
  1024. out:
  1025. if (msg)
  1026. kfree(msg);
  1027. return rc;
  1028. }
  1029. static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
  1030. {
  1031. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1032. struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  1033. list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
  1034. auth_tok_list_head, list) {
  1035. list_del(&auth_tok_list_item->list);
  1036. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  1037. auth_tok_list_item);
  1038. }
  1039. }
  1040. struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
  1041. /**
  1042. * parse_tag_1_packet
  1043. * @crypt_stat: The cryptographic context to modify based on packet contents
  1044. * @data: The raw bytes of the packet.
  1045. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  1046. * a new authentication token will be placed at the
  1047. * end of this list for this packet.
  1048. * @new_auth_tok: Pointer to a pointer to memory that this function
  1049. * allocates; sets the memory address of the pointer to
  1050. * NULL on error. This object is added to the
  1051. * auth_tok_list.
  1052. * @packet_size: This function writes the size of the parsed packet
  1053. * into this memory location; zero on error.
  1054. * @max_packet_size: The maximum allowable packet size
  1055. *
  1056. * Returns zero on success; non-zero on error.
  1057. */
  1058. static int
  1059. parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
  1060. unsigned char *data, struct list_head *auth_tok_list,
  1061. struct ecryptfs_auth_tok **new_auth_tok,
  1062. size_t *packet_size, size_t max_packet_size)
  1063. {
  1064. size_t body_size;
  1065. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1066. size_t length_size;
  1067. int rc = 0;
  1068. (*packet_size) = 0;
  1069. (*new_auth_tok) = NULL;
  1070. /**
  1071. * This format is inspired by OpenPGP; see RFC 2440
  1072. * packet tag 1
  1073. *
  1074. * Tag 1 identifier (1 byte)
  1075. * Max Tag 1 packet size (max 3 bytes)
  1076. * Version (1 byte)
  1077. * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
  1078. * Cipher identifier (1 byte)
  1079. * Encrypted key size (arbitrary)
  1080. *
  1081. * 12 bytes minimum packet size
  1082. */
  1083. if (unlikely(max_packet_size < 12)) {
  1084. printk(KERN_ERR "Invalid max packet size; must be >=12\n");
  1085. rc = -EINVAL;
  1086. goto out;
  1087. }
  1088. if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
  1089. printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
  1090. ECRYPTFS_TAG_1_PACKET_TYPE);
  1091. rc = -EINVAL;
  1092. goto out;
  1093. }
  1094. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  1095. * at end of function upon failure */
  1096. auth_tok_list_item =
  1097. kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
  1098. GFP_KERNEL);
  1099. if (!auth_tok_list_item) {
  1100. printk(KERN_ERR "Unable to allocate memory\n");
  1101. rc = -ENOMEM;
  1102. goto out;
  1103. }
  1104. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  1105. rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  1106. &length_size);
  1107. if (rc) {
  1108. printk(KERN_WARNING "Error parsing packet length; "
  1109. "rc = [%d]\n", rc);
  1110. goto out_free;
  1111. }
  1112. if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
  1113. printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
  1114. rc = -EINVAL;
  1115. goto out_free;
  1116. }
  1117. (*packet_size) += length_size;
  1118. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  1119. printk(KERN_WARNING "Packet size exceeds max\n");
  1120. rc = -EINVAL;
  1121. goto out_free;
  1122. }
  1123. if (unlikely(data[(*packet_size)++] != 0x03)) {
  1124. printk(KERN_WARNING "Unknown version number [%d]\n",
  1125. data[(*packet_size) - 1]);
  1126. rc = -EINVAL;
  1127. goto out_free;
  1128. }
  1129. ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
  1130. &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
  1131. *packet_size += ECRYPTFS_SIG_SIZE;
  1132. /* This byte is skipped because the kernel does not need to
  1133. * know which public key encryption algorithm was used */
  1134. (*packet_size)++;
  1135. (*new_auth_tok)->session_key.encrypted_key_size =
  1136. body_size - (ECRYPTFS_SIG_SIZE + 2);
  1137. if ((*new_auth_tok)->session_key.encrypted_key_size
  1138. > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
  1139. printk(KERN_WARNING "Tag 1 packet contains key larger "
  1140. "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
  1141. rc = -EINVAL;
  1142. goto out;
  1143. }
  1144. memcpy((*new_auth_tok)->session_key.encrypted_key,
  1145. &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
  1146. (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
  1147. (*new_auth_tok)->session_key.flags &=
  1148. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1149. (*new_auth_tok)->session_key.flags |=
  1150. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  1151. (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
  1152. (*new_auth_tok)->flags = 0;
  1153. (*new_auth_tok)->session_key.flags &=
  1154. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  1155. (*new_auth_tok)->session_key.flags &=
  1156. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  1157. list_add(&auth_tok_list_item->list, auth_tok_list);
  1158. goto out;
  1159. out_free:
  1160. (*new_auth_tok) = NULL;
  1161. memset(auth_tok_list_item, 0,
  1162. sizeof(struct ecryptfs_auth_tok_list_item));
  1163. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  1164. auth_tok_list_item);
  1165. out:
  1166. if (rc)
  1167. (*packet_size) = 0;
  1168. return rc;
  1169. }
  1170. /**
  1171. * parse_tag_3_packet
  1172. * @crypt_stat: The cryptographic context to modify based on packet
  1173. * contents.
  1174. * @data: The raw bytes of the packet.
  1175. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  1176. * a new authentication token will be placed at the end
  1177. * of this list for this packet.
  1178. * @new_auth_tok: Pointer to a pointer to memory that this function
  1179. * allocates; sets the memory address of the pointer to
  1180. * NULL on error. This object is added to the
  1181. * auth_tok_list.
  1182. * @packet_size: This function writes the size of the parsed packet
  1183. * into this memory location; zero on error.
  1184. * @max_packet_size: maximum number of bytes to parse
  1185. *
  1186. * Returns zero on success; non-zero on error.
  1187. */
  1188. static int
  1189. parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
  1190. unsigned char *data, struct list_head *auth_tok_list,
  1191. struct ecryptfs_auth_tok **new_auth_tok,
  1192. size_t *packet_size, size_t max_packet_size)
  1193. {
  1194. size_t body_size;
  1195. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1196. size_t length_size;
  1197. int rc = 0;
  1198. (*packet_size) = 0;
  1199. (*new_auth_tok) = NULL;
  1200. /**
  1201. *This format is inspired by OpenPGP; see RFC 2440
  1202. * packet tag 3
  1203. *
  1204. * Tag 3 identifier (1 byte)
  1205. * Max Tag 3 packet size (max 3 bytes)
  1206. * Version (1 byte)
  1207. * Cipher code (1 byte)
  1208. * S2K specifier (1 byte)
  1209. * Hash identifier (1 byte)
  1210. * Salt (ECRYPTFS_SALT_SIZE)
  1211. * Hash iterations (1 byte)
  1212. * Encrypted key (arbitrary)
  1213. *
  1214. * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
  1215. */
  1216. if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
  1217. printk(KERN_ERR "Max packet size too large\n");
  1218. rc = -EINVAL;
  1219. goto out;
  1220. }
  1221. if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
  1222. printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
  1223. ECRYPTFS_TAG_3_PACKET_TYPE);
  1224. rc = -EINVAL;
  1225. goto out;
  1226. }
  1227. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  1228. * at end of function upon failure */
  1229. auth_tok_list_item =
  1230. kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
  1231. if (!auth_tok_list_item) {
  1232. printk(KERN_ERR "Unable to allocate memory\n");
  1233. rc = -ENOMEM;
  1234. goto out;
  1235. }
  1236. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  1237. rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  1238. &length_size);
  1239. if (rc) {
  1240. printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
  1241. rc);
  1242. goto out_free;
  1243. }
  1244. if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
  1245. printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
  1246. rc = -EINVAL;
  1247. goto out_free;
  1248. }
  1249. (*packet_size) += length_size;
  1250. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  1251. printk(KERN_ERR "Packet size exceeds max\n");
  1252. rc = -EINVAL;
  1253. goto out_free;
  1254. }
  1255. (*new_auth_tok)->session_key.encrypted_key_size =
  1256. (body_size - (ECRYPTFS_SALT_SIZE + 5));
  1257. if (unlikely(data[(*packet_size)++] != 0x04)) {
  1258. printk(KERN_WARNING "Unknown version number [%d]\n",
  1259. data[(*packet_size) - 1]);
  1260. rc = -EINVAL;
  1261. goto out_free;
  1262. }
  1263. ecryptfs_cipher_code_to_string(crypt_stat->cipher,
  1264. (u16)data[(*packet_size)]);
  1265. /* A little extra work to differentiate among the AES key
  1266. * sizes; see RFC2440 */
  1267. switch(data[(*packet_size)++]) {
  1268. case RFC2440_CIPHER_AES_192:
  1269. crypt_stat->key_size = 24;
  1270. break;
  1271. default:
  1272. crypt_stat->key_size =
  1273. (*new_auth_tok)->session_key.encrypted_key_size;
  1274. }
  1275. ecryptfs_init_crypt_ctx(crypt_stat);
  1276. if (unlikely(data[(*packet_size)++] != 0x03)) {
  1277. printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
  1278. rc = -ENOSYS;
  1279. goto out_free;
  1280. }
  1281. /* TODO: finish the hash mapping */
  1282. switch (data[(*packet_size)++]) {
  1283. case 0x01: /* See RFC2440 for these numbers and their mappings */
  1284. /* Choose MD5 */
  1285. memcpy((*new_auth_tok)->token.password.salt,
  1286. &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
  1287. (*packet_size) += ECRYPTFS_SALT_SIZE;
  1288. /* This conversion was taken straight from RFC2440 */
  1289. (*new_auth_tok)->token.password.hash_iterations =
  1290. ((u32) 16 + (data[(*packet_size)] & 15))
  1291. << ((data[(*packet_size)] >> 4) + 6);
  1292. (*packet_size)++;
  1293. /* Friendly reminder:
  1294. * (*new_auth_tok)->session_key.encrypted_key_size =
  1295. * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
  1296. memcpy((*new_auth_tok)->session_key.encrypted_key,
  1297. &data[(*packet_size)],
  1298. (*new_auth_tok)->session_key.encrypted_key_size);
  1299. (*packet_size) +=
  1300. (*new_auth_tok)->session_key.encrypted_key_size;
  1301. (*new_auth_tok)->session_key.flags &=
  1302. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1303. (*new_auth_tok)->session_key.flags |=
  1304. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  1305. (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
  1306. break;
  1307. default:
  1308. ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
  1309. "[%d]\n", data[(*packet_size) - 1]);
  1310. rc = -ENOSYS;
  1311. goto out_free;
  1312. }
  1313. (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
  1314. /* TODO: Parametarize; we might actually want userspace to
  1315. * decrypt the session key. */
  1316. (*new_auth_tok)->session_key.flags &=
  1317. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  1318. (*new_auth_tok)->session_key.flags &=
  1319. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  1320. list_add(&auth_tok_list_item->list, auth_tok_list);
  1321. goto out;
  1322. out_free:
  1323. (*new_auth_tok) = NULL;
  1324. memset(auth_tok_list_item, 0,
  1325. sizeof(struct ecryptfs_auth_tok_list_item));
  1326. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  1327. auth_tok_list_item);
  1328. out:
  1329. if (rc)
  1330. (*packet_size) = 0;
  1331. return rc;
  1332. }
  1333. /**
  1334. * parse_tag_11_packet
  1335. * @data: The raw bytes of the packet
  1336. * @contents: This function writes the data contents of the literal
  1337. * packet into this memory location
  1338. * @max_contents_bytes: The maximum number of bytes that this function
  1339. * is allowed to write into contents
  1340. * @tag_11_contents_size: This function writes the size of the parsed
  1341. * contents into this memory location; zero on
  1342. * error
  1343. * @packet_size: This function writes the size of the parsed packet
  1344. * into this memory location; zero on error
  1345. * @max_packet_size: maximum number of bytes to parse
  1346. *
  1347. * Returns zero on success; non-zero on error.
  1348. */
  1349. static int
  1350. parse_tag_11_packet(unsigned char *data, unsigned char *contents,
  1351. size_t max_contents_bytes, size_t *tag_11_contents_size,
  1352. size_t *packet_size, size_t max_packet_size)
  1353. {
  1354. size_t body_size;
  1355. size_t length_size;
  1356. int rc = 0;
  1357. (*packet_size) = 0;
  1358. (*tag_11_contents_size) = 0;
  1359. /* This format is inspired by OpenPGP; see RFC 2440
  1360. * packet tag 11
  1361. *
  1362. * Tag 11 identifier (1 byte)
  1363. * Max Tag 11 packet size (max 3 bytes)
  1364. * Binary format specifier (1 byte)
  1365. * Filename length (1 byte)
  1366. * Filename ("_CONSOLE") (8 bytes)
  1367. * Modification date (4 bytes)
  1368. * Literal data (arbitrary)
  1369. *
  1370. * We need at least 16 bytes of data for the packet to even be
  1371. * valid.
  1372. */
  1373. if (max_packet_size < 16) {
  1374. printk(KERN_ERR "Maximum packet size too small\n");
  1375. rc = -EINVAL;
  1376. goto out;
  1377. }
  1378. if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
  1379. printk(KERN_WARNING "Invalid tag 11 packet format\n");
  1380. rc = -EINVAL;
  1381. goto out;
  1382. }
  1383. rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
  1384. &length_size);
  1385. if (rc) {
  1386. printk(KERN_WARNING "Invalid tag 11 packet format\n");
  1387. goto out;
  1388. }
  1389. if (body_size < 14) {
  1390. printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
  1391. rc = -EINVAL;
  1392. goto out;
  1393. }
  1394. (*packet_size) += length_size;
  1395. (*tag_11_contents_size) = (body_size - 14);
  1396. if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
  1397. printk(KERN_ERR "Packet size exceeds max\n");
  1398. rc = -EINVAL;
  1399. goto out;
  1400. }
  1401. if (data[(*packet_size)++] != 0x62) {
  1402. printk(KERN_WARNING "Unrecognizable packet\n");
  1403. rc = -EINVAL;
  1404. goto out;
  1405. }
  1406. if (data[(*packet_size)++] != 0x08) {
  1407. printk(KERN_WARNING "Unrecognizable packet\n");
  1408. rc = -EINVAL;
  1409. goto out;
  1410. }
  1411. (*packet_size) += 12; /* Ignore filename and modification date */
  1412. memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
  1413. (*packet_size) += (*tag_11_contents_size);
  1414. out:
  1415. if (rc) {
  1416. (*packet_size) = 0;
  1417. (*tag_11_contents_size) = 0;
  1418. }
  1419. return rc;
  1420. }
  1421. /**
  1422. * ecryptfs_verify_version
  1423. * @version: The version number to confirm
  1424. *
  1425. * Returns zero on good version; non-zero otherwise
  1426. */
  1427. static int ecryptfs_verify_version(u16 version)
  1428. {
  1429. int rc = 0;
  1430. unsigned char major;
  1431. unsigned char minor;
  1432. major = ((version >> 8) & 0xFF);
  1433. minor = (version & 0xFF);
  1434. if (major != ECRYPTFS_VERSION_MAJOR) {
  1435. ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
  1436. "Expected [%d]; got [%d]\n",
  1437. ECRYPTFS_VERSION_MAJOR, major);
  1438. rc = -EINVAL;
  1439. goto out;
  1440. }
  1441. if (minor != ECRYPTFS_VERSION_MINOR) {
  1442. ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
  1443. "Expected [%d]; got [%d]\n",
  1444. ECRYPTFS_VERSION_MINOR, minor);
  1445. rc = -EINVAL;
  1446. goto out;
  1447. }
  1448. out:
  1449. return rc;
  1450. }
  1451. int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
  1452. struct ecryptfs_auth_tok **auth_tok,
  1453. char *sig)
  1454. {
  1455. int rc = 0;
  1456. (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
  1457. if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
  1458. printk(KERN_ERR "Could not find key with description: [%s]\n",
  1459. sig);
  1460. rc = process_request_key_err(PTR_ERR(*auth_tok_key));
  1461. goto out;
  1462. }
  1463. (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
  1464. if (ecryptfs_verify_version((*auth_tok)->version)) {
  1465. printk(KERN_ERR
  1466. "Data structure version mismatch. "
  1467. "Userspace tools must match eCryptfs "
  1468. "kernel module with major version [%d] "
  1469. "and minor version [%d]\n",
  1470. ECRYPTFS_VERSION_MAJOR,
  1471. ECRYPTFS_VERSION_MINOR);
  1472. rc = -EINVAL;
  1473. goto out;
  1474. }
  1475. if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
  1476. && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
  1477. printk(KERN_ERR "Invalid auth_tok structure "
  1478. "returned from key query\n");
  1479. rc = -EINVAL;
  1480. goto out;
  1481. }
  1482. out:
  1483. return rc;
  1484. }
  1485. /**
  1486. * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
  1487. * @auth_tok: The passphrase authentication token to use to encrypt the FEK
  1488. * @crypt_stat: The cryptographic context
  1489. *
  1490. * Returns zero on success; non-zero error otherwise
  1491. */
  1492. static int
  1493. decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  1494. struct ecryptfs_crypt_stat *crypt_stat)
  1495. {
  1496. struct scatterlist dst_sg[2];
  1497. struct scatterlist src_sg[2];
  1498. struct mutex *tfm_mutex;
  1499. struct blkcipher_desc desc = {
  1500. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1501. };
  1502. int rc = 0;
  1503. if (unlikely(ecryptfs_verbosity > 0)) {
  1504. ecryptfs_printk(
  1505. KERN_DEBUG, "Session key encryption key (size [%d]):\n",
  1506. auth_tok->token.password.session_key_encryption_key_bytes);
  1507. ecryptfs_dump_hex(
  1508. auth_tok->token.password.session_key_encryption_key,
  1509. auth_tok->token.password.session_key_encryption_key_bytes);
  1510. }
  1511. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  1512. crypt_stat->cipher);
  1513. if (unlikely(rc)) {
  1514. printk(KERN_ERR "Internal error whilst attempting to get "
  1515. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1516. crypt_stat->cipher, rc);
  1517. goto out;
  1518. }
  1519. rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
  1520. auth_tok->session_key.encrypted_key_size,
  1521. src_sg, 2);
  1522. if (rc < 1 || rc > 2) {
  1523. printk(KERN_ERR "Internal error whilst attempting to convert "
  1524. "auth_tok->session_key.encrypted_key to scatterlist; "
  1525. "expected rc = 1; got rc = [%d]. "
  1526. "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
  1527. auth_tok->session_key.encrypted_key_size);
  1528. goto out;
  1529. }
  1530. auth_tok->session_key.decrypted_key_size =
  1531. auth_tok->session_key.encrypted_key_size;
  1532. rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
  1533. auth_tok->session_key.decrypted_key_size,
  1534. dst_sg, 2);
  1535. if (rc < 1 || rc > 2) {
  1536. printk(KERN_ERR "Internal error whilst attempting to convert "
  1537. "auth_tok->session_key.decrypted_key to scatterlist; "
  1538. "expected rc = 1; got rc = [%d]\n", rc);
  1539. goto out;
  1540. }
  1541. mutex_lock(tfm_mutex);
  1542. rc = crypto_blkcipher_setkey(
  1543. desc.tfm, auth_tok->token.password.session_key_encryption_key,
  1544. crypt_stat->key_size);
  1545. if (unlikely(rc < 0)) {
  1546. mutex_unlock(tfm_mutex);
  1547. printk(KERN_ERR "Error setting key for crypto context\n");
  1548. rc = -EINVAL;
  1549. goto out;
  1550. }
  1551. rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
  1552. auth_tok->session_key.encrypted_key_size);
  1553. mutex_unlock(tfm_mutex);
  1554. if (unlikely(rc)) {
  1555. printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
  1556. goto out;
  1557. }
  1558. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1559. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  1560. auth_tok->session_key.decrypted_key_size);
  1561. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  1562. if (unlikely(ecryptfs_verbosity > 0)) {
  1563. ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
  1564. crypt_stat->key_size);
  1565. ecryptfs_dump_hex(crypt_stat->key,
  1566. crypt_stat->key_size);
  1567. }
  1568. out:
  1569. return rc;
  1570. }
  1571. /**
  1572. * ecryptfs_parse_packet_set
  1573. * @crypt_stat: The cryptographic context
  1574. * @src: Virtual address of region of memory containing the packets
  1575. * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
  1576. *
  1577. * Get crypt_stat to have the file's session key if the requisite key
  1578. * is available to decrypt the session key.
  1579. *
  1580. * Returns Zero if a valid authentication token was retrieved and
  1581. * processed; negative value for file not encrypted or for error
  1582. * conditions.
  1583. */
  1584. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  1585. unsigned char *src,
  1586. struct dentry *ecryptfs_dentry)
  1587. {
  1588. size_t i = 0;
  1589. size_t found_auth_tok;
  1590. size_t next_packet_is_auth_tok_packet;
  1591. struct list_head auth_tok_list;
  1592. struct ecryptfs_auth_tok *matching_auth_tok;
  1593. struct ecryptfs_auth_tok *candidate_auth_tok;
  1594. char *candidate_auth_tok_sig;
  1595. size_t packet_size;
  1596. struct ecryptfs_auth_tok *new_auth_tok;
  1597. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  1598. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1599. size_t tag_11_contents_size;
  1600. size_t tag_11_packet_size;
  1601. int rc = 0;
  1602. INIT_LIST_HEAD(&auth_tok_list);
  1603. /* Parse the header to find as many packets as we can; these will be
  1604. * added the our &auth_tok_list */
  1605. next_packet_is_auth_tok_packet = 1;
  1606. while (next_packet_is_auth_tok_packet) {
  1607. size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
  1608. switch (src[i]) {
  1609. case ECRYPTFS_TAG_3_PACKET_TYPE:
  1610. rc = parse_tag_3_packet(crypt_stat,
  1611. (unsigned char *)&src[i],
  1612. &auth_tok_list, &new_auth_tok,
  1613. &packet_size, max_packet_size);
  1614. if (rc) {
  1615. ecryptfs_printk(KERN_ERR, "Error parsing "
  1616. "tag 3 packet\n");
  1617. rc = -EIO;
  1618. goto out_wipe_list;
  1619. }
  1620. i += packet_size;
  1621. rc = parse_tag_11_packet((unsigned char *)&src[i],
  1622. sig_tmp_space,
  1623. ECRYPTFS_SIG_SIZE,
  1624. &tag_11_contents_size,
  1625. &tag_11_packet_size,
  1626. max_packet_size);
  1627. if (rc) {
  1628. ecryptfs_printk(KERN_ERR, "No valid "
  1629. "(ecryptfs-specific) literal "
  1630. "packet containing "
  1631. "authentication token "
  1632. "signature found after "
  1633. "tag 3 packet\n");
  1634. rc = -EIO;
  1635. goto out_wipe_list;
  1636. }
  1637. i += tag_11_packet_size;
  1638. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  1639. ecryptfs_printk(KERN_ERR, "Expected "
  1640. "signature of size [%d]; "
  1641. "read size [%d]\n",
  1642. ECRYPTFS_SIG_SIZE,
  1643. tag_11_contents_size);
  1644. rc = -EIO;
  1645. goto out_wipe_list;
  1646. }
  1647. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  1648. sig_tmp_space, tag_11_contents_size);
  1649. new_auth_tok->token.password.signature[
  1650. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  1651. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1652. break;
  1653. case ECRYPTFS_TAG_1_PACKET_TYPE:
  1654. rc = parse_tag_1_packet(crypt_stat,
  1655. (unsigned char *)&src[i],
  1656. &auth_tok_list, &new_auth_tok,
  1657. &packet_size, max_packet_size);
  1658. if (rc) {
  1659. ecryptfs_printk(KERN_ERR, "Error parsing "
  1660. "tag 1 packet\n");
  1661. rc = -EIO;
  1662. goto out_wipe_list;
  1663. }
  1664. i += packet_size;
  1665. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1666. break;
  1667. case ECRYPTFS_TAG_11_PACKET_TYPE:
  1668. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  1669. "(Tag 11 not allowed by itself)\n");
  1670. rc = -EIO;
  1671. goto out_wipe_list;
  1672. break;
  1673. default:
  1674. ecryptfs_printk(KERN_DEBUG, "No packet at offset "
  1675. "[%d] of the file header; hex value of "
  1676. "character is [0x%.2x]\n", i, src[i]);
  1677. next_packet_is_auth_tok_packet = 0;
  1678. }
  1679. }
  1680. if (list_empty(&auth_tok_list)) {
  1681. printk(KERN_ERR "The lower file appears to be a non-encrypted "
  1682. "eCryptfs file; this is not supported in this version "
  1683. "of the eCryptfs kernel module\n");
  1684. rc = -EINVAL;
  1685. goto out;
  1686. }
  1687. /* auth_tok_list contains the set of authentication tokens
  1688. * parsed from the metadata. We need to find a matching
  1689. * authentication token that has the secret component(s)
  1690. * necessary to decrypt the EFEK in the auth_tok parsed from
  1691. * the metadata. There may be several potential matches, but
  1692. * just one will be sufficient to decrypt to get the FEK. */
  1693. find_next_matching_auth_tok:
  1694. found_auth_tok = 0;
  1695. list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
  1696. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  1697. if (unlikely(ecryptfs_verbosity > 0)) {
  1698. ecryptfs_printk(KERN_DEBUG,
  1699. "Considering cadidate auth tok:\n");
  1700. ecryptfs_dump_auth_tok(candidate_auth_tok);
  1701. }
  1702. rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
  1703. candidate_auth_tok);
  1704. if (rc) {
  1705. printk(KERN_ERR
  1706. "Unrecognized candidate auth tok type: [%d]\n",
  1707. candidate_auth_tok->token_type);
  1708. rc = -EINVAL;
  1709. goto out_wipe_list;
  1710. }
  1711. ecryptfs_find_auth_tok_for_sig(&matching_auth_tok,
  1712. crypt_stat->mount_crypt_stat,
  1713. candidate_auth_tok_sig);
  1714. if (matching_auth_tok) {
  1715. found_auth_tok = 1;
  1716. goto found_matching_auth_tok;
  1717. }
  1718. }
  1719. if (!found_auth_tok) {
  1720. ecryptfs_printk(KERN_ERR, "Could not find a usable "
  1721. "authentication token\n");
  1722. rc = -EIO;
  1723. goto out_wipe_list;
  1724. }
  1725. found_matching_auth_tok:
  1726. if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1727. memcpy(&(candidate_auth_tok->token.private_key),
  1728. &(matching_auth_tok->token.private_key),
  1729. sizeof(struct ecryptfs_private_key));
  1730. rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
  1731. crypt_stat);
  1732. } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1733. memcpy(&(candidate_auth_tok->token.password),
  1734. &(matching_auth_tok->token.password),
  1735. sizeof(struct ecryptfs_password));
  1736. rc = decrypt_passphrase_encrypted_session_key(
  1737. candidate_auth_tok, crypt_stat);
  1738. }
  1739. if (rc) {
  1740. struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  1741. ecryptfs_printk(KERN_WARNING, "Error decrypting the "
  1742. "session key for authentication token with sig "
  1743. "[%.*s]; rc = [%d]. Removing auth tok "
  1744. "candidate from the list and searching for "
  1745. "the next match.\n", candidate_auth_tok_sig,
  1746. ECRYPTFS_SIG_SIZE_HEX, rc);
  1747. list_for_each_entry_safe(auth_tok_list_item,
  1748. auth_tok_list_item_tmp,
  1749. &auth_tok_list, list) {
  1750. if (candidate_auth_tok
  1751. == &auth_tok_list_item->auth_tok) {
  1752. list_del(&auth_tok_list_item->list);
  1753. kmem_cache_free(
  1754. ecryptfs_auth_tok_list_item_cache,
  1755. auth_tok_list_item);
  1756. goto find_next_matching_auth_tok;
  1757. }
  1758. }
  1759. BUG();
  1760. }
  1761. rc = ecryptfs_compute_root_iv(crypt_stat);
  1762. if (rc) {
  1763. ecryptfs_printk(KERN_ERR, "Error computing "
  1764. "the root IV\n");
  1765. goto out_wipe_list;
  1766. }
  1767. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  1768. if (rc) {
  1769. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  1770. "context for cipher [%s]; rc = [%d]\n",
  1771. crypt_stat->cipher, rc);
  1772. }
  1773. out_wipe_list:
  1774. wipe_auth_tok_list(&auth_tok_list);
  1775. out:
  1776. return rc;
  1777. }
  1778. static int
  1779. pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
  1780. struct ecryptfs_crypt_stat *crypt_stat,
  1781. struct ecryptfs_key_record *key_rec)
  1782. {
  1783. struct ecryptfs_msg_ctx *msg_ctx = NULL;
  1784. char *payload = NULL;
  1785. size_t payload_len;
  1786. struct ecryptfs_message *msg;
  1787. int rc;
  1788. rc = write_tag_66_packet(auth_tok->token.private_key.signature,
  1789. ecryptfs_code_for_cipher_string(
  1790. crypt_stat->cipher,
  1791. crypt_stat->key_size),
  1792. crypt_stat, &payload, &payload_len);
  1793. if (rc) {
  1794. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
  1795. goto out;
  1796. }
  1797. rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
  1798. if (rc) {
  1799. ecryptfs_printk(KERN_ERR, "Error sending message to "
  1800. "ecryptfsd\n");
  1801. goto out;
  1802. }
  1803. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  1804. if (rc) {
  1805. ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
  1806. "from the user space daemon\n");
  1807. rc = -EIO;
  1808. goto out;
  1809. }
  1810. rc = parse_tag_67_packet(key_rec, msg);
  1811. if (rc)
  1812. ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
  1813. kfree(msg);
  1814. out:
  1815. kfree(payload);
  1816. return rc;
  1817. }
  1818. /**
  1819. * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
  1820. * @dest: Buffer into which to write the packet
  1821. * @remaining_bytes: Maximum number of bytes that can be writtn
  1822. * @auth_tok: The authentication token used for generating the tag 1 packet
  1823. * @crypt_stat: The cryptographic context
  1824. * @key_rec: The key record struct for the tag 1 packet
  1825. * @packet_size: This function will write the number of bytes that end
  1826. * up constituting the packet; set to zero on error
  1827. *
  1828. * Returns zero on success; non-zero on error.
  1829. */
  1830. static int
  1831. write_tag_1_packet(char *dest, size_t *remaining_bytes,
  1832. struct ecryptfs_auth_tok *auth_tok,
  1833. struct ecryptfs_crypt_stat *crypt_stat,
  1834. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1835. {
  1836. size_t i;
  1837. size_t encrypted_session_key_valid = 0;
  1838. size_t packet_size_length;
  1839. size_t max_packet_size;
  1840. int rc = 0;
  1841. (*packet_size) = 0;
  1842. ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
  1843. ECRYPTFS_SIG_SIZE);
  1844. encrypted_session_key_valid = 0;
  1845. for (i = 0; i < crypt_stat->key_size; i++)
  1846. encrypted_session_key_valid |=
  1847. auth_tok->session_key.encrypted_key[i];
  1848. if (encrypted_session_key_valid) {
  1849. memcpy(key_rec->enc_key,
  1850. auth_tok->session_key.encrypted_key,
  1851. auth_tok->session_key.encrypted_key_size);
  1852. goto encrypted_session_key_set;
  1853. }
  1854. if (auth_tok->session_key.encrypted_key_size == 0)
  1855. auth_tok->session_key.encrypted_key_size =
  1856. auth_tok->token.private_key.key_size;
  1857. rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
  1858. if (rc) {
  1859. printk(KERN_ERR "Failed to encrypt session key via a key "
  1860. "module; rc = [%d]\n", rc);
  1861. goto out;
  1862. }
  1863. if (ecryptfs_verbosity > 0) {
  1864. ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
  1865. ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
  1866. }
  1867. encrypted_session_key_set:
  1868. /* This format is inspired by OpenPGP; see RFC 2440
  1869. * packet tag 1 */
  1870. max_packet_size = (1 /* Tag 1 identifier */
  1871. + 3 /* Max Tag 1 packet size */
  1872. + 1 /* Version */
  1873. + ECRYPTFS_SIG_SIZE /* Key identifier */
  1874. + 1 /* Cipher identifier */
  1875. + key_rec->enc_key_size); /* Encrypted key size */
  1876. if (max_packet_size > (*remaining_bytes)) {
  1877. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1878. "need up to [%td] bytes, but there are only [%td] "
  1879. "available\n", max_packet_size, (*remaining_bytes));
  1880. rc = -EINVAL;
  1881. goto out;
  1882. }
  1883. dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
  1884. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  1885. (max_packet_size - 4),
  1886. &packet_size_length);
  1887. if (rc) {
  1888. ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
  1889. "header; cannot generate packet length\n");
  1890. goto out;
  1891. }
  1892. (*packet_size) += packet_size_length;
  1893. dest[(*packet_size)++] = 0x03; /* version 3 */
  1894. memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
  1895. (*packet_size) += ECRYPTFS_SIG_SIZE;
  1896. dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
  1897. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1898. key_rec->enc_key_size);
  1899. (*packet_size) += key_rec->enc_key_size;
  1900. out:
  1901. if (rc)
  1902. (*packet_size) = 0;
  1903. else
  1904. (*remaining_bytes) -= (*packet_size);
  1905. return rc;
  1906. }
  1907. /**
  1908. * write_tag_11_packet
  1909. * @dest: Target into which Tag 11 packet is to be written
  1910. * @remaining_bytes: Maximum packet length
  1911. * @contents: Byte array of contents to copy in
  1912. * @contents_length: Number of bytes in contents
  1913. * @packet_length: Length of the Tag 11 packet written; zero on error
  1914. *
  1915. * Returns zero on success; non-zero on error.
  1916. */
  1917. static int
  1918. write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
  1919. size_t contents_length, size_t *packet_length)
  1920. {
  1921. size_t packet_size_length;
  1922. size_t max_packet_size;
  1923. int rc = 0;
  1924. (*packet_length) = 0;
  1925. /* This format is inspired by OpenPGP; see RFC 2440
  1926. * packet tag 11 */
  1927. max_packet_size = (1 /* Tag 11 identifier */
  1928. + 3 /* Max Tag 11 packet size */
  1929. + 1 /* Binary format specifier */
  1930. + 1 /* Filename length */
  1931. + 8 /* Filename ("_CONSOLE") */
  1932. + 4 /* Modification date */
  1933. + contents_length); /* Literal data */
  1934. if (max_packet_size > (*remaining_bytes)) {
  1935. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1936. "need up to [%td] bytes, but there are only [%td] "
  1937. "available\n", max_packet_size, (*remaining_bytes));
  1938. rc = -EINVAL;
  1939. goto out;
  1940. }
  1941. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  1942. rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
  1943. (max_packet_size - 4),
  1944. &packet_size_length);
  1945. if (rc) {
  1946. printk(KERN_ERR "Error generating tag 11 packet header; cannot "
  1947. "generate packet length. rc = [%d]\n", rc);
  1948. goto out;
  1949. }
  1950. (*packet_length) += packet_size_length;
  1951. dest[(*packet_length)++] = 0x62; /* binary data format specifier */
  1952. dest[(*packet_length)++] = 8;
  1953. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  1954. (*packet_length) += 8;
  1955. memset(&dest[(*packet_length)], 0x00, 4);
  1956. (*packet_length) += 4;
  1957. memcpy(&dest[(*packet_length)], contents, contents_length);
  1958. (*packet_length) += contents_length;
  1959. out:
  1960. if (rc)
  1961. (*packet_length) = 0;
  1962. else
  1963. (*remaining_bytes) -= (*packet_length);
  1964. return rc;
  1965. }
  1966. /**
  1967. * write_tag_3_packet
  1968. * @dest: Buffer into which to write the packet
  1969. * @remaining_bytes: Maximum number of bytes that can be written
  1970. * @auth_tok: Authentication token
  1971. * @crypt_stat: The cryptographic context
  1972. * @key_rec: encrypted key
  1973. * @packet_size: This function will write the number of bytes that end
  1974. * up constituting the packet; set to zero on error
  1975. *
  1976. * Returns zero on success; non-zero on error.
  1977. */
  1978. static int
  1979. write_tag_3_packet(char *dest, size_t *remaining_bytes,
  1980. struct ecryptfs_auth_tok *auth_tok,
  1981. struct ecryptfs_crypt_stat *crypt_stat,
  1982. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1983. {
  1984. size_t i;
  1985. size_t encrypted_session_key_valid = 0;
  1986. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  1987. struct scatterlist dst_sg[2];
  1988. struct scatterlist src_sg[2];
  1989. struct mutex *tfm_mutex = NULL;
  1990. u8 cipher_code;
  1991. size_t packet_size_length;
  1992. size_t max_packet_size;
  1993. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1994. crypt_stat->mount_crypt_stat;
  1995. struct blkcipher_desc desc = {
  1996. .tfm = NULL,
  1997. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1998. };
  1999. int rc = 0;
  2000. (*packet_size) = 0;
  2001. ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
  2002. ECRYPTFS_SIG_SIZE);
  2003. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  2004. crypt_stat->cipher);
  2005. if (unlikely(rc)) {
  2006. printk(KERN_ERR "Internal error whilst attempting to get "
  2007. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  2008. crypt_stat->cipher, rc);
  2009. goto out;
  2010. }
  2011. if (mount_crypt_stat->global_default_cipher_key_size == 0) {
  2012. struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
  2013. printk(KERN_WARNING "No key size specified at mount; "
  2014. "defaulting to [%d]\n", alg->max_keysize);
  2015. mount_crypt_stat->global_default_cipher_key_size =
  2016. alg->max_keysize;
  2017. }
  2018. if (crypt_stat->key_size == 0)
  2019. crypt_stat->key_size =
  2020. mount_crypt_stat->global_default_cipher_key_size;
  2021. if (auth_tok->session_key.encrypted_key_size == 0)
  2022. auth_tok->session_key.encrypted_key_size =
  2023. crypt_stat->key_size;
  2024. if (crypt_stat->key_size == 24
  2025. && strcmp("aes", crypt_stat->cipher) == 0) {
  2026. memset((crypt_stat->key + 24), 0, 8);
  2027. auth_tok->session_key.encrypted_key_size = 32;
  2028. } else
  2029. auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
  2030. key_rec->enc_key_size =
  2031. auth_tok->session_key.encrypted_key_size;
  2032. encrypted_session_key_valid = 0;
  2033. for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
  2034. encrypted_session_key_valid |=
  2035. auth_tok->session_key.encrypted_key[i];
  2036. if (encrypted_session_key_valid) {
  2037. ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
  2038. "using auth_tok->session_key.encrypted_key, "
  2039. "where key_rec->enc_key_size = [%d]\n",
  2040. key_rec->enc_key_size);
  2041. memcpy(key_rec->enc_key,
  2042. auth_tok->session_key.encrypted_key,
  2043. key_rec->enc_key_size);
  2044. goto encrypted_session_key_set;
  2045. }
  2046. if (auth_tok->token.password.flags &
  2047. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
  2048. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  2049. "session key encryption key of size [%d]\n",
  2050. auth_tok->token.password.
  2051. session_key_encryption_key_bytes);
  2052. memcpy(session_key_encryption_key,
  2053. auth_tok->token.password.session_key_encryption_key,
  2054. crypt_stat->key_size);
  2055. ecryptfs_printk(KERN_DEBUG,
  2056. "Cached session key " "encryption key: \n");
  2057. if (ecryptfs_verbosity > 0)
  2058. ecryptfs_dump_hex(session_key_encryption_key, 16);
  2059. }
  2060. if (unlikely(ecryptfs_verbosity > 0)) {
  2061. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  2062. ecryptfs_dump_hex(session_key_encryption_key, 16);
  2063. }
  2064. rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
  2065. src_sg, 2);
  2066. if (rc < 1 || rc > 2) {
  2067. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  2068. "for crypt_stat session key; expected rc = 1; "
  2069. "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
  2070. rc, key_rec->enc_key_size);
  2071. rc = -ENOMEM;
  2072. goto out;
  2073. }
  2074. rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
  2075. dst_sg, 2);
  2076. if (rc < 1 || rc > 2) {
  2077. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  2078. "for crypt_stat encrypted session key; "
  2079. "expected rc = 1; got rc = [%d]. "
  2080. "key_rec->enc_key_size = [%d]\n", rc,
  2081. key_rec->enc_key_size);
  2082. rc = -ENOMEM;
  2083. goto out;
  2084. }
  2085. mutex_lock(tfm_mutex);
  2086. rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
  2087. crypt_stat->key_size);
  2088. if (rc < 0) {
  2089. mutex_unlock(tfm_mutex);
  2090. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  2091. "context; rc = [%d]\n", rc);
  2092. goto out;
  2093. }
  2094. rc = 0;
  2095. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  2096. crypt_stat->key_size);
  2097. rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,
  2098. (*key_rec).enc_key_size);
  2099. mutex_unlock(tfm_mutex);
  2100. if (rc) {
  2101. printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
  2102. goto out;
  2103. }
  2104. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  2105. if (ecryptfs_verbosity > 0) {
  2106. ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
  2107. key_rec->enc_key_size);
  2108. ecryptfs_dump_hex(key_rec->enc_key,
  2109. key_rec->enc_key_size);
  2110. }
  2111. encrypted_session_key_set:
  2112. /* This format is inspired by OpenPGP; see RFC 2440
  2113. * packet tag 3 */
  2114. max_packet_size = (1 /* Tag 3 identifier */
  2115. + 3 /* Max Tag 3 packet size */
  2116. + 1 /* Version */
  2117. + 1 /* Cipher code */
  2118. + 1 /* S2K specifier */
  2119. + 1 /* Hash identifier */
  2120. + ECRYPTFS_SALT_SIZE /* Salt */
  2121. + 1 /* Hash iterations */
  2122. + key_rec->enc_key_size); /* Encrypted key size */
  2123. if (max_packet_size > (*remaining_bytes)) {
  2124. printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
  2125. "there are only [%td] available\n", max_packet_size,
  2126. (*remaining_bytes));
  2127. rc = -EINVAL;
  2128. goto out;
  2129. }
  2130. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  2131. /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
  2132. * to get the number of octets in the actual Tag 3 packet */
  2133. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  2134. (max_packet_size - 4),
  2135. &packet_size_length);
  2136. if (rc) {
  2137. printk(KERN_ERR "Error generating tag 3 packet header; cannot "
  2138. "generate packet length. rc = [%d]\n", rc);
  2139. goto out;
  2140. }
  2141. (*packet_size) += packet_size_length;
  2142. dest[(*packet_size)++] = 0x04; /* version 4 */
  2143. /* TODO: Break from RFC2440 so that arbitrary ciphers can be
  2144. * specified with strings */
  2145. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher,
  2146. crypt_stat->key_size);
  2147. if (cipher_code == 0) {
  2148. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  2149. "cipher [%s]\n", crypt_stat->cipher);
  2150. rc = -EINVAL;
  2151. goto out;
  2152. }
  2153. dest[(*packet_size)++] = cipher_code;
  2154. dest[(*packet_size)++] = 0x03; /* S2K */
  2155. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  2156. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  2157. ECRYPTFS_SALT_SIZE);
  2158. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  2159. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  2160. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  2161. key_rec->enc_key_size);
  2162. (*packet_size) += key_rec->enc_key_size;
  2163. out:
  2164. if (rc)
  2165. (*packet_size) = 0;
  2166. else
  2167. (*remaining_bytes) -= (*packet_size);
  2168. return rc;
  2169. }
  2170. struct kmem_cache *ecryptfs_key_record_cache;
  2171. /**
  2172. * ecryptfs_generate_key_packet_set
  2173. * @dest_base: Virtual address from which to write the key record set
  2174. * @crypt_stat: The cryptographic context from which the
  2175. * authentication tokens will be retrieved
  2176. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  2177. * for the global parameters
  2178. * @len: The amount written
  2179. * @max: The maximum amount of data allowed to be written
  2180. *
  2181. * Generates a key packet set and writes it to the virtual address
  2182. * passed in.
  2183. *
  2184. * Returns zero on success; non-zero on error.
  2185. */
  2186. int
  2187. ecryptfs_generate_key_packet_set(char *dest_base,
  2188. struct ecryptfs_crypt_stat *crypt_stat,
  2189. struct dentry *ecryptfs_dentry, size_t *len,
  2190. size_t max)
  2191. {
  2192. struct ecryptfs_auth_tok *auth_tok;
  2193. struct ecryptfs_global_auth_tok *global_auth_tok;
  2194. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  2195. &ecryptfs_superblock_to_private(
  2196. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  2197. size_t written;
  2198. struct ecryptfs_key_record *key_rec;
  2199. struct ecryptfs_key_sig *key_sig;
  2200. int rc = 0;
  2201. (*len) = 0;
  2202. mutex_lock(&crypt_stat->keysig_list_mutex);
  2203. key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
  2204. if (!key_rec) {
  2205. rc = -ENOMEM;
  2206. goto out;
  2207. }
  2208. list_for_each_entry(key_sig, &crypt_stat->keysig_list,
  2209. crypt_stat_list) {
  2210. memset(key_rec, 0, sizeof(*key_rec));
  2211. rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
  2212. mount_crypt_stat,
  2213. key_sig->keysig);
  2214. if (rc) {
  2215. printk(KERN_ERR "Error attempting to get the global "
  2216. "auth_tok; rc = [%d]\n", rc);
  2217. goto out_free;
  2218. }
  2219. if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
  2220. printk(KERN_WARNING
  2221. "Skipping invalid auth tok with sig = [%s]\n",
  2222. global_auth_tok->sig);
  2223. continue;
  2224. }
  2225. auth_tok = global_auth_tok->global_auth_tok;
  2226. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  2227. rc = write_tag_3_packet((dest_base + (*len)),
  2228. &max, auth_tok,
  2229. crypt_stat, key_rec,
  2230. &written);
  2231. if (rc) {
  2232. ecryptfs_printk(KERN_WARNING, "Error "
  2233. "writing tag 3 packet\n");
  2234. goto out_free;
  2235. }
  2236. (*len) += written;
  2237. /* Write auth tok signature packet */
  2238. rc = write_tag_11_packet((dest_base + (*len)), &max,
  2239. key_rec->sig,
  2240. ECRYPTFS_SIG_SIZE, &written);
  2241. if (rc) {
  2242. ecryptfs_printk(KERN_ERR, "Error writing "
  2243. "auth tok signature packet\n");
  2244. goto out_free;
  2245. }
  2246. (*len) += written;
  2247. } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  2248. rc = write_tag_1_packet(dest_base + (*len),
  2249. &max, auth_tok,
  2250. crypt_stat, key_rec, &written);
  2251. if (rc) {
  2252. ecryptfs_printk(KERN_WARNING, "Error "
  2253. "writing tag 1 packet\n");
  2254. goto out_free;
  2255. }
  2256. (*len) += written;
  2257. } else {
  2258. ecryptfs_printk(KERN_WARNING, "Unsupported "
  2259. "authentication token type\n");
  2260. rc = -EINVAL;
  2261. goto out_free;
  2262. }
  2263. }
  2264. if (likely(max > 0)) {
  2265. dest_base[(*len)] = 0x00;
  2266. } else {
  2267. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  2268. rc = -EIO;
  2269. }
  2270. out_free:
  2271. kmem_cache_free(ecryptfs_key_record_cache, key_rec);
  2272. out:
  2273. if (rc)
  2274. (*len) = 0;
  2275. mutex_unlock(&crypt_stat->keysig_list_mutex);
  2276. return rc;
  2277. }
  2278. struct kmem_cache *ecryptfs_key_sig_cache;
  2279. int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  2280. {
  2281. struct ecryptfs_key_sig *new_key_sig;
  2282. int rc = 0;
  2283. new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
  2284. if (!new_key_sig) {
  2285. rc = -ENOMEM;
  2286. printk(KERN_ERR
  2287. "Error allocating from ecryptfs_key_sig_cache\n");
  2288. goto out;
  2289. }
  2290. memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
  2291. mutex_lock(&crypt_stat->keysig_list_mutex);
  2292. list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
  2293. mutex_unlock(&crypt_stat->keysig_list_mutex);
  2294. out:
  2295. return rc;
  2296. }
  2297. struct kmem_cache *ecryptfs_global_auth_tok_cache;
  2298. int
  2299. ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  2300. char *sig)
  2301. {
  2302. struct ecryptfs_global_auth_tok *new_auth_tok;
  2303. int rc = 0;
  2304. new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
  2305. GFP_KERNEL);
  2306. if (!new_auth_tok) {
  2307. rc = -ENOMEM;
  2308. printk(KERN_ERR "Error allocating from "
  2309. "ecryptfs_global_auth_tok_cache\n");
  2310. goto out;
  2311. }
  2312. memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
  2313. new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  2314. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  2315. list_add(&new_auth_tok->mount_crypt_stat_list,
  2316. &mount_crypt_stat->global_auth_tok_list);
  2317. mount_crypt_stat->num_global_auth_toks++;
  2318. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  2319. out:
  2320. return rc;
  2321. }