mempool: Protect whole range of populate none across fork
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 19 Mar 2024 15:20:06 +0000 (11:20 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 19 Mar 2024 15:20:06 +0000 (11:20 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Change-Id: I6928128ceffed57fd4ecfdaee50a3085e585387a

include/rseq/mempool.h
src/rseq-mempool.c

index 345d39153960d01c3ed70970e28f29a22c59f881..f439e07fe182cc6403b3e18876ddf5c20aa18308 100644 (file)
@@ -544,7 +544,8 @@ enum rseq_mempool_populate_policy {
         *   mempool. Rely on copy-on-write (COW) of per-cpu pages to
         *   populate per-cpu pages from the initial values pages on
         *   first write. This mempool is only meant for single-process
-        *   use (private mapping).
+        *   use (private mapping). Note that this type of pool cannot
+        *   be accessed from children processes across fork.
         */
        RSEQ_MEMPOOL_POPULATE_PRIVATE_NONE = 0,
        /*
index 96e0830bef35c71a60ce47ad21c5b955ddf1208a..092669ac1e9a395d4a42805044218d5ad272b938 100644 (file)
@@ -751,12 +751,6 @@ struct rseq_mempool_range *rseq_mempool_range_create(struct rseq_mempool *pool)
                                MAP_SHARED | MAP_FIXED, memfd, 0) != (void *) range->init) {
                        goto error_alloc;
                }
-               /*
-                * Make sure the init values shared mapping is not
-                * shared with the children processes across fork.
-                */
-               if (madvise(range->init, pool->attr.stride, MADV_DONTFORK))
-                       goto error_alloc;
                assert(pool->attr.type == MEMPOOL_TYPE_PERCPU);
                /*
                 * Map per-cpu memory as private COW mappings of init values.
@@ -774,6 +768,13 @@ struct rseq_mempool_range *rseq_mempool_range_create(struct rseq_mempool *pool)
                                }
                        }
                }
+               /*
+                * The init values shared mapping should not be shared
+                * with the children processes across fork. Prevent the
+                * whole mapping from being used across fork.
+                */
+               if (madvise(range->mmap_addr, pool->mmap_len, MADV_DONTFORK))
+                       goto error_alloc;
                rseq_memfd_close(memfd);
                memfd = -1;
        }
This page took 0.027006 seconds and 4 git commands to generate.