[PATCH v4 8/8] tpm: In tpm_get_random() replace 'retries' with a zero check

Jarkko Sakkinen posted 8 patches 6 days, 1 hour ago
There is a newer version of this series
[PATCH v4 8/8] tpm: In tpm_get_random() replace 'retries' with a zero check
Posted by Jarkko Sakkinen 6 days, 1 hour ago
Check for zero byte read instead of having retries counter in order to make
wait flag properly enforcing. Progress is still guaranteed given the zero
check and iterations are capped up to TPM_MAX_RNG_DATA iterations at most
(as per theoretical limit).

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 drivers/char/tpm/tpm-interface.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 5cc2bbabd57a..594ad095a90b 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -620,7 +620,6 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait)
 {
 	u32 num_bytes = max;
 	u8 *out_ptr = out;
-	int retries = 5;
 	int total = 0;
 	int rc;
 
@@ -646,8 +645,12 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait)
 		else
 			rc = tpm1_get_random(chip, out_ptr, num_bytes);
 
-		if (rc < 0)
+		if (rc <= 0) {
+			if (!rc)
+				rc = -EIO;
+
 			goto err;
+		}
 
 		if (!wait) {
 			total = rc;
@@ -657,7 +660,7 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait)
 		out_ptr += rc;
 		total += rc;
 		num_bytes -= rc;
-	} while (retries-- && total < max);
+	} while (total < max);
 
 	tpm_put_ops(chip);
 	return total ? total : -EIO;
-- 
2.39.5