Subject: [alpine-devel] Segfault in hwclock

From: Jeff Pohlmeyer <[email-masked]>
Date: Wed, 9 Apr 2014 17:16:14 -0500

The "hwclock" program from util-linux is crashing with musl.

The problem is on line 393 of util-linux-2.23.2/sys-utils/hwclock.c:

  setenv("TZ", getenv("TZUTC"), TRUE);

Apparently musl's version of setenv() doesn't like it when the second
argument is NULL.

( The crash only happens if the "TZUTC" environment variable is unset. )

Here is a simple test app:
/*********************************/
#include <stdlib.h>
int main(int argc, char*argv[]) {
  setenv("FOO",NULL,1);
  return 0;
}
/*********************************/

Compiling with gcc -Wall on ArchLinux/GNU-libc, I get a warning:

  warning: null argument where non-null required (argument 2) [-Wnonnull]

But the program doesn't crash at run time.

With Alpine/musl, there is no compile time warning, but the program crashes.

So it looks like musl's setenv() could stand to be a bit more robust,
but hwclock
really shouldn't be passing it a null, either.

 - Jeff

---
Unsubscribe:  [email-masked]
Help:         [email-masked]
---

---

From: Timo Teras <[email-masked]>
Date: Thu, 10 Apr 2014 09:11:16 +0300

On Wed, 9 Apr 2014 17:16:14 -0500

> The "hwclock" program from util-linux is crashing with musl.

Committed a fix.

> Apparently musl's version of setenv() doesn't like it when the second

Correct. This is per posix specification. value needs to point to a
valid string. This is application code bug.

>   warning: null argument where non-null required (argument 2)

This is implies that glibc thinks same way. value must not be null. The
attribute also allows compiler to assume this and optimize code
accordingly. It would be perfectly valid for gcc to optimize glibc
build in such a way that it would crash too.

> But the program doesn't crash at run time.

Depends on compiler.

> really shouldn't be passing it a null, either.

Fixed application.

Thanks for the report.

- Timo

---