aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorJaegeuk Kim <jaegeuk@kernel.org>2017-04-24 13:00:08 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2017-05-25 09:44:38 -0400
commit8daed21dbce1d28fd082ef6f2faf8990ccebfd6f (patch)
tree3b7ac24183cb28ff4b6f3d11133143761d4dd318 /fs
parentb9c0da6219e19901c5bea6e5c19514929dae9e18 (diff)
f2fs: check entire encrypted bigname when finding a dentry
commit 6332cd32c8290a80e929fc044dc5bdba77396e33 upstream. If user has no key under an encrypted dir, fscrypt gives digested dentries. Previously, when looking up a dentry, f2fs only checks its hash value with first 4 bytes of the digested dentry, which didn't handle hash collisions fully. This patch enhances to check entire dentry bytes likewise ext4. Eric reported how to reproduce this issue by: # seq -f "edir/abcdefghijklmnopqrstuvwxyz012345%.0f" 100000 | xargs touch # find edir -type f | xargs stat -c %i | sort | uniq | wc -l 100000 # sync # echo 3 > /proc/sys/vm/drop_caches # keyctl new_session # find edir -type f | xargs stat -c %i | sort | uniq | wc -l 99999 Cc: <stable@vger.kernel.org> Reported-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> (fixed f2fs_dentry_hash() to work even when the hash is 0) Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'fs')
-rw-r--r--fs/f2fs/dir.c37
-rw-r--r--fs/f2fs/f2fs.h3
-rw-r--r--fs/f2fs/hash.c7
-rw-r--r--fs/f2fs/inline.c4
4 files changed, 31 insertions, 20 deletions
diff --git a/fs/f2fs/dir.c b/fs/f2fs/dir.c
index ebdc90fc71b7..e32d82b011a9 100644
--- a/fs/f2fs/dir.c
+++ b/fs/f2fs/dir.c
@@ -130,19 +130,29 @@ struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
130 continue; 130 continue;
131 } 131 }
132 132
133 /* encrypted case */ 133 if (de->hash_code != namehash)
134 goto not_match;
135
134 de_name.name = d->filename[bit_pos]; 136 de_name.name = d->filename[bit_pos];
135 de_name.len = le16_to_cpu(de->name_len); 137 de_name.len = le16_to_cpu(de->name_len);
136 138
137 /* show encrypted name */ 139#ifdef CONFIG_F2FS_FS_ENCRYPTION
138 if (fname->hash) { 140 if (unlikely(!name->name)) {
139 if (de->hash_code == fname->hash) 141 if (fname->usr_fname->name[0] == '_') {
140 goto found; 142 if (de_name.len >= 16 &&
141 } else if (de_name.len == name->len && 143 !memcmp(de_name.name + de_name.len - 16,
142 de->hash_code == namehash && 144 fname->crypto_buf.name + 8, 16))
143 !memcmp(de_name.name, name->name, name->len)) 145 goto found;
146 goto not_match;
147 }
148 name->name = fname->crypto_buf.name;
149 name->len = fname->crypto_buf.len;
150 }
151#endif
152 if (de_name.len == name->len &&
153 !memcmp(de_name.name, name->name, name->len))
144 goto found; 154 goto found;
145 155not_match:
146 if (max_slots && max_len > *max_slots) 156 if (max_slots && max_len > *max_slots)
147 *max_slots = max_len; 157 *max_slots = max_len;
148 max_len = 0; 158 max_len = 0;
@@ -170,12 +180,7 @@ static struct f2fs_dir_entry *find_in_level(struct inode *dir,
170 struct f2fs_dir_entry *de = NULL; 180 struct f2fs_dir_entry *de = NULL;
171 bool room = false; 181 bool room = false;
172 int max_slots; 182 int max_slots;
173 f2fs_hash_t namehash; 183 f2fs_hash_t namehash = f2fs_dentry_hash(&name, fname);
174
175 if(fname->hash)
176 namehash = cpu_to_le32(fname->hash);
177 else
178 namehash = f2fs_dentry_hash(&name);
179 184
180 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level); 185 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
181 nblock = bucket_blocks(level); 186 nblock = bucket_blocks(level);
@@ -539,7 +544,7 @@ int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
539 544
540 level = 0; 545 level = 0;
541 slots = GET_DENTRY_SLOTS(new_name->len); 546 slots = GET_DENTRY_SLOTS(new_name->len);
542 dentry_hash = f2fs_dentry_hash(new_name); 547 dentry_hash = f2fs_dentry_hash(new_name, NULL);
543 548
544 current_depth = F2FS_I(dir)->i_current_depth; 549 current_depth = F2FS_I(dir)->i_current_depth;
545 if (F2FS_I(dir)->chash == dentry_hash) { 550 if (F2FS_I(dir)->chash == dentry_hash) {
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 3a1640be7ffc..c12f695923b6 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -2016,7 +2016,8 @@ int sanity_check_ckpt(struct f2fs_sb_info *sbi);
2016/* 2016/*
2017 * hash.c 2017 * hash.c
2018 */ 2018 */
2019f2fs_hash_t f2fs_dentry_hash(const struct qstr *); 2019f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
2020 struct fscrypt_name *fname);
2020 2021
2021/* 2022/*
2022 * node.c 2023 * node.c
diff --git a/fs/f2fs/hash.c b/fs/f2fs/hash.c
index 71b7206c431e..eb2e031ea887 100644
--- a/fs/f2fs/hash.c
+++ b/fs/f2fs/hash.c
@@ -70,7 +70,8 @@ static void str2hashbuf(const unsigned char *msg, size_t len,
70 *buf++ = pad; 70 *buf++ = pad;
71} 71}
72 72
73f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info) 73f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
74 struct fscrypt_name *fname)
74{ 75{
75 __u32 hash; 76 __u32 hash;
76 f2fs_hash_t f2fs_hash; 77 f2fs_hash_t f2fs_hash;
@@ -79,6 +80,10 @@ f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info)
79 const unsigned char *name = name_info->name; 80 const unsigned char *name = name_info->name;
80 size_t len = name_info->len; 81 size_t len = name_info->len;
81 82
83 /* encrypted bigname case */
84 if (fname && !fname->disk_name.name)
85 return cpu_to_le32(fname->hash);
86
82 if (is_dot_dotdot(name_info)) 87 if (is_dot_dotdot(name_info))
83 return 0; 88 return 0;
84 89
diff --git a/fs/f2fs/inline.c b/fs/f2fs/inline.c
index 5f1a67f756af..a21faa1c6817 100644
--- a/fs/f2fs/inline.c
+++ b/fs/f2fs/inline.c
@@ -294,7 +294,7 @@ struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
294 return NULL; 294 return NULL;
295 } 295 }
296 296
297 namehash = f2fs_dentry_hash(&name); 297 namehash = f2fs_dentry_hash(&name, fname);
298 298
299 inline_dentry = inline_data_addr(ipage); 299 inline_dentry = inline_data_addr(ipage);
300 300
@@ -531,7 +531,7 @@ int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
531 531
532 f2fs_wait_on_page_writeback(ipage, NODE, true); 532 f2fs_wait_on_page_writeback(ipage, NODE, true);
533 533
534 name_hash = f2fs_dentry_hash(new_name); 534 name_hash = f2fs_dentry_hash(new_name, NULL);
535 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2); 535 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
536 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos); 536 f2fs_update_dentry(ino, mode, &d, new_name, name_hash, bit_pos);
537 537