CINXE.COM
Safe - Compile and execute code in restricted compartments - Perldoc Browser
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Safe - Compile and execute code in restricted compartments - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/Safe"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.20.3</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/Safe">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/Safe">5.40.1</a> <a class="dropdown-item" href="/5.40.0/Safe">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/Safe">5.38.3</a> <a class="dropdown-item" href="/5.38.2/Safe">5.38.2</a> <a class="dropdown-item" href="/5.38.1/Safe">5.38.1</a> <a class="dropdown-item" href="/5.38.0/Safe">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/Safe">5.36.3</a> <a class="dropdown-item" href="/5.36.2/Safe">5.36.2</a> <a class="dropdown-item" href="/5.36.1/Safe">5.36.1</a> <a class="dropdown-item" href="/5.36.0/Safe">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/Safe">5.34.3</a> <a class="dropdown-item" href="/5.34.2/Safe">5.34.2</a> <a class="dropdown-item" href="/5.34.1/Safe">5.34.1</a> <a class="dropdown-item" href="/5.34.0/Safe">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/Safe">5.32.1</a> <a class="dropdown-item" href="/5.32.0/Safe">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/Safe">5.30.3</a> <a class="dropdown-item" href="/5.30.2/Safe">5.30.2</a> <a class="dropdown-item" href="/5.30.1/Safe">5.30.1</a> <a class="dropdown-item" href="/5.30.0/Safe">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/Safe">5.28.3</a> <a class="dropdown-item" href="/5.28.2/Safe">5.28.2</a> <a class="dropdown-item" href="/5.28.1/Safe">5.28.1</a> <a class="dropdown-item" href="/5.28.0/Safe">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/Safe">5.26.3</a> <a class="dropdown-item" href="/5.26.2/Safe">5.26.2</a> <a class="dropdown-item" href="/5.26.1/Safe">5.26.1</a> <a class="dropdown-item" href="/5.26.0/Safe">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/Safe">5.24.4</a> <a class="dropdown-item" href="/5.24.3/Safe">5.24.3</a> <a class="dropdown-item" href="/5.24.2/Safe">5.24.2</a> <a class="dropdown-item" href="/5.24.1/Safe">5.24.1</a> <a class="dropdown-item" href="/5.24.0/Safe">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/Safe">5.22.4</a> <a class="dropdown-item" href="/5.22.3/Safe">5.22.3</a> <a class="dropdown-item" href="/5.22.2/Safe">5.22.2</a> <a class="dropdown-item" href="/5.22.1/Safe">5.22.1</a> <a class="dropdown-item" href="/5.22.0/Safe">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item active" href="/5.20.3/Safe">5.20.3</a> <a class="dropdown-item" href="/5.20.2/Safe">5.20.2</a> <a class="dropdown-item" href="/5.20.1/Safe">5.20.1</a> <a class="dropdown-item" href="/5.20.0/Safe">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/Safe">5.18.4</a> <a class="dropdown-item" href="/5.18.3/Safe">5.18.3</a> <a class="dropdown-item" href="/5.18.2/Safe">5.18.2</a> <a class="dropdown-item" href="/5.18.1/Safe">5.18.1</a> <a class="dropdown-item" href="/5.18.0/Safe">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/Safe">5.16.3</a> <a class="dropdown-item" href="/5.16.2/Safe">5.16.2</a> <a class="dropdown-item" href="/5.16.1/Safe">5.16.1</a> <a class="dropdown-item" href="/5.16.0/Safe">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/Safe">5.14.4</a> <a class="dropdown-item" href="/5.14.3/Safe">5.14.3</a> <a class="dropdown-item" href="/5.14.2/Safe">5.14.2</a> <a class="dropdown-item" href="/5.14.1/Safe">5.14.1</a> <a class="dropdown-item" href="/5.14.0/Safe">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/Safe">5.12.5</a> <a class="dropdown-item" href="/5.12.4/Safe">5.12.4</a> <a class="dropdown-item" href="/5.12.3/Safe">5.12.3</a> <a class="dropdown-item" href="/5.12.2/Safe">5.12.2</a> <a class="dropdown-item" href="/5.12.1/Safe">5.12.1</a> <a class="dropdown-item" href="/5.12.0/Safe">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/Safe">5.10.1</a> <a class="dropdown-item" href="/5.10.0/Safe">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/Safe">5.8.9</a> <a class="dropdown-item" href="/5.8.8/Safe">5.8.8</a> <a class="dropdown-item" href="/5.8.7/Safe">5.8.7</a> <a class="dropdown-item" href="/5.8.6/Safe">5.8.6</a> <a class="dropdown-item" href="/5.8.5/Safe">5.8.5</a> <a class="dropdown-item" href="/5.8.4/Safe">5.8.4</a> <a class="dropdown-item" href="/5.8.3/Safe">5.8.3</a> <a class="dropdown-item" href="/5.8.2/Safe">5.8.2</a> <a class="dropdown-item" href="/5.8.1/Safe">5.8.1</a> <a class="dropdown-item" href="/5.8.0/Safe">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/Safe">5.6.2</a> <a class="dropdown-item" href="/5.6.1/Safe">5.6.1</a> <a class="dropdown-item" href="/5.6.0/Safe">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/Safe">5.005_04</a> <a class="dropdown-item" href="/5.005_03/Safe">5.005_03</a> <a class="dropdown-item" href="/5.005_02/Safe">5.005_02</a> <a class="dropdown-item" href="/5.005_01/Safe">5.005_01</a> <a class="dropdown-item" href="/5.005/Safe">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/Safe">blead</a> <a class="dropdown-item" href="/5.41.10/Safe">5.41.10</a> <a class="dropdown-item" href="/5.41.9/Safe">5.41.9</a> <a class="dropdown-item" href="/5.41.8/Safe">5.41.8</a> <a class="dropdown-item" href="/5.41.7/Safe">5.41.7</a> <a class="dropdown-item" href="/5.41.6/Safe">5.41.6</a> <a class="dropdown-item" href="/5.41.5/Safe">5.41.5</a> <a class="dropdown-item" href="/5.41.4/Safe">5.41.4</a> <a class="dropdown-item" href="/5.41.3/Safe">5.41.3</a> <a class="dropdown-item" href="/5.41.2/Safe">5.41.2</a> <a class="dropdown-item" href="/5.41.1/Safe">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/Safe">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/Safe">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/Safe">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/Safe">5.39.10</a> <a class="dropdown-item" href="/5.39.9/Safe">5.39.9</a> <a class="dropdown-item" href="/5.39.8/Safe">5.39.8</a> <a class="dropdown-item" href="/5.39.7/Safe">5.39.7</a> <a class="dropdown-item" href="/5.39.6/Safe">5.39.6</a> <a class="dropdown-item" href="/5.39.5/Safe">5.39.5</a> <a class="dropdown-item" href="/5.39.4/Safe">5.39.4</a> <a class="dropdown-item" href="/5.39.3/Safe">5.39.3</a> <a class="dropdown-item" href="/5.39.2/Safe">5.39.2</a> <a class="dropdown-item" href="/5.39.1/Safe">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/Safe">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.20.3/perl">Perl</a> <a class="dropdown-item" href="/5.20.3/perlintro">Intro</a> <a class="dropdown-item" href="/5.20.3/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.20.3/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.20.3/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perlop">Operators</a> <a class="dropdown-item" href="/5.20.3/functions">Functions</a> <a class="dropdown-item" href="/5.20.3/variables">Variables</a> <a class="dropdown-item" href="/5.20.3/modules">Modules</a> <a class="dropdown-item" href="/5.20.3/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.20.3/perlcommunity">Community</a> <a class="dropdown-item" href="/5.20.3/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.20.3/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.20.3/Safe">Safe</a> <div id="more"> (<a href="/5.20.3/Safe.txt">source</a>, <a href="https://metacpan.org/pod/Safe">CPAN</a>) </div> <div id="moduleversion">version 2.37</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.20.3. <a href="/Safe">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#WARNING">WARNING</a> </li> <li> <a class="text-decoration-none" href="#METHODS">METHODS</a> <ul> <li> <a class="text-decoration-none" href="#permit-(OP,-...)">permit (OP, ...)</a> </li> <li> <a class="text-decoration-none" href="#permit_only-(OP,-...)">permit_only (OP, ...)</a> </li> <li> <a class="text-decoration-none" href="#deny-(OP,-...)">deny (OP, ...)</a> </li> <li> <a class="text-decoration-none" href="#deny_only-(OP,-...)">deny_only (OP, ...)</a> </li> <li> <a class="text-decoration-none" href="#trap-(OP,-...),-untrap-(OP,-...)">trap (OP, ...), untrap (OP, ...)</a> </li> <li> <a class="text-decoration-none" href="#share-(NAME,-...)">share (NAME, ...)</a> </li> <li> <a class="text-decoration-none" href="#share_from-(PACKAGE,-ARRAYREF)">share_from (PACKAGE, ARRAYREF)</a> </li> <li> <a class="text-decoration-none" href="#varglob-(VARNAME)">varglob (VARNAME)</a> </li> <li> <a class="text-decoration-none" href="#reval-(STRING,-STRICT)">reval (STRING, STRICT)</a> </li> <li> <a class="text-decoration-none" href="#rdo-(FILENAME)">rdo (FILENAME)</a> </li> <li> <a class="text-decoration-none" href="#root-(NAMESPACE)">root (NAMESPACE)</a> </li> <li> <a class="text-decoration-none" href="#mask-(MASK)">mask (MASK)</a> </li> <li> <a class="text-decoration-none" href="#wrap_code_ref-(CODEREF)">wrap_code_ref (CODEREF)</a> </li> <li> <a class="text-decoration-none" href="#wrap_code_refs_within-(...)">wrap_code_refs_within (...)</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#RISKS">RISKS</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>Safe - Compile and execute code in restricted compartments</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use Safe; $compartment = new Safe; $compartment->permit(qw(time sort :browse)); $result = $compartment->reval($unsafe_code);</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p>The Safe extension module allows the creation of compartments in which perl code can be evaluated. Each compartment has</p> <dl> <dt id="a-new-namespace"><a class="permalink" href="#a-new-namespace">#</a><a id="a"></a>a new namespace</dt> <dd> <p>The "root" of the namespace (i.e. "main::") is changed to a different package and code evaluated in the compartment cannot refer to variables outside this namespace, even with run-time glob lookups and other tricks.</p> <p>Code which is compiled outside the compartment can choose to place variables into (or <i>share</i> variables with) the compartment's namespace and only that data will be visible to code evaluated in the compartment.</p> <p>By default, the only variables shared with compartments are the "underscore" variables $_ and @_ (and, technically, the less frequently used %_, the _ filehandle and so on). This is because otherwise perl operators which default to $_ will not work and neither will the assignment of arguments to @_ on subroutine entry.</p> </dd> <dt id="an-operator-mask"><a class="permalink" href="#an-operator-mask">#</a><a id="an"></a>an operator mask</dt> <dd> <p>Each compartment has an associated "operator mask". Recall that perl code is compiled into an internal format before execution. Evaluating perl code (e.g. via "eval" or "do 'file'") causes the code to be compiled into an internal format and then, provided there was no error in the compilation, executed. Code evaluated in a compartment compiles subject to the compartment's operator mask. Attempting to evaluate code in a compartment which contains a masked operator will cause the compilation to fail with an error. The code will not be executed.</p> <p>The default operator mask for a newly created compartment is the ':default' optag.</p> <p>It is important that you read the <a href="/5.20.3/Opcode">Opcode</a> module documentation for more information, especially for detailed definitions of opnames, optags and opsets.</p> <p>Since it is only at the compilation stage that the operator mask applies, controlled access to potentially unsafe operations can be achieved by having a handle to a wrapper subroutine (written outside the compartment) placed into the compartment. For example,</p> <pre><code>$cpt = new Safe; sub wrapper { # vet arguments and perform potentially unsafe operations } $cpt->share('&wrapper');</code></pre> </dd> </dl> <h1 id="WARNING"><a class="permalink" href="#WARNING">#</a>WARNING</h1> <p>The authors make <b>no warranty</b>, implied or otherwise, about the suitability of this software for safety or security purposes.</p> <p>The authors shall not in any case be liable for special, incidental, consequential, indirect or other similar damages arising from the use of this software.</p> <p>Your mileage will vary. If in any doubt <b>do not use it</b>.</p> <h1 id="METHODS"><a class="permalink" href="#METHODS">#</a>METHODS</h1> <p>To create a new compartment, use</p> <pre><code>$cpt = new Safe;</code></pre> <p>Optional argument is (NAMESPACE), where NAMESPACE is the root namespace to use for the compartment (defaults to "Safe::Root0", incremented for each new compartment).</p> <p>Note that version 1.00 of the Safe module supported a second optional parameter, MASK. That functionality has been withdrawn pending deeper consideration. Use the permit and deny methods described below.</p> <p>The following methods can then be used on the compartment object returned by the above constructor. The object argument is implicit in each case.</p> <h2 id="permit-(OP,-...)"><a class="permalink" href="#permit-(OP,-...)">#</a><a id="permit"></a><a id="permit-OP"></a>permit (OP, ...)</h2> <p>Permit the listed operators to be used when compiling code in the compartment (in <i>addition</i> to any operators already permitted).</p> <p>You can list opcodes by names, or use a tag name; see <a href="/5.20.3/Opcode#Predefined-Opcode-Tags">"Predefined Opcode Tags" in Opcode</a>.</p> <h2 id="permit_only-(OP,-...)"><a class="permalink" href="#permit_only-(OP,-...)">#</a><a id="permit_only"></a><a id="permit_only-OP"></a>permit_only (OP, ...)</h2> <p>Permit <i>only</i> the listed operators to be used when compiling code in the compartment (<i>no</i> other operators are permitted).</p> <h2 id="deny-(OP,-...)"><a class="permalink" href="#deny-(OP,-...)">#</a><a id="deny"></a><a id="deny-OP"></a>deny (OP, ...)</h2> <p>Deny the listed operators from being used when compiling code in the compartment (other operators may still be permitted).</p> <h2 id="deny_only-(OP,-...)"><a class="permalink" href="#deny_only-(OP,-...)">#</a><a id="deny_only"></a><a id="deny_only-OP"></a>deny_only (OP, ...)</h2> <p>Deny <i>only</i> the listed operators from being used when compiling code in the compartment (<i>all</i> other operators will be permitted, so you probably don't want to use this method).</p> <h2 id="trap-(OP,-...),-untrap-(OP,-...)"><a class="permalink" href="#trap-(OP,-...),-untrap-(OP,-...)">#</a><a id="trap"></a><a id="trap-OP-...-untrap-OP"></a>trap (OP, ...), untrap (OP, ...)</h2> <p>The trap and untrap methods are synonyms for deny and permit respectfully.</p> <h2 id="share-(NAME,-...)"><a class="permalink" href="#share-(NAME,-...)">#</a><a id="share"></a><a id="share-NAME"></a>share (NAME, ...)</h2> <p>This shares the variable(s) in the argument list with the compartment. This is almost identical to exporting variables using the <a href="/5.20.3/Exporter">Exporter</a> module.</p> <p>Each NAME must be the <b>name</b> of a non-lexical variable, typically with the leading type identifier included. A bareword is treated as a function name.</p> <p>Examples of legal names are '$foo' for a scalar, '@foo' for an array, '%foo' for a hash, '&foo' or 'foo' for a subroutine and '*foo' for a glob (i.e. all symbol table entries associated with "foo", including scalar, array, hash, sub and filehandle).</p> <p>Each NAME is assumed to be in the calling package. See share_from for an alternative method (which <code>share</code> uses).</p> <h2 id="share_from-(PACKAGE,-ARRAYREF)"><a class="permalink" href="#share_from-(PACKAGE,-ARRAYREF)">#</a><a id="share_from"></a><a id="share_from-PACKAGE-ARRAYREF"></a>share_from (PACKAGE, ARRAYREF)</h2> <p>This method is similar to share() but allows you to explicitly name the package that symbols should be shared from. The symbol names (including type characters) are supplied as an array reference.</p> <pre><code>$safe->share_from('main', [ '$foo', '%bar', 'func' ]);</code></pre> <p>Names can include package names, which are relative to the specified PACKAGE. So these two calls have the same effect:</p> <pre><code>$safe->share_from('Scalar::Util', [ 'reftype' ]); $safe->share_from('main', [ 'Scalar::Util::reftype' ]);</code></pre> <h2 id="varglob-(VARNAME)"><a class="permalink" href="#varglob-(VARNAME)">#</a><a id="varglob"></a><a id="varglob-VARNAME"></a>varglob (VARNAME)</h2> <p>This returns a glob reference for the symbol table entry of VARNAME in the package of the compartment. VARNAME must be the <b>name</b> of a variable without any leading type marker. For example:</p> <pre><code>${$cpt->varglob('foo')} = "Hello world";</code></pre> <p>has the same effect as:</p> <pre><code>$cpt = new Safe 'Root'; $Root::foo = "Hello world";</code></pre> <p>but avoids the need to know $cpt's package name.</p> <h2 id="reval-(STRING,-STRICT)"><a class="permalink" href="#reval-(STRING,-STRICT)">#</a><a id="reval"></a><a id="reval-STRING-STRICT"></a>reval (STRING, STRICT)</h2> <p>This evaluates STRING as perl code inside the compartment.</p> <p>The code can only see the compartment's namespace (as returned by the <b>root</b> method). The compartment's root package appears to be the <code>main::</code> package to the code inside the compartment.</p> <p>Any attempt by the code in STRING to use an operator which is not permitted by the compartment will cause an error (at run-time of the main program but at compile-time for the code in STRING). The error is of the form "'%s' trapped by operation mask...".</p> <p>If an operation is trapped in this way, then the code in STRING will not be executed. If such a trapped operation occurs or any other compile-time or return error, then $@ is set to the error message, just as with an eval().</p> <p>If there is no error, then the method returns the value of the last expression evaluated, or a return statement may be used, just as with subroutines and <b>eval()</b>. The context (list or scalar) is determined by the caller as usual.</p> <p>If the return value of reval() is (or contains) any code reference, those code references are wrapped to be themselves executed always in the compartment. See <a href="#wrap_code_refs_within">"wrap_code_refs_within"</a>.</p> <p>The formerly undocumented STRICT argument sets strictness: if true 'use strict;' is used, otherwise it uses 'no strict;'. <b>Note</b>: if STRICT is omitted 'no strict;' is the default.</p> <p>Some points to note:</p> <p>If the entereval op is permitted then the code can use eval "..." to 'hide' code which might use denied ops. This is not a major problem since when the code tries to execute the eval it will fail because the opmask is still in effect. However this technique would allow clever, and possibly harmful, code to 'probe' the boundaries of what is possible.</p> <p>Any string eval which is executed by code executing in a compartment, or by code called from code executing in a compartment, will be eval'd in the namespace of the compartment. This is potentially a serious problem.</p> <p>Consider a function foo() in package pkg compiled outside a compartment but shared with it. Assume the compartment has a root package called 'Root'. If foo() contains an eval statement like eval '$foo = 1' then, normally, $pkg::foo will be set to 1. If foo() is called from the compartment (by whatever means) then instead of setting $pkg::foo, the eval will actually set $Root::pkg::foo.</p> <p>This can easily be demonstrated by using a module, such as the Socket module, which uses eval "..." as part of an AUTOLOAD function. You can 'use' the module outside the compartment and share an (autoloaded) function with the compartment. If an autoload is triggered by code in the compartment, or by any code anywhere that is called by any means from the compartment, then the eval in the Socket module's AUTOLOAD function happens in the namespace of the compartment. Any variables created or used by the eval'd code are now under the control of the code in the compartment.</p> <p>A similar effect applies to <i>all</i> runtime symbol lookups in code called from a compartment but not compiled within it.</p> <h2 id="rdo-(FILENAME)"><a class="permalink" href="#rdo-(FILENAME)">#</a><a id="rdo"></a><a id="rdo-FILENAME"></a>rdo (FILENAME)</h2> <p>This evaluates the contents of file FILENAME inside the compartment. See above documentation on the <b>reval</b> method for further details.</p> <h2 id="root-(NAMESPACE)"><a class="permalink" href="#root-(NAMESPACE)">#</a><a id="root"></a><a id="root-NAMESPACE"></a>root (NAMESPACE)</h2> <p>This method returns the name of the package that is the root of the compartment's namespace.</p> <p>Note that this behaviour differs from version 1.00 of the Safe module where the root module could be used to change the namespace. That functionality has been withdrawn pending deeper consideration.</p> <h2 id="mask-(MASK)"><a class="permalink" href="#mask-(MASK)">#</a><a id="mask"></a><a id="mask-MASK"></a>mask (MASK)</h2> <p>This is a get-or-set method for the compartment's operator mask.</p> <p>With no MASK argument present, it returns the current operator mask of the compartment.</p> <p>With the MASK argument present, it sets the operator mask for the compartment (equivalent to calling the deny_only method).</p> <h2 id="wrap_code_ref-(CODEREF)"><a class="permalink" href="#wrap_code_ref-(CODEREF)">#</a><a id="wrap_code_ref"></a><a id="wrap_code_ref-CODEREF"></a>wrap_code_ref (CODEREF)</h2> <p>Returns a reference to an anonymous subroutine that, when executed, will call CODEREF with the Safe compartment 'in effect'. In other words, with the package namespace adjusted and the opmask enabled.</p> <p>Note that the opmask doesn't affect the already compiled code, it only affects any <i>further</i> compilation that the already compiled code may try to perform.</p> <p>This is particularly useful when applied to code references returned from reval().</p> <p>(It also provides a kind of workaround for RT#60374: "Safe.pm sort {} bug with -Dusethreads". See <a href="http://rt.perl.org/rt3//Public/Bug/Display.html?id=60374">http://rt.perl.org/rt3//Public/Bug/Display.html?id=60374</a> for <i>much</i> more detail.)</p> <h2 id="wrap_code_refs_within-(...)"><a class="permalink" href="#wrap_code_refs_within-(...)">#</a><a id="wrap_code_refs_within"></a>wrap_code_refs_within (...)</h2> <p>Wraps any CODE references found within the arguments by replacing each with the result of calling <a href="#wrap_code_ref">"wrap_code_ref"</a> on the CODE reference. Any ARRAY or HASH references in the arguments are inspected recursively.</p> <p>Returns nothing.</p> <h1 id="RISKS"><a class="permalink" href="#RISKS">#</a>RISKS</h1> <p>This section is just an outline of some of the things code in a compartment might do (intentionally or unintentionally) which can have an effect outside the compartment.</p> <dl> <dt id="Memory"><a class="permalink" href="#Memory">#</a>Memory</dt> <dd> <p>Consuming all (or nearly all) available memory.</p> </dd> <dt id="CPU"><a class="permalink" href="#CPU">#</a>CPU</dt> <dd> <p>Causing infinite loops etc.</p> </dd> <dt id="Snooping"><a class="permalink" href="#Snooping">#</a>Snooping</dt> <dd> <p>Copying private information out of your system. Even something as simple as your user name is of value to others. Much useful information could be gleaned from your environment variables for example.</p> </dd> <dt id="Signals"><a class="permalink" href="#Signals">#</a>Signals</dt> <dd> <p>Causing signals (especially SIGFPE and SIGALARM) to affect your process.</p> <p>Setting up a signal handler will need to be carefully considered and controlled. What mask is in effect when a signal handler gets called? If a user can get an imported function to get an exception and call the user's signal handler, does that user's restricted mask get re-instated before the handler is called? Does an imported handler get called with its original mask or the user's one?</p> </dd> <dt id="State-Changes"><a class="permalink" href="#State-Changes">#</a><a id="State"></a>State Changes</dt> <dd> <p>Ops such as chdir obviously effect the process as a whole and not just the code in the compartment. Ops such as rand and srand have a similar but more subtle effect.</p> </dd> </dl> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Originally designed and implemented by Malcolm Beattie.</p> <p>Reworked to use the Opcode module and other changes added by Tim Bunce.</p> <p>Currently maintained by the Perl 5 Porters, <perl5-porters@perl.org>.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>