Skip to content

Commit e432c72

Browse files
Dean Jenkinsholtmann
authored andcommitted
Bluetooth: __l2cap_wait_ack() add defensive timeout
Add a timeout to prevent the do while loop running in an infinite loop. This ensures that the channel will be instructed to close within 10 seconds so prevents l2cap_sock_shutdown() getting stuck forever. Returns -ENOLINK when the timeout is reached. The channel will be subequently closed and not all data will be ACK'ed. Signed-off-by: Dean Jenkins <Dean_Jenkins@mentor.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent cb02a25 commit e432c72

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎include/net/bluetooth/l2cap.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define L2CAP_MOVE_TIMEOUT msecs_to_jiffies(4000)
5757
#define L2CAP_MOVE_ERTX_TIMEOUT msecs_to_jiffies(60000)
5858
#define L2CAP_WAIT_ACK_POLL_PERIOD msecs_to_jiffies(200)
59+
#define L2CAP_WAIT_ACK_TIMEOUT msecs_to_jiffies(10000)
5960

6061
#define L2CAP_A2MP_DEFAULT_MTU 670
6162

‎net/bluetooth/l2cap_sock.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,11 +1059,15 @@ static int __l2cap_wait_ack(struct sock *sk, struct l2cap_chan *chan)
10591059
DECLARE_WAITQUEUE(wait, current);
10601060
int err = 0;
10611061
int timeo = L2CAP_WAIT_ACK_POLL_PERIOD;
1062+
/* Timeout to prevent infinite loop */
1063+
unsigned long timeout = jiffies + L2CAP_WAIT_ACK_TIMEOUT;
10621064

10631065
add_wait_queue(sk_sleep(sk), &wait);
10641066
set_current_state(TASK_INTERRUPTIBLE);
10651067
do {
1066-
BT_DBG("Waiting for %d ACKs", chan->unacked_frames);
1068+
BT_DBG("Waiting for %d ACKs, timeout %04d ms",
1069+
chan->unacked_frames, time_after(jiffies, timeout) ? 0 :
1070+
jiffies_to_msecs(timeout - jiffies));
10671071

10681072
if (!timeo)
10691073
timeo = L2CAP_WAIT_ACK_POLL_PERIOD;
@@ -1082,6 +1086,11 @@ static int __l2cap_wait_ack(struct sock *sk, struct l2cap_chan *chan)
10821086
if (err)
10831087
break;
10841088

1089+
if (time_after(jiffies, timeout)) {
1090+
err = -ENOLINK;
1091+
break;
1092+
}
1093+
10851094
} while (chan->unacked_frames > 0 &&
10861095
chan->state == BT_CONNECTED);
10871096

0 commit comments

Comments
 (0)