Skip to content

Commit 4f4482d

Browse files
Jon Paul Maloydavem330
authored andcommitted
tipc: compensate for double accounting in socket rcv buffer
The function net/core/sock.c::__release_sock() runs a tight loop to move buffers from the socket backlog queue to the receive queue. As a security measure, sk_backlog.len of the receiving socket is not set to zero until after the loop is finished, i.e., until the whole backlog queue has been transferred to the receive queue. During this transfer, the data that has already been moved is counted both in the backlog queue and the receive queue, hence giving an incorrect picture of the available queue space for new arriving buffers. This leads to unnecessary rejection of buffers by sk_add_backlog(), which in TIPC leads to unnecessarily broken connections. In this commit, we compensate for this double accounting by adding a counter that keeps track of it. The function socket.c::backlog_rcv() receives buffers one by one from __release_sock(), and adds them to the socket receive queue. If the transfer is successful, it increases a new atomic counter 'tipc_sock::dupl_rcvcnt' with 'truesize' of the transferred buffer. If a new buffer arrives during this transfer and finds the socket busy (owned), we attempt to add it to the backlog. However, when sk_add_backlog() is called, we adjust the 'limit' parameter with the value of the new counter, so that the risk of inadvertent rejection is eliminated. It should be noted that this change does not invalidate the original purpose of zeroing 'sk_backlog.len' after the full transfer. We set an upper limit for dupl_rcvcnt, so that if a 'wild' sender (i.e., one that doesn't respect the send window) keeps pumping in buffers to sk_add_backlog(), he will eventually reach an upper limit, (2 x TIPC_CONN_OVERLOAD_LIMIT). After that, no messages can be added to the backlog, and the connection will be broken. Ordinary, well- behaved senders will never reach this buffer limit at all. Signed-off-by: Jon Maloy <jon.maloy@ericsson.com> Reviewed-by: Ying Xue <ying.xue@windriver.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6163a19 commit 4f4482d

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

‎net/tipc/socket.c‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* net/tipc/socket.c: TIPC socket API
2+
* net/tipc/socket.c: TIPC socket API
33
*
44
* Copyright (c) 2001-2007, 2012-2014, Ericsson AB
55
* Copyright (c) 2004-2008, 2010-2013, Wind River Systems
@@ -45,7 +45,7 @@
4545

4646
#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
4747

48-
static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
48+
static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
4949
static void tipc_data_ready(struct sock *sk);
5050
static void tipc_write_space(struct sock *sk);
5151
static int tipc_release(struct socket *sock);
@@ -196,11 +196,12 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
196196
sock->state = state;
197197

198198
sock_init_data(sock, sk);
199-
sk->sk_backlog_rcv = backlog_rcv;
199+
sk->sk_backlog_rcv = tipc_backlog_rcv;
200200
sk->sk_rcvbuf = sysctl_tipc_rmem[1];
201201
sk->sk_data_ready = tipc_data_ready;
202202
sk->sk_write_space = tipc_write_space;
203-
tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
203+
tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
204+
atomic_set(&tsk->dupl_rcvcnt, 0);
204205
tipc_port_unlock(port);
205206

206207
if (sock->state == SS_READY) {
@@ -1416,21 +1417,26 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
14161417
}
14171418

14181419
/**
1419-
* backlog_rcv - handle incoming message from backlog queue
1420+
* tipc_backlog_rcv - handle incoming message from backlog queue
14201421
* @sk: socket
14211422
* @buf: message
14221423
*
14231424
* Caller must hold socket lock, but not port lock.
14241425
*
14251426
* Returns 0
14261427
*/
1427-
static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1428+
static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
14281429
{
14291430
u32 res;
1431+
struct tipc_sock *tsk = tipc_sk(sk);
14301432

14311433
res = filter_rcv(sk, buf);
1432-
if (res)
1434+
if (unlikely(res))
14331435
tipc_reject_msg(buf, res);
1436+
1437+
if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1438+
atomic_add(buf->truesize, &tsk->dupl_rcvcnt);
1439+
14341440
return 0;
14351441
}
14361442

@@ -1445,8 +1451,9 @@ static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
14451451
*/
14461452
u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
14471453
{
1454+
struct tipc_sock *tsk = tipc_sk(sk);
14481455
u32 res;
1449-
1456+
uint limit;
14501457
/*
14511458
* Process message if socket is unlocked; otherwise add to backlog queue
14521459
*
@@ -1457,7 +1464,10 @@ u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
14571464
if (!sock_owned_by_user(sk)) {
14581465
res = filter_rcv(sk, buf);
14591466
} else {
1460-
if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
1467+
if (sk->sk_backlog.len == 0)
1468+
atomic_set(&tsk->dupl_rcvcnt, 0);
1469+
limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1470+
if (sk_add_backlog(sk, buf, limit))
14611471
res = TIPC_ERR_OVERLOAD;
14621472
else
14631473
res = TIPC_OK;

‎net/tipc/socket.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
* @port: port - interacts with 'sk' and with the rest of the TIPC stack
4545
* @peer_name: the peer of the connection, if any
4646
* @conn_timeout: the time we can wait for an unresponded setup request
47+
* @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
4748
*/
4849

4950
struct tipc_sock {
5051
struct sock sk;
5152
struct tipc_port port;
5253
unsigned int conn_timeout;
54+
atomic_t dupl_rcvcnt;
5355
};
5456

5557
static inline struct tipc_sock *tipc_sk(const struct sock *sk)

0 commit comments

Comments
 (0)