Skip to contentSkip to conversation

crypto: atmel-sha204a - Fix OTP sysfs read and error handling

PATCH In review Thorsten Blum 3 participants 3 messages Feb 16 – Feb 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 crypto: atmel-sha204a - Fix OTP sysfs read and error handling

Fix otp_show() to read and print all 64 bytes of the OTP zone. Previously, the loop only printed half of the OTP (32 bytes), and partial output was returned on read errors.

Propagate the actual error from atmel_sha204a_otp_read() instead of producing partial output.

Replace sprintf() with sysfs_emit_at(), which is preferred for formatting sysfs output because it provides safer bounds checking.

Fixes 13909a0c8897 ("crypto: atmel-sha204a - provide the otp content")

---
Compile-tested only.

Changes in v2:
- Return the total number of bytes written by sysfs_emit_at() after
feedback from Lothar (thanks!)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/crypto/atmel-sha204a.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/crypto/atmel-sha204a.c +11 -9
index 0fcf4a39de27..8af767f903ea 100644
@@ -15,6 +15,7 @@
 #include <linux/module.h>
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
+#include <linux/sysfs.h>
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
@@ -119,21 +120,22 @@ static ssize_t otp_show(struct device *dev,
 {
 	u16 addr;
 	u8 otp[OTP_ZONE_SIZE];
-	char *str = buf;
 	struct i2c_client *client = to_i2c_client(dev);
-	int i;
+	ssize_t len = 0;
+	int i, ret;
 
-	for (addr = 0; addr < OTP_ZONE_SIZE/4; addr++) {
-		if (atmel_sha204a_otp_read(client, addr, otp + addr * 4) < 0) {
+	for (addr = 0; addr < OTP_ZONE_SIZE / 4; addr++) {
+		ret = atmel_sha204a_otp_read(client, addr, otp + addr * 4);
+		if (ret < 0) {
 			dev_err(dev, "failed to read otp zone\n");
-			break;
+			return ret;
 		}
 	}
 
-	for (i = 0; i < addr*2; i++)
-		str += sprintf(str, "%02X", otp[i]);
-	str += sprintf(str, "\n");
-	return str - buf;
+	for (i = 0; i < OTP_ZONE_SIZE; i++)
+		len += sysfs_emit_at(buf, len, "%02X", otp[i]);
+	len += sysfs_emit_at(buf, len, "\n");
+	return len;
 }
 static DEVICE_ATTR_RO(otp);
 
Signature · 2 lines
Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4
Reviewed-by on v2 1/1
#1 lore raw

Hi Thorsten,

Quoted 70 lines · Thorsten Blum

Fix otp_show() to read and print all 64 bytes of the OTP zone. Previously, the loop only printed half of the OTP (32 bytes), and partial output was returned on read errors.

Propagate the actual error from atmel_sha204a_otp_read() instead of producing partial output.

Replace sprintf() with sysfs_emit_at(), which is preferred for formatting sysfs output because it provides safer bounds checking.

Fixes 13909a0c8897 ("crypto: atmel-sha204a - provide the otp content")

---
Compile-tested only.

Changes in v2:
- Return the total number of bytes written by sysfs_emit_at() after
feedback from Lothar (thanks!)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/crypto/atmel-sha204a.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
drivers/crypto/atmel-sha204a.c +11 -10
index 0fcf4a39de27..8af767f903ea 100644
@@ -15,6 +15,7 @@
 #include <linux/module.h>
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
+#include <linux/sysfs.h>
 #include <linux/workqueue.h>
 #include "atmel-i2c.h"
 
@@ -119,21 +120,22 @@ static ssize_t otp_show(struct device *dev,
 {
        u16 addr;
        u8 otp[OTP_ZONE_SIZE];
-       char *str = buf;
        struct i2c_client *client = to_i2c_client(dev);
-       int i;
+       ssize_t len = 0;
+       int i, ret;
 
-       for (addr = 0; addr < OTP_ZONE_SIZE/4; addr++) {
-               if (atmel_sha204a_otp_read(client, addr, otp + addr * 4) < 0) {
+       for (addr = 0; addr < OTP_ZONE_SIZE / 4; addr++) {
+               ret = atmel_sha204a_otp_read(client, addr, otp + addr * 4);
+               if (ret < 0) {
                        dev_err(dev, "failed to read otp zone\n");
-                       break;
+                       return ret;
                }
        }
 
-       for (i = 0; i < addr*2; i++)
-               str += sprintf(str, "%02X", otp[i]);
-       str += sprintf(str, "\n");
-       return str - buf;
+       for (i = 0; i < OTP_ZONE_SIZE; i++)
+               len += sysfs_emit_at(buf, len, "%02X", otp[i]);
+       len += sysfs_emit_at(buf, len, "\n");
+       return len;
 }
 static DEVICE_ATTR_RO(otp);
 
--
Quoted 3 lines

Thorsten Blum <[email protected]>
GPG: 1D60 735E 8AEF 3BE4 73B6 9D84 7336 78FD 8DFE EAD4

I took the patch as of link below. I verified on the before described setup.
https://lore.kernel.org/lkml/[email protected]/

worked, LGTM

Best,
L

maintainer
#2 lore raw
In reply to Thorsten Blum
Quoted 16 lines · Thorsten Blum

Fix otp_show() to read and print all 64 bytes of the OTP zone. Previously, the loop only printed half of the OTP (32 bytes), and partial output was returned on read errors.

Propagate the actual error from atmel_sha204a_otp_read() instead of producing partial output.

Replace sprintf() with sysfs_emit_at(), which is preferred for formatting sysfs output because it provides safer bounds checking.

Fixes 13909a0c8897 ("crypto: atmel-sha204a - provide the otp content")

---
Compile-tested only.

Quoted 7 lines

Changes in v2:
- Return the total number of bytes written by sysfs_emit_at() after
feedback from Lothar (thanks!)
- Link to v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/crypto/atmel-sha204a.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt