From 198294a383291be3094db7131fdf0a26a16307cb Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 20 Jan 2015 22:47:35 -0600 Subject: [PATCH] add __progname emulation, prevent setproctitle from overwriting it Make a copy of __progname on start to avoid setproctitle clobbering it later. Check if the OS supports __progname and emulate if unavailable. - from OpenSSH. Thanks to Paul B. Henson for reporting the setproctitle emulation issue and Jonas 'Sortie' Termansen for suggesting __progname emulation. --- Makefile.am | 2 + compat/progname.c | 46 +++++++++++++++++++ configure.ac | 11 +++++ ...-verbose-when-logging-privsep-errors.patch | 2 +- ...privsep-user-to-be-a-privileged-user.patch | 2 +- ...records-on-IPv4-networks-more-libera.patch | 2 +- ...EAI_NODATA-does-not-exist-everywhere.patch | 2 +- ...P-syslog-facility-if-it-is-available.patch | 2 +- ...ll-in-sin_len-sin6_len-if-they-exist.patch | 2 +- ...heck-if-rdomain-support-is-available.patch | 2 +- ...onf-to-indicate-OS-dependent-options.patch | 2 +- ...ding-default-user-and-file-locations.patch | 2 +- ...10-add-p-option-to-create-a-pid-file.patch | 4 +- ...initialize-setproctitle-where-needed.patch | 18 +++++--- ...-fallback-if-SIGINFO-is-not-implemen.patch | 2 +- ...log-context-of-all-fatal-allocations.patch | 12 ++--- 16 files changed, 89 insertions(+), 24 deletions(-) create mode 100644 compat/progname.c diff --git a/Makefile.am b/Makefile.am index aa7ddac..9e50d12 100644 --- a/Makefile.am +++ b/Makefile.am @@ -174,6 +174,8 @@ if !HAVE_MD5 libcompat_la_SOURCES += compat/md5.c endif +libcompat_la_SOURCES += compat/progname.c + if !HAVE_REALLOCARRAY libcompat_la_SOURCES += compat/reallocarray.c endif diff --git a/compat/progname.c b/compat/progname.c new file mode 100644 index 0000000..3238189 --- /dev/null +++ b/compat/progname.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 1999-2004 Damien Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#ifndef HAVE___PROGNAME +char *__progname; +#endif + +/* + * NB. duplicate __progname in case it is an alias for argv[0] + * Otherwise it may get clobbered by setproctitle() + */ +char *get_progname(char *argv0) +{ +#ifdef HAVE___PROGNAME + extern char *__progname; + + return strdup(__progname); +#else + char *p; + + if (argv0 == NULL) + return ("unknown"); /* XXX */ + p = strrchr(argv0, '/'); + if (p == NULL) + p = argv0; + else + p++; + + return (strdup(p)); +#endif +} diff --git a/configure.ac b/configure.ac index ee119d1..3f922c6 100644 --- a/configure.ac +++ b/configure.ac @@ -153,6 +153,17 @@ AM_CONDITIONAL([HAVE_ARC4RANDOM_UNIFORM], -a "x$HOST_OS" != xnetbsd \ -a "x$ac_cv_func_arc4random_uniform" = xyes]) +AC_CACHE_CHECK([if libc defines __progname], ac_cv_libc_defines___progname, [ + AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], + [[ extern char *__progname; printf("%s", __progname); ]])], + [ ac_cv_libc_defines___progname="yes" ], + [ ac_cv_libc_defines___progname="no" + ]) +]) +if test "x$ac_cv_libc_defines___progname" = "xyes" ; then + AC_DEFINE([HAVE___PROGNAME], [1], [Define if libc defines __progname]) +fi + AC_CACHE_CHECK([whether va_copy exists], ac_cv_have_va_copy, [ AC_LINK_IFELSE([AC_LANG_PROGRAM([[ #include diff --git a/patches/0001-be-more-verbose-when-logging-privsep-errors.patch b/patches/0001-be-more-verbose-when-logging-privsep-errors.patch index 1f77ddb..2bd4994 100644 --- a/patches/0001-be-more-verbose-when-logging-privsep-errors.patch +++ b/patches/0001-be-more-verbose-when-logging-privsep-errors.patch @@ -1,4 +1,4 @@ -From 3358e0e85bf40351bc4a8357a878c844c4190ef2 Mon Sep 17 00:00:00 2001 +From c214bf6eca6de29239d00b4a9ce6528f82b25bec Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 1 Jan 2015 13:06:38 -0600 Subject: [PATCH 01/13] be more verbose when logging privsep errors diff --git a/patches/0002-do-not-allow-privsep-user-to-be-a-privileged-user.patch b/patches/0002-do-not-allow-privsep-user-to-be-a-privileged-user.patch index 24ba7ec..b2f7ea7 100644 --- a/patches/0002-do-not-allow-privsep-user-to-be-a-privileged-user.patch +++ b/patches/0002-do-not-allow-privsep-user-to-be-a-privileged-user.patch @@ -1,4 +1,4 @@ -From fb62935494233b756bfbe48b3652c07c378ef55e Mon Sep 17 00:00:00 2001 +From 38bb78974653ef6e791f986cab561c3194f84297 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 1 Jan 2015 13:26:29 -0600 Subject: [PATCH 02/13] do not allow privsep user to be a privileged user diff --git a/patches/0003-Handle-IPv6-DNS-records-on-IPv4-networks-more-libera.patch b/patches/0003-Handle-IPv6-DNS-records-on-IPv4-networks-more-libera.patch index 5a71178..d925063 100644 --- a/patches/0003-Handle-IPv6-DNS-records-on-IPv4-networks-more-libera.patch +++ b/patches/0003-Handle-IPv6-DNS-records-on-IPv4-networks-more-libera.patch @@ -1,4 +1,4 @@ -From 1f533ce93b5826f3c3e3b16b237571c9c62dc1eb Mon Sep 17 00:00:00 2001 +From be2634a7df434a90896a8678da308734943f69bb Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 30 Dec 2014 09:10:22 -0600 Subject: [PATCH 03/13] Handle IPv6 DNS records on IPv4 networks more liberally diff --git a/patches/0004-EAI_NODATA-does-not-exist-everywhere.patch b/patches/0004-EAI_NODATA-does-not-exist-everywhere.patch index ac9ee6d..cb7d4fb 100644 --- a/patches/0004-EAI_NODATA-does-not-exist-everywhere.patch +++ b/patches/0004-EAI_NODATA-does-not-exist-everywhere.patch @@ -1,4 +1,4 @@ -From aff999eff42db8c1022fc4c95cd4f2bc689e0708 Mon Sep 17 00:00:00 2001 +From 266265a2ac85afe947b787e0c991168fa0c4adc0 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 30 Dec 2014 09:04:08 -0600 Subject: [PATCH 04/13] EAI_NODATA does not exist everywhere diff --git a/patches/0005-Use-LOG_NTP-syslog-facility-if-it-is-available.patch b/patches/0005-Use-LOG_NTP-syslog-facility-if-it-is-available.patch index 64a8892..e497a77 100644 --- a/patches/0005-Use-LOG_NTP-syslog-facility-if-it-is-available.patch +++ b/patches/0005-Use-LOG_NTP-syslog-facility-if-it-is-available.patch @@ -1,4 +1,4 @@ -From 0a55dd5a69636bcb8047e84e63f41cac6d8ac4ae Mon Sep 17 00:00:00 2001 +From 125039fad99de2ad2ac0c0738c692dae14c48bfb Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Mon, 12 Jan 2015 21:16:54 -0600 Subject: [PATCH 05/13] Use LOG_NTP syslog facility if it is available diff --git a/patches/0006-conditionally-fill-in-sin_len-sin6_len-if-they-exist.patch b/patches/0006-conditionally-fill-in-sin_len-sin6_len-if-they-exist.patch index 81b7a76..001c0f6 100644 --- a/patches/0006-conditionally-fill-in-sin_len-sin6_len-if-they-exist.patch +++ b/patches/0006-conditionally-fill-in-sin_len-sin6_len-if-they-exist.patch @@ -1,4 +1,4 @@ -From a4991f440faa3e9b073c522b62438a32b7fb6ef3 Mon Sep 17 00:00:00 2001 +From c7bfa2373a06fbed91ca35cac664b1b92cf6a8d0 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 30 Dec 2014 09:02:50 -0600 Subject: [PATCH 06/13] conditionally fill in sin_len/sin6_len if they exist diff --git a/patches/0007-check-if-rdomain-support-is-available.patch b/patches/0007-check-if-rdomain-support-is-available.patch index 6e3af22..fcb39f2 100644 --- a/patches/0007-check-if-rdomain-support-is-available.patch +++ b/patches/0007-check-if-rdomain-support-is-available.patch @@ -1,4 +1,4 @@ -From 682b4ff97c28a1def8192c4eeb9ea8c6b92e3b6c Mon Sep 17 00:00:00 2001 +From c8bac970282975fd81542dcf5173e5ffdad545c3 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 30 Dec 2014 09:05:46 -0600 Subject: [PATCH 07/13] check if rdomain support is available. diff --git a/patches/0008-update-ntpd.conf-to-indicate-OS-dependent-options.patch b/patches/0008-update-ntpd.conf-to-indicate-OS-dependent-options.patch index 18ad97e..393ff6d 100644 --- a/patches/0008-update-ntpd.conf-to-indicate-OS-dependent-options.patch +++ b/patches/0008-update-ntpd.conf-to-indicate-OS-dependent-options.patch @@ -1,4 +1,4 @@ -From 41b570b1794534abe338dfc6dc0d0ff002c211c0 Mon Sep 17 00:00:00 2001 +From 9a2a22b3714bdb398f9b03a64d91aecbdd9b5fc0 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Tue, 30 Dec 2014 09:20:03 -0600 Subject: [PATCH 08/13] update ntpd.conf to indicate OS-dependent options diff --git a/patches/0009-allow-overriding-default-user-and-file-locations.patch b/patches/0009-allow-overriding-default-user-and-file-locations.patch index c882700..0a5e475 100644 --- a/patches/0009-allow-overriding-default-user-and-file-locations.patch +++ b/patches/0009-allow-overriding-default-user-and-file-locations.patch @@ -1,4 +1,4 @@ -From e432380f8540be3021efa57c723e00c50073f953 Mon Sep 17 00:00:00 2001 +From 2e60189298e177d811c4574bf07b4721361d306b Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Thu, 1 Jan 2015 07:18:11 -0600 Subject: [PATCH 09/13] allow overriding default user and file locations diff --git a/patches/0010-add-p-option-to-create-a-pid-file.patch b/patches/0010-add-p-option-to-create-a-pid-file.patch index 9f94311..dbfc3f0 100644 --- a/patches/0010-add-p-option-to-create-a-pid-file.patch +++ b/patches/0010-add-p-option-to-create-a-pid-file.patch @@ -1,4 +1,4 @@ -From 1f659267b350fadc3b430a9723c69c780d2eea6f Mon Sep 17 00:00:00 2001 +From ff82cc8278eafc04ee00f1e847c7424d857b2907 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Wed, 31 Dec 2014 08:26:41 -0600 Subject: [PATCH 10/13] add -p option to create a pid file @@ -35,7 +35,7 @@ index 18b12e8..9eb1fee 100644 Do not set the time immediately at startup. This is the default. diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c -index ee1ecb5..f89532f 100644 +index e2c189c..f5f0dbb 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -83,6 +83,18 @@ sighdlr(int sig) diff --git a/patches/0011-initialize-setproctitle-where-needed.patch b/patches/0011-initialize-setproctitle-where-needed.patch index 9b4ce12..65f91b7 100644 --- a/patches/0011-initialize-setproctitle-where-needed.patch +++ b/patches/0011-initialize-setproctitle-where-needed.patch @@ -1,17 +1,19 @@ -From 198aa5cd7cf86d9c10dbdf24291e1f8afbb12410 Mon Sep 17 00:00:00 2001 +From 026e34d46265334276120711dc80f42ea20c3b82 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Mon, 12 Jan 2015 06:18:31 -0600 Subject: [PATCH 11/13] initialize setproctitle where needed +We need to save a copy of argv and __progname to avoid setproctitle +clobbering them. --- - src/usr.sbin/ntpd/ntpd.c | 16 ++++++++++++++++ - 1 file changed, 16 insertions(+) + src/usr.sbin/ntpd/ntpd.c | 20 ++++++++++++++++++++ + 1 file changed, 20 insertions(+) diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c -index f89532f..ea8cf70 100644 +index f5f0dbb..26463f6 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c -@@ -111,6 +111,11 @@ usage(void) +@@ -111,6 +111,13 @@ usage(void) #define POLL_MAX 8 #define PFD_PIPE 0 @@ -19,14 +21,18 @@ index f89532f..ea8cf70 100644 +#ifndef HAVE_SETPROCTITLE +static char **saved_argv; +#endif ++ ++char *get_progname(char *argv0); + int main(int argc, char *argv[]) { -@@ -134,6 +139,17 @@ main(int argc, char *argv[]) +@@ -134,6 +141,19 @@ main(int argc, char *argv[]) log_init(1); /* log to stderr until daemonized */ ++ __progname = get_progname(argv[0]); ++ +#ifndef HAVE_SETPROCTITLE + int i; + /* Prepare for later setproctitle emulation */ diff --git a/patches/0012-document-SIGUSR1-fallback-if-SIGINFO-is-not-implemen.patch b/patches/0012-document-SIGUSR1-fallback-if-SIGINFO-is-not-implemen.patch index 7279eb5..dff00ac 100644 --- a/patches/0012-document-SIGUSR1-fallback-if-SIGINFO-is-not-implemen.patch +++ b/patches/0012-document-SIGUSR1-fallback-if-SIGINFO-is-not-implemen.patch @@ -1,4 +1,4 @@ -From 1da8b11e25548aa48f055b7c8c1c97310af73fdb Mon Sep 17 00:00:00 2001 +From 0763f9baeb5f915dce6da779cc57a6e374f92cf7 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Mon, 12 Jan 2015 21:17:35 -0600 Subject: [PATCH 12/13] document SIGUSR1 fallback if SIGINFO is not diff --git a/patches/0013-log-context-of-all-fatal-allocations.patch b/patches/0013-log-context-of-all-fatal-allocations.patch index ee5d4aa..961d36d 100644 --- a/patches/0013-log-context-of-all-fatal-allocations.patch +++ b/patches/0013-log-context-of-all-fatal-allocations.patch @@ -1,4 +1,4 @@ -From c33a7dbd5827d8cf3e81193ea128d9b29a596c4c Mon Sep 17 00:00:00 2001 +From a53fb3d0ccb42e59b0ce2e3fa6f141da106a7294 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Mon, 19 Jan 2015 04:37:59 -0600 Subject: [PATCH 13/13] log context of all fatal allocations @@ -87,7 +87,7 @@ index c8f095e..7db98f6 100644 p += sizeof(h->ss); dlen -= sizeof(h->ss); diff --git a/src/usr.sbin/ntpd/ntp_dns.c b/src/usr.sbin/ntpd/ntp_dns.c -index 7d0236f..6abe3a4 100644 +index 14e6b76..b77d486 100644 --- a/src/usr.sbin/ntpd/ntp_dns.c +++ b/src/usr.sbin/ntpd/ntp_dns.c @@ -87,7 +87,7 @@ ntp_dns(int pipe_ntp[2], struct ntpd_conf *nconf, struct passwd *pw) @@ -100,10 +100,10 @@ index 7d0236f..6abe3a4 100644 while (quit_dns == 0) { diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c -index ea8cf70..65503d9 100644 +index 26463f6..b65deb7 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c -@@ -235,7 +235,7 @@ main(int argc, char *argv[]) +@@ -239,7 +239,7 @@ main(int argc, char *argv[]) close(pipe_chld[1]); if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) @@ -112,7 +112,7 @@ index ea8cf70..65503d9 100644 imsg_init(ibuf, pipe_chld[0]); while (quit == 0) { -@@ -619,7 +619,7 @@ ctl_main(int argc, char *argv[]) +@@ -623,7 +623,7 @@ ctl_main(int argc, char *argv[]) break; } } @@ -121,7 +121,7 @@ index ea8cf70..65503d9 100644 usage(); /* NOTREACHED */ -@@ -635,7 +635,7 @@ ctl_main(int argc, char *argv[]) +@@ -639,7 +639,7 @@ ctl_main(int argc, char *argv[]) err(1, "connect: %s", sockname); if ((ibuf_ctl = malloc(sizeof(struct imsgbuf))) == NULL)