net: fix divide by zero in tcp algorithm illinois
Subsystems (3)
PATCH 1/1 net: fix divide by zero in tcp algorithm illinois
Reading TCP stats when using TCP Illinois congestion control algorithm can cause a divide by zero kernel oops.
The division by zero occur in tcp_illinois_info() at:
do_div(t, ca->cnt_rtt);
where ca->cnt_rtt can become zero (when rtt_reset is called)
Steps to Reproduce:
1. Register tcp_illinois:
# sysctl -w net.ipv4.tcp_congestion_control=illinois
2. Monitor internal TCP information via command "ss -i"
# watch -d ss -i
3. Establish new TCP conn to machineEither it fails at the initial conn, or else it needs to wait for a loss or a reset.
This is only related to reading stats. The function avg_delay() also performs the same divide, but is guarded with a (ca->cnt_rtt > 0) at its calling point in update_params(). Thus, simply fix tcp_illinois_info().
To be on the safe side, I use a local stack variable in tcp_illinois_info() to eliminate any race conditions. I'm not sure this is needed, as this would also affect avg_delay(), if this race exists. (Although this is likely already "fix" by compiler optimization and kept in a local register)
---
net/ipv4/tcp_illinois.c | 8 ++++++-- 1 files changed, 6 insertions(+), 2 deletions(-)
net/ipv4/tcp_illinois.c +6 -2
@@ -306,6 +306,7 @@ static void tcp_illinois_info(struct sock *sk, u32 ext, struct sk_buff *skb) {const struct illinois *ca =inet_csk_ca (sk); +u16 cnt_rtt;if (ext & (1 << (INET_DIAG_VEGASINFO -1 ))) { struct tcpvegas_info info = {
@@ -315,8 +316,11 @@ static void tcp_illinois_info(struct sock *sk, u32 ext, };u64 t = ca->sum_rtt; -do_div (t, ca->cnt_rtt); - info.tcpv_rtt = t; + cnt_rtt = ca->cnt_rtt; +if (cnt_rtt >0 ) { +do_div (t, cnt_rtt); + info.tcpv_rtt = t; + }nla_put (skb,INET_DIAG_VEGASINFO ,sizeof (info), &info); }
Quoted 14 lines · Jesper Dangaard Brouer
Reading TCP stats when using TCP Illinois congestion control algorithm can cause a divide by zero kernel oops.
The division by zero occur in
tcp_illinois_info()at:
do_div(t, ca->cnt_rtt);
whereca->cnt_rttcan become zero (whenrtt_resetis called)Steps to Reproduce:
1. Registertcp_illinois:# sysctl -w net.ipv4.tcp_congestion_control=illinois 2. Monitor internal TCP information via command "ss -i" # watch -d ss -i 3. Establish new TCP conn to machine
Quoted 7 lines
Either it fails at the initial conn, or else it needs to wait for a loss or a reset.
This is only related to reading stats. The function
avg_delay()also performs the same divide, but is guarded with a (ca->cnt_rtt> 0) at its calling point inupdate_params(). Thus, simply fixtcp_illinois_info().
avg_delay() is called with socket locked so it is safe.
While get_info() is called with socket not locked.
Quoted 4 lines
To be on the safe side, I use a local stack variable in
tcp_illinois_info()to eliminate any race conditions. I'm not sure this is needed, as this would also affectavg_delay(), if this race exists. (Although this is likely already "fix" by compiler optimization and kept in a local register)
Hmm, this is certainly not a valid reason.
Compiler could do the reverse actually, even with a local var.
Could you please use info.tcpv_rttcnt to be on the safe side ?
net/ipv4/tcp_illinois.c +5 -3
@@ -313,11 +313,13 @@ static void tcp_illinois_info(struct sock *sk, u32 ext, .tcpv_rttcnt = ca->cnt_rtt, .tcpv_minrtt = ca->base_rtt, }; -u64 t = ca->sum_rtt; -do_div (t, ca->cnt_rtt); - info.tcpv_rtt = t; +if (info.tcpv_rttcnt) { +u64 t = ca->sum_rtt; +do_div (t, info.tcpv_rttcnt); + info.tcpv_rtt = t; + }nla_put (skb,INET_DIAG_VEGASINFO ,sizeof (info), &info); } }