keystore.c 79 KB

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