interp.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:31k
- '"
- '" Copyright (c) 1995-1996 Sun Microsystems, Inc.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: interp.n,v 1.9.2.2 2004/10/27 14:23:56 dkf Exp $
- '"
- .so man.macros
- .TH interp n 7.6 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- interp - Create and manipulate Tcl interpreters
- .SH SYNOPSIS
- fBinterp fIoption fR?fIarg arg ...fR?
- .BE
- .SH DESCRIPTION
- .PP
- This command makes it possible to create one or more new Tcl
- interpreters that co-exist with the creating interpreter in the
- same application. The creating interpreter is called the fImasterfR
- and the new interpreter is called a fIslavefR.
- A master can create any number of slaves, and each slave can
- itself create additional slaves for which it is master, resulting
- in a hierarchy of interpreters.
- .PP
- Each interpreter is independent from the others: it has its own name
- space for commands, procedures, and global variables.
- A master interpreter may create connections between its slaves and
- itself using a mechanism called an fIaliasfR. An fIaliasfR is
- a command in a slave interpreter which, when invoked, causes a
- command to be invoked in its master interpreter or in another slave
- interpreter. The only other connections between interpreters are
- through environment variables (the fBenvfR variable), which are
- normally shared among all interpreters in the application. Note that the
- name space for files (such as the names returned by the fBopenfR command)
- is no longer shared between interpreters. Explicit commands are provided to
- share files and to transfer references to open files from one interpreter
- to another.
- .PP
- The fBinterpfR command also provides support for fIsafefR
- interpreters. A safe interpreter is a slave whose functions have
- been greatly restricted, so that it is safe to execute untrusted
- scripts without fear of them damaging other interpreters or the
- application's environment. For example, all IO channel creation
- commands and subprocess creation commands are made inaccessible to safe
- interpreters.
- .VS
- See fBSAFE INTERPRETERSfR below for more information on
- what features are present in a safe interpreter.
- The dangerous functionality is not removed from the safe interpreter;
- instead, it is fIhiddenfR, so that only trusted interpreters can obtain
- access to it. For a detailed explanation of hidden commands, see
- fBHIDDEN COMMANDSfR, below.
- The alias mechanism can be used for protected communication (analogous to a
- kernel call) between a slave interpreter and its master.
- See fBALIAS INVOCATIONfR, below, for more details
- on how the alias mechanism works.
- .VE
- .PP
- A qualified interpreter name is a proper Tcl lists containing a subset of its
- ancestors in the interpreter hierarchy, terminated by the string naming the
- interpreter in its immediate master. Interpreter names are relative to the
- interpreter in which they are used. For example, if fBafR is a slave of
- the current interpreter and it has a slave fBa1fR, which in turn has a
- slave fBa11fR, the qualified name of fBa11fR in fBafR is the list
- fBa1 a11fR.
- .PP
- The fBinterpfR command, described below, accepts qualified interpreter
- names as arguments; the interpreter in which the command is being evaluated
- can always be referred to as fB{}fR (the empty list or string). Note that
- it is impossible to refer to a master (ancestor) interpreter by name in a
- slave interpreter except through aliases. Also, there is no global name by
- which one can refer to the first interpreter created in an application.
- Both restrictions are motivated by safety concerns.
- .SH "THE INTERP COMMAND"
- .PP
- The fBinterpfR command is used to create, delete, and manipulate
- slave interpreters, and to share or transfer
- channels between interpreters. It can have any of several forms, depending
- on the fIoptionfR argument:
- .TP
- fBinterpfR fBaliasfR fIsrcPathfR fIsrcTokenfR
- Returns a Tcl list whose elements are the fItargetCmdfR and
- fIargfRs associated with the alias represented by fIsrcTokenfR
- (this is the value returned when the alias was
- created; it is possible that the name of the source command in the
- slave is different from fIsrcTokenfR).
- .TP
- fBinterpfR fBaliasfR fIsrcPathfR fIsrcTokenfR fB{}fR
- Deletes the alias for fIsrcTokenfR in the slave interpreter identified by
- fIsrcPathfR.
- fIsrcTokenfR refers to the value returned when the alias
- was created; if the source command has been renamed, the renamed
- command will be deleted.
- .TP
- fBinterpfR fBaliasfR fIsrcPathfR fIsrcCmdfR fItargetPathfR fItargetCmd fR?fIarg arg ...fR?
- This command creates an alias between one slave and another (see the
- fBaliasfR slave command below for creating aliases between a slave
- and its master). In this command, either of the slave interpreters
- may be anywhere in the hierarchy of interpreters under the interpreter
- invoking the command.
- fISrcPathfR and fIsrcCmdfR identify the source of the alias.
- fISrcPathfR is a Tcl list whose elements select a particular
- interpreter. For example, ``fBa bfR'' identifies an interpreter
- fBbfR, which is a slave of interpreter fBafR, which is a slave
- of the invoking interpreter. An empty list specifies the interpreter
- invoking the command. fIsrcCmdfR gives the name of a new
- command, which will be created in the source interpreter.
- fITargetPathfR and fItargetCmdfR specify a target interpreter
- and command, and the fIargfR arguments, if any, specify additional
- arguments to fItargetCmdfR which are prepended to any arguments specified
- in the invocation of fIsrcCmdfR.
- fITargetCmdfR may be undefined at the time of this call, or it may
- already exist; it is not created by this command.
- The alias arranges for the given target command to be invoked
- in the target interpreter whenever the given source command is
- invoked in the source interpreter. See fBALIAS INVOCATIONfR below for
- more details.
- The command returns a token that uniquely identifies the command created
- fIsrcCmdfR, even if the command is renamed afterwards. The token may but
- does not have to be equal to fIsrcCmdfR.
- .TP
- fBinterpfR fBaliases fR?fIpathfR?
- This command returns a Tcl list of the tokens of all the source commands for
- aliases defined in the interpreter identified by fIpathfR. The tokens
- correspond to the values returned when
- the aliases were created (which may not be the same
- as the current names of the commands).
- .TP
- fBinterpfR fBcreate fR?fB-safefR? ?fB-|-fR? ?fIpathfR?
- Creates a slave interpreter identified by fIpathfR and a new command,
- called a fIslave commandfR. The name of the slave command is the last
- component of fIpathfR. The new slave interpreter and the slave command
- are created in the interpreter identified by the path obtained by removing
- the last component from fIpathfR. For example, if fIpath is fBa b
- cfR then a new slave interpreter and slave command named fBcfR are
- created in the interpreter identified by the path fBa bfR.
- The slave command may be used to manipulate the new interpreter as
- described below. If fIpathfR is omitted, Tcl creates a unique name of the
- form fBinterpfIxfR, where fIxfR is an integer, and uses it for the
- interpreter and the slave command. If the fB-safefR switch is specified
- (or if the master interpreter is a safe interpreter), the new slave
- interpreter will be created as a safe interpreter with limited
- functionality; otherwise the slave will include the full set of Tcl
- built-in commands and variables. The fB-|-fR switch can be used to
- mark the end of switches; it may be needed if fIpathfR is an unusual
- value such as fB-safefR. The result of the command is the name of the
- new interpreter. The name of a slave interpreter must be unique among all
- the slaves for its master; an error occurs if a slave interpreter by the
- given name already exists in this master.
- The initial recursion limit of the slave interpreter is set to the
- current recursion limit of its parent interpreter.
- .TP
- fBinterpfR fBdelete fR?fIpath ...?fR
- Deletes zero or more interpreters given by the optional fIpathfR
- arguments, and for each interpreter, it also deletes its slaves. The
- command also deletes the slave command for each interpreter deleted.
- For each fIpathfR argument, if no interpreter by that name
- exists, the command raises an error.
- .TP
- fBinterpfR fBevalfR fIpath arg fR?fIarg ...fR?
- This command concatenates all of the fIargfR arguments in the same
- fashion as the fBconcatfR command, then evaluates the resulting string as
- a Tcl script in the slave interpreter identified by fIpathfR. The result
- of this evaluation (including error information such as the fBerrorInfofR
- and fBerrorCodefR variables, if an error occurs) is returned to the
- invoking interpreter.
- Note that the script will be executed in the current context stack frame of the
- fIpathfR interpreter; this is so that the implementations (in a master
- interpreter) of aliases in a slave interpreter can execute scripts in
- the slave that find out information about the slave's current state
- and stack frame.
- .TP
- fBinterp exists fIpathfR
- Returns fB1fR if a slave interpreter by the specified fIpathfR
- exists in this master, fB0fR otherwise. If fIpathfR is omitted, the
- invoking interpreter is used.
- .VS "" BR
- .TP
- fBinterp expose fIpathfR fIhiddenNamefR ?fIexposedCmdNamefR?
- Makes the hidden command fIhiddenNamefR exposed, eventually bringing
- it back under a new fIexposedCmdNamefR name (this name is currently
- accepted only if it is a valid global name space name without any ::),
- in the interpreter
- denoted by fIpathfR.
- If an exposed command with the targeted name already exists, this command
- fails.
- Hidden commands are explained in more detail in fBHIDDEN COMMANDSfR, below.
- .TP
- fBinterpfR fBhidefR fIpathfR fIexposedCmdNamefR ?fIhiddenCmdNamefR?
- Makes the exposed command fIexposedCmdNamefR hidden, renaming
- it to the hidden command fIhiddenCmdNamefR, or keeping the same name if
- fIhiddenCmdNamefR is not given, in the interpreter denoted
- by fIpathfR.
- If a hidden command with the targeted name already exists, this command
- fails.
- Currently both fIexposedCmdNamefR and fIhiddenCmdNamefR can
- not contain namespace qualifiers, or an error is raised.
- Commands to be hidden by fBinterp hidefR are looked up in the global
- namespace even if the current namespace is not the global one. This
- prevents slaves from fooling a master interpreter into hiding the wrong
- command, by making the current namespace be different from the global one.
- Hidden commands are explained in more detail in fBHIDDEN COMMANDSfR, below.
- .TP
- fBinterpfR fBhiddenfR fIpathfR
- Returns a list of the names of all hidden commands in the interpreter
- identified by fIpathfR.
- .TP
- fBinterpfR fBinvokehiddenfR fIpathfR ?fB-globalfR? fIhiddenCmdNamefR ?fIarg ...fR?
- Invokes the hidden command fIhiddenCmdNamefR with the arguments supplied
- in the interpreter denoted by fIpathfR. No substitutions or evaluation
- are applied to the arguments.
- If the fB-globalfR flag is present, the hidden command is invoked at the
- global level in the target interpreter; otherwise it is invoked at the
- current call frame and can access local variables in that and outer call
- frames.
- Hidden commands are explained in more detail in fBHIDDEN COMMANDSfR, below.
- .VE
- .TP
- fBinterp issafefR ?fIpathfR?
- Returns fB1fR if the interpreter identified by the specified fIpathfR
- is safe, fB0fR otherwise.
- .VS "" BR
- .TP
- fBinterp marktrustedfR fIpathfR
- Marks the interpreter identified by fIpathfR as trusted. Does
- not expose the hidden commands. This command can only be invoked from a
- trusted interpreter.
- The command has no effect if the interpreter identified by fIpathfR is
- already trusted.
- .VE
- .TP
- fBinterpfR fBrecursionlimitfR fIpathfR ?fInewlimitfR?
- Returns the maximum allowable nesting depth for the interpreter
- specified by fIpathfR. If fInewlimitfR is specified,
- the interpreter recursion limit will be set so that nesting
- of more than fInewlimitfR calls to fBTcl_Eval()fR
- and related procedures in that interpreter will return an error.
- The fInewlimitfR value is also returned.
- The fInewlimitfR value must be a positive integer between 1 and the
- maximum value of a non-long integer on the platform.
- .sp
- The command sets the maximum size of the Tcl call stack only. It cannot
- by itself prevent stack overflows on the C stack being used by the
- application. If your machine has a limit on the size of the C stack, you
- may get stack overflows before reaching the limit set by the command. If
- this happens, see if there is a mechanism in your system for increasing
- the maximum size of the C stack.
- .TP
- fBinterpfR fBsharefR fIsrcPath channelId destPathfR
- Causes the IO channel identified by fIchannelIdfR to become shared
- between the interpreter identified by fIsrcPathfR and the interpreter
- identified by fIdestPathfR. Both interpreters have the same permissions
- on the IO channel.
- Both interpreters must close it to close the underlying IO channel; IO
- channels accessible in an interpreter are automatically closed when an
- interpreter is destroyed.
- .TP
- fBinterpfR fBslavesfR ?fIpathfR?
- Returns a Tcl list of the names of all the slave interpreters associated
- with the interpreter identified by fIpathfR. If fIpathfR is omitted,
- the invoking interpreter is used.
- .TP
- fBinterpfR fBtargetfR fIpath aliasfR
- Returns a Tcl list describing the target interpreter for an alias. The
- alias is specified with an interpreter path and source command name, just
- as in fBinterp aliasfR above. The name of the target interpreter is
- returned as an interpreter path, relative to the invoking interpreter.
- If the target interpreter for the alias is the invoking interpreter then an
- empty list is returned. If the target interpreter for the alias is not the
- invoking interpreter or one of its descendants then an error is generated.
- The target command does not have to be defined at the time of this invocation.
- .TP
- fBinterpfR fBtransferfR fIsrcPath channelId destPathfR
- Causes the IO channel identified by fIchannelIdfR to become available in
- the interpreter identified by fIdestPathfR and unavailable in the
- interpreter identified by fIsrcPathfR.
- .SH "SLAVE COMMAND"
- .PP
- For each slave interpreter created with the fBinterpfR command, a
- new Tcl command is created in the master interpreter with the same
- name as the new interpreter. This command may be used to invoke
- various operations on the interpreter. It has the following
- general form:
- .CS
- fIslave command fR?fIarg arg ...fR?
- .CE
- fISlavefR is the name of the interpreter, and fIcommandfR
- and the fIargfRs determine the exact behavior of the command.
- The valid forms of this command are:
- .TP
- fIslave fBaliasesfR
- Returns a Tcl list whose elements are the tokens of all the
- aliases in fIslavefR. The tokens correspond to the values returned when
- the aliases were created (which may not be the same
- as the current names of the commands).
- .TP
- fIslave fBalias fIsrcTokenfR
- Returns a Tcl list whose elements are the fItargetCmdfR and
- fIargfRs associated with the alias represented by fIsrcTokenfR
- (this is the value returned when the alias was
- created; it is possible that the actual source command in the
- slave is different from fIsrcTokenfR).
- .TP
- fIslave fBalias fIsrcToken fB{}fR
- Deletes the alias for fIsrcTokenfR in the slave interpreter.
- fIsrcTokenfR refers to the value returned when the alias
- was created; if the source command has been renamed, the renamed
- command will be deleted.
- .TP
- fIslave fBalias fIsrcCmd targetCmd fR?fIarg ..fR?
- Creates an alias such that whenever fIsrcCmdfR is invoked
- in fIslavefR, fItargetCmdfR is invoked in the master.
- The fIargfR arguments will be passed to fItargetCmdfR as additional
- arguments, prepended before any arguments passed in the invocation of
- fIsrcCmdfR.
- See fBALIAS INVOCATIONfR below for details.
- The command returns a token that uniquely identifies the command created
- fIsrcCmdfR, even if the command is renamed afterwards. The token may but
- does not have to be equal to fIsrcCmdfR.
- .TP
- fIslave fBeval fIarg fR?fIarg ..fR?
- This command concatenates all of the fIargfR arguments in
- the same fashion as the fBconcatfR command, then evaluates
- the resulting string as a Tcl script in fIslavefR.
- The result of this evaluation (including error information
- such as the fBerrorInfofR and fBerrorCodefR variables, if an
- error occurs) is returned to the invoking interpreter.
- Note that the script will be executed in the current context stack frame
- of fIslavefR; this is so that the implementations (in a master
- interpreter) of aliases in a slave interpreter can execute scripts in
- the slave that find out information about the slave's current state
- and stack frame.
- .VS "" BR
- .TP
- fIslave fBexpose fIhiddenName fR?fIexposedCmdNamefR?
- This command exposes the hidden command fIhiddenNamefR, eventually bringing
- it back under a new fIexposedCmdNamefR name (this name is currently
- accepted only if it is a valid global name space name without any ::),
- in fIslavefR.
- If an exposed command with the targeted name already exists, this command
- fails.
- For more details on hidden commands, see fBHIDDEN COMMANDSfR, below.
- .TP
- fIslave fBhide fIexposedCmdNamefR ?fIhiddenCmdNamefR?
- This command hides the exposed command fIexposedCmdNamefR, renaming it to
- the hidden command fIhiddenCmdNamefR, or keeping the same name if the
- argument is not given, in the fIslavefR interpreter.
- If a hidden command with the targeted name already exists, this command
- fails.
- Currently both fIexposedCmdNamefR and fIhiddenCmdNamefR can
- not contain namespace qualifiers, or an error is raised.
- Commands to be hidden are looked up in the global
- namespace even if the current namespace is not the global one. This
- prevents slaves from fooling a master interpreter into hiding the wrong
- command, by making the current namespace be different from the global one.
- For more details on hidden commands, see fBHIDDEN COMMANDSfR, below.
- .TP
- fIslave fBhiddenfR
- Returns a list of the names of all hidden commands in fIslavefR.
- .TP
- fIslave fBinvokehiddenfR ?fB-globalfR fIhiddenName fR?fIarg ..fR?
- This command invokes the hidden command fIhiddenNamefR with the
- supplied arguments, in fIslavefR. No substitutions or evaluations are
- applied to the arguments.
- If the fB-globalfR flag is given, the command is invoked at the global
- level in the slave; otherwise it is invoked at the current call frame and
- can access local variables in that or outer call frames.
- For more details on hidden commands,
- see fBHIDDEN COMMANDSfR, below.
- .VE
- .TP
- fIslave fBissafefR
- Returns fB1fR if the slave interpreter is safe, fB0fR otherwise.
- .VS "" BR
- .TP
- fIslave fBmarktrustedfR
- Marks the slave interpreter as trusted. Can only be invoked by a
- trusted interpreter. This command does not expose any hidden
- commands in the slave interpreter. The command has no effect if the slave
- is already trusted.
- .VE
- .TP
- fIslavefR fBrecursionlimitfR ?fInewlimitfR?
- Returns the maximum allowable nesting depth for the fIslavefR interpreter.
- If fInewlimitfR is specified, the recursion limit in fIslavefR will be
- set so that nesting of more than fInewlimitfR calls to fBTcl_Eval()fR
- and related procedures in fIslavefR will return an error.
- The fInewlimitfR value is also returned.
- The fInewlimitfR value must be a positive integer between 1 and the
- maximum value of a non-long integer on the platform.
- .sp
- The command sets the maximum size of the Tcl call stack only. It cannot
- by itself prevent stack overflows on the C stack being used by the
- application. If your machine has a limit on the size of the C stack, you
- may get stack overflows before reaching the limit set by the command. If
- this happens, see if there is a mechanism in your system for increasing
- the maximum size of the C stack.
- .SH "SAFE INTERPRETERS"
- .PP
- A safe interpreter is one with restricted functionality, so that
- is safe to execute an arbitrary script from your worst enemy without
- fear of that script damaging the enclosing application or the rest
- of your computing environment. In order to make an interpreter
- safe, certain commands and variables are removed from the interpreter.
- For example, commands to create files on disk are removed, and the
- fBexecfR command is removed, since it could be used to cause damage
- through subprocesses.
- Limited access to these facilities can be provided, by creating
- aliases to the master interpreter which check their arguments carefully
- and provide restricted access to a safe subset of facilities.
- For example, file creation might be allowed in a particular subdirectory
- and subprocess invocation might be allowed for a carefully selected and
- fixed set of programs.
- .PP
- A safe interpreter is created by specifying the fB-safefR switch
- to the fBinterp createfR command. Furthermore, any slave created
- by a safe interpreter will also be safe.
- .PP
- A safe interpreter is created with exactly the following set of
- built-in commands:
- .DS
- .ta 1.2i 2.4i 3.6i
- fBafter append array binary
- break case catch clock
- close concat continue eof
- error eval expr fblocked
- fcopy fileevent flush for
- foreach format gets global
- if incr info interp
- join lappend lindex linsert
- list llength lrange lreplace
- lsearch lsort namespace package
- pid proc puts read
- regexp regsub rename return
- scan seek set split
- string subst switch tell
- time trace unset update
- uplevel upvar variable vwait
- whilefR
- .DE
- .VS "" BR
- The following commands are hidden by fBinterp createfR when it
- creates a safe interpreter:
- .DS
- .ta 1.2i 2.4i 3.6i
- fBcd encoding exec exit
- fconfigure file glob load
- open pwd socket sourcefR
- .DE
- These commands can be recreated later as Tcl procedures or aliases, or
- re-exposed by fBinterp exposefR.
- .PP
- The following commands from Tcl's library of support procedures are
- not present in a safe interpreter:
- .DS
- .ta 1.6i 3.2i
- fBauto_exec_ok auto_import auto_load
- auto_load_index auto_qualify unknownfR
- .DE
- Note in particular that safe interpreters have no default fBunknownfR
- command, so Tcl's default autoloading facilities are not available.
- Autoload access to Tcl's commands that are normally autoloaded:
- .DS
- .ta 2.1i
- fB
- auto_mkindex auto_mkindex_old
- auto_reset history
- parray pkg_mkIndex
- ::pkg::create ::safe::interpAddToAccessPath
- ::safe::interpCreate ::safe::interpConfigure
- ::safe::interpDelete ::safe::interpFindInAccessPath
- ::safe::interpInit ::safe::setLogCmd
- tcl_endOfWord tcl_findLibrary
- tcl_startOfNextWord tcl_startOfPreviousWord
- tcl_wordBreakAfter tcl_wordBreakBeforefR
- .DE
- can only be provided by explicit definition of an fBunknownfR command
- in the safe interpreter. This will involve exposing the fBsourcefR
- command. This is most easily accomplished by creating the safe interpreter
- with Tcl's fBSafe-TclfR mechanism. fBSafe-TclfR provides safe
- versions of fBsourcefR, fBloadfR, and other Tcl commands needed
- to support autoloading of commands and the loading of packages.
- .VE
- .PP
- In addition, the fBenvfR variable is not present in a safe interpreter,
- so it cannot share environment variables with other interpreters. The
- fBenvfR variable poses a security risk, because users can store
- sensitive information in an environment variable. For example, the PGP
- manual recommends storing the PGP private key protection password in
- the environment variable fIPGPPASSfR. Making this variable available
- to untrusted code executing in a safe interpreter would incur a
- security risk.
- .PP
- If extensions are loaded into a safe interpreter, they may also restrict
- their own functionality to eliminate unsafe commands. For a discussion of
- management of extensions for safety see the manual entries for
- fBSafe-TclfR and the fBloadfR Tcl command.
- .PP
- A safe interpreter may not alter the recursion limit of any interpreter,
- including itself.
- .SH "ALIAS INVOCATION"
- .PP
- The alias mechanism has been carefully designed so that it can
- be used safely when an untrusted script is executing
- in a safe slave and the target of the alias is a trusted
- master. The most important thing in guaranteeing safety is to
- ensure that information passed from the slave to the master is
- never evaluated or substituted in the master; if this were to
- occur, it would enable an evil script in the slave to invoke
- arbitrary functions in the master, which would compromise security.
- .PP
- When the source for an alias is invoked in the slave interpreter, the
- usual Tcl substitutions are performed when parsing that command.
- These substitutions are carried out in the source interpreter just
- as they would be for any other command invoked in that interpreter.
- The command procedure for the source command takes its arguments
- and merges them with the fItargetCmdfR and fIargfRs for the
- alias to create a new array of arguments. If the words
- of fIsrcCmdfR were ``fIsrcCmd arg1 arg2 ... argNfR'',
- the new set of words will be
- ``fItargetCmd arg arg ... arg arg1 arg2 ... argNfR'',
- where fItargetCmdfR and fIargfRs are the values supplied when the
- alias was created. fITargetCmdfR is then used to locate a command
- procedure in the target interpreter, and that command procedure
- is invoked with the new set of arguments. An error occurs if
- there is no command named fItargetCmdfR in the target interpreter.
- No additional substitutions are performed on the words: the
- target command procedure is invoked directly, without
- going through the normal Tcl evaluation mechanism.
- Substitutions are thus performed on each word exactly once:
- fItargetCmdfR and fIargsfR were substituted when parsing the command
- that created the alias, and fIarg1 - argNfR are substituted when
- the alias's source command is parsed in the source interpreter.
- .PP
- When writing the fItargetCmdfRs for aliases in safe interpreters,
- it is very important that the arguments to that command never be
- evaluated or substituted, since this would provide an escape
- mechanism whereby the slave interpreter could execute arbitrary
- code in the master. This in turn would compromise the security
- of the system.
- .VS
- .SH "HIDDEN COMMANDS"
- .PP
- Safe interpreters greatly restrict the functionality available to Tcl
- programs executing within them.
- Allowing the untrusted Tcl program to have direct access to this
- functionality is unsafe, because it can be used for a variety of
- attacks on the environment.
- However, there are times when there is a legitimate need to use the
- dangerous functionality in the context of the safe interpreter. For
- example, sometimes a program must be fBsourcefRd into the interpreter.
- Another example is Tk, where windows are bound to the hierarchy of windows
- for a specific interpreter; some potentially dangerous functions, e.g.
- window management, must be performed on these windows within the
- interpreter context.
- .PP
- The fBinterpfR command provides a solution to this problem in the form of
- fIhidden commandsfR. Instead of removing the dangerous commands entirely
- from a safe interpreter, these commands are hidden so they become
- unavailable to Tcl scripts executing in the interpreter. However, such
- hidden commands can be invoked by any trusted ancestor of the safe
- interpreter, in the context of the safe interpreter, using fBinterp
- invokefR. Hidden commands and exposed commands reside in separate name
- spaces. It is possible to define a hidden command and an exposed command by
- the same name within one interpreter.
- .PP
- Hidden commands in a slave interpreter can be invoked in the body of
- procedures called in the master during alias invocation. For example, an
- alias for fBsourcefR could be created in a slave interpreter. When it is
- invoked in the slave interpreter, a procedure is called in the master
- interpreter to check that the operation is allowable (e.g. it asks to
- source a file that the slave interpreter is allowed to access). The
- procedure then it invokes the hidden fBsourcefR command in the slave
- interpreter to actually source in the contents of the file. Note that two
- commands named fBsourcefR exist in the slave interpreter: the alias, and
- the hidden command.
- .PP
- Because a master interpreter may invoke a hidden command as part of
- handling an alias invocation, great care must be taken to avoid evaluating
- any arguments passed in through the alias invocation.
- Otherwise, malicious slave interpreters could cause a trusted master
- interpreter to execute dangerous commands on their behalf. See the section
- on fBALIAS INVOCATIONfR for a more complete discussion of this topic.
- To help avoid this problem, no substitutions or evaluations are
- applied to arguments of fBinterp invokehiddenfR.
- .PP
- Safe interpreters are not allowed to invoke hidden commands in themselves
- or in their descendants. This prevents safe slaves from gaining access to
- hidden functionality in themselves or their descendants.
- .PP
- The set of hidden commands in an interpreter can be manipulated by a trusted
- interpreter using fBinterp exposefR and fBinterp hidefR. The fBinterp
- exposefR command moves a hidden command to the
- set of exposed commands in the interpreter identified by fIpathfR,
- potentially renaming the command in the process. If an exposed command by
- the targeted name already exists, the operation fails. Similarly,
- fBinterp hidefR moves an exposed command to the set of hidden commands in
- that interpreter. Safe interpreters are not allowed to move commands
- between the set of hidden and exposed commands, in either themselves or
- their descendants.
- .PP
- Currently, the names of hidden commands cannot contain namespace
- qualifiers, and you must first rename a command in a namespace to the
- global namespace before you can hide it.
- Commands to be hidden by fBinterp hidefR are looked up in the global
- namespace even if the current namespace is not the global one. This
- prevents slaves from fooling a master interpreter into hiding the wrong
- command, by making the current namespace be different from the global one.
- .VE
- .SH CREDITS
- .PP
- This mechanism is based on the Safe-Tcl prototype implemented
- by Nathaniel Borenstein and Marshall Rose.
- .SH EXAMPLES
- Creating and using an alias for a command in the current interpreter:
- .CS
- fBinterp aliasfR {} getIndex {} lsearch {alpha beta gamma delta}
- set idx [getIndex delta]
- .CE
- .PP
- Executing an arbitrary command in a safe interpreter where every
- invokation of fBlappendfR is logged:
- .CS
- set i [fBinterp createfR -safe]
- fBinterp hidefR $i lappend
- fBinterp aliasfR $i lappend {} loggedLappend $i
- proc loggedLappend {i args} {
- puts "logged invokation of lappend $args"
- # Be extremely careful about command construction
- eval [linsert $args 0 \
- fBinterp invokehiddenfR $i lappend]
- }
- fBinterp evalfR $i $someUntrustedScript
- .CE
- .SH "SEE ALSO"
- load(n), safe(n), Tcl_CreateSlave(3)
- .SH KEYWORDS
- alias, master interpreter, safe interpreter, slave interpreter