Commit 207091d2d for imagemagick.org

commit 207091d2d5354835fb7a9056b62b1e52b1cdf159
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sat Dec 5 11:01:44 2026 -0500

    harden that the user policy can lower a resource limit but not raise it

diff --git a/MagickCore/policy-private.h b/MagickCore/policy-private.h
index 9d28577c3..b1f2246ff 100644
--- a/MagickCore/policy-private.h
+++ b/MagickCore/policy-private.h
@@ -18,15 +18,16 @@
 #ifndef MAGICKCORE_POLICY_PRIVATE_H
 #define MAGICKCORE_POLICY_PRIVATE_H

-#if defined(__cplusplus) || defined(c_plusplus)
-extern "C" {
-#endif
-
+#include "MagickCore/policy.h"
 #include "MagickCore/quantum-private.h"
 #include "MagickCore/resource-private.h"
 #include "MagickCore/string-private.h"
 #include "MagickCore/utility-private.h"

+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
 /*
   Zero configuration security policy.  Discussion @
diff --git a/MagickCore/policy.c b/MagickCore/policy.c
index 9514467ec..f89baafb8 100644
--- a/MagickCore/policy.c
+++ b/MagickCore/policy.c
@@ -193,7 +193,7 @@ static LinkedListInfo *AcquirePolicyCache(const char *filename,
   if (cache == (LinkedListInfo *) NULL)
     return((LinkedListInfo *) NULL);
   status=MagickTrue;
-#if MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
+#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
   magick_unreferenced(filename);
   status=LoadPolicyCache(cache,ZeroConfigurationPolicy,"[zero-configuration]",0,
     exception);
@@ -872,6 +872,7 @@ MagickExport MagickBooleanType IsRightsAuthorizedByName(
     *canonical_directory = (char *) NULL,
     *canonical_path = (char *) NULL,
     *canonical_candidate = (char *) NULL,
+    *resolved_path = (char *) NULL,
     directory[MagickPathExtent],
     filename[MagickPathExtent];

@@ -941,6 +942,14 @@ MagickExport MagickBooleanType IsRightsAuthorizedByName(
               canonical_directory,DirectorySeparator,filename);
         }
       canonical_path=realpath_utf8(pattern);
+      /*
+        A target that does not exist yet (e.g. a file about to be written) has
+        no realpath; resolve it as canonical directory plus basename instead so
+        that lexical tricks such as "allowed/../denied/new" are still judged by
+        where the file will actually land.
+      */
+      resolved_path=canonical_path != (char *) NULL ? canonical_path :
+        canonical_candidate;
     }
   /*
     Evaluate policies in order; the last matching policy wins.  A path is
@@ -977,8 +986,8 @@ MagickExport MagickBooleanType IsRightsAuthorizedByName(
         /*
           Match the fully resolved path on its own.
         */
-        if ((canonical_path != (char *) NULL) &&
-            (IsPathPolicyMatch(canonical_path,policy->pattern) != MagickFalse))
+        if ((resolved_path != (char *) NULL) &&
+            (IsPathPolicyMatch(resolved_path,policy->pattern) != MagickFalse))
           {
             resolved_matched_any=MagickTrue;
             resolved_rights=policy->rights;
@@ -1573,8 +1582,8 @@ MagickExport MagickBooleanType SetMagickSecurityPolicy(const char *policy,
   if (IsPolicyCacheInstantiated(exception) == MagickFalse)
     return(MagickFalse);
   /*
-    Parse and synchronize user policies in a single pass before committing
-    them to the live policy_cache.
+    Parse user policies exactly once (an <include/> file is read a single time,
+    so it cannot change between validation and commit).
   */
   user_policies=NewLinkedList(0);
   if (user_policies == (LinkedListInfo *) NULL)
@@ -1585,14 +1594,11 @@ MagickExport MagickBooleanType SetMagickSecurityPolicy(const char *policy,
       user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
       return(MagickFalse);
     }
-  LockSemaphoreInfo(policy_semaphore);
-  status=LoadPolicyCache(policy_cache,policy,"[user-policy]",0,exception);
-  UnlockSemaphoreInfo(policy_semaphore);
-  if (status == MagickFalse)
-    {
-      user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
-      return(MagickFalse);
-    }
+  /*
+    Synchronize values before committing, so limits are clamped against the
+    policies already in force (policy.xml) rather than against the user
+    values themselves; a user policy may restrict a limit but not raise it.
+  */
   ResetLinkedListIterator(user_policies);
   p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
   while (p != (PolicyInfo *) NULL)
@@ -1601,6 +1607,24 @@ MagickExport MagickBooleanType SetMagickSecurityPolicy(const char *policy,
       (void) SetMagickSecurityPolicyValue(p->domain,p->name,p->value,exception);
     p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
   }
+  /*
+    Commit the parsed policies to the live cache in order, under one lock.
+  */
+  LockSemaphoreInfo(policy_semaphore);
+  p=(PolicyInfo *) RemoveElementFromLinkedList(user_policies,0);
+  while (p != (PolicyInfo *) NULL)
+  {
+    if (AppendValueToLinkedList(policy_cache,p) == MagickFalse)
+      {
+        (void) ThrowMagickException(exception,GetMagickModule(),
+          ResourceLimitError,"MemoryAllocationFailed","`%s'",
+          p->name == (char *) NULL ? "" : p->name);
+        (void) DestroyPolicyElement(p);
+        status=MagickFalse;
+      }
+    p=(PolicyInfo *) RemoveElementFromLinkedList(user_policies,0);
+  }
+  UnlockSemaphoreInfo(policy_semaphore);
   user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
   return(status);
 }
diff --git a/MagickCore/policy.h b/MagickCore/policy.h
index 3c06031d5..7f959cb9a 100644
--- a/MagickCore/policy.h
+++ b/MagickCore/policy.h
@@ -18,6 +18,7 @@
 #ifndef MAGICKCORE_POLICY_H
 #define MAGICKCORE_POLICY_H

+#include <stdio.h>
 #include "MagickCore/pixel.h"
 #include "MagickCore/exception.h"