Pyseccomp is a pure Python interface to the libseccomp library, leveraging ctypes to provide syscall filtering capabilities via Linux's seccomp mechanism. It aims for API compatibility with libseccomp's official Python bindings. The library is actively maintained, with its latest release (version 0.1.2) published in January 2021.
pip install pyseccompVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a `SyscallFilter` with a default `ALLOW` action. It then adds rules to deny specific syscalls such as `execve`, `execveat`, `vfork`, and `fork`. The example shows how to configure an action (e.g., `LOG` or `ERRNO`) for denied syscalls before loading the filter into the kernel. An attempt to `os.fork()` is included to illustrate how the applied seccomp filter prevents this operation, resulting in an `OSError`.
Upgrade `pyseccomp` to version 0.1.2 or later, which includes a fix for `libseccomp < 2.4` compatibility. It is also recommended to keep your system's `libseccomp` library updated.
Update `pyseccomp` to version 0.1.1 or newer to ensure all necessary function prototypes are included, resolving potential segfaults.
Ensure that the `libseccomp` development package (e.g., `libseccomp-dev` on Debian/Ubuntu, `libseccomp-devel` on Fedora/CentOS) is installed on your operating system.
Start with a permissive policy (`ALLOW`) and progressively add `DENY` rules, or start with a restrictive policy (`KILL`, `TRAP`) and incrementally `ALLOW` only necessary syscalls. Utilize the `CTL_LOG` attribute (`f.set_attr(seccomp.Attr.CTL_LOG, 1)`) to log blocked syscalls during development, aiding in debugging. Thoroughly test the application under the seccomp filter.
Install the `libseccomp` development package using your system's package manager (e.g., `sudo apt-get install libseccomp-dev` on Debian/Ubuntu or `sudo dnf install libseccomp-devel` on Fedora/RHEL).
Install the package using pip: `pip install pyseccomp`.
Run the application with elevated privileges (e.g., as root, or with appropriate capabilities if using a container runtime like Docker/Podman) and ensure the kernel supports seccomp in the execution environment.