As an example, suppose you download an image from the internet and unbeknownst to you its been crafted to generate a 20000 by 20000 pixel image. ImageMagick attempts to allocate enough resources (memory, disk) and your system will likely deny the resource request and exit. However, its also possible that your computer might be temporarily sluggish or unavailable or ImageMagick may abort. To prevent such a scenario, you can set limits in the policy.xml configuration file. You may ask why ImageMagick does not already include reasonable limits? Simply because what is reasonable in your environment, might not be reasonable to someone else. For example, you may have ImageMagick sandboxed where security is not a concern, whereas another user may use ImageMagick to process images on their publically accessible website. Or ImageMagick is running on a host with 1TB of memory whereas another ImageMagick instance is running on an iPhone. By policy, permitting giga-pixel image processing on the large memory host makes sense, not so much for the resource constrained iPhone. If you utilize ImageMagick from a public website, you may want to increase security by preventing usage of the MVG or HTTPS coders. Only you can decide what are reasonable limits taking in consideration your environment. We provide this policy with reasonable limits and encourage you to modify it to suit your local environment:
Code: Select all
<policymap>
<policy domain="resource" name="temporary-path" value="/tmp"/>
<policy domain="resource" name="memory" value="256MiB"/>
<policy domain="resource" name="map" value="512MiB"/>
<policy domain="resource" name="width" value="8KP"/>
<policy domain="resource" name="height" value="8KP"/>
<policy domain="resource" name="area" value="16KP"/>
<policy domain="resource" name="disk" value="1GiB"/>
<policy domain="resource" name="file" value="768"/>
<policy domain="resource" name="thread" value="2"/>
<policy domain="resource" name="throttle" value="0"/>
<policy domain="resource" name="time" value="120"/>
<policy domain="system" name="precision" value="6"/>
<policy domain="cache" name="shared-secret" stealth="true" value="replace with your secret phrase"/>
<policy domain="coder" rights="none" pattern="MVG" />
<policy domain="coder" rights="none" pattern="EPS" />
<policy domain="coder" rights="none" pattern="PS" />
<policy domain="coder" rights="none" pattern="PS2" />
<policy domain="coder" rights="none" pattern="PS3" />
<policy domain="coder" rights="none" pattern="PDF" />
<policy domain="coder" rights="none" pattern="XPS" />
<policy domain="delegate" rights="none" pattern="HTTPS" />
<policy domain="path" rights="none" pattern="@*"/>
</policymap>
Here is what you can expect when you restrict the HTTPS coder, for example:
Code: Select all
$ convert https://www.imagemagick.org/image/wizard.png wizard.jpg
convert: not authorized `HTTPS'
convert: unable to open file: No such file or directory
convert: no images defined `wizard.jpg'
Code: Select all
<policy domain="delegate" rights="none" pattern="*" />
<policy domain="coder" rights="none" pattern="*" />
<policy domain="coder" rights="read | write" pattern="{GIF,JPEG,PNG,WEBP}" />
Code: Select all
<policy domain="system" name="memory-map" value="anonymous"/>
<policy domain="cache" name="memory-map" value="anonymous"/>
<policy domain="system" name="shred" value="1"/>
Code: Select all
<policy domain="system" name="max-memory-request" value="256MiB"/>
Code: Select all
-> identify -list policy
Path: ImageMagick/policy.xml
Policy: Resource
name: time
value: 120
Policy: Resource
name: throttle
value: 0
Policy: Resource
name: thread
value: 2
Policy: Resource
name: file
value: 768
Policy: Resource
name: disk
value: 1GiB
Policy: Resource
name: map
value: 512MiB
Policy: Resource
name: memory
value: 256MiB
Policy: Resource
name: area
value: 16KP
Policy: Resource
name: height
value: 8KP
Policy: Resource
name: width
value: 8KP
Policy: Resource
name: temporary-path
value: /tmp
Policy: System
name: precision
value: 6
Policy: Path
rights: None
pattern: @*
Path: [built-in]
Policy: Undefined
rights: None
As of ImageMagick 7.0.6-0 and 6.9.8-10, you can programmatically set the ImageMagick security policy with SetMagickSecurityPolicy() (MagickCore) or MagickSetSecurityPolicy() (MagickWand).
For additional details about resource limits and the policy configuration file, read Resources and Architecture.
Zero Configuration Security Policy
A zero configuration build of ImageMagick does not permit external configuration files. To define your security policy, you must instead edit the MagickCore/policy-private.h source module, add your policy statements, and then build the ImageMagick distribution. Here is an example zero configuration security policy:
Code: Select all
static const char
*ZeroConfigurationPolicy = \
"<policymap> \
<policy domain=\"coder\" rights=\"none\" pattern=\"MVG\"/> \
</policymap>";
If you spot a security flaw in ImageMagick, contact us and select Security Issue as the issue. Alternatively, post your concern to GitHub. Be sure to include how to reproduce the security flaw and a link to any images needed to reproduce the flaw.
In addition to the security policy, you can make ImageMagick safer by ...
- keeping ImageMagick up-to-date. The latest releases have fixes for any security flaws we discovered in the past.
- sanitizing any filenames or command line options you pass to ImageMagick.
- running ImageMagick in a sanitized software container such as Docker.
- running ImageMagick as the least-privileged user (e.g. 'nobody').
- explicitly setting the image file type. For example, use the filename png:image.png rather than image.png. Without an explicit image type in the filename, ImageMagick guesses the image type.