Skip to content

Commit 020ecbb

Browse files
committed
Merge tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4
Pull ext4 bugfixes from Ted Ts'o: "Miscellaneous ext4 bug fixes for v4.5" * tag 'ext4_for_linus_stable' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: ext4: fix crashes in dioread_nolock mode ext4: fix bh->b_state corruption ext4: fix memleak in ext4_readdir() ext4: remove unused parameter "newblock" in convert_initialized_extent() ext4: don't read blocks from disk after extents being swapped ext4: fix potential integer overflow ext4: add a line break for proc mb_groups display ext4: ioctl: fix erroneous return value ext4: fix scheduling in atomic on group checksum failure ext4 crypto: move context consistency check to ext4_file_open() ext4 crypto: revalidate dentry after adding or removing the key
2 parents ce6b714 + 74dae42 commit 020ecbb

13 files changed

Lines changed: 176 additions & 39 deletions

File tree

‎fs/ext4/balloc.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ static int ext4_init_block_bitmap(struct super_block *sb,
191191
/* If checksum is bad mark all blocks used to prevent allocation
192192
* essentially implementing a per-group read-only flag. */
193193
if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
194-
ext4_error(sb, "Checksum bad for group %u", block_group);
195194
grp = ext4_get_group_info(sb, block_group);
196195
if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
197196
percpu_counter_sub(&sbi->s_freeclusters_counter,
@@ -442,14 +441,16 @@ ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
442441
}
443442
ext4_lock_group(sb, block_group);
444443
if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
445-
446444
err = ext4_init_block_bitmap(sb, bh, block_group, desc);
447445
set_bitmap_uptodate(bh);
448446
set_buffer_uptodate(bh);
449447
ext4_unlock_group(sb, block_group);
450448
unlock_buffer(bh);
451-
if (err)
449+
if (err) {
450+
ext4_error(sb, "Failed to init block bitmap for group "
451+
"%u: %d", block_group, err);
452452
goto out;
453+
}
453454
goto verify;
454455
}
455456
ext4_unlock_group(sb, block_group);

‎fs/ext4/crypto.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,59 @@ uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size)
467467
return size;
468468
return 0;
469469
}
470+
471+
/*
472+
* Validate dentries for encrypted directories to make sure we aren't
473+
* potentially caching stale data after a key has been added or
474+
* removed.
475+
*/
476+
static int ext4_d_revalidate(struct dentry *dentry, unsigned int flags)
477+
{
478+
struct inode *dir = d_inode(dentry->d_parent);
479+
struct ext4_crypt_info *ci = EXT4_I(dir)->i_crypt_info;
480+
int dir_has_key, cached_with_key;
481+
482+
if (!ext4_encrypted_inode(dir))
483+
return 0;
484+
485+
if (ci && ci->ci_keyring_key &&
486+
(ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
487+
(1 << KEY_FLAG_REVOKED) |
488+
(1 << KEY_FLAG_DEAD))))
489+
ci = NULL;
490+
491+
/* this should eventually be an flag in d_flags */
492+
cached_with_key = dentry->d_fsdata != NULL;
493+
dir_has_key = (ci != NULL);
494+
495+
/*
496+
* If the dentry was cached without the key, and it is a
497+
* negative dentry, it might be a valid name. We can't check
498+
* if the key has since been made available due to locking
499+
* reasons, so we fail the validation so ext4_lookup() can do
500+
* this check.
501+
*
502+
* We also fail the validation if the dentry was created with
503+
* the key present, but we no longer have the key, or vice versa.
504+
*/
505+
if ((!cached_with_key && d_is_negative(dentry)) ||
506+
(!cached_with_key && dir_has_key) ||
507+
(cached_with_key && !dir_has_key)) {
508+
#if 0 /* Revalidation debug */
509+
char buf[80];
510+
char *cp = simple_dname(dentry, buf, sizeof(buf));
511+
512+
if (IS_ERR(cp))
513+
cp = (char *) "???";
514+
pr_err("revalidate: %s %p %d %d %d\n", cp, dentry->d_fsdata,
515+
cached_with_key, d_is_negative(dentry),
516+
dir_has_key);
517+
#endif
518+
return 0;
519+
}
520+
return 1;
521+
}
522+
523+
const struct dentry_operations ext4_encrypted_d_ops = {
524+
.d_revalidate = ext4_d_revalidate,
525+
};

‎fs/ext4/dir.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
111111
int dir_has_error = 0;
112112
struct ext4_str fname_crypto_str = {.name = NULL, .len = 0};
113113

114+
if (ext4_encrypted_inode(inode)) {
115+
err = ext4_get_encryption_info(inode);
116+
if (err && err != -ENOKEY)
117+
return err;
118+
}
119+
114120
if (is_dx_dir(inode)) {
115121
err = ext4_dx_readdir(file, ctx);
116122
if (err != ERR_BAD_DX_DIR) {
@@ -157,8 +163,11 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx)
157163
index, 1);
158164
file->f_ra.prev_pos = (loff_t)index << PAGE_CACHE_SHIFT;
159165
bh = ext4_bread(NULL, inode, map.m_lblk, 0);
160-
if (IS_ERR(bh))
161-
return PTR_ERR(bh);
166+
if (IS_ERR(bh)) {
167+
err = PTR_ERR(bh);
168+
bh = NULL;
169+
goto errout;
170+
}
162171
}
163172

164173
if (!bh) {

‎fs/ext4/ext4.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,7 @@ struct page *ext4_encrypt(struct inode *inode,
23022302
int ext4_decrypt(struct page *page);
23032303
int ext4_encrypted_zeroout(struct inode *inode, ext4_lblk_t lblk,
23042304
ext4_fsblk_t pblk, ext4_lblk_t len);
2305+
extern const struct dentry_operations ext4_encrypted_d_ops;
23052306

23062307
#ifdef CONFIG_EXT4_FS_ENCRYPTION
23072308
int ext4_init_crypto(void);

‎fs/ext4/extents.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,7 +3928,7 @@ static int
39283928
convert_initialized_extent(handle_t *handle, struct inode *inode,
39293929
struct ext4_map_blocks *map,
39303930
struct ext4_ext_path **ppath, int flags,
3931-
unsigned int allocated, ext4_fsblk_t newblock)
3931+
unsigned int allocated)
39323932
{
39333933
struct ext4_ext_path *path = *ppath;
39343934
struct ext4_extent *ex;
@@ -4347,7 +4347,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
43474347
(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
43484348
allocated = convert_initialized_extent(
43494349
handle, inode, map, &path,
4350-
flags, allocated, newblock);
4350+
flags, allocated);
43514351
goto out2;
43524352
} else if (!ext4_ext_is_unwritten(ex))
43534353
goto out;

‎fs/ext4/file.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
350350
struct super_block *sb = inode->i_sb;
351351
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
352352
struct vfsmount *mnt = filp->f_path.mnt;
353+
struct inode *dir = filp->f_path.dentry->d_parent->d_inode;
353354
struct path path;
354355
char buf[64], *cp;
355356
int ret;
@@ -393,6 +394,14 @@ static int ext4_file_open(struct inode * inode, struct file * filp)
393394
if (ext4_encryption_info(inode) == NULL)
394395
return -ENOKEY;
395396
}
397+
if (ext4_encrypted_inode(dir) &&
398+
!ext4_is_child_context_consistent_with_parent(dir, inode)) {
399+
ext4_warning(inode->i_sb,
400+
"Inconsistent encryption contexts: %lu/%lu\n",
401+
(unsigned long) dir->i_ino,
402+
(unsigned long) inode->i_ino);
403+
return -EPERM;
404+
}
396405
/*
397406
* Set up the jbd2_inode if we are opening the inode for
398407
* writing and the journal is present

‎fs/ext4/ialloc.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ static int ext4_init_inode_bitmap(struct super_block *sb,
7676
/* If checksum is bad mark all blocks and inodes use to prevent
7777
* allocation, essentially implementing a per-group read-only flag. */
7878
if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
79-
ext4_error(sb, "Checksum bad for group %u", block_group);
8079
grp = ext4_get_group_info(sb, block_group);
8180
if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
8281
percpu_counter_sub(&sbi->s_freeclusters_counter,
@@ -191,8 +190,11 @@ ext4_read_inode_bitmap(struct super_block *sb, ext4_group_t block_group)
191190
set_buffer_verified(bh);
192191
ext4_unlock_group(sb, block_group);
193192
unlock_buffer(bh);
194-
if (err)
193+
if (err) {
194+
ext4_error(sb, "Failed to init inode bitmap for group "
195+
"%u: %d", block_group, err);
195196
goto out;
197+
}
196198
return bh;
197199
}
198200
ext4_unlock_group(sb, block_group);

‎fs/ext4/inode.c‎

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,34 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
686686
return retval;
687687
}
688688

689+
/*
690+
* Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
691+
* we have to be careful as someone else may be manipulating b_state as well.
692+
*/
693+
static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
694+
{
695+
unsigned long old_state;
696+
unsigned long new_state;
697+
698+
flags &= EXT4_MAP_FLAGS;
699+
700+
/* Dummy buffer_head? Set non-atomically. */
701+
if (!bh->b_page) {
702+
bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
703+
return;
704+
}
705+
/*
706+
* Someone else may be modifying b_state. Be careful! This is ugly but
707+
* once we get rid of using bh as a container for mapping information
708+
* to pass to / from get_block functions, this can go away.
709+
*/
710+
do {
711+
old_state = READ_ONCE(bh->b_state);
712+
new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
713+
} while (unlikely(
714+
cmpxchg(&bh->b_state, old_state, new_state) != old_state));
715+
}
716+
689717
/* Maximum number of blocks we map for direct IO at once. */
690718
#define DIO_MAX_BLOCKS 4096
691719

@@ -722,7 +750,7 @@ static int _ext4_get_block(struct inode *inode, sector_t iblock,
722750
ext4_io_end_t *io_end = ext4_inode_aio(inode);
723751

724752
map_bh(bh, inode->i_sb, map.m_pblk);
725-
bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
753+
ext4_update_bh_state(bh, map.m_flags);
726754
if (io_end && io_end->flag & EXT4_IO_END_UNWRITTEN)
727755
set_buffer_defer_completion(bh);
728756
bh->b_size = inode->i_sb->s_blocksize * map.m_len;
@@ -1685,7 +1713,7 @@ int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
16851713
return ret;
16861714

16871715
map_bh(bh, inode->i_sb, map.m_pblk);
1688-
bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1716+
ext4_update_bh_state(bh, map.m_flags);
16891717

16901718
if (buffer_unwritten(bh)) {
16911719
/* A delayed write to unwritten bh should be marked
@@ -3253,29 +3281,29 @@ static ssize_t ext4_ext_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
32533281
* case, we allocate an io_end structure to hook to the iocb.
32543282
*/
32553283
iocb->private = NULL;
3256-
ext4_inode_aio_set(inode, NULL);
3257-
if (!is_sync_kiocb(iocb)) {
3258-
io_end = ext4_init_io_end(inode, GFP_NOFS);
3259-
if (!io_end) {
3260-
ret = -ENOMEM;
3261-
goto retake_lock;
3262-
}
3263-
/*
3264-
* Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3265-
*/
3266-
iocb->private = ext4_get_io_end(io_end);
3267-
/*
3268-
* we save the io structure for current async direct
3269-
* IO, so that later ext4_map_blocks() could flag the
3270-
* io structure whether there is a unwritten extents
3271-
* needs to be converted when IO is completed.
3272-
*/
3273-
ext4_inode_aio_set(inode, io_end);
3274-
}
3275-
32763284
if (overwrite) {
32773285
get_block_func = ext4_get_block_overwrite;
32783286
} else {
3287+
ext4_inode_aio_set(inode, NULL);
3288+
if (!is_sync_kiocb(iocb)) {
3289+
io_end = ext4_init_io_end(inode, GFP_NOFS);
3290+
if (!io_end) {
3291+
ret = -ENOMEM;
3292+
goto retake_lock;
3293+
}
3294+
/*
3295+
* Grab reference for DIO. Will be dropped in
3296+
* ext4_end_io_dio()
3297+
*/
3298+
iocb->private = ext4_get_io_end(io_end);
3299+
/*
3300+
* we save the io structure for current async direct
3301+
* IO, so that later ext4_map_blocks() could flag the
3302+
* io structure whether there is a unwritten extents
3303+
* needs to be converted when IO is completed.
3304+
*/
3305+
ext4_inode_aio_set(inode, io_end);
3306+
}
32793307
get_block_func = ext4_get_block_write;
32803308
dio_flags = DIO_LOCKING;
32813309
}

‎fs/ext4/ioctl.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static int ext4_ioctl_setflags(struct inode *inode,
208208
{
209209
struct ext4_inode_info *ei = EXT4_I(inode);
210210
handle_t *handle = NULL;
211-
int err = EPERM, migrate = 0;
211+
int err = -EPERM, migrate = 0;
212212
struct ext4_iloc iloc;
213213
unsigned int oldflags, mask, i;
214214
unsigned int jflag;

‎fs/ext4/mballoc.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2285,7 +2285,7 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
22852285
if (group == 0)
22862286
seq_puts(seq, "#group: free frags first ["
22872287
" 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
2288-
" 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]");
2288+
" 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
22892289

22902290
i = (sb->s_blocksize_bits + 2) * sizeof(sg.info.bb_counters[0]) +
22912291
sizeof(struct ext4_group_info);

0 commit comments

Comments
 (0)