Skip to contentSkip to conversation

cmd: tpm: Fix attempt to return value not in enum

PATCH Superseded Andrew Goodbody 2 participants 2 messages Jun 26, 2025 – Jun 27, 2025

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cmd: tpm: Fix attempt to return value not in enum

The function tpm2_name_to_algorithm is defined as returning an enum for the algorithm specified but it also attempts to return an error on failure, but that error is not included in the enum. Add the error to the enum so that it can be returned.

This issue was reported by Smatch.

Fixes 954b95e77ef0 (tpm-v2: add support for mapping algorithm names to algos)
---
 cmd/tpm-v1.c                 | 1 +
 cmd/tpm_test.c               | 1 +
 drivers/tpm/sandbox_common.c | 1 +
 include/tpm-v2.h             | 1 +
 4 files changed, 4 insertions(+)
cmd/tpm-v1.c +1 -0
index 6e019d1c729..25402f7543b 100644
@@ -8,6 +8,7 @@
 #include <malloc.h>
 #include <vsprintf.h>
 #include <asm/unaligned.h>
+#include <linux/errno.h>
 #include <tpm-common.h>
 #include <tpm-v1.h>
 #include "tpm-user-utils.h"
cmd/tpm_test.c +1 -0
index af83d78c3fe..1d9644d5aae 100644
@@ -7,6 +7,7 @@
 #include <cpu_func.h>
 #include <log.h>
 #include <time.h>
+#include <linux/errno.h>
 #include <tpm-v1.h>
 #include <linux/printk.h>
 #include "tpm-user-utils.h"
drivers/tpm/sandbox_common.c +1 -0
index 596e0156389..4bc003a2c09 100644
@@ -7,6 +7,7 @@
 
 #define LOG_CATEGORY	UCLASS_TPM
 
+#include <linux/errno.h>
 #include <tpm-v1.h>
 #include <tpm-v2.h>
 #include <asm/unaligned.h>
include/tpm-v2.h +1 -0
index ece422df0c7..6c516dd3c9b 100644
@@ -266,6 +266,7 @@ enum tpm2_return_codes {
  * TPM2 algorithms.
  */
 enum tpm2_algorithms {
+	TPM2_ALG_INVAL		= -EINVAL,
 	TPM2_ALG_SHA1		= 0x04,
 	TPM2_ALG_XOR		= 0x0A,
 	TPM2_ALG_SHA256		= 0x0B,

---
base-commit: 903eb123236ccbd8ef05d43507a2a910b785bd56
change-id: 20250625-tpm_enum-098ee9a5eb6b

Best regards,

Signature · 1 line
Andrew Goodbody <[email protected]>
#1 lore raw

Hi Andrew,

Quoted 9 lines · Andrew Goodbody

The function tpm2_name_to_algorithm is defined as returning an enum for the algorithm specified but it also attempts to return an error on failure, but that error is not included in the enum. Add the error to the enum so that it can be returned.

This issue was reported by Smatch.

Fixes 954b95e77ef0 (tpm-v2: add support for mapping algorithm names to algos)

This doesn't really fix a bug so please drop this.

Quoted 1 line

The change itself is fine , but can you also change the callsites to return TPM2_ALG_INVAL instead of -EINVAL?

Thanks
/Ilias

Quoted 64 lines
---
 cmd/tpm-v1.c                 | 1 +
 cmd/tpm_test.c               | 1 +
 drivers/tpm/sandbox_common.c | 1 +
 include/tpm-v2.h             | 1 +
 4 files changed, 4 insertions(+)
cmd/tpm-v1.c +1 -0
index 6e019d1c729..25402f7543b 100644
@@ -8,6 +8,7 @@
 #include <malloc.h>
 #include <vsprintf.h>
 #include <asm/unaligned.h>
+#include <linux/errno.h>
 #include <tpm-common.h>
 #include <tpm-v1.h>
 #include "tpm-user-utils.h"
cmd/tpm_test.c +1 -0
index af83d78c3fe..1d9644d5aae 100644
@@ -7,6 +7,7 @@
 #include <cpu_func.h>
 #include <log.h>
 #include <time.h>
+#include <linux/errno.h>
 #include <tpm-v1.h>
 #include <linux/printk.h>
 #include "tpm-user-utils.h"
drivers/tpm/sandbox_common.c +1 -0
index 596e0156389..4bc003a2c09 100644
@@ -7,6 +7,7 @@
 
 #define LOG_CATEGORY   UCLASS_TPM
 
+#include <linux/errno.h>
 #include <tpm-v1.h>
 #include <tpm-v2.h>
 #include <asm/unaligned.h>
include/tpm-v2.h +1 -1
index ece422df0c7..6c516dd3c9b 100644
@@ -266,6 +266,7 @@ enum tpm2_return_codes {
  * TPM2 algorithms.
  */
 enum tpm2_algorithms {
+       TPM2_ALG_INVAL          = -EINVAL,
        TPM2_ALG_SHA1           = 0x04,
        TPM2_ALG_XOR            = 0x0A,
        TPM2_ALG_SHA256         = 0x0B,
 
---

base-commit: 903eb123236ccbd8ef05d43507a2a910b785bd56
change-id: 20250625-tpm_enum-098ee9a5eb6b

Best regards,
--
Andrew Goodbody <[email protected]>