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 Jan 8, 2024
1 parent 6950d93 commit 52e9322
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion poetry.lock

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

29 changes: 29 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,7 @@
import plotext as plt
from structlog import get_logger
from unblob_native import math_tools as mt
from unblob_native.sandbox import AccessFS, restrict_access # type: ignore

from unblob.handlers import BUILTIN_DIR_HANDLERS, BUILTIN_HANDLERS, Handlers

Expand Down Expand Up @@ -111,6 +113,29 @@ 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(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 @@ -135,6 +160,10 @@ def process_file(
)
return ProcessResult()

if not hasattr(process_file, "_sandboxed"):
sandbox(extract_dir, report_file)
process_file._sandboxed = True # noqa: SLF001

process_result = _process_task(config, task)

if not config.skip_extraction:
Expand Down

0 comments on commit 52e9322

Please sign in to comment.