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"
129 { UndefinedPolicyDomain, UndefinedPolicyRights, (
const char *) NULL,
130 (
const char *) NULL, (
const char *) NULL }
142static MagickBooleanType
144 LoadPolicyCache(
LinkedListInfo *,
const char *,
const char *,
const size_t,
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);
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));
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);
287 policyname[MagickPathExtent],
300 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
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);
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);
346 if ((domain == UndefinedPolicyDomain) || (policy->domain == domain))
347 if (LocaleCompare(policyname,policy->name) == 0)
354 (
void) SetHeadElementInLinkedList(policy_cache,p);
355 UnlockSemaphoreInfo(policy_semaphore);
386MagickExport
const PolicyInfo **GetPolicyInfoList(
const char *pattern,
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)
405 policies=(
const PolicyInfo **) AcquireQuantumMemory((
size_t)
406 GetNumberOfElementsInLinkedList(policy_cache)+1UL,
sizeof(*policies));
409 LockSemaphoreInfo(policy_semaphore);
410 p=GetHeadElementInLinkedList(policy_cache);
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);
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,
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);
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);
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));
597static MagickBooleanType IsPolicyCacheInstantiated(
ExceptionInfo *exception)
601 GetMaxMemoryRequest();
603 ActivateSemaphoreInfo(&policy_semaphore);
604 LockSemaphoreInfo(policy_semaphore);
606 policy_cache=AcquirePolicyCache(PolicyFilename,exception);
607 UnlockSemaphoreInfo(policy_semaphore);
609 return(policy_cache != (
LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
640MagickExport MagickBooleanType IsRightsAuthorized(
const PolicyDomain domain,
641 const PolicyRights rights,
const char *pattern)
655 if ((GetLogEventMask() & PolicyEvent) != 0)
656 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
657 "Domain: %s; rights=%s; pattern=\"%s\" ...",
658 CommandOptionToMnemonic(MagickPolicyDomainOptions,domain),
659 CommandOptionToMnemonic(MagickPolicyRightsOptions,rights),pattern);
660 exception=AcquireExceptionInfo();
661 policy_info=GetPolicyInfo(
"*",exception);
662 exception=DestroyExceptionInfo(exception);
665 authorized=MagickTrue;
666 LockSemaphoreInfo(policy_semaphore);
667 p=GetHeadElementInLinkedList(policy_cache);
674 if ((policy->domain == domain) &&
675 (GlobExpression(pattern,policy->pattern,MagickFalse) != MagickFalse))
677 if ((rights & ReadPolicyRights) != 0)
678 authorized=(policy->rights & ReadPolicyRights) != 0 ? MagickTrue :
680 if ((rights & WritePolicyRights) != 0)
681 authorized=(policy->rights & WritePolicyRights) != 0 ? MagickTrue :
683 if ((rights & ExecutePolicyRights) != 0)
684 authorized=(policy->rights & ExecutePolicyRights) != 0 ? MagickTrue :
689 UnlockSemaphoreInfo(policy_semaphore);
717MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
736 if (file == (
const FILE *) NULL)
738 policy_info=GetPolicyInfoList(
"*",&number_policies,exception);
739 if (policy_info == (
const PolicyInfo **) NULL)
741 path=(
const char *) NULL;
742 for (i=0; i < (ssize_t) number_policies; i++)
744 if (policy_info[i]->stealth != MagickFalse)
746 if (((path == (
const char *) NULL) ||
747 (LocaleCompare(path,policy_info[i]->path) != 0)) &&
748 (policy_info[i]->path != (
char *) NULL))
749 (void) FormatLocaleFile(file,
"\nPath: %s\n",policy_info[i]->path);
750 path=policy_info[i]->path;
751 domain=CommandOptionToMnemonic(MagickPolicyDomainOptions,
752 policy_info[i]->domain);
753 (void) FormatLocaleFile(file,
" Policy: %s\n",domain);
754 if ((policy_info[i]->domain == CachePolicyDomain) ||
755 (policy_info[i]->domain == ResourcePolicyDomain) ||
756 (policy_info[i]->domain == SystemPolicyDomain))
758 if (policy_info[i]->name != (
char *) NULL)
759 (void) FormatLocaleFile(file,
" name: %s\n",policy_info[i]->name);
760 if (policy_info[i]->value != (
char *) NULL)
761 (void) FormatLocaleFile(file,
" value: %s\n",policy_info[i]->value);
765 (void) FormatLocaleFile(file,
" rights: ");
766 if (policy_info[i]->rights == NoPolicyRights)
767 (void) FormatLocaleFile(file,
"None ");
768 if ((policy_info[i]->rights & ReadPolicyRights) != 0)
769 (void) FormatLocaleFile(file,
"Read ");
770 if ((policy_info[i]->rights & WritePolicyRights) != 0)
771 (void) FormatLocaleFile(file,
"Write ");
772 if ((policy_info[i]->rights & ExecutePolicyRights) != 0)
773 (void) FormatLocaleFile(file,
"Execute ");
774 (void) FormatLocaleFile(file,
"\n");
775 if (policy_info[i]->pattern != (
char *) NULL)
776 (void) FormatLocaleFile(file,
" pattern: %s\n",
777 policy_info[i]->pattern);
780 policy_info=(
const PolicyInfo **) RelinquishMagickMemory((
void *)
817 const char *policy,
const char *filename,
const size_t depth,
821 keyword[MagickPathExtent],
839 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
840 "Loading policy file \"%s\" ...",filename);
841 if (policy == (
char *) NULL)
845 token=AcquirePolicyString(policy,MagickPathExtent);
846 extent=strlen(token)+MagickPathExtent;
847 for (q=policy; *q !=
'\0'; )
852 (void) GetNextToken(q,&q,extent,token);
855 (void) CopyMagickString(keyword,token,MagickPathExtent);
856 if (LocaleNCompare(keyword,
"<!DOCTYPE",9) == 0)
861 while ((LocaleNCompare(q,
"]>",2) != 0) && (*q !=
'\0'))
862 (void) GetNextToken(q,&q,extent,token);
865 if (LocaleNCompare(keyword,
"<!--",4) == 0)
870 while ((LocaleNCompare(q,
"->",2) != 0) && (*q !=
'\0'))
871 (void) GetNextToken(q,&q,extent,token);
874 if (LocaleCompare(keyword,
"<include") == 0)
879 while (((*token !=
'/') && (*(token+1) !=
'>')) && (*q !=
'\0'))
881 (void) CopyMagickString(keyword,token,MagickPathExtent);
882 (void) GetNextToken(q,&q,extent,token);
885 (void) GetNextToken(q,&q,extent,token);
886 if (LocaleCompare(keyword,
"file") == 0)
888 if (depth > MagickMaxRecursionDepth)
889 (void) ThrowMagickException(exception,GetMagickModule(),
890 ConfigureError,
"IncludeElementNestedTooDeeply",
"`%s'",token);
894 path[MagickPathExtent],
897 GetPathComponent(filename,HeadPath,path);
899 (void) ConcatenateMagickString(path,DirectorySeparator,
901 if (*token == *DirectorySeparator)
902 (void) CopyMagickString(path,token,MagickPathExtent);
904 (
void) ConcatenateMagickString(path,token,MagickPathExtent);
905 file_xml=FileToXML(path,~0UL);
906 if (file_xml != (
char *) NULL)
908 status&=(MagickStatusType) LoadPolicyCache(cache,file_xml,
909 path,depth+1,exception);
910 file_xml=DestroyString(file_xml);
917 if (LocaleCompare(keyword,
"<policy") == 0)
922 policy_info=(
PolicyInfo *) AcquireCriticalMemory(
sizeof(*policy_info));
923 (void) memset(policy_info,0,
sizeof(*policy_info));
924 policy_info->path=AcquirePolicyString(filename,1);
925 policy_info->exempt=MagickFalse;
926 policy_info->signature=MagickCoreSignature;
931 if ((LocaleCompare(keyword,
"/>") == 0) ||
932 (LocaleCompare(keyword,
"</policy>") == 0))
934 status=AppendValueToLinkedList(cache,policy_info);
935 if (status == MagickFalse)
936 (void) ThrowMagickException(exception,GetMagickModule(),
937 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",
942 (void) GetNextToken(q,(
const char **) NULL,extent,token);
945 (void) GetNextToken(q,&q,extent,token);
946 (void) GetNextToken(q,&q,extent,token);
952 if (LocaleCompare((
char *) keyword,
"domain") == 0)
954 policy_info->domain=(PolicyDomain) ParseCommandOption(
955 MagickPolicyDomainOptions,MagickTrue,token);
963 if (LocaleCompare((
char *) keyword,
"name") == 0)
965 policy_info->name=AcquirePolicyString(token,1);
973 if (LocaleCompare((
char *) keyword,
"pattern") == 0)
975 policy_info->pattern=AcquirePolicyString(token,1);
983 if (LocaleCompare((
char *) keyword,
"rights") == 0)
985 policy_info->rights=(PolicyRights) ParseCommandOption(
986 MagickPolicyRightsOptions,MagickTrue,token);
994 if (LocaleCompare((
char *) keyword,
"stealth") == 0)
996 policy_info->stealth=IsStringTrue(token);
1004 if (LocaleCompare((
char *) keyword,
"value") == 0)
1006 policy_info->value=AcquirePolicyString(token,1);
1015 token=(
char *) RelinquishMagickMemory(token);
1016 return(status != 0 ? MagickTrue : MagickFalse);
1037MagickPrivate MagickBooleanType PolicyComponentGenesis(
void)
1040 policy_semaphore=AcquireSemaphoreInfo();
1063static void *DestroyPolicyElement(
void *policy_info)
1069 if (p->exempt == MagickFalse)
1071 if (p->value != (
char *) NULL)
1072 p->value=DestroyString(p->value);
1073 if (p->pattern != (
char *) NULL)
1074 p->pattern=DestroyString(p->pattern);
1075 if (p->name != (
char *) NULL)
1076 p->name=DestroyString(p->name);
1077 if (p->path != (
char *) NULL)
1078 p->path=DestroyString(p->path);
1081 return((
void *) NULL);
1084MagickPrivate
void PolicyComponentTerminus(
void)
1087 ActivateSemaphoreInfo(&policy_semaphore);
1088 LockSemaphoreInfo(policy_semaphore);
1090 policy_cache=DestroyLinkedList(policy_cache,DestroyPolicyElement);
1091 UnlockSemaphoreInfo(policy_semaphore);
1092 RelinquishSemaphoreInfo(&policy_semaphore);
1122static MagickBooleanType ValidateSecurityPolicy(
const char *policy,
1125#if defined(MAGICKCORE_XML_DELEGATE)
1132 document=xmlReadMemory(policy,(
int) strlen(policy),url,NULL,
1133 XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
1134 if (document == (xmlDocPtr) NULL)
1136 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
1137 "PolicyValidationException",
"'%s'",url);
1138 return(MagickFalse);
1140 xmlFreeDoc(document);
1149MagickExport MagickBooleanType SetMagickSecurityPolicy(
const char *policy,
1165 if (policy == (
const char *) NULL)
1166 return(MagickFalse);
1167 if (ValidateSecurityPolicy(policy,PolicyFilename,exception) == MagickFalse)
1168 return(MagickFalse);
1169 status=LoadPolicyCache(policy_cache,policy,
"[user-policy]",0,exception);
1170 if (status == MagickFalse)
1175 user_policies=NewLinkedList(0);
1176 status=LoadPolicyCache(user_policies,policy,
"[user-policy]",0,exception);
1177 if (status == MagickFalse)
1179 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1180 return(MagickFalse);
1182 ResetLinkedListIterator(user_policies);
1183 p=(
PolicyInfo *) GetNextValueInLinkedList(user_policies);
1186 if ((p->name != (
char *) NULL) && (p->value != (
char *) NULL))
1187 (void) SetMagickSecurityPolicyValue(p->domain,p->name,p->value,exception);
1188 p=(
PolicyInfo *) GetNextValueInLinkedList(user_policies);
1190 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1227MagickExport MagickBooleanType SetMagickSecurityPolicyValue(
1228 const PolicyDomain domain,
const char *name,
const char *value,
1231 magick_unreferenced(exception);
1233 if ((name == (
const char *) NULL) || (value == (
const char *) NULL))
1234 return(MagickFalse);
1237 case CachePolicyDomain:
1239 if (LocaleCompare(name,
"memory-map") == 0)
1241 if (LocaleCompare(value,
"anonymous") != 0)
1242 return(MagickFalse);
1243 ResetCacheAnonymousMemory();
1244 ResetStreamAnonymousMemory();
1249 case ResourcePolicyDomain:
1254 type=ParseCommandOption(MagickResourceOptions,MagickFalse,name);
1260 limit=MagickResourceInfinity;
1261 if (LocaleCompare(
"unlimited",value) != 0)
1262 limit=StringToMagickSizeType(value,100.0);
1263 if ((ResourceType) type == TimeResource)
1264 limit=(MagickSizeType) ParseMagickTimeToLive(value);
1265 return(SetMagickResourceLimit((ResourceType) type,limit));
1269 case SystemPolicyDomain:
1271 if (LocaleCompare(name,
"max-memory-request") == 0)
1276 limit=MagickResourceInfinity;
1277 if (LocaleCompare(
"unlimited",value) != 0)
1278 limit=StringToMagickSizeType(value,100.0);
1279 SetMaxMemoryRequest(limit);
1282 if (LocaleCompare(name,
"max-profile-size") == 0)
1287 limit=MagickResourceInfinity;
1288 if (LocaleCompare(
"unlimited",value) != 0)
1289 limit=StringToMagickSizeType(value,100.0);
1290 SetMaxProfileSize(limit);
1293 if (LocaleCompare(name,
"memory-map") == 0)
1295 if (LocaleCompare(value,
"anonymous") != 0)
1296 return(MagickFalse);
1297 ResetVirtualAnonymousMemory();
1300 if (LocaleCompare(name,
"precision") == 0)
1305 limit=StringToInteger(value);
1306 SetMagickPrecision(limit);
1311 case CoderPolicyDomain:
1312 case DelegatePolicyDomain:
1313 case FilterPolicyDomain:
1314 case ModulePolicyDomain:
1315 case PathPolicyDomain:
1319 return(MagickFalse);