[PATCH] hwclock: avoid UB if TZUTC is unset; include sys/time.h for timeval

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

 



Fix a crash reported by Jean-Marc Pigeon on the musl mailing list.

If TZUTC is unset, setenv() will be called with value = NULL,
which invokes undefined behavior.
With musl libc, this causes a crash.
On uclibc and glibc, this currently happens to act like the long form
here, but glibc will make this an error:
 https://sourceware.org/ml/libc-alpha/2015-03/threads.html#00449

Additionally, include <sys/time.h> to define struct timeval.
---
 sys-utils/hwclock.c | 5 ++++-
 sys-utils/hwclock.h | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c
index e1e5816..67971f0 100644
--- a/sys-utils/hwclock.c
+++ b/sys-utils/hwclock.c
@@ -408,7 +408,10 @@ mktime_tz(struct tm tm, const bool universal,
 		 * libc's:
 		 *    TZUTC=:/usr/share/zoneinfo/right/UTC
 		 */
-		setenv("TZ", getenv("TZUTC"), TRUE);
+		char *tzutc;
+
+		tzutc = getenv("TZUTC");
+		setenv("TZ", tzutc ? tzutc : "", TRUE);
 		/*
 		 * Note: tzset() gets called implicitly by the time code,
 		 * but only the first time. When changing the environment
diff --git a/sys-utils/hwclock.h b/sys-utils/hwclock.h
index 974d96a..7f19c39 100644
--- a/sys-utils/hwclock.h
+++ b/sys-utils/hwclock.h
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
+#include <sys/time.h>
 
 #include "c.h"
 
-- 
2.3.5

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux