Skip to contentSkip to conversation

tpm: Attempt to return value not in enum

PATCH Superseded Andrew Goodbody 2 participants 6 messages Jun 27, 2025 – Jun 30, 2025 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover tpm: Attempt to return value not in enum

The function tpm2_name_to_algorithm attempts to return -EINVAL for an error but that is not included in the enum tpm2_algorithms which is defined as the return type for this function. Add -EINVAL as TPM2_ALG_INVAL to the enum so that it can be returned as desired. Update the code to use this enum value.

---
Changes in v2:
- Add patch to make use of new enum value TPM2_ALG_INVAL
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Andrew Goodbody (2):
cmd: tpm: Fix attempt to return value not in enum
tpm: Make use of TPM2_ALG_INVAL from enum

 cmd/tpm-v1.c                 | 1 +
 cmd/tpm-v2.c                 | 6 +++---
 cmd/tpm_test.c               | 1 +
 drivers/tpm/sandbox_common.c | 1 +
 include/tpm-v2.h             | 1 +
 lib/tpm-v2.c                 | 2 +-
 6 files changed, 8 insertions(+), 4 deletions(-)

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

Best regards,

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

PATCH v2 1/2 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.

---
 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,
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH v2 1/2] cmd: tpm: Fix attempt to return value not in enum

Hi Andrew

Quoted 21 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.

---
 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 +0 -0
index 6e019d1c729..25402f7543b 100644

cmd/tpm-v1.c line 11

Quoted 4 lines
cmd/tpm-v1.c +1 -0
@@ -8,6 +8,7 @@
 #include <malloc.h>
 #include <vsprintf.h>
 #include <asm/unaligned.h>
+#include <linux/errno.h>

Why is this needed? Shouldn't it only be on the header file?

Thanks
/Ilias

Quoted 43 lines
cmd/tpm-v1.c +0 -0
 #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,
 
--

2.39.5

#3 lore raw

Re: [PATCH v2 1/2] cmd: tpm: Fix attempt to return value not in enum

Quoted 22 lines · Ilias Apalodimas

Hi Andrew

Quoted 16 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.

---
  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(+)
Quoted 11 lines

cmd/tpm-v1.c line 11

Quoted 9 lines
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>

Why is this needed? Shouldn't it only be on the header file?

OK, I will rework it.

Andrew

Quoted 45 lines

Thanks
/Ilias

Quoted 43 lines
cmd/tpm-v1.c +0 -0
  #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,
 
--

2.39.5

#4 lore raw

PATCH v2 2/2 tpm: Make use of TPM2_ALG_INVAL from enum

In reply to Andrew Goodbody

Now that the enum includes TPM2_ALG_INVAL, use that name in the code.

---
 cmd/tpm-v2.c | 6 +++---
 lib/tpm-v2.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
cmd/tpm-v2.c +3 -3
index a62862e94f9..346e21d27bb 100644
@@ -113,7 +113,7 @@ static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 	if (argc == 4) {
 		algo = tpm2_name_to_algorithm(argv[3]);
-		if (algo < 0)
+		if (algo == TPM2_ALG_INVAL)
 			return CMD_RET_FAILURE;
 	}
 	algo_len = tpm2_algorithm_to_len(algo);
@@ -157,7 +157,7 @@ static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 	if (argc == 4) {
 		algo = tpm2_name_to_algorithm(argv[3]);
-		if (algo < 0)
+		if (algo == TPM2_ALG_INVAL)
 			return CMD_RET_FAILURE;
 	}
 	algo_len = tpm2_algorithm_to_len(algo);
@@ -288,7 +288,7 @@ static int do_tpm2_pcrallocate(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 
 	algo = tpm2_name_to_algorithm(argv[1]);
-	if (algo == -EINVAL)
+	if (algo == TPM2_ALG_INVAL)
 		return CMD_RET_USAGE;
 
 	ret = get_tpm(&dev);
lib/tpm-v2.c +1 -1
index 9ca7933c094..5b21c57ae42 100644
@@ -1141,7 +1141,7 @@ enum tpm2_algorithms tpm2_name_to_algorithm(const char *name)
 	}
 	printf("%s: unsupported algorithm %s\n", __func__, name);
 
-	return -EINVAL;
+	return TPM2_ALG_INVAL;
 }
 
 const char *tpm2_algorithm_name(enum tpm2_algorithms algo)
Signature · 1 line
2.39.5
Reviewed-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] tpm: Make use of TPM2_ALG_INVAL from enum

Quoted 5 lines · Andrew Goodbody

Now that the enum includes TPM2_ALG_INVAL, use that name in the code.

Quoted 53 lines
---
 cmd/tpm-v2.c | 6 +++---
 lib/tpm-v2.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
cmd/tpm-v2.c +3 -3
index a62862e94f9..346e21d27bb 100644
@@ -113,7 +113,7 @@ static int do_tpm2_pcr_extend(struct cmd_tbl *cmdtp, int flag, int argc,
                return CMD_RET_USAGE;
        if (argc == 4) {
                algo = tpm2_name_to_algorithm(argv[3]);
-               if (algo < 0)
+               if (algo == TPM2_ALG_INVAL)
                        return CMD_RET_FAILURE;
        }
        algo_len = tpm2_algorithm_to_len(algo);
@@ -157,7 +157,7 @@ static int do_tpm_pcr_read(struct cmd_tbl *cmdtp, int flag, int argc,
                return CMD_RET_USAGE;
        if (argc == 4) {
                algo = tpm2_name_to_algorithm(argv[3]);
-               if (algo < 0)
+               if (algo == TPM2_ALG_INVAL)
                        return CMD_RET_FAILURE;
        }
        algo_len = tpm2_algorithm_to_len(algo);
@@ -288,7 +288,7 @@ static int do_tpm2_pcrallocate(struct cmd_tbl *cmdtp, int flag, int argc,
                return CMD_RET_USAGE;
 
        algo = tpm2_name_to_algorithm(argv[1]);
-       if (algo == -EINVAL)
+       if (algo == TPM2_ALG_INVAL)
                return CMD_RET_USAGE;
 
        ret = get_tpm(&dev);
lib/tpm-v2.c +1 -2
index 9ca7933c094..5b21c57ae42 100644
@@ -1141,7 +1141,7 @@ enum tpm2_algorithms tpm2_name_to_algorithm(const char *name)
        }
        printf("%s: unsupported algorithm %s\n", __func__, name);
 
-       return -EINVAL;
+       return TPM2_ALG_INVAL;
 }
 
 const char *tpm2_algorithm_name(enum tpm2_algorithms algo)
 
--

2.39.5