Skip to content

Commit 9bb59a2

Browse files
liuhangbindavem330
authored andcommitted
tcp: warn if offset reach the maxlen limit when using snprintf
snprintf returns the number of chars that would be written, not number of chars that were actually written. As such, 'offs' may get larger than 'tbl.maxlen', causing the 'tbl.maxlen - offs' being < 0, and since the parameter is size_t, it would overflow. Since using scnprintf may hide the limit error, while the buffer is still enough now, let's just add a WARN_ON_ONCE in case it reach the limit in future. v2: Use WARN_ON_ONCE as Jiri and Eric suggested. Suggested-by: Jiri Benc <jbenc@redhat.com> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c0d59da commit 9bb59a2

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

net/ipv4/sysctl_net_ipv4.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ static int proc_tcp_fastopen_key(struct ctl_table *table, int write,
340340
user_key[i * 4 + 1],
341341
user_key[i * 4 + 2],
342342
user_key[i * 4 + 3]);
343+
344+
if (WARN_ON_ONCE(off >= tbl.maxlen - 1))
345+
break;
346+
343347
if (i + 1 < n_keys)
344348
off += snprintf(tbl.data + off, tbl.maxlen - off, ",");
345349
}

net/ipv4/tcp_cong.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ void tcp_get_available_congestion_control(char *buf, size_t maxlen)
256256
offs += snprintf(buf + offs, maxlen - offs,
257257
"%s%s",
258258
offs == 0 ? "" : " ", ca->name);
259+
260+
if (WARN_ON_ONCE(offs >= maxlen))
261+
break;
259262
}
260263
rcu_read_unlock();
261264
}
@@ -285,6 +288,9 @@ void tcp_get_allowed_congestion_control(char *buf, size_t maxlen)
285288
offs += snprintf(buf + offs, maxlen - offs,
286289
"%s%s",
287290
offs == 0 ? "" : " ", ca->name);
291+
292+
if (WARN_ON_ONCE(offs >= maxlen))
293+
break;
288294
}
289295
rcu_read_unlock();
290296
}

net/ipv4/tcp_ulp.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ void tcp_get_available_ulp(char *buf, size_t maxlen)
9292
offs += snprintf(buf + offs, maxlen - offs,
9393
"%s%s",
9494
offs == 0 ? "" : " ", ulp_ops->name);
95+
96+
if (WARN_ON_ONCE(offs >= maxlen))
97+
break;
9598
}
9699
rcu_read_unlock();
97100
}

0 commit comments

Comments
 (0)