mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] getservbyport_r: fix out-of-bounds buffer read
@ 2023-02-27 20:18 Alexey Izbyshev
  0 siblings, 0 replies; only message in thread
From: Alexey Izbyshev @ 2023-02-27 20:18 UTC (permalink / raw)
  To: musl

If the buffer passed to getservbyport_r is just enough to store two
pointers after aligning it, getnameinfo is called with buflen == 0
(which means that service name is not needed) and trivially succeeds.
Then, strtol is called on the address just past the buffer end, and
if it doesn't happen to find the port number there, getservbyport_r
spuriously succeeds and returns the same bad address to the caller.

Fix this by ensuring that buflen is at least 1 when passed to
 src/network/getservbyport_r.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/network/getservbyport_r.c b/src/network/getservbyport_r.c
index b7f21c6b..d3a22b8c 100644
--- a/src/network/getservbyport_r.c
+++ b/src/network/getservbyport_r.c
@@ -26,7 +26,7 @@ int getservbyport_r(int port, const char *prots,
 	/* Align buffer */
 	i = (uintptr_t)buf & sizeof(char *)-1;
 	if (!i) i = sizeof(char *);
-	if (buflen < 3*sizeof(char *)-i)
+	if (buflen <= 3*sizeof(char *)-i)
 		return ERANGE;
 	buf += sizeof(char *)-i;
 	buflen -= sizeof(char *)-i;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-02-27 20:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-27 20:18 [musl] [PATCH] getservbyport_r: fix out-of-bounds buffer read Alexey Izbyshev

Code repositories for project(s) associated with this public inbox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).