| 1 |
'\" |
| 2 |
'\" Copyright (c) 1995-1996 Sun Microsystems, Inc. |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH "Safe Tcl" n 8.0 Tcl "Tcl Built-In Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
safe \- Creating and manipulating safe interpreters |
| 13 |
.SH SYNOPSIS |
| 14 |
\fB::safe::interpCreate\fR ?\fIchild\fR? ?\fIoptions...\fR? |
| 15 |
.sp |
| 16 |
\fB::safe::interpInit\fR \fIchild\fR ?\fIoptions...\fR? |
| 17 |
.sp |
| 18 |
\fB::safe::interpConfigure\fR \fIchild\fR ?\fIoptions...\fR? |
| 19 |
.sp |
| 20 |
\fB::safe::interpDelete\fR \fIchild\fR |
| 21 |
.sp |
| 22 |
\fB::safe::interpAddToAccessPath\fR \fIchild\fR \fIdirectory\fR |
| 23 |
.sp |
| 24 |
\fB::safe::interpFindInAccessPath\fR \fIchild\fR \fIdirectory\fR |
| 25 |
.sp |
| 26 |
\fB::safe::setLogCmd\fR ?\fIcmd arg...\fR? |
| 27 |
.SS OPTIONS |
| 28 |
.PP |
| 29 |
?\fB\-accessPath\fR \fIpathList\fR? |
| 30 |
?\fB\-statics\fR \fIboolean\fR? ?\fB\-noStatics\fR? |
| 31 |
?\fB\-nested\fR \fIboolean\fR? ?\fB\-nestedLoadOk\fR? |
| 32 |
?\fB\-deleteHook\fR \fIscript\fR? |
| 33 |
.BE |
| 34 |
.SH DESCRIPTION |
| 35 |
Safe Tcl is a mechanism for executing untrusted Tcl scripts |
| 36 |
safely and for providing mediated access by such scripts to |
| 37 |
potentially dangerous functionality. |
| 38 |
.PP |
| 39 |
Safe Tcl ensures that untrusted Tcl scripts cannot harm the |
| 40 |
hosting application. |
| 41 |
It prevents integrity and privacy attacks. Untrusted Tcl |
| 42 |
scripts are prevented from corrupting the state of the hosting |
| 43 |
application or computer. Untrusted scripts are also prevented from |
| 44 |
disclosing information stored on the hosting computer or in the |
| 45 |
hosting application to any party. |
| 46 |
.PP |
| 47 |
Safe Tcl allows a parent interpreter to create safe, restricted |
| 48 |
interpreters that contain a set of predefined aliases for the \fBsource\fR, |
| 49 |
\fBload\fR, \fBfile\fR, \fBencoding\fR, and \fBexit\fR commands and |
| 50 |
are able to use the auto-loading and package mechanisms. |
| 51 |
.PP |
| 52 |
No knowledge of the file system structure is leaked to the |
| 53 |
safe interpreter, because it has access only to a virtualized path |
| 54 |
containing tokens. When the safe interpreter requests to source a file, it |
| 55 |
uses the token in the virtual path as part of the file name to source; the |
| 56 |
parent interpreter transparently |
| 57 |
translates the token into a real directory name and executes the |
| 58 |
requested operation (see the section \fBSECURITY\fR below for details). |
| 59 |
Different levels of security can be selected by using the optional flags |
| 60 |
of the commands described below. |
| 61 |
.PP |
| 62 |
All commands provided in the parent interpreter by Safe Tcl reside in |
| 63 |
the \fBsafe\fR namespace. |
| 64 |
.SH COMMANDS |
| 65 |
The following commands are provided in the parent interpreter: |
| 66 |
.TP |
| 67 |
\fB::safe::interpCreate\fR ?\fIchild\fR? ?\fIoptions...\fR? |
| 68 |
Creates a safe interpreter, installs the aliases described in the section |
| 69 |
\fBALIASES\fR and initializes the auto-loading and package mechanism as |
| 70 |
specified by the supplied \fIoptions\fR. |
| 71 |
See the \fBOPTIONS\fR section below for a description of the |
| 72 |
optional arguments. |
| 73 |
If the \fIchild\fR argument is omitted, a name will be generated. |
| 74 |
\fB::safe::interpCreate\fR always returns the interpreter name. |
| 75 |
.sp |
| 76 |
The interpreter name \fIchild\fR may include namespace separators, |
| 77 |
but may not have leading or trailing namespace separators, or excess |
| 78 |
colon characters in namespace separators. The interpreter name is |
| 79 |
qualified relative to the global namespace ::, not the namespace in which |
| 80 |
the \fB::safe::interpCreate\fR command is evaluated. |
| 81 |
.TP |
| 82 |
\fB::safe::interpInit\fR \fIchild\fR ?\fIoptions...\fR? |
| 83 |
This command is similar to \fBinterpCreate\fR except it that does not |
| 84 |
create the safe interpreter. \fIchild\fR must have been created by some |
| 85 |
other means, like \fBinterp create\fR \fB\-safe\fR. The interpreter |
| 86 |
name \fIchild\fR may include namespace separators, subject to the same |
| 87 |
restrictions as for \fBinterpCreate\fR. |
| 88 |
.TP |
| 89 |
\fB::safe::interpConfigure\fR \fIchild\fR ?\fIoptions...\fR? |
| 90 |
If no \fIoptions\fR are given, returns the settings for all options for the |
| 91 |
named safe interpreter as a list of options and their current values |
| 92 |
for that \fIchild\fR. |
| 93 |
If a single additional argument is provided, |
| 94 |
it will return a list of 2 elements \fIname\fR and \fIvalue\fR where |
| 95 |
\fIname\fR is the full name of that option and \fIvalue\fR the current value |
| 96 |
for that option and the \fIchild\fR. |
| 97 |
If more than two additional arguments are provided, it will reconfigure the |
| 98 |
safe interpreter and change each and only the provided options. |
| 99 |
See the section on \fBOPTIONS\fR below for options description. |
| 100 |
Example of use: |
| 101 |
.RS |
| 102 |
.PP |
| 103 |
.CS |
| 104 |
# Create new interp with the same configuration as "$i0": |
| 105 |
set i1 [safe::interpCreate {*}[safe::interpConfigure $i0]] |
| 106 |
|
| 107 |
# Get the current deleteHook |
| 108 |
set dh [safe::interpConfigure $i0 \-del] |
| 109 |
|
| 110 |
# Change (only) the statics loading ok attribute of an |
| 111 |
# interp and its deleteHook (leaving the rest unchanged): |
| 112 |
safe::interpConfigure $i0 \-delete {foo bar} \-statics 0 |
| 113 |
.CE |
| 114 |
.RE |
| 115 |
.TP |
| 116 |
\fB::safe::interpDelete\fR \fIchild\fR |
| 117 |
Deletes the safe interpreter and cleans up the corresponding |
| 118 |
parent interpreter data structures. |
| 119 |
If a \fIdeleteHook\fR script was specified for this interpreter it is |
| 120 |
evaluated before the interpreter is deleted, with the name of the |
| 121 |
interpreter as an additional argument. |
| 122 |
.TP |
| 123 |
\fB::safe::interpFindInAccessPath\fR \fIchild\fR \fIdirectory\fR |
| 124 |
This command finds and returns the token for the real directory |
| 125 |
\fIdirectory\fR in the safe interpreter's current virtual access path. |
| 126 |
It generates an error if the directory is not found. |
| 127 |
Example of use: |
| 128 |
.RS |
| 129 |
.PP |
| 130 |
.CS |
| 131 |
$child eval [list set tk_library \e |
| 132 |
[::safe::interpFindInAccessPath $name $tk_library]] |
| 133 |
.CE |
| 134 |
.RE |
| 135 |
.TP |
| 136 |
\fB::safe::interpAddToAccessPath\fR \fIchild\fR \fIdirectory\fR |
| 137 |
This command adds \fIdirectory\fR to the virtual path maintained for the |
| 138 |
safe interpreter in the parent, and returns the token that can be used in |
| 139 |
the safe interpreter to obtain access to files in that directory. |
| 140 |
If the directory is already in the virtual path, it only returns the token |
| 141 |
without adding the directory to the virtual path again. |
| 142 |
Example of use: |
| 143 |
.RS |
| 144 |
.PP |
| 145 |
.CS |
| 146 |
$child eval [list set tk_library \e |
| 147 |
[::safe::interpAddToAccessPath $name $tk_library]] |
| 148 |
.CE |
| 149 |
.RE |
| 150 |
.TP |
| 151 |
\fB::safe::setLogCmd\fR ?\fIcmd arg...\fR? |
| 152 |
This command installs a script that will be called when interesting |
| 153 |
life cycle events occur for a safe interpreter. |
| 154 |
When called with no arguments, it returns the currently installed script. |
| 155 |
When called with one argument, an empty string, the currently installed |
| 156 |
script is removed and logging is turned off. |
| 157 |
The script will be invoked with one additional argument, a string |
| 158 |
describing the event of interest. |
| 159 |
The main purpose is to help in debugging safe interpreters. |
| 160 |
Using this facility you can get complete error messages while the safe |
| 161 |
interpreter gets only generic error messages. |
| 162 |
This prevents a safe interpreter from seeing messages about failures |
| 163 |
and other events that might contain sensitive information such as real |
| 164 |
directory names. |
| 165 |
.RS |
| 166 |
.PP |
| 167 |
Example of use: |
| 168 |
.PP |
| 169 |
.CS |
| 170 |
::safe::setLogCmd puts stderr |
| 171 |
.CE |
| 172 |
.PP |
| 173 |
Below is the output of a sample session in which a safe interpreter |
| 174 |
attempted to source a file not found in its virtual access path. |
| 175 |
Note that the safe interpreter only received an error message saying that |
| 176 |
the file was not found: |
| 177 |
.PP |
| 178 |
.CS |
| 179 |
NOTICE for child interp10 : Created |
| 180 |
NOTICE for child interp10 : Setting accessPath=(/foo/bar) staticsok=1 nestedok=0 deletehook=() |
| 181 |
NOTICE for child interp10 : auto_path in interp10 has been set to {$p(:0:)} |
| 182 |
ERROR for child interp10 : /foo/bar/init.tcl: no such file or directory |
| 183 |
.CE |
| 184 |
.RE |
| 185 |
.SS OPTIONS |
| 186 |
The following options are common to |
| 187 |
\fB::safe::interpCreate\fR, \fB::safe::interpInit\fR, |
| 188 |
and \fB::safe::interpConfigure\fR. |
| 189 |
Any option name can be abbreviated to its minimal |
| 190 |
non-ambiguous name. |
| 191 |
Option names are not case sensitive. |
| 192 |
.TP |
| 193 |
\fB\-accessPath\fR \fIdirectoryList\fR |
| 194 |
This option sets the list of directories from which the safe interpreter |
| 195 |
can \fBsource\fR and \fBload\fR files. |
| 196 |
If this option is not specified, or if it is given as the |
| 197 |
empty list, the safe interpreter will use the same directories as its |
| 198 |
parent for auto-loading. |
| 199 |
See the section \fBSECURITY\fR below for more detail about virtual paths, |
| 200 |
tokens and access control. |
| 201 |
.TP |
| 202 |
\fB\-statics\fR \fIboolean\fR |
| 203 |
This option specifies if the safe interpreter will be allowed |
| 204 |
to load statically linked packages (like \fBload {} Tk\fR). |
| 205 |
The default value is \fBtrue\fR : |
| 206 |
safe interpreters are allowed to load statically linked packages. |
| 207 |
.TP |
| 208 |
\fB\-noStatics\fR |
| 209 |
This option is a convenience shortcut for \fB\-statics false\fR and |
| 210 |
thus specifies that the safe interpreter will not be allowed |
| 211 |
to load statically linked packages. |
| 212 |
.TP |
| 213 |
\fB\-nested\fR \fIboolean\fR |
| 214 |
This option specifies if the safe interpreter will be allowed |
| 215 |
to load packages into its own sub-interpreters. |
| 216 |
The default value is \fBfalse\fR : |
| 217 |
safe interpreters are not allowed to load packages into |
| 218 |
their own sub-interpreters. |
| 219 |
.TP |
| 220 |
\fB\-nestedLoadOk\fR |
| 221 |
This option is a convenience shortcut for \fB\-nested true\fR and |
| 222 |
thus specifies the safe interpreter will be allowed |
| 223 |
to load packages into its own sub-interpreters. |
| 224 |
.TP |
| 225 |
\fB\-deleteHook\fR \fIscript\fR |
| 226 |
When this option is given a non-empty \fIscript\fR, it will be |
| 227 |
evaluated in the parent with the name of |
| 228 |
the safe interpreter as an additional argument |
| 229 |
just before actually deleting the safe interpreter. |
| 230 |
Giving an empty value removes any currently installed deletion hook |
| 231 |
script for that safe interpreter. |
| 232 |
The default value (\fB{}\fR) is not to have any deletion call back. |
| 233 |
.SH ALIASES |
| 234 |
The following aliases are provided in a safe interpreter: |
| 235 |
.TP |
| 236 |
\fBsource\fR \fIfileName\fR |
| 237 |
The requested file, a Tcl source file, is sourced into the safe interpreter |
| 238 |
if it is found. |
| 239 |
The \fBsource\fR alias can only source files from directories in |
| 240 |
the virtual path for the safe interpreter. The \fBsource\fR alias requires |
| 241 |
the safe interpreter to |
| 242 |
use one of the token names in its virtual path to denote the directory in |
| 243 |
which the file to be sourced can be found. |
| 244 |
See the section on \fBSECURITY\fR for more discussion of restrictions on |
| 245 |
valid filenames. |
| 246 |
.TP |
| 247 |
\fBload\fR \fIfileName\fR |
| 248 |
The requested file, a shared object file, is dynamically loaded into the |
| 249 |
safe interpreter if it is found. |
| 250 |
The filename must contain a token name mentioned in the virtual path for |
| 251 |
the safe interpreter for it to be found successfully. |
| 252 |
Additionally, the shared object file must contain a safe entry point; see |
| 253 |
the manual page for the \fBload\fR command for more details. |
| 254 |
.TP |
| 255 |
\fBfile\fR ?\fIsubCmd args...\fR? |
| 256 |
The \fBfile\fR alias provides access to a safe subset of the subcommands of |
| 257 |
the \fBfile\fR command; it allows only \fBdirname\fR, \fBjoin\fR, |
| 258 |
\fBextension\fR, \fBroot\fR, \fBtail\fR, \fBpathtype\fR and \fBsplit\fR |
| 259 |
subcommands. For more details on what these subcommands do see the manual |
| 260 |
page for the \fBfile\fR command. |
| 261 |
.TP |
| 262 |
\fBencoding\fR ?\fIsubCmd args...\fR? |
| 263 |
The \fBencoding\fR alias provides access to a safe subset of the |
| 264 |
subcommands of the \fBencoding\fR command; it disallows setting of |
| 265 |
the system encoding, but allows all other subcommands including |
| 266 |
\fBsystem\fR to check the current encoding. |
| 267 |
.TP |
| 268 |
\fBexit\fR |
| 269 |
The calling interpreter is deleted and its computation is stopped, but the |
| 270 |
Tcl process in which this interpreter exists is not terminated. |
| 271 |
.SH SECURITY |
| 272 |
Safe Tcl does not attempt to completely prevent annoyance and |
| 273 |
denial of service attacks. These forms of attack prevent the |
| 274 |
application or user from temporarily using the computer to perform |
| 275 |
useful work, for example by consuming all available CPU time or |
| 276 |
all available screen real estate. |
| 277 |
These attacks, while aggravating, are deemed to be of lesser importance |
| 278 |
in general than integrity and privacy attacks that Safe Tcl |
| 279 |
is to prevent. |
| 280 |
.PP |
| 281 |
The commands available in a safe interpreter, in addition to |
| 282 |
the safe set as defined in \fBinterp\fR manual page, are mediated aliases |
| 283 |
for \fBsource\fR, \fBload\fR, \fBexit\fR, and safe subsets of |
| 284 |
\fBfile\fR and \fBencoding\fR. The safe interpreter can also auto-load |
| 285 |
code and it can request that packages be loaded. |
| 286 |
.PP |
| 287 |
Because some of these commands access the local file system, there is a |
| 288 |
potential for information leakage about its directory structure. |
| 289 |
To prevent this, commands that take file names as arguments in a safe |
| 290 |
interpreter use tokens instead of the real directory names. |
| 291 |
These tokens are translated to the real directory name while a request to, |
| 292 |
e.g., source a file is mediated by the parent interpreter. |
| 293 |
This virtual path system is maintained in the parent interpreter for each safe |
| 294 |
interpreter created by \fB::safe::interpCreate\fR or initialized by |
| 295 |
\fB::safe::interpInit\fR and |
| 296 |
the path maps tokens accessible in the safe interpreter into real path |
| 297 |
names on the local file system thus preventing safe interpreters |
| 298 |
from gaining knowledge about the |
| 299 |
structure of the file system of the host on which the interpreter is |
| 300 |
executing. |
| 301 |
The only valid file names arguments |
| 302 |
for the \fBsource\fR and \fBload\fR aliases provided to the child |
| 303 |
are path in the form of |
| 304 |
\fB[file join \fItoken filename\fB]\fR (i.e. when using the |
| 305 |
native file path formats: \fItoken\fB/\fIfilename\fR |
| 306 |
on Unix and \fItoken\fB\e\fIfilename\fR on Windows), |
| 307 |
where \fItoken\fR is representing one of the directories |
| 308 |
of the \fIaccessPath\fR list and \fIfilename\fR is |
| 309 |
one file in that directory (no sub directories access are allowed). |
| 310 |
.PP |
| 311 |
When a token is used in a safe interpreter in a request to source or |
| 312 |
load a file, the token is checked and |
| 313 |
translated to a real path name and the file to be |
| 314 |
sourced or loaded is located on the file system. |
| 315 |
The safe interpreter never gains knowledge of the actual path name under |
| 316 |
which the file is stored on the file system. |
| 317 |
.PP |
| 318 |
To further prevent potential information leakage from sensitive files that |
| 319 |
are accidentally included in the set of files that can be sourced by a safe |
| 320 |
interpreter, the \fBsource\fR alias restricts access to files |
| 321 |
meeting the following constraints: the file name must |
| 322 |
fourteen characters or shorter, must not contain more than one dot |
| 323 |
.PQ \fB.\fR "" , |
| 324 |
must end up with the extension |
| 325 |
.PQ \fB.tcl\fR |
| 326 |
or be called |
| 327 |
.PQ \fBtclIndex\fR . |
| 328 |
.PP |
| 329 |
Each element of the initial access path |
| 330 |
list will be assigned a token that will be set in |
| 331 |
the child \fBauto_path\fR and the first element of that list will be set as |
| 332 |
the \fBtcl_library\fR for that child. |
| 333 |
.PP |
| 334 |
If the access path argument is not given or is the empty list, |
| 335 |
the default behavior is to let the child access the same packages |
| 336 |
as the parent has access to (Or to be more precise: |
| 337 |
only packages written in Tcl (which by definition cannot be dangerous |
| 338 |
as they run in the child interpreter) and C extensions that |
| 339 |
provides a _SafeInit entry point). For that purpose, the parent's |
| 340 |
\fBauto_path\fR will be used to construct the child access path. |
| 341 |
In order that the child successfully loads the Tcl library files |
| 342 |
(which includes the auto-loading mechanism itself) the \fBtcl_library\fR will be |
| 343 |
added or moved to the first position if necessary, in the |
| 344 |
child access path, so the child |
| 345 |
\fBtcl_library\fR will be the same as the parent's (its real |
| 346 |
path will still be invisible to the child though). |
| 347 |
In order that auto-loading works the same for the child and |
| 348 |
the parent in this by default case, the first-level |
| 349 |
sub directories of each directory in the parent \fBauto_path\fR will |
| 350 |
also be added (if not already included) to the child access path. |
| 351 |
You can always specify a more |
| 352 |
restrictive path for which sub directories will never be searched by |
| 353 |
explicitly specifying your directory list with the \fB\-accessPath\fR flag |
| 354 |
instead of relying on this default mechanism. |
| 355 |
.PP |
| 356 |
When the \fIaccessPath\fR is changed after the first creation or |
| 357 |
initialization (i.e. through \fBinterpConfigure -accessPath \fR\fIlist\fR), |
| 358 |
an \fBauto_reset\fR is automatically evaluated in the safe interpreter |
| 359 |
to synchronize its \fBauto_index\fR with the new token list. |
| 360 |
.SH "SEE ALSO" |
| 361 |
interp(n), library(n), load(n), package(n), source(n), unknown(n) |
| 362 |
.SH KEYWORDS |
| 363 |
alias, auto\-loading, auto_mkindex, load, parent interpreter, safe |
| 364 |
interpreter, child interpreter, source |
| 365 |
'\" Local Variables: |
| 366 |
'\" mode: nroff |
| 367 |
'\" End: |