Skip to contentSkip to conversation

tpm_emulator: Report an error if chardev is missing

PATCH Superseded Stefan Berger 4 participants 4 messages Jul 24, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm_emulator: Report an error if chardev is missing

This patch fixes the odd error reporting when trying to send a file descriptor to the TPM emulator if one has not passed a valid chardev.

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Failed to send CMD_SET_DATAFD: Success
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Could not cleanly shutdown the TPM: Success

This is the new error report:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: missing chardev

This change does not hide the display of supported TPM types if a non-existent type is passed:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev nonexistent,id=tpm0
qemu-system-x86_64: -tpmdev nonexistent,id=tpm0: Parameter 'type' expects a TPM backend type
Supported TPM types (choose only one):
 passthrough   Passthrough TPM backend driver
    emulator   TPM emulator backend driver
---
 backends/tpm/tpm_emulator.c | 8 ++++++++
 1 file changed, 8 insertions(+)
backends/tpm/tpm_emulator.c +8 -0
index 9605339f93..55cbc9c60e 100644
@@ -568,6 +568,9 @@ static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
         }
 
         tpm_emu->options->chardev = g_strdup(value);
+    } else {
+        error_report("tpm-emulator: missing chardev");
+        goto err;
     }
 
     if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
@@ -925,6 +928,11 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
 {
     ptm_res res;
 
+    if (!tpm_emu->options->chardev) {
+        /* was never properly initialized */
+        return;
+    }
+
     if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
         error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
                      strerror(errno));
Signature · 1 line
2.24.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 17 lines · Stefan Berger

This patch fixes the odd error reporting when trying to send a file descriptor to the TPM emulator if one has not passed a valid chardev.

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Failed to send
CMD_SET_DATAFD: Success
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Could not
cleanly shutdown the TPM: Success

This is the new error report:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: missing chardev

This change does not hide the display of supported TPM types if a non-existent type is passed:

Quoted 9 lines
$ x86_64-softmmu/qemu-system-x86_64 -tpmdev nonexistent,id=tpm0
qemu-system-x86_64: -tpmdev nonexistent,id=tpm0: Parameter 'type' expects
a TPM backend type
Supported TPM types (choose only one):
 passthrough   Passthrough TPM backend driver
    emulator   TPM emulator backend driver

---

Quoted 38 lines
 backends/tpm/tpm_emulator.c | 8 ++++++++
 1 file changed, 8 insertions(+)
backends/tpm/tpm_emulator.c +3 -0
index 9605339f93..55cbc9c60e 100644
@@ -568,6 +568,9 @@ static int tpm_emulator_handle_device_opts(TPMEmulator
 *tpm_emu, QemuOpts *opts)
         }
 
         tpm_emu->options->chardev = g_strdup(value);
+    } else {
+        error_report("tpm-emulator: missing chardev");
+        goto err;
     }
 
     if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
backends/tpm/tpm_emulator.c +5 -0
@@ -925,6 +928,11 @@ static void tpm_emulator_shutdown(TPMEmulator
 *tpm_emu)
 {
     ptm_res res;
 
+    if (!tpm_emu->options->chardev) {
+        /* was never properly initialized */
+        return;
+    }
+
     if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res))
 < 0) {

error_report("tpm-emulator: Could not cleanly shutdown the TPM:
%s",

                      strerror(errno));

--
2.24.1

Signature · 1 line
Marc-André Lureau
Reviewed-by on v1 1/1
#2 lore raw
In reply to Stefan Berger
Quoted 11 lines · Stefan Berger

This patch fixes the odd error reporting when trying to send a file descriptor to the TPM emulator if one has not passed a valid chardev.

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Failed to send CMD_SET_DATAFD: Success
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Could not cleanly shutdown the TPM: Success

This is the new error report:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: missing chardev
                                                ~~~~~~~~~~~~~~

The "tpm-emulator: " part looks superfluous.

Hmm, many error messages in this file have it. Feel free to keep it now for consistency, and clean it up later.

We usually report missing option parameters like "parameter 'chardev' is missing". Please consider sticking to that.

Quoted 19 lines

This change does not hide the display of supported TPM types if a non-existent type is passed:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev nonexistent,id=tpm0
qemu-system-x86_64: -tpmdev nonexistent,id=tpm0: Parameter 'type' expects a TPM backend type
Supported TPM types (choose only one):
 passthrough   Passthrough TPM backend driver
    emulator   TPM emulator backend driver
---
 backends/tpm/tpm_emulator.c | 8 ++++++++
 1 file changed, 8 insertions(+)
backends/tpm/tpm_emulator.c +0 -0
index 9605339f93..55cbc9c60e 100644

backends/tpm/tpm_emulator.c static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts) · line 576

Quoted 9 lines
backends/tpm/tpm_emulator.c +3 -0
@@ -568,6 +568,9 @@ static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
         }
 
         tpm_emu->options->chardev = g_strdup(value);
+    } else {
+        error_report("tpm-emulator: missing chardev");
+        goto err;
     }
 
     if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {

This is minimally invasive.

I'd prefer

       Error *err = NULL;
       const char *value;
       Chardev *dev;

       value = qemu_opt_get(opts, "chardev");
       if (!value) {
           error_report("tpm-emulator: missing chardev");
           goto err;
       }

       dev = qemu_chr_find(value);
       if (!dev) {
           error_report("tpm-emulator: tpm chardev '%s' not found.", value);
           goto err;
       }

Your choice.

backends/tpm/tpm_emulator.c static void tpm_emulator_shutdown(TPMEmulator *tpm_emu) · line 935

Quoted 9 lines
backends/tpm/tpm_emulator.c +5 -0
@@ -925,6 +928,11 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
 {
     ptm_res res;
 
+    if (!tpm_emu->options->chardev) {
+        /* was never properly initialized */
+        return;
+    }
+

Is this still reachable? If yes, how?

If not, drop the hunk and have my

Quoted 3 lines
backends/tpm/tpm_emulator.c +0 -0
     if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
         error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
                      strerror(errno));
#3 lore raw
Quoted 15 lines · Markus Armbruster
Quoted 11 lines · Stefan Berger

This patch fixes the odd error reporting when trying to send a file descriptor to the TPM emulator if one has not passed a valid chardev.

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Failed to send CMD_SET_DATAFD: Success
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Could not cleanly shutdown the TPM: Success

This is the new error report:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0
qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: missing chardev
                                                 ~~~~~~~~~~~~~~
Quoted 7 lines

The "tpm-emulator: " part looks superfluous.

Hmm, many error messages in this file have it. Feel free to keep it now for consistency, and clean it up later.

We usually report missing option parameters like "parameter 'chardev' is missing". Please consider sticking to that.

Fixed.

Quoted 48 lines
Quoted 19 lines

This change does not hide the display of supported TPM types if a non-existent type is passed:

$ x86_64-softmmu/qemu-system-x86_64 -tpmdev nonexistent,id=tpm0
qemu-system-x86_64: -tpmdev nonexistent,id=tpm0: Parameter 'type' expects a TPM backend type
Supported TPM types (choose only one):
  passthrough   Passthrough TPM backend driver
     emulator   TPM emulator backend driver
---
  backends/tpm/tpm_emulator.c | 8 ++++++++
  1 file changed, 8 insertions(+)
backends/tpm/tpm_emulator.c +0 -0
index 9605339f93..55cbc9c60e 100644

backends/tpm/tpm_emulator.c static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts) · line 576

Quoted 9 lines
backends/tpm/tpm_emulator.c +3 -0
@@ -568,6 +568,9 @@ static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
          }
 
          tpm_emu->options->chardev = g_strdup(value);
+    } else {
+        error_report("tpm-emulator: missing chardev");
+        goto err;
      }
 
      if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {

This is minimally invasive.

I'd prefer

        Error *err = NULL;
        const char *value;
        Chardev *dev;

        value = qemu_opt_get(opts, "chardev");
        if (!value) {
            error_report("tpm-emulator: missing chardev");
            goto err;
        }

        dev = qemu_chr_find(value);
        if (!dev) {
            error_report("tpm-emulator: tpm chardev '%s' not found.", value);
            goto err;
        }
Quoted 1 line

Your choice.

Ok, changed it to this.

Quoted 11 lines

backends/tpm/tpm_emulator.c static void tpm_emulator_shutdown(TPMEmulator *tpm_emu) · line 935

Quoted 10 lines
backends/tpm/tpm_emulator.c +5 -0
@@ -925,6 +928,11 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
  {
      ptm_res res;
 
+    if (!tpm_emu->options->chardev) {
+        /* was never properly initialized */
+        return;
+    }
+

Is this still reachable? If yes, how?

tpm_emulator_inst_finalize still runs.

Sending V2. Thanks.

Quoted 7 lines

If not, drop the hunk and have my

Quoted 3 lines
backends/tpm/tpm_emulator.c +0 -0
      if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
          error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
                       strerror(errno));