MagickCore 7.1.2-37
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 "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>
71#endif
72␌
73/*
74 Define declarations.
75*/
76#define PolicyFilename "policy.xml"
77␌
78/*
79 Typedef declarations.
80*/
82{
83 char
84 *path;
85
86 PolicyDomain
87 domain;
88
89 PolicyRights
90 rights;
91
92 char
93 *name,
94 *pattern,
95 *value;
96
97 MagickBooleanType
98 exempt,
99 stealth,
100 debug;
101
103 *semaphore;
104
105 size_t
106 signature;
107};
108
109typedef struct _PolicyMapInfo
110{
111 const PolicyDomain
112 domain;
113
114 const PolicyRights
115 rights;
116
117 const char
118 *name,
119 *pattern,
120 *value;
121} PolicyMapInfo;
122␌
123/*
124 Static declarations.
125*/
126static const PolicyMapInfo
127 PolicyMap[] =
128 {
129 { UndefinedPolicyDomain, UndefinedPolicyRights, (const char *) NULL,
130 (const char *) NULL, (const char *) NULL }
131 };
132
133static LinkedListInfo
134 *policy_cache = (LinkedListInfo *) NULL;
135
136static SemaphoreInfo
137 *policy_semaphore = (SemaphoreInfo *) NULL;
138␌
139/*
140 Forward declarations.
141*/
142static MagickBooleanType
143 IsPolicyCacheInstantiated(ExceptionInfo *),
144 LoadPolicyCache(LinkedListInfo *,const char *,const char *,const size_t,
145 ExceptionInfo *);
146
147static void
148 *DestroyPolicyElement(void *);
149␌
150/*
151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152% %
153% %
154% %
155% A c q u i r e P o l i c y C a c h e %
156% %
157% %
158% %
159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160%
161% AcquirePolicyCache() caches one or more policy configurations which provides
162% a mapping between policy attributes and a policy name.
163% It returns NULL if a policy configuration fails to load.
164%
165% The format of the AcquirePolicyCache method is:
166%
167% LinkedListInfo *AcquirePolicyCache(const char *filename,
168% ExceptionInfo *exception)
169%
170% A description of each parameter follows:
171%
172% o filename: the policy configuration file name.
173%
174% o exception: return any errors or warnings in this structure.
175%
176*/
177static LinkedListInfo *AcquirePolicyCache(const char *filename,
178 ExceptionInfo *exception)
179{
180 LinkedListInfo
181 *cache;
182
183 MagickBooleanType
184 status;
185
186 ssize_t
187 i;
188
189 /*
190 Load external policy map.
191 */
192 cache=NewLinkedList(0);
193 if (cache == (LinkedListInfo *) NULL)
194 return((LinkedListInfo *) NULL);
195 status=MagickTrue;
196#if MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
197 magick_unreferenced(filename);
198 status=LoadPolicyCache(cache,ZeroConfigurationPolicy,"[zero-configuration]",0,
199 exception);
200#else
201 {
202 const StringInfo
203 *option;
204
205 LinkedListInfo
206 *options;
207
208 options=GetConfigureOptions(filename,exception);
209 option=(const StringInfo *) GetNextValueInLinkedList(options);
210 while (option != (const StringInfo *) NULL)
211 {
212 status=LoadPolicyCache(cache,(const char *) GetStringInfoDatum(option),
213 GetStringInfoPath(option),0,exception);
214 if (status == MagickFalse)
215 break;
216 option=(const StringInfo *) GetNextValueInLinkedList(options);
217 }
218 options=DestroyConfigureOptions(options);
219 }
220#endif
221 if (status == MagickFalse)
222 {
223 cache=DestroyLinkedList(cache,DestroyPolicyElement);
224 CatchException(exception);
225 return((LinkedListInfo *) NULL);
226 }
227 /*
228 Load built-in policy map.
229 */
230 for (i=0; i < (ssize_t) (sizeof(PolicyMap)/sizeof(*PolicyMap)); i++)
231 {
232 const PolicyMapInfo
233 *p;
234
235 PolicyInfo
236 *policy_info;
237
238 p=PolicyMap+i;
239 policy_info=(PolicyInfo *) AcquireMagickMemory(sizeof(*policy_info));
240 if (policy_info == (PolicyInfo *) NULL)
241 {
242 (void) ThrowMagickException(exception,GetMagickModule(),
243 ResourceLimitError,"MemoryAllocationFailed","`%s'",
244 p->name == (char *) NULL ? "" : p->name);
245 CatchException(exception);
246 continue;
247 }
248 (void) memset(policy_info,0,sizeof(*policy_info));
249 policy_info->path=(char *) "[built-in]";
250 policy_info->domain=p->domain;
251 policy_info->rights=p->rights;
252 policy_info->name=(char *) p->name;
253 policy_info->pattern=(char *) p->pattern;
254 policy_info->value=(char *) p->value;
255 policy_info->exempt=MagickTrue;
256 policy_info->signature=MagickCoreSignature;
257 status=AppendValueToLinkedList(cache,policy_info);
258 if (status == MagickFalse)
259 {
260 policy_info=(PolicyInfo *) RelinquishMagickMemory(policy_info);
261 (void) ThrowMagickException(exception,GetMagickModule(),
262 ResourceLimitError,"MemoryAllocationFailed","`%s'",
263 p->name == (char *) NULL ? "" : p->name);
264 CatchException(exception);
265 }
266 }
267 return(cache);
268}
269␌
270/*
271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272% %
273% %
274% %
275+ G e t P o l i c y I n f o %
276% %
277% %
278% %
279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280%
281% GetPolicyInfo() searches the policy list for the specified name and if found
282% returns attributes for that policy.
283%
284% The format of the GetPolicyInfo method is:
285%
286% PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
287%
288% A description of each parameter follows:
289%
290% o name: the policy name.
291%
292% o exception: return any errors or warnings in this structure.
293%
294*/
295static PolicyInfo *GetPolicyInfo(const char *name,ExceptionInfo *exception)
296{
297 char
298 policyname[MagickPathExtent],
299 *q;
300
301 ElementInfo
302 *p;
303
304 PolicyDomain
305 domain;
306
307 PolicyInfo
308 *policy;
309
310 assert(exception != (ExceptionInfo *) NULL);
311 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
312 return((PolicyInfo *) NULL);
313 /*
314 Strip names of whitespace in O(n) single pass.
315 */
316 *policyname='\0';
317 if (name != (const char *) NULL)
318 {
319 const char
320 *src;
321
322 char
323 *dst;
324
325 dst=policyname;
326 for (src=name; (*src != '\0') && ((dst-policyname) < (MagickPathExtent-1)); src++)
327 {
328 if (isspace((int) ((unsigned char) *src)) == 0)
329 *dst++=(*src);
330 }
331 *dst='\0';
332 }
333 /*
334 Strip domain from policy name (e.g. resource:map).
335 */
336 domain=UndefinedPolicyDomain;
337 for (q=policyname; *q != '\0'; q++)
338 {
339 ssize_t
340 parsed_domain;
341
342 if (*q != ':')
343 continue;
344 *q='\0';
345 parsed_domain=ParseCommandOption(MagickPolicyDomainOptions,
346 MagickTrue,policyname);
347 if (parsed_domain >= 0)
348 domain=(PolicyDomain) parsed_domain;
349 (void) memmove(policyname,q+1,strlen(q+1)+1);
350 break;
351 }
352 /*
353 Search for policy tag (last matching policy wins).
354 */
355 policy=(PolicyInfo *) NULL;
356 LockSemaphoreInfo(policy_semaphore);
357 p=GetHeadElementInLinkedList(policy_cache);
358 if ((name == (const char *) NULL) || (LocaleCompare(name,"*") == 0))
359 {
360 if (p != (ElementInfo *) NULL)
361 policy=(PolicyInfo *) p->value;
362 UnlockSemaphoreInfo(policy_semaphore);
363 return(policy);
364 }
365 while (p != (ElementInfo *) NULL)
366 {
367 PolicyInfo
368 *candidate;
369
370 candidate=(PolicyInfo *) p->value;
371 if ((candidate->name != (char *) NULL) &&
372 ((domain == UndefinedPolicyDomain) || (candidate->domain == domain)) &&
373 (LocaleCompare(policyname,candidate->name) == 0))
374 policy=candidate;
375 p=p->next;
376 }
377 UnlockSemaphoreInfo(policy_semaphore);
378 return(policy);
379}
380␌
381/*
382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383% %
384% %
385% %
386% G e t P o l i c y I n f o L i s t %
387% %
388% %
389% %
390%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391%
392% GetPolicyInfoList() returns any policies that match the specified pattern.
393%
394% The format of the GetPolicyInfoList function is:
395%
396% const PolicyInfo **GetPolicyInfoList(const char *pattern,
397% size_t *number_policies,ExceptionInfo *exception)
398%
399% A description of each parameter follows:
400%
401% o pattern: Specifies a pointer to a text string containing a pattern.
402%
403% o number_policies: returns the number of policies in the list.
404%
405% o exception: return any errors or warnings in this structure.
406%
407*/
408MagickExport const PolicyInfo **GetPolicyInfoList(const char *pattern,
409 size_t *number_policies,ExceptionInfo *exception)
410{
411 const PolicyInfo
412 **policies;
413
414 ElementInfo
415 *p;
416
417 size_t
418 capacity;
419
420 ssize_t
421 i;
422
423 assert(pattern != (char *) NULL);
424 assert(number_policies != (size_t *) NULL);
425 if (IsEventLogging() != MagickFalse)
426 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
427 *number_policies=0;
428 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
429 return((const PolicyInfo **) NULL);
430 LockSemaphoreInfo(policy_semaphore);
431 capacity=(size_t) GetNumberOfElementsInLinkedList(policy_cache)+1UL;
432 policies=(const PolicyInfo **) AcquireQuantumMemory(capacity,
433 sizeof(*policies));
434 if (policies == (const PolicyInfo **) NULL)
435 {
436 UnlockSemaphoreInfo(policy_semaphore);
437 return((const PolicyInfo **) NULL);
438 }
439 p=GetHeadElementInLinkedList(policy_cache);
440 for (i=0; (p != (ElementInfo *) NULL) && ((size_t) i < (capacity-1UL)); )
441 {
442 const PolicyInfo
443 *policy;
444
445 policy=(const PolicyInfo *) p->value;
446 if ((policy->stealth == MagickFalse) &&
447 (policy->name != (char *) NULL) &&
448 (GlobExpression(policy->name,pattern,MagickFalse) != MagickFalse))
449 policies[i++]=policy;
450 p=p->next;
451 }
452 UnlockSemaphoreInfo(policy_semaphore);
453 if (i == 0)
454 policies=(const PolicyInfo **) RelinquishMagickMemory((void*) policies);
455 else
456 policies[i]=(PolicyInfo *) NULL;
457 *number_policies=(size_t) i;
458 return(policies);
459}
460␌
461/*
462%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463% %
464% %
465% %
466% G e t P o l i c y L i s t %
467% %
468% %
469% %
470%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471%
472% GetPolicyList() returns any policies that match the specified pattern.
473%
474% The format of the GetPolicyList function is:
475%
476% char **GetPolicyList(const char *pattern,size_t *number_policies,
477% ExceptionInfo *exception)
478%
479% A description of each parameter follows:
480%
481% o pattern: a pointer to a text string containing a pattern.
482%
483% o number_policies: returns the number of policies in the list.
484%
485% o exception: return any errors or warnings in this structure.
486%
487*/
488
489static char *AcquirePolicyString(const char *source,const size_t pad)
490{
491 char
492 *destination;
493
494 size_t
495 length;
496
497 length=0;
498 if (source != (char *) NULL)
499 length+=strlen(source);
500 destination=(char *) NULL;
501 if (~length >= pad)
502 destination=(char *) AcquireMagickMemory((length+pad)*sizeof(*destination));
503 if (destination == (char *) NULL)
504 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
505 if (source != (char *) NULL)
506 (void) memcpy(destination,source,length*sizeof(*destination));
507 destination[length]='\0';
508 return(destination);
509}
510
511MagickExport char **GetPolicyList(const char *pattern,size_t *number_policies,
512 ExceptionInfo *exception)
513{
514 char
515 **policies;
516
517 const ElementInfo
518 *p;
519
520 size_t
521 capacity;
522
523 ssize_t
524 i;
525
526 assert(pattern != (char *) NULL);
527 assert(number_policies != (size_t *) NULL);
528 if (IsEventLogging() != MagickFalse)
529 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
530 *number_policies=0;
531 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
532 return((char **) NULL);
533 LockSemaphoreInfo(policy_semaphore);
534 capacity=(size_t) GetNumberOfElementsInLinkedList(policy_cache)+1UL;
535 policies=(char **) AcquireQuantumMemory(capacity,sizeof(*policies));
536 if (policies == (char **) NULL)
537 {
538 UnlockSemaphoreInfo(policy_semaphore);
539 return((char **) NULL);
540 }
541 p=GetHeadElementInLinkedList(policy_cache);
542 for (i=0; (p != (ElementInfo *) NULL) && ((size_t) i < (capacity-1UL)); )
543 {
544 const PolicyInfo
545 *policy;
546
547 policy=(const PolicyInfo *) p->value;
548 if ((policy->stealth == MagickFalse) &&
549 (policy->name != (char *) NULL) &&
550 (GlobExpression(policy->name,pattern,MagickFalse) != MagickFalse))
551 policies[i++]=AcquirePolicyString(policy->name,1);
552 p=p->next;
553 }
554 UnlockSemaphoreInfo(policy_semaphore);
555 if (i == 0)
556 policies=(char **) RelinquishMagickMemory(policies);
557 else
558 policies[i]=(char *) NULL;
559 *number_policies=(size_t) i;
560 return(policies);
561}
562␌
563/*
564%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565% %
566% %
567% %
568% G e t P o l i c y V a l u e %
569% %
570% %
571% %
572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573%
574% GetPolicyValue() returns the value associated with the named policy.
575%
576% The format of the GetPolicyValue method is:
577%
578% char *GetPolicyValue(const char *name)
579%
580% A description of each parameter follows:
581%
582% o name: The name of the policy.
583%
584*/
585MagickExport char *GetPolicyValue(const char *name)
586{
587 char
588 *result;
589
590 const PolicyInfo
591 *policy_info;
592
593 ExceptionInfo
594 *exception;
595
596 assert(name != (const char *) NULL);
597 if (IsEventLogging() != MagickFalse)
598 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",name);
599 exception=AcquireExceptionInfo();
600 policy_info=GetPolicyInfo(name,exception);
601 exception=DestroyExceptionInfo(exception);
602 if (policy_info == (PolicyInfo *) NULL)
603 return((char *) NULL);
604 result=(char *) NULL;
605 LockSemaphoreInfo(policy_semaphore);
606 if ((policy_info->value != (const char *) NULL) &&
607 (*policy_info->value != '\0'))
608 result=AcquirePolicyString(policy_info->value,1);
609 UnlockSemaphoreInfo(policy_semaphore);
610 return(result);
611}
612␌
613/*
614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615% %
616% %
617% %
618+ I s P a t h A u t h o r i z e d %
619% %
620% %
621% %
622%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623%
624% IsPathAuthorized() determines if the specified path is authorized based on
625% the current policy settings.
626%
627% The format of the IsPathAuthorized method is:
628%
629% MagickBooleanType IsPathAuthorized(const PolicyRights rights,
630% const char *path)
631%
632% A description of each parameter follows.
633%
634% o rights: The policy rights to check.
635%
636% o path: The path to check.
637%
638*/
639
640static inline MagickBooleanType IsPolicyPathSeparator(const char c)
641{
642#if defined(MAGICKCORE_WINDOWS_SUPPORT)
643 if ((c == '/') || (c == '\\'))
644 return(MagickTrue);
645#endif
646 return(c == *DirectorySeparator ? MagickTrue : MagickFalse);
647}
648
649static inline MagickBooleanType IsPathContainsSymlink(const char *path)
650{
651 char
652 partial[MagickPathExtent];
653
654 const char
655 *p;
656
657 ssize_t
658 offset = 0;
659
660 if (path == (const char *) NULL)
661 return(MagickFalse);
662 *partial='\0';
663 p=path;
664 if (IsPolicyPathSeparator(*p) != MagickFalse)
665 {
666 /*
667 Path starts with a directory separator, include it.
668 */
669 if ((offset+1) >= (ssize_t) sizeof(partial))
670 return(MagickTrue);
671 partial[offset++]=(*DirectorySeparator);
672 p++;
673 partial[offset]='\0';
674 }
675 while (*p != '\0')
676 {
677 char
678 component[MagickPathExtent];
679
680 ssize_t
681 i = 0;
682
683 /*
684 Copy next component into a temporary buffer; fail closed if oversized.
685 */
686 while ((*p != '\0') && (IsPolicyPathSeparator(*p) == MagickFalse))
687 {
688 if ((i+1) >= (ssize_t) sizeof(component))
689 return(MagickTrue);
690 component[i++]=(*p++);
691 }
692 component[i]='\0';
693 if (i == 0)
694 {
695 /*
696 Skip repeated separators.
697 */
698 if (IsPolicyPathSeparator(*p) != MagickFalse)
699 p++;
700 continue;
701 }
702 if ((offset > 0) && (partial[offset-1] != *DirectorySeparator))
703 {
704 /*
705 Append separator if needed.
706 */
707 if ((offset+1) >= (ssize_t) sizeof(partial))
708 return(MagickTrue);
709 partial[offset++]=(*DirectorySeparator);
710 partial[offset]='\0';
711 }
712 /*
713 Append component; fail closed on buffer overflow.
714 */
715 if ((offset+i) >= (ssize_t) sizeof(partial))
716 return(MagickTrue);
717 (void) memcpy(partial+offset,component,(size_t) i);
718 offset+=i;
719 partial[offset]='\0';
720 if (*p != '\0')
721 {
722 /*
723 Check whether this prefix is a symlink.
724 */
725 if (is_symlink_utf8(partial) != MagickFalse)
726 return(MagickTrue);
727 }
728 /*
729 Skip separator.
730 */
731 if (IsPolicyPathSeparator(*p) != MagickFalse)
732 p++;
733 }
734 return(MagickFalse);
735}
736
737MagickExport MagickBooleanType IsPathAuthorized(const PolicyRights rights,
738 const char *path)
739{
740 MagickBooleanType
741 status,
742 symlink_follow_allowed;
743
744 if (path == (const char *) NULL)
745 return(MagickFalse);
746 symlink_follow_allowed=IsRightsAuthorizedByName(
747 SystemPolicyDomain,"symlink",rights,"follow");
748 status=IsRightsAuthorized(PathPolicyDomain,rights,path);
749 if ((status != MagickFalse) && (symlink_follow_allowed == MagickFalse))
750 {
751 if ((is_symlink_utf8(path) != MagickFalse) ||
752 (IsPathContainsSymlink(path) != MagickFalse))
753 status=MagickFalse;
754 }
755 if (status != MagickFalse)
756 status=IsFileResourceIdentityValid(path);
757 return(status);
758}
759␌
760/*
761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762% %
763% %
764% %
765+ 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 %
766% %
767% %
768% %
769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770%
771% IsPolicyCacheInstantiated() determines if the policy list is instantiated.
772% If not, it instantiates the list and returns it.
773%
774% The format of the IsPolicyInstantiated method is:
775%
776% MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
777%
778% A description of each parameter follows.
779%
780% o exception: return any errors or warnings in this structure.
781%
782*/
783static MagickBooleanType IsPolicyCacheInstantiated(ExceptionInfo *exception)
784{
785 if (policy_cache == (LinkedListInfo *) NULL)
786 {
787 (void) GetMaxMemoryRequest(); /* avoid OMP deadlock */
788 if (policy_semaphore == (SemaphoreInfo *) NULL)
789 ActivateSemaphoreInfo(&policy_semaphore);
790 LockSemaphoreInfo(policy_semaphore);
791 if (policy_cache == (LinkedListInfo *) NULL)
792 policy_cache=AcquirePolicyCache(PolicyFilename,exception);
793 UnlockSemaphoreInfo(policy_semaphore);
794 }
795 return(policy_cache != (LinkedListInfo *) NULL ? MagickTrue : MagickFalse);
796}
797␌
798/*
799%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800% %
801% %
802% %
803% I s R i g h t s A u t h o r i z e d %
804% %
805% %
806% %
807%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
808%
809% IsRightsAuthorized() returns MagickTrue if the policy authorizes the
810% requested rights for the specified domain.
811%
812% Policy evaluation uses a "last match wins" model. Be careful when adding
813% new rules: any later policy can override earlier denies or allows. Place
814% broad deny rules first, followed by specific exceptions, and review
815% ordering to avoid accidental authorization.
816%
817% The format of the IsRightsAuthorized method is:
818%
819% MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
820% const PolicyRights rights,const char *pattern)
821%
822% A description of each parameter follows:
823%
824% o domain: the policy domain.
825%
826% o rights: the policy rights.
827%
828% o pattern: the pattern.
829%
830*/
831
832static MagickBooleanType IsPathPolicyMatch(const char *path,const char *pattern)
833{
834#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
835 char
836 *normalized_path,
837 *normalized_pattern,
838 *p;
839
840 MagickBooleanType
841 match;
842
843 if ((path == (const char *) NULL) || (pattern == (const char *) NULL))
844 return(MagickFalse);
845 normalized_path=AcquireString(path);
846 normalized_pattern=AcquireString(pattern);
847 for (p=normalized_path; *p != '\0'; p++)
848 if (*p == '\\')
849 *p='/';
850 for (p=normalized_pattern; *p != '\0'; p++)
851 if (*p == '\\')
852 *p='/';
853 match=GlobExpression(normalized_path,normalized_pattern,MagickFalse);
854 normalized_path=DestroyString(normalized_path);
855 normalized_pattern=DestroyString(normalized_pattern);
856 return(match);
857#else
858 if ((path == (const char *) NULL) || (pattern == (const char *) NULL))
859 return(MagickFalse);
860 return(GlobExpression(path,pattern,MagickFalse));
861#endif
862}
863
864MagickExport MagickBooleanType IsRightsAuthorizedByName(
865 const PolicyDomain domain,const char *name,const PolicyRights rights,
866 const char *pattern)
867{
868 char
869 *canonical_directory = (char *) NULL,
870 *canonical_path = (char *) NULL,
871 *canonical_candidate = (char *) NULL,
872 directory[MagickPathExtent],
873 filename[MagickPathExtent];
874
875 ElementInfo
876 *p;
877
878 ExceptionInfo
879 *exception;
880
881 MagickBooleanType
882 matched_any = MagickFalse,
883 paths_provisioned = MagickFalse,
884 resolved_matched_any = MagickFalse,
885 status;
886
887 PolicyRights
888 effective_rights = AllPolicyRights,
889 resolved_rights = AllPolicyRights;
890
891 /*
892 Load policies.
893 */
894 if ((GetLogEventMask() & PolicyEvent) != 0)
895 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
896 "Domain: %s; name: %s; rights=%s; pattern=\"%s\"; ...",
897 CommandOptionToMnemonic(MagickPolicyDomainOptions,domain),
898 name == (const char *) NULL ? "undefined" : name,
899 CommandOptionToMnemonic(MagickPolicyRightsOptions,rights),
900 pattern == (const char *) NULL ? "undefined" : pattern);
901 exception=AcquireExceptionInfo();
902 status=IsPolicyCacheInstantiated(exception);
903 exception=DestroyExceptionInfo(exception);
904 if (status == MagickFalse)
905 {
906 if ((GetLogEventMask() & PolicyEvent) != 0)
907 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
908 " authorized: false (security policies could not be loaded)");
909 return(MagickFalse);
910 }
911 if (pattern == (const char *) NULL)
912 {
913 if ((GetLogEventMask() & PolicyEvent) != 0)
914 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
915 " authorized: false (pattern is null)");
916 return(MagickFalse);
917 }
918 /*
919 Pre-provision canonical paths outside the policy semaphore lock to avoid
920 blocking concurrent policy checks on filesystem I/O (realpath).
921 */
922 if (domain == PathPolicyDomain)
923 {
924 paths_provisioned=MagickTrue;
925 GetPathComponent(pattern,HeadPath,directory);
926 GetPathComponent(pattern,TailPath,filename);
927 canonical_directory=realpath_utf8(directory);
928 if ((canonical_directory != (char *) NULL) && (*filename != '\0'))
929 {
930 size_t
931 length;
932
933 length=strlen(canonical_directory)+strlen(filename)+2;
934 canonical_candidate=(char *) AcquireCriticalMemory(length*
935 sizeof(*canonical_candidate));
936 if (canonical_candidate != (char *) NULL)
937 (void) FormatLocaleString(canonical_candidate,length,"%s%s%s",
938 canonical_directory,DirectorySeparator,filename);
939 }
940 canonical_path=realpath_utf8(pattern);
941 }
942 /*
943 Evaluate policies in order; the last matching policy wins. A path is
944 evaluated twice: once by the name it was given (lexical, canonical
945 directory, or canonical directory plus basename) and once by its fully
946 resolved path. Both evaluations must authorize the request, so a symbolic
947 link inside an allowed directory cannot reach a target the policy denies.
948 */
949 LockSemaphoreInfo(policy_semaphore);
950 p=GetHeadElementInLinkedList(policy_cache);
951 for ( ; p != (ElementInfo *) NULL; p=p->next)
952 {
953 const PolicyInfo
954 *policy = (PolicyInfo *) p->value;
955
956 MagickBooleanType
957 match = MagickFalse;
958
959 if ((policy->domain != domain) || (policy->pattern == (char *) NULL))
960 continue;
961 if ((name != (char *) NULL) && ((policy->name == (char *) NULL) ||
962 (LocaleCompare(name,policy->name) != 0)))
963 continue;
964 if (policy->domain == PathPolicyDomain)
965 {
966 /*
967 Match the given name against the directory and basename forms.
968 */
969 match=IsPathPolicyMatch(pattern,policy->pattern);
970 if ((canonical_directory != (char *) NULL) && (match == MagickFalse))
971 match=IsPathPolicyMatch(canonical_directory,policy->pattern);
972 if ((canonical_candidate != (char *) NULL) && (match == MagickFalse))
973 match=IsPathPolicyMatch(canonical_candidate,policy->pattern);
974 /*
975 Match the fully resolved path on its own.
976 */
977 if ((canonical_path != (char *) NULL) &&
978 (IsPathPolicyMatch(canonical_path,policy->pattern) != MagickFalse))
979 {
980 resolved_matched_any=MagickTrue;
981 resolved_rights=policy->rights;
982 }
983 }
984 else
985 match=GlobExpression(pattern,policy->pattern,MagickFalse);
986 if (match == MagickFalse)
987 continue;
988 matched_any=MagickTrue;
989 effective_rights=policy->rights;
990 }
991 UnlockSemaphoreInfo(policy_semaphore);
992 /*
993 Is rights authorized?
994 */
995 status=MagickTrue;
996 if (matched_any != MagickFalse)
997 {
998 if (((rights & ReadPolicyRights) != 0) &&
999 ((effective_rights & ReadPolicyRights) == 0))
1000 status=MagickFalse;
1001 if (((rights & WritePolicyRights) != 0) &&
1002 ((effective_rights & WritePolicyRights) == 0))
1003 status=MagickFalse;
1004 if (((rights & ExecutePolicyRights) != 0) &&
1005 ((effective_rights & ExecutePolicyRights) == 0))
1006 status=MagickFalse;
1007 }
1008 if (resolved_matched_any != MagickFalse)
1009 {
1010 if (((rights & ReadPolicyRights) != 0) &&
1011 ((resolved_rights & ReadPolicyRights) == 0))
1012 status=MagickFalse;
1013 if (((rights & WritePolicyRights) != 0) &&
1014 ((resolved_rights & WritePolicyRights) == 0))
1015 status=MagickFalse;
1016 if (((rights & ExecutePolicyRights) != 0) &&
1017 ((resolved_rights & ExecutePolicyRights) == 0))
1018 status=MagickFalse;
1019 }
1020 /*
1021 A symbolic link that does not resolve (dangling or looping) has no target
1022 to evaluate; following it on write could create a file anywhere.
1023 */
1024 if ((paths_provisioned != MagickFalse) && (canonical_path == (char *) NULL) &&
1025 (is_symlink_utf8(pattern) != MagickFalse))
1026 status=MagickFalse;
1027 if (canonical_directory != (char *) NULL)
1028 canonical_directory=DestroyString(canonical_directory);
1029 if (canonical_candidate != (char *) NULL)
1030 canonical_candidate=DestroyString(canonical_candidate);
1031 if (canonical_path != (char *) NULL)
1032 canonical_path=DestroyString(canonical_path);
1033 if ((GetLogEventMask() & PolicyEvent) != 0)
1034 (void) LogMagickEvent(PolicyEvent,GetMagickModule(),
1035 " authorized: %s",status == MagickFalse ? "false" : "true");
1036 return(status);
1037}
1038
1039MagickExport MagickBooleanType IsRightsAuthorized(const PolicyDomain domain,
1040 const PolicyRights rights,const char *pattern)
1041{
1042 return(IsRightsAuthorizedByName(domain,(const char *) NULL,rights,pattern));
1043}
1044␌
1045/*
1046%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047% %
1048% %
1049% %
1050% L i s t P o l i c y I n f o %
1051% %
1052% %
1053% %
1054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1055%
1056% ListPolicyInfo() lists policies to the specified file.
1057%
1058% The format of the ListPolicyInfo method is:
1059%
1060% MagickBooleanType ListPolicyInfo(FILE *file,ExceptionInfo *exception)
1061%
1062% A description of each parameter follows.
1063%
1064% o file: List policy names to this file handle.
1065%
1066% o exception: return any errors or warnings in this structure.
1067%
1068*/
1069MagickExport MagickBooleanType ListPolicyInfo(FILE *file,
1070 ExceptionInfo *exception)
1071{
1072 const char
1073 *path,
1074 *domain;
1075
1076 const PolicyInfo
1077 **policy_info;
1078
1079 ssize_t
1080 i;
1081
1082 size_t
1083 number_policies;
1084
1085 /*
1086 List name and attributes of each policy in the list.
1087 */
1088 if (file == (const FILE *) NULL)
1089 file=stdout;
1090 policy_info=GetPolicyInfoList("*",&number_policies,exception);
1091 if (policy_info == (const PolicyInfo **) NULL)
1092 return(MagickFalse);
1093 path=(const char *) NULL;
1094 for (i=0; i < (ssize_t) number_policies; i++)
1095 {
1096 if (policy_info[i]->stealth != MagickFalse)
1097 continue;
1098 if (((path == (const char *) NULL) ||
1099 (LocaleCompare(path,policy_info[i]->path) != 0)) &&
1100 (policy_info[i]->path != (char *) NULL))
1101 (void) FormatLocaleFile(file,"\nPath: %s\n",policy_info[i]->path);
1102 path=policy_info[i]->path;
1103 domain=CommandOptionToMnemonic(MagickPolicyDomainOptions,
1104 policy_info[i]->domain);
1105 (void) FormatLocaleFile(file," Policy: %s\n",domain);
1106 if ((policy_info[i]->domain == CachePolicyDomain) ||
1107 (policy_info[i]->domain == ResourcePolicyDomain) ||
1108 (policy_info[i]->domain == SystemPolicyDomain))
1109 {
1110 if (policy_info[i]->name != (char *) NULL)
1111 (void) FormatLocaleFile(file," name: %s\n",policy_info[i]->name);
1112 if (policy_info[i]->value != (char *) NULL)
1113 (void) FormatLocaleFile(file," value: %s\n",policy_info[i]->value);
1114 }
1115 else
1116 {
1117 (void) FormatLocaleFile(file," rights: ");
1118 if (policy_info[i]->rights == NoPolicyRights)
1119 (void) FormatLocaleFile(file,"None ");
1120 if ((policy_info[i]->rights & ReadPolicyRights) != 0)
1121 (void) FormatLocaleFile(file,"Read ");
1122 if ((policy_info[i]->rights & WritePolicyRights) != 0)
1123 (void) FormatLocaleFile(file,"Write ");
1124 if ((policy_info[i]->rights & ExecutePolicyRights) != 0)
1125 (void) FormatLocaleFile(file,"Execute ");
1126 (void) FormatLocaleFile(file,"\n");
1127 if (policy_info[i]->pattern != (char *) NULL)
1128 (void) FormatLocaleFile(file," pattern: %s\n",
1129 policy_info[i]->pattern);
1130 }
1131 }
1132 policy_info=(const PolicyInfo **) RelinquishMagickMemory((void *)
1133 policy_info);
1134 (void) fflush(file);
1135 return(MagickTrue);
1136}
1137␌
1138/*
1139%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1140% %
1141% %
1142% %
1143+ L o a d P o l i c y C a c h e %
1144% %
1145% %
1146% %
1147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1148%
1149% LoadPolicyCache() loads the policy configurations which provides a mapping
1150% between policy attributes and a policy domain.
1151%
1152% The format of the LoadPolicyCache method is:
1153%
1154% MagickBooleanType LoadPolicyCache(LinkedListInfo *cache,const char *xml,
1155% const char *filename,const size_t depth,ExceptionInfo *exception)
1156%
1157% A description of each parameter follows:
1158%
1159% o xml: The policy list in XML format.
1160%
1161% o filename: The policy list filename.
1162%
1163% o depth: depth of <include /> statements.
1164%
1165% o exception: return any errors or warnings in this structure.
1166%
1167*/
1168
1169static void *DestroyPolicyElement(void *policy_info)
1170{
1171 PolicyInfo
1172 *p;
1173
1174 p=(PolicyInfo *) policy_info;
1175 if (p->exempt == MagickFalse)
1176 {
1177 if (p->value != (char *) NULL)
1178 p->value=DestroyString(p->value);
1179 if (p->pattern != (char *) NULL)
1180 p->pattern=DestroyString(p->pattern);
1181 if (p->name != (char *) NULL)
1182 p->name=DestroyString(p->name);
1183 if (p->path != (char *) NULL)
1184 p->path=DestroyString(p->path);
1185 }
1186 p=(PolicyInfo *) RelinquishMagickMemory(p);
1187 return((void *) NULL);
1188}
1189
1190static MagickBooleanType LoadPolicyCache(LinkedListInfo *cache,
1191 const char *policy,const char *filename,const size_t depth,
1192 ExceptionInfo *exception)
1193{
1194 char
1195 keyword[MagickPathExtent],
1196 *token;
1197
1198 const char
1199 *q;
1200
1201 MagickBooleanType
1202 status;
1203
1204 PolicyInfo
1205 *policy_info;
1206
1207 size_t
1208 extent;
1209
1210 /*
1211 Load the policy map file.
1212 */
1213 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1214 "Loading policy file \"%s\" ...",filename);
1215 if ((cache == (LinkedListInfo *) NULL) || (policy == (char *) NULL))
1216 return(MagickFalse);
1217 status=MagickTrue;
1218 policy_info=(PolicyInfo *) NULL;
1219 token=AcquirePolicyString(policy,MagickPathExtent);
1220 extent=strlen(token)+MagickPathExtent;
1221 for (q=policy; *q != '\0'; )
1222 {
1223 /*
1224 Interpret XML.
1225 */
1226 if (SkipXMLComment(&q) == MagickFalse)
1227 {
1228 (void) ThrowMagickException(exception,GetMagickModule(),
1229 ConfigureError,"UnterminatedComment","`%s'",filename);
1230 status=MagickFalse;
1231 break;
1232 }
1233 (void) GetNextToken(q,&q,extent,token);
1234 if (*token == '\0')
1235 break;
1236 (void) CopyMagickString(keyword,token,MagickPathExtent);
1237 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1238 {
1239 if (SkipXMLDocType(&q) == MagickFalse)
1240 {
1241 /*
1242 Detect unterminated DOCTYPE.
1243 */
1244 (void) ThrowMagickException(exception,GetMagickModule(),
1245 ConfigureError,"UnterminatedDOCTYPE","`%s'",filename);
1246 status=MagickFalse;
1247 break;
1248 }
1249 continue;
1250 }
1251 if (LocaleCompare(keyword,"<include") == 0)
1252 {
1253 /*
1254 Include element.
1255 */
1256 while (((*token != '/') || (*(token+1) != '>')) && (*q != '\0'))
1257 {
1258 (void) CopyMagickString(keyword,token,MagickPathExtent);
1259 (void) GetNextToken(q,&q,extent,token);
1260 if (*token != '=')
1261 continue;
1262 (void) GetNextToken(q,&q,extent,token);
1263 if (LocaleCompare(keyword,"file") == 0)
1264 {
1265 if (depth >= MagickMaxRecursionDepth)
1266 {
1267 (void) ThrowMagickException(exception,GetMagickModule(),
1268 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",
1269 token);
1270 status=MagickFalse;
1271 break;
1272 }
1273 else
1274 {
1275 char
1276 path[MagickPathExtent],
1277 *file_xml;
1278
1279 GetPathComponent(filename,HeadPath,path);
1280 if (*path != '\0')
1281 (void) ConcatenateMagickString(path,DirectorySeparator,
1282 MagickPathExtent);
1283 if (IsPolicyPathSeparator(*token) != MagickFalse)
1284 (void) CopyMagickString(path,token,MagickPathExtent);
1285 else
1286 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1287 file_xml=FileToXML(path,~0UL);
1288 if (file_xml != (char *) NULL)
1289 {
1290 if (LoadPolicyCache(cache,file_xml,path,depth+1,exception) == MagickFalse)
1291 status=MagickFalse;
1292 file_xml=DestroyString(file_xml);
1293 if (status == MagickFalse)
1294 break;
1295 }
1296 }
1297 }
1298 }
1299 if (status == MagickFalse)
1300 break;
1301 continue;
1302 }
1303 if (LocaleCompare(keyword,"<policy") == 0)
1304 {
1305 /*
1306 Policy element (replace any unclosed prior element to prevent leak).
1307 */
1308 if (policy_info != (PolicyInfo *) NULL)
1309 policy_info=(PolicyInfo *) DestroyPolicyElement(policy_info);
1310 policy_info=(PolicyInfo *) AcquireCriticalMemory(sizeof(*policy_info));
1311 (void) memset(policy_info,0,sizeof(*policy_info));
1312 policy_info->path=AcquirePolicyString(filename,1);
1313 policy_info->exempt=MagickFalse;
1314 policy_info->signature=MagickCoreSignature;
1315 continue;
1316 }
1317 if (policy_info == (PolicyInfo *) NULL)
1318 continue;
1319 if ((LocaleCompare(keyword,"/>") == 0) ||
1320 (LocaleCompare(keyword,"</policy>") == 0))
1321 {
1322 status=AppendValueToLinkedList(cache,policy_info);
1323 if (status == MagickFalse)
1324 {
1325 (void) ThrowMagickException(exception,GetMagickModule(),
1326 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1327 policy_info->name == (char *) NULL ? "" : policy_info->name);
1328 policy_info=(PolicyInfo *) DestroyPolicyElement(policy_info);
1329 }
1330 policy_info=(PolicyInfo *) NULL;
1331 continue;
1332 }
1333 (void) GetNextToken(q,(const char **) NULL,extent,token);
1334 if (*token != '=')
1335 continue;
1336 (void) GetNextToken(q,&q,extent,token);
1337 (void) GetNextToken(q,&q,extent,token);
1338 switch (*keyword)
1339 {
1340 case 'D':
1341 case 'd':
1342 {
1343 if (LocaleCompare((char *) keyword,"domain") == 0)
1344 {
1345 ssize_t
1346 parsed_domain;
1347
1348 parsed_domain=ParseCommandOption(MagickPolicyDomainOptions,
1349 MagickTrue,token);
1350 if (parsed_domain < 0)
1351 {
1352 (void) ThrowMagickException(exception,GetMagickModule(),
1353 ConfigureError,"UnrecognizedPolicyDomain","`%s'",token);
1354 status=MagickFalse;
1355 }
1356 else
1357 policy_info->domain=(PolicyDomain) parsed_domain;
1358 break;
1359 }
1360 break;
1361 }
1362 case 'N':
1363 case 'n':
1364 {
1365 if (LocaleCompare((char *) keyword,"name") == 0)
1366 {
1367 if (policy_info->name != (char *) NULL)
1368 policy_info->name=DestroyString(policy_info->name);
1369 policy_info->name=AcquirePolicyString(token,1);
1370 break;
1371 }
1372 break;
1373 }
1374 case 'P':
1375 case 'p':
1376 {
1377 if (LocaleCompare((char *) keyword,"pattern") == 0)
1378 {
1379 if (policy_info->pattern != (char *) NULL)
1380 policy_info->pattern=DestroyString(policy_info->pattern);
1381 policy_info->pattern=AcquirePolicyString(token,1);
1382 break;
1383 }
1384 break;
1385 }
1386 case 'R':
1387 case 'r':
1388 {
1389 if (LocaleCompare((char *) keyword,"rights") == 0)
1390 {
1391 ssize_t
1392 parsed_rights;
1393
1394 parsed_rights=ParseCommandOption(MagickPolicyRightsOptions,
1395 MagickTrue,token);
1396 if (parsed_rights < 0)
1397 {
1398 (void) ThrowMagickException(exception,GetMagickModule(),
1399 ConfigureError,"UnrecognizedPolicyRights","`%s'",token);
1400 status=MagickFalse;
1401 }
1402 else
1403 policy_info->rights=(PolicyRights) parsed_rights;
1404 break;
1405 }
1406 break;
1407 }
1408 case 'S':
1409 case 's':
1410 {
1411 if (LocaleCompare((char *) keyword,"stealth") == 0)
1412 {
1413 policy_info->stealth=IsStringTrue(token);
1414 break;
1415 }
1416 break;
1417 }
1418 case 'V':
1419 case 'v':
1420 {
1421 if (LocaleCompare((char *) keyword,"value") == 0)
1422 {
1423 if (policy_info->value != (char *) NULL)
1424 policy_info->value=DestroyString(policy_info->value);
1425 policy_info->value=AcquirePolicyString(token,1);
1426 break;
1427 }
1428 break;
1429 }
1430 default:
1431 break;
1432 }
1433 if (status == MagickFalse)
1434 break;
1435 }
1436 if (policy_info != (PolicyInfo *) NULL)
1437 (void) DestroyPolicyElement(policy_info);
1438 token=(char *) RelinquishMagickMemory(token);
1439 return(status);
1440}
1441␌
1442/*
1443%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444% %
1445% %
1446% %
1447+ P o l i c y C o m p o n e n t G e n e s i s %
1448% %
1449% %
1450% %
1451%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1452%
1453% PolicyComponentGenesis() instantiates the policy component.
1454%
1455% The format of the PolicyComponentGenesis method is:
1456%
1457% MagickBooleanType PolicyComponentGenesis(void)
1458%
1459*/
1460MagickPrivate MagickBooleanType PolicyComponentGenesis(void)
1461{
1462 if (policy_semaphore == (SemaphoreInfo *) NULL)
1463 policy_semaphore=AcquireSemaphoreInfo();
1464 return(MagickTrue);
1465}
1466␌
1467/*
1468%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1469% %
1470% %
1471% %
1472+ P o l i c y C o m p o n e n t T e r m i n u s %
1473% %
1474% %
1475% %
1476%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477%
1478% PolicyComponentTerminus() destroys the policy component.
1479%
1480% The format of the PolicyComponentTerminus method is:
1481%
1482% PolicyComponentTerminus(void)
1483%
1484*/
1485MagickPrivate void PolicyComponentTerminus(void)
1486{
1487 if (policy_semaphore == (SemaphoreInfo *) NULL)
1488 ActivateSemaphoreInfo(&policy_semaphore);
1489 LockSemaphoreInfo(policy_semaphore);
1490 if (policy_cache != (LinkedListInfo *) NULL)
1491 policy_cache=DestroyLinkedList(policy_cache,DestroyPolicyElement);
1492 UnlockSemaphoreInfo(policy_semaphore);
1493 RelinquishSemaphoreInfo(&policy_semaphore);
1494}
1495␌
1496/*
1497%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1498% %
1499% %
1500% %
1501% S e t M a g i c k S e c u r i t y P o l i c y %
1502% %
1503% %
1504% %
1505%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1506%
1507% SetMagickSecurityPolicy() sets or restricts the ImageMagick security policy.
1508% It returns MagickFalse if the policy does not parse.
1509%
1510% The format of the SetMagickSecurityPolicy method is:
1511%
1512% MagickBooleanType SetMagickSecurityPolicy(const char *policy,
1513% ExceptionInfo *exception)
1514%
1515% A description of each parameter follows:
1516%
1517% o policy: the security policy in the XML format.
1518%
1519% o exception: return any errors or warnings in this structure.
1520%
1521*/
1522
1523static MagickBooleanType ValidateSecurityPolicy(const char *policy,
1524 const char *url,ExceptionInfo *exception)
1525{
1526#if defined(MAGICKCORE_XML_DELEGATE)
1527 xmlDocPtr
1528 document;
1529
1530 /*
1531 Parse security policy (disable network and entity expansion for XXE safety).
1532 */
1533 document=xmlReadMemory(policy,(int) strlen(policy),url,NULL,
1534 XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_NONET);
1535 if (document == (xmlDocPtr) NULL)
1536 {
1537 (void) ThrowMagickException(exception,GetMagickModule(),ConfigureError,
1538 "PolicyValidationException","'%s'",url);
1539 return(MagickFalse);
1540 }
1541 xmlFreeDoc(document);
1542#else
1543 (void) policy;
1544 (void) url;
1545 (void) exception;
1546#endif
1547 return(MagickTrue);
1548}
1549
1550MagickExport MagickBooleanType SetMagickSecurityPolicy(const char *policy,
1551 ExceptionInfo *exception)
1552{
1553 MagickBooleanType
1554 status;
1555
1556 LinkedListInfo
1557 *user_policies;
1558
1559 PolicyInfo
1560 *p;
1561
1562 /*
1563 Load user policies.
1564 */
1565 assert(exception != (ExceptionInfo *) NULL);
1566 if (policy == (const char *) NULL)
1567 return(MagickFalse);
1568 if (ValidateSecurityPolicy(policy,PolicyFilename,exception) == MagickFalse)
1569 return(MagickFalse);
1570 if (IsPolicyCacheInstantiated(exception) == MagickFalse)
1571 return(MagickFalse);
1572 /*
1573 Parse and synchronize user policies in a single pass before committing
1574 them to the live policy_cache.
1575 */
1576 user_policies=NewLinkedList(0);
1577 if (user_policies == (LinkedListInfo *) NULL)
1578 return(MagickFalse);
1579 status=LoadPolicyCache(user_policies,policy,"[user-policy]",0,exception);
1580 if (status == MagickFalse)
1581 {
1582 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1583 return(MagickFalse);
1584 }
1585 LockSemaphoreInfo(policy_semaphore);
1586 status=LoadPolicyCache(policy_cache,policy,"[user-policy]",0,exception);
1587 UnlockSemaphoreInfo(policy_semaphore);
1588 if (status == MagickFalse)
1589 {
1590 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1591 return(MagickFalse);
1592 }
1593 ResetLinkedListIterator(user_policies);
1594 p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
1595 while (p != (PolicyInfo *) NULL)
1596 {
1597 if ((p->name != (char *) NULL) && (p->value != (char *) NULL))
1598 (void) SetMagickSecurityPolicyValue(p->domain,p->name,p->value,exception);
1599 p=(PolicyInfo *) GetNextValueInLinkedList(user_policies);
1600 }
1601 user_policies=DestroyLinkedList(user_policies,DestroyPolicyElement);
1602 return(status);
1603}
1604␌
1605/*
1606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1607% %
1608% %
1609% %
1610% 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 %
1611% %
1612% %
1613% %
1614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1615%
1616% SetMagickSecurityPolicyValue() sets a value associated with an ImageMagick
1617% security policy. For most policies, the value must be less than any value
1618% set by the security policy configuration file (i.e. policy.xml). It returns
1619% MagickFalse if the policy cannot be modified or if the policy does not parse.
1620%
1621% The format of the SetMagickSecurityPolicyValue method is:
1622%
1623% MagickBooleanType SetMagickSecurityPolicyValue(
1624% const PolicyDomain domain,const char *name,const char *value,
1625% ExceptionInfo *exception)
1626%
1627% A description of each parameter follows:
1628%
1629% o domain: the domain of the policy (e.g. system, resource).
1630%
1631% o name: the name of the policy.
1632%
1633% o value: the value to set the policy to.
1634%
1635% o exception: return any errors or warnings in this structure.
1636%
1637*/
1638MagickExport MagickBooleanType SetMagickSecurityPolicyValue(
1639 const PolicyDomain domain,const char *name,const char *value,
1640 ExceptionInfo *exception)
1641{
1642 magick_unreferenced(exception);
1643 assert(exception != (ExceptionInfo *) NULL);
1644 if ((name == (const char *) NULL) || (value == (const char *) NULL))
1645 return(MagickFalse);
1646 switch (domain)
1647 {
1648 case CachePolicyDomain:
1649 {
1650 if (LocaleCompare(name,"memory-map") == 0)
1651 {
1652 if (LocaleCompare(value,"anonymous") != 0)
1653 return(MagickFalse);
1654 ResetCacheAnonymousMemory();
1655 ResetStreamAnonymousMemory();
1656 return(MagickTrue);
1657 }
1658 break;
1659 }
1660 case ResourcePolicyDomain:
1661 {
1662 ssize_t
1663 type;
1664
1665 type=ParseCommandOption(MagickResourceOptions,MagickFalse,name);
1666 if (type >= 0)
1667 {
1668 MagickSizeType
1669 limit;
1670
1671 limit=MagickResourceInfinity;
1672 if (LocaleCompare("unlimited",value) != 0)
1673 limit=StringToMagickSizeType(value,100.0);
1674 if ((ResourceType) type == TimeResource)
1675 limit=(MagickSizeType) ParseMagickTimeToLive(value);
1676 return(SetMagickResourceLimit((ResourceType) type,limit));
1677 }
1678 break;
1679 }
1680 case SystemPolicyDomain:
1681 {
1682 if (LocaleCompare(name,"max-memory-request") == 0)
1683 {
1684 MagickSizeType
1685 limit;
1686
1687 limit=MagickResourceInfinity;
1688 if (LocaleCompare("unlimited",value) != 0)
1689 limit=StringToMagickSizeType(value,100.0);
1690 SetMaxMemoryRequest(limit);
1691 return(MagickTrue);
1692 }
1693 if (LocaleCompare(name,"max-profile-size") == 0)
1694 {
1695 MagickSizeType
1696 limit;
1697
1698 limit=MagickResourceInfinity;
1699 if (LocaleCompare("unlimited",value) != 0)
1700 limit=StringToMagickSizeType(value,100.0);
1701 SetMaxProfileSize(limit);
1702 return(MagickTrue);
1703 }
1704 if (LocaleCompare(name,"memory-map") == 0)
1705 {
1706 if (LocaleCompare(value,"anonymous") != 0)
1707 return(MagickFalse);
1708 ResetVirtualAnonymousMemory();
1709 return(MagickTrue);
1710 }
1711 if (LocaleCompare(name,"precision") == 0)
1712 {
1713 int
1714 limit;
1715
1716 limit=StringToInteger(value);
1717 SetMagickPrecision(limit);
1718 return(MagickTrue);
1719 }
1720 break;
1721 }
1722 case CoderPolicyDomain:
1723 case DelegatePolicyDomain:
1724 case FilterPolicyDomain:
1725 case ModulePolicyDomain:
1726 case PathPolicyDomain:
1727 default:
1728 break;
1729 }
1730 return(MagickFalse);
1731}