ovl: do not hash ovl dentry when inode is NULL in ovl_lookup
PATCH 1/1 ovl: do not hash ovl dentry when inode is NULL in ovl_lookup
The dentry is hashed even if the inode is NULL in ovl_lookup and later we get this dentry in lookup_dcache. But this dentry may become stale when someone could (mistakenly or maliciously) manually unlink the whiteout file directly from upperdir. So the NUll pointer error occur when create the same name directory and system crash.
Simple reproducer:
$ mkdir lower upper work merge $ touch lower/file $ mount -t overlay overlay -olowerdir=lower,upperdir=upper,workdir=work merge $ rm merge/file $ ls -al merge/file ls: cannot access merge/file: No such file or directory $ rm upper/file $ ls -al merge/ ls: cannot access merge/file: No such file or directory total 0 drwxr-xr-x 1 root root 6 Oct 30 09:16 . drwxr-xr-x 6 root root 57 Oct 30 09:15 .. -????????? ? ? ? ? ? file $ mkdir merge/file Killed
[ 2313.705789] BUG: unable to handle kernel NULL pointer dereference at 0000000000000000 [ 2313.706124] PGD 80000005082af067 P4D 80000005082af067 PUD 4f9133067 PMD 0 [ 2313.706408] Oops: 0000 [#1] SMP PTI [ 2313.706592] CPU: 1 PID: 6162 Comm: mkdir Not tainted 4.19.0-tlinux2+ #3 [ 2313.706861] Hardware name: innotek GmbH VirtualBox/VirtualBox, BIOS VirtualBox 12/01/2006 [ 2313.707304] RIP: 0010:xfs_vn_rename+0x4d/0x150 [xfs] [ 2313.707619] Code: 00 00 00 48 89 44 24 30 31 c0 41 f7 c0 f8 ff ff ff 0f 85 ec 00 00 00 41 [ 2313.708495] RSP: 0018:ffffa1b18746bc78 EFLAGS: 00010202 [ 2313.708824] RAX: 0000000000000000 RBX: 0000000000000002 RCX: ffff9171b9db5c00 [ 2313.709198] RDX: ffff9171c26dc8c8 RSI: ffff9171ba113480 RDI: ffff9171ba025b88 [ 2313.709573] RBP: ffff9171b9db5c00 R08: 0000000000000002 R09: 0000000000000002 [ 2313.709947] R10: 0000000000000000 R11: 000000007fffffff R12: ffff9171ba113480 [ 2313.710324] R13: ffff9171ba025b88 R14: ffff9171c26dc8c8 R15: 0000000000000000 [ 2313.710699] FS: 00007fb6c909b800(0000) GS:ffff9171cba80000(0000) knlGS:0000000000000000 [ 2313.711187] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 2313.711524] CR2: 0000000000000000 CR3: 000000050691a000 CR4: 00000000000406e0 [ 2313.711900] Call Trace: [ 2313.712949] vfs_rename+0x1d8/0x740 [ 2313.713243] ? ovl_check_setxattr+0x5c/0xf0 [overlay] [ 2313.713579] ovl_do_rename+0x36/0xa0 [overlay] [ 2313.713884] ovl_create_or_link+0x508/0x5a0 [overlay] [ 2313.714209] ? ovl_sync_fs+0x10/0x50 [overlay] [ 2313.714514] ? kmem_cache_alloc+0x190/0x1a0 [ 2313.714809] ? inode_init_always+0x110/0x1c0 [ 2313.715105] ? inode_sb_list_add+0x47/0x80 [ 2313.715397] ? ovl_fill_inode+0x134/0x140 [overlay] [ 2313.715711] ovl_create_object+0xda/0x110 [overlay] [ 2313.716027] vfs_mkdir+0xbf/0x160 [ 2313.716297] do_mkdirat+0x77/0xe0 [ 2313.716602] do_syscall_64+0x3f/0xf0 [ 2313.716891] entry_SYSCALL_64_after_hwframe+0x44/0xa9
--- fs/overlayfs/namei.c | 2 ++ 1 file changed, 2 insertions(+)
fs/overlayfs/namei.c +2 -0
@@ -1075,6 +1075,8 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,dput (index);kfree (stack);kfree (d.redirect); +if (!inode) +return NULL ;return d_splice_alias (inode, dentry); out_free_oe:
Signature · 4 lines
1.8.3.1 -- kaixuxia
Miklos Szeredi
maintainerQuoted 24 lines · kaixuxia
The dentry is hashed even if the inode is NULL in
ovl_lookupand later we get this dentry inlookup_dcache. But this dentry may become stale when someone could (mistakenly or maliciously) manually unlink the whiteout file directly from upperdir. So the NUll pointer error occur when create the same name directory and system crash.Simple reproducer:
$ mkdir lower upper work merge $ touch lower/file $ mount -t overlay overlay -olowerdir=lower,upperdir=upper,workdir=work merge $ rm merge/file $ ls -al merge/file ls: cannot access merge/file: No such file or directory $ rm upper/file $ ls -al merge/ ls: cannot access merge/file: No such file or directory total 0 drwxr-xr-x 1 root root 6 Oct 30 09:16 . drwxr-xr-x 6 root root 57 Oct 30 09:15 .. -????????? ? ? ? ? ? file $ mkdir merge/file Killed
Quoted 2 lines
[ 2313.705789] BUG: unable to handle kernel NULL pointer dereference at0000000000000000
Good catch!
Not hashing a negative dentry is not a good solution, however, because in the normal case cached negative dentries are quite useful and improve performance of various workloads.
Here's a tested patch that should fix the real issue: vfs_rename() with RENAME_EXCHANGE must get two positive dentries.
Amir, we need a test for this in one of the suites. Which one is more appropriate, unionmount-testsuite or xfstests?
Thanks,
Miklos
---
From: Miklos Szeredi <[email protected]>
Subject: ovl: check whiteout in ovl_create_over_whiteout()
Kaixuxia repors that it's possible to crash overlayfs by removing the whiteout on the upper layer before creating an object over it. This is a reproducer:
mkdir lower upper work merge
touch lower/file
mount -t overlay overlay -olowerdir=lower,upperdir=upper,workdir=work merge rm merge/file
ls -al merge/file
rm upper/file
ls -al merge/
mkdir merge/file
Before commencing with a vfs_rename(..., RENAME_EXCHANGE) verify that the lookup of "upper" is positive and is a whiteout, and return ESTALE otherwise.
Reported by: kaixuxia <[email protected]>
--- fs/overlayfs/dir.c | 4 ++++ 1 file changed, 4 insertions(+)
fs/overlayfs/dir.c +4 -0
@@ -462,6 +462,10 @@ static int ovl_create_over_whiteout(struif (IS_ERR (upper)) goto out_unlock; + err = -ESTALE ; +if (d_is_negative (upper) || !IS_WHITEOUT (d_inode (upper))) + goto out_dput; + newdentry =ovl_create_temp (workdir, cattr); err =PTR_ERR (newdentry);if (IS_ERR (newdentry))
Amir Goldstein
maintainerQuoted 35 lines · Miklos Szeredi
Quoted 24 lines · kaixuxia
The dentry is hashed even if the inode is NULL in
ovl_lookupand later we get this dentry inlookup_dcache. But this dentry may become stale when someone could (mistakenly or maliciously) manually unlink the whiteout file directly from upperdir. So the NUll pointer error occur when create the same name directory and system crash.Simple reproducer:
$ mkdir lower upper work merge $ touch lower/file $ mount -t overlay overlay -olowerdir=lower,upperdir=upper,workdir=work merge $ rm merge/file $ ls -al merge/file ls: cannot access merge/file: No such file or directory $ rm upper/file $ ls -al merge/ ls: cannot access merge/file: No such file or directory total 0 drwxr-xr-x 1 root root 6 Oct 30 09:16 . drwxr-xr-x 6 root root 57 Oct 30 09:15 .. -????????? ? ? ? ? ? file $ mkdir merge/file KilledQuoted 2 lines
[ 2313.705789] BUG: unable to handle kernel NULL pointer dereference at0000000000000000
Good catch!
Not hashing a negative dentry is not a good solution, however, because in the normal case cached negative dentries are quite useful and improve performance of various workloads.
Quoted 5 lines
Here's a tested patch that should fix the real issue:
vfs_rename()withRENAME_EXCHANGEmust get two positive dentries.Amir, we need a test for this in one of the suites. Which one is more appropriate, unionmount-testsuite or xfstests?
xfstests.
Its a simple regression test for a bug fix that mangles with underlying files, much like overlay/010.
unionmount-testsuite never mangles with lower files - it is only testing the VFS interface (+ few verification checks on underlying layers).
Thanks,
Amir.