+DEFINE_PRIVATE_STRING_TABLE_LOOKUP(coredump_storage, CoredumpStorage);
+static DEFINE_CONFIG_PARSE_ENUM(config_parse_coredump_storage, coredump_storage, CoredumpStorage, "Failed to parse storage setting");
+
+static CoredumpStorage arg_storage = COREDUMP_STORAGE_EXTERNAL;
+static bool arg_compress = true;
+static off_t arg_process_size_max = PROCESS_SIZE_MAX;
+static off_t arg_external_size_max = EXTERNAL_SIZE_MAX;
+static size_t arg_journal_size_max = JOURNAL_SIZE_MAX;
+static off_t arg_keep_free = (off_t) -1;
+static off_t arg_max_use = (off_t) -1;
+
+static int parse_config(void) {
+ static const ConfigTableItem items[] = {
+ { "Coredump", "Storage", config_parse_coredump_storage, 0, &arg_storage },
+ { "Coredump", "Compress", config_parse_bool, 0, &arg_compress },
+ { "Coredump", "ProcessSizeMax", config_parse_iec_off, 0, &arg_process_size_max },
+ { "Coredump", "ExternalSizeMax", config_parse_iec_off, 0, &arg_external_size_max },
+ { "Coredump", "JournalSizeMax", config_parse_iec_size, 0, &arg_journal_size_max },
+ { "Coredump", "KeepFree", config_parse_iec_off, 0, &arg_keep_free },
+ { "Coredump", "MaxUse", config_parse_iec_off, 0, &arg_max_use },
+ {}
+ };
+
+ return config_parse(NULL, "/etc/systemd/coredump.conf", NULL,
+ "Coredump\0",
+ config_item_table_lookup, items,
+ false, false, true, NULL);
+}
+
+static int fix_acl(int fd, uid_t uid) {
+
+#ifdef HAVE_ACL
+ _cleanup_(acl_freep) acl_t acl = NULL;
+ acl_entry_t entry;
+ acl_permset_t permset;
+
+ assert(fd >= 0);
+
+ if (uid <= SYSTEM_UID_MAX)
+ return 0;
+
+ /* Make sure normal users can read (but not write or delete)
+ * their own coredumps */
+
+ acl = acl_get_fd(fd);
+ if (!acl) {
+ log_error("Failed to get ACL: %m");
+ return -errno;
+ }
+
+ if (acl_create_entry(&acl, &entry) < 0 ||
+ acl_set_tag_type(entry, ACL_USER) < 0 ||
+ acl_set_qualifier(entry, &uid) < 0) {
+ log_error("Failed to patch ACL: %m");
+ return -errno;
+ }
+
+ if (acl_get_permset(entry, &permset) < 0 ||
+ acl_add_perm(permset, ACL_READ) < 0 ||
+ calc_acl_mask_if_needed(&acl) < 0) {
+ log_warning("Failed to patch ACL: %m");
+ return -errno;
+ }
+
+ if (acl_set_fd(fd, acl) < 0) {
+ log_error("Failed to apply ACL: %m");
+ return -errno;
+ }
+#endif
+
+ return 0;
+}
+
+static int fix_xattr(int fd, const char *info[_INFO_LEN]) {
+
+ static const char * const xattrs[_INFO_LEN] = {
+ [INFO_PID] = "user.coredump.pid",
+ [INFO_UID] = "user.coredump.uid",
+ [INFO_GID] = "user.coredump.gid",
+ [INFO_SIGNAL] = "user.coredump.signal",
+ [INFO_TIMESTAMP] = "user.coredump.timestamp",
+ [INFO_COMM] = "user.coredump.comm",
+ [INFO_EXE] = "user.coredump.exe",
+ };
+
+ int r = 0;
+ unsigned i;
+
+ assert(fd >= 0);
+
+ /* Attach some metadata to coredumps via extended
+ * attributes. Just because we can. */
+
+ for (i = 0; i < _INFO_LEN; i++) {
+ int k;
+
+ if (isempty(info[i]) || !xattrs[i])
+ continue;
+
+ k = fsetxattr(fd, xattrs[i], info[i], strlen(info[i]), XATTR_CREATE);
+ if (k < 0 && r == 0)
+ r = -errno;
+ }
+
+ return r;
+}
+
+#define filename_escape(s) xescape((s), "./ ")
+
+static int fix_permissions(
+ int fd,
+ const char *filename,
+ const char *target,
+ const char *info[_INFO_LEN],
+ uid_t uid) {
+
+ assert(fd >= 0);
+ assert(filename);
+ assert(target);
+ assert(info);
+
+ /* Ignore errors on these */
+ fchmod(fd, 0640);
+ fix_acl(fd, uid);
+ fix_xattr(fd, info);
+
+ if (fsync(fd) < 0) {
+ log_error("Failed to sync coredump %s: %m", filename);
+ return -errno;
+ }
+
+ if (rename(filename, target) < 0) {
+ log_error("Failed to rename coredump %s -> %s: %m", filename, target);
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int maybe_remove_external_coredump(const char *filename, off_t size) {
+
+ /* Returns 1 if might remove, 0 if will not remove, < 0 on error. */
+
+ if (IN_SET(arg_storage, COREDUMP_STORAGE_EXTERNAL, COREDUMP_STORAGE_BOTH) &&
+ size <= arg_external_size_max)
+ return 0;
+
+ if (!filename)
+ return 1;
+
+ if (unlink(filename) < 0 && errno != ENOENT) {
+ log_error("Failed to unlink %s: %m", filename);
+ return -errno;
+ }
+
+ return 1;
+}
+
+static int make_filename(const char *info[_INFO_LEN], char **ret) {
+ _cleanup_free_ char *c = NULL, *u = NULL, *p = NULL, *t = NULL;
+ sd_id128_t boot;
+ int r;
+
+ assert(info);
+
+ c = filename_escape(info[INFO_COMM]);
+ if (!c)
+ return -ENOMEM;
+
+ u = filename_escape(info[INFO_UID]);
+ if (!u)
+ return -ENOMEM;
+
+ r = sd_id128_get_boot(&boot);
+ if (r < 0)
+ return r;
+
+ p = filename_escape(info[INFO_PID]);
+ if (!p)
+ return -ENOMEM;
+
+ t = filename_escape(info[INFO_TIMESTAMP]);
+ if (!t)
+ return -ENOMEM;
+
+ if (asprintf(ret,
+ "/var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR ".%s.%s000000",
+ c,
+ u,
+ SD_ID128_FORMAT_VAL(boot),
+ p,
+ t) < 0)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static int save_external_coredump(
+ const char *info[_INFO_LEN],
+ uid_t uid,
+ char **ret_filename,
+ int *ret_fd,
+ off_t *ret_size) {
+
+ _cleanup_free_ char *fn = NULL, *tmp = NULL;
+ _cleanup_close_ int fd = -1;
+ struct stat st;
+ int r;
+
+ assert(info);
+ assert(ret_filename);
+ assert(ret_fd);
+ assert(ret_size);
+
+ r = make_filename(info, &fn);
+ if (r < 0) {
+ log_error("Failed to determine coredump file name: %s", strerror(-r));
+ return r;
+ }
+
+ tmp = tempfn_random(fn);
+ if (!tmp)
+ return log_oom();
+
+ mkdir_p_label("/var/lib/systemd/coredump", 0755);
+
+ fd = open(tmp, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
+ if (fd < 0) {
+ log_error("Failed to create coredump file %s: %m", tmp);
+ return -errno;
+ }
+
+ r = copy_bytes(STDIN_FILENO, fd, arg_process_size_max);
+ if (r == -EFBIG) {
+ log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", info[INFO_PID], info[INFO_COMM]);
+ goto fail;
+ } else if (IN_SET(r, -EDQUOT, -ENOSPC)) {
+ log_error("Not enough disk space for coredump of %s (%s), refusing.", info[INFO_PID], info[INFO_COMM]);
+ goto fail;
+ } else if (r < 0) {
+ log_error("Failed to dump coredump to file: %s", strerror(-r));
+ goto fail;
+ }
+
+ if (fstat(fd, &st) < 0) {
+ log_error("Failed to fstat coredump %s: %m", tmp);
+ goto fail;
+ }
+
+ if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
+ log_error("Failed to seek on %s: %m", tmp);
+ goto fail;
+ }
+
+#if defined(HAVE_XZ) || defined(HAVE_LZ4)
+ /* If we will remove the coredump anyway, do not compress. */
+ if (maybe_remove_external_coredump(NULL, st.st_size) == 0
+ && arg_compress) {
+
+ _cleanup_free_ char *fn_compressed = NULL, *tmp_compressed = NULL;
+ _cleanup_close_ int fd_compressed = -1;
+
+ fn_compressed = strappend(fn, COMPRESSED_EXT);
+ if (!fn_compressed) {
+ log_oom();
+ goto uncompressed;
+ }
+
+ tmp_compressed = tempfn_random(fn_compressed);
+ if (!tmp_compressed) {
+ log_oom();
+ goto uncompressed;
+ }
+
+ fd_compressed = open(tmp_compressed, O_CREAT|O_EXCL|O_RDWR|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0640);
+ if (fd_compressed < 0) {
+ log_error("Failed to create file %s: %m", tmp_compressed);
+ goto uncompressed;
+ }
+
+ r = compress_stream(fd, fd_compressed, -1);
+ if (r < 0) {
+ log_error("Failed to compress %s: %s", tmp_compressed, strerror(-r));
+ goto fail_compressed;
+ }
+
+ r = fix_permissions(fd_compressed, tmp_compressed, fn_compressed, info, uid);
+ if (r < 0)
+ goto fail_compressed;
+
+ /* OK, this worked, we can get rid of the uncompressed version now */
+ unlink_noerrno(tmp);
+
+ *ret_filename = fn_compressed; /* compressed */
+ *ret_fd = fd; /* uncompressed */
+ *ret_size = st.st_size; /* uncompressed */
+
+ fn_compressed = NULL;
+ fd = -1;
+
+ return 0;
+
+ fail_compressed:
+ unlink_noerrno(tmp_compressed);
+ }
+
+uncompressed:
+#endif
+ r = fix_permissions(fd, tmp, fn, info, uid);
+ if (r < 0)
+ goto fail;
+
+ *ret_filename = fn;
+ *ret_fd = fd;
+ *ret_size = st.st_size;
+
+ fn = NULL;
+ fd = -1;
+
+ return 0;
+
+fail:
+ unlink_noerrno(tmp);
+ return r;
+}
+
+static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_size) {
+ _cleanup_free_ char *field = NULL;
+ ssize_t n;
+
+ assert(fd >= 0);
+ assert(ret);
+ assert(ret_size);
+
+ if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
+ log_warning("Failed to seek: %m");
+ return -errno;
+ }
+
+ field = malloc(9 + size);
+ if (!field) {
+ log_warning("Failed to allocate memory for coredump, coredump will not be stored.");
+ return -ENOMEM;
+ }
+
+ memcpy(field, "COREDUMP=", 9);
+
+ n = read(fd, field + 9, size);
+ if (n < 0) {
+ log_error("Failed to read core data: %s", strerror(-n));
+ return (int) n;
+ }
+ if ((size_t) n < size) {
+ log_error("Core data too short.");
+ return -EIO;
+ }
+
+ *ret = field;
+ *ret_size = size + 9;
+
+ field = NULL;
+
+ return 0;
+}
+
+/* Joins /proc/[pid]/fd/ and /proc/[pid]/fdinfo/ into the following lines:
+ * 0:/dev/pts/23
+ * pos: 0
+ * flags: 0100002
+ *
+ * 1:/dev/pts/23
+ * pos: 0
+ * flags: 0100002
+ *
+ * 2:/dev/pts/23
+ * pos: 0
+ * flags: 0100002
+ * EOF
+ */
+static int compose_open_fds(pid_t pid, char **open_fds) {
+ _cleanup_fclose_ FILE *stream = NULL;
+ char path[PATH_MAX], line[LINE_MAX];
+ size_t ignored_size;
+ const char *fddelim = "";
+ struct dirent *dent = NULL;
+ _cleanup_closedir_ DIR *proc_fd_dir = NULL;
+ int r = 0;
+
+ assert(pid >= 0);
+ assert(open_fds != NULL);
+
+ sprintf(path, "/proc/"PID_FMT"/fd", pid);
+ proc_fd_dir = opendir(path);
+
+ if (proc_fd_dir == NULL)
+ return -ENOENT;
+
+ stream = open_memstream(open_fds, &ignored_size);
+ if (!stream)
+ return -ENOMEM;
+
+ for (dent = readdir(proc_fd_dir); dent != NULL; dent = readdir(proc_fd_dir)) {
+ _cleanup_free_ char *fdname = NULL;
+ _cleanup_fclose_ FILE *fdinfo = NULL;
+
+ if (dent->d_name[0] == '.' || strcmp(dent->d_name, "..") == 0)
+ continue;
+
+ /* Too long path is unlikely a path to valid file descriptor in /proc/[pid]/fd */
+ /* Skip it. */
+ r = snprintf(path, sizeof(path), "/proc/"PID_FMT"/fd/%s", pid, dent->d_name);
+ if (r >= (int)sizeof(path))
+ continue;
+
+ r = readlink_malloc(path, &fdname);
+ if (r < 0)
+ return r;
+
+ fprintf(stream, "%s%s:%s\n", fddelim, dent->d_name, fdname);
+ fddelim = "\n";
+
+ /* Use the directory entry from /proc/[pid]/fd with /proc/[pid]/fdinfo */
+
+ /* Too long path is unlikely a path to valid file descriptor info in /proc/[pid]/fdinfo */
+ /* Skip it. */
+ r = snprintf(path, sizeof(path), "/proc/"PID_FMT"/fdinfo/%s", pid, dent->d_name);
+ if (r >= (int)sizeof(path))
+ continue;
+
+ fdinfo = fopen(path, "re");
+ if (fdinfo == NULL)
+ continue;
+
+ while(fgets(line, sizeof(line), fdinfo) != NULL)
+ fprintf(stream, "%s%s",
+ line, strchr(line, '\n') == NULL ? "\n" : "");
+ }
+
+ return 0;
+}
+