Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ax25: Fix ax25 session cleanup problems

There are session cleanup problems in ax25_release() and
ax25_disconnect(). If we setup a session and then disconnect,
the disconnected session is still in "LISTENING" state that
is shown below.

Active AX.25 sockets
Dest Source Device State Vr/Vs Send-Q Recv-Q
DL9SAU-4 DL9SAU-3 ??? LISTENING 000/000 0 0
DL9SAU-3 DL9SAU-4 ??? LISTENING 000/000 0 0

The first reason is caused by del_timer_sync() in ax25_release().
The timers of ax25 are used for correct session cleanup. If we use
ax25_release() to close ax25 sessions and ax25_dev is not null,
the del_timer_sync() functions in ax25_release() will execute.
As a result, the sessions could not be cleaned up correctly,
because the timers have stopped.

In order to solve this problem, this patch adds a device_up flag
in ax25_dev in order to judge whether the device is up. If there
are sessions to be cleaned up, the del_timer_sync() in
ax25_release() will not execute. What's more, we add ax25_cb_del()
in ax25_kill_by_device(), because the timers have been stopped
and there are no functions that could delete ax25_cb if we do not
call ax25_release(). Finally, we reorder the position of
ax25_list_lock in ax25_cb_del() in order to synchronize among
different functions that call ax25_cb_del().

The second reason is caused by improper check in ax25_disconnect().
The incoming ax25 sessions which ax25->sk is null will close
heartbeat timer, because the check "if(!ax25->sk || ..)" is
satisfied. As a result, the session could not be cleaned up properly.

In order to solve this problem, this patch changes the improper
check to "if(ax25->sk && ..)" in ax25_disconnect().

What`s more, the ax25_disconnect() may be called twice, which is
not necessary. For example, ax25_kill_by_device() calls
ax25_disconnect() and sets ax25->state to AX25_STATE_0, but
ax25_release() calls ax25_disconnect() again.

In order to solve this problem, this patch add a check in
ax25_release(). If the flag of ax25->sk equals to SOCK_DEAD,
the ax25_disconnect() in ax25_release() should not be executed.

Fixes: 82e31755e55f ("ax25: Fix UAF bugs in ax25 timers")
Fixes: 8a367e74c012 ("ax25: Fix segfault after sock connection timeout")
Reported-and-tested-by: Thomas Osterried <thomas@osterried.de>
Signed-off-by: Duoming Zhou <duoming@zju.edu.cn>
Link: https://lore.kernel.org/r/20220530152158.108619-1-duoming@zju.edu.cn
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Duoming Zhou and committed by
Paolo Abeni
7d8a3a47 9f4fc18b

+20 -11
+1
include/net/ax25.h
··· 228 228 ax25_dama_info dama; 229 229 #endif 230 230 refcount_t refcount; 231 + bool device_up; 231 232 } ax25_dev; 232 233 233 234 typedef struct ax25_cb {
+17 -10
net/ax25/af_ax25.c
··· 62 62 */ 63 63 static void ax25_cb_del(ax25_cb *ax25) 64 64 { 65 + spin_lock_bh(&ax25_list_lock); 65 66 if (!hlist_unhashed(&ax25->ax25_node)) { 66 - spin_lock_bh(&ax25_list_lock); 67 67 hlist_del_init(&ax25->ax25_node); 68 - spin_unlock_bh(&ax25_list_lock); 69 68 ax25_cb_put(ax25); 70 69 } 70 + spin_unlock_bh(&ax25_list_lock); 71 71 } 72 72 73 73 /* ··· 81 81 82 82 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) 83 83 return; 84 + ax25_dev->device_up = false; 84 85 85 86 spin_lock_bh(&ax25_list_lock); 86 87 again: ··· 92 91 spin_unlock_bh(&ax25_list_lock); 93 92 ax25_disconnect(s, ENETUNREACH); 94 93 s->ax25_dev = NULL; 94 + ax25_cb_del(s); 95 95 spin_lock_bh(&ax25_list_lock); 96 96 goto again; 97 97 } ··· 105 103 dev_put_track(ax25_dev->dev, &ax25_dev->dev_tracker); 106 104 ax25_dev_put(ax25_dev); 107 105 } 106 + ax25_cb_del(s); 108 107 release_sock(sk); 109 108 spin_lock_bh(&ax25_list_lock); 110 109 sock_put(sk); ··· 998 995 if (sk->sk_type == SOCK_SEQPACKET) { 999 996 switch (ax25->state) { 1000 997 case AX25_STATE_0: 1001 - release_sock(sk); 1002 - ax25_disconnect(ax25, 0); 1003 - lock_sock(sk); 998 + if (!sock_flag(ax25->sk, SOCK_DEAD)) { 999 + release_sock(sk); 1000 + ax25_disconnect(ax25, 0); 1001 + lock_sock(sk); 1002 + } 1004 1003 ax25_destroy_socket(ax25); 1005 1004 break; 1006 1005 ··· 1058 1053 ax25_destroy_socket(ax25); 1059 1054 } 1060 1055 if (ax25_dev) { 1061 - del_timer_sync(&ax25->timer); 1062 - del_timer_sync(&ax25->t1timer); 1063 - del_timer_sync(&ax25->t2timer); 1064 - del_timer_sync(&ax25->t3timer); 1065 - del_timer_sync(&ax25->idletimer); 1056 + if (!ax25_dev->device_up) { 1057 + del_timer_sync(&ax25->timer); 1058 + del_timer_sync(&ax25->t1timer); 1059 + del_timer_sync(&ax25->t2timer); 1060 + del_timer_sync(&ax25->t3timer); 1061 + del_timer_sync(&ax25->idletimer); 1062 + } 1066 1063 dev_put_track(ax25_dev->dev, &ax25_dev->dev_tracker); 1067 1064 ax25_dev_put(ax25_dev); 1068 1065 }
+1
net/ax25/ax25_dev.c
··· 62 62 ax25_dev->dev = dev; 63 63 dev_hold_track(dev, &ax25_dev->dev_tracker, GFP_ATOMIC); 64 64 ax25_dev->forward = NULL; 65 + ax25_dev->device_up = true; 65 66 66 67 ax25_dev->values[AX25_VALUES_IPDEFMODE] = AX25_DEF_IPDEFMODE; 67 68 ax25_dev->values[AX25_VALUES_AXDEFMODE] = AX25_DEF_AXDEFMODE;
+1 -1
net/ax25/ax25_subr.c
··· 268 268 del_timer_sync(&ax25->t3timer); 269 269 del_timer_sync(&ax25->idletimer); 270 270 } else { 271 - if (!ax25->sk || !sock_flag(ax25->sk, SOCK_DESTROY)) 271 + if (ax25->sk && !sock_flag(ax25->sk, SOCK_DESTROY)) 272 272 ax25_stop_heartbeat(ax25); 273 273 ax25_stop_t1timer(ax25); 274 274 ax25_stop_t2timer(ax25);