mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Anton Moryakov <ant.v.moryakov@gmail.com>
To: musl@lists.openwall.com
Cc: Anton Moryakov <ant.v.moryakov@gmail.com>
Subject: [musl] [PATCH] src: string: Replace unsafe strcpy with strncat in strcat()
Date: Sun, 16 Feb 2025 20:39:27 +0300	[thread overview]
Message-ID: <20250216173927.712148-1-ant.v.moryakov@gmail.com> (raw)

Static analyzer reported:
PROC_USE.VULNERABLE Use of vulnerable function 'strcpy' at strcat.c:5. This function is unsafe, use strncpy instead.

Corrections explained:
Replaced the vulnerable function strcpy with strncat in strcat()
to prevent potential buffer overflows.

Previous code:
    strcpy(dest + strlen(dest), src);

New code:
    strncat(dest, src, strlen(src));

This improves security but does not guarantee full buffer safety. 
For complete protection, dest_size should be explicitly checked

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>

---
 src/string/strcat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/string/strcat.c b/src/string/strcat.c
index 33f749b1..d341facf 100644
--- a/src/string/strcat.c
+++ b/src/string/strcat.c
@@ -2,6 +2,6 @@
 
 char *strcat(char *restrict dest, const char *restrict src)
 {
-	strcpy(dest + strlen(dest), src);
+    strncat(dest, src, strlen(src));
 	return dest;
 }
-- 
2.30.2


             reply	other threads:[~2025-02-16 17:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-16 17:39 Anton Moryakov [this message]
2025-02-16 18:02 ` Rich Felker
2025-02-16 18:03   ` Anton Moryakov
2025-02-16 18:06     ` Rich Felker
2025-02-16 17:39 Anton Moryakov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250216173927.712148-1-ant.v.moryakov@gmail.com \
    --to=ant.v.moryakov@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).