All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Amarnath Valluri <amarnath.valluri@intel.com>
Cc: qemu-devel@nongnu.org, patrick.ohly@intel.com,
	stefanb@linux.vnet.ibm.com
Subject: Re: [Qemu-devel] [PATCH 7/7] tpm: New backend driver to support TPM emulator
Date: Wed, 5 Apr 2017 16:30:44 +0100	[thread overview]
Message-ID: <20170405153044.GA17970@redhat.com> (raw)
In-Reply-To: <1490965817-16913-9-git-send-email-amarnath.valluri@intel.com>

On Fri, Mar 31, 2017 at 04:10:17PM +0300, Amarnath Valluri wrote:
> +static int tpm_emulator_spawn_emulator(TPMEmulator *tpm_pt)
> +{
> +    int fds[2];
> +    int ctrl_fds[2];
> +    pid_t cpid;
> +
> +    if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds) < 0) {
> +        return -1;
> +    }
> +
> +    if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, ctrl_fds) < 0) {
> +        closesocket(fds[0]);
> +        closesocket(fds[1]);
> +        return -1;
> +    }
> +
> +    cpid = fork();

You must use  qemu_fork() to ensure signals are put into a sane state
and also to avoid signal handler races which can hit between fork & exec.

> +    if (cpid < 0) {
> +        error_report("tpm-emulator: Fork failure: %s", strerror(errno));
> +        closesocket(fds[0]); closesocket(fds[1]);
> +        closesocket(ctrl_fds[0]); closesocket(ctrl_fds[1]);
> +        return -1;
> +    }
> +
> +    if (cpid == 0) { /* CHILD */
> +        int i;
> +        char fd_str[128] = "";
> +        char ctrl_fd_str[128] = "";
> +        char tpmstate_str[1024] = "";
> +        char log_str[1024] = "";
> +        const char *params[] = {
> +            tpm_pt->ops.path, "socket",
> +            "--fd", fd_str,
> +            "--ctrl", ctrl_fd_str,
> +            "--tpmstate", tpmstate_str,
> +            "--log", log_str,
> +            NULL /* End */
> +        };
> +
> +        /* close all unused inherited sockets */
> +        closesocket(fds[0]);
> +        closesocket(ctrl_fds[0]);
> +        for (i = STDERR_FILENO + 1; i < fds[1]; i++) {
> +            closesocket(i);
> +        }

So you're assuming that fds[1] is the most highest open file descriptor
in QEMU, since you opened it a few lines earlier. Unfortunately this is not
valid, as QEMU is multi-threaded and so other threads may have opened FDs
in this time window. In addition there may be previously opened FDs with
high numbers. If you want to close all open FDs, you need to use sysconf
to query _SC_OPEN_MAX and iterate upto that value.

> +
> +        sprintf(fd_str, "%d", fds[1]);
> +        sprintf(ctrl_fd_str, "type=unixio,clientfd=%d", ctrl_fds[1]);
> +        sprintf(tpmstate_str, "dir=%s", tpm_pt->ops.tpmstatedir);

All these unchecked sprintfs into fixed buffers are bad and the directory
based ones are in fact buffer overflows if the user gives a tpmstatedir
path longer than 1020 characters. Use g_strdup_printf() to malloc strings
on the stack and free them when done.

> +        if (tpm_pt->ops.has_logfile) {
> +            sprintf(log_str, "file=%s,level=%d", tpm_pt->ops.logfile,
> +                    (int)tpm_pt->ops.loglevel);
> +        } else {
> +            /* truncate logs */
> +            params[8] = NULL;
> +        }
> +        DPRINT("Running cmd: ")
> +        for (i = 0; params[i]; i++) {
> +            DPRINT(" %s", params[i])
> +        }
> +        DPRINT("\n")
> +        if (execv(tpm_pt->ops.path, (char * const *)params) < 0) {
> +            error_report("execv() failure : %s", strerror(errno));
> +        }
> +        closesocket(fds[1]);
> +        closesocket(ctrl_fds[1]);
> +        exit(0);
> +    } else { /* self */
> +        DPRINTF("tpm-emulator: child pid: %d", cpid);
> +        /* FIXME: find better way of finding swtpm ready
> +                  maybe write 'ready'bit on socket ?
> +           give some time to child to get ready */
> +        sleep(1);
> +
> +        tpm_pt->tpm_fd = fds[0];
> +        tpm_pt->tpm_ctrl_fd = ctrl_fds[0];

Unless you switch over to using QEMU Chardevs backends as the I/O mechanism,
you should uses QIOChannelSocket for I/O and event handlers, rather than using
raw sockets directly.

ie, create one with qio_channel_socket_new_fd(fds[0]);

....

> +        tpm_pt->child_running = true;
> +
> +        qemu_add_child_watch(cpid);
> +
> +        fcntl(tpm_pt->tpm_fd, F_SETFL, O_NONBLOCK);


..then

qio_channel_set_blocking

> +        qemu_set_fd_handler(tpm_pt->tpm_fd, tpm_emulator_fd_handler, NULL,
> +                            tpm_pt);

and use qio_channel_add_watch,

etc, likewise in the places where you do actual I/O

> +
> +        /* close unsed sockets */
> +        closesocket(fds[1]);
> +        closesocket(ctrl_fds[1]);
> +    }
> +
> +    return 0;
> +}

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|

  parent reply	other threads:[~2017-04-05 15:30 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-31 13:10 [Qemu-devel] [PATCH 0/7] Provide support for the software TPM emulator Amarnath Valluri
2017-03-31 13:10 ` [Qemu-devel] [PATCH 1/7] tpm-backend: Remove unneeded member variable from backend class Amarnath Valluri
2017-04-03 17:02   ` Marc-André Lureau
2017-04-04 13:14   ` Philippe Mathieu-Daudé
2017-03-31 13:10 ` [Qemu-devel] [PATCH 2/7] tpm-backend: Move thread handling inside TPMBackend Amarnath Valluri
2017-04-04 10:56   ` Marc-André Lureau
2017-04-04 11:21     ` Amarnath Valluri
2017-03-31 13:10 ` [Qemu-devel] [PATCH 3/7] tpm-backend: Initialize and free data members in it's own methods Amarnath Valluri
2017-04-04 12:57   ` Marc-André Lureau
2017-03-31 13:10 ` [Qemu-devel] [PATCH 4/7] tpm-backend: Call interface methods only if backend implements them Amarnath Valluri
2017-04-04 13:15   ` Marc-André Lureau
2017-03-31 13:10 ` [Qemu-devel] [PATCH 5/7] tmp backend: Add new api to read backend tpm options Amarnath Valluri
2017-04-03 19:24   ` Eric Blake
2017-03-31 13:10 ` [Qemu-devel] [PATCH 6/7] tpm-passthrough: move reusable code to utils Amarnath Valluri
2017-04-04 13:53   ` Marc-André Lureau
2017-03-31 13:10 ` [Qemu-devel] [PATCH 7/7] Added support for TPM emulator Amarnath Valluri
2017-04-03 19:30   ` Eric Blake
2017-03-31 13:10 ` [Qemu-devel] [PATCH 7/7] tpm: New backend driver to support " Amarnath Valluri
2017-04-04 16:23   ` Marc-André Lureau
2017-04-05 15:30   ` Daniel P. Berrange [this message]
2017-04-02  8:33 ` [Qemu-devel] [PATCH 0/7] Provide support for the software " no-reply
2017-04-03 17:07 ` Daniel P. Berrange
2017-04-03 17:18   ` Marc-André Lureau
2017-04-04 15:43     ` Daniel P. Berrange
2017-04-04 16:27       ` Stefan Berger
2017-04-03 17:32   ` Patrick Ohly
2017-04-03 17:38     ` Dr. David Alan Gilbert
2017-04-03 19:41       ` Patrick Ohly
2017-04-04  8:02         ` Dr. David Alan Gilbert
2017-04-03 17:34   ` Dr. David Alan Gilbert
2017-04-04 12:08   ` Stefan Berger
2017-04-05  7:09   ` Amarnath Valluri
2017-04-05 15:04     ` Stefan Berger
2017-04-05 15:08       ` Marc-André Lureau
2017-04-05 17:32         ` Stefan Berger
2017-04-05 17:49           ` Marc-André Lureau
2017-04-05 18:00             ` Stefan Berger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170405153044.GA17970@redhat.com \
    --to=berrange@redhat.com \
    --cc=amarnath.valluri@intel.com \
    --cc=patrick.ohly@intel.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanb@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.