* [musl] [PATCH] src: network: replace unsafe strcat with strncat in getnameinfo.c
@ 2025-02-25 13:26 Anton Moryakov
2025-02-25 14:09 ` Rich Felker
0 siblings, 1 reply; 2+ messages in thread
From: Anton Moryakov @ 2025-02-25 13:26 UTC (permalink / raw)
To: musl; +Cc: Anton Moryakov
Replaced the vulnerable `strcat` function with `strncat` to prevent
potential buffer overflow. The new implementation limits the number
of characters copied to the remaining space in the destination buffer,
ensuring safe string concatenation.
The change addresses the following warning:
/.build/src/network/getnameinfo.c:178
Use of vulnerable function 'strcat' at getnameinfo.c:178. This function
is unsafe, use strncat instead.
While it is unclear if the static analyzer correctly identified this
as a vulnerability, it is better to err on the side of caution and
make the code safer by using `strncat`. The fix calculates the
available space in the buffer using `sizeof(buf) - strlen(buf) - 1`
to leave room for the null terminator.
Triggers found by static analyzer Svace.
Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
src/network/getnameinfo.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/network/getnameinfo.c b/src/network/getnameinfo.c
index 133c15b3..e15f4457 100644
--- a/src/network/getnameinfo.c
+++ b/src/network/getnameinfo.c
@@ -179,7 +179,7 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
if (!p)
p = itoa(num, scopeid);
*--p = '%';
- strcat(buf, p);
+ strncat(buf, p, sizeof(buf) - strlen(buf) - 1);
}
}
if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
--
2.30.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [musl] [PATCH] src: network: replace unsafe strcat with strncat in getnameinfo.c
2025-02-25 13:26 [musl] [PATCH] src: network: replace unsafe strcat with strncat in getnameinfo.c Anton Moryakov
@ 2025-02-25 14:09 ` Rich Felker
0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2025-02-25 14:09 UTC (permalink / raw)
To: Anton Moryakov; +Cc: musl
On Tue, Feb 25, 2025 at 04:26:46PM +0300, Anton Moryakov wrote:
> Replaced the vulnerable `strcat` function with `strncat` to prevent
> potential buffer overflow. The new implementation limits the number
> of characters copied to the remaining space in the destination buffer,
> ensuring safe string concatenation.
>
> The change addresses the following warning:
> /.build/src/network/getnameinfo.c:178
> Use of vulnerable function 'strcat' at getnameinfo.c:178. This function
> is unsafe, use strncat instead.
>
> While it is unclear if the static analyzer correctly identified this
> as a vulnerability, it is better to err on the side of caution and
> make the code safer by using `strncat`. The fix calculates the
> available space in the buffer using `sizeof(buf) - strlen(buf) - 1`
> to leave room for the null terminator.
>
> Triggers found by static analyzer Svace.
>
> Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
>
> ---
> src/network/getnameinfo.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/src/network/getnameinfo.c b/src/network/getnameinfo.c
> index 133c15b3..e15f4457 100644
> --- a/src/network/getnameinfo.c
> +++ b/src/network/getnameinfo.c
> @@ -179,7 +179,7 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl,
> if (!p)
> p = itoa(num, scopeid);
> *--p = '%';
> - strcat(buf, p);
> + strncat(buf, p, sizeof(buf) - strlen(buf) - 1);
> }
> }
> if (strlen(buf) >= nodelen) return EAI_OVERFLOW;
> --
> 2.30.2
buf is 256 bytes. inet_ntop returns at most INET6_ADDRSTRLEN (46)
bytes. p points to a string of length at most IF_NAMESIZE+1 (17) or
3*sizeof(int) (12) bytes.
Rich
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-02-25 14:09 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-25 13:26 [musl] [PATCH] src: network: replace unsafe strcat with strncat in getnameinfo.c Anton Moryakov
2025-02-25 14:09 ` Rich Felker
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
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).