Skip to content

Commit 085cbbd

Browse files
Tom Herbertgregkh
authored andcommitted
kcm: Check if sk_user_data already set in kcm_attach
commit e557124 upstream. This is needed to prevent sk_user_data being overwritten. The check is done under the callback lock. This should prevent a socket from being attached twice to a KCM mux. It also prevents a socket from being attached for other use cases of sk_user_data as long as the other cases set sk_user_data under the lock. Followup work is needed to unify all the use cases of sk_user_data to use the same locking. Reported-by: syzbot+114b15f2be420a8886c3@syzkaller.appspotmail.com Fixes: ab7ac4e ("kcm: Kernel Connection Multiplexor module") Signed-off-by: Tom Herbert <tom@quantonium.net> Reviewed-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bd3ccdc commit 085cbbd

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

net/kcm/kcmsock.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,18 +1404,30 @@ static int kcm_attach(struct socket *sock, struct socket *csock,
14041404
return err;
14051405
}
14061406

1407-
sock_hold(csk);
1408-
14091407
write_lock_bh(&csk->sk_callback_lock);
1408+
1409+
/* Check if sk_user_data is aready by KCM or someone else.
1410+
* Must be done under lock to prevent race conditions.
1411+
*/
1412+
if (csk->sk_user_data) {
1413+
write_unlock_bh(&csk->sk_callback_lock);
1414+
strp_done(&psock->strp);
1415+
kmem_cache_free(kcm_psockp, psock);
1416+
return -EALREADY;
1417+
}
1418+
14101419
psock->save_data_ready = csk->sk_data_ready;
14111420
psock->save_write_space = csk->sk_write_space;
14121421
psock->save_state_change = csk->sk_state_change;
14131422
csk->sk_user_data = psock;
14141423
csk->sk_data_ready = psock_data_ready;
14151424
csk->sk_write_space = psock_write_space;
14161425
csk->sk_state_change = psock_state_change;
1426+
14171427
write_unlock_bh(&csk->sk_callback_lock);
14181428

1429+
sock_hold(csk);
1430+
14191431
/* Finished initialization, now add the psock to the MUX. */
14201432
spin_lock_bh(&mux->lock);
14211433
head = &mux->psocks;

0 commit comments

Comments
 (0)