Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Jul 2024 20:26:04 GMT
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 158f319428c1 - main - route: avoid overlapping strcpy
Message-ID:  <202407232026.46NKQ4M1036542@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=158f319428c10143ce2ffe766416207c75578931

commit 158f319428c10143ce2ffe766416207c75578931
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2024-07-23 20:25:46 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2024-07-23 20:25:56 +0000

    route: avoid overlapping strcpy
    
    Passing overlapping buffers to strcpy yields an undefined result, so
    let's avoid it. The copy doesn't really need to happen anyways, we can
    just point to the domain part of the hostname.
    
    This was discovered with _FORTIFY_SOURCE.
    
    Sponsored by:   Klara, Inc.
    Sponsored by:   Stormshield
    Reviewed by:    allanjude, emaste, imp, melifaro (all previous version)
    Differential Revision:  https://reviews.freebsd.org/D32615
---
 sbin/route/route.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/sbin/route/route.c b/sbin/route/route.c
index 03844308fe84..cea63df3aa11 100644
--- a/sbin/route/route.c
+++ b/sbin/route/route.c
@@ -94,8 +94,8 @@ static u_long  rtm_inits;
 static uid_t	uid;
 static int	defaultfib;
 static int	numfibs;
-static char	domain[MAXHOSTNAMELEN + 1];
-static bool	domain_initialized;
+static char	domain_storage[MAXHOSTNAMELEN + 1];
+static const char	*domain;
 static char	rt_line[NI_MAXHOST];
 static char	net_line[MAXHOSTNAMELEN + 1];
 
@@ -581,14 +581,16 @@ routename(struct sockaddr *sa)
 	const char *cp;
 	int n;
 
-	if (!domain_initialized) {
-		domain_initialized = true;
-		if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
-		    (cp = strchr(domain, '.'))) {
-			domain[MAXHOSTNAMELEN] = '\0';
-			(void)strcpy(domain, cp + 1);
-		} else
-			domain[0] = '\0';
+	if (domain == NULL) {
+		if (gethostname(domain_storage,
+		    sizeof(domain_storage) - 1) == 0 &&
+		    (cp = strchr(domain_storage, '.')) != NULL) {
+			domain_storage[sizeof(domain_storage) - 1] = '\0';
+			domain = cp + 1;
+		} else {
+			domain_storage[0] = '\0';
+			domain = domain_storage;
+		}
 	}
 
 	/* If the address is zero-filled, use "default". */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202407232026.46NKQ4M1036542>