anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
65878a
From 035c9e559064114e3f7ba19b593a97c4a4d4f060 Mon Sep 17 00:00:00 2001
65878a
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
65878a
Date: Sat, 28 Dec 2013 19:33:23 -0500
65878a
Subject: [PATCH] journal: fix access to munmapped memory in
65878a
 sd_journal_enumerate_unique
65878a
65878a
sd_j_e_u needs to keep a reference to an object while comparing it
65878a
with possibly duplicate objects in other files. Because the size of
65878a
mmap cache is limited, with enough files and object to compare to,
65878a
at some point the object being compared would be munmapped, resulting
65878a
in a segmentation fault.
65878a
65878a
Fix this issue by turning keep_always into a reference count that can
65878a
be increased and decreased. Other callers which set keep_always=true
65878a
are unmodified: their references are never released but are ignored
65878a
when the whole file is closed, which happens at some point. keep_always
65878a
is increased in sd_j_e_u and later on released.
65878a
---
65878a
 src/journal/journal-file.c   |  5 +---
65878a
 src/journal/journal-file.h   | 24 +++++++++++++++++++
65878a
 src/journal/journal-verify.c |  4 ----
65878a
 src/journal/mmap-cache.c     | 57 +++++++++++++++++++++++++++++++++++---------
65878a
 src/journal/mmap-cache.h     | 18 +++++++++++++-
65878a
 src/journal/sd-journal.c     | 18 +++++++++++---
65878a
 6 files changed, 103 insertions(+), 23 deletions(-)
65878a
65878a
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
65878a
index 748816a..9dbd674 100644
65878a
--- a/src/journal/journal-file.c
65878a
+++ b/src/journal/journal-file.c
65878a
@@ -419,7 +419,6 @@ int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Objec
65878a
         void *t;
65878a
         Object *o;
65878a
         uint64_t s;
65878a
-        unsigned context;
65878a
 
65878a
         assert(f);
65878a
         assert(ret);
65878a
@@ -428,10 +427,8 @@ int journal_file_move_to_object(JournalFile *f, int type, uint64_t offset, Objec
65878a
         if (!VALID64(offset))
65878a
                 return -EFAULT;
65878a
 
65878a
-        /* One context for each type, plus one catch-all for the rest */
65878a
-        context = type > 0 && type < _OBJECT_TYPE_MAX ? type : 0;
65878a
 
65878a
-        r = journal_file_move_to(f, context, false, offset, sizeof(ObjectHeader), &t);
65878a
+        r = journal_file_move_to(f, type_to_context(type), false, offset, sizeof(ObjectHeader), &t);
65878a
         if (r < 0)
65878a
                 return r;
65878a
 
65878a
diff --git a/src/journal/journal-file.h b/src/journal/journal-file.h
65878a
index 5cc2c2d..376c3d4 100644
65878a
--- a/src/journal/journal-file.h
65878a
+++ b/src/journal/journal-file.h
65878a
@@ -128,6 +128,10 @@ int journal_file_open_reliably(
65878a
 #define ALIGN64(x) (((x) + 7ULL) & ~7ULL)
65878a
 #define VALID64(x) (((x) & 7ULL) == 0ULL)
65878a
 
65878a
+/* Use six characters to cover the offsets common in smallish journal
65878a
+ * files without adding too many zeros. */
65878a
+#define OFSfmt "%06"PRIx64
65878a
+
65878a
 static inline bool VALID_REALTIME(uint64_t u) {
65878a
         /* This considers timestamps until the year 3112 valid. That should be plenty room... */
65878a
         return u > 0 && u < (1ULL << 55);
65878a
@@ -197,3 +201,23 @@ int journal_file_get_cutoff_realtime_usec(JournalFile *f, usec_t *from, usec_t *
65878a
 int journal_file_get_cutoff_monotonic_usec(JournalFile *f, sd_id128_t boot, usec_t *from, usec_t *to);
65878a
 
65878a
 bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec);
65878a
+
65878a
+
65878a
+static unsigned type_to_context(int type) {
65878a
+        /* One context for each type, plus one catch-all for the rest */
65878a
+        return type > 0 && type < _OBJECT_TYPE_MAX ? type : 0;
65878a
+}
65878a
+
65878a
+static inline int journal_file_object_keep(JournalFile *f, Object *o, uint64_t offset) {
65878a
+        unsigned context = type_to_context(o->object.type);
65878a
+
65878a
+        return mmap_cache_get(f->mmap, f->fd, f->prot, context, true,
65878a
+                              offset, o->object.size, &f->last_stat, NULL);
65878a
+}
65878a
+
65878a
+static inline int journal_file_object_release(JournalFile *f, Object *o, uint64_t offset) {
65878a
+        unsigned context = type_to_context(o->object.type);
65878a
+
65878a
+        return mmap_cache_release(f->mmap, f->fd, f->prot, context,
65878a
+                                  offset, o->object.size);
65878a
+}
65878a
diff --git a/src/journal/journal-verify.c b/src/journal/journal-verify.c
65878a
index 82b0f0a..f2422ff 100644
65878a
--- a/src/journal/journal-verify.c
65878a
+++ b/src/journal/journal-verify.c
65878a
@@ -34,10 +34,6 @@
65878a
 #include "compress.h"
65878a
 #include "fsprg.h"
65878a
 
65878a
-/* Use six characters to cover the offsets common in smallish journal
65878a
- * files without adding to many zeros. */
65878a
-#define OFSfmt "%06"PRIx64
65878a
-
65878a
 static int journal_file_object_verify(JournalFile *f, uint64_t offset, Object *o) {
65878a
         uint64_t i;
65878a
 
65878a
diff --git a/src/journal/mmap-cache.c b/src/journal/mmap-cache.c
65878a
index 03b57be..cfb26da 100644
65878a
--- a/src/journal/mmap-cache.c
65878a
+++ b/src/journal/mmap-cache.c
65878a
@@ -38,7 +38,7 @@ typedef struct FileDescriptor FileDescriptor;
65878a
 struct Window {
65878a
         MMapCache *cache;
65878a
 
65878a
-        bool keep_always;
65878a
+        unsigned keep_always;
65878a
         bool in_unused;
65878a
 
65878a
         int prot;
65878a
@@ -182,7 +182,7 @@ static void context_detach_window(Context *c) {
65878a
         c->window = NULL;
65878a
         LIST_REMOVE(Context, by_window, w->contexts, c);
65878a
 
65878a
-        if (!w->contexts && !w->keep_always) {
65878a
+        if (!w->contexts && w->keep_always == 0) {
65878a
                 /* Not used anymore? */
65878a
                 LIST_PREPEND(Window, unused, c->cache->unused, w);
65878a
                 if (!c->cache->last_unused)
65878a
@@ -357,7 +357,6 @@ static int try_context(
65878a
         assert(m->n_ref > 0);
65878a
         assert(fd >= 0);
65878a
         assert(size > 0);
65878a
-        assert(ret);
65878a
 
65878a
         c = hashmap_get(m->contexts, UINT_TO_PTR(context+1));
65878a
         if (!c)
65878a
@@ -375,9 +374,10 @@ static int try_context(
65878a
                 return 0;
65878a
         }
65878a
 
65878a
-        c->window->keep_always = c->window->keep_always || keep_always;
65878a
+        c->window->keep_always += keep_always;
65878a
 
65878a
-        *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
65878a
+        if (ret)
65878a
+                *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
65878a
         return 1;
65878a
 }
65878a
 
65878a
@@ -399,7 +399,6 @@ static int find_mmap(
65878a
         assert(m->n_ref > 0);
65878a
         assert(fd >= 0);
65878a
         assert(size > 0);
65878a
-        assert(ret);
65878a
 
65878a
         f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
65878a
         if (!f)
65878a
@@ -419,9 +418,10 @@ static int find_mmap(
65878a
                 return -ENOMEM;
65878a
 
65878a
         context_attach_window(c, w);
65878a
-        w->keep_always = w->keep_always || keep_always;
65878a
+        w->keep_always += keep_always;
65878a
 
65878a
-        *ret = (uint8_t*) w->ptr + (offset - w->offset);
65878a
+        if (ret)
65878a
+                *ret = (uint8_t*) w->ptr + (offset - w->offset);
65878a
         return 1;
65878a
 }
65878a
 
65878a
@@ -447,7 +447,6 @@ static int add_mmap(
65878a
         assert(m->n_ref > 0);
65878a
         assert(fd >= 0);
65878a
         assert(size > 0);
65878a
-        assert(ret);
65878a
 
65878a
         woffset = offset & ~((uint64_t) page_size() - 1ULL);
65878a
         wsize = size + (offset - woffset);
65878a
@@ -517,7 +516,8 @@ static int add_mmap(
65878a
         c->window = w;
65878a
         LIST_PREPEND(Context, by_window, w->contexts, c);
65878a
 
65878a
-        *ret = (uint8_t*) w->ptr + (offset - w->offset);
65878a
+        if (ret)
65878a
+                *ret = (uint8_t*) w->ptr + (offset - w->offset);
65878a
         return 1;
65878a
 }
65878a
 
65878a
@@ -538,7 +538,6 @@ int mmap_cache_get(
65878a
         assert(m->n_ref > 0);
65878a
         assert(fd >= 0);
65878a
         assert(size > 0);
65878a
-        assert(ret);
65878a
 
65878a
         /* Check whether the current context is the right one already */
65878a
         r = try_context(m, fd, prot, context, keep_always, offset, size, ret);
65878a
@@ -554,6 +553,42 @@ int mmap_cache_get(
65878a
         return add_mmap(m, fd, prot, context, keep_always, offset, size, st, ret);
65878a
 }
65878a
 
65878a
+int mmap_cache_release(
65878a
+                MMapCache *m,
65878a
+                int fd,
65878a
+                int prot,
65878a
+                unsigned context,
65878a
+                uint64_t offset,
65878a
+                size_t size) {
65878a
+
65878a
+        FileDescriptor *f;
65878a
+        Window *w;
65878a
+
65878a
+        assert(m);
65878a
+        assert(m->n_ref > 0);
65878a
+        assert(fd >= 0);
65878a
+        assert(size > 0);
65878a
+
65878a
+        f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
65878a
+        if (!f)
65878a
+                return -EBADF;
65878a
+
65878a
+        assert(f->fd == fd);
65878a
+
65878a
+        LIST_FOREACH(by_fd, w, f->windows)
65878a
+                if (window_matches(w, fd, prot, offset, size))
65878a
+                        break;
65878a
+
65878a
+        if (!w)
65878a
+                return -ENOENT;
65878a
+
65878a
+        if (w->keep_always == 0)
65878a
+                return -ENOLCK;
65878a
+
65878a
+        w->keep_always -= 1;
65878a
+        return 0;
65878a
+}
65878a
+
65878a
 void mmap_cache_close_fd(MMapCache *m, int fd) {
65878a
         FileDescriptor *f;
65878a
 
65878a
diff --git a/src/journal/mmap-cache.h b/src/journal/mmap-cache.h
65878a
index 0c42fb8..e5e3b38 100644
65878a
--- a/src/journal/mmap-cache.h
65878a
+++ b/src/journal/mmap-cache.h
65878a
@@ -31,6 +31,22 @@ MMapCache* mmap_cache_new(void);
65878a
 MMapCache* mmap_cache_ref(MMapCache *m);
65878a
 MMapCache* mmap_cache_unref(MMapCache *m);
65878a
 
65878a
-int mmap_cache_get(MMapCache *m, int fd, int prot, unsigned context, bool keep_always, uint64_t offset, size_t size, struct stat *st, void **ret);
65878a
+int mmap_cache_get(
65878a
+        MMapCache *m,
65878a
+        int fd,
65878a
+        int prot,
65878a
+        unsigned context,
65878a
+        bool keep_always,
65878a
+        uint64_t offset,
65878a
+        size_t size,
65878a
+        struct stat *st,
65878a
+        void **ret);
65878a
+int mmap_cache_release(
65878a
+        MMapCache *m,
65878a
+        int fd,
65878a
+        int prot,
65878a
+        unsigned context,
65878a
+        uint64_t offset,
65878a
+        size_t size);
65878a
 void mmap_cache_close_fd(MMapCache *m, int fd);
65878a
 void mmap_cache_close_context(MMapCache *m, unsigned context);
65878a
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
65878a
index 9676f0f..67a77e6 100644
65878a
--- a/src/journal/sd-journal.c
65878a
+++ b/src/journal/sd-journal.c
65878a
@@ -2506,9 +2506,7 @@ _public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
65878a
 }
65878a
 
65878a
 _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
65878a
-        Object *o;
65878a
         size_t k;
65878a
-        int r;
65878a
 
65878a
         if (!j)
65878a
                 return -EINVAL;
65878a
@@ -2533,9 +2531,11 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
65878a
         for (;;) {
65878a
                 JournalFile *of;
65878a
                 Iterator i;
65878a
+                Object *o;
65878a
                 const void *odata;
65878a
                 size_t ol;
65878a
                 bool found;
65878a
+                int r;
65878a
 
65878a
                 /* Proceed to next data object in the field's linked list */
65878a
                 if (j->unique_offset == 0) {
65878a
@@ -2572,8 +2572,16 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
65878a
                         return r;
65878a
 
65878a
                 /* Let's do the type check by hand, since we used 0 context above. */
65878a
-                if (o->object.type != OBJECT_DATA)
65878a
+                if (o->object.type != OBJECT_DATA) {
65878a
+                        log_error("%s:offset " OFSfmt ": object has type %d, expected %d",
65878a
+                                  j->unique_file->path, j->unique_offset,
65878a
+                                  o->object.type, OBJECT_DATA);
65878a
                         return -EBADMSG;
65878a
+                }
65878a
+
65878a
+                r = journal_file_object_keep(j->unique_file, o, j->unique_offset);
65878a
+                if (r < 0)
65878a
+                        return r;
65878a
 
65878a
                 r = return_data(j, j->unique_file, o, &odata, &ol);
65878a
                 if (r < 0)
65878a
@@ -2607,6 +2615,10 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
65878a
                 if (found)
65878a
                         continue;
65878a
 
65878a
+                r = journal_file_object_release(j->unique_file, o, j->unique_offset);
65878a
+                if (r < 0)
65878a
+                        return r;
65878a
+
65878a
                 r = return_data(j, j->unique_file, o, data, l);
65878a
                 if (r < 0)
65878a
                         return r;