History

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

Linus Torvalds committed 10 years ago

Commit 020ecbb2 parents ce6b714 74dae4213 files · +176 −39Browse files

Unified · Split

13 files
  1. fs/ext4/balloc.c +4 −3
  2. fs/ext4/crypto.c +56 −0
  3. fs/ext4/dir.c +11 −2
  4. fs/ext4/ext4.h +1 −0
  5. fs/ext4/extents.c +2 −2
  6. fs/ext4/file.c +9 −0
  7. fs/ext4/ialloc.c +4 −2
  8. fs/ext4/inode.c +50 −22
  9. fs/ext4/ioctl.c +1 −1
  10. fs/ext4/mballoc.c +1 −1
  11. fs/ext4/move_extent.c +12 −3
  12. fs/ext4/namei.c +24 −2
  13. fs/ext4/resize.c +1 −1
fs/ext4/balloc.c+4−3
@@ -191,7 +191,6 @@ static int ext4_init_block_bitmap(struct super_block *sb, · 190 unmodified lines
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) · 244 unmodified lines
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+56−0
@@ -467,3 +467,59 @@ uint32_t ext4_validate_encryption_key_size(uint32_t mode, uint32_t size) · 466 unmodified lines
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+11−2
@@ -111,6 +111,12 @@ static int ext4_readdir(struct file *file, struct dir_context *ctx) · 110 unmodified lines
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) · 40 unmodified lines
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+1−0
@@ -2302,6 +2302,7 @@ struct page *ext4_encrypt(struct inode *inode, · 2301 unmodified lines
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+2−2
@@ -3928,7 +3928,7 @@ static int · 3927 unmodified lines
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, · 412 unmodified lines
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+9−0
@@ -350,6 +350,7 @@ static int ext4_file_open(struct inode * inode, struct file * filp) · 349 unmodified lines
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) · 37 unmodified lines
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+4−2
@@ -76,7 +76,6 @@ static int ext4_init_inode_bitmap(struct super_block *sb, · 75 unmodified lines
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) · 108 unmodified lines
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+50−22
@@ -686,6 +686,34 @@ out_sem: · 685 unmodified lines
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, · 30 unmodified lines
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, · 956 unmodified lines
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, · 1561 unmodified lines
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+1−1
@@ -208,7 +208,7 @@ static int ext4_ioctl_setflags(struct inode *inode, · 207 unmodified lines
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+1−1
@@ -2285,7 +2285,7 @@ static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v) · 2284 unmodified lines
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);
fs/ext4/move_extent.c+12−3
@@ -265,11 +265,12 @@ move_extent_per_page(struct file *o_filp, struct inode *donor_inode, · 264 unmodified lines
265265 ext4_lblk_t orig_blk_offset, donor_blk_offset;
266266 unsigned long blocksize = orig_inode->i_sb->s_blocksize;
267267 unsigned int tmp_data_size, data_size, replaced_size;
268− int err2, jblocks, retries = 0;
268+ int i, err2, jblocks, retries = 0;
269269 int replaced_count = 0;
270270 int from = data_offset_in_page << orig_inode->i_blkbits;
271271 int blocks_per_page = PAGE_CACHE_SIZE >> orig_inode->i_blkbits;
272272 struct super_block *sb = orig_inode->i_sb;
273+ struct buffer_head *bh = NULL;
273274
274275 /*
275276 * It needs twice the amount of ordinary journal buffers because
@@ -380,8 +381,16 @@ data_copy: · 104 unmodified lines
380381 }
381382 /* Perform all necessary steps similar write_begin()/write_end()
382383 * but keeping in mind that i_size will not change */
383− *err = __block_write_begin(pagep[0], from, replaced_size,
384− ext4_get_block);
384+ if (!page_has_buffers(pagep[0]))
385+ create_empty_buffers(pagep[0], 1 << orig_inode->i_blkbits, 0);
386+ bh = page_buffers(pagep[0]);
387+ for (i = 0; i < data_offset_in_page; i++)
388+ bh = bh->b_this_page;
389+ for (i = 0; i < block_len_in_page; i++) {
390+ *err = ext4_get_block(orig_inode, orig_blk_offset + i, bh, 0);
391+ if (*err < 0)
392+ break;
393+ }
385394 if (!*err)
386395 *err = block_commit_write(pagep[0], from, from + replaced_size);
387396
fs/ext4/namei.c+24−2
@@ -1558,6 +1558,24 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi · 1557 unmodified lines
15581558 struct ext4_dir_entry_2 *de;
15591559 struct buffer_head *bh;
15601560
1561+ if (ext4_encrypted_inode(dir)) {
1562+ int res = ext4_get_encryption_info(dir);
1563+
1564+ /*
1565+ * This should be a properly defined flag for
1566+ * dentry->d_flags when we uplift this to the VFS.
1567+ * d_fsdata is set to (void *) 1 if if the dentry is
1568+ * created while the directory was encrypted and we
1569+ * don't have access to the key.
1570+ */
1571+ dentry->d_fsdata = NULL;
1572+ if (ext4_encryption_info(dir))
1573+ dentry->d_fsdata = (void *) 1;
1574+ d_set_d_op(dentry, &ext4_encrypted_d_ops);
1575+ if (res && res != -ENOKEY)
1576+ return ERR_PTR(res);
1577+ }
1578+
15611579 if (dentry->d_name.len > EXT4_NAME_LEN)
15621580 return ERR_PTR(-ENAMETOOLONG);
15631581
@@ -1585,11 +1603,15 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi · 21 unmodified lines
15851603 return ERR_PTR(-EFSCORRUPTED);
15861604 }
15871605 if (!IS_ERR(inode) && ext4_encrypted_inode(dir) &&
1588− (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1589− S_ISLNK(inode->i_mode)) &&
1606+ (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
15901607 !ext4_is_child_context_consistent_with_parent(dir,
15911608 inode)) {
1609+ int nokey = ext4_encrypted_inode(inode) &&
1610+ !ext4_encryption_info(inode);
1611+
15921612 iput(inode);
1613+ if (nokey)
1614+ return ERR_PTR(-ENOKEY);
15931615 ext4_warning(inode->i_sb,
15941616 "Inconsistent encryption contexts: %lu/%lu\n",
15951617 (unsigned long) dir->i_ino,
fs/ext4/resize.c+1−1
@@ -198,7 +198,7 @@ static struct ext4_new_flex_group_data *alloc_flex_gd(unsigned long flexbg_size) · 197 unmodified lines
198198 if (flex_gd == NULL)
199199 goto out3;
200200
201− if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_flex_group_data))
201+ if (flexbg_size >= UINT_MAX / sizeof(struct ext4_new_group_data))
202202 goto out2;
203203 flex_gd->count = flexbg_size;
204204

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t