diff options
author | Tom Herbert <tom@quantonium.net> | 2018-03-13 15:01:43 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2018-03-16 11:12:16 -0400 |
commit | 2cc683e88c0c993ac3721d9b702cb0630abe2879 (patch) | |
tree | 5fc199bed7dada28faa68ad075e5118937d48ecf | |
parent | e693be293f16fe446c3a1b72f8bbc0a5101ca5ea (diff) |
kcm: lock lower socket in kcm_attach
Need to lock lower socket in order to provide mutual exclusion
with kcm_unattach.
v2: Add Reported-by for syzbot
Fixes: ab7ac4eb9832e32a09f4e804 ("kcm: Kernel Connection Multiplexor module")
Reported-by: syzbot+ea75c0ffcd353d32515f064aaebefc5279e6161e@syzkaller.appspotmail.com
Signed-off-by: Tom Herbert <tom@quantonium.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | net/kcm/kcmsock.c | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/net/kcm/kcmsock.c b/net/kcm/kcmsock.c index f297d53a11aa..34355fd19f27 100644 --- a/net/kcm/kcmsock.c +++ b/net/kcm/kcmsock.c | |||
@@ -1381,24 +1381,32 @@ static int kcm_attach(struct socket *sock, struct socket *csock, | |||
1381 | .parse_msg = kcm_parse_func_strparser, | 1381 | .parse_msg = kcm_parse_func_strparser, |
1382 | .read_sock_done = kcm_read_sock_done, | 1382 | .read_sock_done = kcm_read_sock_done, |
1383 | }; | 1383 | }; |
1384 | int err; | 1384 | int err = 0; |
1385 | 1385 | ||
1386 | csk = csock->sk; | 1386 | csk = csock->sk; |
1387 | if (!csk) | 1387 | if (!csk) |
1388 | return -EINVAL; | 1388 | return -EINVAL; |
1389 | 1389 | ||
1390 | lock_sock(csk); | ||
1391 | |||
1390 | /* Only allow TCP sockets to be attached for now */ | 1392 | /* Only allow TCP sockets to be attached for now */ |
1391 | if ((csk->sk_family != AF_INET && csk->sk_family != AF_INET6) || | 1393 | if ((csk->sk_family != AF_INET && csk->sk_family != AF_INET6) || |
1392 | csk->sk_protocol != IPPROTO_TCP) | 1394 | csk->sk_protocol != IPPROTO_TCP) { |
1393 | return -EOPNOTSUPP; | 1395 | err = -EOPNOTSUPP; |
1396 | goto out; | ||
1397 | } | ||
1394 | 1398 | ||
1395 | /* Don't allow listeners or closed sockets */ | 1399 | /* Don't allow listeners or closed sockets */ |
1396 | if (csk->sk_state == TCP_LISTEN || csk->sk_state == TCP_CLOSE) | 1400 | if (csk->sk_state == TCP_LISTEN || csk->sk_state == TCP_CLOSE) { |
1397 | return -EOPNOTSUPP; | 1401 | err = -EOPNOTSUPP; |
1402 | goto out; | ||
1403 | } | ||
1398 | 1404 | ||
1399 | psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL); | 1405 | psock = kmem_cache_zalloc(kcm_psockp, GFP_KERNEL); |
1400 | if (!psock) | 1406 | if (!psock) { |
1401 | return -ENOMEM; | 1407 | err = -ENOMEM; |
1408 | goto out; | ||
1409 | } | ||
1402 | 1410 | ||
1403 | psock->mux = mux; | 1411 | psock->mux = mux; |
1404 | psock->sk = csk; | 1412 | psock->sk = csk; |
@@ -1407,7 +1415,7 @@ static int kcm_attach(struct socket *sock, struct socket *csock, | |||
1407 | err = strp_init(&psock->strp, csk, &cb); | 1415 | err = strp_init(&psock->strp, csk, &cb); |
1408 | if (err) { | 1416 | if (err) { |
1409 | kmem_cache_free(kcm_psockp, psock); | 1417 | kmem_cache_free(kcm_psockp, psock); |
1410 | return err; | 1418 | goto out; |
1411 | } | 1419 | } |
1412 | 1420 | ||
1413 | write_lock_bh(&csk->sk_callback_lock); | 1421 | write_lock_bh(&csk->sk_callback_lock); |
@@ -1419,7 +1427,8 @@ static int kcm_attach(struct socket *sock, struct socket *csock, | |||
1419 | write_unlock_bh(&csk->sk_callback_lock); | 1427 | write_unlock_bh(&csk->sk_callback_lock); |
1420 | strp_done(&psock->strp); | 1428 | strp_done(&psock->strp); |
1421 | kmem_cache_free(kcm_psockp, psock); | 1429 | kmem_cache_free(kcm_psockp, psock); |
1422 | return -EALREADY; | 1430 | err = -EALREADY; |
1431 | goto out; | ||
1423 | } | 1432 | } |
1424 | 1433 | ||
1425 | psock->save_data_ready = csk->sk_data_ready; | 1434 | psock->save_data_ready = csk->sk_data_ready; |
@@ -1455,7 +1464,10 @@ static int kcm_attach(struct socket *sock, struct socket *csock, | |||
1455 | /* Schedule RX work in case there are already bytes queued */ | 1464 | /* Schedule RX work in case there are already bytes queued */ |
1456 | strp_check_rcv(&psock->strp); | 1465 | strp_check_rcv(&psock->strp); |
1457 | 1466 | ||
1458 | return 0; | 1467 | out: |
1468 | release_sock(csk); | ||
1469 | |||
1470 | return err; | ||
1459 | } | 1471 | } |
1460 | 1472 | ||
1461 | static int kcm_attach_ioctl(struct socket *sock, struct kcm_attach *info) | 1473 | static int kcm_attach_ioctl(struct socket *sock, struct kcm_attach *info) |
@@ -1507,6 +1519,7 @@ static void kcm_unattach(struct kcm_psock *psock) | |||
1507 | 1519 | ||
1508 | if (WARN_ON(psock->rx_kcm)) { | 1520 | if (WARN_ON(psock->rx_kcm)) { |
1509 | write_unlock_bh(&csk->sk_callback_lock); | 1521 | write_unlock_bh(&csk->sk_callback_lock); |
1522 | release_sock(csk); | ||
1510 | return; | 1523 | return; |
1511 | } | 1524 | } |
1512 | 1525 | ||