28 #include <linux/string.h>
32 #include <linux/random.h>
35 #include <linux/slab.h>
43 static int process_request_key_err(
long err_code)
62 "[0x%.16lx]\n", err_code);
68 static int process_find_global_auth_tok_for_sig_err(
int err_code)
80 rc = process_request_key_err(err_code);
104 (*size) = (
unsigned char)data[0];
106 }
else if (data[0] < 224) {
108 (*size) = (((
unsigned char)(data[0]) - 192) * 256);
109 (*size) += ((
unsigned char)(data[1]) + 192);
111 }
else if (data[0] == 255) {
137 size_t *packet_size_length)
143 (*packet_size_length) = 1;
144 }
else if (size < 65536) {
145 dest[0] = (((size - 192) / 256) + 192);
146 dest[1] = ((size - 192) % 256);
147 (*packet_size_length) = 2;
152 "Unsupported packet size: [%zd]\n", size);
159 char **
packet,
size_t *packet_len)
163 size_t packet_size_len;
189 "header; cannot generate packet length\n");
192 i += packet_size_len;
200 "header; cannot generate packet length\n");
203 i += packet_size_len;
222 u16 expected_checksum = 0;
234 if (message_len < 4) {
245 "[%d]\n", data[i-1]);
256 if (message_len < (i + m_size)) {
258 "is shorter than expected\n");
264 "The decrypted key is not long enough to "
265 "include a cipher code and checksum\n");
269 *cipher_code = data[i++];
274 "the maximum key size [%d]\n",
283 expected_checksum += (
unsigned char)(data[i++]) << 8;
284 expected_checksum += (
unsigned char)(data[i++]);
287 if (expected_checksum != checksum) {
289 "encryption key; expected [%x]; calculated "
290 "[%x]\n", expected_checksum, checksum);
299 write_tag_66_packet(
char *signature,
u8 cipher_code,
307 size_t packet_size_len;
332 "header; cannot generate packet length\n");
335 i += packet_size_len;
343 "header; cannot generate packet length\n");
346 i += packet_size_len;
347 message[i++] = cipher_code;
350 for (j = 0; j < crypt_stat->
key_size; j++)
351 checksum += crypt_stat->
key[j];
352 message[i++] = (checksum / 256) % 256;
353 message[i++] = (checksum % 256);
379 if (message_len < 4) {
382 "message length is [%d]\n", __func__, message_len, 4);
394 "value [%d]\n", __func__, data[i-1]);
415 "the maximum key size [%d]\n", __func__,
431 static int ecryptfs_verify_version(
u16 version)
437 major = ((version >> 8) & 0xFF);
438 minor = (version & 0xFF);
441 "Expected [%d]; got [%d]\n",
448 "Expected [%d]; got [%d]\n",
465 ecryptfs_verify_auth_tok_from_key(
struct key *auth_tok_key,
470 (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
471 if (ecryptfs_verify_version((*auth_tok)->version)) {
473 "tools must match eCryptfs kernel module with major "
474 "version [%d] and minor version [%d]\n",
482 "returned from key query\n");
491 ecryptfs_find_global_auth_tok_for_sig(
492 struct key **auth_tok_key,
499 (*auth_tok_key) =
NULL;
517 goto out_invalid_auth_tok;
521 rc = ecryptfs_verify_auth_tok_from_key(
524 goto out_invalid_auth_tok_unlock;
527 key_get(*auth_tok_key);
532 out_invalid_auth_tok_unlock:
534 out_invalid_auth_tok:
560 ecryptfs_find_auth_tok_for_sig(
561 struct key **auth_tok_key,
568 rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok,
569 mount_crypt_stat, sig);
576 if (mount_crypt_stat->
flags
627 char *
filename,
size_t filename_size)
630 struct key *auth_tok_key =
NULL;
636 "[%zd] bytes of kernel memory\n", __func__,
sizeof(*s));
642 rc = ecryptfs_find_auth_tok_for_sig(
648 "fnek sig [%s]; rc = [%d]\n", __func__,
657 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
699 "kzalloc [%zd] bytes\n", __func__,
713 "header; cannot generate packet length; rc = [%d]\n",
715 goto out_free_unlock;
727 "cipher [%s] with key bytes [%zd]\n", __func__,
731 goto out_free_unlock;
739 "password tokens\n", __func__);
740 goto out_free_unlock;
744 (
u8 *)s->
auth_tok->token.password.session_key_encryption_key,
745 s->
auth_tok->token.password.session_key_encryption_key_bytes);
752 "allocate hash crypto context; rc = [%d]\n",
754 goto out_free_unlock;
759 "%s: Error initializing crypto hash; rc = [%d]\n",
761 goto out_release_free_unlock;
763 rc = crypto_hash_update(
765 s->
auth_tok->token.password.session_key_encryption_key_bytes);
768 "%s: Error updating crypto hash; rc = [%d]\n",
770 goto out_release_free_unlock;
775 "%s: Error finalizing crypto hash; rc = [%d]\n",
777 goto out_release_free_unlock;
789 "%s: Error initializing crypto hash; "
790 "rc = [%d]\n", __func__, rc);
791 goto out_release_free_unlock;
797 "%s: Error updating crypto hash; "
798 "rc = [%d]\n", __func__, rc);
799 goto out_release_free_unlock;
804 "%s: Error finalizing crypto hash; "
805 "rc = [%d]\n", __func__, rc);
806 goto out_release_free_unlock;
820 "convert filename memory to scatterlist; rc = [%d]. "
821 "block_aligned_filename_size = [%zd]\n", __func__, rc,
823 goto out_release_free_unlock;
829 "convert encrypted filename memory to scatterlist; "
830 "rc = [%d]. block_aligned_filename_size = [%zd]\n",
832 goto out_release_free_unlock;
840 rc = crypto_blkcipher_setkey(
842 s->
auth_tok->token.password.session_key_encryption_key,
846 "rc = [%d]. s->auth_tok->token.password.session_key_"
847 "encryption_key = [0x%p]; mount_crypt_stat->"
848 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
850 s->
auth_tok->token.password.session_key_encryption_key,
852 goto out_release_free_unlock;
858 "rc = [%d]\n", __func__, rc);
859 goto out_release_free_unlock;
862 (*packet_size) = s->
i;
863 (*remaining_bytes) -= (*packet_size);
864 out_release_free_unlock:
917 char *data,
size_t max_packet_size)
920 struct key *auth_tok_key =
NULL;
924 (*filename_size) = 0;
929 "[%zd] bytes of kernel memory\n", __func__,
sizeof(*s));
936 "at least [%d]\n", __func__, max_packet_size,
951 "tag [0x%.2x]\n", __func__,
961 "rc = [%d]\n", __func__, rc);
969 "size is [%zd]\n", __func__, max_packet_size,
987 rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
992 "fnek sig [%s]; rc = [%d]\n", __func__, s->
fnek_sig_hex,
1001 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1010 "convert encrypted filename memory to scatterlist; "
1011 "rc = [%d]. block_aligned_filename_size = [%zd]\n",
1020 "kmalloc [%zd] bytes\n", __func__,
1029 "convert decrypted filename memory to scatterlist; "
1030 "rc = [%d]. block_aligned_filename_size = [%zd]\n",
1032 goto out_free_unlock;
1045 "password tokens\n", __func__);
1046 goto out_free_unlock;
1048 rc = crypto_blkcipher_setkey(
1050 s->
auth_tok->token.password.session_key_encryption_key,
1054 "rc = [%d]. s->auth_tok->token.password.session_key_"
1055 "encryption_key = [0x%p]; mount_crypt_stat->"
1056 "global_default_fn_cipher_key_bytes = [%zd]\n", __func__,
1058 s->
auth_tok->token.password.session_key_encryption_key,
1060 goto out_free_unlock;
1066 "rc = [%d]\n", __func__, rc);
1067 goto out_free_unlock;
1075 "find valid separator between random characters and "
1076 "the filename\n", __func__);
1078 goto out_free_unlock;
1082 if (!((*filename_size) > 0 && (*filename_size <
PATH_MAX))) {
1084 "invalid\n", __func__, (*filename_size));
1086 goto out_free_unlock;
1091 "kmalloc [%zd] bytes\n", __func__,
1092 ((*filename_size) + 1));
1094 goto out_free_unlock;
1097 (*filename)[(*filename_size)] =
'\0';
1105 (*filename_size) = 0;
1156 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
1162 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->
session_key),
1163 &payload, &payload_len);
1177 "from the user space daemon\n");
1181 rc = parse_tag_65_packet(&(auth_tok->
session_key),
1210 static void wipe_auth_tok_list(
struct list_head *auth_tok_list_head)
1216 auth_tok_list_head,
list) {
1219 auth_tok_list_item);
1244 unsigned char *data,
struct list_head *auth_tok_list,
1246 size_t *packet_size,
size_t max_packet_size)
1254 (*new_auth_tok) =
NULL;
1268 if (
unlikely(max_packet_size < 12)) {
1281 auth_tok_list_item =
1282 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
1284 if (!auth_tok_list_item) {
1289 (*new_auth_tok) = &auth_tok_list_item->
auth_tok;
1302 (*packet_size) += length_size;
1303 if (
unlikely((*packet_size) + body_size > max_packet_size)) {
1308 if (
unlikely(data[(*packet_size)++] != 0x03)) {
1310 data[(*packet_size) - 1]);
1320 (*new_auth_tok)->session_key.encrypted_key_size =
1322 if ((*new_auth_tok)->session_key.encrypted_key_size
1325 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
1329 memcpy((*new_auth_tok)->session_key.encrypted_key,
1331 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
1332 (*new_auth_tok)->session_key.flags &=
1334 (*new_auth_tok)->session_key.flags |=
1337 (*new_auth_tok)->flags = 0;
1338 (*new_auth_tok)->session_key.flags &=
1340 (*new_auth_tok)->session_key.flags &=
1342 list_add(&auth_tok_list_item->
list, auth_tok_list);
1345 (*new_auth_tok) =
NULL;
1346 memset(auth_tok_list_item, 0,
1349 auth_tok_list_item);
1376 unsigned char *data,
struct list_head *auth_tok_list,
1378 size_t *packet_size,
size_t max_packet_size)
1386 (*new_auth_tok) =
NULL;
1416 auth_tok_list_item =
1417 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
GFP_KERNEL);
1418 if (!auth_tok_list_item) {
1423 (*new_auth_tok) = &auth_tok_list_item->
auth_tok;
1436 (*packet_size) += length_size;
1437 if (
unlikely((*packet_size) + body_size > max_packet_size)) {
1442 (*new_auth_tok)->session_key.encrypted_key_size =
1444 if ((*new_auth_tok)->session_key.encrypted_key_size
1447 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
1451 if (
unlikely(data[(*packet_size)++] != 0x04)) {
1453 data[(*packet_size) - 1]);
1458 (
u16)data[(*packet_size)]);
1463 switch(data[(*packet_size)++]) {
1469 (*new_auth_tok)->session_key.encrypted_key_size;
1474 if (
unlikely(data[(*packet_size)++] != 0x03)) {
1480 switch (data[(*packet_size)++]) {
1483 memcpy((*new_auth_tok)->token.password.salt,
1487 (*new_auth_tok)->token.password.hash_iterations =
1488 ((
u32) 16 + (data[(*packet_size)] & 15))
1489 << ((data[(*packet_size)] >> 4) + 6);
1494 memcpy((*new_auth_tok)->session_key.encrypted_key,
1495 &data[(*packet_size)],
1496 (*new_auth_tok)->session_key.encrypted_key_size);
1498 (*new_auth_tok)->session_key.encrypted_key_size;
1499 (*new_auth_tok)->session_key.flags &=
1501 (*new_auth_tok)->session_key.flags |=
1503 (*new_auth_tok)->token.password.hash_algo = 0x01;
1507 "[%d]\n", data[(*packet_size) - 1]);
1514 (*new_auth_tok)->session_key.flags &=
1516 (*new_auth_tok)->session_key.flags &=
1518 list_add(&auth_tok_list_item->
list, auth_tok_list);
1521 (*new_auth_tok) =
NULL;
1522 memset(auth_tok_list_item, 0,
1525 auth_tok_list_item);
1549 parse_tag_11_packet(
unsigned char *data,
unsigned char *contents,
1550 size_t max_contents_bytes,
size_t *tag_11_contents_size,
1551 size_t *packet_size,
size_t max_packet_size)
1558 (*tag_11_contents_size) = 0;
1573 if (max_packet_size < 16) {
1589 if (body_size < 14) {
1594 (*packet_size) += length_size;
1595 (*tag_11_contents_size) = (body_size - 14);
1596 if (
unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
1601 if (
unlikely((*tag_11_contents_size) > max_contents_bytes)) {
1607 if (data[(*packet_size)++] != 0x62) {
1612 if (data[(*packet_size)++] != 0x08) {
1617 (*packet_size) += 12;
1618 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
1619 (*packet_size) += (*tag_11_contents_size);
1623 (*tag_11_contents_size) = 0;
1635 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1636 (*auth_tok_key) = ecryptfs_get_encrypted_key(sig);
1637 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
1640 rc = process_request_key_err(PTR_ERR(*auth_tok_key));
1641 (*auth_tok_key) =
NULL;
1646 rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok);
1650 (*auth_tok_key) =
NULL;
1670 struct mutex *tfm_mutex;
1678 KERN_DEBUG,
"Session key encryption key (size [%d]):\n",
1688 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1697 "auth_tok->session_key.encrypted_key to scatterlist; "
1698 "expected rc = 1; got rc = [%d]. "
1699 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1710 "auth_tok->session_key.decrypted_key to scatterlist; "
1711 "expected rc = 1; got rc = [%d]\n", rc);
1715 rc = crypto_blkcipher_setkey(
1724 rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
1760 struct dentry *ecryptfs_dentry)
1763 size_t found_auth_tok;
1764 size_t next_packet_is_auth_tok_packet;
1768 char *candidate_auth_tok_sig;
1773 size_t tag_11_contents_size;
1774 size_t tag_11_packet_size;
1775 struct key *auth_tok_key =
NULL;
1778 INIT_LIST_HEAD(&auth_tok_list);
1781 next_packet_is_auth_tok_packet = 1;
1782 while (next_packet_is_auth_tok_packet) {
1787 rc = parse_tag_3_packet(crypt_stat,
1788 (
unsigned char *)&src[i],
1789 &auth_tok_list, &new_auth_tok,
1790 &packet_size, max_packet_size);
1798 rc = parse_tag_11_packet((
unsigned char *)&src[i],
1801 &tag_11_contents_size,
1802 &tag_11_packet_size,
1806 "(ecryptfs-specific) literal "
1807 "packet containing "
1808 "authentication token "
1809 "signature found after "
1814 i += tag_11_packet_size;
1817 "signature of size [%d]; "
1818 "read size [%zd]\n",
1820 tag_11_contents_size);
1825 sig_tmp_space, tag_11_contents_size);
1831 rc = parse_tag_1_packet(crypt_stat,
1832 (
unsigned char *)&src[i],
1833 &auth_tok_list, &new_auth_tok,
1834 &packet_size, max_packet_size);
1846 "(Tag 11 not allowed by itself)\n");
1852 "of the file header; hex value of "
1853 "character is [0x%.2x]\n", i, src[i]);
1854 next_packet_is_auth_tok_packet = 0;
1857 if (list_empty(&auth_tok_list)) {
1859 "eCryptfs file; this is not supported in this version "
1860 "of the eCryptfs kernel module\n");
1870 find_next_matching_auth_tok:
1873 candidate_auth_tok = &auth_tok_list_item->
auth_tok;
1876 "Considering cadidate auth tok:\n");
1879 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1880 candidate_auth_tok);
1883 "Unrecognized candidate auth tok type: [%d]\n",
1888 rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
1891 candidate_auth_tok_sig);
1894 goto found_matching_auth_tok;
1897 if (!found_auth_tok) {
1899 "authentication token\n");
1903 found_matching_auth_tok:
1910 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
1918 rc = decrypt_passphrase_encrypted_session_key(
1919 candidate_auth_tok, crypt_stat);
1929 "session key for authentication token with sig "
1930 "[%.*s]; rc = [%d]. Removing auth tok "
1931 "candidate from the list and searching for "
1933 candidate_auth_tok_sig, rc);
1935 auth_tok_list_item_tmp,
1936 &auth_tok_list,
list) {
1937 if (candidate_auth_tok
1938 == &auth_tok_list_item->
auth_tok) {
1941 ecryptfs_auth_tok_list_item_cache,
1942 auth_tok_list_item);
1943 goto find_next_matching_auth_tok;
1957 "context for cipher [%s]; rc = [%d]\n",
1961 wipe_auth_tok_list(&auth_tok_list);
1967 pki_encrypt_session_key(
struct key *auth_tok_key,
1973 char *payload =
NULL;
1974 size_t payload_len = 0;
1982 crypt_stat, &payload, &payload_len);
1998 "from the user space daemon\n");
2002 rc = parse_tag_67_packet(key_rec, msg);
2025 write_tag_1_packet(
char *
dest,
size_t *remaining_bytes,
2031 size_t encrypted_session_key_valid = 0;
2032 size_t packet_size_length;
2033 size_t max_packet_size;
2039 encrypted_session_key_valid = 0;
2040 for (i = 0; i < crypt_stat->
key_size; i++)
2041 encrypted_session_key_valid |=
2043 if (encrypted_session_key_valid) {
2049 goto encrypted_session_key_set;
2051 if (auth_tok->
session_key.encrypted_key_size == 0)
2054 rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat,
2058 "module; rc = [%d]\n", rc);
2065 encrypted_session_key_set:
2068 max_packet_size = (1
2074 if (max_packet_size > (*remaining_bytes)) {
2076 "need up to [%td] bytes, but there are only [%td] "
2077 "available\n", max_packet_size, (*remaining_bytes));
2083 (max_packet_size - 4),
2084 &packet_size_length);
2087 "header; cannot generate packet length\n");
2090 (*packet_size) += packet_size_length;
2091 dest[(*packet_size)++] = 0x03;
2102 (*remaining_bytes) -= (*packet_size);
2117 write_tag_11_packet(
char *dest,
size_t *remaining_bytes,
char *contents,
2118 size_t contents_length,
size_t *packet_length)
2120 size_t packet_size_length;
2121 size_t max_packet_size;
2124 (*packet_length) = 0;
2127 max_packet_size = (1
2134 if (max_packet_size > (*remaining_bytes)) {
2136 "need up to [%td] bytes, but there are only [%td] "
2137 "available\n", max_packet_size, (*remaining_bytes));
2143 (max_packet_size - 4),
2144 &packet_size_length);
2147 "generate packet length. rc = [%d]\n", rc);
2150 (*packet_length) += packet_size_length;
2151 dest[(*packet_length)++] = 0x62;
2152 dest[(*packet_length)++] = 8;
2153 memcpy(&dest[(*packet_length)],
"_CONSOLE", 8);
2154 (*packet_length) += 8;
2155 memset(&dest[(*packet_length)], 0x00, 4);
2156 (*packet_length) += 4;
2157 memcpy(&dest[(*packet_length)], contents, contents_length);
2158 (*packet_length) += contents_length;
2161 (*packet_length) = 0;
2163 (*remaining_bytes) -= (*packet_length);
2180 write_tag_3_packet(
char *dest,
size_t *remaining_bytes,
2186 size_t encrypted_session_key_valid = 0;
2192 size_t packet_size_length;
2193 size_t max_packet_size;
2209 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
2224 if (auth_tok->
session_key.encrypted_key_size == 0)
2235 encrypted_session_key_valid = 0;
2236 for (i = 0; i < auth_tok->
session_key.encrypted_key_size; i++)
2237 encrypted_session_key_valid |=
2239 if (encrypted_session_key_valid) {
2241 "using auth_tok->session_key.encrypted_key, "
2242 "where key_rec->enc_key_size = [%zd]\n",
2247 goto encrypted_session_key_set;
2252 "session key encryption key of size [%d]\n",
2254 session_key_encryption_key_bytes);
2255 memcpy(session_key_encryption_key,
2259 "Cached session key encryption key:\n");
2271 "for crypt_stat session key; expected rc = 1; "
2272 "got rc = [%d]. key_rec->enc_key_size = [%zd]\n",
2281 "for crypt_stat encrypted session key; "
2282 "expected rc = 1; got rc = [%d]. "
2283 "key_rec->enc_key_size = [%zd]\n", rc,
2289 rc = crypto_blkcipher_setkey(desc.
tfm, session_key_encryption_key,
2294 "context; rc = [%d]\n", rc);
2300 rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,
2301 (*key_rec).enc_key_size);
2314 encrypted_session_key_set:
2317 max_packet_size = (1
2326 if (max_packet_size > (*remaining_bytes)) {
2328 "there are only [%td] available\n", max_packet_size,
2329 (*remaining_bytes));
2337 (max_packet_size - 4),
2338 &packet_size_length);
2341 "generate packet length. rc = [%d]\n", rc);
2344 (*packet_size) += packet_size_length;
2345 dest[(*packet_size)++] = 0x04;
2350 if (cipher_code == 0) {
2352 "cipher [%s]\n", crypt_stat->
cipher);
2356 dest[(*packet_size)++] = cipher_code;
2357 dest[(*packet_size)++] = 0x03;
2358 dest[(*packet_size)++] = 0x01;
2362 dest[(*packet_size)++] = 0x60;
2370 (*remaining_bytes) -= (*packet_size);
2394 struct dentry *ecryptfs_dentry,
size_t *len,
2398 struct key *auth_tok_key =
NULL;
2400 &ecryptfs_superblock_to_private(
2401 ecryptfs_dentry->
d_sb)->mount_crypt_stat;
2416 memset(key_rec, 0,
sizeof(*key_rec));
2417 rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key,
2423 "sig = [%s]\n", key_sig->
keysig);
2424 rc = process_find_global_auth_tok_for_sig_err(rc);
2428 rc = write_tag_3_packet((dest_base + (*len)),
2430 crypt_stat, key_rec,
2436 "writing tag 3 packet\n");
2441 rc = write_tag_11_packet((dest_base + (*len)), &max,
2446 "auth tok signature packet\n");
2451 rc = write_tag_1_packet(dest_base + (*len), &max,
2452 auth_tok_key, auth_tok,
2453 crypt_stat, key_rec, &written);
2456 "writing tag 1 packet\n");
2464 "authentication token type\n");
2470 dest_base[(*len)] = 0x00;
2493 "Error allocating from ecryptfs_key_sig_cache\n");
2508 char *sig,
u32 global_auth_tok_flags)
2513 new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
2515 if (!new_auth_tok) {
2518 "ecryptfs_global_auth_tok_cache\n");
2522 new_auth_tok->
flags = global_auth_tok_flags;