40#include "MagickCore/studio.h"
41#include "MagickCore/cache-private.h"
42#include "MagickCore/client.h"
43#include "MagickCore/configure.h"
44#include "MagickCore/configure-private.h"
45#include "MagickCore/exception.h"
46#include "MagickCore/exception-private.h"
47#include "MagickCore/linked-list-private.h"
48#include "MagickCore/magick-private.h"
49#include "MagickCore/memory_.h"
50#include "MagickCore/memory-private.h"
51#include "MagickCore/monitor.h"
52#include "MagickCore/monitor-private.h"
53#include "MagickCore/option.h"
54#include "MagickCore/policy.h"
55#include "MagickCore/policy-private.h"
56#include "MagickCore/resource_.h"
57#include "MagickCore/resource-private.h"
58#include "MagickCore/semaphore.h"
59#include "MagickCore/stream-private.h"
60#include "MagickCore/string_.h"
61#include "MagickCore/string-private.h"
62#include "MagickCore/token.h"
63#include "MagickCore/timer-private.h"
64#include "MagickCore/utility.h"
65#include "MagickCore/utility-private.h"
66#include "MagickCore/xml-tree.h"
67#include "MagickCore/xml-tree-private.h"
68#if defined(MAGICKCORE_XML_DELEGATE)
69# include <libxml/parser.h>
70# include <libxml/tree.h>
76#define PolicyFilename "policy.xml"
126static const PolicyMapInfo
129 { UndefinedPolicyDomain, UndefinedPolicyRights, (
const char *) NULL,
130 (
const char *) NULL, (
const char *) NULL }
134 *policy_cache = (LinkedListInfo *) NULL;
142static MagickBooleanType
143 IsPolicyCacheInstantiated(ExceptionInfo *),
144 LoadPolicyCache(LinkedListInfo *,
const char *,
const char *,
const size_t,
173static LinkedListInfo *AcquirePolicyCache(
const char *filename,
174 ExceptionInfo *exception)
188 cache=NewLinkedList(0);
190#if MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
191 magick_unreferenced(filename);
192 status=LoadPolicyCache(cache,ZeroConfigurationPolicy,
"[zero-configuration]",0,
194 if (status == MagickFalse)
195 CatchException(exception);
204 options=GetConfigureOptions(filename,exception);
205 option=(
const StringInfo *) GetNextValueInLinkedList(options);
206 while (option != (
const StringInfo *) NULL)
208 status=LoadPolicyCache(cache,(
const char *) GetStringInfoDatum(option),
209 GetStringInfoPath(option),0,exception);
210 if (status == MagickFalse)
211 CatchException(exception);
212 option=(
const StringInfo *) GetNextValueInLinkedList(options);
214 options=DestroyConfigureOptions(options);
220 for (i=0; i < (ssize_t) (
sizeof(PolicyMap)/
sizeof(*PolicyMap)); i++)
229 policy_info=(PolicyInfo *) AcquireMagickMemory(
sizeof(*policy_info));
230 if (policy_info == (PolicyInfo *) NULL)
232 (void) ThrowMagickException(exception,GetMagickModule(),
233 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
234 p->name == (
char *) NULL ?
"" : p->name);
235 CatchException(exception);
238 (void) memset(policy_info,0,
sizeof(*policy_info));
239 policy_info->path=(
char *)
"[built-in]";
240 policy_info->domain=p->domain;
241 policy_info->rights=p->rights;
242 policy_info->name=(
char *) p->name;
243 policy_info->pattern=(
char *) p->pattern;
244 policy_info->value=(
char *) p->value;
245 policy_info->exempt=MagickTrue;
246 policy_info->signature=MagickCoreSignature;
247 status=AppendValueToLinkedList(cache,policy_info);
248 if (status == MagickFalse)
250 (void) ThrowMagickException(exception,GetMagickModule(),
251 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
252 p->name == (
char *) NULL ?
"" : p->name);
253 CatchException(exception);
284static PolicyInfo *GetPolicyInfo(
const char *name,ExceptionInfo *exception)
287 policyname[MagickPathExtent],
299 assert(exception != (ExceptionInfo *) NULL);
300 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
301 return((PolicyInfo *) NULL);
306 if (name != (
const char *) NULL)
307 (void) CopyMagickString(policyname,name,MagickPathExtent);
308 for (q=policyname; *q !=
'\0'; q++)
310 if (isspace((
int) ((
unsigned char) *q)) == 0)
312 (void) CopyMagickString(q,q+1,MagickPathExtent);
318 domain=UndefinedPolicyDomain;
319 for (q=policyname; *q !=
'\0'; q++)
324 domain=(PolicyDomain) ParseCommandOption(MagickPolicyDomainOptions,
325 MagickTrue,policyname);
326 (void) CopyMagickString(policyname,q+1,MagickPathExtent);
332 policy=(PolicyInfo *) NULL;
333 LockSemaphoreInfo(policy_semaphore);
334 ResetLinkedListIterator(policy_cache);
335 p=GetHeadElementInLinkedList(policy_cache);
336 if ((name == (
const char *) NULL) || (LocaleCompare(name,
"*") == 0))
338 UnlockSemaphoreInfo(policy_semaphore);
339 if (p != (ElementInfo *) NULL)
340 policy=(PolicyInfo *) p->value;
343 while (p != (ElementInfo *) NULL)
345 policy=(PolicyInfo *) p->value;
346 if ((domain == UndefinedPolicyDomain) || (policy->domain == domain))
347 if (LocaleCompare(policyname,policy->name) == 0)
351 if (p == (ElementInfo *) NULL)
352 policy=(PolicyInfo *) NULL;
354 (
void) SetHeadElementInLinkedList(policy_cache,p);
355 UnlockSemaphoreInfo(policy_semaphore);
386MagickExport
const PolicyInfo **GetPolicyInfoList(
const char *pattern,
387 size_t *number_policies,ExceptionInfo *exception)
398 assert(pattern != (
char *) NULL);
399 assert(number_policies != (
size_t *) NULL);
400 if (IsEventLogging() != MagickFalse)
401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",pattern);
403 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
404 return((
const PolicyInfo **) NULL);
405 policies=(
const PolicyInfo **) AcquireQuantumMemory((
size_t)
406 GetNumberOfElementsInLinkedList(policy_cache)+1UL,
sizeof(*policies));
407 if (policies == (
const PolicyInfo **) NULL)
408 return((
const PolicyInfo **) NULL);
409 LockSemaphoreInfo(policy_semaphore);
410 p=GetHeadElementInLinkedList(policy_cache);
411 for (i=0; p != (ElementInfo *) NULL; )
416 policy=(
const PolicyInfo *) p->value;
417 if ((policy->stealth == MagickFalse) &&
418 (GlobExpression(policy->name,pattern,MagickFalse) != MagickFalse))
419 policies[i++]=policy;
422 UnlockSemaphoreInfo(policy_semaphore);
424 policies=(
const PolicyInfo **) RelinquishMagickMemory((
void*) policies);
426 policies[i]=(PolicyInfo *) NULL;
427 *number_policies=(size_t) i;
459static char *AcquirePolicyString(
const char *source,
const size_t pad)
468 if (source != (
char *) NULL)
469 length+=strlen(source);
470 destination=(
char *) NULL;
473 destination=(
char *) AcquireMagickMemory((length+pad)*
sizeof(*destination));
474 if (destination == (
char *) NULL)
475 ThrowFatalException(ResourceLimitFatalError,
"UnableToAcquireString");
476 if (source != (
char *) NULL)
477 (void) memcpy(destination,source,length*
sizeof(*destination));
478 destination[length]=
'\0';
482MagickExport
char **GetPolicyList(
const char *pattern,
size_t *number_policies,
483 ExceptionInfo *exception)
494 assert(pattern != (
char *) NULL);
495 assert(number_policies != (
size_t *) NULL);
496 if (IsEventLogging() != MagickFalse)
497 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",pattern);
499 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
500 return((
char **) NULL);
501 policies=(
char **) AcquireQuantumMemory((
size_t)
502 GetNumberOfElementsInLinkedList(policy_cache)+1UL,
sizeof(*policies));
503 if (policies == (
char **) NULL)
504 return((
char **) NULL);
505 LockSemaphoreInfo(policy_semaphore);
506 p=GetHeadElementInLinkedList(policy_cache);
507 for (i=0; p != (ElementInfo *) NULL; )
512 policy=(
const PolicyInfo *) p->value;
513 if ((policy->stealth == MagickFalse) &&
514 (GlobExpression(policy->name,pattern,MagickFalse) != MagickFalse))
515 policies[i++]=AcquirePolicyString(policy->name,1);
518 UnlockSemaphoreInfo(policy_semaphore);
520 policies=(
char **) RelinquishMagickMemory(policies);
522 policies[i]=(
char *) NULL;
523 *number_policies=(size_t) i;
549MagickExport
char *GetPolicyValue(
const char *name)
560 assert(name != (
const char *) NULL);
561 if (IsEventLogging() != MagickFalse)
562 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",name);
563 exception=AcquireExceptionInfo();
564 policy_info=GetPolicyInfo(name,exception);
565 exception=DestroyExceptionInfo(exception);
566 if (policy_info == (PolicyInfo *) NULL)
567 return((
char *) NULL);
568 value=policy_info->value;
569 if ((value == (
const char *) NULL) || (*value ==
'\0'))
570 return((
char *) NULL);
571 return(AcquirePolicyString(value,1));
600static inline MagickBooleanType IsPathContainsSymlink(
const char *path)
603 partial[MagickPathExtent];
611 if (path == (
const char *) NULL)
615 if (*p == *DirectorySeparator)
620 if ((offset+1) >= (ssize_t)
sizeof(partial))
622 partial[offset++]=(*p++);
623 partial[offset]=
'\0';
628 component[MagickPathExtent];
636 while ((*p !=
'\0') && (*p != *DirectorySeparator) &&
637 ((i+1) < (ssize_t)
sizeof(component)))
638 component[i++]=(*p++);
645 if (*p == *DirectorySeparator)
649 if ((offset > 0) && (partial[offset-1] != *DirectorySeparator))
654 if ((offset+1) >= (ssize_t)
sizeof(partial))
656 partial[offset++]=(*DirectorySeparator);
657 partial[offset]=
'\0';
662 if ((offset+i) >= (ssize_t)
sizeof(partial))
664 (void) memcpy(partial+offset,component,i);
666 partial[offset]=
'\0';
672 if (is_symlink_utf8(partial) != MagickFalse)
678 if (*p == *DirectorySeparator)
684MagickExport MagickBooleanType IsPathAuthorized(
const PolicyRights rights,
687 MagickBooleanType symlink_follow_allowed = IsRightsAuthorizedByName(
688 SystemPolicyDomain,
"symlink",rights,
"follow");
689 MagickBooleanType status =
690 ((IsRightsAuthorized(PathPolicyDomain,rights,path) != MagickFalse) &&
691 ((symlink_follow_allowed != MagickFalse) ||
692 (is_symlink_utf8(path) == MagickFalse))) ? MagickTrue : MagickFalse;
693 if ((status != MagickFalse) && (symlink_follow_allowed == MagickFalse))
695 if ((is_symlink_utf8(path) != MagickFalse) ||
696 (IsPathContainsSymlink(path) != MagickFalse))
699 if (status != MagickFalse)
700 status=IsFileResourceIdentityValid(path);
727static MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
729 if (policy_cache == (LinkedListInfo *) NULL)
731 (void) GetMaxMemoryRequest();
733 ActivateSemaphoreInfo(&policy_semaphore);
734 LockSemaphoreInfo(policy_semaphore);
735 if (policy_cache == (LinkedListInfo *) NULL)
736 policy_cache=AcquirePolicyCache(PolicyFilename,exception);
737 UnlockSemaphoreInfo(policy_semaphore);
739 return(policy_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
776MagickExport MagickBooleanType IsRightsAuthorizedByName(
777 const PolicyDomain domain,
const char *name,
const PolicyRights rights,
781 *canonical_directory = (
char *) NULL,
782 *canonical_path = (
char *) NULL,
783 *canonical_candidate = (
char *) NULL,
784 directory[MagickPathExtent],
785 filename[MagickPathExtent];
794 canonical_matched_any = MagickFalse,
795 matched_any = MagickFalse,
796 paths_provisioned = MagickFalse,
800 canonical_allowed_accumulator = AllPolicyRights,
801 effective_rights = AllPolicyRights;
806 if ((GetLogEventMask() & PolicyEvent) != 0)
807 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
808 "Domain: %s; name: %s; rights=%s; pattern=\"%s\"; ...",
809 CommandOptionToMnemonic(MagickPolicyDomainOptions,domain),
810 name == (
const char *) NULL ?
"undefined" : name,
811 CommandOptionToMnemonic(MagickPolicyRightsOptions,rights),
812 pattern == (
const char *) NULL ?
"undefined" : pattern);
813 exception=AcquireExceptionInfo();
814 status=IsPolicyCacheInstantiated(exception);
815 exception=DestroyExceptionInfo(exception);
816 if (status == MagickFalse)
818 if ((GetLogEventMask() & PolicyEvent) != 0)
819 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
820 " authorized: true (no security policies found)");
827 LockSemaphoreInfo(policy_semaphore);
828 ResetLinkedListIterator(policy_cache);
829 p=GetHeadElementInLinkedList(policy_cache);
830 for ( ; p != (ElementInfo *) NULL; p=p->next)
833 *policy = (PolicyInfo *) p->value;
837 matched_canonical = MagickFalse;
839 if (policy->domain != domain)
841 if ((name != (
char *) NULL) && (LocaleCompare(name,policy->name) != 0))
843 match=GlobExpression(pattern,policy->pattern,MagickFalse);
844 if (policy->domain == PathPolicyDomain)
846 if (paths_provisioned == MagickFalse)
851 paths_provisioned=MagickTrue;
852 GetPathComponent(pattern,HeadPath,directory);
853 GetPathComponent(pattern,TailPath,filename);
854 canonical_directory=realpath_utf8(directory);
855 if ((canonical_directory != (
char *) NULL) && (*filename !=
'\0'))
860 length=strlen(canonical_directory)+strlen(filename)+2;
861 canonical_candidate=(
char *) AcquireCriticalMemory(length*
862 sizeof(*canonical_candidate));
863 if (canonical_candidate != (
char *) NULL)
864 (void) FormatLocaleString(canonical_candidate,length,
"%s%s%s",
865 canonical_directory,DirectorySeparator,filename);
867 canonical_path=realpath_utf8(pattern);
872 if ((canonical_directory != (
char *) NULL) && (match == MagickFalse))
873 match=GlobExpression(canonical_directory,policy->pattern,MagickFalse);
874 if ((canonical_candidate != (
char *) NULL) && (match == MagickFalse))
875 match=GlobExpression(canonical_candidate,policy->pattern,MagickFalse);
876 if ((canonical_path != (
char *) NULL) && (match == MagickFalse))
877 match=GlobExpression(canonical_path,policy->pattern,MagickFalse);
878 if ((canonical_path != (
char *) NULL) &&
879 (GlobExpression(canonical_path,policy->pattern,MagickFalse) != MagickFalse))
880 matched_canonical=MagickTrue;
882 if ((canonical_candidate != (
char *) NULL) &&
883 (GlobExpression(canonical_candidate,policy->pattern,MagickFalse) != MagickFalse))
884 matched_canonical=MagickTrue;
886 if ((canonical_directory != (
char *) NULL) &&
887 (GlobExpression(canonical_directory,policy->pattern,MagickFalse) != MagickFalse))
888 matched_canonical=MagickTrue;
890 if (match == MagickFalse)
892 matched_any=MagickTrue;
893 effective_rights=policy->rights;
894 if (matched_canonical != MagickFalse)
899 canonical_matched_any=MagickTrue;
900 canonical_allowed_accumulator=(PolicyRights) ((
int)
901 canonical_allowed_accumulator & (
int) policy->rights);
904 UnlockSemaphoreInfo(policy_semaphore);
905 if (canonical_directory != (
char *) NULL)
906 canonical_directory=DestroyString(canonical_directory);
907 if (canonical_candidate != (
char *) NULL)
908 canonical_candidate=DestroyString(canonical_candidate);
909 if (canonical_path != (
char *) NULL)
910 canonical_path=DestroyString(canonical_path);
915 if (matched_any != MagickFalse)
917 if (((rights & ReadPolicyRights) != 0) &&
918 ((effective_rights & ReadPolicyRights) == 0))
920 if (((rights & WritePolicyRights) != 0) &&
921 ((effective_rights & WritePolicyRights) == 0))
923 if (((rights & ExecutePolicyRights) != 0) &&
924 ((effective_rights & ExecutePolicyRights) == 0))
930 if (canonical_matched_any != MagickFalse)
932 PolicyRights canonical_denied_mask = (PolicyRights) ((
int)
933 AllPolicyRights & (
int) ~canonical_allowed_accumulator);
934 if ((canonical_denied_mask & rights) != 0)
937 if ((GetLogEventMask() & PolicyEvent) != 0)
938 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
939 " authorized: %s",status == MagickFalse ?
"false" :
"true");
943MagickExport MagickBooleanType IsRightsAuthorized(
const PolicyDomain domain,
944 const PolicyRights rights,
const char *pattern)
946 return(IsRightsAuthorizedByName(domain,(
const char *) NULL,rights,pattern));
973MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
974 ExceptionInfo *exception)
992 if (file == (
const FILE *) NULL)
994 policy_info=GetPolicyInfoList(
"*",&number_policies,exception);
995 if (policy_info == (
const PolicyInfo **) NULL)
997 path=(
const char *) NULL;
998 for (i=0; i < (ssize_t) number_policies; i++)
1000 if (policy_info[i]->stealth != MagickFalse)
1002 if (((path == (
const char *) NULL) ||
1003 (LocaleCompare(path,policy_info[i]->path) != 0)) &&
1004 (policy_info[i]->path != (
char *) NULL))
1005 (void) FormatLocaleFile(file,
"\nPath: %s\n",policy_info[i]->path);
1006 path=policy_info[i]->path;
1007 domain=CommandOptionToMnemonic(MagickPolicyDomainOptions,
1008 policy_info[i]->domain);
1009 (void) FormatLocaleFile(file,
" Policy: %s\n",domain);
1010 if ((policy_info[i]->domain == CachePolicyDomain) ||
1011 (policy_info[i]->domain == ResourcePolicyDomain) ||
1012 (policy_info[i]->domain == SystemPolicyDomain))
1014 if (policy_info[i]->name != (
char *) NULL)
1015 (void) FormatLocaleFile(file,
" name: %s\n",policy_info[i]->name);
1016 if (policy_info[i]->value != (
char *) NULL)
1017 (void) FormatLocaleFile(file,
" value: %s\n",policy_info[i]->value);
1021 (void) FormatLocaleFile(file,
" rights: ");
1022 if (policy_info[i]->rights == NoPolicyRights)
1023 (void) FormatLocaleFile(file,
"None ");
1024 if ((policy_info[i]->rights & ReadPolicyRights) != 0)
1025 (void) FormatLocaleFile(file,
"Read ");
1026 if ((policy_info[i]->rights & WritePolicyRights) != 0)
1027 (void) FormatLocaleFile(file,
"Write ");
1028 if ((policy_info[i]->rights & ExecutePolicyRights) != 0)
1029 (void) FormatLocaleFile(file,
"Execute ");
1030 (void) FormatLocaleFile(file,
"\n");
1031 if (policy_info[i]->pattern != (
char *) NULL)
1032 (void) FormatLocaleFile(file,
" pattern: %s\n",
1033 policy_info[i]->pattern);
1036 policy_info=(
const PolicyInfo **) RelinquishMagickMemory((
void *)
1038 (void) fflush(file);
1072static MagickBooleanType LoadPolicyCache(LinkedListInfo *cache,
1073 const char *policy,
const char *filename,
const size_t depth,
1074 ExceptionInfo *exception)
1077 keyword[MagickPathExtent],
1095 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1096 "Loading policy file \"%s\" ...",filename);
1097 if (policy == (
char *) NULL)
1098 return(MagickFalse);
1100 policy_info=(PolicyInfo *) NULL;
1101 token=AcquirePolicyString(policy,MagickPathExtent);
1102 extent=strlen(token)+MagickPathExtent;
1103 for (q=policy; *q !=
'\0'; )
1108 (void) GetNextToken(q,&q,extent,token);
1111 (void) CopyMagickString(keyword,token,MagickPathExtent);
1112 if (LocaleNCompare(keyword,
"<!DOCTYPE",9) == 0)
1117 while ((LocaleNCompare(q,
"]>",2) != 0) && (*q !=
'\0'))
1118 (void) GetNextToken(q,&q,extent,token);
1121 if (LocaleNCompare(keyword,
"<!--",4) == 0)
1126 while ((LocaleNCompare(q,
"->",2) != 0) && (*q !=
'\0'))
1127 (void) GetNextToken(q,&q,extent,token);
1130 if (LocaleCompare(keyword,
"<include") == 0)
1135 while (((*token !=
'/') && (*(token+1) !=
'>')) && (*q !=
'\0'))
1137 (void) CopyMagickString(keyword,token,MagickPathExtent);
1138 (void) GetNextToken(q,&q,extent,token);
1141 (void) GetNextToken(q,&q,extent,token);
1142 if (LocaleCompare(keyword,
"file") == 0)
1144 if (depth > MagickMaxRecursionDepth)
1145 (void) ThrowMagickException(exception,GetMagickModule(),
1146 ConfigureError,
"IncludeElementNestedTooDeeply",
"`%s'",token);
1150 path[MagickPathExtent],
1153 GetPathComponent(filename,HeadPath,path);
1155 (void) ConcatenateMagickString(path,DirectorySeparator,
1157 if (*token == *DirectorySeparator)
1158 (void) CopyMagickString(path,token,MagickPathExtent);
1160 (
void) ConcatenateMagickString(path,token,MagickPathExtent);
1161 file_xml=FileToXML(path,~0UL);
1162 if (file_xml != (
char *) NULL)
1164 status&=(MagickStatusType) LoadPolicyCache(cache,file_xml,
1165 path,depth+1,exception);
1166 file_xml=DestroyString(file_xml);
1173 if (LocaleCompare(keyword,
"<policy") == 0)
1178 policy_info=(PolicyInfo *) AcquireCriticalMemory(
sizeof(*policy_info));
1179 (void) memset(policy_info,0,
sizeof(*policy_info));
1180 policy_info->path=AcquirePolicyString(filename,1);
1181 policy_info->exempt=MagickFalse;
1182 policy_info->signature=MagickCoreSignature;
1185 if (policy_info == (PolicyInfo *) NULL)
1187 if ((LocaleCompare(keyword,
"/>") == 0) ||
1188 (LocaleCompare(keyword,
"</policy>") == 0))
1190 status=AppendValueToLinkedList(cache,policy_info);
1191 if (status == MagickFalse)
1192 (void) ThrowMagickException(exception,GetMagickModule(),
1193 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
1195 policy_info=(PolicyInfo *) NULL;
1198 (void) GetNextToken(q,(
const char **) NULL,extent,token);
1201 (void) GetNextToken(q,&q,extent,token);
1202 (void) GetNextToken(q,&q,extent,token);
1208 if (LocaleCompare((
char *) keyword,
"domain") == 0)
1210 policy_info->domain=(PolicyDomain) ParseCommandOption(
1211 MagickPolicyDomainOptions,MagickTrue,token);
1219 if (LocaleCompare((
char *) keyword,
"name") == 0)
1221 policy_info->name=AcquirePolicyString(token,1);
1229 if (LocaleCompare((
char *) keyword,
"pattern") == 0)
1231 policy_info->pattern=AcquirePolicyString(token,1);
1239 if (LocaleCompare((
char *) keyword,
"rights") == 0)
1241 policy_info->rights=(PolicyRights) ParseCommandOption(
1242 MagickPolicyRightsOptions,MagickTrue,token);
1250 if (LocaleCompare((
char *) keyword,
"stealth") == 0)
1252 policy_info->stealth=IsStringTrue(token);
1260 if (LocaleCompare((
char *) keyword,
"value") == 0)
1262 policy_info->value=AcquirePolicyString(token,1);
1271 token=(
char *) RelinquishMagickMemory(token);
1272 return(status != 0 ? MagickTrue : MagickFalse);
1293MagickPrivate MagickBooleanType PolicyComponentGenesis(
void)
1296 policy_semaphore=AcquireSemaphoreInfo();
1319static void *DestroyPolicyElement(
void *policy_info)
1324 p=(PolicyInfo *) policy_info;
1325 if (p->exempt == MagickFalse)
1327 if (p->value != (
char *) NULL)
1328 p->value=DestroyString(p->value);
1329 if (p->pattern != (
char *) NULL)
1330 p->pattern=DestroyString(p->pattern);
1331 if (p->name != (
char *) NULL)
1332 p->name=DestroyString(p->name);
1333 if (p->path != (
char *) NULL)
1334 p->path=DestroyString(p->path);
1336 p=(PolicyInfo *) RelinquishMagickMemory(p);
1337 return((
void *) NULL);
1340MagickPrivate
void PolicyComponentTerminus(
void)
1343 ActivateSemaphoreInfo(&policy_semaphore);
1344 LockSemaphoreInfo(policy_semaphore);
1345 if (policy_cache != (LinkedListInfo *) NULL)
1346 policy_cache=DestroyLinkedList(policy_cache,DestroyPolicyElement);
1347 UnlockSemaphoreInfo(policy_semaphore);
1348 RelinquishSemaphoreInfo(&policy_semaphore);
1378static MagickBooleanType ValidateSecurityPolicy(
const char *policy,
1379 const char *url,ExceptionInfo *exception)
1381#if defined(MAGICKCORE_XML_DELEGATE)
1388 document=xmlReadMemory(policy,(
int) strlen(policy),url,NULL,
1389 XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
1390 if (document == (xmlDocPtr) NULL)
1392 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
1393 "PolicyValidationException",
"'%s'",url);
1394 return(MagickFalse);
1396 xmlFreeDoc(document);
1405MagickExport MagickBooleanType SetMagickSecurityPolicy(
const char *policy,
1406 ExceptionInfo *exception)
1420 assert(exception != (ExceptionInfo *) NULL);
1421 if (policy == (
const char *) NULL)
1422 return(MagickFalse);
1423 if (ValidateSecurityPolicy(policy,PolicyFilename,exception) == MagickFalse)
1424 return(MagickFalse);
1425 status=LoadPolicyCache(policy_cache,policy,
"[user-policy]",0,exception);
1426 if (status == MagickFalse)
1431 user_policies=NewLinkedList(0);
1432 status=LoadPolicyCache(user_policies,policy,
"[user-policy]",0,exception);
1433 if (status == MagickFalse)
1435 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1436 return(MagickFalse);
1438 ResetLinkedListIterator(user_policies);
1439 p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
1440 while (p != (PolicyInfo *) NULL)
1442 if ((p->name != (
char *) NULL) && (p->value != (
char *) NULL))
1443 (void) SetMagickSecurityPolicyValue(p->domain,p->name,p->value,exception);
1444 p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
1446 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1483MagickExport MagickBooleanType SetMagickSecurityPolicyValue(
1484 const PolicyDomain domain,
const char *name,
const char *value,
1485 ExceptionInfo *exception)
1487 magick_unreferenced(exception);
1488 assert(exception != (ExceptionInfo *) NULL);
1489 if ((name == (
const char *) NULL) || (value == (
const char *) NULL))
1490 return(MagickFalse);
1493 case CachePolicyDomain:
1495 if (LocaleCompare(name,
"memory-map") == 0)
1497 if (LocaleCompare(value,
"anonymous") != 0)
1498 return(MagickFalse);
1499 ResetCacheAnonymousMemory();
1500 ResetStreamAnonymousMemory();
1505 case ResourcePolicyDomain:
1510 type=ParseCommandOption(MagickResourceOptions,MagickFalse,name);
1516 limit=MagickResourceInfinity;
1517 if (LocaleCompare(
"unlimited",value) != 0)
1518 limit=StringToMagickSizeType(value,100.0);
1519 if ((ResourceType) type == TimeResource)
1520 limit=(MagickSizeType) ParseMagickTimeToLive(value);
1521 return(SetMagickResourceLimit((ResourceType) type,limit));
1525 case SystemPolicyDomain:
1527 if (LocaleCompare(name,
"max-memory-request") == 0)
1532 limit=MagickResourceInfinity;
1533 if (LocaleCompare(
"unlimited",value) != 0)
1534 limit=StringToMagickSizeType(value,100.0);
1535 SetMaxMemoryRequest(limit);
1538 if (LocaleCompare(name,
"max-profile-size") == 0)
1543 limit=MagickResourceInfinity;
1544 if (LocaleCompare(
"unlimited",value) != 0)
1545 limit=StringToMagickSizeType(value,100.0);
1546 SetMaxProfileSize(limit);
1549 if (LocaleCompare(name,
"memory-map") == 0)
1551 if (LocaleCompare(value,
"anonymous") != 0)
1552 return(MagickFalse);
1553 ResetVirtualAnonymousMemory();
1556 if (LocaleCompare(name,
"precision") == 0)
1561 limit=StringToInteger(value);
1562 SetMagickPrecision(limit);
1567 case CoderPolicyDomain:
1568 case DelegatePolicyDomain:
1569 case FilterPolicyDomain:
1570 case ModulePolicyDomain:
1571 case PathPolicyDomain:
1575 return(MagickFalse);