[libvirt] [PATCH v3 04/11] Helper functions for host TPM support
Corey Bryant
coreyb at linux.vnet.ibm.com
Mon Apr 1 20:07:57 UTC 2013
On 03/21/2013 11:42 AM, Stefan Berger wrote:
> Signed-off-by: Stefan Berger<stefanb at linux.vnet.ibm.com>
>
> ---
> po/POTFILES.in | 1
> src/Makefile.am | 1
> src/libvirt_private.syms | 4 +
> src/util/virtpm.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++
> src/util/virtpm.h | 27 ++++++++++
> 5 files changed, 157 insertions(+)
>
> Index: libvirt/src/Makefile.am
> ===================================================================
> --- libvirt.orig/src/Makefile.am
> +++ libvirt/src/Makefile.am
> @@ -122,6 +122,7 @@ UTIL_SOURCES = \
> util/virthreadwin32.h \
> util/virthreadpool.c util/virthreadpool.h \
> util/virtime.h util/virtime.c \
> + util/virtpm.h util/virtpm.c \
> util/virtypedparam.c util/virtypedparam.h \
> util/virusb.c util/virusb.h \
> util/viruri.h util/viruri.c \
> Index: libvirt/src/util/virtpm.c
> ===================================================================
> --- /dev/null
> +++ libvirt/src/util/virtpm.c
> @@ -0,0 +1,124 @@
> +/*
> + * virtpm.c: TPM support
> + *
> + * Copyright (C) 2013 IBM Corporation
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library. If not, see
> + *<http://www.gnu.org/licenses/>.
> + *
> + * Author: Stefan Berger<stefanb at linux.vnet.ibm.com>
> + */
> +
> +#include <config.h>
> +
> +#include <stdio.h>
> +#include <dirent.h>
> +#include <unistd.h>
> +#include <sys/stat.h>
> +
> +#include "virobject.h"
> +#include "viralloc.h"
> +#include "virutil.h"
> +#include "virerror.h"
> +#include "virbuffer.h"
> +#include "virtpm.h"
> +
> +#define VIR_FROM_THIS VIR_FROM_NONE
> +
> +/*
> + * Check whether the given base path, e.g., /sys/class/misc/tpm0/device,
> + * is the sysfs entry of a TPM. A TPM sysfs entry should be uniquely
> + * recognizable by the file entries 'pcrs' and 'cancel'.
> + * Upon success 'true' is returned and the basebath buffer has '/cancel'
s/basebath/basepath
> + * appended.
> + */
> +static bool
> +virTPMCheckSysfsCancel(char *basepath, size_t bufsz)
> +{
> + char *path = NULL;
> + struct stat statbuf;
> +
> + if (virAsprintf(&path, "%s/pcrs", basepath) < 0) {
> + virReportOOMError();
> + goto error;
> + }
> + if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode))
> + goto error;
> +
> + VIR_FREE(path);
> +
> + if (virAsprintf(&path, "%s/cancel", basepath) < 0) {
> + virReportOOMError();
> + goto error;
> + }
> +
> + if (stat(path, &statbuf) == -1 || !S_ISREG(statbuf.st_mode))
> + goto error;
> +
> + if (!virStrncpy(basepath, path, strlen(path) + 1, bufsz)) {
> + virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
> + _("Basepath buffer is too small"));
> + goto error;
> + }
> +
> + VIR_FREE(path);
> +
> + return true;
> +
> +error:
> + VIR_FREE(path);
> + return false;
> +}
> +
> +
> +char *
> +virTPMFindCancelPath(void)
> +{
> + unsigned int idx;
> + int len;
> + DIR *pnp_dir;
> + char path[100], *p;
Is there any reason not to use PATH_MAX instead of 100 here?
> + struct dirent entry, *result;
> + bool found = false;
> +
> + snprintf(path, sizeof(path), "/sys/class/misc");
> + pnp_dir = opendir(path);
> + if (pnp_dir != NULL) {
> + while (readdir_r(pnp_dir, &entry, &result) == 0 &&
> + result != NULL) {
> + if (sscanf(entry.d_name, "tpm%u%n", &idx, &len) < 1 ||
> + len <= strlen("tpm") ||
> + len != strlen(entry.d_name)) {
> + continue;
> + }
> + snprintf(path, sizeof(path), "/sys/class/misc/%s/device",
> + entry.d_name);
> + if (!virTPMCheckSysfsCancel(path, sizeof(path))) {
> + continue;
> + }
> +
> + found = true;
> + break;
> + }
> + closedir(pnp_dir);
> + }
> +
> + if (found) {
> + if (!(p = strdup(path)))
> + virReportOOMError();
> + return p;
> + }
> +
> + return NULL;
> +}
> Index: libvirt/src/libvirt_private.syms
> ===================================================================
> --- libvirt.orig/src/libvirt_private.syms
> +++ libvirt/src/libvirt_private.syms
> @@ -1772,6 +1772,10 @@ virTimeStringThen;
> virTimeStringThenRaw;
>
>
> +# util/virtpm.h
> +virTPMFindCancelPath;
> +
> +
> # util/virtypedparam.h
> virTypedParameterArrayValidate;
> virTypedParameterAssign;
> Index: libvirt/src/util/virtpm.h
> ===================================================================
> --- /dev/null
> +++ libvirt/src/util/virtpm.h
> @@ -0,0 +1,27 @@
> +/*
> + * virtpm.h: TPM support
> + *
> + * Copyright (C) 2013 IBM Corporation
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with this library. If not, see
> + *<http://www.gnu.org/licenses/>.
> + *
> + * Author: Stefan Berger<stefanb at linux.vnet.ibm.com>
> + */
> +#ifndef __VIR_TPM_H__
> +# define __VIR_TPM_H__
> +
> +char *virTPMFindCancelPath(void);
> +
> +#endif /* __VIR_TPM_H__ */
> Index: libvirt/po/POTFILES.in
> ===================================================================
> --- libvirt.orig/po/POTFILES.in
> +++ libvirt/po/POTFILES.in
> @@ -181,6 +181,7 @@ src/util/virsysinfo.c
> src/util/virerror.c
> src/util/virerror.h
> src/util/virtime.c
> +src/util/virtpm.c
> src/util/virtypedparam.c
> src/util/viruri.c
> src/util/virusb.c
>
--
Regards,
Corey Bryant
More information about the libvir-list
mailing list