Debian Codegraph @

texmf-dist/doc/luametatex/base/luametatex-security.tex

raw
1 % language=us runpath=texruns:manuals/luametatex
2
3 \environment luametatex-style
4
5 \startdocument[title=security]
6
7 \startsection[title=Introduction]
8
9 This is a relatively short chapter where we pay some attention to possible side
10 effects that can come from running a program like \TEX\ and especially
11 \LUAMETATEX. A \TEX\ program is normally pretty robust but the fact that we use
12 an opened up engine and also need to keep performance in mind, also means that
13 there are ways to have a less nice experience. However, given the nature of the
14 program we also expect users to know what they are doing. If you run \TEX\ as
15 service there are ways to keep problems local (using a virtual machine) or using
16 constrained input like \XML. In \CONTEXT\ you can also limit some functionality
17 by using sandbox features but those are seldom used.
18
19 The engine starts out with everything available but you can selectively disable
20 features and thereby add some protection. When you use \CONTEXT\ this means that
21 there might be features that you cannot use or that are restricted in use. When
22 an open \TEX\ engine like \LUATEX\ or \LUAMETATEX\ is used we assume that you (or
23 those who developed the macros that you use) know what they are doing. As a side
24 note: \LUAMETATEX\ is likely a bit more hardened than \LUATEX.
25
26 \stopsection
27
28 \startsection[title=Primitives]
29
30 Internally \TEX\ has grouped the user interface in commands. Most commands have
31 variants. So, for instance there is are \prm {hrule} and \prm {vrule} than are
32 triggering \type {hrule_cmd} and \type {vrule_cmd} with a sub command, here in
33 both cases zero (or \typ {normal_rule_code}. The \prm {nohrule} and \type
34 {novrule} primitives trigger these rule commands with sub command \typ
35 {empty_rule_code}.
36
37 Primitive initialization boils down to relating control sequences (normally
38 starting with a backslash) to a combination of such commands and subcommands and
39 there are hundreds of such combinations.
40
41 You can decide to selectively initialize these primitives but also to just
42 redefine the control sequence. so:
43
44 \starttyping[option=TEX]
45 \let\left\MyLeftCommand
46 \stoptyping
47
48 will replace the primitive by whatever \tex {MyLeftCommand} does. Unless you save
49 the original meaning the primitive is lost; well not entirely as we will see
50 later. Overload protection can be set up to prevent primitives being redefined.
51
52 We are aware of the fact that overload protection and the related prefixes that
53 drive the properties (like \prm {permanent}, \prm {frozen}, \prm {immutable},
54 \prm {tolerant} and more) are very much inspired by how \CONTEXT\ developers see
55 things so it is unlikely that other macro packages will use these features. But
56 that is probably true for most of the features that make \LUAMETATEX\ differ from
57 \LUATEX. The same can be said for the embedded \METAPOST\ library which is also
58 an enhanced follow up and \LUA\ helpers of any kind.
59
60
61 \stopsection
62
63 \startsection[title=Macros]
64
65 Macros are user defined commands (when using \prm {def} and friends) or aliases
66 (when using \prm {let} and co).
67
68 \starttyping[option=TEX]
69 \def\foo{fooled}
70 \stoptyping
71
72 When \prm {overloadmode} is active the next one:
73
74 \starttyping[option=TEX]
75 \permanent\def\foo{fooled}
76 \stoptyping
77
78 will make sure that a user doesn't redefine this macro. One can argue that the
79 strength of \TEX\ is that you have complete control which is what you have unless
80 a macro package decides that being a bit more strict makes sense. Primitives are
81 permanent by default.
82
83 \stopsection
84
85 \startsection[title=Tokens]
86
87 Tokens are the storage concept of commands, and keep in mind that even a letter
88 is just that, for instance a \type {letter_cmd} with a sub code being a \UNICODE
89 ? In \LUA\ you can create tokens so that is actually a loophole: you can redefine
90 a primitive but that doesn't remove the command and its subcommand: these stay
91 around.
92
93 The \typ {tex.inhibitprimitive} function can block such a combination either
94 via its primitive name or by the two codes.
95
96 \starttyping
97 \ctxlua{tex.inhibitprimitive("primitive")}
98 \stoptyping
99
100 That way there is no change that this old school primitive will kick in. This
101 might be good to know for a macro package because then it doesn't need to take
102 that possibility into account.
103
104 \stopsection
105
106 \startsection[title=Nodes]
107
108 Robust node usage is bit more complex. Where tokens are collected, consumed,
109 deallocated or kept with a reference count and thereby managed, nodes are the
110 result of building and manipulating (content) lists and at the \LUA\ end one can
111 create bad links (like loops) or forget to make copies when flushing nodes
112 multiple times.
113
114 In \LUAMETATEX\ at least there is protection against assigning the wrong fields,
115 accessing freed nodes etc. But it will never be completely safe. So, what can
116 happen is that nodes are already freed (often a warning), list linking is bad
117 (endless loops or crashes), internal mechanisms get confused (quit with error or
118 just crash).
119
120 So here you mainly have to trust the writers of the macro package and they will
121 not on purpose trigger problems. The good news is that it being an relatively
122 long standing community, the \TEX\ world has some leverage and protection against
123 malice built in.
124
125 \stopsection
126
127 \startsection[title=\LUA]
128
129 Here is the real possibility to mess with your run. There is little we can do
130 about it. The good news is that \LUA\ is relatively well designed wrt memory
131 management so it's your own doing if something goes bad. It is good to keep in
132 mind that even well intended code can behave bad and create crashes. Normally a
133 crash is harmless and just a crash.
134
135 \stopsection
136
137 \startsection[title=Files]
138
139 Whenever a program can mess with files there is the danger of corruption. This is
140 however also true for the other \TEX\ engines. It therefore makes no sense to
141 give examples here as it might give away ideas. Again there is the possibility to
142 use sandboxing in \CONTEXT\ but you are anyways dependent on what your macro
143 package provides.
144
145 In this perspective we should also mention that you can produce a bad \PDF\ file
146 (assuming that this is your output format) and that might only show up after
147 years.
148
149 \stopsection
150
151 \startsection[title=Callbacks]
152
153 These extend the engine and some are actually mandate, like those reading from files
154 and loading fonts. Most intrusive are those that intercept the node lists
155 in various states and it's important that things happen in the right order. Give back
156 the wrong result and you might get into problems. Normally the engine intercept
157 the unexpected but who knows.
158
159 The macro package is responsible for managing this. It can put restrictions on
160 using callbacks, prohibit changes to them etc. For instance in \CONTEXT\ the
161 current callback properties are:
162
163 \start \small \startluacode
164 moduledata.context.callbacks()
165 -- callbacks.report()
166 \stopluacode \stop
167
168 There are probably ways around all this protection because after all the files
169 that make a format are on the system. But the fact that one then to deliberately
170 has to do that also indicates (or at least makes one aware) that one is \quote
171 {on its own}. The \TEX\ ecosystem and the programs and macros used are open
172 source so anything can be done with it, good and bad. So all boils down to trust.
173
174 \stopsection
175
176 \startsection[title=Libraries]
177
178 There is plenty built in so \LUAMETATEX\ can do without libraries. However, there
179 are some so called optional library interfaces available. These are dynamic in the
180 sense that they assume a stable API and bind functions when the library is
181 loaded. Again controlling this is a macro package specific feature. Often one can
182 just as well call a program (that uses such a library) which might be a better
183 options, for instance when converting an image. We kept the interfaces minimal
184 and assume some \LUA\ wrapping. It helps to keep the dependencies to a minimum.
185
186 \stopsection
187
188 \startsection[title=Execution]
189
190 Then there is program execution (aka \type {\write18} in other engines than
191 \LUATEX\ and \LUAMETATEX). The engine is not responsible for how \typ
192 {os.execute} and \typ {io.popen} are used and macro packages can decide to
193 disable it.
194
195 \stopsection
196
197 \stopdocument