Skip to content

Commit

Permalink
linux: reject sysctl kernel.domainname when OCI knob domainname is set
Browse files Browse the repository at this point in the history
Setting sysctl `kernel.domainname` directly by user is not environment
agnostic, it shows either incorrect ( on non-working ) behaviour in
`rootless` environment.

It was decided to make this part of `runtime-spec` so the OCI runtime
can itself handle this behaviour correctly. As a result a new field
`domainname` was added to `runtime-spec`. Since crun already implementes
this field therefore `sysctl` configured by user conflicts with the
behaviour expected by the OCI runtime.

Runtime-spec PR: opencontainers/runtime-spec#1156

Furthermore a similar `sysctl` `kernal.hostname` is blocked by crun explicitly
to prevent this conflicting behaviour. https://github.com/containers/crun/blob/main/src/libcrun/linux.c#L3203

Following commit ensures that crun rejects sysctl `kernel.domainname`
when OCI field `domainname` is already set.

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed Oct 3, 2022
1 parent a73a1d4 commit ab895b8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/libcrun/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -3161,7 +3161,7 @@ const char *sysctlRequiringIPC[] = {
};

static int
validate_sysctl (const char *original_value, const char *name, unsigned long namespaces_created, libcrun_error_t *err)
validate_sysctl (const char *original_value, const char *name, unsigned long namespaces_created, runtime_spec_schema_config_schema *def, libcrun_error_t *err)
{
const char *namespace = "";

Expand Down Expand Up @@ -3192,11 +3192,22 @@ validate_sysctl (const char *original_value, const char *name, unsigned long nam

if (strcmp (name, "kernel/domainname") == 0)
{
if (namespaces_created & CLONE_NEWUTS)
return 0;
if (! is_empty_string (def->domainname))
{
// Value of sysctl `kernel/domainname` is going to
// conflict with already set field `domainname` in
// OCI spec, in such scenario crun will fail to prevent
// unexpected behaviour for end user.
return crun_make_error (err, 0, "the sysctl `%s` conflicts with OCI field `domainname`", original_value);
}
else
{
if (namespaces_created & CLONE_NEWUTS)
return 0;

namespace = "UTS";
goto fail;
namespace = "UTS";
goto fail;
}
}

if (strcmp (name, "kernel/hostname") == 0)
Expand Down Expand Up @@ -3256,7 +3267,7 @@ libcrun_set_sysctl (libcrun_container_t *container, libcrun_error_t *err)
if (*it == '.')
*it = '/';

ret = validate_sysctl (def->linux->sysctl->keys[i], name, namespaces_created, err);
ret = validate_sysctl (def->linux->sysctl->keys[i], name, namespaces_created, def, err);
if (UNLIKELY (ret < 0))
return ret;

Expand Down
24 changes: 23 additions & 1 deletion tests/test_domainname.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,31 @@ def test_domainname():
if cid is not None:
run_crun_command(["delete", "-f", cid])


def test_domainname_conflict_sysctl():
# Setting sysctl `kernel.domainname` and OCI field `domainname` must fail
# since it produces unexpected behaviour for the end-users.
conf = base_config()
conf['process']['args'] = ['/init', 'getdomainname']
conf['domainname'] = "foomachine"
add_all_namespaces(conf)
conf['linux']['sysctl'] = {'kernel.domainname' : 'foo'}
cid = None
try:
out, cid = run_and_get_output(conf)
if out == "(none)\n":
return 0
sys.stderr.write("unexpected success\n")
return -1
except:
return 0
finally:
if cid is not None:
run_crun_command(["delete", "-f", cid])
return 0

all_tests = {
"domainname" : test_domainname,
"domainname conflict with syctl" : test_domainname_conflict_sysctl,
}

if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions tests/test_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,4 @@ def test_listen_pid_env():

if __name__ == "__main__":
tests_main(all_tests)

0 comments on commit ab895b8

Please sign in to comment.