![]() |
Trusted documents and secure evaluation of scripts | ![]() |
A document can cause Scheme code to be evaluated in the following ways; each of them is subject to the checks described on this page.
The primitive extern calls a Scheme function with the evaluated arguments during typesetting (edit_env_rep::rewrite,
Typeset/Env/env_exec.cpp
; the experimental style evaluator has the same code in
Style/Evaluate/evaluate_rewrite.cpp
).
A link whose target vertex is a (script …) is executed when the user follows it. This is how action works: its default definition (
Typeset/Env/env_default.cpp
) creates a locus with a link of type action to a script. Following the link ends up in go-to-vertex and execute-script (
link/link-navigate.scm
).
A locus may carry an (observer id callback) attribute, whose callback is called when the locus is modified (build_locus, called when a locus is typeset,
Typeset/Concat/concat_active.cpp
).
Commands attached to the buttons of widgets rendered inside documents are evaluated by gui-on-select (
utils/misc/gui-utils.scm
); scripts attached to the input fields of exercises by edu-exec (
education/edu-edit.scm
); Scheme fragments of automatic documents by build-scheme* (
utils/automate/auto-build.scm
).
Plug-in sessions are not in this list: evaluating a session input is an explicit action of the user, and the plug-in runs with the rights of the user anyway.
A document is trusted (in the code: secure) if its file name lies below one of the directories of the search path $TEXMACS_SECURE_PATH:
bool is_secure (url u) {
return descends (u, expand (url_path ("$TEXMACS_SECURE_PATH")));
} |
(
System/Classes/url.cpp
, exported to Scheme as url-secure?). At startup,
init_texmacs.cpp
appends $TEXMACS_PATH:$TEXMACS_HOME_PATH to whatever the user put in this environment variable. So the documentation and the style files of TeXmacs, and everything below the user's TeXmacs directory, are trusted.
The trust status is recorded in the typesetting environment:
the constructor of edit_env_rep sets secure to is_secure (base_file_name), where the base file name is the master of the buffer (
Typeset/Env/env.cpp
,
Edit/Editor/edit_typeset.cpp
);
while an included file is typeset (include in
concat_macro.cpp
, var-include in
bridge_rewrite.cpp
), secure is set from the name of the included file and restored afterwards; the experimental style evaluator does the same with the environment variable secure (
Style/Evaluate/evaluate_control.cpp
).
The field secure of new_buffer_rep, also initialized with is_secure, is not used by any of these checks.
The user chooses a policy with the preference security (the Security item of the preferences menu, also in the preferences dialog). Its handler notify-security (
texmacs/texmacs/tm-server.scm
) translates it into the global script_status (
System/Misc/sys_utils.cpp
) with set-script-status:
“accept no scripts”.
“prompt on scripts” (the default).
“accept all scripts”.
The checks then work as follows.
If the environment is not trusted and script_status is below 2, the expression (function and evaluated arguments) is passed to secure?; if it is rejected, the result is the error "insecure script". There is no prompt here, even with policy 1, since this happens during typesetting.
The observer is registered only if the environment is trusted or if (callback #f #f #f) passes secure?. The policy is not consulted.
When a link is created inside a locus, the typesetter adds the attribute secure with the trust status of the environment (build_locus). execute-script executes the script at once if this attribute is true, if the script passes secure?, or if the policy is “accept all scripts”; with “prompt on scripts” it asks the user; otherwise it refuses with the message “Unsecure script refused”. There are two code paths: old-execute-script for scripts written as an expression (…), and new-execute-script for a function name with arguments.
The commands are evaluated with secure-eval, that is, only if they pass secure?; the policy is not consulted. build-scheme* skips the check when auto-safe-mode? is set.
secure? (
kernel/texmacs/tm-secure.scm
) is a static analysis of the expression: nothing is evaluated. An expression is accepted by secure-expr? if
it is a constant (number, string, boolean, tree, empty list) or a symbol;
it is a quoted datum, or a quasiquote all of whose unquoted parts are accepted;
its head is one of the special forms and, begin, cond, if, lambda, or, set! or with, and the sub-expressions are accepted (the variables bound by lambda and with are added to a local environment);
its head is a variable of the local environment, or a symbol whose property :secure is set, and all arguments are accepted;
its head is itself a compound expression, and all elements are accepted.
The property :secure is set in two ways:
by the option (:secure #t) of tm-define (
kernel/texmacs/tm-define.scm
), which many editing routines use, among which most entry points of the encryption code;
by define-secure-symbols for a list of primitive functions on booleans, lists, strings and numbers, display, texmacs-version and refresh-now.
If the first test fails, secure? forces the lazy loading of the plug-ins (lazy-plugin-force) and tries again, since some secure functions are only defined once their plug-in has been loaded. secure-eval evaluates an expression if it passes secure?, and returns #f otherwise.
Declaring a function :secure means that any document may call it with any arguments it can compute. A function should therefore only be declared secure if it cannot read or write files, run programs, open network connections, change preferences or otherwise act beyond the current document, whatever its arguments. Opening a dialog is not harmless either: an untrusted document should not be able to show a passphrase prompt of its own choosing. See threat model and pitfalls for the known limits of the checker.