Skip to content

Commit

Permalink
Refactoring: remove duplicate code into utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippWendler authored and EshaanAgg committed Jun 28, 2024
1 parent b2487c4 commit 1ad20fe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
15 changes: 15 additions & 0 deletions benchexec/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"CONTAINER_GID",
"CONTAINER_HOME",
"CONTAINER_HOSTNAME",
"check_apparmor_userns_restriction",
]


Expand Down Expand Up @@ -124,6 +125,20 @@
)


def check_apparmor_userns_restriction(error: OSError):
"""Check whether the passed OSError was likely caused by Ubuntu's AppArmor-based
restriction of user namespaces."""
return (
error.errno
in [
errno.EPERM,
errno.EACCES,
]
and util.try_read_file("/proc/sys/kernel/apparmor_restrict_unprivileged_userns")
== "1"
)


@contextlib.contextmanager
def allocate_stack(size=DEFAULT_STACK_SIZE):
"""Allocate some memory that can be used as a stack.
Expand Down
7 changes: 1 addition & 6 deletions benchexec/containerexecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,12 +754,7 @@ def child():
traceback.extract_tb(e.__traceback__, limit=-1)[0].line,
e,
)
if util.try_read_file(
"/proc/sys/kernel/apparmor_restrict_unprivileged_userns"
) == "1" and e.errno in [
errno.EPERM,
errno.EACCES,
]:
if container.check_apparmor_userns_restriction(e):
logging.critical(container._ERROR_MSG_USER_NS_RESTRICTION)
return CHILD_OSERROR

Expand Down
8 changes: 1 addition & 7 deletions benchexec/containerized_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,7 @@ def _init_container_and_load_tool(tool_module, *args, **kwargs):
try:
_init_container(*args, **kwargs)
except OSError as e:
if (
util.try_read_file("/proc/sys/kernel/apparmor_restrict_unprivileged_userns")
== "1"
) and e.errno in [
errno.EPERM,
errno.EACCES,
]:
if container.check_apparmor_userns_restriction(e):
raise BenchExecException(container._ERROR_MSG_USER_NS_RESTRICTION)
raise BenchExecException(f"Failed to configure container: {e}")
return _load_tool(tool_module)
Expand Down

0 comments on commit 1ad20fe

Please sign in to comment.