66-tools

Software

obarun.org

66-ns

66-ns(namespace) setup a namespace and execs a program inside it.

Interface

    66-ns [ -h ] [ -z ] [ -v verbosity ] [ -d notif ] [ -o ns_options,... ] [ -e element:type:options:,... ] [ -r rule ] prog

66-ns sets up a namespace(sandbox) according to the options passed at the command line or/and by a rule file. 66-ns allows to have a fine grain of the namespace configuration and permit to limit the exposure of the system from prog processes. This tool can be used to start a service inside a namespace or directly from a terminal.

The host file system is mounted recursively by default such as if you don't pass any options to 66-ns the result is a strict copy of the host.

With a good configuration—see Usage examples, it allows to survey a daemon which forks itself, thus multi-processes which fork themselves.

66-ns can only by launched as root user.

Exit codes

Options

Namespace options

The -o option can be:

Element options

An element is set by a series of options separated by colon. The first option must be the name of the element to handle and must be an absolute path. Other options have no order of preference. let's see an example before going forward:

    66-ns -e /etc:options=ro -e /etc/resolv.conf:type=clone:options=rw -e /dev:type=tmpfs:options=nosuid,strictatime,noexec,mode=755,size=4m,nr_inodes=64k -e /proc:type=proc -e /sys:type=recursive:options=ro sh -i

Options can be:

Type specifications

Rule file

It can be really tedious, repetitive and unreadable to setup a namespace uniquely with the -e option e.g:

    66-ns -e /dev:type=tmpfs:options=nosuid,strictatime,noexec,mode=755,size=4m,nr_inodes=64k -e /dev/pts:options=rw: -e /dev/shm:options=rw -e /dev/hugepages:options=rw -e /dev/mqueue:options=rw -e /dev/ptmx:type=clone sh -i

The rule file solves this.

General rule file syntax

The rule file has the format INI. To reduce the complexity of the parse process, comments are not allowed where empty an line exists. You can set the rule file name as you want as long as the name is suffixed with .rule.

Rule file invocation

The invocation of a rule file can be done by relative, absolute path or directly by its name. In case of direct name invocation, the file need to be place at %%ns_rule%% directory. The default path can be changed at compile time by passing the --with-ns-rule=DIR at ./configure.

Rule file syntax

The file is made of sections which can contain one or more key value pairs where the key name corresponds to the element option name and value to the option itself. The name of the section corresponds to the element name to handle.

The examples/rule subdirectory of the 66-tools package contains a set of rules, which is actually a working, valid set for setup a namespace.

A special section named [include] allows to include another rule file in that rule file e.g.:

[include]
system-strict.rule
/usr/lib/66/script/ns/private-dev.rule

[/etc/resolv.conf]
options=rw

Each rule name set at [include] section is parsed and applied in conjunction to the definition of the /etc/resolv.conf element.

Translation format between rule file and -e option

For example, this command line invocation:

    66-ns -e /dev:type=tmpfs:options=nosuid,strictatime,noexec,mode=755,size=4m,nr_inodes=64k -e /dev/pts:options=rw: -e /dev/shm:options=rw -e /dev/hugepages:options=rw -e /dev/mqueue:options=rw -e /dev/ptmx:type=clone sh -i

can be made by the following invocation:

    66-ns -r dev-example.rule sh -i

where dev-example.rule contain:

[/dev]
type=tmpfs
options=nosuid,strictatime,noexec,mode=755,size=4m,nr_inodes=64k

[/dev/pts]
options=rw

[/dev/shm]
options=rw

[/dev/hugepages]
options=rw

[/dev/mqueue]
options=rw

[/dev/ptmx]
type=clone

Usage examples

Mounts / as read-only file system:

    66-ns -e /:options=ro sh -i

Mounts / as read-only except /dev which is recursively mounted read-write:

    66-ns -e /:options=ro -e /dev:type=recursive sh -i

Avoid access to the /usr/lib/modules directory:

    66-ns -e /usr/lib/modules:type=hidden sh -i

Prog cannot gain new privileges, mounts a new “/proc” virtual file system and makes /etc read-only:

    66-ns -o nonewprivileges, -e /proc:type=proc -e /etc:options=ro sh -i

Mount /etc with tmpfs type and give access uniquely to /etc/resolv.conf file and /etc/ssh directory which is read-only:

    66-ns -e /etc:type=tmpfs -e /etc/resolv.conf:type=clone /etc/ssh:options=bind,ro sh -i

Invoke a rule file:

    66-ns -e -r myrule sh -i

Invoke 66-ns inside a frontend file to keep the control of dhcpcd which forks itself:

[main]
@type = longrun
@version = 0.1.1
@description = "Keep control on dhcpcd daemon which forks itself"
@user = ( root )
@options = ( log )

[start]
@execute = ( 66-ns -o unshare=pid dhcpcd )

Hide the all processes from the host and run a trivial PID1 inside the namespace(66-ns will be the PID1 and sh the PID2):

    66-ns -o unshare=pid -e /proc:type=proc sh -i