keystore.c 33 KB

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