From owner-svn-src-all@freebsd.org Tue Sep 3 14:06:15 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2FCBEDC167; Tue, 3 Sep 2019 14:06:00 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2610:1c1:1:6074::16:84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 46N7yv0qHPz4P40; Tue, 3 Sep 2019 14:05:59 +0000 (UTC) (envelope-from yuripv@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 1452) id 28FA019F07; Tue, 3 Sep 2019 14:05:53 +0000 (UTC) X-Original-To: yuripv@localmail.freebsd.org Delivered-To: yuripv@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id 78AFAA00D; Sat, 30 Mar 2019 17:42:31 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2610:1c1:1:6074::16:84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "freefall.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 2887D6D62F; Sat, 30 Mar 2019 17:42:31 +0000 (UTC) (envelope-from owner-src-committers@freebsd.org) Received: by freefall.freebsd.org (Postfix, from userid 538) id 0AB69A007; Sat, 30 Mar 2019 17:42:31 +0000 (UTC) Delivered-To: src-committers@localmail.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client CN "mx1.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by freefall.freebsd.org (Postfix) with ESMTPS id 7BDB89FFB for ; Sat, 30 Mar 2019 17:42:28 +0000 (UTC) (envelope-from markj@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) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4C13A6D614; Sat, 30 Mar 2019 17:42:28 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 216271A45A; Sat, 30 Mar 2019 17:42:28 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x2UHgSZt032906; Sat, 30 Mar 2019 17:42:28 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x2UHgRZr032904; Sat, 30 Mar 2019 17:42:27 GMT (envelope-from markj@FreeBSD.org) Message-Id: <201903301742.x2UHgRZr032904@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r345738 - in stable/12/lib/libutil: . tests X-SVN-Group: stable-12 X-SVN-Commit-Author: markj X-SVN-Commit-Paths: in stable/12/lib/libutil: . tests X-SVN-Commit-Revision: 345738 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk X-Loop: FreeBSD.org Sender: owner-src-committers@freebsd.org X-Rspamd-Queue-Id: 2887D6D62F X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.97 / 15.00]; local_wl_from(0.00)[freebsd.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.997,0]; NEURAL_HAM_SHORT(-0.97)[-0.970,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_LONG(-1.00)[-1.000,0] Status: O X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Date: Tue, 03 Sep 2019 14:06:16 -0000 X-Original-Date: Sat, 30 Mar 2019 17:42:27 +0000 (UTC) X-List-Received-Date: Tue, 03 Sep 2019 14:06:16 -0000 Author: markj Date: Sat Mar 30 17:42:27 2019 New Revision: 345738 URL: https://svnweb.freebsd.org/changeset/base/345738 Log: MFC r345596: Fix pidfile_open(3) to handle relative paths with multiple components. Modified: stable/12/lib/libutil/pidfile.c stable/12/lib/libutil/tests/pidfile_test.c Directory Properties: stable/12/ (props changed) Modified: stable/12/lib/libutil/pidfile.c ============================================================================== --- stable/12/lib/libutil/pidfile.c Sat Mar 30 17:24:56 2019 (r345737) +++ stable/12/lib/libutil/pidfile.c Sat Mar 30 17:42:27 2019 (r345738) @@ -100,8 +100,9 @@ pidfile_read(int dirfd, const char *filename, pid_t *p } struct pidfh * -pidfile_open(const char *path, mode_t mode, pid_t *pidptr) +pidfile_open(const char *pathp, mode_t mode, pid_t *pidptr) { + char path[MAXPATHLEN]; struct pidfh *pfh; struct stat sb; int error, fd, dirfd, dirlen, filenamelen, count; @@ -112,19 +113,22 @@ pidfile_open(const char *path, mode_t mode, pid_t *pid if (pfh == NULL) return (NULL); - if (path == NULL) { + if (pathp == NULL) { dirlen = snprintf(pfh->pf_dir, sizeof(pfh->pf_dir), "/var/run/"); filenamelen = snprintf(pfh->pf_filename, sizeof(pfh->pf_filename), "%s.pid", getprogname()); } else { - dirlen = snprintf(pfh->pf_dir, sizeof(pfh->pf_dir), - "%s", path); - filenamelen = snprintf(pfh->pf_filename, - sizeof(pfh->pf_filename), "%s", path); - - dirname(pfh->pf_dir); - basename(pfh->pf_filename); + if (strlcpy(path, pathp, sizeof(path)) >= sizeof(path)) { + free(pfh); + errno = ENAMETOOLONG; + return (NULL); + } + dirlen = strlcpy(pfh->pf_dir, dirname(path), + sizeof(pfh->pf_dir)); + (void)strlcpy(path, pathp, sizeof(path)); + filenamelen = strlcpy(pfh->pf_filename, basename(path), + sizeof(pfh->pf_filename)); } if (dirlen >= (int)sizeof(pfh->pf_dir) || Modified: stable/12/lib/libutil/tests/pidfile_test.c ============================================================================== --- stable/12/lib/libutil/tests/pidfile_test.c Sat Mar 30 17:24:56 2019 (r345737) +++ stable/12/lib/libutil/tests/pidfile_test.c Sat Mar 30 17:42:27 2019 (r345738) @@ -263,6 +263,40 @@ test_pidfile_inherited(void) return (result); } +/* + * Make sure we handle relative pidfile paths correctly. + */ +static const char * +test_pidfile_relative(void) +{ + char path[PATH_MAX], pid[32], tmpdir[PATH_MAX]; + struct pidfh *pfh; + int fd; + + (void)snprintf(tmpdir, sizeof(tmpdir), "%s.XXXXXX", __func__); + if (mkdtemp(tmpdir) == NULL) + return (strerror(errno)); + (void)snprintf(path, sizeof(path), "%s/pidfile", tmpdir); + + pfh = pidfile_open(path, 0600, NULL); + if (pfh == NULL) + return (strerror(errno)); + if (pidfile_write(pfh) != 0) + return (strerror(errno)); + fd = open(path, O_RDONLY); + if (fd < 0) + return (strerror(errno)); + if (read(fd, pid, sizeof(pid)) < 0) + return (strerror(errno)); + if (atoi(pid) != getpid()) + return ("pid mismatch"); + if (close(fd) != 0) + return (strerror(errno)); + if (pidfile_close(pfh) != 0) + return (strerror(errno)); + return (NULL); +} + static struct test { const char *name; const char *(*func)(void); @@ -271,6 +305,7 @@ static struct test { { "pidfile_self", test_pidfile_self }, { "pidfile_contested", test_pidfile_contested }, { "pidfile_inherited", test_pidfile_inherited }, + { "pidfile_relative", test_pidfile_relative }, }; int