keystore.c 79 KB

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