MagickCore 6.9.13-49
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
policy.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% PPPP OOO L IIIII CCCC Y Y %
6% P P O O L I C Y Y %
7% PPPP O O L I C Y %
8% P O O L I C Y %
9% P OOO LLLLL IIIII CCCC Y %
10% %
11% %
12% MagickCore Policy Methods %
13% %
14% Software Design %
15% Cristy %
16% July 1992 %
17% %
18% %
19% Copyright 1999 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% https://imagemagick.org/license/ %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35*/
36
37/*
38 Include declarations.
39*/
40#include "magick/studio.h"
41#include "magick/cache-private.h"
42#include "magick/client.h"
43#include "magick/configure.h"
44#include "magick/exception.h"
45#include "magick/exception-private.h"
46#include "magick/hashmap-private.h"
47#include "magick/locale_.h"
48#include "magick/magick-private.h"
49#include "magick/memory_.h"
50#include "magick/memory-private.h"
51#include "magick/monitor.h"
52#include "magick/monitor-private.h"
53#include "magick/option.h"
54#include "magick/policy.h"
55#include "magick/policy-private.h"
56#include "magick/resource_.h"
57#include "magick/semaphore.h"
58#include "magick/stream-private.h"
59#include "magick/string_.h"
60#include "magick/string-private.h"
61#include "magick/timer-private.h"
62#include "magick/token.h"
63#include "magick/utility.h"
64#include "magick/utility-private.h"
65#include "magick/xml-tree.h"
66#include "magick/xml-tree-private.h"
67#if defined(MAGICKCORE_XML_DELEGATE)
68# include <libxml/parser.h>
69# include <libxml/tree.h>
70#endif
71
72/*
73 Define declarations.
74*/
75#define PolicyFilename "policy.xml"
76
77/*
78 Typedef declarations.
79*/
81{
82 char
83 *path;
84
85 PolicyDomain
86 domain;
87
88 PolicyRights
89 rights;
90
91 char
92 *name,
93 *pattern,
94 *value;
95
96 MagickBooleanType
97 exempt,
98 stealth,
99 debug;
100
102 *semaphore;
103
104 size_t
105 signature;
106};
107
108typedef struct _PolicyMapInfo
109{
110 const PolicyDomain
111 domain;
112
113 const PolicyRights
114 rights;
115
116 const char
117 *name,
118 *pattern,
119 *value;
120} PolicyMapInfo;
121
122/*
123 Static declarations.
124*/
125static const PolicyMapInfo
126 PolicyMap[] =
127 {
128 { UndefinedPolicyDomain, UndefinedPolicyRights, (const char *) NULL,
129 (const char *) NULL, (const char *) NULL }
130 };
131
132static LinkedListInfo
133 *policy_cache = (LinkedListInfo *) NULL;
134
135static SemaphoreInfo
136 *policy_semaphore = (SemaphoreInfo *) NULL;
137
138/*
139 Forward declarations.
140*/
141static MagickBooleanType
142 IsPolicyCacheInstantiated(ExceptionInfo *),
143 LoadPolicyCache(LinkedListInfo *,const char *,const char *,const size_t,
144 ExceptionInfo *),
145 SetMagickSecurityPolicyValue(const PolicyDomain,const char *,const char *,
146 ExceptionInfo *);
147
148/*
149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150% %
151% %
152% %
153% A c q u i r e P o l i c y C a c h e %
154% %
155% %
156% %
157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158%
159% AcquirePolicyCache() caches one or more policy configurations which provides
160% a mapping between policy attributes and a policy name.
161%
162% The format of the AcquirePolicyCache method is:
163%
164% LinkedListInfo *AcquirePolicyCache(const char *filename,
165% ExceptionInfo *exception)
166%
167% A description of each parameter follows:
168%
169% o filename: the policy configuration file name.
170%
171% o exception: return any errors or warnings in this structure.
172%
173*/
174static LinkedListInfo *AcquirePolicyCache(const char *filename,
175 ExceptionInfo *exception)
176{
177 LinkedListInfo
178 *cache;
179
180 MagickStatusType
181 status;
182
183 ssize_t
184 i;
185
186 /*
187 Load external policy map.
188 */
189 cache=NewLinkedList(0);
190 if (cache == (LinkedListInfo *) NULL)
191 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
192 status=MagickTrue;
193#if MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
194 magick_unreferenced(filename);
195 status=LoadPolicyCache(cache,ZeroConfigurationPolicy,"[zero-configuration]",0,
196 exception);
197 if (status == MagickFalse)
198 CatchException(exception);
199#else
200 {
201 const StringInfo
202 *option;
203
204 LinkedListInfo
205 *options;
206
207 options=GetConfigureOptions(filename,exception);
208 option=(const StringInfo *) GetNextValueInLinkedList(options);
209 while (option != (const StringInfo *) NULL)
210 {
211 status&=LoadPolicyCache(cache,(const char *) GetStringInfoDatum(option),
212 GetStringInfoPath(option),0,exception);
213 if (status == MagickFalse)
214 CatchException(exception);
215 option=(const StringInfo *) GetNextValueInLinkedList(options);
216 }
217 options=DestroyConfigureOptions(options);
218 }
219#endif
220 /*
221 Load built-in policy map.
222 */
223 for (i=0; i < (ssize_t) (sizeof(PolicyMap)/sizeof(*PolicyMap)); i++)
224 {
225 const PolicyMapInfo
226 *p;
227
228 PolicyInfo
229 *policy_info;
230
231 p=PolicyMap+i;
232 policy_info=(PolicyInfo *) AcquireMagickMemory(sizeof(*policy_info));
233 if (policy_info == (PolicyInfo *) NULL)
234 {
235 (void) ThrowMagickException(exception,GetMagickModule(),
236 ResourceLimitError,"MemoryAllocationFailed","`%s'",
237 p->name == (char *) NULL ? "" : p->name);
238 CatchException(exception);
239 status=MagickFalse;
240 continue;
241 }
242 (void) memset(policy_info,0,sizeof(*policy_info));
243 policy_info->path=(char *) "[built-in]";
244 policy_info->domain=p->domain;
245 policy_info->rights=p->rights;
246 policy_info->name=(char *) p->name;
247 policy_info->pattern=(char *) p->pattern;
248 policy_info->value=(char *) p->value;
249 policy_info->exempt=MagickTrue;
250 policy_info->signature=MagickCoreSignature;
251 status&=AppendValueToLinkedList(cache,policy_info);
252 if (status == MagickFalse)
253 {
254 (void) ThrowMagickException(exception,GetMagickModule(),
255 ResourceLimitError,"MemoryAllocationFailed","`%s'",
256 p->name == (char *) NULL ? "" : p->name);
257 CatchException(exception);
258 }
259 }
260 if (status == MagickFalse)
261 CatchException(exception);
262 return(cache);
263}
264
265/*
266%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267% %
268% %
269% %
270+ G e t P o l i c y I n f o %
271% %
272% %
273% %
274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275%
276% GetPolicyInfo() searches the policy list for the specified name and if found
277% returns attributes for that policy.
278%
279% The format of the GetPolicyInfo method is:
280%
281% PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
282%
283% A description of each parameter follows:
284%
285% o name: the policy name.
286%
287% o exception: return any errors or warnings in this structure.
288%
289*/
290static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
291{
292 char
293 policyname[MagickPathExtent],
294 *q;
295
296 ElementInfo
297 *p;
298
299 PolicyDomain
300 domain;
301
302 PolicyInfo
303 *policy;
304
305 assert(exception != (ExceptionInfo *) NULL);
306 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
307 return((PolicyInfo *) NULL);
308 /*
309 Strip names of whitespace.
310 */
311 *policyname='\0';
312 if (name != (const char *) NULL)
313 (void) CopyMagickString(policyname,name,MagickPathExtent);
314 for (q=policyname; *q != '\0'; q++)
315 {
316 if (isspace((int) ((unsigned char) *q)) == 0)
317 continue;
318 (void) CopyMagickString(q,q+1,MagickPathExtent);
319 q--;
320 }
321 /*
322 Strip domain from policy name (e.g. resource:map).
323 */
324 domain=UndefinedPolicyDomain;
325 for (q=policyname; *q != '\0'; q++)
326 {
327 if (*q != ':')
328 continue;
329 *q='\0';
330 domain=(PolicyDomain) ParseCommandOption(MagickPolicyDomainOptions,
331 MagickTrue,policyname);
332 (void) CopyMagickString(policyname,q+1,MagickPathExtent);
333 break;
334 }
335 /*
336 Search for policy tag.
337 */
338 policy=(PolicyInfo *) NULL;
339 LockSemaphoreInfo(policy_semaphore);
340 ResetLinkedListIterator(policy_cache);
341 p=GetHeadElementInLinkedList(policy_cache);
342 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
343 {
344 UnlockSemaphoreInfo(policy_semaphore);
345 if (p != (ElementInfo *) NULL)
346 policy=(PolicyInfo *) p->value;
347 return(policy);
348 }
349 while (p != (ElementInfo *) NULL)
350 {
351 policy=(PolicyInfo *) p->value;
352 if ((domain == UndefinedPolicyDomain) || (policy->domain == domain))
353 if (LocaleCompare(policyname,policy->name) == 0)
354 break;
355 p=p->next;
356 }
357 if (p == (ElementInfo *) NULL)
358 policy=(PolicyInfo *) NULL;
359 else
360 (void) SetHeadElementInLinkedList(policy_cache,p);
361 UnlockSemaphoreInfo(policy_semaphore);
362 return(policy);
363}
364
365/*
366%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367% %
368% %
369% %
370% G e t P o l i c y I n f o L i s t %
371% %
372% %
373% %
374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375%
376% GetPolicyInfoList() returns any policies that match the specified pattern.
377%
378% The format of the GetPolicyInfoList function is:
379%
380% const PolicyInfo **GetPolicyInfoList(const char *pattern,
381% size_t *number_policies,ExceptionInfo *exception)
382%
383% A description of each parameter follows:
384%
385% o pattern: Specifies a pointer to a text string containing a pattern.
386%
387% o number_policies: returns the number of policies in the list.
388%
389% o exception: return any errors or warnings in this structure.
390%
391*/
392MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
393 size_t *number_policies,ExceptionInfo *exception)
394{
395 const PolicyInfo
396 **policies;
397
398 const PolicyInfo
399 *p;
400
401 ssize_t
402 i;
403
404 /*
405 Allocate policy list.
406 */
407 assert(pattern != (char *) NULL);
408 assert(number_policies != (size_t *) NULL);
409 if (IsEventLogging() != MagickFalse)
410 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
411 *number_policies=0;
412 p=GetPolicyInfo("*",exception);
413 if (p == (const PolicyInfo *) NULL)
414 return((const PolicyInfo **) NULL);
415 policies=(const PolicyInfo **) AcquireQuantumMemory((size_t)
416 GetNumberOfElementsInLinkedList(policy_cache)+1UL,sizeof(*policies));
417 if (policies == (const PolicyInfo **) NULL)
418 return((const PolicyInfo **) NULL);
419 /*
420 Generate policy list.
421 */
422 LockSemaphoreInfo(policy_semaphore);
423 ResetLinkedListIterator(policy_cache);
424 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
425 for (i=0; p != (const PolicyInfo *) NULL; )
426 {
427 if ((p->stealth == MagickFalse) &&
428 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
429 policies[i++]=p;
430 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
431 }
432 UnlockSemaphoreInfo(policy_semaphore);
433 policies[i]=(PolicyInfo *) NULL;
434 *number_policies=(size_t) i;
435 return(policies);
436}
437
438/*
439%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440% %
441% %
442% %
443% G e t P o l i c y L i s t %
444% %
445% %
446% %
447%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448%
449% GetPolicyList() returns any policies that match the specified pattern.
450%
451% The format of the GetPolicyList function is:
452%
453% char **GetPolicyList(const char *pattern,size_t *number_policies,
454% ExceptionInfo *exception)
455%
456% A description of each parameter follows:
457%
458% o pattern: a pointer to a text string containing a pattern.
459%
460% o number_policies: returns the number of policies in the list.
461%
462% o exception: return any errors or warnings in this structure.
463%
464*/
465
466static char *AcquirePolicyString(const char *source,const size_t pad)
467{
468 char
469 *destination;
470
471 size_t
472 length;
473
474 length=0;
475 if (source != (char *) NULL)
476 length+=strlen(source);
477 destination=(char *) NULL;
478 if (~length >= pad)
479 destination=(char *) AcquireMagickMemory((length+pad)*sizeof(*destination));
480 if (destination == (char *) NULL)
481 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
482 if (source != (char *) NULL)
483 (void) memcpy(destination,source,length*sizeof(*destination));
484 destination[length]='\0';
485 return(destination);
486}
487
488MagickExport char **GetPolicyList(const char *pattern,size_t *number_policies,
489 ExceptionInfo *exception)
490{
491 char
492 **policies;
493
494 const PolicyInfo
495 *p;
496
497 ssize_t
498 i;
499
500 /*
501 Allocate policy list.
502 */
503 assert(pattern != (char *) NULL);
504 assert(number_policies != (size_t *) NULL);
505 if (IsEventLogging() != MagickFalse)
506 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
507 *number_policies=0;
508 p=GetPolicyInfo("*",exception);
509 if (p == (const PolicyInfo *) NULL)
510 return((char **) NULL);
511 policies=(char **) AcquireQuantumMemory((size_t)
512 GetNumberOfElementsInLinkedList(policy_cache)+1UL,sizeof(*policies));
513 if (policies == (char **) NULL)
514 return((char **) NULL);
515 /*
516 Generate policy list.
517 */
518 LockSemaphoreInfo(policy_semaphore);
519 ResetLinkedListIterator(policy_cache);
520 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
521 for (i=0; p != (const PolicyInfo *) NULL; )
522 {
523 if ((p->stealth == MagickFalse) &&
524 (GlobExpression(p->name,pattern,MagickFalse) != MagickFalse))
525 policies[i++]=AcquirePolicyString(p->name,1);
526 p=(const PolicyInfo *) GetNextValueInLinkedList(policy_cache);
527 }
528 UnlockSemaphoreInfo(policy_semaphore);
529 policies[i]=(char *) NULL;
530 *number_policies=(size_t) i;
531 return(policies);
532}
533
534/*
535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536% %
537% %
538% %
539% G e t P o l i c y V a l u e %
540% %
541% %
542% %
543%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544%
545% GetPolicyValue() returns the value associated with the named policy.
546%
547% The format of the GetPolicyValue method is:
548%
549% char *GetPolicyValue(const char *name)
550%
551% A description of each parameter follows:
552%
553% o policy_info: The policy info.
554%
555*/
556MagickExport char *GetPolicyValue(const char *name)
557{
558 const char
559 *value;
560
561 const PolicyInfo
562 *policy_info;
563
564 ExceptionInfo
565 *exception;
566
567 assert(name != (const char *) NULL);
568 if (IsEventLogging() != MagickFalse)
569 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",name);
570 exception=AcquireExceptionInfo();
571 policy_info=GetPolicyInfo(name,exception);
572 exception=DestroyExceptionInfo(exception);
573 if (policy_info == (PolicyInfo *) NULL)
574 return((char *) NULL);
575 value=policy_info->value;
576 if ((value == (const char *) NULL) || (*value == '\0'))
577 return((char *) NULL);
578 return(AcquirePolicyString(value,1));
579}
580
581/*
582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583% %
584% %
585% %
586+ I s P o l i c y C a c h e I n s t a n t i a t e d %
587% %
588% %
589% %
590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591%
592% IsPolicyCacheInstantiated() determines if the policy list is instantiated.
593% If not, it instantiates the list and returns it.
594%
595% The format of the IsPolicyInstantiated method is:
596%
597% MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
598%
599% A description of each parameter follows.
600%
601% o exception: return any errors or warnings in this structure.
602%
603*/
604static MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
605{
606 if (policy_cache == (LinkedListInfo *) NULL)
607 {
608 (void) GetMaxMemoryRequest(); /* avoid OMP deadlock */
609 if (policy_semaphore == (SemaphoreInfo *) NULL)
610 ActivateSemaphoreInfo(&policy_semaphore);
611 LockSemaphoreInfo(policy_semaphore);
612 if (policy_cache == (LinkedListInfo *) NULL)
613 policy_cache=AcquirePolicyCache(PolicyFilename,exception);
614 UnlockSemaphoreInfo(policy_semaphore);
615 }
616 return(policy_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
617}
618
619/*
620%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
621% %
622% %
623% %
624% I s R i g h t s A u t h o r i z e d %
625% %
626% %
627% %
628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629%
630% IsRightsAuthorized() returns MagickTrue if the policy authorizes the
631% requested rights for the specified domain.
632%
633% The format of the IsRightsAuthorized method is:
634%
635% MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
636% const PolicyRights rights,const char *pattern)
637%
638% A description of each parameter follows:
639%
640% o domain: the policy domain.
641%
642% o rights: the policy rights.
643%
644% o pattern: the pattern.
645%
646*/
647
648MagickPrivate MagickBooleanType IsRightsAuthorizedByName(
649 const PolicyDomain domain,const char *name,const PolicyRights rights,
650 const char *pattern)
651{
652 char
653 *canonical_directory = (char *) NULL,
654 *canonical_path = (char *) NULL,
655 *canonical_candidate = (char *) NULL,
656 directory[MagickPathExtent],
657 filename[MagickPathExtent];
658
659 const PolicyInfo
660 **policies = (const PolicyInfo **) NULL;
661
662 ExceptionInfo
663 *exception;
664
665 MagickBooleanType
666 matched_any = MagickFalse,
667 paths_provisioned = MagickFalse;
668
669 PolicyRights
670 effective_rights = AllPolicyRights; /* rights authorized unless denied */
671
672 size_t
673 count = 0;
674
675 ssize_t
676 i;
677
678 if ((GetLogEventMask() & PolicyEvent) != 0)
679 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
680 "Domain: %s; rights=%s; pattern=\"%s\" ...",
681 CommandOptionToMnemonic(MagickPolicyDomainOptions,domain),
682 CommandOptionToMnemonic(MagickPolicyRightsOptions,rights),pattern);
683 /*
684 Load policies.
685 */
686 exception=AcquireExceptionInfo();
687 policies=GetPolicyInfoList("*",&count,exception);
688 exception=DestroyExceptionInfo(exception);
689 if (policies == (const PolicyInfo **) NULL)
690 return(MagickTrue);
691 /*
692 Evaluate policies in order; last match wins.
693 */
694 for (i=0; i < (ssize_t) count; i++)
695 {
696 const PolicyInfo
697 *policy = policies[i];
698
699 MagickBooleanType
700 match = MagickFalse;
701
702 if (policy->domain != domain)
703 continue;
704 if ((name != (char *) NULL) && (LocaleCompare(name,policy->name) != 0))
705 continue;
706 match=GlobExpression(pattern,policy->pattern,MagickFalse);
707 if (policy->domain == PathPolicyDomain)
708 {
709 if (paths_provisioned == MagickFalse)
710 {
711 /*
712 Generate directory, basename, and canonical path.
713 */
714 paths_provisioned=MagickTrue;
715 GetPathComponent(pattern,HeadPath,directory);
716 GetPathComponent(pattern,TailPath,filename);
717 canonical_directory=realpath_utf8(directory);
718 if ((canonical_directory != (char *) NULL) && (*filename != '\0'))
719 {
720 size_t
721 length;
722
723 length=strlen(canonical_directory)+strlen(filename)+2;
724 canonical_candidate=(char *) AcquireQuantumMemory(length,
725 sizeof(*canonical_candidate));
726 if (canonical_candidate != (char *) NULL)
727 (void) FormatLocaleString(canonical_candidate,length,"%s%s%s",
728 canonical_directory,DirectorySeparator,filename);
729 }
730 canonical_path=realpath_utf8(pattern);
731 }
732 /*
733 Match against directory, basename, and canonical path.
734 */
735 if ((canonical_directory != (char *) NULL) && (match == MagickFalse))
736 match=GlobExpression(canonical_directory,policy->pattern,MagickFalse);
737 if ((canonical_candidate != (char *) NULL) && (match == MagickFalse))
738 match=GlobExpression(canonical_candidate,policy->pattern,MagickFalse);
739 if ((canonical_path != (char *) NULL) && (match == MagickFalse))
740 match=GlobExpression(canonical_path,policy->pattern,MagickFalse);
741 }
742 if (match == MagickFalse)
743 continue;
744 matched_any=MagickTrue;
745 effective_rights=policy->rights;
746 }
747 policies=(const PolicyInfo **) RelinquishMagickMemory((void *) policies);
748 if (canonical_directory != (char *) NULL)
749 canonical_directory=DestroyString(canonical_directory);
750 if (canonical_candidate != (char *) NULL)
751 canonical_candidate=DestroyString(canonical_candidate);
752 if (canonical_path != (char *) NULL)
753 canonical_path=DestroyString(canonical_path);
754 /*
755 Is rights authorized?
756 */
757 if (matched_any == MagickFalse)
758 return(MagickTrue);
759 if (((rights & ReadPolicyRights) != 0) &&
760 ((effective_rights & ReadPolicyRights) == 0))
761 return(MagickFalse);
762 if (((rights & WritePolicyRights) != 0) &&
763 ((effective_rights & WritePolicyRights) == 0))
764 return(MagickFalse);
765 if (((rights & ExecutePolicyRights) != 0) &&
766 ((effective_rights & ExecutePolicyRights) == 0))
767 return(MagickFalse);
768 return(MagickTrue);
769}
770
771MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
772 const PolicyRights rights,const char *pattern)
773{
774 return(IsRightsAuthorizedByName(domain,(const char *) NULL,rights,pattern));
775}
776
777/*
778%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779% %
780% %
781% %
782% L i s t P o l i c y I n f o %
783% %
784% %
785% %
786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
787%
788% ListPolicyInfo() lists policies to the specified file.
789%
790% The format of the ListPolicyInfo method is:
791%
792% MagickBooleanType ListPolicyInfo(FILE *file,ExceptionInfo *exception)
793%
794% A description of each parameter follows.
795%
796% o file: List policy names to this file handle.
797%
798% o exception: return any errors or warnings in this structure.
799%
800*/
801MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
802 ExceptionInfo *exception)
803{
804 const char
805 *path,
806 *domain;
807
808 const PolicyInfo
809 **policy_info;
810
811 ssize_t
812 i;
813
814 size_t
815 number_policies;
816
817 /*
818 List name and attributes of each policy in the list.
819 */
820 if (file == (const FILE *) NULL)
821 file=stdout;
822 policy_info=GetPolicyInfoList("*",&number_policies,exception);
823 if (policy_info == (const PolicyInfo **) NULL)
824 return(MagickFalse);
825 path=(const char *) NULL;
826 for (i=0; i < (ssize_t) number_policies; i++)
827 {
828 if (policy_info[i]->stealth != MagickFalse)
829 continue;
830 if (((path == (const char *) NULL) ||
831 (LocaleCompare(path,policy_info[i]->path) != 0)) &&
832 (policy_info[i]->path != (char *) NULL))
833 (void) FormatLocaleFile(file,"\nPath: %s\n",policy_info[i]->path);
834 path=policy_info[i]->path;
835 domain=CommandOptionToMnemonic(MagickPolicyDomainOptions,
836 policy_info[i]->domain);
837 (void) FormatLocaleFile(file," Policy: %s\n",domain);
838 if ((policy_info[i]->domain == CachePolicyDomain) ||
839 (policy_info[i]->domain == ResourcePolicyDomain) ||
840 (policy_info[i]->domain == SystemPolicyDomain))
841 {
842 if (policy_info[i]->name != (char *) NULL)
843 (void) FormatLocaleFile(file," name: %s\n",policy_info[i]->name);
844 if (policy_info[i]->value != (char *) NULL)
845 (void) FormatLocaleFile(file," value: %s\n",policy_info[i]->value);
846 }
847 else
848 {
849 (void) FormatLocaleFile(file," rights: ");
850 if (policy_info[i]->rights == NoPolicyRights)
851 (void) FormatLocaleFile(file,"None ");
852 if ((policy_info[i]->rights & ReadPolicyRights) != 0)
853 (void) FormatLocaleFile(file,"Read ");
854 if ((policy_info[i]->rights & WritePolicyRights) != 0)
855 (void) FormatLocaleFile(file,"Write ");
856 if ((policy_info[i]->rights & ExecutePolicyRights) != 0)
857 (void) FormatLocaleFile(file,"Execute ");
858 (void) FormatLocaleFile(file,"\n");
859 if (policy_info[i]->pattern != (char *) NULL)
860 (void) FormatLocaleFile(file," pattern: %s\n",
861 policy_info[i]->pattern);
862 }
863 }
864 policy_info=(const PolicyInfo **) RelinquishMagickMemory((void *)
865 policy_info);
866 (void) fflush(file);
867 return(MagickTrue);
868}
869
870/*
871%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
872% %
873% %
874% %
875+ L o a d P o l i c y C a c h e %
876% %
877% %
878% %
879%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880%
881% LoadPolicyCache() loads the policy configurations which provides a mapping
882% between policy attributes and a policy domain.
883%
884% The format of the LoadPolicyCache method is:
885%
886% MagickBooleanType LoadPolicyCache(LinkedListInfo *cache,const char *xml,
887% const char *filename,const size_t depth,ExceptionInfo *exception)
888%
889% A description of each parameter follows:
890%
891% o xml: The policy list in XML format.
892%
893% o filename: The policy list filename.
894%
895% o depth: depth of <include /> statements.
896%
897% o exception: return any errors or warnings in this structure.
898%
899*/
900static MagickBooleanType LoadPolicyCache(LinkedListInfo *cache,const char *xml,
901 const char *filename,const size_t depth,ExceptionInfo *exception)
902{
903 char
904 keyword[MagickPathExtent],
905 *token;
906
907 const char
908 *q;
909
910 MagickStatusType
911 status;
912
913 PolicyInfo
914 *policy_info;
915
916 size_t
917 extent;
918
919 /*
920 Load the policy map file.
921 */
922 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
923 "Loading policy file \"%s\" ...",filename);
924 if (xml == (char *) NULL)
925 return(MagickFalse);
926 status=MagickTrue;
927 policy_info=(PolicyInfo *) NULL;
928 token=AcquirePolicyString(xml,MagickPathExtent);
929 extent=strlen(token)+MagickPathExtent;
930 for (q=(const char *) xml; *q != '\0'; )
931 {
932 /*
933 Interpret XML.
934 */
935 (void) GetNextToken(q,&q,extent,token);
936 if (*token == '\0')
937 break;
938 (void) CopyMagickString(keyword,token,MagickPathExtent);
939 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
940 {
941 /*
942 Docdomain element.
943 */
944 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
945 (void) GetNextToken(q,&q,extent,token);
946 continue;
947 }
948 if (LocaleNCompare(keyword,"<!--",4) == 0)
949 {
950 /*
951 Comment element.
952 */
953 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
954 (void) GetNextToken(q,&q,extent,token);
955 continue;
956 }
957 if (LocaleCompare(keyword,"<include") == 0)
958 {
959 /*
960 Include element.
961 */
962 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
963 {
964 (void) CopyMagickString(keyword,token,MagickPathExtent);
965 (void) GetNextToken(q,&q,extent,token);
966 if (*token != '=')
967 continue;
968 (void) GetNextToken(q,&q,extent,token);
969 if (LocaleCompare(keyword,"file") == 0)
970 {
971 if (depth > MagickMaxRecursionDepth)
972 (void) ThrowMagickException(exception,GetMagickModule(),
973 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
974 else
975 {
976 char
977 path[MagickPathExtent],
978 *xml;
979
980 GetPathComponent(filename,HeadPath,path);
981 if (*path != '\0')
982 (void) ConcatenateMagickString(path,DirectorySeparator,
983 MagickPathExtent);
984 if (*token == *DirectorySeparator)
985 (void) CopyMagickString(path,token,MagickPathExtent);
986 else
987 (void) ConcatenateMagickString(path,token,MagickPathExtent);
988 xml=FileToXML(path,~0UL);
989 if (xml != (char *) NULL)
990 {
991 status&=LoadPolicyCache(cache,xml,path,depth+1,
992 exception);
993 xml=(char *) RelinquishMagickMemory(xml);
994 }
995 }
996 }
997 }
998 continue;
999 }
1000 if (LocaleCompare(keyword,"<policy") == 0)
1001 {
1002 /*
1003 Policy element.
1004 */
1005 policy_info=(PolicyInfo *) AcquireMagickMemory(sizeof(*policy_info));
1006 if (policy_info == (PolicyInfo *) NULL)
1007 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1008 (void) memset(policy_info,0,sizeof(*policy_info));
1009 policy_info->path=AcquirePolicyString(filename,1);
1010 policy_info->exempt=MagickFalse;
1011 policy_info->signature=MagickCoreSignature;
1012 continue;
1013 }
1014 if (policy_info == (PolicyInfo *) NULL)
1015 continue;
1016 if ((LocaleCompare(keyword,"/>") == 0) ||
1017 (LocaleCompare(keyword,"</policy>") == 0))
1018 {
1019 status=AppendValueToLinkedList(cache,policy_info);
1020 if (status == MagickFalse)
1021 (void) ThrowMagickException(exception,GetMagickModule(),
1022 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1023 policy_info->name);
1024 policy_info=(PolicyInfo *) NULL;
1025 continue;
1026 }
1027 (void) GetNextToken(q,(const char **) NULL,extent,token);
1028 if (*token != '=')
1029 continue;
1030 (void) GetNextToken(q,&q,extent,token);
1031 (void) GetNextToken(q,&q,extent,token);
1032 switch (*keyword)
1033 {
1034 case 'D':
1035 case 'd':
1036 {
1037 if (LocaleCompare((char *) keyword,"domain") == 0)
1038 {
1039 policy_info->domain=(PolicyDomain) ParseCommandOption(
1040 MagickPolicyDomainOptions,MagickTrue,token);
1041 break;
1042 }
1043 break;
1044 }
1045 case 'N':
1046 case 'n':
1047 {
1048 if (LocaleCompare((char *) keyword,"name") == 0)
1049 {
1050 policy_info->name=AcquirePolicyString(token,1);
1051 break;
1052 }
1053 break;
1054 }
1055 case 'P':
1056 case 'p':
1057 {
1058 if (LocaleCompare((char *) keyword,"pattern") == 0)
1059 {
1060 policy_info->pattern=AcquirePolicyString(token,1);
1061 break;
1062 }
1063 break;
1064 }
1065 case 'R':
1066 case 'r':
1067 {
1068 if (LocaleCompare((char *) keyword,"rights") == 0)
1069 {
1070 policy_info->rights=(PolicyRights) ParseCommandOption(
1071 MagickPolicyRightsOptions,MagickTrue,token);
1072 break;
1073 }
1074 break;
1075 }
1076 case 'S':
1077 case 's':
1078 {
1079 if (LocaleCompare((char *) keyword,"stealth") == 0)
1080 {
1081 policy_info->stealth=IsMagickTrue(token);
1082 break;
1083 }
1084 break;
1085 }
1086 case 'V':
1087 case 'v':
1088 {
1089 if (LocaleCompare((char *) keyword,"value") == 0)
1090 {
1091 policy_info->value=AcquirePolicyString(token,1);
1092 break;
1093 }
1094 break;
1095 }
1096 default:
1097 break;
1098 }
1099 }
1100 token=(char *) RelinquishMagickMemory(token);
1101 if (status == MagickFalse)
1102 CatchException(exception);
1103 return(status != 0 ? MagickTrue : MagickFalse);
1104}
1105
1106/*
1107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1108% %
1109% %
1110% %
1111+ P o l i c y C o m p o n e n t G e n e s i s %
1112% %
1113% %
1114% %
1115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1116%
1117% PolicyComponentGenesis() instantiates the policy component.
1118%
1119% The format of the PolicyComponentGenesis method is:
1120%
1121% MagickBooleanType PolicyComponentGenesis(void)
1122%
1123*/
1124MagickExport MagickBooleanType PolicyComponentGenesis(void)
1125{
1126 if (policy_semaphore == (SemaphoreInfo *) NULL)
1127 policy_semaphore=AllocateSemaphoreInfo();
1128 return(MagickTrue);
1129}
1130
1131/*
1132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1133% %
1134% %
1135% %
1136+ P o l i c y C o m p o n e n t T e r m i n u s %
1137% %
1138% %
1139% %
1140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1141%
1142% PolicyComponentTerminus() destroys the policy component.
1143%
1144% The format of the PolicyComponentTerminus method is:
1145%
1146% PolicyComponentTerminus(void)
1147%
1148*/
1149
1150static void *DestroyPolicyElement(void *policy_info)
1151{
1152 PolicyInfo
1153 *p;
1154
1155 p=(PolicyInfo *) policy_info;
1156 if (p->exempt == MagickFalse)
1157 {
1158 if (p->value != (char *) NULL)
1159 p->value=DestroyString(p->value);
1160 if (p->pattern != (char *) NULL)
1161 p->pattern=DestroyString(p->pattern);
1162 if (p->name != (char *) NULL)
1163 p->name=DestroyString(p->name);
1164 if (p->path != (char *) NULL)
1165 p->path=DestroyString(p->path);
1166 }
1167 p=(PolicyInfo *) RelinquishMagickMemory(p);
1168 return((void *) NULL);
1169}
1170
1171MagickExport void PolicyComponentTerminus(void)
1172{
1173 if (policy_semaphore == (SemaphoreInfo *) NULL)
1174 ActivateSemaphoreInfo(&policy_semaphore);
1175 LockSemaphoreInfo(policy_semaphore);
1176 if (policy_cache != (LinkedListInfo *) NULL)
1177 policy_cache=DestroyLinkedList(policy_cache,DestroyPolicyElement);
1178 UnlockSemaphoreInfo(policy_semaphore);
1179 DestroySemaphoreInfo(&policy_semaphore);
1180}
1181
1182/*
1183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1184% %
1185% %
1186% %
1187% S e t M a g i c k S e c u r i t y P o l i c y %
1188% %
1189% %
1190% %
1191%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1192%
1193% SetMagickSecurityPolicy() sets or restricts the ImageMagick security policy.
1194% It returns MagickFalse if the policy the policy does not parse.
1195%
1196% The format of the SetMagickSecurityPolicy method is:
1197%
1198% MagickBooleanType SetMagickSecurityPolicy(const char *policy,
1199% ExceptionInfo *exception)
1200%
1201% A description of each parameter follows:
1202%
1203% o policy: the security policy in the XML format.
1204%
1205% o exception: return any errors or warnings in this structure.
1206%
1207*/
1208
1209static MagickBooleanType ValidateSecurityPolicy(const char *policy,
1210 const char *url,ExceptionInfo *exception)
1211{
1212#if defined(MAGICKCORE_XML_DELEGATE)
1213 xmlDocPtr
1214 document;
1215
1216 /*
1217 Parse security policy.
1218 */
1219 document=xmlReadMemory(policy,(int) strlen(policy),url,NULL,
1220 XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
1221 if (document == (xmlDocPtr) NULL)
1222 {
1223 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
1224 "PolicyValidationException","'%s'",url);
1225 return(MagickFalse);
1226 }
1227 xmlFreeDoc(document);
1228#else
1229 (void) policy;
1230 (void) url;
1231 (void) exception;
1232#endif
1233 return(MagickTrue);
1234}
1235
1236MagickExport MagickBooleanType SetMagickSecurityPolicy(const char *policy,
1237 ExceptionInfo *exception)
1238{
1239 MagickBooleanType
1240 status;
1241
1242 LinkedListInfo
1243 *user_policies;
1244
1245 PolicyInfo
1246 *p;
1247
1248 /*
1249 Load user policies.
1250 */
1251 assert(exception != (ExceptionInfo *) NULL);
1252 if (policy == (const char *) NULL)
1253 return(MagickFalse);
1254 if (ValidateSecurityPolicy(policy,PolicyFilename,exception) == MagickFalse)
1255 return(MagickFalse);
1256 status=LoadPolicyCache(policy_cache,policy,"[user-policy]",0,exception);
1257 if (status == MagickFalse)
1258 return(status);
1259 /*
1260 Synchronize user policies.
1261 */
1262 user_policies=NewLinkedList(0);
1263 status=LoadPolicyCache(user_policies,policy,"[user-policy]",0,exception);
1264 if (status == MagickFalse)
1265 {
1266 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1267 return(MagickFalse);
1268 }
1269 ResetLinkedListIterator(user_policies);
1270 p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
1271 while (p != (PolicyInfo *) NULL)
1272 {
1273 if ((p->name != (char *) NULL) && (p->value != (char *) NULL))
1274 (void) SetMagickSecurityPolicyValue(p->domain,p->name,p->value,exception);
1275 p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
1276 }
1277 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1278 return(status);
1279}
1280
1281/*
1282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1283% %
1284% %
1285% %
1286% S e t M a g i c k S e c u r i t y P o l i c y V a l u e %
1287% %
1288% %
1289% %
1290%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1291%
1292% SetMagickSecurityPolicyValue() sets a value associated with an ImageMagick
1293% security policy. For most policies, the value must be less than any value
1294% set by the security policy configuration file (i.e. policy.xml). It returns
1295% MagickFalse if the policy cannot be modified or if the policy does not parse.
1296%
1297% The format of the SetMagickSecurityPolicyValue method is:
1298%
1299% MagickBooleanType SetMagickSecurityPolicyValue(
1300% const PolicyDomain domain,const char *name,const char *value,
1301% ExceptionInfo *exception)
1302%
1303% A description of each parameter follows:
1304%
1305% o domain: the domain of the policy (e.g. system, resource).
1306%
1307% o name: the name of the policy.
1308%
1309% o value: the value to set the policy to.
1310%
1311% o exception: return any errors or warnings in this structure.
1312%
1313*/
1314static MagickBooleanType SetMagickSecurityPolicyValue(const PolicyDomain domain,
1315 const char *name,const char *value,ExceptionInfo *exception)
1316{
1317 magick_unreferenced(exception);
1318 assert(exception != (ExceptionInfo *) NULL);
1319 if ((name == (const char *) NULL) || (value == (const char *) NULL))
1320 return(MagickFalse);
1321 switch (domain)
1322 {
1323 case CachePolicyDomain:
1324 {
1325 if (LocaleCompare(name,"memory-map") == 0)
1326 {
1327 if (LocaleCompare(value,"anonymous") != 0)
1328 return(MagickFalse);
1329 ResetCacheAnonymousMemory();
1330 ResetStreamAnonymousMemory();
1331 return(MagickTrue);
1332 }
1333 break;
1334 }
1335 case ResourcePolicyDomain:
1336 {
1337 ssize_t
1338 type;
1339
1340 type=ParseCommandOption(MagickResourceOptions,MagickFalse,name);
1341 if (type >= 0)
1342 {
1343 MagickSizeType
1344 limit;
1345
1346 limit=MagickResourceInfinity;
1347 if (LocaleCompare("unlimited",value) != 0)
1348 limit=StringToMagickSizeType(value,100.0);
1349 if ((ResourceType) type == TimeResource)
1350 limit=(MagickSizeType) ParseMagickTimeToLive(value);
1351 return(SetMagickResourceLimit((ResourceType) type,limit));
1352 }
1353 break;
1354 }
1355 case SystemPolicyDomain:
1356 {
1357 if (LocaleCompare(name,"max-memory-request") == 0)
1358 {
1359 MagickSizeType
1360 limit;
1361
1362 limit=MagickResourceInfinity;
1363 if (LocaleCompare("unlimited",value) != 0)
1364 limit=StringToMagickSizeType(value,100.0);
1365 SetMaxMemoryRequest(limit);
1366 return(MagickTrue);
1367 }
1368 if (LocaleCompare(name,"memory-map") == 0)
1369 {
1370 if (LocaleCompare(value,"anonymous") != 0)
1371 return(MagickFalse);
1372 ResetVirtualAnonymousMemory();
1373 return(MagickTrue);
1374 }
1375 if (LocaleCompare(name,"precision") == 0)
1376 {
1377 int
1378 limit;
1379
1380 limit=StringToInteger(value);
1381 SetMagickPrecision(limit);
1382 return(MagickTrue);
1383 }
1384 break;
1385 }
1386 case CoderPolicyDomain:
1387 case DelegatePolicyDomain:
1388 case FilterPolicyDomain:
1389 case ModulePolicyDomain:
1390 case PathPolicyDomain:
1391 default:
1392 break;
1393 }
1394 return(MagickFalse);
1395}