diff --git a/README.md b/README.md index 074b966..d419d5c 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ You can browse all advisories at https://github.com/Infinitode/CodeSafe/security - **Affected versions**: 0.0.6 only. Versions 0.0.1-0.0.5 are not affected by this specific bug: they return results through a `multiprocessing.Queue` with the standard library's `pickle`, and this gadget cannot survive that encoder (`Can't pickle local object`). Those versions are affected by the earlier advisories instead. Note that 0.0.5's `Queue.get()` does unpickle in the parent, so the underlying pattern is similar, but no working payload of this kind was found for 0.0.5. - **Fixed in**: Version **0.0.7**. The result boundary no longer involves pickle at all: the child encodes only plain data (see below) and the parent reconstructs it from primitives. A returned value has no code path into the caller. - **Workaround for 0.0.6**: none that is complete - treat every expression as fully trusted, or upgrade. -- **Related hardening in the same release**: `str.format`/`format_map` templates that name attributes or keys (`'{0.__class__}'.format(obj)`) are rejected; rebinding a safe-builtin name (`(len := f)()`, `[f() for len in [..]]`, `(lambda len: len())(f)`) no longer lets an expression call a non-allowlisted callable; callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) follow the same allowlist; `__builtins__` is not reachable by name; `SystemExit`/`KeyboardInterrupt`/`GeneratorExit` raised by an expression arrive as a catchable `RuntimeError` instead of killing or unwinding the caller; and `file_access=False` can no longer be bypassed by re-introducing `open` through `allowed_builtins`. +- **Related hardening in the same release**: `str.format`/`format_map` templates that name attributes or keys (`'{0.__class__}'.format(obj)`) are rejected; callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) now follow the same allowlist as direct calls - before this, one arriving through `allowed_vars` was invoked by the sandbox even though it was never allowlisted; `__builtins__` is not reachable by name; and `SystemExit`/`KeyboardInterrupt`/`GeneratorExit` raised by an expression arrive as a catchable `RuntimeError` instead of killing or unwinding the caller. **What `safe_eval()` returns in 0.0.7.** Results are restricted to plain data: `None`, `bool`, `int`, `float`, `complex`, `str`, `bytes`, `decimal.Decimal`, `fractions.Fraction`. Containers of plain data (`list`, `tuple`, `dict`, `set`) are returned only when `allow_objects=True` is passed. Lambdas, classes, instances of user-defined types, modules and generators are no longer returned - they raise a clear `RuntimeError` instead of being serialized. If your code relied on returning lambdas or dynamic classes, restructure the expression to return data. @@ -134,11 +134,11 @@ python -m unittest test.py test_poc_vulnerability.py -v * **Security Fix (critical)**: `safe_eval()` no longer deserializes the evaluation result with `cloudpickle` in the calling process. In 0.0.6 the child serialized its result and the parent called `cloudpickle.loads()` on it with full builtins, so an expression could return a `__reduce__` gadget that ran arbitrary code in the *caller* - outside the sandbox that had just been enforced. The result now crosses the process boundary as plain primitives and is rebuilt with a non-executable decoder. See the [Security Advisory](#security-advisory) above. * **Changed**: `safe_eval()` returns plain data only - `None`, `bool`, `int`, `float`, `complex`, `str`, `bytes`, `Decimal`, `Fraction`. Containers of plain data are opt-in via the new `allow_objects=True` parameter. Lambdas, dynamic classes, instances of user-defined types, modules and generators raise a clear `RuntimeError` instead of being pickled. **This removes a feature that 0.0.6 advertised**; it was also the attack surface. * **Security Fix**: `str.format`/`format_map` templates that traverse attributes or mapping keys (`'{0.__class__}'.format(obj)`, `'{0[key]}'.format(obj)`) are rejected - a replacement field is a string constant, so these lookups produce no `ast.Attribute` node for the checker to inspect. Reported by [@stilwel](https://github.com/stilwel); see the [Security Advisory](#security-advisory) above. -* **Security Fix**: An expression can no longer rebind a safe-builtin *name* and then call it - `(len := host_callable)()`, `[host_callable() for len in [..]]` and `(lambda len: len())(host_callable)` previously reached a caller-supplied callable that was not on `allowed_function_calls`. Such calls now require explicit allowlisting. -* **Security Fix**: Callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) follow the same allowlist as direct calls. `allowed_vars` entries are data, not implicit call permissions. +* **Security (defence in depth)**: Rebinding a safe-builtin name and calling through it (`(len := host_callable)()`, `[host_callable() for len in [..]]`, `(lambda len: len())(host_callable)`) is now refused by an explicit name-resolution rule rather than only by the call-target check. Note that 0.0.6 already rejected every such shape with `UnsafeExpressionError` - this hardens the invariant, it does not close an exploitable hole. +* **Security Fix**: Callables passed to builtins that invoke them (`sorted(key=...)`, `iter(f, None)`) follow the same allowlist as direct calls. On 0.0.6 such a callable *was* invoked inside the sandbox even though it was never allowlisted - it then failed afterwards (an `AttributeError`, or a timeout for `iter`) - so this closes a real gap in the call policy, though it did not by itself yield code execution in the caller. `allowed_vars` entries are data, not implicit call permissions. * **Security Fix**: `__builtins__` is no longer reachable by name from an expression, and can no longer be rebound or mutated. * **Security Fix**: An expression raising `SystemExit`, `KeyboardInterrupt` or `GeneratorExit` no longer terminates or unwinds the calling process; it arrives as a catchable `RuntimeError`. Exceptions of user-defined types are reported the same way instead of being reconstructed in the parent. -* **Security Fix**: `file_access=False` and `network_access=False` are enforced *after* `allowed_builtins` is merged, so passing `open` (or similar) through `allowed_builtins` no longer bypasses the flag. +* **Security (defence in depth)**: `file_access=False` and `network_access=False` are now enforced *after* `allowed_builtins` is merged, so the guarantee no longer depends on the AST call check happening to reject the target first. In 0.0.6 the bypass was already unreachable - `open` passed through `allowed_builtins` was rejected with `UnsafeExpressionError` unless it was *also* listed in `allowed_function_calls`, and in that case the file handle could not cross the result boundary. This makes the invariant structural. * **Security**: Returned data is bounded (nesting depth 100, 1M nodes, 1 MiB of `str`/`bytes`), and int-to-str conversion is capped for the duration of a call so a returned giant integer cannot burn CPU in the caller. * **Changed**: The result transport is a `multiprocessing.Pipe` drained by a reader thread instead of a `Queue`. A result larger than the OS pipe buffer can no longer deadlock the parent, and the timing of `EvaluationTimeoutError` is tighter. * **Changed**: The `cloudpickle` dependency was removed - CodeSafe no longer imports pickle on the evaluation path at all.