keystore.c 79 KB

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