Skip to content

Commit

Permalink
feat: introduce landlock based sandboxing
Browse files Browse the repository at this point in the history
Co-authored-by: Quentin Kaiser <[email protected]>
  • Loading branch information
vlaci and qkaiser committed Feb 21, 2024
1 parent 3c2db4a commit 6a81e08
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ lz4 = "^4.3.2"
lief = "^0.12.3"
cryptography = ">=41.0,<43.0"
treelib = "^1.7.0"
unblob-native = "^0.1.1"
unblob-native = "^0.1.2"
jefferson = "^0.4.5"
rich = "^13.3.5"
pyfatfs = "^1.0.5"
Expand Down
37 changes: 37 additions & 0 deletions unblob/processing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import multiprocessing
import shutil
import sys
from operator import attrgetter
from pathlib import Path
from typing import Iterable, List, Optional, Sequence, Set, Tuple, Type, Union
Expand All @@ -9,6 +10,11 @@
import plotext as plt
from structlog import get_logger
from unblob_native import math_tools as mt
from unblob_native.sandbox import ( # type: ignore
AccessFS,
SandboxError,
restrict_access,
)

from unblob.handlers import BUILTIN_DIR_HANDLERS, BUILTIN_HANDLERS, Handlers

Expand Down Expand Up @@ -112,6 +118,30 @@ def get_extract_dir_for(self, path: Path) -> Path:
return extract_dir.expanduser().resolve()


def sandbox(extract_dir: Path, report_file: Optional[Path]):
restrictions = [
AccessFS.read("/"),
AccessFS.read_write("/dev/shm"), # noqa: S108
AccessFS.read_write(extract_dir.as_posix()),
AccessFS.make_dir(extract_dir.parent.as_posix()),
]

if report_file:
restrictions += [
AccessFS.read_write(report_file),
AccessFS.make_reg(report_file.parent),
]

if "pytest" in sys.modules:
restrictions += [
AccessFS.read_write("/tmp"), # noqa: S108
AccessFS.read_write("/build"),
AccessFS.read_write(Path(__file__).parent.parent.resolve().as_posix()),
]

restrict_access(*restrictions)


@terminate_gracefully
def process_file(
config: ExtractionConfig, input_path: Path, report_file: Optional[Path] = None
Expand All @@ -136,6 +166,13 @@ def process_file(
)
return ProcessResult()

try:
if not hasattr(process_file, "_sandboxed"):
sandbox(extract_dir, report_file)
process_file._sandboxed = True # noqa: SLF001
except SandboxError:
logger.warning("Sandboxing FS access is unavailable on this system, skipping.")

process_result = _process_task(config, task)

if not config.skip_extraction:
Expand Down

0 comments on commit 6a81e08

Please sign in to comment.