From 3eb19cfd8a7c03920aeecae6692048429288af88 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Tue, 15 Mar 2022 23:17:44 +0100 Subject: tcp, udp, util: Enforce 24-bit limit on socket numbers This should never happen, but there are no formal guarantees: ensure socket numbers are below SOCKET_MAX. Signed-off-by: Stefano Brivio --- util.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 2d8952a..ff7d97b 100644 --- a/util.c +++ b/util.c @@ -235,10 +235,17 @@ int sock_l4(struct ctx *c, int af, uint8_t proto, uint16_t port, fd = socket(af, SOCK_STREAM | SOCK_NONBLOCK, proto); else fd = socket(af, SOCK_DGRAM | SOCK_NONBLOCK, proto); + if (fd < 0) { perror("L4 socket"); return -1; } + + if (fd > SOCKET_MAX) { + close(fd); + return -EIO; + } + ref.r.s = fd; if (af == AF_INET) { -- cgit v1.2.3