]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEBUG: fd: make sure we never try to insert/delete an impossible FD number
authorWilly Tarreau <w@1wt.eu>
Mon, 31 Jan 2022 19:05:02 +0000 (20:05 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 31 Jan 2022 20:00:35 +0000 (21:00 +0100)
It's among the cases that would provoke memory corruption, let's add
some tests against negative FDs and those larger than the table. This
must never ever happen and would currently result in silent corruption
or a crash. Better have a noticeable one exhibiting the call chain if
that were to happen.

include/haproxy/fd.h
src/fd.c

index 40ef38f734ab8db285bd7733c8bff181f5e49452..8bf30cd255821b6d7f01abdf70525519890d031e 100644 (file)
@@ -319,6 +319,11 @@ static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned
 {
        extern void sock_conn_iocb(int);
 
+       /* This must never happen and would definitely indicate a bug, in
+        * addition to overwriting some unexpected memory areas.
+        */
+       BUG_ON(fd < 0 || fd >= global.maxsock);
+
        fdtab[fd].owner = owner;
        fdtab[fd].iocb = iocb;
        fdtab[fd].state = 0;
index 30fefc473efcfd0c52a508baba60e56debdf45f9..c2dfcf1d6d1d18ae1d7183db66097888b46f6757 100644 (file)
--- a/src/fd.c
+++ b/src/fd.c
@@ -336,6 +336,11 @@ void _fd_delete_orphan(int fd)
  */
 void fd_delete(int fd)
 {
+       /* This must never happen and would definitely indicate a bug, in
+        * addition to overwriting some unexpected memory areas.
+        */
+       BUG_ON(fd < 0 || fd >= global.maxsock);
+
        /* we must postpone removal of an FD that may currently be in use
         * by another thread. This can happen in the following two situations:
         *   - after a takeover, the owning thread closes the connection but