NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/27309



The following reply was made to PR bin/27309; it has been noted by GNATS.

From: Jan Schaumann <jschauma%netmeister.org@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc: 
Subject: Re: bin/27309
Date: Thu, 7 Nov 2024 21:36:23 -0500

 Stumbling upon this old bug from (double-checks
 creation date) 20 years ago, I figured I'd take a stab
 at addressing it:
 
 
 Index: syslogd.c
 ===================================================================
 RCS file: /cvsroot/src/usr.sbin/syslogd/syslogd.c,v
 retrieving revision 1.145
 diff -u -p -b -u -r1.145 syslogd.c
 --- syslogd.c	8 Nov 2024 02:23:54 -0000	1.145
 +++ syslogd.c	8 Nov 2024 02:30:20 -0000
 @@ -316,6 +316,7 @@ main(int argc, char *argv[])
  	struct group   *gr;
  	struct passwd  *pw;
  	unsigned long l;
 ++	char pfpath[PATH_MAX];
  
  	/* should we set LC_TIME="C" to ensure correct timestamps&parsing? */
  	(void)setlocale(LC_ALL, "");
 @@ -564,6 +565,33 @@ getgroup:
  	    LOG_NFACILITIES, IETF_NUM_PRIVALUES>>3);
  #endif
  
 +#ifdef __NetBSD_Version__
 +	if ((uid != 0) || (gid != 0)) {
 +		/* Create the pidfile here so we can chown it to the target
 +		 * user/group and possibly report any error before daemonizing.
 +		 * We then call pidfile(3) again to write the actual
 +		 * daemon pid below.
 +		 *
 +		 * Note: this will likely leave the truncated pidfile in
 +		 * place upon exit, since the effective user is unlikely
 +		 * to have write permissions to _PATH_VARRUN. */
 +		if (pidfile(NULL)) {
 +			logerror("Failed to create pidfile");
 +			die(0, 0, NULL);
 +		}
 +		l = sizeof(pfpath);
 +		if ((size_t)snprintf(pfpath, l, "%s%s.pid",
 +					_PATH_VARRUN, getprogname()) >= l) {
 +			logerror("Pidfile path `%s' too long.", pfpath);
 +			die(0, 0, NULL);
 +		}
 +		if (chown(pfpath, uid, gid) < 0) {
 +			logerror("Failed to chown pidfile `%s` to `%d:%d`", pfpath, uid, gid);
 +			die(0, 0, NULL);
 +		}
 +	}
 +#endif /* __NetBSD_Version__ */
 +
  	/*
  	 * All files are open, we can drop privileges and chroot.
  	 */
 


Home | Main Index | Thread Index | Old Index