[systemd-commits] 5 commits - src/shared

David Herrmann dvdhrm at kemper.freedesktop.org
Mon Aug 18 04:11:34 PDT 2014


 src/shared/memfd.c |   31 ++++++++++++++++---------------
 src/shared/memfd.h |   15 +++++----------
 2 files changed, 21 insertions(+), 25 deletions(-)

New commits:
commit 8a02decaf1e81bad3c06752e998734c96ab11260
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Mon Aug 18 13:07:43 2014 +0200

    memfd: use _cleanup_ if applicable
    
    We now have a sd_memfd_freep helper, use it if applicable.

diff --git a/src/shared/memfd.c b/src/shared/memfd.c
index d94c626..e246f91 100644
--- a/src/shared/memfd.c
+++ b/src/shared/memfd.c
@@ -241,7 +241,7 @@ int sd_memfd_set_size(sd_memfd *m, uint64_t sz) {
 }
 
 int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p) {
-        sd_memfd *n;
+        _cleanup_(sd_memfd_freep) sd_memfd *n = NULL;
         int r;
 
         r = sd_memfd_new(&n, name);
@@ -249,18 +249,15 @@ int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p) {
                 return r;
 
         r = sd_memfd_set_size(n, sz);
-        if (r < 0) {
-                sd_memfd_free(n);
+        if (r < 0)
                 return r;
-        }
 
         r = sd_memfd_map(n, 0, sz, p);
-        if (r < 0) {
-                sd_memfd_free(n);
+        if (r < 0)
                 return r;
-        }
 
         *m = n;
+        n = NULL;
         return 0;
 }
 

commit 23972f4244f7609658c2a17f85508d50e4739990
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Mon Aug 18 13:05:48 2014 +0200

    memfd: map unsealed files as MAP_SHARED
    
    We need to map sealed files as MAP_PRIVATE so far as the kernel treats
    MAP_SHARED as writable mapping (you can run mprotect(PROT_WRITE) at any
    time on those). However, unsealed files must be mapped as MAP_SHARED.
    Otherwise, we never end up writing to the real file.

diff --git a/src/shared/memfd.c b/src/shared/memfd.c
index 6804b42..d94c626 100644
--- a/src/shared/memfd.c
+++ b/src/shared/memfd.c
@@ -176,7 +176,11 @@ int sd_memfd_map(sd_memfd *m, uint64_t offset, size_t size, void **p) {
         if (sealed < 0)
                 return sealed;
 
-        q = mmap(NULL, size, sealed ? PROT_READ : PROT_READ|PROT_WRITE, MAP_PRIVATE, m->fd, offset);
+        if (sealed)
+                q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, m->fd, offset);
+        else
+                q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, m->fd, offset);
+
         if (q == MAP_FAILED)
                 return -errno;
 

commit c7dab73a5fa6e775813765fe925caaa7c4e549fa
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Mon Aug 18 13:03:09 2014 +0200

    memfd: disallow importing memfds without sealing
    
    We use memfds for sealing. Lets not bother with memfds created without
    MFD_ALLOW_SEALING for now. They're equivalent to random shmem files, so
    don't bother treating them as sealable memfds.

diff --git a/src/shared/memfd.c b/src/shared/memfd.c
index 6a2e121..6804b42 100644
--- a/src/shared/memfd.c
+++ b/src/shared/memfd.c
@@ -97,12 +97,17 @@ int sd_memfd_new(sd_memfd **m, const char *name) {
 
 int sd_memfd_new_from_fd(sd_memfd **m, int fd) {
         sd_memfd *n;
+        int r;
 
         assert_return(m, -EINVAL);
         assert_return(fd >= 0, -EINVAL);
 
-        /* Check if this is a sealable fd */
-        if (fcntl(fd, F_GET_SEALS) < 0)
+        /* Check if this is a sealable fd. The kernel sets F_SEAL_SEAL on memfds
+         * that don't support sealing, so check for that, too. A file with
+         * *only* F_SEAL_SEAL set is the same as a random shmem file, so no
+         * reason to allow opening it as memfd. */
+        r = fcntl(fd, F_GET_SEALS);
+        if (r < 0 || r == F_SEAL_SEAL)
                 return -ENOTTY;
 
         n = new0(struct sd_memfd, 1);

commit 302e4b4963c471baefa60b220e3e05f93a49de45
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Mon Aug 18 12:57:55 2014 +0200

    memfd: don't open kdbus for memfd
    
    No reason to open /dev/kdbus/control if we want memfds. memfd_create() is
    always available.

diff --git a/src/shared/memfd.c b/src/shared/memfd.c
index dcebfc9..6a2e121 100644
--- a/src/shared/memfd.c
+++ b/src/shared/memfd.c
@@ -39,16 +39,11 @@ struct sd_memfd {
 
 int sd_memfd_new(sd_memfd **m, const char *name) {
 
-        _cleanup_close_ int kdbus = -1;
         _cleanup_free_ char *g = NULL;
         sd_memfd *n;
 
         assert_return(m, -EINVAL);
 
-        kdbus = open("/dev/kdbus/control", O_RDWR|O_NOCTTY|O_CLOEXEC);
-        if (kdbus < 0)
-                return -errno;
-
         if (name) {
                 /* The kernel side is pretty picky about the character
                  * set here, let's do the usual bus escaping to deal

commit 7e2ce386ca9ed825aaac2babe09946380af6bb94
Author: David Herrmann <dh.herrmann at gmail.com>
Date:   Mon Aug 18 12:57:03 2014 +0200

    memfd: internalize header
    
    Fix the memfd.h header to use handy features like #pragma, cleanup-funcs
    and util.h. Also drop the EXTERN-C macros.

diff --git a/src/shared/memfd.h b/src/shared/memfd.h
index 6de045c..452fb50 100644
--- a/src/shared/memfd.h
+++ b/src/shared/memfd.h
@@ -1,7 +1,6 @@
 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
-#ifndef foosdmemfdhfoo
-#define foosdmemfdhfoo
+#pragma once
 
 /***
   This file is part of systemd.
@@ -25,10 +24,8 @@
 #include <inttypes.h>
 #include <sys/types.h>
 #include <stdio.h>
-
-#include "_sd-common.h"
-
-_SD_BEGIN_DECLARATIONS;
+#include "macro.h"
+#include "util.h"
 
 typedef struct sd_memfd sd_memfd;
 
@@ -38,6 +35,8 @@ int sd_memfd_new_and_map(sd_memfd **m, const char *name, size_t sz, void **p);
 
 void sd_memfd_free(sd_memfd *m);
 
+DEFINE_TRIVIAL_CLEANUP_FUNC(sd_memfd*, sd_memfd_free);
+
 int sd_memfd_get_fd(sd_memfd *m);
 int sd_memfd_dup_fd(sd_memfd *n);
 int sd_memfd_get_file(sd_memfd *m, FILE **f);
@@ -51,7 +50,3 @@ int sd_memfd_get_size(sd_memfd *m, uint64_t *sz);
 int sd_memfd_set_size(sd_memfd *m, uint64_t sz);
 
 int sd_memfd_get_name(sd_memfd *m, char **name);
-
-_SD_END_DECLARATIONS;
-
-#endif



More information about the systemd-commits mailing list