return errors for unsupported operations
[openssl.git] / crypto / sec_mem.c
index ea9523b2b91e4095a5f07c88bcb85c039e988faf..0b2f1fda77428ef36139d09a874366ca8c2d854f 100644 (file)
  */
 #include <openssl/crypto.h>
 #include <e_os.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <assert.h>
 
 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
 # define IMPLEMENTED
+# include <stdlib.h>
+# 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)
 #define UNLOCK()    CRYPTO_w_unlock(CRYPTO_LOCK_MALLOC)
 #define CLEAR(p, s) OPENSSL_cleanse(p, s)
-#define PAGE_SIZE    4096
+#ifndef PAGE_SIZE
+# define PAGE_SIZE    4096
+#endif
 
 #ifdef IMPLEMENTED
-size_t secure_mem_used;
+static size_t secure_mem_used;
 
 static int secure_mem_initialized;
 static int too_late;
@@ -312,33 +317,53 @@ static int sh_init(size_t size, int minsize)
     for (i = sh.bittable_size; i; i >>= 1)
         sh.freelist_size++;
 
-    sh.freelist = OPENSSL_malloc(sh.freelist_size * sizeof (char *));
+    sh.freelist = OPENSSL_zalloc(sh.freelist_size * sizeof (char *));
     OPENSSL_assert(sh.freelist != NULL);
     if (sh.freelist == NULL)
         goto err;
-    memset(sh.freelist, 0, sh.freelist_size * sizeof (char *));
 
-    sh.bittable = OPENSSL_malloc(sh.bittable_size >> 3);
+    sh.bittable = OPENSSL_zalloc(sh.bittable_size >> 3);
     OPENSSL_assert(sh.bittable != NULL);
     if (sh.bittable == NULL)
         goto err;
-    memset(sh.bittable, 0, sh.bittable_size >> 3);
 
-    sh.bitmalloc = OPENSSL_malloc(sh.bittable_size >> 3);
+    sh.bitmalloc = OPENSSL_zalloc(sh.bittable_size >> 3);
     OPENSSL_assert(sh.bitmalloc != NULL);
     if (sh.bitmalloc == NULL)
         goto err;
-    memset(sh.bitmalloc, 0, sh.bittable_size >> 3);
 
     /* Allocate space for heap, and two extra pages as guards */
-#ifdef _SC_PAGE_SIZE
-    pgsize = (size_t)sysconf(_SC_PAGE_SIZE);
+#if defined(_SC_PAGE_SIZE) || defined (_SC_PAGESIZE)
+    {
+# if defined(_SC_PAGE_SIZE)
+        long tmppgsize = sysconf(_SC_PAGE_SIZE);
+# else
+        long tmppgsize = sysconf(_SC_PAGESIZE);
+# endif
+        if (tmppgsize < 1)
+            pgsize = PAGE_SIZE;
+        else
+            pgsize = (size_t)tmppgsize;
+    }
 #else
     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;