Skip to content

Commit e0bcb42

Browse files
committed
Merge tag 'ecryptfs-4.15-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tyhicks/ecryptfs
Pull eCryptfs updates from Tyler Hicks: - miscellaneous code cleanups and refactoring - fix a possible use after free bug when unloading the module * tag 'ecryptfs-4.15-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tyhicks/ecryptfs: eCryptfs: constify attribute_group structures. ecryptfs: remove unnecessary i_version bump ecryptfs: use ARRAY_SIZE ecryptfs: Adjust four checks for null pointers ecryptfs: Return an error code only as a constant in ecryptfs_add_global_auth_tok() ecryptfs: Delete 21 error messages for a failed memory allocation eCryptfs: use after free in ecryptfs_release_messaging() ecryptfs: remove private bin2hex implementation ecryptfs: add missing \n to end of various error messages
2 parents b6b220b + 4670269 commit e0bcb42

8 files changed

Lines changed: 34 additions & 98 deletions

File tree

‎fs/ecryptfs/crypto.c‎

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,12 @@
3636
#include <linux/scatterlist.h>
3737
#include <linux/slab.h>
3838
#include <asm/unaligned.h>
39+
#include <linux/kernel.h>
3940
#include "ecryptfs_kernel.h"
4041

4142
#define DECRYPT 0
4243
#define ENCRYPT 1
4344

44-
/**
45-
* ecryptfs_to_hex
46-
* @dst: Buffer to take hex character representation of contents of
47-
* src; must be at least of size (src_size * 2)
48-
* @src: Buffer to be converted to a hex string representation
49-
* @src_size: number of bytes to convert
50-
*/
51-
void ecryptfs_to_hex(char *dst, char *src, size_t src_size)
52-
{
53-
int x;
54-
55-
for (x = 0; x < src_size; x++)
56-
sprintf(&dst[x * 2], "%.2x", (unsigned char)src[x]);
57-
}
58-
5945
/**
6046
* ecryptfs_from_hex
6147
* @dst: Buffer to take the bytes from src hex; must be at least of
@@ -899,8 +885,7 @@ static int ecryptfs_process_flags(struct ecryptfs_crypt_stat *crypt_stat,
899885
u32 flags;
900886

901887
flags = get_unaligned_be32(page_virt);
902-
for (i = 0; i < ((sizeof(ecryptfs_flag_map)
903-
/ sizeof(struct ecryptfs_flag_map_elem))); i++)
888+
for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
904889
if (flags & ecryptfs_flag_map[i].file_flag) {
905890
crypt_stat->flags |= ecryptfs_flag_map[i].local_flag;
906891
} else
@@ -937,8 +922,7 @@ void ecryptfs_write_crypt_stat_flags(char *page_virt,
937922
u32 flags = 0;
938923
int i;
939924

940-
for (i = 0; i < ((sizeof(ecryptfs_flag_map)
941-
/ sizeof(struct ecryptfs_flag_map_elem))); i++)
925+
for (i = 0; i < ARRAY_SIZE(ecryptfs_flag_map); i++)
942926
if (crypt_stat->flags & ecryptfs_flag_map[i].local_flag)
943927
flags |= ecryptfs_flag_map[i].file_flag;
944928
/* Version is in top 8 bits of the 32-bit flag vector */
@@ -1434,8 +1418,6 @@ int ecryptfs_read_metadata(struct dentry *ecryptfs_dentry)
14341418
page_virt = kmem_cache_alloc(ecryptfs_header_cache, GFP_USER);
14351419
if (!page_virt) {
14361420
rc = -ENOMEM;
1437-
printk(KERN_ERR "%s: Unable to allocate page_virt\n",
1438-
__func__);
14391421
goto out;
14401422
}
14411423
rc = ecryptfs_read_lower(page_virt, 0, crypt_stat->extent_size,
@@ -1522,9 +1504,6 @@ ecryptfs_encrypt_filename(struct ecryptfs_filename *filename,
15221504
filename->encrypted_filename =
15231505
kmalloc(filename->encrypted_filename_size, GFP_KERNEL);
15241506
if (!filename->encrypted_filename) {
1525-
printk(KERN_ERR "%s: Out of memory whilst attempting "
1526-
"to kmalloc [%zd] bytes\n", __func__,
1527-
filename->encrypted_filename_size);
15281507
rc = -ENOMEM;
15291508
goto out;
15301509
}
@@ -1669,12 +1648,10 @@ ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
16691648
BUG_ON(!mutex_is_locked(&key_tfm_list_mutex));
16701649

16711650
tmp_tfm = kmem_cache_alloc(ecryptfs_key_tfm_cache, GFP_KERNEL);
1672-
if (key_tfm != NULL)
1651+
if (key_tfm)
16731652
(*key_tfm) = tmp_tfm;
16741653
if (!tmp_tfm) {
16751654
rc = -ENOMEM;
1676-
printk(KERN_ERR "Error attempting to allocate from "
1677-
"ecryptfs_key_tfm_cache\n");
16781655
goto out;
16791656
}
16801657
mutex_init(&tmp_tfm->key_tfm_mutex);
@@ -1690,7 +1667,7 @@ ecryptfs_add_new_key_tfm(struct ecryptfs_key_tfm **key_tfm, char *cipher_name,
16901667
"cipher with name = [%s]; rc = [%d]\n",
16911668
tmp_tfm->cipher_name, rc);
16921669
kmem_cache_free(ecryptfs_key_tfm_cache, tmp_tfm);
1693-
if (key_tfm != NULL)
1670+
if (key_tfm)
16941671
(*key_tfm) = NULL;
16951672
goto out;
16961673
}
@@ -1881,7 +1858,7 @@ ecryptfs_decode_from_filename(unsigned char *dst, size_t *dst_size,
18811858
size_t src_byte_offset = 0;
18821859
size_t dst_byte_offset = 0;
18831860

1884-
if (dst == NULL) {
1861+
if (!dst) {
18851862
(*dst_size) = ecryptfs_max_decoded_size(src_size);
18861863
goto out;
18871864
}
@@ -1949,9 +1926,6 @@ int ecryptfs_encrypt_and_encode_filename(
19491926

19501927
filename = kzalloc(sizeof(*filename), GFP_KERNEL);
19511928
if (!filename) {
1952-
printk(KERN_ERR "%s: Out of memory whilst attempting "
1953-
"to kzalloc [%zd] bytes\n", __func__,
1954-
sizeof(*filename));
19551929
rc = -ENOMEM;
19561930
goto out;
19571931
}
@@ -1980,9 +1954,6 @@ int ecryptfs_encrypt_and_encode_filename(
19801954
+ encoded_name_no_prefix_size);
19811955
(*encoded_name) = kmalloc((*encoded_name_size) + 1, GFP_KERNEL);
19821956
if (!(*encoded_name)) {
1983-
printk(KERN_ERR "%s: Out of memory whilst attempting "
1984-
"to kzalloc [%zd] bytes\n", __func__,
1985-
(*encoded_name_size));
19861957
rc = -ENOMEM;
19871958
kfree(filename->encrypted_filename);
19881959
kfree(filename);
@@ -2064,9 +2035,6 @@ int ecryptfs_decode_and_decrypt_filename(char **plaintext_name,
20642035
name, name_size);
20652036
decoded_name = kmalloc(decoded_name_size, GFP_KERNEL);
20662037
if (!decoded_name) {
2067-
printk(KERN_ERR "%s: Out of memory whilst attempting "
2068-
"to kmalloc [%zd] bytes\n", __func__,
2069-
decoded_name_size);
20702038
rc = -ENOMEM;
20712039
goto out;
20722040
}

‎fs/ecryptfs/ecryptfs_kernel.h‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <crypto/skcipher.h>
3232
#include <keys/user-type.h>
3333
#include <keys/encrypted-type.h>
34+
#include <linux/kernel.h>
3435
#include <linux/fs.h>
3536
#include <linux/fs_stack.h>
3637
#include <linux/namei.h>
@@ -51,7 +52,13 @@
5152
#define ECRYPTFS_XATTR_NAME "user.ecryptfs"
5253

5354
void ecryptfs_dump_auth_tok(struct ecryptfs_auth_tok *auth_tok);
54-
extern void ecryptfs_to_hex(char *dst, char *src, size_t src_size);
55+
static inline void
56+
ecryptfs_to_hex(char *dst, char *src, size_t src_size)
57+
{
58+
char *end = bin2hex(dst, src, src_size);
59+
*end = '\0';
60+
}
61+
5562
extern void ecryptfs_from_hex(char *dst, char *src, int dst_size);
5663

5764
struct ecryptfs_key_record {

‎fs/ecryptfs/inode.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ static int ecryptfs_inode_set(struct inode *inode, void *opaque)
6464
/* i_size will be overwritten for encrypted regular files */
6565
fsstack_copy_inode_size(inode, lower_inode);
6666
inode->i_ino = lower_inode->i_ino;
67-
inode->i_version++;
6867
inode->i_mapping->a_ops = &ecryptfs_aops;
6968

7069
if (S_ISLNK(inode->i_mode))
@@ -334,9 +333,6 @@ static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,
334333

335334
dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
336335
if (!dentry_info) {
337-
printk(KERN_ERR "%s: Out of memory whilst attempting "
338-
"to allocate ecryptfs_dentry_info struct\n",
339-
__func__);
340336
dput(lower_dentry);
341337
return ERR_PTR(-ENOMEM);
342338
}

‎fs/ecryptfs/keystore.c‎

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -639,11 +639,9 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
639639
int rc = 0;
640640

641641
s = kzalloc(sizeof(*s), GFP_KERNEL);
642-
if (!s) {
643-
printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
644-
"[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
642+
if (!s)
645643
return -ENOMEM;
646-
}
644+
647645
(*packet_size) = 0;
648646
rc = ecryptfs_find_auth_tok_for_sig(
649647
&auth_tok_key,
@@ -687,7 +685,7 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
687685
* separator, and then the filename */
688686
s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE
689687
+ s->block_aligned_filename_size);
690-
if (dest == NULL) {
688+
if (!dest) {
691689
(*packet_size) = s->max_packet_size;
692690
goto out_unlock;
693691
}
@@ -714,9 +712,6 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
714712
s->block_aligned_filename = kzalloc(s->block_aligned_filename_size,
715713
GFP_KERNEL);
716714
if (!s->block_aligned_filename) {
717-
printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
718-
"kzalloc [%zd] bytes\n", __func__,
719-
s->block_aligned_filename_size);
720715
rc = -ENOMEM;
721716
goto out_unlock;
722717
}
@@ -769,10 +764,6 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
769764
s->hash_desc = kmalloc(sizeof(*s->hash_desc) +
770765
crypto_shash_descsize(s->hash_tfm), GFP_KERNEL);
771766
if (!s->hash_desc) {
772-
printk(KERN_ERR "%s: Out of kernel memory whilst attempting to "
773-
"kmalloc [%zd] bytes\n", __func__,
774-
sizeof(*s->hash_desc) +
775-
crypto_shash_descsize(s->hash_tfm));
776767
rc = -ENOMEM;
777768
goto out_release_free_unlock;
778769
}
@@ -925,11 +916,9 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
925916
(*filename_size) = 0;
926917
(*filename) = NULL;
927918
s = kzalloc(sizeof(*s), GFP_KERNEL);
928-
if (!s) {
929-
printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc "
930-
"[%zd] bytes of kernel memory\n", __func__, sizeof(*s));
919+
if (!s)
931920
return -ENOMEM;
932-
}
921+
933922
if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) {
934923
printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be "
935924
"at least [%d]\n", __func__, max_packet_size,
@@ -1015,9 +1004,6 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
10151004
s->decrypted_filename = kmalloc(s->block_aligned_filename_size,
10161005
GFP_KERNEL);
10171006
if (!s->decrypted_filename) {
1018-
printk(KERN_ERR "%s: Out of memory whilst attempting to "
1019-
"kmalloc [%zd] bytes\n", __func__,
1020-
s->block_aligned_filename_size);
10211007
rc = -ENOMEM;
10221008
goto out_unlock;
10231009
}
@@ -1097,9 +1083,6 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
10971083
}
10981084
(*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL);
10991085
if (!(*filename)) {
1100-
printk(KERN_ERR "%s: Out of memory whilst attempting to "
1101-
"kmalloc [%zd] bytes\n", __func__,
1102-
((*filename_size) + 1));
11031086
rc = -ENOMEM;
11041087
goto out_free_unlock;
11051088
}
@@ -1333,7 +1316,7 @@ parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
13331316
if ((*new_auth_tok)->session_key.encrypted_key_size
13341317
> ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
13351318
printk(KERN_WARNING "Tag 1 packet contains key larger "
1336-
"than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
1319+
"than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n");
13371320
rc = -EINVAL;
13381321
goto out;
13391322
}
@@ -2525,11 +2508,9 @@ int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
25252508
struct ecryptfs_key_sig *new_key_sig;
25262509

25272510
new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
2528-
if (!new_key_sig) {
2529-
printk(KERN_ERR
2530-
"Error allocating from ecryptfs_key_sig_cache\n");
2511+
if (!new_key_sig)
25312512
return -ENOMEM;
2532-
}
2513+
25332514
memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
25342515
new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
25352516
/* Caller must hold keysig_list_mutex */
@@ -2545,24 +2526,19 @@ ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
25452526
char *sig, u32 global_auth_tok_flags)
25462527
{
25472528
struct ecryptfs_global_auth_tok *new_auth_tok;
2548-
int rc = 0;
25492529

25502530
new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
25512531
GFP_KERNEL);
2552-
if (!new_auth_tok) {
2553-
rc = -ENOMEM;
2554-
printk(KERN_ERR "Error allocating from "
2555-
"ecryptfs_global_auth_tok_cache\n");
2556-
goto out;
2557-
}
2532+
if (!new_auth_tok)
2533+
return -ENOMEM;
2534+
25582535
memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
25592536
new_auth_tok->flags = global_auth_tok_flags;
25602537
new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
25612538
mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
25622539
list_add(&new_auth_tok->mount_crypt_stat_list,
25632540
&mount_crypt_stat->global_auth_tok_list);
25642541
mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
2565-
out:
2566-
return rc;
2542+
return 0;
25672543
}
25682544

‎fs/ecryptfs/main.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static int ecryptfs_parse_options(struct ecryptfs_sb_info *sbi, char *options,
426426
mount_crypt_stat->global_default_cipher_key_size);
427427
if (!cipher_code) {
428428
ecryptfs_printk(KERN_ERR,
429-
"eCryptfs doesn't support cipher: %s",
429+
"eCryptfs doesn't support cipher: %s\n",
430430
mount_crypt_stat->global_default_cipher_name);
431431
rc = -EINVAL;
432432
goto out;
@@ -781,7 +781,7 @@ static struct attribute *attributes[] = {
781781
NULL,
782782
};
783783

784-
static struct attribute_group attr_group = {
784+
static const struct attribute_group attr_group = {
785785
.attrs = attributes,
786786
};
787787

‎fs/ecryptfs/messaging.c‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
147147
(*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
148148
if (!(*daemon)) {
149149
rc = -ENOMEM;
150-
printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
151-
"GFP_KERNEL memory\n", __func__, sizeof(**daemon));
152150
goto out;
153151
}
154152
(*daemon)->file = file;
@@ -250,8 +248,6 @@ int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
250248
msg_ctx->msg = kmemdup(msg, msg_size, GFP_KERNEL);
251249
if (!msg_ctx->msg) {
252250
rc = -ENOMEM;
253-
printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
254-
"GFP_KERNEL memory\n", __func__, msg_size);
255251
goto unlock;
256252
}
257253
msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
@@ -386,7 +382,6 @@ int __init ecryptfs_init_messaging(void)
386382
GFP_KERNEL);
387383
if (!ecryptfs_daemon_hash) {
388384
rc = -ENOMEM;
389-
printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
390385
mutex_unlock(&ecryptfs_daemon_hash_mux);
391386
goto out;
392387
}
@@ -398,7 +393,6 @@ int __init ecryptfs_init_messaging(void)
398393
GFP_KERNEL);
399394
if (!ecryptfs_msg_ctx_arr) {
400395
rc = -ENOMEM;
401-
printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
402396
goto out;
403397
}
404398
mutex_init(&ecryptfs_msg_ctx_lists_mux);
@@ -442,15 +436,16 @@ void ecryptfs_release_messaging(void)
442436
}
443437
if (ecryptfs_daemon_hash) {
444438
struct ecryptfs_daemon *daemon;
439+
struct hlist_node *n;
445440
int i;
446441

447442
mutex_lock(&ecryptfs_daemon_hash_mux);
448443
for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
449444
int rc;
450445

451-
hlist_for_each_entry(daemon,
452-
&ecryptfs_daemon_hash[i],
453-
euid_chain) {
446+
hlist_for_each_entry_safe(daemon, n,
447+
&ecryptfs_daemon_hash[i],
448+
euid_chain) {
454449
rc = ecryptfs_exorcise_daemon(daemon);
455450
if (rc)
456451
printk(KERN_ERR "%s: Error whilst "

‎fs/ecryptfs/miscdev.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,8 @@ int ecryptfs_send_miscdev(char *data, size_t data_size,
163163
struct ecryptfs_message *msg;
164164

165165
msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
166-
if (!msg) {
167-
printk(KERN_ERR "%s: Out of memory whilst attempting "
168-
"to kmalloc(%zd, GFP_KERNEL)\n", __func__,
169-
(sizeof(*msg) + data_size));
166+
if (!msg)
170167
return -ENOMEM;
171-
}
172168

173169
mutex_lock(&msg_ctx->mux);
174170
msg_ctx->msg = msg;
@@ -383,7 +379,7 @@ ecryptfs_miscdev_write(struct file *file, const char __user *buf,
383379
goto memdup;
384380
} else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
385381
printk(KERN_WARNING "%s: Acceptable packet size range is "
386-
"[%d-%zu], but amount of data written is [%zu].",
382+
"[%d-%zu], but amount of data written is [%zu].\n",
387383
__func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
388384
return -EINVAL;
389385
}

0 commit comments

Comments
 (0)