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