[systemd-devel] [PATCH 6/6] Add SHA1 hash sum to coredump report
Oleksii Shevchuk
alxchk at gmail.com
Mon Feb 11 15:14:43 PST 2013
If coredump goes to raw file, compute hash sum of it and store to
journal. Feature is optional, if libgrcypt enabled (HAVE_GCRYPT)
---
Makefile.am | 9 ++++++++
src/journal/coredump.c | 48 +++++++++++++++++++++++++++++++++++++++++++
src/journal/journald-server.h | 2 +-
3 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/Makefile.am b/Makefile.am
index af9e903..42cf7c6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2672,6 +2672,15 @@ systemd_coredump_LDADD += \
libsystemd-login-internal.la
endif
+if HAVE_GCRYPT
+systemd_coredump_CFLAGS = \
+ $(GCRYPT_CFLAGS) \
+ -Wno-pointer-arith
+
+systemd_coredump_LDADD += \
+ $(GCRYPT_LIBS)
+endif
+
rootlibexec_PROGRAMS += \
systemd-coredump
diff --git a/src/journal/coredump.c b/src/journal/coredump.c
index 680b26c..c9bda68 100644
--- a/src/journal/coredump.c
+++ b/src/journal/coredump.c
@@ -24,6 +24,10 @@
#include <stdio.h>
#include <sys/prctl.h>
+#if defined(HAVE_GCRYPT)
+#include <gcrypt.h>
+#endif
+
#include <systemd/sd-journal.h>
#ifdef HAVE_LOGIND
@@ -50,6 +54,13 @@ enum {
_ARG_MAX
};
+#if defined(HAVE_GCRYPT)
+static void gcry_md_closep(gcry_md_hd_t *p)
+{
+ gcry_md_close(*p);
+}
+#endif
+
static int submit_process_core(struct iovec iovec[15], int idx,
const char * comm,
const int journal)
@@ -61,6 +72,10 @@ static int submit_process_core(struct iovec iovec[15], int idx,
_cleanup_free_ char * corelink = NULL;
_cleanup_free_ char * t = NULL;
+#if defined(HAVE_GCRYPT)
+ _cleanup_free_ char * h = NULL;
+#endif
+
if (journal) {
mkdir_p_label("/var/lib/systemd/coredump", 0755);
corelink = strdup("/var/lib/systemd/coredump/core.systemd-journald");
@@ -152,6 +167,17 @@ journal:
log_debug("chmod %s: %s", corepath, strerror(errno));
}
+#if defined(HAVE_GCRYPT)
+ __attribute__((cleanup(gcry_md_closep)))
+ gcry_md_hd_t HD;
+ gcry_error_t gr = gcry_md_open (&HD, GCRY_MD_SHA1, 0);
+ if (gr != GPG_ERR_NO_ERROR) {
+ log_error("Failed to initialize gcrypt hash context: %s",
+ gpg_strerror(gr));
+ goto finish;
+ }
+#endif
+
for (;;) {
uint8_t buffer[4096];
size_t l, q;
@@ -171,6 +197,9 @@ journal:
log_error("Failed to write coredump: %m");
goto finish;
}
+#if defined(HAVE_GCRYPT)
+ gcry_md_write(HD, buffer, l);
+#endif
}
fflush(corefile);
@@ -190,6 +219,25 @@ journal:
t = strjoin("COREDUMP_FILE=coredump/", corelink, NULL);
if (t)
IOVEC_SET_STRING(iovec[idx ++], t);
+
+#if defined(HAVE_GCRYPT)
+ char * bh = hexstr(gcry_md_read(HD, GCRY_MD_SHA1), 20);
+
+ if (! bh) {
+ r = log_oom();
+ goto finish;
+ }
+
+ h = strjoin("COREDUMP_FILE_SHA1=", bh, NULL);
+ free(bh);
+
+ if (! h) {
+ r = log_oom();
+ goto finish;
+ }
+
+ IOVEC_SET_STRING(iovec[idx ++], h);
+#endif
}
r = sd_journal_sendv(iovec, idx);
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
index 9f50a29..3d05925 100644
--- a/src/journal/journald-server.h
+++ b/src/journal/journald-server.h
@@ -121,7 +121,7 @@ typedef struct Server {
struct udev *udev;
} Server;
-#define N_IOVEC_META_FIELDS 17
+#define N_IOVEC_META_FIELDS 18
#define N_IOVEC_KERNEL_FIELDS 64
#define N_IOVEC_UDEV_FIELDS 32
--
1.8.1.2
More information about the systemd-devel
mailing list