SUNRPC: Fix use-after-free in svc_age_temp_xprts_now()
Subsystems (3)
Chuck Lever
maintainerPATCH 1/1 SUNRPC: Fix use-after-free in svc_age_temp_xprts_now()
svc_age_temp_xprts_now() moves each matching temporary transport onto a local list under sv_lock, then drops the lock and walks the local list without holding a reference on any of them. In the meantime svc_delete_xprt() can take sv_lock, unlink the same xpt_list node, and drop the transport's last reference. An idle temporary transport holds only that one reference, so a peer FIN that arrives between the two loops frees the svc_sock out from under the second loop. The notifier then sets XPT_CLOSE in freed memory, and svc_xprt_enqueue() reads xpt_flags and xpt_server from the freed object and queues that object on a pool for an nfsd thread. KASAN reports a slab-use-after-free in svc_xprt_enqueue() under nfsd_inetaddr_event(), with the object freed by svc_sock_free() on an nfsd thread.
The two-phase walk dates from when the second loop called into the socket layer and could sleep. Commit 546125d16142 ("sunrpc: don't call sleeping functions from the notifier block callbacks") moved that work to the nfsd threads, so nothing left in the second loop needs sv_lock dropped. Mark and enqueue each matching transport under sv_lock in a single pass, as svc_age_temp_xprts() already does, so that svc_delete_xprt() cannot unlink and free a transport the notifier is still touching.
--- net/sunrpc/svc_xprt.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
Mohammed, thanks for the report. Please test and report your results.
net/sunrpc/svc_xprt.c +4 -12
@@ -1075,29 +1075,21 @@ void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) {struct svc_xprt *xprt;struct list_head *le, *next; -LIST_HEAD (to_be_closed);spin_lock_bh (&serv->sv_lock);list_for_each_safe (le, next, &serv->sv_tempsocks) { xprt = list_entry(le, struct svc_xprt, xpt_list); -if (rpc_cmp_addr (server_addr, (struct sockaddr *) - &xprt->xpt_local)) { -dprintk ("svc_age_temp_xprts_now: found %p\n" , xprt); -list_move (le, &to_be_closed); - } - } -spin_unlock_bh (&serv->sv_lock); - -while (!list_empty (&to_be_closed)) { - le = to_be_closed.next; +if (!rpc_cmp_addr (server_addr, (struct sockaddr *) + &xprt->xpt_local)) +continue ;list_del_init (le); - xprt = list_entry(le, struct svc_xprt, xpt_list);set_bit (XPT_CLOSE , &xprt->xpt_flags);set_bit (XPT_KILL_TEMP , &xprt->xpt_flags);dprintk ("svc_age_temp_xprts_now: queuing xprt %p for closing\n" , xprt);svc_xprt_enqueue (xprt); } +spin_unlock_bh (&serv->sv_lock); }EXPORT_SYMBOL_GPL (svc_age_temp_xprts_now);
Signature · 1 line
2.54.0
Quoted 23 lines · Chuck Lever
svc_age_temp_xprts_now()moves each matching temporary transport onto a local list undersv_lock, then drops the lock and walks the local list without holding a reference on any of them. In the meantimesvc_delete_xprt()can takesv_lock, unlink the samexpt_listnode, and drop the transport's last reference. An idle temporary transport holds only that one reference, so a peer FIN that arrives between the two loops frees thesvc_sockout from under the second loop. The notifier then setsXPT_CLOSEin freed memory, andsvc_xprt_enqueue()readsxpt_flagsandxpt_serverfrom the freed object and queues that object on a pool for an nfsd thread. KASAN reports a slab-use-after-free insvc_xprt_enqueue()undernfsd_inetaddr_event(), with the object freed bysvc_sock_free()on an nfsd thread.The two-phase walk dates from when the second loop called into the socket layer and could sleep. Commit
546125d16142("sunrpc: don't call sleeping functions from the notifier block callbacks") moved that work to the nfsd threads, so nothing left in the second loop needssv_lockdropped. Mark and enqueue each matching transport undersv_lockin a single pass, assvc_age_temp_xprts()already does, so thatsvc_delete_xprt()cannot unlink and free a transport the notifier is still touching.
Quoted 3 lines
Fixes c3d4879e01be ("sunrpc: Add a function to close temporary transports immediately")Reported-by Mohammed Alquraini <[email protected]>Signed-off-by Chuck Lever <[email protected]>
Everything being moved into the locked region is safe in that context, so this is a nice simplification as well as being a bug-fix.
Thanks,
NeilBrown
Quoted 48 lines
--- net/sunrpc/svc_xprt.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)Mohammed, thanks for the report. Please test and report your results.
net/sunrpc/svc_xprt.c +4
-12@@ -1075,29 +1075,21 @@ void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) {struct svc_xprt *xprt;struct list_head *le, *next; -LIST_HEAD (to_be_closed);spin_lock_bh (&serv->sv_lock);list_for_each_safe (le, next, &serv->sv_tempsocks) { xprt = list_entry(le, struct svc_xprt, xpt_list); -if (rpc_cmp_addr (server_addr, (struct sockaddr *) - &xprt->xpt_local)) { -dprintk ("svc_age_temp_xprts_now: found %p\n" , xprt); -list_move (le, &to_be_closed); - } - } -spin_unlock_bh (&serv->sv_lock); - -while (!list_empty (&to_be_closed)) { - le = to_be_closed.next; +if (!rpc_cmp_addr (server_addr, (struct sockaddr *) + &xprt->xpt_local)) +continue ;list_del_init (le); - xprt = list_entry(le, struct svc_xprt, xpt_list);set_bit (XPT_CLOSE , &xprt->xpt_flags);set_bit (XPT_KILL_TEMP , &xprt->xpt_flags);dprintk ("svc_age_temp_xprts_now: queuing xprt %p for closing\n" , xprt);svc_xprt_enqueue (xprt); } +spin_unlock_bh (&serv->sv_lock); }EXPORT_SYMBOL_GPL (svc_age_temp_xprts_now);Signature · 1 line
2.54.0
Hi Chuck,
This patch fixes the issue.
Thanks for the quick response and fix!
Best,
Mohammed
Quoted 74 lines · Chuck Lever
svc_age_temp_xprts_now()moves each matching temporary transport onto a local list undersv_lock, then drops the lock and walks the local list without holding a reference on any of them. In the meantimesvc_delete_xprt()can takesv_lock, unlink the samexpt_listnode, and drop the transport's last reference. An idle temporary transport holds only that one reference, so a peer FIN that arrives between the two loops frees thesvc_sockout from under the second loop. The notifier then setsXPT_CLOSEin freed memory, andsvc_xprt_enqueue()readsxpt_flagsandxpt_serverfrom the freed object and queues that object on a pool for an nfsd thread. KASAN reports a slab-use-after-free insvc_xprt_enqueue()undernfsd_inetaddr_event(), with the object freed bysvc_sock_free()on an nfsd thread.The two-phase walk dates from when the second loop called into the socket layer and could sleep. Commit
546125d16142("sunrpc: don't call sleeping functions from the notifier block callbacks") moved that work to the nfsd threads, so nothing left in the second loop needssv_lockdropped. Mark and enqueue each matching transport undersv_lockin a single pass, assvc_age_temp_xprts()already does, so thatsvc_delete_xprt()cannot unlink and free a transport the notifier is still touching.Fixes c3d4879e01be ("sunrpc: Add a function to close temporary transports immediately")Reported-by Mohammed Alquraini <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- net/sunrpc/svc_xprt.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)Mohammed, thanks for the report. Please test and report your results.
net/sunrpc/svc_xprt.c +4
-13@@ -1075,29 +1075,21 @@ void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) {struct svc_xprt *xprt;struct list_head *le, *next; -LIST_HEAD (to_be_closed);spin_lock_bh (&serv->sv_lock);list_for_each_safe (le, next, &serv->sv_tempsocks) { xprt = list_entry(le, struct svc_xprt, xpt_list); -if (rpc_cmp_addr (server_addr, (struct sockaddr *) - &xprt->xpt_local)) { -dprintk ("svc_age_temp_xprts_now: found %p\n" , xprt); -list_move (le, &to_be_closed); - } - } -spin_unlock_bh (&serv->sv_lock); - -while (!list_empty (&to_be_closed)) { - le = to_be_closed.next; +if (!rpc_cmp_addr (server_addr, (struct sockaddr *) + &xprt->xpt_local)) +continue ;list_del_init (le); - xprt = list_entry(le, struct svc_xprt, xpt_list);set_bit (XPT_CLOSE , &xprt->xpt_flags);set_bit (XPT_KILL_TEMP , &xprt->xpt_flags);dprintk ("svc_age_temp_xprts_now: queuing xprt %p for closing\n" , xprt);svc_xprt_enqueue (xprt); } +spin_unlock_bh (&serv->sv_lock); }EXPORT_SYMBOL_GPL (svc_age_temp_xprts_now); --2.54.0
Quoted 60 lines · Chuck Lever
svc_age_temp_xprts_now()moves each matching temporary transport onto a local list undersv_lock, then drops the lock and walks the local list without holding a reference on any of them. In the meantimesvc_delete_xprt()can takesv_lock, unlink the samexpt_listnode, and drop the transport's last reference. An idle temporary transport holds only that one reference, so a peer FIN that arrives between the two loops frees thesvc_sockout from under the second loop. The notifier then setsXPT_CLOSEin freed memory, andsvc_xprt_enqueue()readsxpt_flagsandxpt_serverfrom the freed object and queues that object on a pool for an nfsd thread. KASAN reports a slab-use-after-free insvc_xprt_enqueue()undernfsd_inetaddr_event(), with the object freed bysvc_sock_free()on an nfsd thread.The two-phase walk dates from when the second loop called into the socket layer and could sleep. Commit
546125d16142("sunrpc: don't call sleeping functions from the notifier block callbacks") moved that work to the nfsd threads, so nothing left in the second loop needssv_lockdropped. Mark and enqueue each matching transport undersv_lockin a single pass, assvc_age_temp_xprts()already does, so thatsvc_delete_xprt()cannot unlink and free a transport the notifier is still touching.Fixes c3d4879e01be ("sunrpc: Add a function to close temporary transports immediately")Reported-by Mohammed Alquraini <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- net/sunrpc/svc_xprt.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)Mohammed, thanks for the report. Please test and report your results.
net/sunrpc/svc_xprt.c +3
-12@@ -1075,29 +1075,21 @@ void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) {struct svc_xprt *xprt;struct list_head *le, *next; -LIST_HEAD (to_be_closed);spin_lock_bh (&serv->sv_lock);list_for_each_safe (le, next, &serv->sv_tempsocks) { xprt = list_entry(le, struct svc_xprt, xpt_list); -if (rpc_cmp_addr (server_addr, (struct sockaddr *) - &xprt->xpt_local)) { -dprintk ("svc_age_temp_xprts_now: found %p\n" , xprt); -list_move (le, &to_be_closed); - } - } -spin_unlock_bh (&serv->sv_lock); - -while (!list_empty (&to_be_closed)) { - le = to_be_closed.next; +if (!rpc_cmp_addr (server_addr, (struct sockaddr *) + &xprt->xpt_local)) +continue ;list_del_init (le); - xprt = list_entry(le, struct svc_xprt, xpt_list);
net/sunrpc/svc_xprt.c void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) · line 1095
Quoted 10 lines
net/sunrpc/svc_xprt.c +1
-0set_bit (XPT_CLOSE , &xprt->xpt_flags);set_bit (XPT_KILL_TEMP , &xprt->xpt_flags);dprintk ("svc_age_temp_xprts_now: queuing xprt %p for closing\n" , xprt);svc_xprt_enqueue (xprt); } +spin_unlock_bh (&serv->sv_lock); }EXPORT_SYMBOL_GPL (svc_age_temp_xprts_now);
Quoted 5 lines · Mohammed
Hi Chuck,
This patch fixes the issue.
Thanks for the quick response and fix!
May I add:
?
Quoted 78 lines
Best,
MohammedQuoted 74 lines · Chuck Lever
svc_age_temp_xprts_now()moves each matching temporary transport onto a local list undersv_lock, then drops the lock and walks the local list without holding a reference on any of them. In the meantimesvc_delete_xprt()can takesv_lock, unlink the samexpt_listnode, and drop the transport's last reference. An idle temporary transport holds only that one reference, so a peer FIN that arrives between the two loops frees thesvc_sockout from under the second loop. The notifier then setsXPT_CLOSEin freed memory, andsvc_xprt_enqueue()readsxpt_flagsandxpt_serverfrom the freed object and queues that object on a pool for an nfsd thread. KASAN reports a slab-use-after-free insvc_xprt_enqueue()undernfsd_inetaddr_event(), with the object freed bysvc_sock_free()on an nfsd thread.The two-phase walk dates from when the second loop called into the socket layer and could sleep. Commit
546125d16142("sunrpc: don't call sleeping functions from the notifier block callbacks") moved that work to the nfsd threads, so nothing left in the second loop needssv_lockdropped. Mark and enqueue each matching transport undersv_lockin a single pass, assvc_age_temp_xprts()already does, so thatsvc_delete_xprt()cannot unlink and free a transport the notifier is still touching.Fixes c3d4879e01be ("sunrpc: Add a function to close temporary transports immediately")Reported-by Mohammed Alquraini <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- net/sunrpc/svc_xprt.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)Mohammed, thanks for the report. Please test and report your results.
net/sunrpc/svc_xprt.c +4
-13@@ -1075,29 +1075,21 @@ void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) {struct svc_xprt *xprt;struct list_head *le, *next; -LIST_HEAD (to_be_closed);spin_lock_bh (&serv->sv_lock);list_for_each_safe (le, next, &serv->sv_tempsocks) { xprt = list_entry(le, struct svc_xprt, xpt_list); -if (rpc_cmp_addr (server_addr, (struct sockaddr *) - &xprt->xpt_local)) { -dprintk ("svc_age_temp_xprts_now: found %p\n" , xprt); -list_move (le, &to_be_closed); - } - } -spin_unlock_bh (&serv->sv_lock); - -while (!list_empty (&to_be_closed)) { - le = to_be_closed.next; +if (!rpc_cmp_addr (server_addr, (struct sockaddr *) + &xprt->xpt_local)) +continue ;list_del_init (le); - xprt = list_entry(le, struct svc_xprt, xpt_list);set_bit (XPT_CLOSE , &xprt->xpt_flags);set_bit (XPT_KILL_TEMP , &xprt->xpt_flags);dprintk ("svc_age_temp_xprts_now: queuing xprt %p for closing\n" , xprt);svc_xprt_enqueue (xprt); } +spin_unlock_bh (&serv->sv_lock); }EXPORT_SYMBOL_GPL (svc_age_temp_xprts_now); --2.54.0
Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
Sure thing.
Quoted 97 lines · Chuck Lever
Quoted 5 lines · Mohammed
Hi Chuck,
This patch fixes the issue.
Thanks for the quick response and fix!
May I add:
Tested-by Mohammed <[email protected]>?
Quoted 75 lines
Best,
MohammedQuoted 71 lines · Chuck Lever
svc_age_temp_xprts_now()moves each matching temporary transport onto a local list undersv_lock, then drops the lock and walks the local list without holding a reference on any of them. In the meantimesvc_delete_xprt()can takesv_lock, unlink the samexpt_listnode, and drop the transport's last reference. An idle temporary transport holds only that one reference, so a peer FIN that arrives between the two loops frees thesvc_sockout from under the second loop. The notifier then setsXPT_CLOSEin freed memory, andsvc_xprt_enqueue()readsxpt_flagsandxpt_serverfrom the freed object and queues that object on a pool for an nfsd thread. KASAN reports a slab-use-after-free insvc_xprt_enqueue()undernfsd_inetaddr_event(), with the object freed bysvc_sock_free()on an nfsd thread.The two-phase walk dates from when the second loop called into the socket layer and could sleep. Commit
546125d16142("sunrpc: don't call sleeping functions from the notifier block callbacks") moved that work to the nfsd threads, so nothing left in the second loop needssv_lockdropped. Mark and enqueue each matching transport undersv_lockin a single pass, assvc_age_temp_xprts()already does, so thatsvc_delete_xprt()cannot unlink and free a transport the notifier is still touching.Fixes c3d4879e01be ("sunrpc: Add a function to close temporary transports immediately")Reported-by Mohammed Alquraini <[email protected]>Signed-off-by Chuck Lever <[email protected]>--- net/sunrpc/svc_xprt.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)Mohammed, thanks for the report. Please test and report your results.
net/sunrpc/svc_xprt.c +4
-12@@ -1075,29 +1075,21 @@ void svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr) {struct svc_xprt *xprt;struct list_head *le, *next; -LIST_HEAD (to_be_closed);spin_lock_bh (&serv->sv_lock);list_for_each_safe (le, next, &serv->sv_tempsocks) { xprt = list_entry(le, struct svc_xprt, xpt_list); -if (rpc_cmp_addr (server_addr, (struct sockaddr *) - &xprt->xpt_local)) { -dprintk ("svc_age_temp_xprts_now: found %p\n" , xprt); -list_move (le, &to_be_closed); - } - } -spin_unlock_bh (&serv->sv_lock); - -while (!list_empty (&to_be_closed)) { - le = to_be_closed.next; +if (!rpc_cmp_addr (server_addr, (struct sockaddr *) + &xprt->xpt_local)) +continue ;list_del_init (le); - xprt = list_entry(le, struct svc_xprt, xpt_list);set_bit (XPT_CLOSE , &xprt->xpt_flags);set_bit (XPT_KILL_TEMP , &xprt->xpt_flags);dprintk ("svc_age_temp_xprts_now: queuing xprt %p for closing\n" , xprt);svc_xprt_enqueue (xprt); } +spin_unlock_bh (&serv->sv_lock); }EXPORT_SYMBOL_GPL (svc_age_temp_xprts_now);Quoted 3 lines
Quoted 3 lines
--
2.54.0--
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)