]> granicus.if.org Git - imagemagick/commitdiff
...
authorCristy <urban-warrior@imagemagick.org>
Sat, 8 Apr 2017 23:02:34 +0000 (19:02 -0400)
committerCristy <urban-warrior@imagemagick.org>
Sat, 8 Apr 2017 23:02:34 +0000 (19:02 -0400)
MagickCore/memory.c
config/policy.xml

index 985ab26a3bfd42689a33b428aaaa47eb7c0c9405..c24448f19199b482f602c507338e78f03db70d63 100644 (file)
@@ -60,6 +60,7 @@
 #include "MagickCore/exception-private.h"
 #include "MagickCore/memory_.h"
 #include "MagickCore/memory-private.h"
+#include "MagickCore/policy.h"
 #include "MagickCore/resource_.h"
 #include "MagickCore/semaphore.h"
 #include "MagickCore/string_.h"
@@ -569,6 +570,9 @@ MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
   size_t
     extent;
 
+  static ssize_t
+    virtual_anonymous_memory = (-1);
+
   if (HeapOverflowSanityCheck(count,quantum) != MagickFalse)
     return((MemoryInfo *) NULL);
   memory_info=(MemoryInfo *) MagickAssumeAligned(AcquireAlignedMemory(1,
@@ -579,64 +583,88 @@ MagickExport MemoryInfo *AcquireVirtualMemory(const size_t count,
   extent=count*quantum;
   memory_info->length=extent;
   memory_info->signature=MagickCoreSignature;
-  if (AcquireMagickResource(MemoryResource,extent) != MagickFalse)
+  if (virtual_anonymous_memory < 0)
     {
-      memory_info->blob=AcquireAlignedMemory(1,extent);
-      if (memory_info->blob != NULL)
+      char
+        *value;
+
+      /*
+        Does the security policy require anonymous mapping for pixel cache?
+      */
+      virtual_anonymous_memory=0;
+      value=GetPolicyValue("system:memory-map");
+      if (LocaleCompare(value,"anonymous") == 0)
         {
-          memory_info->type=AlignedVirtualMemory;
-          return(memory_info);
+#if defined(MAGICKCORE_HAVE_MMAP) && defined(MAP_ANONYMOUS)
+          virtual_anonymous_memory=1;
+#endif
         }
+      value=DestroyString(value);
     }
-  RelinquishMagickResource(MemoryResource,extent);
-  if (AcquireMagickResource(MapResource,extent) != MagickFalse)
+  if (virtual_anonymous_memory <= 0)
     {
-      /*
-        Heap memory failed, try anonymous memory mapping.
-      */
-      memory_info->blob=MapBlob(-1,IOMode,0,extent);
-      if (memory_info->blob != NULL)
+      if (AcquireMagickResource(MemoryResource,extent) != MagickFalse)
         {
-          memory_info->type=MapVirtualMemory;
-          return(memory_info);
+          memory_info->blob=AcquireAlignedMemory(1,extent);
+          if (memory_info->blob != NULL)
+            {
+              memory_info->type=AlignedVirtualMemory;
+              return(memory_info);
+            }
         }
-      if (AcquireMagickResource(DiskResource,extent) != MagickFalse)
+      RelinquishMagickResource(MemoryResource,extent);
+    }
+  else
+    {
+      if (AcquireMagickResource(MapResource,extent) != MagickFalse)
         {
-          int
-            file;
-
           /*
-            Anonymous memory mapping failed, try file-backed memory mapping.
-            If the MapResource request failed, there is no point in trying
-            file-backed memory mapping.
+            Acquire anonymous memory map.
           */
-          file=AcquireUniqueFileResource(memory_info->filename);
-          if (file != -1)
+          memory_info->blob=MapBlob(-1,IOMode,0,extent);
+          if (memory_info->blob != NULL)
+            {
+              memory_info->type=MapVirtualMemory;
+              return(memory_info);
+            }
+          if (AcquireMagickResource(DiskResource,extent) != MagickFalse)
             {
-              MagickOffsetType
-                offset;
+              int
+                file;
 
-              offset=(MagickOffsetType) lseek(file,extent-1,SEEK_SET);
-              if ((offset == (MagickOffsetType) (extent-1)) &&
-                  (write(file,"",1) == 1))
+              /*
+                Anonymous memory mapping failed, try file-backed memory mapping.
+                If the MapResource request failed, there is no point in trying
+                file-backed memory mapping.
+              */
+              file=AcquireUniqueFileResource(memory_info->filename);
+              if (file != -1)
                 {
-                  memory_info->blob=MapBlob(file,IOMode,0,extent);
-                  if (memory_info->blob != NULL)
+                  MagickOffsetType
+                    offset;
+
+                  offset=(MagickOffsetType) lseek(file,extent-1,SEEK_SET);
+                  if ((offset == (MagickOffsetType) (extent-1)) &&
+                      (write(file,"",1) == 1))
                     {
-                      (void) close(file);
-                      memory_info->type=MapVirtualMemory;
-                      return(memory_info);
+                      memory_info->blob=MapBlob(file,IOMode,0,extent);
+                      if (memory_info->blob != NULL)
+                        {
+                          (void) close(file);
+                          memory_info->type=MapVirtualMemory;
+                          return(memory_info);
+                        }
                     }
+                  /*
+                    File-backed memory mapping fail, delete the temporary file.
+                  */
+                  (void) close(file);
+                  (void) RelinquishUniqueFileResource(memory_info->filename);
+                  *memory_info->filename = '\0';
                 }
-              /*
-                File-backed memory mapping failed, delete the temporary file.
-              */
-              (void) close(file);
-              (void) RelinquishUniqueFileResource(memory_info->filename);
-              *memory_info->filename = '\0';
             }
+          RelinquishMagickResource(DiskResource,extent);
         }
-      RelinquishMagickResource(DiskResource,extent);
     }
   RelinquishMagickResource(MapResource,extent);
   if (memory_info->blob == NULL)
index 60c20478ee2178bf4638b4de5aa9caf87c743e49..f41dba585572742f094a8555ebd1e6b1cc561e13 100644 (file)
@@ -64,6 +64,7 @@
 <policymap>
   <!-- <policy domain="system" name="shred" value="2"/> -->
   <!-- <policy domain="system" name="precision" value="6"/> -->
+  <!-- <policy domain="system" name="memory-map" value="anonymous"/> -->
   <!-- <policy domain="resource" name="temporary-path" value="/tmp"/> -->
   <!-- <policy domain="resource" name="memory" value="2GiB"/> -->
   <!-- <policy domain="resource" name="map" value="4GiB"/> -->
@@ -75,6 +76,7 @@
   <!-- <policy domain="resource" name="thread" value="4"/> -->
   <!-- <policy domain="resource" name="throttle" value="0"/> -->
   <!-- <policy domain="resource" name="time" value="3600"/> -->
+  <!-- <policy domain="resource" name="time" value="3600"/> -->
   <!-- <policy domain="coder" rights="none" pattern="MVG" /> -->
   <!-- <policy domain="delegate" rights="none" pattern="HTTPS" /> -->
   <!-- <policy domain="path" rights="none" pattern="@*" /> -->