[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 3/4] tools/libxl: add emulated TPM support backed by swtpm



Hi Jason,

> libxl typically tracks spawned daemons through xenstore keys - like QEMU
> writing its "state" key as "running".

That's not required, libxl can find out if QEMU is ready or not via the
QMP socket. In some cases, QEMU isn't able to write to xenstore its
current status, and we wait on both QMP and the xenstore path.

> swtpm does not write to
> xenstores, so that is not used.  Instead the daemon is launched first
> and then QEMU launches and connects to it.  If there is a failure,
> the QEMU log messages will list tpm in the error message.

> diff --git a/tools/libs/light/libxl_create.c b/tools/libs/light/libxl_create.c
> index a8b0c8c50070..94a2a0129f8d 100644
> --- a/tools/libs/light/libxl_create.c
> +++ b/tools/libs/light/libxl_create.c
> @@ -230,6 +230,43 @@ int libxl__domain_build_info_setdefault(libxl__gc *gc,
>      if (!b_info->event_channels)
>          b_info->event_channels = 1023;
>  
> +    if (b_info->tpm.version) {
> +        if (b_info->tpm.version != 1 && b_info->tpm.version != 2) {
> +            LOG(ERROR, "TPM version must be 1 or 2.");
> +            return ERROR_INVAL;
> +        }
> +
> +        if (b_info->tpm.statefile && !b_info->tpm.statefile[0]) {
> +            LOG(ERROR, "TPM statefile must not be empty.");

I'm not sure the check here make much sense. Or the error message lead
me to believe both statedir and statefile needed to have a value. I
guess here you mean "empty string" when it is set.

> +            return ERROR_INVAL;
> +        }
> +
> +        if (b_info->tpm.statedir && !b_info->tpm.statedir[0]) {
> +            LOG(ERROR, "TPM statedir must not be empty.");
> +            return ERROR_INVAL;
> +        }

`swtpm` seems to split the option on ',' for options. We might want to
reject path that have a comma.

> diff --git a/tools/libs/light/libxl_dm.c b/tools/libs/light/libxl_dm.c
> index 511ec76a6542..843b8e29ebd3 100644
> --- a/tools/libs/light/libxl_dm.c
> +++ b/tools/libs/light/libxl_dm.c
> @@ -1980,6 +2004,75 @@ static int libxl__write_stub_dmargs(libxl__gc *gc,
>      return rc;
>  }
>  
> +/*
> + * Build the swtpm command line.  swtpm is run in --daemon mode: the
> + * process we exec forks the actual daemon and exits once the control
> + * socket is ready, so QEMU can connect to it immediately afterwards.
> + */
> +static char **libxl__swtpm_argv(libxl__gc *gc, uint32_t domid,
> +                                const libxl_domain_config *guest_config)
> +{
> +    const libxl_domain_build_info *b_info = &guest_config->b_info;
> +    const libxl_domain_create_info *c_info = &guest_config->c_info;
> +    const char *sock = libxl__swtpm_socket_path(gc, domid);
> +    const char *pidfile = GCSPRINTF("%s/swtpm-%u.pid",
> +                                    libxl__run_dir_path(), domid);
> +    flexarray_t *args = flexarray_make(gc, 16, 1);
> +    const char *tpmstate;
> +
> +    if (b_info->tpm.statefile)
> +        tpmstate = GCSPRINTF("backend-uri=file://%s", b_info->tpm.statefile);
> +    else
> +        tpmstate = GCSPRINTF("dir=%s", b_info->tpm.statedir);
> +
> +    flexarray_vappend(args,
> +                      b_info->tpm.swtpm_override ?: "swtpm", "socket",

Can you put "socket" on a new line? So that the path to the program to
be launch is on a line on it's own.

> +                      "--tpmstate", tpmstate,
> +                      "--ctrl", GCSPRINTF("type=unixio,path=%s", sock),
> +                      NULL);
> +    if (b_info->tpm.version == 2)
> +        flexarray_append(args, "--tpm2");
> +    if (b_info->tpm.loglevel)
> +        flexarray_vappend(args, "--log",
> +            GCSPRINTF("file=%s/swtpm-%s.log,level=%d",
> +                      XEN_LOG_DIR, c_info->name, b_info->tpm.loglevel),

Could you use libxl_create_logfile()? So like for QEMU logs, the log
file will be renamed/rotated when we start the domain, and the log file
won't grow forever.

> +            NULL);
> +    if (b_info->device_model_user)
> +        flexarray_vappend(args, "--runas", b_info->device_model_user, NULL);

I don't know if just checking for device_model_user is enough. We might
use dm_restrict without a specific user, and
libxl__domain_get_device_model_uid() will find a user id in a specified
range.

> +    flexarray_vappend(args, "--daemon",
> +                      "--pid", GCSPRINTF("file=%s", pidfile), NULL);
> +    flexarray_append(args, NULL);
> +
> +    return (char **)flexarray_contents(args);
> +}
> +
> +int libxl__destroy_swtpm(libxl__gc *gc, uint32_t domid)
> +{
> +    const char *pidfile = GCSPRINTF("%s/swtpm-%u.pid",
> +                                    libxl__run_dir_path(), domid);

Could you create a function for that PID path? Like you did for the
socket.

> @@ -2875,6 +2973,69 @@ static void 
> device_model_probe_spawn_outcome(libxl__egc *egc,
>  
>  static void device_model_launch(libxl__egc *egc,

Could you rename that function? Since that step isn't about starting
QEMU anymore, but about starting swtpm.

>      libxl__dm_spawn_state *dmss, int rc)
> +{
> +    STATE_AO_GC(dmss->spawn.ao);
> +    const libxl_domain_build_info *b_info = &dmss->guest_config->b_info;
> +    const int domid = dmss->guest_domid;
> +    pid_t pid;
> +
> +    if (rc)
> +        goto out;
> +
> +    if (!b_info->tpm.version) {
> +        device_model_spawn_qemu(egc, dmss, 0);
> +        return;

Instead, could you replace that by "rc = 0; goto out;", so we keep only
one path out of the function.

> +    }
> +
> +    /* Remove any stale pidfile so teardown can't read a previous pid. */
> +    rc = libxl__remove_file(gc, GCSPRINTF("%s/swtpm-%u.pid",
> +                                          libxl__run_dir_path(), domid));
> +    if (rc)
> +        goto out;
> +
> +    pid = libxl__ev_child_fork(gc, &dmss->swtpm_child, 
> device_model_swtpm_cb);
> +    if (pid == -1) {
> +        rc = ERROR_FAIL;
> +        goto out;
> +    }
> +    if (!pid) {
> +        /* child */
> +        char **argv = libxl__swtpm_argv(gc, domid, dmss->guest_config);
> +        int null = open("/dev/null", O_RDWR);
> +        char **arg;
> +
> +        LOGD(DEBUG, domid, "Spawning swtpm %s with arguments:", argv[0]);
> +        for (arg = argv; *arg; arg++)
> +            LOGD(DEBUG, domid, "  %s", *arg);
> +
> +        setsid();
> +        libxl__exec(gc, null, null, null, argv[0], argv, NULL);
> +        /* not reached */
> +    }
> +    return;
> +
> +out:
> +    device_model_spawn_qemu(egc, dmss, rc);
> +}
> diff --git a/tools/libs/light/libxl_types.idl 
> b/tools/libs/light/libxl_types.idl
> index ce697303b64d..dc6790b82157 100644
> --- a/tools/libs/light/libxl_types.idl
> +++ b/tools/libs/light/libxl_types.idl
> @@ -584,6 +584,22 @@ libxl_xs_quota_list = Struct("xs_quota_list", [
>      ("quota", Array(libxl_xs_quota_item, "num_quota"))
>      ])
>  
> +libxl_tpm_interface = Enumeration("tpm_interface", [
> +    (0, "default"),
> +    (1, "tis"),
> +    (2, "crb"),
> +    ])
> +
> +# Emulated TPM backed by swtpm.  A version of 0 means no TPM.
> +libxl_tpm = Struct("tpm", [
> +    ("statefile", string),
> +    ("statedir",  string),
> +    ("swtpm_override", string),
> +    ("version",   integer, {'init_val': '0'}),
> +    ("loglevel",  integer, {'init_val': '0'}),

init_val=0 is the default, since we always do memset(0) at
initialisation. So those 'init_val' are redundant.

> +    ("interface", libxl_tpm_interface),
> +    ])
> +
>  libxl_domain_build_info = Struct("domain_build_info",[
>      ("max_vcpus",       integer),
>      ("avail_vcpus",     libxl_bitmap),
> diff --git a/tools/libs/light/libxl_x86.c b/tools/libs/light/libxl_x86.c
> index 9e8b48372d63..3182cfc9dfb0 100644
> --- a/tools/libs/light/libxl_x86.c
> +++ b/tools/libs/light/libxl_x86.c
> @@ -825,6 +825,24 @@ int libxl__arch_domain_build_info_setdefault(libxl__gc 
> *gc,
>      libxl_defbool_setdefault(&b_info->arch_x86.msr_relaxed, false);
>      libxl_defbool_setdefault(&b_info->trap_unmapped_accesses, false);
>  
> +    if (b_info->tpm.version && b_info->type == LIBXL_DOMAIN_TYPE_PVH) {
> +        LOG(ERROR, "TPM is not supported for PVH guests on x86.");

You don't need to say "on x86" here.

> diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
> index 0ccece3e8a18..1fed3223808e 100644
> --- a/tools/xl/xl_parse.c
> +++ b/tools/xl/xl_parse.c
> @@ -3060,6 +3060,26 @@ void parse_config_data(const char *config_source,
>          }
>      }
>  
> +    if (!xlu_cfg_get_long(config, "tpm_version", &l, 0)) {
> +        b_info->tpm.version = l;
> +
> +        xlu_cfg_replace_string(config, "tpm_statefile",
> +                               &b_info->tpm.statefile, 0);
> +        xlu_cfg_replace_string(config, "tpm_statedir",
> +                               &b_info->tpm.statedir, 0);
> +        xlu_cfg_replace_string(config, "tpm_swtpm_override",
> +                               &b_info->tpm.swtpm_override, 0);
> +        if (!xlu_cfg_get_long(config, "tpm_loglevel", &l, 0))
> +            b_info->tpm.loglevel = l;
> +
> +        if (!xlu_cfg_get_string(config, "tpm_interface", &buf, 0) &&
> +            (libxl_tpm_interface_from_string(buf, &b_info->tpm.interface) ||
> +             b_info->tpm.interface == LIBXL_TPM_INTERFACE_DEFAULT)) {
> +            fprintf(stderr, "xl: tpm_interface must be tis or crb\n");

libxl can deal with that, I think.

If libxl_tpm_interface_from_string() returns an error, we can just say
"unknown interface", that way, the error message won't need updating if
we add a new interface.

Thanks,


--
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.