Skip to contentSkip to conversation

tpm: Use EMSGSIZE instead of EBADMSG to compile on OpenBSD

PATCH In review Stefan Berger 3 participants 6 messages Oct 11, 2017 – Oct 15, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 tpm: Use EMSGSIZE instead of EBADMSG to compile on OpenBSD

EBADMSG was only added to OpenBSD very recently. To make QEMU compilable on older OpenBSD versions use EMSGSIZE instead when a mismatch between number of received bytes and message size indicated in the header was found.

Return -EMSGSIZE and convert all other errnos in the same functions to return the negative errno.

---
 hw/tpm/tpm_util.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
hw/tpm/tpm_util.c +5 -5
index fb929f6..73d7796 100644
@@ -68,10 +68,10 @@ static int tpm_util_test(int fd,
 
     n = write(fd, request, requestlen);
     if (n < 0) {
-        return errno;
+        return -errno;
     }
     if (n != requestlen) {
-        return EFAULT;
+        return -EFAULT;
     }
 
     FD_ZERO(&readfds);
@@ -80,18 +80,18 @@ static int tpm_util_test(int fd,
     /* wait for a second */
     n = select(fd + 1, &readfds, NULL, NULL, &tv);
     if (n != 1) {
-        return errno;
+        return -errno;
     }
 
     n = read(fd, &buf, sizeof(buf));
     if (n < sizeof(struct tpm_resp_hdr)) {
-        return EFAULT;
+        return -EFAULT;
     }
 
     resp = (struct tpm_resp_hdr *)buf;
     /* check the header */
     if (be32_to_cpu(resp->len) != n) {
-        return EBADMSG;
+        return -EMSGSIZE;
     }
 
     *return_tag = be16_to_cpu(resp->tag);
Signature · 1 line
2.5.5
#1 lore raw
Quoted 9 lines · Stefan Berger

EBADMSG was only added to OpenBSD very recently. To make QEMU compilable on older OpenBSD versions use EMSGSIZE instead when a mismatch between number of received bytes and message size indicated in the header was found.

Return -EMSGSIZE and convert all other errnos in the same functions to return the negative errno.

Can someone have a look at this, please?

Stefan

Quoted 43 lines
---
  hw/tpm/tpm_util.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
hw/tpm/tpm_util.c +5 -5
index fb929f6..73d7796 100644
@@ -68,10 +68,10 @@ static int tpm_util_test(int fd,
 
      n = write(fd, request, requestlen);
      if (n < 0) {
-        return errno;
+        return -errno;
      }
      if (n != requestlen) {
-        return EFAULT;
+        return -EFAULT;
      }
 
      FD_ZERO(&readfds);
@@ -80,18 +80,18 @@ static int tpm_util_test(int fd,
      /* wait for a second */
      n = select(fd + 1, &readfds, NULL, NULL, &tv);
      if (n != 1) {
-        return errno;
+        return -errno;
      }
 
      n = read(fd, &buf, sizeof(buf));
      if (n < sizeof(struct tpm_resp_hdr)) {
-        return EFAULT;
+        return -EFAULT;
      }
 
      resp = (struct tpm_resp_hdr *)buf;
      /* check the header */
      if (be32_to_cpu(resp->len) != n) {
-        return EBADMSG;
+        return -EMSGSIZE;
      }
 
      *return_tag = be16_to_cpu(resp->tag);
Reviewed-by on v3 1/1
#2 lore raw
In reply to Stefan Berger

Hi

Quoted 9 lines · Stefan Berger

EBADMSG was only added to OpenBSD very recently. To make QEMU compilable on older OpenBSD versions use EMSGSIZE instead when a mismatch between number of received bytes and message size indicated in the header was found.

Return -EMSGSIZE and convert all other errnos in the same functions to return the negative errno.

Looks good to me,
but given that the return value isn't used, perhaps you could have changed the function to return a bool success instead?

Quoted 46 lines
---
 hw/tpm/tpm_util.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
hw/tpm/tpm_util.c +5 -6
index fb929f6..73d7796 100644
@@ -68,10 +68,10 @@ static int tpm_util_test(int fd,
 
     n = write(fd, request, requestlen);
     if (n < 0) {
-        return errno;
+        return -errno;
     }
     if (n != requestlen) {
-        return EFAULT;
+        return -EFAULT;
     }
 
     FD_ZERO(&readfds);
@@ -80,18 +80,18 @@ static int tpm_util_test(int fd,
     /* wait for a second */
     n = select(fd + 1, &readfds, NULL, NULL, &tv);
     if (n != 1) {
-        return errno;
+        return -errno;
     }
 
     n = read(fd, &buf, sizeof(buf));
     if (n < sizeof(struct tpm_resp_hdr)) {
-        return EFAULT;
+        return -EFAULT;
     }
 
     resp = (struct tpm_resp_hdr *)buf;
     /* check the header */
     if (be32_to_cpu(resp->len) != n) {
-        return EBADMSG;
+        return -EMSGSIZE;
     }
 
     *return_tag = be16_to_cpu(resp->tag);
--

2.5.5

Signature · 1 line
Marc-André Lureau
#3 lore raw
Quoted 12 lines · Marc-André Lureau

Hi

Quoted 8 lines · Stefan Berger

EBADMSG was only added to OpenBSD very recently. To make QEMU compilable on older OpenBSD versions use EMSGSIZE instead when a mismatch between number of received bytes and message size indicated in the header was found.

Return -EMSGSIZE and convert all other errnos in the same functions to return the negative errno.

Quoted 8 lines
Quoted 1 line

Looks good to me,
but given that the return value isn't used, perhaps you could have changed the function to return a bool success instead?

AIUI this is just test code, so we probably don't gain too much from extensively bikeshedding it. (Having said that, I can't resist asking why it's not in tests/ :-))

thanks
-- PMM

#4 lore raw
Quoted 18 lines · Peter Maydell
Quoted 16 lines · Marc-André Lureau

Hi

Quoted 9 lines · Stefan Berger

EBADMSG was only added to OpenBSD very recently. To make QEMU compilable on older OpenBSD versions use EMSGSIZE instead when a mismatch between number of received bytes and message size indicated in the header was found.

Return -EMSGSIZE and convert all other errnos in the same functions to return the negative errno.

Quoted 6 lines
Quoted 3 lines

Looks good to me,
but given that the return value isn't used, perhaps you could have changed the function to return a bool success instead?

AIUI this is just test code, so we probably don't gain too much from extensively bikeshedding it. (Having said that, I can't resist asking why it's not in tests/ :-))

This is NOT test code. It's determining whether the external TPM is a TPM 1.2 or TPM 2 emulation. This avoids having to start QEMU with a '--tpm2' parmeter equivalent as well as the external emulator. Passing this to the external emulator is enough.

Stefan

Quoted 4 lines

thanks
-- PMM

#5 lore raw
Quoted 4 lines · Stefan Berger

This is NOT test code. It's determining whether the external TPM is a TPM 1.2 or TPM 2 emulation. This avoids having to start QEMU with a '--tpm2' parmeter equivalent as well as the external emulator. Passing this to the external emulator is enough.

Sorry -- I was misled by the way the function is named "tpm_util_test()" and the comment in front of it says "A basic test of a TPM device".

thanks
-- PMM