crypto/sec_mem.c: fix anonymous mmap on legacy systems.
authorAndy Polyakov <appro@openssl.org>
Fri, 13 Nov 2015 20:30:44 +0000 (21:30 +0100)
committerAndy Polyakov <appro@openssl.org>
Mon, 16 Nov 2015 12:15:00 +0000 (13:15 +0100)
Reviewed-by: Kurt Roeckx <kurt@openssl.org>
crypto/sec_mem.c

index b7a9d3b6e4290950f7d99f3570ae3fb3e4bd9755..2e29219d52ec349ac65fc52a9953f6172da39cd5 100644 (file)
 # include <string.h>
 # include <assert.h>
 # include <unistd.h>
+# include <sys/types.h>
 # include <sys/mman.h>
 # include <sys/param.h>
+# include <sys/stat.h>
+# include <fcntl.h>
 #endif
 
 #define LOCK()      CRYPTO_w_lock(CRYPTO_LOCK_MALLOC)
@@ -336,8 +339,21 @@ static int sh_init(size_t size, int minsize)
     pgsize = PAGE_SIZE;
 #endif
     sh.map_size = pgsize + sh.arena_size + pgsize;
-    sh.map_result = mmap(NULL, sh.map_size,
-                         PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
+    if (1) {
+#ifdef MAP_ANON
+        sh.map_result = mmap(NULL, sh.map_size,
+                             PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
+    } else {
+#endif
+        int fd;
+
+        sh.map_result = MAP_FAILED;
+        if ((fd = open("/dev/zero", O_RDWR)) >= 0) {
+            sh.map_result = mmap(NULL, sh.map_size,
+                                 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
+            close(fd);
+        }
+    }
     OPENSSL_assert(sh.map_result != MAP_FAILED);
     if (sh.map_result == MAP_FAILED)
         goto err;