Skip to content

Commit 9cec64d

Browse files
Luis Henriquesgregkh
Luis Henriques
authored andcommitted
ceph: fix buffer free while holding i_ceph_lock in __ceph_setxattr()
[ Upstream commit 86968ef ] Calling ceph_buffer_put() in __ceph_setxattr() may end up freeing the i_xattrs.prealloc_blob buffer while holding the i_ceph_lock. This can be fixed by postponing the call until later, when the lock is released. The following backtrace was triggered by fstests generic/117. BUG: sleeping function called from invalid context at mm/vmalloc.c:2283 in_atomic(): 1, irqs_disabled(): 0, pid: 650, name: fsstress 3 locks held by fsstress/650: #0: 00000000870a0fe8 (sb_writers#8){.+.+}, at: mnt_want_write+0x20/0x50 raspberrypi#1: 00000000ba0c4c74 (&type->i_mutex_dir_key#6){++++}, at: vfs_setxattr+0x55/0xa0 raspberrypi#2: 000000008dfbb3f2 (&(&ci->i_ceph_lock)->rlock){+.+.}, at: __ceph_setxattr+0x297/0x810 CPU: 1 PID: 650 Comm: fsstress Not tainted 5.2.0+ raspberrypi#437 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.12.1-0-ga5cab58-prebuilt.qemu.org 04/01/2014 Call Trace: dump_stack+0x67/0x90 ___might_sleep.cold+0x9f/0xb1 vfree+0x4b/0x60 ceph_buffer_release+0x1b/0x60 __ceph_setxattr+0x2b4/0x810 __vfs_setxattr+0x66/0x80 __vfs_setxattr_noperm+0x59/0xf0 vfs_setxattr+0x81/0xa0 setxattr+0x115/0x230 ? filename_lookup+0xc9/0x140 ? rcu_read_lock_sched_held+0x74/0x80 ? rcu_sync_lockdep_assert+0x2e/0x60 ? __sb_start_write+0x142/0x1a0 ? mnt_want_write+0x20/0x50 path_setxattr+0xba/0xd0 __x64_sys_lsetxattr+0x24/0x30 do_syscall_64+0x50/0x1c0 entry_SYSCALL_64_after_hwframe+0x49/0xbe RIP: 0033:0x7ff23514359a Signed-off-by: Luis Henriques <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Ilya Dryomov <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 3f452f4 commit 9cec64d

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

fs/ceph/xattr.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ int __ceph_setxattr(struct inode *inode, const char *name,
951951
struct ceph_inode_info *ci = ceph_inode(inode);
952952
struct ceph_mds_client *mdsc = ceph_sb_to_client(inode->i_sb)->mdsc;
953953
struct ceph_cap_flush *prealloc_cf = NULL;
954+
struct ceph_buffer *old_blob = NULL;
954955
int issued;
955956
int err;
956957
int dirty = 0;
@@ -1019,13 +1020,15 @@ int __ceph_setxattr(struct inode *inode, const char *name,
10191020
struct ceph_buffer *blob;
10201021

10211022
spin_unlock(&ci->i_ceph_lock);
1022-
dout(" preaallocating new blob size=%d\n", required_blob_size);
1023+
ceph_buffer_put(old_blob); /* Shouldn't be required */
1024+
dout(" pre-allocating new blob size=%d\n", required_blob_size);
10231025
blob = ceph_buffer_new(required_blob_size, GFP_NOFS);
10241026
if (!blob)
10251027
goto do_sync_unlocked;
10261028
spin_lock(&ci->i_ceph_lock);
1029+
/* prealloc_blob can't be released while holding i_ceph_lock */
10271030
if (ci->i_xattrs.prealloc_blob)
1028-
ceph_buffer_put(ci->i_xattrs.prealloc_blob);
1031+
old_blob = ci->i_xattrs.prealloc_blob;
10291032
ci->i_xattrs.prealloc_blob = blob;
10301033
goto retry;
10311034
}
@@ -1041,6 +1044,7 @@ int __ceph_setxattr(struct inode *inode, const char *name,
10411044
}
10421045

10431046
spin_unlock(&ci->i_ceph_lock);
1047+
ceph_buffer_put(old_blob);
10441048
if (lock_snap_rwsem)
10451049
up_read(&mdsc->snap_rwsem);
10461050
if (dirty)

0 commit comments

Comments
 (0)