/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#ifdef HAVE_ELFUTILS
#include <dwarf.h>
#endif
#include "sd-journal.h"
#include "sd-login.h"
#include "sd-daemon.h"
#include "acl-util.h"
#include "alloc-util.h"
#include "capability-util.h"
#include "cgroup-util.h"
#include "compress.h"
#include "conf-parser.h"
#include "copy.h"
#include "coredump-vacuum.h"
#include "dirent-util.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "io-util.h"
#include "journald-native.h"
#include "log.h"
#include "macro.h"
#include "mkdir.h"
#include "parse-util.h"
#include "process-util.h"
#include "socket-util.h"
#include "special.h"
#include "stacktrace.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "user-util.h"
#include "util.h"
/* The maximum size up to which we process coredumps */
/* The maximum size up to which we leave the coredump around on disk */
/* The maximum size up to which we store the coredump in the journal */
/* Make sure to not make this larger than the maximum journal entry
* size. See DATA_SIZE_MAX in journald-native.c. */
enum {
/* We use this as array indexes for a couple of special fields we use for naming coredumping files, and
* attaching xattrs */
};
typedef enum CoredumpStorage {
[COREDUMP_STORAGE_NONE] = "none",
[COREDUMP_STORAGE_EXTERNAL] = "external",
[COREDUMP_STORAGE_JOURNAL] = "journal",
[COREDUMP_STORAGE_BOTH] = "both",
};
static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
static bool arg_compress = true;
static int parse_config(void) {
{}
};
"Coredump\0",
false, NULL);
}
#ifdef HAVE_ACL
int r;
if (uid <= SYSTEM_UID_MAX)
return 0;
/* Make sure normal users can read (but not write or delete)
* their own coredumps */
if (!acl)
return -errno;
}
r = calc_acl_mask_if_needed(&acl);
if (r < 0)
return log_warning_errno(r, "Failed to patch ACL: %m");
#endif
return 0;
}
[CONTEXT_PID] = "user.coredump.pid",
[CONTEXT_UID] = "user.coredump.uid",
[CONTEXT_GID] = "user.coredump.gid",
[CONTEXT_SIGNAL] = "user.coredump.signal",
[CONTEXT_TIMESTAMP] = "user.coredump.timestamp",
[CONTEXT_COMM] = "user.coredump.comm",
[CONTEXT_EXE] = "user.coredump.exe",
};
int r = 0;
unsigned i;
/* Attach some metadata to coredumps via extended
* attributes. Just because we can. */
for (i = 0; i < _CONTEXT_MAX; i++) {
int k;
continue;
if (k < 0 && r == 0)
r = -errno;
}
return r;
}
static int fix_permissions(
int fd,
const char *filename,
const char *target,
const char *context[_CONTEXT_MAX],
/* Ignore errors on these */
return 0;
}
/* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
return 0;
if (!filename)
return 1;
return 1;
}
int r;
if (!c)
return -ENOMEM;
if (!u)
return -ENOMEM;
r = sd_id128_get_boot(&boot);
if (r < 0)
return r;
if (!p)
return -ENOMEM;
if (!t)
return -ENOMEM;
c,
u,
p,
t) < 0)
return -ENOMEM;
return 0;
}
static int save_external_coredump(
const char *context[_CONTEXT_MAX],
int input_fd,
char **ret_filename,
int *ret_node_fd,
int *ret_data_fd,
int r;
if (r < 0)
return log_error_errno(r, "Failed to parse UID: %m");
if (r < 0)
if (rlimit <= 0) {
/* Is coredumping disabled? Then don't bother saving/processing the coredump */
log_info("Core Dumping has been disabled for process %s (%s).", context[CONTEXT_PID], context[CONTEXT_COMM]);
return -EBADSLT;
}
/* Never store more than the process configured, or than we actually shall keep or process */
if (r < 0)
return log_error_errno(r, "Failed to determine coredump file name: %m");
if (r < 0)
return log_error_errno(r, "Failed to determine temporary file name: %m");
if (fd < 0)
if (r == -EFBIG) {
log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]);
goto fail;
log_error("Not enough disk space for coredump of %s (%s), refusing.", context[CONTEXT_PID], context[CONTEXT_COMM]);
goto fail;
} else if (r < 0) {
log_error_errno(r, "Failed to dump coredump to file: %m");
goto fail;
}
goto fail;
}
goto fail;
}
/* If we will remove the coredump anyway, do not compress. */
&& arg_compress) {
if (!fn_compressed) {
log_oom();
goto uncompressed;
}
if (r < 0) {
goto uncompressed;
}
if (fd_compressed < 0) {
goto uncompressed;
}
if (r < 0) {
goto fail_compressed;
}
if (r < 0)
goto fail_compressed;
/* OK, this worked, we can get rid of the uncompressed version now */
return 0;
(void) unlink(tmp_compressed);
}
#endif
if (r < 0)
goto fail;
*ret_filename = fn;
*ret_data_fd = fd;
*ret_node_fd = -1;
fd = -1;
return 0;
fail:
return r;
}
ssize_t n;
if (!field) {
log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
return -ENOMEM;
}
if (n < 0)
return log_error_errno((int) n, "Failed to read core data: %m");
log_error("Core data too short.");
return -EIO;
}
return 0;
}
/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
* pos: 0
* flags: 0100002
*
* pos: 0
* flags: 0100002
*
* pos: 0
* flags: 0100002
* EOF
*/
int r = 0;
if (!proc_fd_dir)
return -errno;
if (proc_fdinfo_fd < 0)
return -errno;
if (!stream)
return -ENOMEM;
int fd;
if (r < 0)
return r;
fddelim = "\n";
/* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
if (fd < 0)
continue;
continue;
}
}
}
errno = 0;
if (errno > 0)
return -errno;
return 0;
}
int r;
if (r < 0)
return r;
if (uid <= SYSTEM_UID_MAX) {
if (r < 0) {
}
} else {
if (r < 0)
return r;
}
}
static int submit_coredump(
const char *context[_CONTEXT_MAX],
int input_fd) {
int r;
/* Vacuum before we write anything again */
/* Always stream the coredump to disk, if that's possible */
r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
if (r < 0)
/* Skip whole core dumping part */
goto log;
/* If we don't want to keep the coredump on disk, remove it now, as later on we will lack the privileges for
* it. However, we keep the fd to it, so that we can still process it and log it. */
if (r < 0)
return r;
if (r == 0) {
const char *coredump_filename;
}
/* Vacuum again, but exclude the coredump we just created */
(void) coredump_vacuum(coredump_node_fd >= 0 ? coredump_node_fd : coredump_fd, arg_keep_free, arg_max_use);
/* Now, let's drop privileges to become the user who owns the segfaulted process and allocate the coredump
* memory under the user's uid. This also ensures that the credentials journald will see are the ones of the
* coredumping user, thus making sure the user gets access to the core dump. Let's also get rid of all
* capabilities, if we run as root, we won't need them anymore. */
r = change_uid_gid(context);
if (r < 0)
return log_error_errno(r, "Failed to drop privileges: %m");
#ifdef HAVE_ELFUTILS
/* Try to get a strack trace if we can */
if (coredump_size <= arg_process_size_max) {
if (r >= 0)
core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.\n\n", stacktrace, NULL);
else if (r == -EINVAL)
else
log_warning_errno(r, "Failed to generate stack trace: %m");
}
if (!core_message)
#endif
log:
core_message = strjoin("MESSAGE=Process ", context[CONTEXT_PID], " (", context[CONTEXT_COMM], ") of user ", context[CONTEXT_UID], " dumped core.", NULL);
if (core_message)
/* Optionally store the entire coredump in the journal */
/* Store the coredump itself in the journal */
if (r >= 0) {
n_iovec++;
}
}
if (r < 0)
return log_error_errno(r, "Failed to log coredump: %m");
return 0;
}
[CONTEXT_PID] = "COREDUMP_PID=",
[CONTEXT_UID] = "COREDUMP_UID=",
[CONTEXT_GID] = "COREDUMP_GID=",
[CONTEXT_SIGNAL] = "COREDUMP_SIGNAL=",
[CONTEXT_TIMESTAMP] = "COREDUMP_TIMESTAMP=",
[CONTEXT_COMM] = "COREDUMP_COMM=",
[CONTEXT_EXE] = "COREDUMP_EXE=",
[CONTEXT_RLIMIT] = "COREDUMP_RLIMIT=",
};
unsigned i;
for (i = 0; i < _CONTEXT_MAX; i++) {
size_t l;
l = strlen(context_field_names[i]);
continue;
continue;
/* Note that these strings are NUL terminated, because we made sure that a trailing NUL byte is in the
* buffer, though not included in the iov_len count. (see below) */
break;
}
}
int r;
log_open();
for (;;) {
union {
} control = {};
.msg_control = &control,
.msg_controllen = sizeof(control),
.msg_iovlen = 1,
};
ssize_t n;
int l;
r = log_oom();
goto finish;
}
goto finish;
}
assert(l >= 0);
r = log_oom();
goto finish;
}
if (n < 0) {
goto finish;
}
if (n == 0) {
/* The final zero-length datagram carries the file descriptor and tells us that we're done. */
}
}
if (!found) {
log_error("Coredump file descriptor missing.");
r = -EBADMSG;
goto finish;
}
assert(coredump_fd < 0);
break;
}
/* Add trailing NUL byte, in case these are strings */
cmsg_close_all(&mh);
n_iovec++;
}
r = log_oom();
goto finish;
}
/* Make sure we we got all data we really need */
assert(coredump_fd >= 0);
for (i = 0; i < n_iovec; i++)
return r;
}
};
size_t i;
int r;
if (fd < 0)
for (i = 0; i < n_iovec; i++) {
ssize_t n;
if (n < 0)
}
if (r < 0)
return log_error_errno(r, "Failed to send coredump fd: %m");
return 0;
}
int r;
/* If we are journald, we cut things short, don't write to the journal, but still create a coredump. */
if (arg_storage != COREDUMP_STORAGE_NONE)
r = save_external_coredump(context, input_fd, &filename, &coredump_node_fd, &coredump_fd, &coredump_size);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
/* The small core field we allocate on the stack, to keep things simple */
char
/* The larger ones we allocate on the heap */
_cleanup_free_ char
const char *p;
char *t;
int r;
log_error("Not enough arguments passed from kernel (%i, expected %i).", argc - 1, CONTEXT_COMM + 1 - 1);
return -EINVAL;
}
if (r < 0)
return log_error_errno(r, "Failed to parse PID.");
if (r < 0) {
log_warning_errno(r, "Failed to get COMM, falling back to the command line: %m");
if (!comm)
return log_oom();
}
if (r < 0)
log_warning_errno(r, "Failed to get EXE, ignoring: %m");
if (cg_pid_get_unit(pid, &t) >= 0) {
if (streq(t, SPECIAL_JOURNALD_SERVICE)) {
free(t);
}
free(t);
}
/* OK, now we know it's not the journal, hence we can make use of it now. */
log_open();
if (cg_pid_get_user_unit(pid, &t) >= 0) {
free(t);
}
if (sd_pid_get_session(pid, &t) >= 0) {
free(t);
}
if (r > 0)
}
if (sd_pid_get_slice(pid, &t) >= 0) {
free(t);
}
if (comm) {
}
if (exe) {
}
if (get_process_cmdline(pid, 0, false, &t) >= 0) {
free(t);
}
free(t);
}
if (compose_open_fds(pid, &t) >= 0) {
free(t);
if (core_open_fds)
}
if (read_full_file(p, &t, NULL) >= 0) {
free(t);
if (core_proc_status)
}
if (read_full_file(p, &t, NULL) >= 0) {
free(t);
if (core_proc_maps)
}
if (read_full_file(p, &t, NULL) >= 0) {
free(t);
if (core_proc_limits)
}
if (read_full_file(p, &t, NULL) >=0) {
free(t);
if (core_proc_cgroup)
}
if (get_process_cwd(pid, &t) >= 0) {
free(t);
}
if (get_process_root(pid, &t) >= 0) {
free(t);
}
if (get_process_environ(pid, &t) >= 0) {
free(t);
if (core_environ)
}
}
int r;
/* First, log to a safe place, since we don't know what crashed and it might be journald which we'd rather not
* log to then. */
log_open();
/* Make sure we never enter a loop */
(void) prctl(PR_SET_DUMPABLE, 0);
/* Ignore all parse errors */
(void) parse_config();
r = sd_listen_fds(false);
if (r < 0) {
log_error_errno(r, "Failed to determine number of file descriptor: %m");
goto finish;
}
/* If we got an fd passed, we are running in coredumpd mode. Otherwise we are invoked from the kernel as
* coredump handler */
if (r == 0)
else if (r == 1)
else {
log_error("Received unexpected number of file descriptors.");
r = -EINVAL;
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}