[libvirt] [PATCH] tpm: adapt sysfs cancel path for new TPM driver

Stefan Berger stefanb at linux.vnet.ibm.com
Tue Nov 17 14:18:29 UTC 2015


On 11/16/2015 10:12 PM, Stefan Berger wrote:
> Adapt the sysfs TPM command cancel path for the TPM driver that
> does not use a miscdevice anymore since Linux 4.0. Support old
> and new paths.
>
> Signed-off-by: Stefan Berger <stefanb at linux.vnet.ibm.com>
> ---
>   src/util/virtpm.c                                   | 21 +++++++++++++++++++--
>   .../qemuxml2argv-tpm-passthrough.args               |  2 +-
>   2 files changed, 20 insertions(+), 3 deletions(-)
>
> diff --git a/src/util/virtpm.c b/src/util/virtpm.c
> index 88f8361..40a758b 100644
> --- a/src/util/virtpm.c
> +++ b/src/util/virtpm.c
> @@ -23,9 +23,12 @@
>   #include <config.h>
>
>   #include <sys/stat.h>
> +#include <fcntl.h>
>
>   #include "virstring.h"
>   #include "virerror.h"
> +#include "viralloc.h"
> +#include "virfile.h"
>   #include "virtpm.h"
>
>   #define VIR_FROM_THIS VIR_FROM_NONE
> @@ -41,13 +44,27 @@ virTPMCreateCancelPath(const char *devpath)
>   {
>       char *path = NULL;
>       const char *dev;
> +    const char *prefix[] = {"misc/", "tpm/"};
> +    int i, fd;
>
>       if (devpath) {
>           dev = strrchr(devpath, '/');
>           if (dev) {
>               dev++;
> -            if (virAsprintf(&path, "/sys/class/misc/%s/device/cancel",
> -                            dev) < 0)
> +            for (i = 0; i < ARRAY_CARDINALITY(prefix); i++) {
> +                if (virAsprintf(&path, "/sys/class/%s%s/device/cancel",
> +                                prefix[i], dev) < 0)
> +                     goto cleanup;
> +
> +                fd = open(path, O_WRONLY);

To get that path right we need to test it's availability by opening it. 
Unfortunately there's  problem with that now related to the test case 
below where, if the test is run as non-root, /dev/null will be returned, 
and if run as root possibly one of those constructed paths will be 
returned. What's the best way to fix this ?

Regards,
    Stefan


> +                if (fd >= 0) {
> +                    VIR_FORCE_CLOSE(fd);
> +                    break;
> +                }
> +                VIR_FREE(path);
> +            }
> +            /* /dev/null does not allow to cancel cmds but it can be used */
> +            if (!path && virAsprintf(&path, "/dev/null") < 0)
>                   goto cleanup;
>           } else {
>               virReportError(VIR_ERR_INTERNAL_ERROR,
> diff --git a/tests/qemuxml2argvdata/qemuxml2argv-tpm-passthrough.args b/tests/qemuxml2argvdata/qemuxml2argv-tpm-passthrough.args
> index f33120f..1e48603 100644
> --- a/tests/qemuxml2argvdata/qemuxml2argv-tpm-passthrough.args
> +++ b/tests/qemuxml2argvdata/qemuxml2argv-tpm-passthrough.args
> @@ -17,6 +17,6 @@ QEMU_AUDIO_DRV=none \
>   -boot c \
>   -usb \
>   -tpmdev passthrough,id=tpm-tpm0,path=/dev/tpm0,\
> -cancel-path=/sys/class/misc/tpm0/device/cancel \
> +cancel-path=/dev/null \
>   -device tpm-tis,tpmdev=tpm-tpm0,id=tpm0 \
>   -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3




More information about the libvir-list mailing list