From nobody Thu Sep 28 15:52:46 2023 X-Original-To: dev-commits-src-all@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 4RxJ0G4DC6z4tyWw; Thu, 28 Sep 2023 15:52:46 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RxJ0G3QrJz4Lgf; Thu, 28 Sep 2023 15:52:46 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695916366; 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=usrmWjhLd+PqRUrijKB9Uot7by5eIeWEBULkdEv/Kkk=; b=AkSCAKmez5hOp2pPmvbl/TycdrzoQ+irPjwPwpL9i1jinybcbpMem5xDYF462tkkIpU2aA HiHHoXBmWpyDo35yadnWSKCiancf7V9JVWHZjMjNfZpFTjJPfb0y0GjfUyA8lf3mdN8rH2 7Pj8K0/TTq7VTq9Uhykt/+UaUCkfSuAkltNO1VgUcdMuRwZfOq2o6JgIIxDunPKDiyykIa 8M4y9+dB6YYCJxJT8L65rHscoXcp/lfuHBjCP69t2aHXovVC9JLExX9x+4txrdEduPoOhH fg63npV0CWzehy7yuIXdE9oX7N4X/W3m7MVPIO/wFFcnyo7UJ8IeKy8a3oKCCw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695916366; a=rsa-sha256; cv=none; b=ri3pgBCksWd5Otjki/VFbNMyRuM/T0LgBc98PytSKKhcP4tUl7hrWonQMKY9d9898nR8rB MqXH3C7XSaTEGr00hew9LHxoptpqrlc8Fo07NhcnniePFlZK2dX4vnaUx4f2LH02V8AZ7j LFDyusKOiL4GSLHDHhkU0tl72NHG7iu3WC2E69pEylyZEEb6rsyWeQmtYFSVCzR8VoQTo5 dhkg4WGUBPaa7vyDe96HE3MtAkoGbMLPpDj2AamTfYnVIl1ir6e2x5ethNdZSmVyYPbA/n 78OVm2043utAYzk3U/2cBnby22Oi+XZc1WM5AYzaXNg/LREiiwd1yxWGooBm9A== 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=1695916366; 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=usrmWjhLd+PqRUrijKB9Uot7by5eIeWEBULkdEv/Kkk=; b=iKTEWTyB53ERCi7skSOBUi1Sr5q21DS1d88h/8LgXUYLfnSqSCgdmF3m1ty6X4YE96AdZg 1rWeLs2tmbTSmx179pEjkJwgEdeUaaS3scU1L5PafnpbwgdGttqfG6Zw2kwFUlMLeyPXOy NIbIsktJIfSqDHO5rXcJuj74ZmjvRJNEKV5qWBN3v7Na3gI6IBH4jjQOynqpkkznKvBCNv bcZxe7gQTNaYs68c4OdJT/pW2UuV4gZrPpKAG6aENlmnFMLg3OT0UfjJCuBoILL5Fy/b7N 8xkBGM7zf0L5VRbAgsd9Tc1himVq9X6YcuI2zzo0NUOdYo8AjdCZajqVQSJ6gA== 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 4RxJ0G2BqczCy1; Thu, 28 Sep 2023 15:52:46 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38SFqk5m022490; Thu, 28 Sep 2023 15:52:46 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38SFqkQe022487; Thu, 28 Sep 2023 15:52:46 GMT (envelope-from git) Date: Thu, 28 Sep 2023 15:52:46 GMT Message-Id: <202309281552.38SFqkQe022487@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 7428ebfe7e25 - main - syslogd: Pre-open null file descriptor List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 7428ebfe7e25d56e2a5853d36d2c34c54a2f4db3 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=7428ebfe7e25d56e2a5853d36d2c34c54a2f4db3 commit 7428ebfe7e25d56e2a5853d36d2c34c54a2f4db3 Author: Jake Freeland AuthorDate: 2023-09-01 02:51:17 +0000 Commit: Mark Johnston CommitDate: 2023-09-28 15:51:54 +0000 syslogd: Pre-open null file descriptor Open _PATH_DEVNULL before entering capability mode. Access to /dev/null is needed for dup2() when piping commands. Reviewed by: markj MFC after: 3 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D41381 --- usr.sbin/syslogd/syslogd.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 9f4c21cab026..2ff39d8c3676 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -398,6 +398,7 @@ static const int sigcatch[] = { SIGCHLD, }; +static int nulldesc; /* /dev/null descriptor */ static bool Debug; /* debug flag */ static bool Foreground = false; /* Run in foreground, instead of daemonizing */ static bool resolve = true; /* resolve hostname */ @@ -803,6 +804,14 @@ main(int argc, char *argv[]) consfile.f_file = -1; (void)strlcpy(consfile.fu_fname, _PATH_CONSOLE + sizeof(_PATH_DEV) - 1, sizeof(consfile.fu_fname)); + + nulldesc = open(_PATH_DEVNULL, O_RDWR); + if (nulldesc == -1) { + warn("cannot open %s", _PATH_DEVNULL); + pidfile_remove(pfh); + exit(1); + } + (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); if ((!Foreground) && (!Debug)) { @@ -3235,7 +3244,6 @@ markit(void) static int waitdaemon(int maxwait) { - int fd; int status; pid_t pid, childpid; @@ -3266,13 +3274,9 @@ waitdaemon(int maxwait) return (-1); (void)chdir("/"); - if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { - (void)dup2(fd, STDIN_FILENO); - (void)dup2(fd, STDOUT_FILENO); - (void)dup2(fd, STDERR_FILENO); - if (fd > STDERR_FILENO) - (void)close(fd); - } + (void)dup2(nulldesc, STDIN_FILENO); + (void)dup2(nulldesc, STDOUT_FILENO); + (void)dup2(nulldesc, STDERR_FILENO); return (getppid()); } @@ -3595,20 +3599,16 @@ static int p_open(const char *prog, int *rpd) { sigset_t sigset = { }; - int nulldesc, pfd[2], pd; + int pfd[2], pd; pid_t pid; char *argv[4]; /* sh -c cmd NULL */ char errmsg[200]; if (pipe(pfd) == -1) return (-1); - if ((nulldesc = open(_PATH_DEVNULL, O_RDWR)) == -1) - /* we are royally screwed anyway */ - return (-1); switch ((pid = pdfork(&pd, PD_CLOEXEC))) { case -1: - close(nulldesc); return (-1); case 0: @@ -3639,7 +3639,6 @@ p_open(const char *prog, int *rpd) (void)execvp(_PATH_BSHELL, argv); _exit(255); } - close(nulldesc); close(pfd[0]); /* * Avoid blocking on a hung pipe. With O_NONBLOCK, we are