chiark / gitweb /
sd-journal: fix sd_journal_enumerate_unique skipping values
authorJan Janssen <medhefgo@web.de>
Sat, 6 Sep 2014 08:36:34 +0000 (10:36 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 10 Oct 2014 03:16:36 +0000 (23:16 -0400)
sd_journal_enumerate_unique will lock its mmap window to prevent it
from being released by calling mmap_cache_get with keep_always=true.
This call may return windows that are wider, but compatible with the
parameters provided to it.

This can result in a mismatch where the window to be released cannot
properly be selected, because we have more than one window matching the
parameters of mmap_cache_release. Therefore, introduce a release_cookie
to be used when releasing the window.

https://bugs.freedesktop.org/show_bug.cgi?id=79380

src/journal/journal-file.c
src/journal/journal-file.h
src/journal/journal-verify.c
src/journal/mmap-cache.c
src/journal/mmap-cache.h
src/journal/sd-journal.c
src/journal/test-mmap-cache.c

index f25cda6ddca76c49f582cc81272d8fe3f3f28720..038b437e1ff25642d357c81c5c86cabb02c549f9 100644 (file)
@@ -391,7 +391,7 @@ static int journal_file_move_to(JournalFile *f, int context, bool keep_always, u
                         return -EADDRNOTAVAIL;
         }
 
                         return -EADDRNOTAVAIL;
         }
 
-        return mmap_cache_get(f->mmap, f->fd, f->prot, context, keep_always, offset, size, &f->last_stat, ret);
+        return mmap_cache_get(f->mmap, f->fd, f->prot, context, keep_always, offset, size, &f->last_stat, ret, NULL);
 }
 
 static uint64_t minimum_header_size(Object *o) {
 }
 
 static uint64_t minimum_header_size(Object *o) {
index 6b4bf0d5aed265b3f55822a3b7031e4744351aab..fa5b943e46a2f6393ba1fae0ca86449db413d312 100644 (file)
@@ -212,17 +212,14 @@ static unsigned type_to_context(int type) {
         return type > 0 && type < _OBJECT_TYPE_MAX ? type : 0;
 }
 
         return type > 0 && type < _OBJECT_TYPE_MAX ? type : 0;
 }
 
-static inline int journal_file_object_keep(JournalFile *f, Object *o, uint64_t offset) {
+static inline int journal_file_object_keep(JournalFile *f, Object *o, uint64_t offset, void **release_cookie) {
         unsigned context = type_to_context(o->object.type);
         uint64_t s = le64toh(o->object.size);
 
         return mmap_cache_get(f->mmap, f->fd, f->prot, context, true,
         unsigned context = type_to_context(o->object.type);
         uint64_t s = le64toh(o->object.size);
 
         return mmap_cache_get(f->mmap, f->fd, f->prot, context, true,
-                              offset, s, &f->last_stat, NULL);
+                              offset, s, &f->last_stat, NULL, release_cookie);
 }
 
 }
 
-static inline int journal_file_object_release(JournalFile *f, Object *o, uint64_t offset) {
-        unsigned context = type_to_context(o->object.type);
-        uint64_t s = le64toh(o->object.size);
-
-        return mmap_cache_release(f->mmap, f->fd, f->prot, context, offset, s);
+static inline int journal_file_object_release(JournalFile *f, void *release_cookie) {
+        return mmap_cache_release(f->mmap, f->fd, release_cookie);
 }
 }
index b4e8f73c412e4c5d34bb264ee0afc5d97e17e5a4..f74adcbc8938955e074f7bd6029ceeaa5caf1c70 100644 (file)
@@ -368,7 +368,7 @@ static int contains_uint64(MMapCache *m, int fd, uint64_t n, uint64_t p) {
 
                 c = (a + b) / 2;
 
 
                 c = (a + b) / 2;
 
-                r = mmap_cache_get(m, fd, PROT_READ|PROT_WRITE, 0, false, c * sizeof(uint64_t), sizeof(uint64_t), NULL, (void **) &z);
+                r = mmap_cache_get(m, fd, PROT_READ|PROT_WRITE, 0, false, c * sizeof(uint64_t), sizeof(uint64_t), NULL, (void **) &z, NULL);
                 if (r < 0)
                         return r;
 
                 if (r < 0)
                         return r;
 
index 2d268fc3329156a445856f8f31855469a828b07b..b7db6f1da5c2c7ffbc10c00215e783891304fb24 100644 (file)
@@ -352,7 +352,8 @@ static int try_context(
                 bool keep_always,
                 uint64_t offset,
                 size_t size,
                 bool keep_always,
                 uint64_t offset,
                 size_t size,
-                void **ret) {
+                void **ret,
+                void **release_cookie) {
 
         Context *c;
 
 
         Context *c;
 
@@ -381,6 +382,8 @@ static int try_context(
 
         if (ret)
                 *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
 
         if (ret)
                 *ret = (uint8_t*) c->window->ptr + (offset - c->window->offset);
+        if (keep_always && release_cookie)
+                *release_cookie = c->window;
         return 1;
 }
 
         return 1;
 }
 
@@ -392,7 +395,8 @@ static int find_mmap(
                 bool keep_always,
                 uint64_t offset,
                 size_t size,
                 bool keep_always,
                 uint64_t offset,
                 size_t size,
-                void **ret) {
+                void **ret,
+                void **release_cookie) {
 
         FileDescriptor *f;
         Window *w;
 
         FileDescriptor *f;
         Window *w;
@@ -425,6 +429,8 @@ static int find_mmap(
 
         if (ret)
                 *ret = (uint8_t*) w->ptr + (offset - w->offset);
 
         if (ret)
                 *ret = (uint8_t*) w->ptr + (offset - w->offset);
+        if (keep_always && release_cookie)
+                *release_cookie = c->window;
         return 1;
 }
 
         return 1;
 }
 
@@ -437,7 +443,8 @@ static int add_mmap(
                 uint64_t offset,
                 size_t size,
                 struct stat *st,
                 uint64_t offset,
                 size_t size,
                 struct stat *st,
-                void **ret) {
+                void **ret,
+                void **release_cookie) {
 
         uint64_t woffset, wsize;
         Context *c;
 
         uint64_t woffset, wsize;
         Context *c;
@@ -521,6 +528,8 @@ static int add_mmap(
 
         if (ret)
                 *ret = (uint8_t*) w->ptr + (offset - w->offset);
 
         if (ret)
                 *ret = (uint8_t*) w->ptr + (offset - w->offset);
+        if (keep_always && release_cookie)
+                *release_cookie = c->window;
         return 1;
 
 outofmem:
         return 1;
 
 outofmem:
@@ -537,7 +546,8 @@ int mmap_cache_get(
                 uint64_t offset,
                 size_t size,
                 struct stat *st,
                 uint64_t offset,
                 size_t size,
                 struct stat *st,
-                void **ret) {
+                void **ret,
+                void **release_cookie) {
 
         int r;
 
 
         int r;
 
@@ -547,14 +557,14 @@ int mmap_cache_get(
         assert(size > 0);
 
         /* Check whether the current context is the right one already */
         assert(size > 0);
 
         /* Check whether the current context is the right one already */
-        r = try_context(m, fd, prot, context, keep_always, offset, size, ret);
+        r = try_context(m, fd, prot, context, keep_always, offset, size, ret, release_cookie);
         if (r != 0) {
                 m->n_hit ++;
                 return r;
         }
 
         /* Search for a matching mmap */
         if (r != 0) {
                 m->n_hit ++;
                 return r;
         }
 
         /* Search for a matching mmap */
-        r = find_mmap(m, fd, prot, context, keep_always, offset, size, ret);
+        r = find_mmap(m, fd, prot, context, keep_always, offset, size, ret, release_cookie);
         if (r != 0) {
                 m->n_hit ++;
                 return r;
         if (r != 0) {
                 m->n_hit ++;
                 return r;
@@ -563,16 +573,13 @@ int mmap_cache_get(
         m->n_missed++;
 
         /* Create a new mmap */
         m->n_missed++;
 
         /* Create a new mmap */
-        return add_mmap(m, fd, prot, context, keep_always, offset, size, st, ret);
+        return add_mmap(m, fd, prot, context, keep_always, offset, size, st, ret, release_cookie);
 }
 
 int mmap_cache_release(
                 MMapCache *m,
                 int fd,
 }
 
 int mmap_cache_release(
                 MMapCache *m,
                 int fd,
-                int prot,
-                unsigned context,
-                uint64_t offset,
-                size_t size) {
+                void *release_cookie) {
 
         FileDescriptor *f;
         Window *w;
 
         FileDescriptor *f;
         Window *w;
@@ -580,7 +587,6 @@ int mmap_cache_release(
         assert(m);
         assert(m->n_ref > 0);
         assert(fd >= 0);
         assert(m);
         assert(m->n_ref > 0);
         assert(fd >= 0);
-        assert(size > 0);
 
         f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
         if (!f)
 
         f = hashmap_get(m->fds, INT_TO_PTR(fd + 1));
         if (!f)
@@ -589,7 +595,7 @@ int mmap_cache_release(
         assert(f->fd == fd);
 
         LIST_FOREACH(by_fd, w, f->windows)
         assert(f->fd == fd);
 
         LIST_FOREACH(by_fd, w, f->windows)
-                if (window_matches(w, fd, prot, offset, size))
+                if (w == release_cookie)
                         break;
 
         if (!w)
                         break;
 
         if (!w)
index 647555a73e5e50767e3d543b38d4dd08b9bc9c6a..76e5316248d71baa9c8496cbecd14e5370be6e69 100644 (file)
@@ -40,14 +40,12 @@ int mmap_cache_get(
         uint64_t offset,
         size_t size,
         struct stat *st,
         uint64_t offset,
         size_t size,
         struct stat *st,
-        void **ret);
+        void **ret,
+        void **release_cookie);
 int mmap_cache_release(
         MMapCache *m,
         int fd,
 int mmap_cache_release(
         MMapCache *m,
         int fd,
-        int prot,
-        unsigned context,
-        uint64_t offset,
-        size_t size);
+        void *release_cookie);
 void mmap_cache_close_fd(MMapCache *m, int fd);
 void mmap_cache_close_context(MMapCache *m, unsigned context);
 
 void mmap_cache_close_fd(MMapCache *m, int fd);
 void mmap_cache_close_context(MMapCache *m, unsigned context);
 
index b72a0867e78c7d5a6a2f089ef99b2c2f04089af8..479444c8dff94b8a4c22234a120d347cb5f3842f 100644 (file)
@@ -2528,6 +2528,7 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
                 size_t ol;
                 bool found;
                 int r;
                 size_t ol;
                 bool found;
                 int r;
+                void *release_cookie;
 
                 /* Proceed to next data object in the field's linked list */
                 if (j->unique_offset == 0) {
 
                 /* Proceed to next data object in the field's linked list */
                 if (j->unique_offset == 0) {
@@ -2568,7 +2569,7 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
                         return -EBADMSG;
                 }
 
                         return -EBADMSG;
                 }
 
-                r = journal_file_object_keep(j->unique_file, o, j->unique_offset);
+                r = journal_file_object_keep(j->unique_file, o, j->unique_offset, &release_cookie);
                 if (r < 0)
                         return r;
 
                 if (r < 0)
                         return r;
 
@@ -2616,13 +2617,13 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
                                 found = true;
                 }
 
                                 found = true;
                 }
 
-                if (found)
-                        continue;
-
-                r = journal_file_object_release(j->unique_file, o, j->unique_offset);
+                r = journal_file_object_release(j->unique_file, release_cookie);
                 if (r < 0)
                         return r;
 
                 if (r < 0)
                         return r;
 
+                if (found)
+                        continue;
+
                 r = return_data(j, j->unique_file, o, data, l);
                 if (r < 0)
                         return r;
                 r = return_data(j, j->unique_file, o, data, l);
                 if (r < 0)
                         return r;
index b7bb260fcfdce9516c4ce010b763861c240d5e34..778e884c3ff032927498c4b215221b75e3538500 100644 (file)
@@ -49,23 +49,23 @@ int main(int argc, char *argv[]) {
         assert(z >= 0);
         unlink(pz);
 
         assert(z >= 0);
         unlink(pz);
 
-        r = mmap_cache_get(m, x, PROT_READ, 0, false, 1, 2, NULL, &p);
+        r = mmap_cache_get(m, x, PROT_READ, 0, false, 1, 2, NULL, &p, NULL);
         assert(r >= 0);
 
         assert(r >= 0);
 
-        r = mmap_cache_get(m, x, PROT_READ, 0, false, 2, 2, NULL, &q);
+        r = mmap_cache_get(m, x, PROT_READ, 0, false, 2, 2, NULL, &q, NULL);
         assert(r >= 0);
 
         assert((uint8_t*) p + 1 == (uint8_t*) q);
 
         assert(r >= 0);
 
         assert((uint8_t*) p + 1 == (uint8_t*) q);
 
-        r = mmap_cache_get(m, x, PROT_READ, 1, false, 3, 2, NULL, &q);
+        r = mmap_cache_get(m, x, PROT_READ, 1, false, 3, 2, NULL, &q, NULL);
         assert(r >= 0);
 
         assert((uint8_t*) p + 2 == (uint8_t*) q);
 
         assert(r >= 0);
 
         assert((uint8_t*) p + 2 == (uint8_t*) q);
 
-        r = mmap_cache_get(m, x, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p);
+        r = mmap_cache_get(m, x, PROT_READ, 0, false, 16ULL*1024ULL*1024ULL, 2, NULL, &p, NULL);
         assert(r >= 0);
 
         assert(r >= 0);
 
-        r = mmap_cache_get(m, x, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q);
+        r = mmap_cache_get(m, x, PROT_READ, 1, false, 16ULL*1024ULL*1024ULL+1, 2, NULL, &q, NULL);
         assert(r >= 0);
 
         assert((uint8_t*) p + 1 == (uint8_t*) q);
         assert(r >= 0);
 
         assert((uint8_t*) p + 1 == (uint8_t*) q);