Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 15 Mar 2018 10:38:31 +0000
From: Bracken Dawson <abdawson@...il.com>
To: musl@...ts.openwall.com
Subject: Program with constructor function segfaults frequently with musl

I have been having trouble getting a cgo program to run with musl, it has
been segfaulting frequently and with 'No stack' when run under gdb.

I have managed to reproduce such a failure in pure c with a very small
example:

```
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

__attribute__((constructor)) void enter_namespace(int argc, char *argv[]) {
    struct option long_options[] = {
        {"some-option", required_argument, 0, 's'},
        {0,0,0,0}
    };
    int option_index, c, pid;
    while ((c = getopt_long_only(argc, argv, "m:", long_options,
&option_index)) != -1) {
        switch (c) {
            case 's':
                pid = atoi(optarg);
                if (pid < 1) {
                    fprintf(stderr, "Invalid some-option: %s\n", optarg);
                    exit(1);
                }
                break;
            case 0:
                break;
        }
    }
}

int main(void) {
  return 0;
}
```

Run with or without options will segfault frequently:
```
~ # gcc -g test.c
~ # ./a.out
Segmentation fault
~ # ./a.out
~ # ./a.out
~ # ./a.out
~ # ./a.out
Segmentation fault
~ #
~ # gdb ./a.out
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html
>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-alpine-linux-musl".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) r
Starting program: /root/a.out
warning: Error disabling address space randomization: Operation not
permitted
During startup program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt
No stack.
(gdb)
```
It appears that having any code in a constructor function leads to this
problem, having the same code in the main function does not segfault.

I'm not sure how to take this any further without a backtrace.

I am using musl 1.1.16-r14 on alpine 3.6.0. The code above works with glibc.

Regards,
Bracken Dawson.

:wq

Content of type "text/html" skipped

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.