fs: Handle register_shrinker failure
PATCH 1/1 fs: Handle register_shrinker failure
register_shrinker allocates dynamic memory and thus is susceptible to failures under low-memory situation. Currently,get_userns ignores the return value of register_shrinker, potentially exposing not fully initialised object. This can lead to a NULL-ptr deref everytime shrinker->nr_deferred is referenced.
Fix this by failing to register the filesystem in case there is not enough memory to fully construct the shrinker object.
--- fs/super.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
fs/super.c +13 -1
@@ -518,7 +518,19 @@ struct super_block *sget_userns(struct file_system_type *type,hlist_add_head (&s->s_instances, &type->fs_supers);spin_unlock (&sb_lock);get_filesystem (type); -register_shrinker (&s->s_shrink); + err =register_shrinker (&s->s_shrink); +if (err) { +spin_lock (&sb_lock); +list_del (&s->s_list); +hlist_del (&s->s_instances); +spin_unlock (&sb_lock); + +up_write (&s->s_umount); +destroy_super (s); +put_filesystem (type); +return ERR_PTR (err); + } +return s; }
Signature · 1 line
2.7.4
Quoted 9 lines · Nikolay Borisov
register_shrinkerallocates dynamic memory and thus is susceptible to failures under low-memory situation. Currently,get_userns ignores the return value ofregister_shrinker, potentially exposing not fully initialised object. This can lead to a NULL-ptr deref everytimeshrinker->nr_deferredis referenced.Fix this by failing to register the filesystem in case there is not enough memory to fully construct the shrinker object.
Signed-off-by Nikolay Borisov <[email protected]>
Looks good, though the situation seems rare.
Quoted 30 lines
--- fs/super.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)fs/super.c +13
-1@@ -518,7 +518,19 @@ struct super_block *sget_userns(struct file_system_type *type,hlist_add_head (&s->s_instances, &type->fs_supers);spin_unlock (&sb_lock);get_filesystem (type); -register_shrinker (&s->s_shrink); + err =register_shrinker (&s->s_shrink); +if (err) { +spin_lock (&sb_lock); +list_del (&s->s_list); +hlist_del (&s->s_instances); +spin_unlock (&sb_lock); + +up_write (&s->s_umount); +destroy_super (s); +put_filesystem (type); +return ERR_PTR (err); + } +return s; }
Signature · 1 line
Goldwyn
Al Viro
maintainerQuoted 23 lines · Nikolay Borisov
register_shrinkerallocates dynamic memory and thus is susceptible to failures under low-memory situation. Currently,get_userns ignores the return value ofregister_shrinker, potentially exposing not fully initialised object. This can lead to a NULL-ptr deref everytimeshrinker->nr_deferredis referenced.Fix this by failing to register the filesystem in case there is not enough memory to fully construct the shrinker object.
Signed-off-by Nikolay Borisov <[email protected]>--- fs/super.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)fs/super.c +1
-1@@ -518,7 +518,19 @@ struct super_block *sget_userns(struct file_system_type *type,hlist_add_head (&s->s_instances, &type->fs_supers);spin_unlock (&sb_lock);get_filesystem (type); -register_shrinker (&s->s_shrink); + err =register_shrinker (&s->s_shrink);
fs/super.c struct super_block *sget_userns(struct file_system_type *type, · line 531
Quoted 10 lines
fs/super.c +10
-0+if (err) { +spin_lock (&sb_lock); +list_del (&s->s_list); +hlist_del (&s->s_instances); +spin_unlock (&sb_lock); + +up_write (&s->s_umount); +destroy_super (s); +put_filesystem (type); +return ERR_PTR (err);
I really don't like that. Your "remove it from all lists and pray that nobody has picked a reference of any kind" at the very least needs a careful written proof of correctness. AFAICS, somebody might've found it on the list and attempted to grab ->s_umount (grab_super() from another thread calling sget()). Then they'd block until your up_write() in there and bugger the system up trying to play with ->s_umount in the object you've freed.
NAK. Yes, the bug is real, but this is not a solution.
Al Viro
maintainerQuoted 9 lines · Al Viro
I really don't like that. Your "remove it from all lists and pray that nobody has picked a reference of any kind" at the very least needs a careful written proof of correctness. AFAICS, somebody might've found it on the list and attempted to grab ->s_umount (
grab_super()from another thread callingsget()). Then they'd block until yourup_write()in there and bugger the system up trying to play with ->s_umount in the object you've freed.NAK. Yes, the bug is real, but this is not a solution.
Why do we register it that early, anyway? super_cache_scan() won't do anything until we are done with setting the sucker up and dropped ->s_umount. How about we initialize ->s_shrink.list in alloc_super(), have deactivate_locked_super() call unregister_shrinker() only if list_empty(...) and have mount_fs() do
error = register_shrinker(&sb->s_shrink);
if (error)
goto out_sb;
sb->s_flags |= MS_BORN;
error = security_sb_kern_mount(sb, flags, secdata);
if (error)
goto out_sb;Folks? Am I missing something subtle here?