aboutsummaryrefslogtreecommitdiff
path: root/src/lib/malloc
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2019-02-25 08:55:25 -0500
committerNick Mathewson <nickm@torproject.org>2019-02-25 08:55:25 -0500
commit065e7da8e6fdbd9331de8c13344275a8e0fbf32d (patch)
tree063d36265dd691d1e49232358e4267c417d4ed96 /src/lib/malloc
parent69238ca2da923c8a50d5c1007f3e702eea163b50 (diff)
downloadtor-065e7da8e6fdbd9331de8c13344275a8e0fbf32d.tar.gz
tor-065e7da8e6fdbd9331de8c13344275a8e0fbf32d.zip
Re-enable and fix unit test for nofork mappings
This test was previously written to use the contents of the system headers to decide whether INHERIT_NONE or INHERIT_ZERO was going to work. But that won't work across different environments, such as (for example) when the kernel doesn't match the headers. Instead, we add a testing-only feature to the code to track which of these options actually worked, and verify that it behaved as we expected. Closes ticket 29541; bugfix not on any released version of Tor.
Diffstat (limited to 'src/lib/malloc')
-rw-r--r--src/lib/malloc/map_anon.c34
-rw-r--r--src/lib/malloc/map_anon.h4
2 files changed, 36 insertions, 2 deletions
diff --git a/src/lib/malloc/map_anon.c b/src/lib/malloc/map_anon.c
index 2fc6e89ea2..5dac5256a6 100644
--- a/src/lib/malloc/map_anon.c
+++ b/src/lib/malloc/map_anon.c
@@ -107,6 +107,29 @@ nodump_mem(void *mem, size_t sz)
#endif
}
+#ifdef TOR_UNIT_TESTS
+static unsigned last_anon_map_noinherit = ~0;
+/* Testing helper: return the outcome of the last call to noinherit_mem():
+ * 0 if it did no good; 1 if it caused the memory not to be inherited, and
+ * 2 if it caused the memory to be cleared on fork */
+unsigned
+get_last_anon_map_noinherit(void)
+{
+ return last_anon_map_noinherit;
+}
+static void
+set_last_anon_map_noinherit(unsigned f)
+{
+ last_anon_map_noinherit = f;
+}
+#else
+static void
+set_last_anon_map_noinherit(unsigned f)
+{
+ (void)f;
+}
+#endif
+
/**
* Helper: try to prevent the <b>sz</b> bytes at <b>mem</b> from being
* accessible in child processes -- ideally by having them set to 0 after a
@@ -117,13 +140,20 @@ nodump_mem(void *mem, size_t sz)
static int
noinherit_mem(void *mem, size_t sz)
{
+ set_last_anon_map_noinherit(0);
#ifdef FLAG_ZERO
int r = MINHERIT(mem, sz, FLAG_ZERO);
- if (r == 0)
+ if (r == 0) {
+ set_last_anon_map_noinherit(2);
return 0;
+ }
#endif
#ifdef FLAG_NOINHERIT
- return MINHERIT(mem, sz, FLAG_NOINHERIT);
+ int r2 = MINHERIT(mem, sz, FLAG_NOINHERIT);
+ if (r2 == 0) {
+ set_last_anon_map_noinherit(1);
+ }
+ return r2;
#else
(void)mem;
(void)sz;
diff --git a/src/lib/malloc/map_anon.h b/src/lib/malloc/map_anon.h
index cc5797e4ec..395145bd71 100644
--- a/src/lib/malloc/map_anon.h
+++ b/src/lib/malloc/map_anon.h
@@ -34,4 +34,8 @@
void *tor_mmap_anonymous(size_t sz, unsigned flags);
void tor_munmap_anonymous(void *mapping, size_t sz);
+#ifdef TOR_UNIT_TESTS
+unsigned get_last_anon_map_noinherit(void);
+#endif
+
#endif /* !defined(TOR_MAP_ANON_H) */