From nobody Thu Aug 08 20:07:02 2024 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WfykH1cmZz5S8p7; Thu, 08 Aug 2024 20:07:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WfykH0g4Cz4pdk; Thu, 8 Aug 2024 20:07:03 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1723147623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=shqNHeU4i0TcaccDA0Tol7azt5EhwvoZOENpJvqRpzE=; b=Ahu4sTVXIh2wIneUQYwrOgKYF8bZR4EFiBQvtEYECkWHExgr1WpZkKi4N03w/EvQmruDM8 wsbokvwmp6ZoM9r2llJ8XZPqZXM6fhvraMuj/FA23Bo142Zy9UCFfMiU6ZqrzXws6qKgO3 edliEPQomZHNfu2oqIbmGEWi8QrILU9WRmzvRu7LClPjRB6jMZ48Cu8VcFHunbKtAIpzZ8 h0v2g3dpau3LOlZDDk63jXqgAE4wCZC0viJ2OqLl25fanacXpFzzQRXCa/NNUoldAcqerp aIHUgFN5PmeKfE3wIMwZVck2U5pMn/4US5pb1kQJodwTqfjinucDkjGZR0Yrrw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1723147623; a=rsa-sha256; cv=none; b=PAysAhK46nCSh5WqvJ6L/BoZwItkVWxoa1K8UeYdrWlJBlL4pEiqwF7e13cKWFuY/TweZd PgDacaMPYqvJ9euevSB/Z54/qsFX9iHJEyZgQvxVx60lBoX0eCFXGs3NjX0UYeOBQIv5um 7dCYRFCsayNI8Fm21VzrW6WDn9UK3TbeNJwrBkD09c68wqDW1fD22rXQ5x8j1Z7OgcYAcf LFNeMMyErzpJGS5A327S5P5pZBDUMqSpeL9l0w85ojHbrF0R9t4EQCQhk/vFgcYFHd1z+y lkx4kBiV4TDi9S8Jp+dBdP/ne2AlNJk5uuR2Cpw/zQNvk00AG5ZrqUzlVX1oqA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1723147623; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=shqNHeU4i0TcaccDA0Tol7azt5EhwvoZOENpJvqRpzE=; b=W8XFOcE0dPKPkwR78pY6ORdssDA2ljE/OLcPJWrSg7uvGfZMoRYsF1H6q1E2y4SI/mwubq KKyAtuLOoo/7//p3ld2I9w9dQD9+t5bllrXtlPh+9fSSXHKROmub081m/zmGzyp58fjTod lDaUyMRqzZAOeF/SIvAPSzKHNVtBC5mzTUC8NFELKIrRsDYcNcUOonTGahqI2pQIx2XIJX lzqaKi/0GlSzsb6WTOdbufjD+5u36b16MifJGOGY3/DJatAxFgbEp89bA+JrSGJX4XZcWq vTvAJA67Gj42LjCT9+a9s1cOdY8FgpVVumBhbWL+/fw7B/hCXn9LiFqv/u38sw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WfykH0GGpzn1B; Thu, 8 Aug 2024 20:07:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 478K72ix017109; Thu, 8 Aug 2024 20:07:02 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 478K72pb017106; Thu, 8 Aug 2024 20:07:02 GMT (envelope-from git) Date: Thu, 8 Aug 2024 20:07:02 GMT Message-Id: <202408082007.478K72pb017106@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: 40c79e979ea8 - stable/14 - route: avoid overlapping strcpy List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 40c79e979ea8dbbf2262297865754910f8d704c0 Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=40c79e979ea8dbbf2262297865754910f8d704c0 commit 40c79e979ea8dbbf2262297865754910f8d704c0 Author: Kyle Evans AuthorDate: 2024-07-23 20:25:46 +0000 Commit: Kyle Evans CommitDate: 2024-08-08 19:59:15 +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) (cherry picked from commit 158f319428c10143ce2ffe766416207c75578931) --- 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 7cf2bf842559..ad7a906bc142 100644 --- a/sbin/route/route.c +++ b/sbin/route/route.c @@ -107,8 +107,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]; @@ -594,14 +594,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". */