Skip to content

Commit fc46676

Browse files
committed
linux: reject sysctl kernel.domainname when OCI knob domainname is set
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 <[email protected]>
1 parent a73a1d4 commit fc46676

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/libcrun/linux.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,7 +3161,7 @@ const char *sysctlRequiringIPC[] = {
31613161
};
31623162

31633163
static int
3164-
validate_sysctl (const char *original_value, const char *name, unsigned long namespaces_created, libcrun_error_t *err)
3164+
validate_sysctl (const char *original_value, const char *name, unsigned long namespaces_created, runtime_spec_schema_config_schema *def, libcrun_error_t *err)
31653165
{
31663166
const char *namespace = "";
31673167

@@ -3192,11 +3192,20 @@ validate_sysctl (const char *original_value, const char *name, unsigned long nam
31923192

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

3198-
namespace = "UTS";
3199-
goto fail;
3206+
namespace = "UTS";
3207+
goto fail;
3208+
}
32003209
}
32013210

32023211
if (strcmp (name, "kernel/hostname") == 0)
@@ -3256,7 +3265,7 @@ libcrun_set_sysctl (libcrun_container_t *container, libcrun_error_t *err)
32563265
if (*it == '.')
32573266
*it = '/';
32583267

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

tests/test_domainname.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,31 @@ def test_domainname():
4343
if cid is not None:
4444
run_crun_command(["delete", "-f", cid])
4545

46-
46+
def test_domainname_conflict_sysctl():
47+
# Setting sysctl `kernel.domainname` and OCI field `domainname` must fail
48+
# since it produces unexpected behaviour for the end-users.
49+
conf = base_config()
50+
conf['process']['args'] = ['/init', 'getdomainname']
51+
conf['domainname'] = "foomachine"
52+
add_all_namespaces(conf)
53+
conf['linux']['sysctl'] = {'kernel.domainname' : 'foo'}
54+
cid = None
55+
try:
56+
out, cid = run_and_get_output(conf)
57+
if out == "(none)\n":
58+
return 0
59+
sys.stderr.write("unexpected success\n")
60+
return -1
61+
except:
62+
return 0
63+
finally:
64+
if cid is not None:
65+
run_crun_command(["delete", "-f", cid])
66+
return 0
67+
4768
all_tests = {
4869
"domainname" : test_domainname,
70+
"domainname conflict with syctl" : test_domainname_conflict_sysctl,
4971
}
5072

5173
if __name__ == "__main__":

tests/test_start.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,4 @@ def test_listen_pid_env():
486486

487487
if __name__ == "__main__":
488488
tests_main(all_tests)
489+

0 commit comments

Comments
 (0)