4

I have been using lighttpd with cgi on ubuntu. I have come across a "500 - Internal Server Error" case which I want to debug.

Is there a way I can ask lighttpd/cgi to dump core when my binary crashes? I came across this page, it has a field for enabling core dump but including server.core-files="enable" in lighhtpd.conf doesn't seem to work.

Any idea?

Thanks.

P.S. Following is the test program I used for generating core:

int main()
{
    char* html = "<html>\n"\
                 "<header>" \
                 "<title>This is title</title></header>\n" \
                 "<body>\n"\
                 "Hello world\n"\
                 "</body>\n"\
                 "</html>";
    html[1] = 'r';
    std::cout<<html;
    return 0;
}

I have set limit to unlimited by doing "ulimit -c unlimited". I can see a core in my current directory when I run binary from command line. But no core in cgi-bin when called from browser. Cgi log has following entries:

2018-07-29 17:20:41: (server.c.1558) server stopped by UID = 0 PID = 1

2018-07-29 17:21:18: (log.c.164) server started

2018-07-29 21:29:16: (mod_cgi.c.601) cgi died, pid: 28197

2018-07-29 21:29:17: (mod_cgi.c.601) cgi died, pid: 28202

Community
  • 1
  • 1
Shivendra Mishra
  • 638
  • 4
  • 25

3 Answers3

0

You don't need to ask lighttpd to generate a core dump on crashes. You need to ask the kernel. Check with sysctl -a | grep kernel.core_pattern how/where/if core files are written on your system and change it if needed.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • It's "core" actually (kernel.core_pattern = core). I can't find core in cgi-bin but I can see in current directory when I run binary from command line. – Shivendra Mishra Jul 29 '18 at 15:36
0

Making long story short, this is what standard says about string literals:

A string literal that does not begin with u, U, or L is an ordinary string literal, also referred to as a narrow string literal. An ordinary string literal has type “array of n const char”, where n is the size of the string as defined below; it has static storage duration (3.7) and is initialized with the given characters.

So what You are doing here is somewhat equivalent:

const char tmp_html[] = "<html>\n"\
             "<header>" \
             "<title>This is title</title></header>\n" \
             "<body>\n"\
             "Hello world\n"\
             "</body>\n"\
             "</html>";
char *html = const_cast<char *>(tmp_html);
html[1] = 'r';

Considering You are casting out const and fact that it most probably constness causes putting literal in not-writeble memory it is no wonder that the program crashes. Simplest solution would be using C++ instead of archaic C solutions:

int main()
{
    std::string html = "<html>\n"\
                 "<header>" \
                 "<title>This is title</title></header>\n" \
                 "<body>\n"\
                 "Hello world\n"\
                 "</body>\n"\
                 "</html>";
    html[1] = 'r';
    std::cout<<html;
    return 0;
}
bartop
  • 9,971
  • 1
  • 23
  • 54
0

I got core dumps of a cgi invoked by lighttpd after applying the following changes to Ubuntu 18.04.

ulimit -c                                # 0=disabled
ulimit -c unlimited                      # enable core dumping
cat /proc/sys/fs/suid_dumpable           # 0=disabled, 1=???, 2=???
sysctl -w fs.suid_dumpable=2             # to enable dumping of setuid root process
cat /proc/sys/kernel/core_pattern        # to check how core dump is handled
echo core>/proc/sys/kernel/core_pattern  # to stop apport intercepting dump

For lighttpd add the following setting to lighttpd.conf and restart lighttpd

server.core-files = "enable"

The ulimit change only applies in the current shell. To make it permanent change it in /etc/security/limits.conf and restart lighttpd.

Jeffrey Ross
  • 141
  • 3