GNU TeXmacsUser manualReferenceAboutEnglish
Trusted documents and secure evaluation of scripts

1.Where documents execute code

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.

extern

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

).

Links to scripts

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

).

Observers

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

).

Widgets and exercises

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.

2.Trusted documents

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 field secure of new_buffer_rep, also initialized with is_secure, is not used by any of these checks.

3.The script policy

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:

0

“accept no scripts”.

1

“prompt on scripts” (the default).

2

“accept all scripts”.

The checks then work as follows.

extern

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.

Observers

The observer is registered only if the environment is trusted or if (callback #f #f #f) passes secure?. The policy is not consulted.

Links

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.

Widgets and exercises

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.

4.The checker secure?

secure? (

kernel/texmacs/tm-secure.scm

) is a static analysis of the expression: nothing is evaluated. An expression is accepted by secure-expr? if

The property :secure is set in two ways:

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.

5.Writing secure functions

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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".