Blender V5.3
bpy_driver_bytecode.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include <Python.h>
14
16
17#define USE_BYTECODE_SECURE
18
19#ifdef USE_BYTECODE_SECURE
20# include <opcode.h>
21/* WARNING(@ideasman42): Using `Py_BUILD_CORE` is a last resort,
22 * the alternative would be not to inspect OP-CODES at all. */
23# define Py_BUILD_CORE
24# include <internal/pycore_code.h>
25#endif
26
27namespace blender {
28
29#ifdef USE_BYTECODE_SECURE
30
31static bool is_opcode_secure(const int opcode)
32{
33 /*
34 * Intentionally Excluded Opcodes
35 * ==============================
36 *
37 * Likely Safe but Unnecessary
38 * ---------------------------
39 *
40 * These opcodes appear safe but are not needed for driver expressions.
41 *
42 * In the interest of reducing the attack surface, exclude them unless
43 * practical use cases are found.
44 *
45 * - `CALL_INTRINSIC_2`: Used for exception handling (`except*`) and type hints.
46 * Not needed for driver expressions.
47 *
48 * - `MAKE_FUNCTION`, `LOAD_BUILD_CLASS`: Function/class creation not needed.
49 *
50 * Known to be Dangerous
51 * ---------------------
52 *
53 * These opcodes are excluded for security reasons:
54 *
55 * - `DICT_MERGE`, `DICT_UPDATE`: Could potentially be used to manipulate
56 * the namespace via `globals()`, allowing malicious code execution.
57 *
58 * - `IMPORT_NAME`, `IMPORT_FROM`: Module imports are not allowed.
59 *
60 * - `STORE_NAME`, `STORE_GLOBAL`, `STORE_ATTR`: Storing to names/globals/attributes
61 * could modify the namespace or objects in unsafe ways.
62 *
63 * - `LOAD_ATTR`: Attribute access is not allowed as it could access
64 * private/internal attributes.
65 */
66
67# define OK_OP(op) \
68 case op: \
69 return true;
70
71 switch (opcode) {
72 OK_OP(CACHE)
73 OK_OP(COPY) /* Ok for short-circuit boolean evaluation (`and`, `or`). */
74 OK_OP(POP_TOP)
75 OK_OP(PUSH_NULL)
76 OK_OP(NOP)
77# if PY_VERSION_HEX >= 0x030e0000
78 OK_OP(NOT_TAKEN)
79# endif
80 OK_OP(TO_BOOL) /* Ok for boolean conversion in `and`/`or` expressions. */
81 OK_OP(UNARY_NEGATIVE)
82 OK_OP(UNARY_NOT)
83 OK_OP(UNARY_INVERT)
84# if PY_VERSION_HEX < 0x030e0000
85 OK_OP(BINARY_SUBSCR) /* Replaced with existing `BINARY_OP`. */
86# endif
87 OK_OP(GET_LEN)
88 OK_OP(RETURN_VALUE)
89 OK_OP(SWAP)
90 OK_OP(BUILD_TUPLE)
91 OK_OP(BUILD_LIST)
92 OK_OP(BUILD_SET)
93 OK_OP(BUILD_MAP)
94 OK_OP(COMPARE_OP)
95 OK_OP(JUMP_FORWARD)
96 OK_OP(LOAD_GLOBAL)
97 OK_OP(IS_OP)
98 OK_OP(CONTAINS_OP)
99 OK_OP(BINARY_OP)
100 OK_OP(LOAD_FAST)
101 OK_OP(LOAD_FAST_AND_CLEAR) /* Ok, optimized variant of `LOAD_FAST`. */
102 OK_OP(LOAD_FAST_LOAD_FAST) /* Ok, optimized double `LOAD_FAST`. */
103# if PY_VERSION_HEX >= 0x030e0000
104 OK_OP(LOAD_FAST_BORROW) /* Ok, optimized variant of `LOAD_FAST`. */
105 OK_OP(LOAD_FAST_BORROW_LOAD_FAST_BORROW) /* Ok, optimized double `LOAD_FAST`. */
106# endif
107 OK_OP(STORE_FAST)
108 OK_OP(STORE_FAST_LOAD_FAST) /* Ok, optimized `STORE_FAST` + `LOAD_FAST`. */
109 OK_OP(STORE_FAST_STORE_FAST) /* Ok, optimized double `STORE_FAST`. */
110 OK_OP(DELETE_FAST)
111 OK_OP(BUILD_SLICE)
112 OK_OP(LOAD_DEREF)
113 OK_OP(STORE_DEREF)
114 OK_OP(RESUME)
115 OK_OP(LIST_EXTEND)
116 OK_OP(SET_UPDATE)
117/* NOTE(@ideasman42): Don't enable dict manipulation, unless we can prove there is not way it
118 * can be used to manipulate the name-space (potentially allowing malicious code). */
119# if 0
120 OK_OP(DICT_MERGE)
121 OK_OP(DICT_UPDATE)
122# endif
123
124# if PY_VERSION_HEX < 0x030e0000
125 OK_OP(RETURN_CONST)
126# endif
127 /* Ok, conditional jumps only affect control flow within the expression. */
128 OK_OP(POP_JUMP_IF_FALSE) /* Used for `and` expressions and `if` conditionals. */
129 OK_OP(POP_JUMP_IF_TRUE) /* Used for `or` expressions. */
130 OK_OP(POP_JUMP_IF_NONE) /* Used for `is not None` conditionals. */
131 OK_OP(POP_JUMP_IF_NOT_NONE) /* Used for `is None` conditionals. */
132 OK_OP(CALL_INTRINSIC_1)
133 /* Special cases. */
134 OK_OP(LOAD_CONST) /* Ok because constants are accepted. */
135 OK_OP(LOAD_NAME) /* Ok, because `PyCodeObject.names` is checked. */
136# if PY_VERSION_HEX >= 0x030e0000
137 OK_OP(LOAD_SMALL_INT)
138# endif
139 OK_OP(CALL) /* Ok, because we check its "name" before calling. */
140 OK_OP(CALL_KW) /* Ok, because it's used for calling functions with keyword arguments. */
141
142 OK_OP(CALL_FUNCTION_EX);
143
144 /* OK because the names are checked. */
145 OK_OP(CALL_ALLOC_AND_ENTER_INIT)
146 OK_OP(CALL_BOUND_METHOD_EXACT_ARGS)
147 OK_OP(CALL_BOUND_METHOD_GENERAL)
148 OK_OP(CALL_BUILTIN_CLASS)
149 OK_OP(CALL_BUILTIN_FAST)
150 OK_OP(CALL_BUILTIN_FAST_WITH_KEYWORDS)
151 OK_OP(CALL_BUILTIN_O)
152 OK_OP(CALL_ISINSTANCE)
153 OK_OP(CALL_LEN)
154 OK_OP(CALL_LIST_APPEND)
155 OK_OP(CALL_METHOD_DESCRIPTOR_FAST)
156 OK_OP(CALL_METHOD_DESCRIPTOR_FAST_WITH_KEYWORDS)
157 OK_OP(CALL_METHOD_DESCRIPTOR_NOARGS)
158 OK_OP(CALL_METHOD_DESCRIPTOR_O)
159 OK_OP(CALL_NON_PY_GENERAL)
160 OK_OP(CALL_PY_EXACT_ARGS)
161 OK_OP(CALL_PY_GENERAL)
162 OK_OP(CALL_STR_1)
163 OK_OP(CALL_TUPLE_1)
164 OK_OP(CALL_TYPE_1)
165 }
166
167# undef OK_OP
168 return false;
169}
170
171bool BPY_driver_secure_bytecode_test_ex(PyObject *expr_code,
172 PyObject *py_namespace_array[],
173 const bool verbose,
174 const char *error_prefix)
175{
176 PyCodeObject *py_code = reinterpret_cast<PyCodeObject *>(expr_code);
177
178 /* Check names. */
179 {
180 for (int i = 0; i < PyTuple_GET_SIZE(py_code->co_names); i++) {
181 PyObject *name = PyTuple_GET_ITEM(py_code->co_names, i);
182 const char *name_str = PyUnicode_AsUTF8(name);
183 bool contains_name = false;
184 for (int j = 0; py_namespace_array[j]; j++) {
185 if (PyDict_Contains(py_namespace_array[j], name)) {
186 contains_name = true;
187 break;
188 }
189 }
190
191 if ((contains_name == false) || (name_str[0] == '_')) {
192 if (verbose) {
193 fprintf(stderr,
194 "\t%s: restricted access disallows name '%s', "
195 "enable auto-execution to support\n",
196 error_prefix,
197 name_str);
198 }
199 return false;
200 }
201 }
202 }
203
204 /* Check opcodes. */
205 {
206 const _Py_CODEUNIT *codestr;
207 Py_ssize_t code_len;
208
209 PyObject *co_code;
210
211 co_code = PyCode_GetCode(py_code);
212 if (!co_code) {
213 PyErr_Print();
214 return false;
215 }
216
217 PyBytes_AsStringAndSize(co_code, (char **)&codestr, &code_len);
218 code_len /= sizeof(*codestr);
219 bool ok = true;
220
221 /* Loop over op-code's, the op-code arguments are ignored. */
222 for (Py_ssize_t i = 0; i < code_len; i++) {
223 const int opcode = _Py_OPCODE(codestr[i]);
224 if (!is_opcode_secure(opcode)) {
225 if (verbose) {
226 fprintf(stderr,
227 "\t%s: restricted access disallows opcode '%d', "
228 "enable auto-execution to support\n",
229 error_prefix,
230 opcode);
231 }
232 ok = false;
233 break;
234 }
235 }
236
237 Py_DECREF(co_code);
238 if (!ok) {
239 return false;
240 }
241 }
242
243 return true;
244}
245
246#endif /* USE_BYTECODE_SECURE */
247
248}; // namespace blender
#define SWAP(type, a, b)
#define OK_OP(op)
BMesh Iterators.
bool BPY_driver_secure_bytecode_test_ex(PyObject *expr_code, PyObject *py_namespace_array[], const bool verbose, const char *error_prefix)
static bool is_opcode_secure(const int opcode)
#define CALL(member, value_str)