Seth Woolley's Man Viewer

safe(n) - Safe Base, Safe Base - A mechanism for creating and manipulating safe interpreters - man n safe

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

Safe Tcl(n)                  Tcl Built-In Commands                 Safe Tcl(n)

       Safe Base  -  A  mechanism  for  creating  and manipulating safe inter-

       ::safe::interpCreate ?slave? ?options...?

       ::safe::interpInit slave ?options...?

       ::safe::interpConfigure slave ?options...?

       ::safe::interpDelete slave

       ::safe::interpAddToAccessPath slave directory

       ::safe::interpFindInAccessPath slave directory

       ::safe::setLogCmd ?cmd arg...?

       ?-accessPath  pathList?   ?-statics  boolean?  ?-noStatics?    ?-nested
       boolean? ?-nestedLoadOk?  ?-deleteHook script?

       Safe  Tcl is a mechanism for executing untrusted Tcl scripts safely and
       for providing mediated access(2,5) by such scripts to potentially  dangerous

       The  Safe Base ensures that untrusted Tcl scripts cannot harm the host-
       ing application.  The Safe Base prevents integrity and privacy attacks.
       Untrusted  Tcl  scripts  are prevented from corrupting the state of the
       hosting application or computer. Untrusted scripts are  also  prevented
       from  disclosing  information  stored on the hosting computer or in(1,8) the
       hosting application to any party.

       The Safe Base allows a master(5,8) interpreter to  create  safe,  restricted
       interpreters  that  contain a set(7,n,1 builtins) of predefined aliases for the source,
       load(7,n), file(1,n), encoding(3,n), and exit(3,n,1 builtins) commands and are able to use  the  auto-
       loading and package mechanisms.

       No  knowledge of the file(1,n) system structure is leaked to the safe inter-
       preter, because it has access(2,5) only to  a  virtualized  path  containing
       tokens.  When  the  safe interpreter requests to source a file(1,n), it uses
       the token in(1,8) the virtual(5,8) path as part of the file(1,n) name to  source;  the
       master(5,8)  interpreter  transparently  translates  the  token  into a real
       directory name and executes the requested operation  (see  the  section
       SECURITY  below  for  details).   Different  levels  of security can be
       selected by using the optional flags of the commands described below.

       All commands provided in(1,8) the master(5,8) interpreter by the Safe Base reside
       in(1,8) the safe namespace:

       The following commands are provided in(1,8) the master(5,8) interpreter:

       ::safe::interpCreate ?slave? ?options...?
              Creates  a  safe  interpreter, installs the aliases described in(1,8)
              the section ALIASES and initializes the auto-loading and package
              mechanism as specified by the supplied options.  See the OPTIONS
              section below for a description of the optional  arguments.   If
              the  slave  argument  is  omitted,  a  name  will  be generated.
              ::safe::interpCreate always returns the interpreter name.

       ::safe::interpInit slave ?options...?
              This command is similar to interpCreate except it that does  not
              create  the  safe  interpreter.  slave must have been created by
              some other means, like interp create -safe.

       ::safe::interpConfigure slave ?options...?
              If no options are given, returns the settings  for  all  options
              for  the  named(5,8)  safe interpreter as a list of options and their
              current values for that slave.  If a single additional  argument
              is  provided, it will return a list of 2 elements name and value
              where name is the full name of that option and value the current
              value  for  that  option  and the slave.  If more than two addi-
              tional arguments are provided,  it  will  reconfigure  the  safe
              interpreter  and change each and only the provided options.  See
              the section on OPTIONS below for options  description.   Example
              of use:
              # Create a new interp with the same configuration as "$i0" : set(7,n,1 builtins)
              i1 [eval safe::interpCreate [safe::interpConfigure $i0]]  #  Get
              the  current deleteHook set(7,n,1 builtins) dh [safe::interpConfigure $i0  -del]
              # Change (only) the statics loading ok attribute of an interp  #
              and  its deleteHook (leaving the rest unchanged) : safe::interp-
              Configure $i0  -delete {foo bar} -statics 0 ;

       ::safe::interpDelete slave
              Deletes the safe interpreter and  cleans  up  the  corresponding
              master(5,8)  interpreter data structures.  If a deleteHook script was
              specified for this interpreter it is evaluated before the inter-
              preter  is deleted, with the name of the interpreter as an addi-
              tional argument.

       ::safe::interpFindInAccessPath slave directory
              This command finds and returns the token for the real  directory
              directory in(1,8) the safe interpreter's current virtual(5,8) access(2,5) path.
              It generates an error(8,n) if(3,n) the directory is not found.  Example of
              $slave eval [list set(7,n,1 builtins) tk_library [::safe::interpFindInAccessPath
              $name $tk_library]]

       ::safe::interpAddToAccessPath slave directory
              This command adds directory to the virtual(5,8) path  maintained  for
              the  safe  interpreter in(1,8) the master(5,8), and returns the token that
              can be used in(1,8) the safe interpreter to obtain access(2,5) to files in(1,8)
              that  directory.   If  the  directory  is already in(1,8) the virtual(5,8)
              path, it only returns the token without adding the directory  to
              the virtual(5,8) path again.  Example of use:
              $slave  eval [list set(7,n,1 builtins) tk_library [::safe::interpAddToAccessPath
              $name $tk_library]]

       ::safe::setLogCmd ?cmd arg...?
              This command installs a script that will be called  when  inter-
              esting  life  cycle  events  occur for a safe interpreter.  When
              called with no arguments, it  returns  the  currently  installed
              script.   When  called  with  one argument, an empty string(3,n), the
              currently installed script is removed and logging is turned off.
              The  script  will  be  invoked  with  one additional argument, a
              string(3,n) describing the event of interest.  The main purpose is to
              help  in(1,8)  debugging  safe interpreters.  Using this facility you
              can get complete error(8,n) messages while the safe interpreter  gets(3,n)
              only  generic  error(8,n) messages.  This prevents a safe interpreter
              from seeing messages about failures and other events that  might
              contain sensitive information such as real directory names.
              Example  of use: ::safe::setLogCmd puts(3,n) stderr Below is the out-
              put of a sample session in(1,8) which a safe interpreter attempted to
              source  a  file(1,n) not found in(1,8) its virtual(5,8) access(2,5) path.  Note that
              the safe interpreter only received an error(8,n) message saying  that
              the  file(1,n)  was  not  found:  NOTICE for slave interp10 : Created
              NOTICE for slave interp10 : Setting accessPath=(/foo/bar)  stat-
              icsok=1  nestedok=0  deletehook=()  NOTICE  for slave interp10 :
              auto_path in(1,8) interp10 has been set(7,n,1 builtins) to {$p(:0:)} ERROR for  slave
              interp10 : /foo/bar/init.tcl: no such file(1,n) or directory

       The    following    options   are   common   to   ::safe::interpCreate,
       ::safe::interpInit, and ::safe::interpConfigure.  Any option  name  can
       be abbreviated to its minimal non-ambiguous name.  Option names are not
       case sensitive.

       -accessPath directoryList
              This option sets the list of directories  from  which  the  safe
              interpreter  can  source  and load(7,n) files.  If this option is not
              specified, or if(3,n) it is given as the empty list, the safe  inter-
              preter  will  use  the  same directories as its master(5,8) for auto-
              loading.  See the section SECURITY below for more  detail  about
              virtual(5,8) paths, tokens and access(2,5) control.

       -statics boolean
              This option specifies if(3,n) the safe interpreter will be allowed to
              load(7,n) statically linked packages (like load(7,n) {} Tk).  The  default
              value is true : safe interpreters are allowed to load(7,n) statically
              linked packages.

              This option is a convenience shortcut  for  -statics  false  and
              thus  specifies that the safe interpreter will not be allowed to
              load(7,n) statically linked packages.

       -nested boolean
              This option specifies if(3,n) the safe interpreter will be allowed to
              load(7,n)  packages into its own sub-interpreters.  The default value
              is false : safe interpreters are not allowed  to  load(7,n)  packages
              into their own sub-interpreters.

              This  option is a convenience shortcut for -nested true and thus
              specifies the safe interpreter will be allowed to load(7,n)  packages
              into its own sub-interpreters.

       -deleteHook script
              When this option is given an non empty script, it will be evalu-
              ated in(1,8) the master(5,8) with the name of the safe interpreter  as  an
              additional  argument  just  before  actually  deleting  the safe
              interpreter.   Giving  an  empty  value  removes  any  currently
              installed  deletion  hook script for that safe interpreter.  The
              default value ({}) is not to have any deletion call back.

       The following aliases are provided in(1,8) a safe interpreter:

       source fileName
              The requested file(1,n), a Tcl source file(1,n), is sourced into the  safe
              interpreter  if(3,n)  it  is found.  The source alias can only source
              files from directories in(1,8) the virtual(5,8) path for the  safe  inter-
              preter.  The  source  alias requires the safe interpreter to use
              one of the token names in(1,8) its virtual(5,8) path to denote the  direc-
              tory in(1,8) which the file(1,n) to be sourced can be found.  See the sec-
              tion on SECURITY for more discussion of  restrictions  on  valid

       load(7,n) fileName
              The  requested file(1,n), a shared object file(1,n), is dynamically loaded
              into the safe interpreter if(3,n) it is  found.   The  filename  must
              contain  a token name mentioned in(1,8) the virtual(5,8) path for the safe
              interpreter for it to be found successfully.  Additionally,  the
              shared object file(1,n) must contain a safe entry point; see the man-
              ual page for the load(7,n) command for more details.

       file(1,n) ?subCmd args...?
              The file(1,n) alias provides access(2,5) to a safe subset of  the  subcom-
              mands  of the file(1,n) command; it allows only dirname, join(1,n), exten-
              sion, root, tail,  pathname  and  split(1,n)  subcommands.  For  more
              details on what these subcommands do see the manual page for the
              file(1,n) command.

       encoding(3,n) ?subCmd args...?
              The encoding(3,n) alias provides access(2,5) to a safe subset of the  sub-
              commands  of  the encoding(3,n) command;  it disallows setting of the
              system encoding(3,n), but allows all other subcommands including sys-
              tem to check the current encoding.

       exit(3,n,1 builtins)   The  calling  interpreter  is  deleted  and  its  computation is
              stopped, but the Tcl process in(1,8) which this interpreter exists is
              not terminated.

       The  Safe  Base  does  not  attempt to completely prevent annoyance and
       denial of service attacks. These forms of attack prevent  the  applica-
       tion  or  user  from  temporarily  using the computer to perform useful
       work, for example by consuming all available CPU time(1,2,n) or all  available
       screen real estate.  These attacks, while aggravating, are deemed to be
       of lesser importance in(1,8) general than integrity and privacy attacks that
       the Safe Base is to prevent.

       The  commands  available in(1,8) a safe interpreter, in(1,8) addition to the safe
       set(7,n,1 builtins) as defined in(1,8) interp manual page, are mediated aliases for  source,
       load(7,n), exit(3,n,1 builtins), and safe subsets of file(1,n) and encoding(3,n). The safe interpreter
       can also auto-load code and it can request that packages be loaded.

       Because some of these commands access(2,5) the local file(1,n) system, there is a
       potential  for  information  leakage about its directory structure.  To
       prevent this, commands that take file(1,n) names  as  arguments  in(1,8)  a  safe
       interpreter  use  tokens  instead  of  the real directory names.  These
       tokens are translated to the real directory name while  a  request  to,
       e.g.,  source  a file(1,n) is mediated by the master(5,8) interpreter.  This vir-
       tual(5,8) path system is maintained in(1,8) the master(5,8) interpreter for each  safe
       interpreter   created   by   ::safe::interpCreate   or  initialized  by
       ::safe::interpInit and the path maps  tokens  accessible  in(1,8)  the  safe
       interpreter into real path names on the local file(1,n) system thus prevent-
       ing safe interpreters from gaining knowledge about the structure of the
       file(1,n)  system  of  the  host(1,5) on which the interpreter is executing.  The
       only valid file(1,n) names arguments for the source and  load(7,n)  aliases  pro-
       vided  to  the slave are path in(1,8) the form of [file(1,n) join(1,n) token filename]
       (ie, when using the native file(1,n) path formats: token/filename  on  Unix,
       token\filename  on Windows, and token:filename on the Mac), where token
       is representing one of the directories of the accessPath list and file-
       name  is  one  file(1,n)  in(1,8)  that  directory (no sub directories access(2,5) are

       When a token is used in(1,8) a safe interpreter in(1,8) a request  to  source  or
       load(7,n)  a  file(1,n),  the token is checked and translated to a real path name
       and the file(1,n) to be sourced or loaded is located  on  the  file(1,n)  system.
       The  safe  interpreter  never  gains  knowledge of the actual path name
       under which the file(1,n) is stored on the file(1,n) system.

       To further prevent potential information leakage from  sensitive  files
       that  are accidentally included in(1,8) the set(7,n,1 builtins) of files that can be sourced
       by a safe interpreter, the source alias restricts access(2,5) to files meet-
       ing  the  following constraints: the file(1,n) name must fourteen characters
       or shorter, must not contain more than one dot ("."), must end up  with
       the extension .tcl or be called tclIndex.

       Each  element  of the initial access(2,5) path list will be assigned a token
       that will be set(7,n,1 builtins) in(1,8) the slave auto_path and the first element  of  that
       list will be set(7,n,1 builtins) as the tcl_library for that slave.

       If  the  access(2,5)  path  argument  is not given or is the empty list, the
       default behavior is to let the slave access(2,5) the same  packages  as  the
       master(5,8)  has  access(2,5) to (Or to be more precise: only packages written in(1,8)
       Tcl (which by definition can't be dangerous as they run  in(1,8)  the  slave
       interpreter)  and  C extensions that provides a Safe_Init entry point).
       For that purpose, the master(5,8)'s auto_path will be used to construct  the
       slave  access(2,5) path.  In order that the slave successfully loads the Tcl
       library files (which includes the auto-loading  mechanism  itself)  the
       tcl_library  will be added or moved to the first position if(3,n) necessary,
       in(1,8) the slave access(2,5) path, so the slave tcl_library will be the same  as
       the  master(5,8)'s  (its  real  path  will  still  be invisible to the slave
       though).  In order that auto-loading works the same for the  slave  and
       the  master(5,8) in(1,8) this by default case, the first-level sub directories of
       each directory in(1,8) the master(5,8) auto_path  will  also  be  added  (if(3,n)  not
       already  included)  to the slave access(2,5) path.  You can always specify a
       more restrictive path for which sub directories will never be  searched
       by  explicitly specifying your directory list with the -accessPath flag
       instead of relying on this default mechanism.

       When the accessPath is changed after the first creation or  initializa-
       tion  (ie  through  interpConfigure -accessPath list), an auto_reset is
       automatically evaluated in(1,8) the  safe  interpreter  to  synchronize  its
       auto_index with the new token list.

       interp(n), library(n), load(7,n)(n), package(n), source(n), unknown(n)

       alias,  auto-loading,  auto_mkindex,  load(7,n),  master(5,8)  interpreter,  safe
       interpreter, slave interpreter, source

Tcl                                   8.0                          Safe Tcl(n)

References for this manual (incoming links)