keystore.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. * In-kernel key management code. Includes functions to parse and
  4. * write authentication token-related packets with the underlying
  5. * file.
  6. *
  7. * Copyright (C) 2004-2006 International Business Machines Corp.
  8. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  9. * Michael C. Thompson <mcthomps@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. * 02111-1307, USA.
  25. */
  26. #include <linux/string.h>
  27. #include <linux/sched.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/key.h>
  31. #include <linux/random.h>
  32. #include <linux/crypto.h>
  33. #include <linux/scatterlist.h>
  34. #include "ecryptfs_kernel.h"
  35. /**
  36. * request_key returned an error instead of a valid key address;
  37. * determine the type of error, make appropriate log entries, and
  38. * return an error code.
  39. */
  40. int process_request_key_err(long err_code)
  41. {
  42. int rc = 0;
  43. switch (err_code) {
  44. case ENOKEY:
  45. ecryptfs_printk(KERN_WARNING, "No key\n");
  46. rc = -ENOENT;
  47. break;
  48. case EKEYEXPIRED:
  49. ecryptfs_printk(KERN_WARNING, "Key expired\n");
  50. rc = -ETIME;
  51. break;
  52. case EKEYREVOKED:
  53. ecryptfs_printk(KERN_WARNING, "Key revoked\n");
  54. rc = -EINVAL;
  55. break;
  56. default:
  57. ecryptfs_printk(KERN_WARNING, "Unknown error code: "
  58. "[0x%.16x]\n", err_code);
  59. rc = -EINVAL;
  60. }
  61. return rc;
  62. }
  63. static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
  64. {
  65. struct list_head *walker;
  66. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  67. walker = auth_tok_list_head->next;
  68. while (walker != auth_tok_list_head) {
  69. auth_tok_list_item =
  70. list_entry(walker, struct ecryptfs_auth_tok_list_item,
  71. list);
  72. walker = auth_tok_list_item->list.next;
  73. memset(auth_tok_list_item, 0,
  74. sizeof(struct ecryptfs_auth_tok_list_item));
  75. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  76. auth_tok_list_item);
  77. }
  78. }
  79. struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
  80. /**
  81. * parse_packet_length
  82. * @data: Pointer to memory containing length at offset
  83. * @size: This function writes the decoded size to this memory
  84. * address; zero on error
  85. * @length_size: The number of bytes occupied by the encoded length
  86. *
  87. * Returns Zero on success
  88. */
  89. static int parse_packet_length(unsigned char *data, size_t *size,
  90. size_t *length_size)
  91. {
  92. int rc = 0;
  93. (*length_size) = 0;
  94. (*size) = 0;
  95. if (data[0] < 192) {
  96. /* One-byte length */
  97. (*size) = data[0];
  98. (*length_size) = 1;
  99. } else if (data[0] < 224) {
  100. /* Two-byte length */
  101. (*size) = ((data[0] - 192) * 256);
  102. (*size) += (data[1] + 192);
  103. (*length_size) = 2;
  104. } else if (data[0] == 255) {
  105. /* Five-byte length; we're not supposed to see this */
  106. ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
  107. "supported\n");
  108. rc = -EINVAL;
  109. goto out;
  110. } else {
  111. ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
  112. rc = -EINVAL;
  113. goto out;
  114. }
  115. out:
  116. return rc;
  117. }
  118. /**
  119. * write_packet_length
  120. * @dest: The byte array target into which to write the
  121. * length. Must have at least 5 bytes allocated.
  122. * @size: The length to write.
  123. * @packet_size_length: The number of bytes used to encode the
  124. * packet length is written to this address.
  125. *
  126. * Returns zero on success; non-zero on error.
  127. */
  128. static int write_packet_length(char *dest, size_t size,
  129. size_t *packet_size_length)
  130. {
  131. int rc = 0;
  132. if (size < 192) {
  133. dest[0] = size;
  134. (*packet_size_length) = 1;
  135. } else if (size < 65536) {
  136. dest[0] = (((size - 192) / 256) + 192);
  137. dest[1] = ((size - 192) % 256);
  138. (*packet_size_length) = 2;
  139. } else {
  140. rc = -EINVAL;
  141. ecryptfs_printk(KERN_WARNING,
  142. "Unsupported packet size: [%d]\n", size);
  143. }
  144. return rc;
  145. }
  146. /**
  147. * parse_tag_3_packet
  148. * @crypt_stat: The cryptographic context to modify based on packet
  149. * contents.
  150. * @data: The raw bytes of the packet.
  151. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  152. * a new authentication token will be placed at the end
  153. * of this list for this packet.
  154. * @new_auth_tok: Pointer to a pointer to memory that this function
  155. * allocates; sets the memory address of the pointer to
  156. * NULL on error. This object is added to the
  157. * auth_tok_list.
  158. * @packet_size: This function writes the size of the parsed packet
  159. * into this memory location; zero on error.
  160. * @max_packet_size: maximum number of bytes to parse
  161. *
  162. * Returns zero on success; non-zero on error.
  163. */
  164. static int
  165. parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
  166. unsigned char *data, struct list_head *auth_tok_list,
  167. struct ecryptfs_auth_tok **new_auth_tok,
  168. size_t *packet_size, size_t max_packet_size)
  169. {
  170. int rc = 0;
  171. size_t body_size;
  172. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  173. size_t length_size;
  174. (*packet_size) = 0;
  175. (*new_auth_tok) = NULL;
  176. /* we check that:
  177. * one byte for the Tag 3 ID flag
  178. * two bytes for the body size
  179. * do not exceed the maximum_packet_size
  180. */
  181. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  182. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  183. rc = -EINVAL;
  184. goto out;
  185. }
  186. /* check for Tag 3 identifyer - one byte */
  187. if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
  188. ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
  189. ECRYPTFS_TAG_3_PACKET_TYPE);
  190. rc = -EINVAL;
  191. goto out;
  192. }
  193. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  194. * at end of function upon failure */
  195. auth_tok_list_item =
  196. kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
  197. if (!auth_tok_list_item) {
  198. ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
  199. rc = -ENOMEM;
  200. goto out;
  201. }
  202. memset(auth_tok_list_item, 0,
  203. sizeof(struct ecryptfs_auth_tok_list_item));
  204. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  205. /* check for body size - one to two bytes */
  206. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  207. &length_size);
  208. if (rc) {
  209. ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  210. "rc = [%d]\n", rc);
  211. goto out_free;
  212. }
  213. if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
  214. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  215. body_size);
  216. rc = -EINVAL;
  217. goto out_free;
  218. }
  219. (*packet_size) += length_size;
  220. /* now we know the length of the remainting Tag 3 packet size:
  221. * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
  222. * number of hash iterations
  223. * ECRYPTFS_SALT_SIZE bytes for salt
  224. * body_size bytes minus the stuff above is the encrypted key size
  225. */
  226. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  227. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  228. rc = -EINVAL;
  229. goto out_free;
  230. }
  231. /* There are 5 characters of additional information in the
  232. * packet */
  233. (*new_auth_tok)->session_key.encrypted_key_size =
  234. body_size - (0x05 + ECRYPTFS_SALT_SIZE);
  235. ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
  236. (*new_auth_tok)->session_key.encrypted_key_size);
  237. /* Version 4 (from RFC2440) - one byte */
  238. if (unlikely(data[(*packet_size)++] != 0x04)) {
  239. ecryptfs_printk(KERN_DEBUG, "Unknown version number "
  240. "[%d]\n", data[(*packet_size) - 1]);
  241. rc = -EINVAL;
  242. goto out_free;
  243. }
  244. /* cipher - one byte */
  245. ecryptfs_cipher_code_to_string(crypt_stat->cipher,
  246. (u16)data[(*packet_size)]);
  247. /* A little extra work to differentiate among the AES key
  248. * sizes; see RFC2440 */
  249. switch(data[(*packet_size)++]) {
  250. case RFC2440_CIPHER_AES_192:
  251. crypt_stat->key_size = 24;
  252. break;
  253. default:
  254. crypt_stat->key_size =
  255. (*new_auth_tok)->session_key.encrypted_key_size;
  256. }
  257. ecryptfs_init_crypt_ctx(crypt_stat);
  258. /* S2K identifier 3 (from RFC2440) */
  259. if (unlikely(data[(*packet_size)++] != 0x03)) {
  260. ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
  261. "supported\n");
  262. rc = -ENOSYS;
  263. goto out_free;
  264. }
  265. /* TODO: finish the hash mapping */
  266. /* hash algorithm - one byte */
  267. switch (data[(*packet_size)++]) {
  268. case 0x01: /* See RFC2440 for these numbers and their mappings */
  269. /* Choose MD5 */
  270. /* salt - ECRYPTFS_SALT_SIZE bytes */
  271. memcpy((*new_auth_tok)->token.password.salt,
  272. &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
  273. (*packet_size) += ECRYPTFS_SALT_SIZE;
  274. /* This conversion was taken straight from RFC2440 */
  275. /* number of hash iterations - one byte */
  276. (*new_auth_tok)->token.password.hash_iterations =
  277. ((u32) 16 + (data[(*packet_size)] & 15))
  278. << ((data[(*packet_size)] >> 4) + 6);
  279. (*packet_size)++;
  280. /* encrypted session key -
  281. * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
  282. memcpy((*new_auth_tok)->session_key.encrypted_key,
  283. &data[(*packet_size)],
  284. (*new_auth_tok)->session_key.encrypted_key_size);
  285. (*packet_size) +=
  286. (*new_auth_tok)->session_key.encrypted_key_size;
  287. (*new_auth_tok)->session_key.flags &=
  288. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  289. (*new_auth_tok)->session_key.flags |=
  290. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  291. (*new_auth_tok)->token.password.hash_algo = 0x01;
  292. break;
  293. default:
  294. ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
  295. "[%d]\n", data[(*packet_size) - 1]);
  296. rc = -ENOSYS;
  297. goto out_free;
  298. }
  299. (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
  300. /* TODO: Parametarize; we might actually want userspace to
  301. * decrypt the session key. */
  302. ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
  303. ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  304. ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
  305. ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  306. list_add(&auth_tok_list_item->list, auth_tok_list);
  307. goto out;
  308. out_free:
  309. (*new_auth_tok) = NULL;
  310. memset(auth_tok_list_item, 0,
  311. sizeof(struct ecryptfs_auth_tok_list_item));
  312. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  313. auth_tok_list_item);
  314. out:
  315. if (rc)
  316. (*packet_size) = 0;
  317. return rc;
  318. }
  319. /**
  320. * parse_tag_11_packet
  321. * @data: The raw bytes of the packet
  322. * @contents: This function writes the data contents of the literal
  323. * packet into this memory location
  324. * @max_contents_bytes: The maximum number of bytes that this function
  325. * is allowed to write into contents
  326. * @tag_11_contents_size: This function writes the size of the parsed
  327. * contents into this memory location; zero on
  328. * error
  329. * @packet_size: This function writes the size of the parsed packet
  330. * into this memory location; zero on error
  331. * @max_packet_size: maximum number of bytes to parse
  332. *
  333. * Returns zero on success; non-zero on error.
  334. */
  335. static int
  336. parse_tag_11_packet(unsigned char *data, unsigned char *contents,
  337. size_t max_contents_bytes, size_t *tag_11_contents_size,
  338. size_t *packet_size, size_t max_packet_size)
  339. {
  340. int rc = 0;
  341. size_t body_size;
  342. size_t length_size;
  343. (*packet_size) = 0;
  344. (*tag_11_contents_size) = 0;
  345. /* check that:
  346. * one byte for the Tag 11 ID flag
  347. * two bytes for the Tag 11 length
  348. * do not exceed the maximum_packet_size
  349. */
  350. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  351. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  352. rc = -EINVAL;
  353. goto out;
  354. }
  355. /* check for Tag 11 identifyer - one byte */
  356. if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
  357. ecryptfs_printk(KERN_WARNING,
  358. "Invalid tag 11 packet format\n");
  359. rc = -EINVAL;
  360. goto out;
  361. }
  362. /* get Tag 11 content length - one or two bytes */
  363. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  364. &length_size);
  365. if (rc) {
  366. ecryptfs_printk(KERN_WARNING,
  367. "Invalid tag 11 packet format\n");
  368. goto out;
  369. }
  370. (*packet_size) += length_size;
  371. if (body_size < 13) {
  372. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  373. body_size);
  374. rc = -EINVAL;
  375. goto out;
  376. }
  377. /* We have 13 bytes of surrounding packet values */
  378. (*tag_11_contents_size) = (body_size - 13);
  379. /* now we know the length of the remainting Tag 11 packet size:
  380. * 14 fix bytes for: special flag one, special flag two,
  381. * 12 skipped bytes
  382. * body_size bytes minus the stuff above is the Tag 11 content
  383. */
  384. /* FIXME why is the body size one byte smaller than the actual
  385. * size of the body?
  386. * this seems to be an error here as well as in
  387. * write_tag_11_packet() */
  388. if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
  389. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  390. rc = -EINVAL;
  391. goto out;
  392. }
  393. /* special flag one - one byte */
  394. if (data[(*packet_size)++] != 0x62) {
  395. ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
  396. rc = -EINVAL;
  397. goto out;
  398. }
  399. /* special flag two - one byte */
  400. if (data[(*packet_size)++] != 0x08) {
  401. ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
  402. rc = -EINVAL;
  403. goto out;
  404. }
  405. /* skip the next 12 bytes */
  406. (*packet_size) += 12; /* We don't care about the filename or
  407. * the timestamp */
  408. /* get the Tag 11 contents - tag_11_contents_size bytes */
  409. memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
  410. (*packet_size) += (*tag_11_contents_size);
  411. out:
  412. if (rc) {
  413. (*packet_size) = 0;
  414. (*tag_11_contents_size) = 0;
  415. }
  416. return rc;
  417. }
  418. /**
  419. * decrypt_session_key - Decrypt the session key with the given auth_tok.
  420. *
  421. * Returns Zero on success; non-zero error otherwise.
  422. */
  423. static int decrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
  424. struct ecryptfs_crypt_stat *crypt_stat)
  425. {
  426. struct ecryptfs_password *password_s_ptr;
  427. struct scatterlist src_sg[2], dst_sg[2];
  428. struct mutex *tfm_mutex = NULL;
  429. /* TODO: Use virt_to_scatterlist for these */
  430. char *encrypted_session_key;
  431. char *session_key;
  432. struct blkcipher_desc desc = {
  433. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  434. };
  435. int rc = 0;
  436. password_s_ptr = &auth_tok->token.password;
  437. if (ECRYPTFS_CHECK_FLAG(password_s_ptr->flags,
  438. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET))
  439. ecryptfs_printk(KERN_DEBUG, "Session key encryption key "
  440. "set; skipping key generation\n");
  441. ecryptfs_printk(KERN_DEBUG, "Session key encryption key (size [%d])"
  442. ":\n",
  443. password_s_ptr->session_key_encryption_key_bytes);
  444. if (ecryptfs_verbosity > 0)
  445. ecryptfs_dump_hex(password_s_ptr->session_key_encryption_key,
  446. password_s_ptr->
  447. session_key_encryption_key_bytes);
  448. if (!strcmp(crypt_stat->cipher,
  449. crypt_stat->mount_crypt_stat->global_default_cipher_name)
  450. && crypt_stat->mount_crypt_stat->global_key_tfm) {
  451. desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
  452. tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
  453. } else {
  454. char *full_alg_name;
  455. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  456. crypt_stat->cipher,
  457. "ecb");
  458. if (rc)
  459. goto out;
  460. desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  461. CRYPTO_ALG_ASYNC);
  462. kfree(full_alg_name);
  463. if (IS_ERR(desc.tfm)) {
  464. rc = PTR_ERR(desc.tfm);
  465. printk(KERN_ERR "Error allocating crypto context; "
  466. "rc = [%d]\n", rc);
  467. goto out;
  468. }
  469. crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  470. }
  471. if (tfm_mutex)
  472. mutex_lock(tfm_mutex);
  473. rc = crypto_blkcipher_setkey(desc.tfm,
  474. password_s_ptr->session_key_encryption_key,
  475. crypt_stat->key_size);
  476. if (rc < 0) {
  477. printk(KERN_ERR "Error setting key for crypto context\n");
  478. rc = -EINVAL;
  479. goto out_free_tfm;
  480. }
  481. /* TODO: virt_to_scatterlist */
  482. encrypted_session_key = (char *)__get_free_page(GFP_KERNEL);
  483. if (!encrypted_session_key) {
  484. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  485. rc = -ENOMEM;
  486. goto out_free_tfm;
  487. }
  488. session_key = (char *)__get_free_page(GFP_KERNEL);
  489. if (!session_key) {
  490. kfree(encrypted_session_key);
  491. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  492. rc = -ENOMEM;
  493. goto out_free_tfm;
  494. }
  495. memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key,
  496. auth_tok->session_key.encrypted_key_size);
  497. src_sg[0].page = virt_to_page(encrypted_session_key);
  498. src_sg[0].offset = 0;
  499. BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE);
  500. src_sg[0].length = auth_tok->session_key.encrypted_key_size;
  501. dst_sg[0].page = virt_to_page(session_key);
  502. dst_sg[0].offset = 0;
  503. auth_tok->session_key.decrypted_key_size =
  504. auth_tok->session_key.encrypted_key_size;
  505. dst_sg[0].length = auth_tok->session_key.encrypted_key_size;
  506. rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
  507. auth_tok->session_key.encrypted_key_size);
  508. if (rc) {
  509. printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
  510. goto out_free_memory;
  511. }
  512. auth_tok->session_key.decrypted_key_size =
  513. auth_tok->session_key.encrypted_key_size;
  514. memcpy(auth_tok->session_key.decrypted_key, session_key,
  515. auth_tok->session_key.decrypted_key_size);
  516. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  517. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  518. auth_tok->session_key.decrypted_key_size);
  519. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  520. ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
  521. if (ecryptfs_verbosity > 0)
  522. ecryptfs_dump_hex(crypt_stat->key,
  523. crypt_stat->key_size);
  524. out_free_memory:
  525. memset(encrypted_session_key, 0, PAGE_CACHE_SIZE);
  526. free_page((unsigned long)encrypted_session_key);
  527. memset(session_key, 0, PAGE_CACHE_SIZE);
  528. free_page((unsigned long)session_key);
  529. out_free_tfm:
  530. if (tfm_mutex)
  531. mutex_unlock(tfm_mutex);
  532. else
  533. crypto_free_blkcipher(desc.tfm);
  534. out:
  535. return rc;
  536. }
  537. /**
  538. * ecryptfs_parse_packet_set
  539. * @dest: The header page in memory
  540. * @version: Version of file format, to guide parsing behavior
  541. *
  542. * Get crypt_stat to have the file's session key if the requisite key
  543. * is available to decrypt the session key.
  544. *
  545. * Returns Zero if a valid authentication token was retrieved and
  546. * processed; negative value for file not encrypted or for error
  547. * conditions.
  548. */
  549. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  550. unsigned char *src,
  551. struct dentry *ecryptfs_dentry)
  552. {
  553. size_t i = 0;
  554. int rc = 0;
  555. size_t found_auth_tok = 0;
  556. size_t next_packet_is_auth_tok_packet;
  557. char sig[ECRYPTFS_SIG_SIZE_HEX];
  558. struct list_head auth_tok_list;
  559. struct list_head *walker;
  560. struct ecryptfs_auth_tok *chosen_auth_tok = NULL;
  561. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  562. &ecryptfs_superblock_to_private(
  563. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  564. struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
  565. size_t packet_size;
  566. struct ecryptfs_auth_tok *new_auth_tok;
  567. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  568. size_t tag_11_contents_size;
  569. size_t tag_11_packet_size;
  570. INIT_LIST_HEAD(&auth_tok_list);
  571. /* Parse the header to find as many packets as we can, these will be
  572. * added the our &auth_tok_list */
  573. next_packet_is_auth_tok_packet = 1;
  574. while (next_packet_is_auth_tok_packet) {
  575. size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
  576. switch (src[i]) {
  577. case ECRYPTFS_TAG_3_PACKET_TYPE:
  578. rc = parse_tag_3_packet(crypt_stat,
  579. (unsigned char *)&src[i],
  580. &auth_tok_list, &new_auth_tok,
  581. &packet_size, max_packet_size);
  582. if (rc) {
  583. ecryptfs_printk(KERN_ERR, "Error parsing "
  584. "tag 3 packet\n");
  585. rc = -EIO;
  586. goto out_wipe_list;
  587. }
  588. i += packet_size;
  589. rc = parse_tag_11_packet((unsigned char *)&src[i],
  590. sig_tmp_space,
  591. ECRYPTFS_SIG_SIZE,
  592. &tag_11_contents_size,
  593. &tag_11_packet_size,
  594. max_packet_size);
  595. if (rc) {
  596. ecryptfs_printk(KERN_ERR, "No valid "
  597. "(ecryptfs-specific) literal "
  598. "packet containing "
  599. "authentication token "
  600. "signature found after "
  601. "tag 3 packet\n");
  602. rc = -EIO;
  603. goto out_wipe_list;
  604. }
  605. i += tag_11_packet_size;
  606. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  607. ecryptfs_printk(KERN_ERR, "Expected "
  608. "signature of size [%d]; "
  609. "read size [%d]\n",
  610. ECRYPTFS_SIG_SIZE,
  611. tag_11_contents_size);
  612. rc = -EIO;
  613. goto out_wipe_list;
  614. }
  615. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  616. sig_tmp_space, tag_11_contents_size);
  617. new_auth_tok->token.password.signature[
  618. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  619. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  620. ECRYPTFS_ENCRYPTED);
  621. break;
  622. case ECRYPTFS_TAG_11_PACKET_TYPE:
  623. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  624. "(Tag 11 not allowed by itself)\n");
  625. rc = -EIO;
  626. goto out_wipe_list;
  627. break;
  628. default:
  629. ecryptfs_printk(KERN_DEBUG, "No packet at offset "
  630. "[%d] of the file header; hex value of "
  631. "character is [0x%.2x]\n", i, src[i]);
  632. next_packet_is_auth_tok_packet = 0;
  633. }
  634. }
  635. if (list_empty(&auth_tok_list)) {
  636. rc = -EINVAL; /* Do not support non-encrypted files in
  637. * the 0.1 release */
  638. goto out;
  639. }
  640. /* If we have a global auth tok, then we should try to use
  641. * it */
  642. if (mount_crypt_stat->global_auth_tok) {
  643. memcpy(sig, mount_crypt_stat->global_auth_tok_sig,
  644. ECRYPTFS_SIG_SIZE_HEX);
  645. chosen_auth_tok = mount_crypt_stat->global_auth_tok;
  646. } else
  647. BUG(); /* We should always have a global auth tok in
  648. * the 0.1 release */
  649. /* Scan list to see if our chosen_auth_tok works */
  650. list_for_each(walker, &auth_tok_list) {
  651. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  652. auth_tok_list_item =
  653. list_entry(walker, struct ecryptfs_auth_tok_list_item,
  654. list);
  655. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  656. if (unlikely(ecryptfs_verbosity > 0)) {
  657. ecryptfs_printk(KERN_DEBUG,
  658. "Considering cadidate auth tok:\n");
  659. ecryptfs_dump_auth_tok(candidate_auth_tok);
  660. }
  661. /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
  662. if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD
  663. && !strncmp(candidate_auth_tok->token.password.signature,
  664. sig, ECRYPTFS_SIG_SIZE_HEX)) {
  665. found_auth_tok = 1;
  666. goto leave_list;
  667. /* TODO: Transfer the common salt into the
  668. * crypt_stat salt */
  669. }
  670. }
  671. leave_list:
  672. if (!found_auth_tok) {
  673. ecryptfs_printk(KERN_ERR, "Could not find authentication "
  674. "token on temporary list for sig [%.*s]\n",
  675. ECRYPTFS_SIG_SIZE_HEX, sig);
  676. rc = -EIO;
  677. goto out_wipe_list;
  678. } else {
  679. memcpy(&(candidate_auth_tok->token.password),
  680. &(chosen_auth_tok->token.password),
  681. sizeof(struct ecryptfs_password));
  682. rc = decrypt_session_key(candidate_auth_tok, crypt_stat);
  683. if (rc) {
  684. ecryptfs_printk(KERN_ERR, "Error decrypting the "
  685. "session key\n");
  686. goto out_wipe_list;
  687. }
  688. rc = ecryptfs_compute_root_iv(crypt_stat);
  689. if (rc) {
  690. ecryptfs_printk(KERN_ERR, "Error computing "
  691. "the root IV\n");
  692. goto out_wipe_list;
  693. }
  694. }
  695. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  696. if (rc) {
  697. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  698. "context for cipher [%s]; rc = [%d]\n",
  699. crypt_stat->cipher, rc);
  700. }
  701. out_wipe_list:
  702. wipe_auth_tok_list(&auth_tok_list);
  703. out:
  704. return rc;
  705. }
  706. /**
  707. * write_tag_11_packet
  708. * @dest: Target into which Tag 11 packet is to be written
  709. * @max: Maximum packet length
  710. * @contents: Byte array of contents to copy in
  711. * @contents_length: Number of bytes in contents
  712. * @packet_length: Length of the Tag 11 packet written; zero on error
  713. *
  714. * Returns zero on success; non-zero on error.
  715. */
  716. static int
  717. write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
  718. size_t *packet_length)
  719. {
  720. int rc = 0;
  721. size_t packet_size_length;
  722. (*packet_length) = 0;
  723. if ((13 + contents_length) > max) {
  724. rc = -EINVAL;
  725. ecryptfs_printk(KERN_ERR, "Packet length larger than "
  726. "maximum allowable\n");
  727. goto out;
  728. }
  729. /* General packet header */
  730. /* Packet tag */
  731. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  732. /* Packet length */
  733. rc = write_packet_length(&dest[(*packet_length)],
  734. (13 + contents_length), &packet_size_length);
  735. if (rc) {
  736. ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
  737. "header; cannot generate packet length\n");
  738. goto out;
  739. }
  740. (*packet_length) += packet_size_length;
  741. /* Tag 11 specific */
  742. /* One-octet field that describes how the data is formatted */
  743. dest[(*packet_length)++] = 0x62; /* binary data */
  744. /* One-octet filename length followed by filename */
  745. dest[(*packet_length)++] = 8;
  746. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  747. (*packet_length) += 8;
  748. /* Four-octet number indicating modification date */
  749. memset(&dest[(*packet_length)], 0x00, 4);
  750. (*packet_length) += 4;
  751. /* Remainder is literal data */
  752. memcpy(&dest[(*packet_length)], contents, contents_length);
  753. (*packet_length) += contents_length;
  754. out:
  755. if (rc)
  756. (*packet_length) = 0;
  757. return rc;
  758. }
  759. /**
  760. * write_tag_3_packet
  761. * @dest: Buffer into which to write the packet
  762. * @max: Maximum number of bytes that can be written
  763. * @auth_tok: Authentication token
  764. * @crypt_stat: The cryptographic context
  765. * @key_rec: encrypted key
  766. * @packet_size: This function will write the number of bytes that end
  767. * up constituting the packet; set to zero on error
  768. *
  769. * Returns zero on success; non-zero on error.
  770. */
  771. static int
  772. write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
  773. struct ecryptfs_crypt_stat *crypt_stat,
  774. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  775. {
  776. size_t i;
  777. size_t signature_is_valid = 0;
  778. size_t encrypted_session_key_valid = 0;
  779. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  780. struct scatterlist dest_sg[2];
  781. struct scatterlist src_sg[2];
  782. struct mutex *tfm_mutex = NULL;
  783. size_t key_rec_size;
  784. size_t packet_size_length;
  785. size_t cipher_code;
  786. struct blkcipher_desc desc = {
  787. .tfm = NULL,
  788. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  789. };
  790. int rc = 0;
  791. (*packet_size) = 0;
  792. /* Check for a valid signature on the auth_tok */
  793. for (i = 0; i < ECRYPTFS_SIG_SIZE_HEX; i++)
  794. signature_is_valid |= auth_tok->token.password.signature[i];
  795. if (!signature_is_valid)
  796. BUG();
  797. ecryptfs_from_hex((*key_rec).sig, auth_tok->token.password.signature,
  798. ECRYPTFS_SIG_SIZE);
  799. encrypted_session_key_valid = 0;
  800. for (i = 0; i < crypt_stat->key_size; i++)
  801. encrypted_session_key_valid |=
  802. auth_tok->session_key.encrypted_key[i];
  803. if (encrypted_session_key_valid) {
  804. memcpy((*key_rec).enc_key,
  805. auth_tok->session_key.encrypted_key,
  806. auth_tok->session_key.encrypted_key_size);
  807. goto encrypted_session_key_set;
  808. }
  809. if (auth_tok->session_key.encrypted_key_size == 0)
  810. auth_tok->session_key.encrypted_key_size =
  811. crypt_stat->key_size;
  812. if (crypt_stat->key_size == 24
  813. && strcmp("aes", crypt_stat->cipher) == 0) {
  814. memset((crypt_stat->key + 24), 0, 8);
  815. auth_tok->session_key.encrypted_key_size = 32;
  816. }
  817. (*key_rec).enc_key_size =
  818. auth_tok->session_key.encrypted_key_size;
  819. if (ECRYPTFS_CHECK_FLAG(auth_tok->token.password.flags,
  820. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET)) {
  821. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  822. "session key encryption key of size [%d]\n",
  823. auth_tok->token.password.
  824. session_key_encryption_key_bytes);
  825. memcpy(session_key_encryption_key,
  826. auth_tok->token.password.session_key_encryption_key,
  827. crypt_stat->key_size);
  828. ecryptfs_printk(KERN_DEBUG,
  829. "Cached session key " "encryption key: \n");
  830. if (ecryptfs_verbosity > 0)
  831. ecryptfs_dump_hex(session_key_encryption_key, 16);
  832. }
  833. if (unlikely(ecryptfs_verbosity > 0)) {
  834. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  835. ecryptfs_dump_hex(session_key_encryption_key, 16);
  836. }
  837. rc = virt_to_scatterlist(crypt_stat->key,
  838. (*key_rec).enc_key_size, src_sg, 2);
  839. if (!rc) {
  840. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  841. "for crypt_stat session key\n");
  842. rc = -ENOMEM;
  843. goto out;
  844. }
  845. rc = virt_to_scatterlist((*key_rec).enc_key,
  846. (*key_rec).enc_key_size, dest_sg, 2);
  847. if (!rc) {
  848. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  849. "for crypt_stat encrypted session key\n");
  850. rc = -ENOMEM;
  851. goto out;
  852. }
  853. if (!strcmp(crypt_stat->cipher,
  854. crypt_stat->mount_crypt_stat->global_default_cipher_name)
  855. && crypt_stat->mount_crypt_stat->global_key_tfm) {
  856. desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
  857. tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
  858. } else {
  859. char *full_alg_name;
  860. rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
  861. crypt_stat->cipher,
  862. "ecb");
  863. if (rc)
  864. goto out;
  865. desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
  866. CRYPTO_ALG_ASYNC);
  867. kfree(full_alg_name);
  868. if (IS_ERR(desc.tfm)) {
  869. rc = PTR_ERR(desc.tfm);
  870. ecryptfs_printk(KERN_ERR, "Could not initialize crypto "
  871. "context for cipher [%s]; rc = [%d]\n",
  872. crypt_stat->cipher, rc);
  873. goto out;
  874. }
  875. crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  876. }
  877. if (tfm_mutex)
  878. mutex_lock(tfm_mutex);
  879. rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
  880. crypt_stat->key_size);
  881. if (rc < 0) {
  882. if (tfm_mutex)
  883. mutex_unlock(tfm_mutex);
  884. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  885. "context; rc = [%d]\n", rc);
  886. goto out;
  887. }
  888. rc = 0;
  889. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  890. crypt_stat->key_size);
  891. rc = crypto_blkcipher_encrypt(&desc, dest_sg, src_sg,
  892. (*key_rec).enc_key_size);
  893. if (rc) {
  894. printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
  895. goto out;
  896. }
  897. if (tfm_mutex)
  898. mutex_unlock(tfm_mutex);
  899. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  900. if (ecryptfs_verbosity > 0)
  901. ecryptfs_dump_hex((*key_rec).enc_key,
  902. (*key_rec).enc_key_size);
  903. encrypted_session_key_set:
  904. /* Now we have a valid key_rec. Append it to the
  905. * key_rec set. */
  906. key_rec_size = (sizeof(struct ecryptfs_key_record)
  907. - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
  908. + ((*key_rec).enc_key_size));
  909. /* TODO: Include a packet size limit as a parameter to this
  910. * function once we have multi-packet headers (for versions
  911. * later than 0.1 */
  912. if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
  913. ecryptfs_printk(KERN_ERR, "Keyset too large\n");
  914. rc = -EINVAL;
  915. goto out;
  916. }
  917. /* TODO: Packet size limit */
  918. /* We have 5 bytes of surrounding packet data */
  919. if ((0x05 + ECRYPTFS_SALT_SIZE
  920. + (*key_rec).enc_key_size) >= max) {
  921. ecryptfs_printk(KERN_ERR, "Authentication token is too "
  922. "large\n");
  923. rc = -EINVAL;
  924. goto out;
  925. }
  926. /* This format is inspired by OpenPGP; see RFC 2440
  927. * packet tag 3 */
  928. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  929. /* ver+cipher+s2k+hash+salt+iter+enc_key */
  930. rc = write_packet_length(&dest[(*packet_size)],
  931. (0x05 + ECRYPTFS_SALT_SIZE
  932. + (*key_rec).enc_key_size),
  933. &packet_size_length);
  934. if (rc) {
  935. ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet "
  936. "header; cannot generate packet length\n");
  937. goto out;
  938. }
  939. (*packet_size) += packet_size_length;
  940. dest[(*packet_size)++] = 0x04; /* version 4 */
  941. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
  942. if (cipher_code == 0) {
  943. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  944. "cipher [%s]\n", crypt_stat->cipher);
  945. rc = -EINVAL;
  946. goto out;
  947. }
  948. dest[(*packet_size)++] = cipher_code;
  949. dest[(*packet_size)++] = 0x03; /* S2K */
  950. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  951. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  952. ECRYPTFS_SALT_SIZE);
  953. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  954. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  955. memcpy(&dest[(*packet_size)], (*key_rec).enc_key,
  956. (*key_rec).enc_key_size);
  957. (*packet_size) += (*key_rec).enc_key_size;
  958. out:
  959. if (desc.tfm && !tfm_mutex)
  960. crypto_free_blkcipher(desc.tfm);
  961. if (rc)
  962. (*packet_size) = 0;
  963. return rc;
  964. }
  965. /**
  966. * ecryptfs_generate_key_packet_set
  967. * @dest: Virtual address from which to write the key record set
  968. * @crypt_stat: The cryptographic context from which the
  969. * authentication tokens will be retrieved
  970. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  971. * for the global parameters
  972. * @len: The amount written
  973. * @max: The maximum amount of data allowed to be written
  974. *
  975. * Generates a key packet set and writes it to the virtual address
  976. * passed in.
  977. *
  978. * Returns zero on success; non-zero on error.
  979. */
  980. int
  981. ecryptfs_generate_key_packet_set(char *dest_base,
  982. struct ecryptfs_crypt_stat *crypt_stat,
  983. struct dentry *ecryptfs_dentry, size_t *len,
  984. size_t max)
  985. {
  986. int rc = 0;
  987. struct ecryptfs_auth_tok *auth_tok;
  988. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  989. &ecryptfs_superblock_to_private(
  990. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  991. size_t written;
  992. struct ecryptfs_key_record key_rec;
  993. (*len) = 0;
  994. if (mount_crypt_stat->global_auth_tok) {
  995. auth_tok = mount_crypt_stat->global_auth_tok;
  996. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  997. rc = write_tag_3_packet((dest_base + (*len)),
  998. max, auth_tok,
  999. crypt_stat, &key_rec,
  1000. &written);
  1001. if (rc) {
  1002. ecryptfs_printk(KERN_WARNING, "Error "
  1003. "writing tag 3 packet\n");
  1004. goto out;
  1005. }
  1006. (*len) += written;
  1007. /* Write auth tok signature packet */
  1008. rc = write_tag_11_packet(
  1009. (dest_base + (*len)),
  1010. (max - (*len)),
  1011. key_rec.sig, ECRYPTFS_SIG_SIZE, &written);
  1012. if (rc) {
  1013. ecryptfs_printk(KERN_ERR, "Error writing "
  1014. "auth tok signature packet\n");
  1015. goto out;
  1016. }
  1017. (*len) += written;
  1018. } else {
  1019. ecryptfs_printk(KERN_WARNING, "Unsupported "
  1020. "authentication token type\n");
  1021. rc = -EINVAL;
  1022. goto out;
  1023. }
  1024. if (rc) {
  1025. ecryptfs_printk(KERN_WARNING, "Error writing "
  1026. "authentication token packet with sig "
  1027. "= [%s]\n",
  1028. mount_crypt_stat->global_auth_tok_sig);
  1029. rc = -EIO;
  1030. goto out;
  1031. }
  1032. } else
  1033. BUG();
  1034. if (likely((max - (*len)) > 0)) {
  1035. dest_base[(*len)] = 0x00;
  1036. } else {
  1037. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  1038. rc = -EIO;
  1039. }
  1040. out:
  1041. if (rc)
  1042. (*len) = 0;
  1043. return rc;
  1044. }