Skip to content

Commit 28d60d8

Browse files
committed
krun: ensure spec->linux->resources->devices exists
Fixes #1856 Signed-off-by: iczero <[email protected]>
1 parent f7e1211 commit 28d60d8

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/libcrun/handlers/krun.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -706,13 +706,9 @@ libkrun_modify_oci_configuration (void *cookie arg_unused, libcrun_context_t *co
706706
struct krun_config *kconf = (struct krun_config *) cookie;
707707
struct stat st_kvm, st_sev, st_nitro;
708708
bool has_kvm = true, has_sev = true, has_nitro = true;
709-
size_t len;
709+
size_t old_len, new_len;
710710
int ret;
711711

712-
if (def->linux == NULL || def->linux->resources == NULL
713-
|| def->linux->resources->devices == NULL)
714-
return 0;
715-
716712
/* Always allow the /dev/kvm device. */
717713

718714
ret = stat ("/dev/kvm", &st_kvm);
@@ -739,31 +735,38 @@ libkrun_modify_oci_configuration (void *cookie arg_unused, libcrun_context_t *co
739735
has_nitro = false;
740736
}
741737

742-
if (has_kvm)
743-
{
744-
len = def->linux->resources->devices_len;
745-
def->linux->resources->devices = xrealloc (def->linux->resources->devices,
746-
device_size * (len + 2 + (has_sev ? 1 : 0)));
738+
kconf->has_kvm = has_kvm;
739+
kconf->has_nitro = has_nitro;
747740

748-
def->linux->resources->devices[len] = make_oci_spec_dev ("a", st_kvm.st_rdev, true, "rwm");
749-
if (has_sev)
750-
def->linux->resources->devices[len + 1] = make_oci_spec_dev ("a", st_sev.st_rdev, true, "rwm");
741+
if (! has_kvm && ! has_nitro)
742+
return 0;
751743

752-
def->linux->resources->devices_len += has_sev ? 2 : 1;
753-
}
744+
/* spec says these are optional, ensure they exist so we can add our devices */
745+
if (def->linux == NULL)
746+
def->linux = xmalloc0 (sizeof (runtime_spec_schema_config_linux));
747+
748+
if (def->linux->resources == NULL)
749+
def->linux->resources = xmalloc0 (sizeof (runtime_spec_schema_config_linux_resources));
754750

751+
old_len = def->linux->resources->devices_len;
752+
new_len = old_len;
753+
if (has_kvm)
754+
new_len += has_sev ? 2 : 1;
755755
if (has_nitro)
756-
{
757-
len = def->linux->resources->devices_len;
758-
def->linux->resources->devices = xrealloc (def->linux->resources->devices,
759-
device_size * (len + 2));
756+
new_len += 1;
757+
758+
def->linux->resources->devices = xrealloc (def->linux->resources->devices, device_size * (new_len + 1));
759+
def->linux->resources->devices_len = new_len;
760760

761-
def->linux->resources->devices[len] = make_oci_spec_dev ("a", st_nitro.st_rdev, true, "rwm");
762-
def->linux->resources->devices_len++;
761+
if (has_kvm)
762+
{
763+
def->linux->resources->devices[old_len++] = make_oci_spec_dev ("a", st_kvm.st_rdev, true, "rwm");
764+
if (has_sev)
765+
def->linux->resources->devices[old_len++] = make_oci_spec_dev ("a", st_sev.st_rdev, true, "rwm");
763766
}
764767

765-
kconf->has_kvm = has_kvm;
766-
kconf->has_nitro = has_nitro;
768+
if (has_nitro)
769+
def->linux->resources->devices[old_len++] = make_oci_spec_dev ("a", st_nitro.st_rdev, true, "rwm");
767770

768771
return 0;
769772
}

0 commit comments

Comments
 (0)