Skip to contentSkip to conversation

nvmem: Fix Critical and High issues reported by Sashiko

PATCH New Lee Jones 2 participants 12 messages Dec 3

Subsystems (5)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

cover nvmem: Fix Critical and High issues reported by Sashiko

This series fixes 10 Critical and High issues across the NVMEM core and drivers (Meson eFuse, Spreadtrum eFuse, 93xx46 EEPROM, and STM32 BSEC OP-TEE TA), covering out-of-bounds reads, integer underflow, multi-byte bit-offset corruption, WP GPIO and erase/write locking races, stack DMA buffers, unchecked SMC sizes/short transfers, and an OP-TEE session leak.

These issues were reported by Sashiko.

Cc Alexandre Torgue <[email protected]>
Cc Anatolij Gustschin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Baolin Wang <[email protected]>
Cc Bartosz Golaszewski <[email protected]>
Cc Carlo Caione <[email protected]>
Cc Chunyan Zhang <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Dmitry Baryshkov <[email protected]>
Cc Etienne Carriere <[email protected]>
Cc Freeman Liu <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Jerome Brunet <[email protected]>
Cc Joy Chakraborty <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc Khouloud Touil <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Martin Blumenstingl <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Neil Armstrong <[email protected]>
Cc Orson Zhai <[email protected]>
Cc Patrick Delaunay <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Srinivas Kandagatla <[email protected]>

Lee Jones (10):
nvmem: meson-efuse: Reject negative or zero SMC size in
meson_efuse_probe()
nvmem: core: Prevent integer underflow in nvmem_cell_attr_read()
nvmem: sprd: Avoid out-of-bounds read on partial writes in
sprd_efuse_raw_prog()
nvmem: core: Handle multi-byte bit offsets in
nvmem_cell_prepare_write_buffer()
nvmem: core: Serialise writes when toggling the write-protect GPIO
nvmem: eeprom_93xx46: Reject unaligned writes on 16-bit devices
nvmem: eeprom_93xx46: Hold device lock across erase/write enable
sequences
nvmem: eeprom_93xx46: Use DMA-safe command buffer for SPI transfers
nvmem: meson-efuse: Detect short reads and writes from Secure Monitor
nvmem: stm32: Avoid OP-TEE session leak on unaligned writes

 drivers/nvmem/core.c                | 50 ++++++++++++-----
 drivers/nvmem/eeprom_93xx46.c       | 85 +++++++++++++++--------------
 drivers/nvmem/internals.h           |  2 +
 drivers/nvmem/meson-efuse.c         | 17 ++++--
 drivers/nvmem/sprd-efuse.c          | 13 ++++-
 drivers/nvmem/stm32-bsec-optee-ta.c |  8 +--
 6 files changed, 111 insertions(+), 64 deletions(-)
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#1 lore raw

PATCH 1/10 nvmem: meson-efuse: Reject negative or zero SMC size in meson_efuse_probe()

meson_sm_call() writes the signed Secure Monitor return value into its s32 output pointer and returns 0 whenever the command ID is valid. meson_efuse_probe() passes an unsigned int pointer to meson_sm_call() and only checks for a negative return value from meson_sm_call() itself. If firmware returns a negative SMC error code (such as SMCCC_RET_NOT_SUPPORTED), size wraps to a large unsigned value and is assigned to econfig->size.

Change size to s32 and reject negative or zero values from SM_EFUSE_USER_MAX with -EINVAL.

Fixes ad855eae6caf ("nvmem: amlogic: Add Amlogic Meson EFUSE driver")

---

Cc Carlo Caione <[email protected]>
Cc Jerome Brunet <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc Martin Blumenstingl <[email protected]>
Cc Neil Armstrong <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/meson-efuse.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/nvmem/meson-efuse.c +3 -2
index d7f9ac99a212..e8f384098be5 100644
@@ -51,7 +51,7 @@ static int meson_efuse_probe(struct platform_device *pdev)
 	struct nvmem_device *nvmem;
 	struct nvmem_config *econfig;
 	struct clk *clk;
-	unsigned int size;
+	s32 size;
 	struct device_node *sm_np __free(device_node) =
 			of_parse_phandle(pdev->dev.of_node, "secure-monitor", 0);
 
@@ -68,7 +68,8 @@ static int meson_efuse_probe(struct platform_device *pdev)
 	if (IS_ERR(clk))
 		return dev_err_probe(dev, PTR_ERR(clk), "failed to get efuse gate");
 
-	if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0) {
+	if (meson_sm_call(fw, SM_EFUSE_USER_MAX, &size, 0, 0, 0, 0, 0) < 0 ||
+	    size <= 0) {
 		dev_err(dev, "failed to get max user");
 		return -EINVAL;
 	}
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#2 lore raw

PATCH 2/10 nvmem: core: Prevent integer underflow in nvmem_cell_attr_read()

In reply to Lee Jones

sysfs_kf_bin_read() skips its pos >= size check when the binary attribute has i_size == 0. For a zero-length NVMEM cell, a userspace read with a non-zero offset therefore reaches nvmem_cell_attr_read(), where cell_sz - pos underflows as unsigned int, causing memcpy() to dereference ZERO_SIZE_PTR + pos.

Check whether pos is negative or at least cell_sz before computing read_len and copying into buf, returning 0 bytes when out of range.

Fixes 0331c611949f ("nvmem: core: Expose cells through sysfs")

---

Cc Greg Kroah-Hartman <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/core.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/nvmem/core.c +6 -2
index cf263633a4a2..83ed6d4e3c5f 100644
@@ -384,8 +384,12 @@ static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
 		goto destroy_cell;
 	}
 
-	read_len = min_t(unsigned int, cell_sz - pos, count);
-	memcpy(buf, content + pos, read_len);
+	if (pos < 0 || pos >= cell_sz) {
+		read_len = 0;
+	} else {
+		read_len = min_t(size_t, cell_sz - pos, count);
+		memcpy(buf, content + pos, read_len);
+	}
 	kfree(content);
 
 destroy_cell:
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#3 lore raw

PATCH 3/10 nvmem: sprd: Avoid out-of-bounds read on partial writes in sprd_efuse_raw_prog()

In reply to Lee Jones

sprd_efuse_write() allows partial-block writes smaller than SPRD_EFUSE_BLOCK_WIDTH (4 bytes) because the device registers with word_size and stride set to 1. Passing val directly to sprd_efuse_raw_prog() as u32 * and dereferencing *data performs a 4-byte out-of-bounds heap read past short buffers and burns slab contents into hardware eFuses, while writes larger than 4 bytes are silently truncated.

Validate that bytes is in [1, SPRD_EFUSE_BLOCK_WIDTH], copy bytes into a zero-initialised __le32 local variable, and pass the converted u32 value to sprd_efuse_raw_prog() by value.

Fixes 096030e7f449 ("nvmem: sprd: Add Spreadtrum SoCs eFuse support")

---

Cc Baolin Wang <[email protected]>
Cc Chunyan Zhang <[email protected]>
Cc Freeman Liu <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Orson Zhai <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/sprd-efuse.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/nvmem/sprd-efuse.c +10 -3
index 82995f8bce47..6b764c510d94 100644
@@ -193,7 +193,7 @@ static void sprd_efuse_set_prog_en(struct sprd_efuse *efuse, bool en)
 }
 
 static int sprd_efuse_raw_prog(struct sprd_efuse *efuse, u32 blk, bool doub,
-			       bool lock, u32 *data)
+			       bool lock, u32 data)
 {
 	u32 status;
 	int ret = 0;
@@ -221,7 +221,7 @@ static int sprd_efuse_raw_prog(struct sprd_efuse *efuse, u32 blk, bool doub,
 	if (lock)
 		sprd_efuse_set_auto_check(efuse, true);
 
-	writel(*data, efuse->base + SPRD_EFUSE_MEM(blk));
+	writel(data, efuse->base + SPRD_EFUSE_MEM(blk));
 
 	/* Disable auto-check and data double after programming */
 	if (lock)
@@ -336,9 +336,15 @@ static int sprd_efuse_write(void *context, u32 offset, void *val, size_t bytes)
 {
 	struct sprd_efuse *efuse = context;
 	bool blk_double = efuse->data->blk_double;
+	__le32 data = 0;
 	bool lock;
 	int ret;
 
+	if (!bytes || bytes > SPRD_EFUSE_BLOCK_WIDTH)
+		return -EINVAL;
+
+	memcpy(&data, val, bytes);
+
 	ret = sprd_efuse_lock(efuse);
 	if (ret)
 		return ret;
@@ -360,7 +366,8 @@ static int sprd_efuse_write(void *context, u32 offset, void *val, size_t bytes)
 	else
 		lock = true;
 
-	ret = sprd_efuse_raw_prog(efuse, offset, blk_double, lock, val);
+	ret = sprd_efuse_raw_prog(efuse, offset, blk_double, lock,
+				  le32_to_cpu(data));
 
 	clk_disable_unprepare(efuse->clk);
 
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#4 lore raw

PATCH 4/10 nvmem: core: Handle multi-byte bit offsets in nvmem_cell_prepare_write_buffer()

In reply to Lee Jones

nvmem_cell_prepare_write_buffer() assumes cell->bit_offset is less than BITS_PER_BYTE. When cell->bit_offset is 8 or greater, shifting by bit_offset and passing negative shift counts to GENMASK() or right shifts triggers undefined behaviour, leaves leading bytes overwritten with zero, and places the payload at byte 0 instead of bit_offset / BITS_PER_BYTE. In addition, the carry shift used BITS_PER_BYTE - 1 - bit_offset instead of BITS_PER_BYTE - bit_offset, and the trailing-byte pointer p was left at byte 0 when bit_offset was zero.

Split cell->bit_offset into bytes_offset and bit_offset modulo BITS_PER_BYTE, reject bytes_offset >= cell->bytes, preserve existing leading bytes via an aligned read from cell->offset, bound the payload copy to cell->bytes - bytes_offset, fix the carry shift and trailing byte pointer, and mask the trailing byte before preserving its upper bits.

Fixes 7a06ef751077 ("nvmem: core: fix bit offsets of more than one byte")

---

Cc Dmitry Baryshkov <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/core.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
drivers/nvmem/core.c +24 -11
index 83ed6d4e3c5f..e7c687cb6ce6 100644
@@ -1765,33 +1765,46 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 					     u8 *_buf, int len)
 {
 	struct nvmem_device *nvmem = cell->nvmem;
-	int i, rc, nbits, bit_offset = cell->bit_offset;
-	u8 v, *p, *buf, *b, pbyte, pbits;
+	int i, rc, nbits, bytes_offset, bit_offset;
+	u8 v = 0, *p, *buf, *b, pbyte, pbits;
+	size_t copy_len, lead_bytes;
 
 	nbits = cell->nbits;
+	bytes_offset = cell->bit_offset / BITS_PER_BYTE;
+	bit_offset = cell->bit_offset % BITS_PER_BYTE;
+	if (bytes_offset >= cell->bytes)
+		return ERR_PTR(-EINVAL);
+
 	buf = kzalloc(cell->bytes, GFP_KERNEL);
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	memcpy(buf, _buf, len);
-	p = b = buf;
+	lead_bytes = bytes_offset + (bit_offset ? 1 : 0);
+	if (lead_bytes) {
+		rc = nvmem_access_locked(nvmem, ops, cell->offset, buf,
+					 lead_bytes, false);
+		if (rc)
+			goto err;
+		v = buf[bytes_offset];
+	}
+
+	copy_len = min_t(size_t, len, cell->bytes - bytes_offset);
+	memcpy(buf + bytes_offset, _buf, copy_len);
+	b = buf + bytes_offset;
+	p = buf + cell->bytes - 1;
 
 	if (bit_offset) {
 		pbyte = *b;
 		*b <<= bit_offset;
 
 		/* setup the first byte with lsb bits from nvmem */
-		rc = nvmem_access_locked(nvmem, ops, cell->offset, &v, 1, false);
-		if (rc)
-			goto err;
 		*b++ |= GENMASK(bit_offset - 1, 0) & v;
 
 		/* setup rest of the byte if any */
-		for (i = 1; i < cell->bytes; i++) {
+		for (i = bytes_offset + 1; i < cell->bytes; i++) {
 			/* Get last byte bits and shift them towards lsb */
-			pbits = pbyte >> (BITS_PER_BYTE - 1 - bit_offset);
+			pbits = pbyte >> (BITS_PER_BYTE - bit_offset);
 			pbyte = *b;
-			p = b;
 			*b <<= bit_offset;
 			*b++ |= pbits;
 		}
@@ -1804,8 +1817,8 @@ static void *nvmem_cell_prepare_write_buffer(struct nvmem_cell_entry *cell,
 					 &v, 1, false);
 		if (rc)
 			goto err;
+		*p &= GENMASK(((nbits + bit_offset) % BITS_PER_BYTE) - 1, 0);
 		*p |= GENMASK(7, (nbits + bit_offset) % BITS_PER_BYTE) & v;
-
 	}
 
 	return buf;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#5 lore raw

PATCH 5/10 nvmem: core: Serialise writes when toggling the write-protect GPIO

In reply to Lee Jones

nvmem_access_chunk() deasserts nvmem->wp_gpio, calls ops->reg_write(), and re-asserts nvmem->wp_gpio while callers hold only the shared SRCU read lock. Concurrent writers can therefore overlap, allowing one writer to re-assert write protection while another writer is still executing ops->reg_write().

Add a per-device wp_lock mutex to struct nvmem_device and hold it across the WP GPIO deassert, ops->reg_write(), and WP GPIO re-assert sequence whenever nvmem->wp_gpio is present.

Fixes 2a127da461a9 ("nvmem: add support for the write-protect pin")

---

Cc Bartosz Golaszewski <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Khouloud Touil <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Maxime Ripard <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/core.c      | 7 +++++++
 drivers/nvmem/internals.h | 2 ++
 2 files changed, 9 insertions(+)
drivers/nvmem/core.c +7 -0
index e7c687cb6ce6..2ba1ab037907 100644
@@ -70,6 +70,11 @@ static int nvmem_access_chunk(struct nvmem_device *nvmem,
 	if (!ops->reg_write)
 		return -EOPNOTSUPP;
 
+	if (!nvmem->wp_gpio)
+		return ops->reg_write(nvmem->priv, offset, val, bytes);
+
+	guard(mutex)(&nvmem->wp_lock);
+
 	ret = gpiod_set_value_cansleep(nvmem->wp_gpio, 0);
 	if (ret)
 		return ret;
@@ -574,6 +579,7 @@ static void nvmem_release(struct device *dev)
 	nvmem_device_remove_all_cells(nvmem);
 	ida_free(&nvmem_ida, nvmem->id);
 	of_node_put(nvmem->dev.of_node);
+	mutex_destroy(&nvmem->wp_lock);
 	cleanup_srcu_struct(&nvmem->srcu);
 	kfree(nvmem);
 }
@@ -983,6 +989,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
 		return ERR_PTR(rval);
 	}
 
+	mutex_init(&nvmem->wp_lock);
 	rcu_assign_pointer(nvmem->ops, ops);
 
 	device_initialize(&nvmem->dev);
drivers/nvmem/internals.h +2 -0
index bc7a99f5aefb..3d59ac799170 100644
@@ -4,6 +4,7 @@
 #define _LINUX_NVMEM_INTERNALS_H
 
 #include <linux/device.h>
+#include <linux/mutex.h>
 #include <linux/nvmem-consumer.h>
 #include <linux/nvmem-provider.h>
 #include <linux/srcu.h>
@@ -34,6 +35,7 @@ struct nvmem_device {
 	const struct nvmem_keepout *keepout;
 	unsigned int		nkeepout;
 	struct gpio_desc	*wp_gpio;
+	struct mutex		wp_lock;	/* serialises WP GPIO writes */
 	struct nvmem_layout	*layout;
 	struct nvmem_operations __rcu *ops;
 	void *priv;
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#6 lore raw

PATCH 6/10 nvmem: eeprom_93xx46: Reject unaligned writes on 16-bit devices

In reply to Lee Jones

eeprom_93xx46_probe() hardcodes nvmem_config.word_size to 1 even for 16-bit (EE_ADDR16) devices, while eeprom_93xx46_write() silently masks off the low bit of count and returns 0 on success. A 1-byte write therefore silently writes nothing and reports success, and an odd offset is shifted down by eeprom_93xx46_write_word(), overwriting the adjacent byte.

Set nvmem_config.word_size to 2 for EE_ADDR16 devices and reject odd offsets or byte counts in eeprom_93xx46_write() with -EINVAL.

Fixes a8ab316ab125 ("nvmem: 93xx46: remove nvmem regmap dependency")

---

Cc Anatolij Gustschin <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/eeprom_93xx46.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/nvmem/eeprom_93xx46.c +4 -3
index f9c3ab52c2f9..6b159d5fa08e 100644
@@ -282,10 +282,11 @@ static int eeprom_93xx46_write(void *priv, unsigned int off,
 	if (unlikely(!count))
 		return count;
 
-	/* only write even number of bytes on 16-bit devices */
+	/* only write even offsets and counts on 16-bit devices */
 	if (edev->pdata->flags & EE_ADDR16) {
+		if ((off & 1) || (count & 1))
+			return -EINVAL;
 		step = 2;
-		count &= ~1;
 	}
 
 	/* erase/write enable */
@@ -513,7 +514,7 @@ static int eeprom_93xx46_probe(struct spi_device *spi)
 	edev->nvmem_config.reg_write = eeprom_93xx46_write;
 	edev->nvmem_config.priv = edev;
 	edev->nvmem_config.stride = 4;
-	edev->nvmem_config.word_size = 1;
+	edev->nvmem_config.word_size = (pd->flags & EE_ADDR16) ? 2 : 1;
 	edev->nvmem_config.size = edev->size;
 
 	edev->nvmem = devm_nvmem_register(&spi->dev, &edev->nvmem_config);
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#7 lore raw

PATCH 7/10 nvmem: eeprom_93xx46: Hold device lock across erase/write enable sequences

In reply to Lee Jones

eeprom_93xx46_ew() and eeprom_93xx46_eral() each acquire and release edev->lock internally, leaving edev->lock unheld between enabling erase/write mode, issuing the write or erase commands, and disabling erase/write mode again. A concurrent writer or eraser can therefore send EWDS before the first caller writes, causing the EEPROM to ignore writes silently while returning 0. In addition, erase_store() returned early on an erase error without sending EWDS.

Move locking to the callers (eeprom_93xx46_write() and erase_store()) so edev->lock is held across the entire EWEN, write/erase, and EWDS sequence, assert the lock in eeprom_93xx46_ew() and eeprom_93xx46_eral(), and always send EWDS after eeprom_93xx46_eral() in erase_store().

Fixes 06b4501e88ad ("misc/eeprom: add driver for microwire 93xx46 EEPROMs")

---

Cc Anatolij Gustschin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/eeprom_93xx46.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)
drivers/nvmem/eeprom_93xx46.c +13 -11
index 6b159d5fa08e..35966552ddbd 100644
@@ -7,10 +7,12 @@
 
 #include <linux/array_size.h>
 #include <linux/bits.h>
+#include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/gpio/consumer.h>
 #include <linux/kstrtox.h>
+#include <linux/lockdep.h>
 #include <linux/log2.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
@@ -207,7 +209,7 @@ static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
 
 	spi_message_init_with_transfers(&m, &t, 1);
 
-	mutex_lock(&edev->lock);
+	lockdep_assert_held(&edev->lock);
 
 	gpiod_set_value_cansleep(edev->pdata->select, 1);
 
@@ -220,7 +222,6 @@ static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
 
 	gpiod_set_value_cansleep(edev->pdata->select, 0);
 
-	mutex_unlock(&edev->lock);
 	return ret;
 }
 
@@ -289,13 +290,13 @@ static int eeprom_93xx46_write(void *priv, unsigned int off,
 		step = 2;
 	}
 
+	guard(mutex)(&edev->lock);
+
 	/* erase/write enable */
 	ret = eeprom_93xx46_ew(edev, 1);
 	if (ret)
 		return ret;
 
-	mutex_lock(&edev->lock);
-
 	gpiod_set_value_cansleep(edev->pdata->select, 1);
 
 	for (i = 0; i < count; i += step) {
@@ -308,8 +309,6 @@ static int eeprom_93xx46_write(void *priv, unsigned int off,
 
 	gpiod_set_value_cansleep(edev->pdata->select, 0);
 
-	mutex_unlock(&edev->lock);
-
 	/* erase/write disable */
 	eeprom_93xx46_ew(edev, 0);
 	return ret;
@@ -341,7 +340,7 @@ static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
 
 	spi_message_init_with_transfers(&m, &t, 1);
 
-	mutex_lock(&edev->lock);
+	lockdep_assert_held(&edev->lock);
 
 	gpiod_set_value_cansleep(edev->pdata->select, 1);
 
@@ -353,7 +352,6 @@ static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
 
 	gpiod_set_value_cansleep(edev->pdata->select, 0);
 
-	mutex_unlock(&edev->lock);
 	return ret;
 }
 
@@ -369,15 +367,19 @@ static ssize_t erase_store(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	if (erase) {
+		int ewds_ret;
+
+		guard(mutex)(&edev->lock);
+
 		ret = eeprom_93xx46_ew(edev, 1);
 		if (ret)
 			return ret;
 		ret = eeprom_93xx46_eral(edev);
+		ewds_ret = eeprom_93xx46_ew(edev, 0);
 		if (ret)
 			return ret;
-		ret = eeprom_93xx46_ew(edev, 0);
-		if (ret)
-			return ret;
+		if (ewds_ret)
+			return ewds_ret;
 	}
 	return count;
 }
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#8 lore raw

PATCH 8/10 nvmem: eeprom_93xx46: Use DMA-safe command buffer for SPI transfers

In reply to Lee Jones

eeprom_93xx46_read(), eeprom_93xx46_ew(), eeprom_93xx46_write_word(), and eeprom_93xx46_eral() all pass a stack-allocated u16 cmd_addr as tx_buf to spi_sync(). Stack memory is not DMA-safe under CONFIG_VMAP_STACK or DMA-capable SPI controllers.

Move cmd_addr into the heap-allocated struct eeprom_93xx46_dev with cacheline alignment, serialised under edev->lock across all four SPI operations.

Fixes 06b4501e88ad ("misc/eeprom: add driver for microwire 93xx46 EEPROMs")

---

Cc Anatolij Gustschin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Randy Dunlap <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/eeprom_93xx46.c | 58 ++++++++++++++++++-----------------
 1 file changed, 30 insertions(+), 28 deletions(-)
drivers/nvmem/eeprom_93xx46.c +30 -28
index 35966552ddbd..e191ac2e3745 100644
@@ -7,6 +7,7 @@
 
 #include <linux/array_size.h>
 #include <linux/bits.h>
+#include <linux/cache.h>
 #include <linux/cleanup.h>
 #include <linux/delay.h>
 #include <linux/device.h>
@@ -87,6 +88,7 @@ struct eeprom_93xx46_dev {
 	struct nvmem_device *nvmem;
 	int addrlen;
 	int size;
+	u16 cmd_addr ____cacheline_aligned;
 };
 
 static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev)
@@ -129,28 +131,29 @@ static int eeprom_93xx46_read(void *priv, unsigned int off,
 	while (count) {
 		struct spi_message m;
 		struct spi_transfer t[2] = {};
-		u16 cmd_addr = OP_READ << edev->addrlen;
 		size_t nbytes = count;
 
+		edev->cmd_addr = OP_READ << edev->addrlen;
+
 		if (edev->pdata->flags & EE_ADDR8) {
-			cmd_addr |= off;
+			edev->cmd_addr |= off;
 			if (has_quirk_single_word_read(edev))
 				nbytes = 1;
 		} else {
-			cmd_addr |= (off >> 1);
+			edev->cmd_addr |= (off >> 1);
 			if (has_quirk_single_word_read(edev))
 				nbytes = 2;
 		}
 
 		dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
-			cmd_addr, edev->spi->max_speed_hz);
+			edev->cmd_addr, edev->spi->max_speed_hz);
 
 		if (has_quirk_extra_read_cycle(edev)) {
-			cmd_addr <<= 1;
+			edev->cmd_addr <<= 1;
 			bits += 1;
 		}
 
-		t[0].tx_buf = (char *)&cmd_addr;
+		t[0].tx_buf = &edev->cmd_addr;
 		t[0].len = 2;
 		t[0].bits_per_word = bits;
 
@@ -187,30 +190,29 @@ static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
 	struct spi_message m;
 	struct spi_transfer t = {};
 	int bits, ret;
-	u16 cmd_addr;
+
+	lockdep_assert_held(&edev->lock);
 
 	/* The opcode in front of the address is three bits. */
 	bits = edev->addrlen + 3;
 
-	cmd_addr = OP_START << edev->addrlen;
-	cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << (edev->addrlen - 6);
+	edev->cmd_addr = OP_START << edev->addrlen;
+	edev->cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << (edev->addrlen - 6);
 
 	if (has_quirk_instruction_length(edev)) {
-		cmd_addr <<= 2;
+		edev->cmd_addr <<= 2;
 		bits += 2;
 	}
 
 	dev_dbg(&edev->spi->dev, "ew %s cmd 0x%04x, %d bits\n",
-		str_enable_disable(is_on), cmd_addr, bits);
+		str_enable_disable(is_on), edev->cmd_addr, bits);
 
-	t.tx_buf = &cmd_addr;
+	t.tx_buf = &edev->cmd_addr;
 	t.len = 2;
 	t.bits_per_word = bits;
 
 	spi_message_init_with_transfers(&m, &t, 1);
 
-	lockdep_assert_held(&edev->lock);
-
 	gpiod_set_value_cansleep(edev->pdata->select, 1);
 
 	ret = spi_sync(edev->spi, &m);
@@ -232,7 +234,8 @@ eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
 	struct spi_message m;
 	struct spi_transfer t[2] = {};
 	int bits, data_len, ret;
-	u16 cmd_addr;
+
+	lockdep_assert_held(&edev->lock);
 
 	if (unlikely(off >= edev->size))
 		return -EINVAL;
@@ -240,19 +243,19 @@ eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
 	/* The opcode in front of the address is three bits. */
 	bits = edev->addrlen + 3;
 
-	cmd_addr = OP_WRITE << edev->addrlen;
+	edev->cmd_addr = OP_WRITE << edev->addrlen;
 
 	if (edev->pdata->flags & EE_ADDR8) {
-		cmd_addr |= off;
+		edev->cmd_addr |= off;
 		data_len = 1;
 	} else {
-		cmd_addr |= (off >> 1);
+		edev->cmd_addr |= (off >> 1);
 		data_len = 2;
 	}
 
-	dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
+	dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", edev->cmd_addr);
 
-	t[0].tx_buf = (char *)&cmd_addr;
+	t[0].tx_buf = &edev->cmd_addr;
 	t[0].len = 2;
 	t[0].bits_per_word = bits;
 
@@ -319,29 +322,28 @@ static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
 	struct spi_message m;
 	struct spi_transfer t = {};
 	int bits, ret;
-	u16 cmd_addr;
+
+	lockdep_assert_held(&edev->lock);
 
 	/* The opcode in front of the address is three bits. */
 	bits = edev->addrlen + 3;
 
-	cmd_addr = OP_START << edev->addrlen;
-	cmd_addr |= ADDR_ERAL << (edev->addrlen - 6);
+	edev->cmd_addr = OP_START << edev->addrlen;
+	edev->cmd_addr |= ADDR_ERAL << (edev->addrlen - 6);
 
 	if (has_quirk_instruction_length(edev)) {
-		cmd_addr <<= 2;
+		edev->cmd_addr <<= 2;
 		bits += 2;
 	}
 
-	dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits);
+	dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", edev->cmd_addr, bits);
 
-	t.tx_buf = &cmd_addr;
+	t.tx_buf = &edev->cmd_addr;
 	t.len = 2;
 	t.bits_per_word = bits;
 
 	spi_message_init_with_transfers(&m, &t, 1);
 
-	lockdep_assert_held(&edev->lock);
-
 	gpiod_set_value_cansleep(edev->pdata->select, 1);
 
 	ret = spi_sync(edev->spi, &m);
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
author
#9 lore raw

PATCH 9/10 nvmem: meson-efuse: Detect short reads and writes from Secure Monitor

In reply to Lee Jones

meson_efuse_read() and meson_efuse_write() treat any non-negative byte count returned by meson_sm_call_read() or meson_sm_call_write() as success and return 0, silently hiding partial reads or writes.

Return -EIO when meson_sm_call_write() reports a byte count other than bytes, or when meson_sm_call_read() returns a non-zero byte count that does not match bytes (preserving the 0 return convention documented by meson_sm_call_read()).

Fixes 7a0a6d0a7c80 ("nvmem: meson-efuse: Fix return value of nvmem callbacks")

---

Cc Carlo Caione <[email protected]>
Cc Dan Carpenter <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Jerome Brunet <[email protected]>
Cc Joy Chakraborty <[email protected]>
Cc Kevin Hilman <[email protected]>
Cc Martin Blumenstingl <[email protected]>
Cc Neil Armstrong <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/meson-efuse.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/nvmem/meson-efuse.c +10 -2
index e8f384098be5..383ee9281693 100644
@@ -22,8 +22,12 @@ static int meson_efuse_read(void *context, unsigned int offset,
 
 	ret = meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset,
 				 bytes, 0, 0, 0);
+	if (ret < 0)
+		return ret;
+	if (ret && ret != bytes)
+		return -EIO;
 
-	return ret < 0 ? ret : 0;
+	return 0;
 }
 
 static int meson_efuse_write(void *context, unsigned int offset,
@@ -34,8 +38,12 @@ static int meson_efuse_write(void *context, unsigned int offset,
 
 	ret = meson_sm_call_write(fw, (u8 *)val, bytes, SM_EFUSE_WRITE, offset,
 				  bytes, 0, 0, 0);
+	if (ret < 0)
+		return ret;
+	if (ret != bytes)
+		return -EIO;
 
-	return ret < 0 ? ret : 0;
+	return 0;
 }
 
 static const struct of_device_id meson_efuse_match[] = {
Signature · 1 line
2.56.1.911.g883d8bcb20-goog
#10 lore raw

Re: [PATCH 09/10] nvmem: meson-efuse: Detect short reads and writes from Secure Monitor

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] nvmem: meson_efuse_read() exempts 0 return from short-read detection --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] nvmem: meson_efuse_read() exempts 0 return from short-read detection
--

commit f954b4c965de299d7ba849b64117c5f529cb5207
Author: Lee Jones <[email protected]>

nvmem: meson-efuse: Detect short reads and writes from Secure Monitor

This commit checks the byte count returned by meson_sm_call_read() and meson_sm_call_write() in meson_efuse_read() and meson_efuse_write(), returning -EIO on short transfers instead of silently returning 0.

Quoted 6 lines
drivers/nvmem/meson-efuse.c +0 -0
index 029cad70a0af1..0482057219d22 100644
@@ -22,8 +22,12 @@ static int meson_efuse_read(void *context, unsigned int offset,
 

drivers/nvmem/meson-efuse.c static int meson_efuse_read(void *context, unsigned int offset, · line 31

Quoted 10 lines
drivers/nvmem/meson-efuse.c +5 -1
 	ret = meson_sm_call_read(fw, (u8 *)val, bytes, SM_EFUSE_READ, offset,
 				 bytes, 0, 0, 0);
+	if (ret < 0)
+		return ret;
+	if (ret && ret != bytes)
+		return -EIO;
 
-	return ret < 0 ? ret : 0;
+	return 0;
 }

[Severity: Medium]
Should this check "if (ret != bytes)" directly instead of exempting ret == 0?

SM_EFUSE_READ returns the byte count on success; the 0-return fallback in meson_sm_call_read() was added by 7a9bc330df9b ("firmware: meson-sm: Allow 0 as valid return value") for special commands like SM_GET_CHIP_ID that do not report a length.

If Secure Monitor returns 0 for a non-zero SM_EFUSE_READ, meson_sm_call_read() copies stale data from fw->sm_shmem_out_base into val and returns 0. With the "ret &&" guard, meson_efuse_read() treats that 0-byte read as success and returns stale shared memory to nvmem callers instead of -EIO.

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9

_______________________________________________
linux-amlogic mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-amlogic
author
#11 lore raw

PATCH 10/10 nvmem: stm32: Avoid OP-TEE session leak on unaligned writes

In reply to Lee Jones

stm32_bsec_optee_ta_write() opens a TEE session via stm32_bsec_ta_open_session() before validating that offset and bytes are 4-byte aligned. Because the parent NVMEM device registers with word_size and stride set to 1, an unaligned write reaches this function and returns -EINVAL directly without closing the opened TEE session.

Validate offset and bytes alignment before opening the OP-TEE session.

Fixes 6a0bc3522e74 ("nvmem: stm32: add OP-TEE support for STM32MP13x")

---

Cc Alexandre Torgue <[email protected]>
Cc Etienne Carriere <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Maxime Coquelin <[email protected]>
Cc Patrick Delaunay <[email protected]>
Cc Srinivas Kandagatla <[email protected]>
 drivers/nvmem/stm32-bsec-optee-ta.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/nvmem/stm32-bsec-optee-ta.c +4 -4
index f89ce791dd12..7ffeaa4258a1 100644
@@ -211,14 +211,14 @@ int stm32_bsec_optee_ta_write(struct tee_context *ctx, unsigned int lower,
 	int ret;
 	u32 session_id;
 
-	ret = stm32_bsec_ta_open_session(ctx, &session_id);
-	if (ret)
-		return ret;
-
 	/* Allow only writing complete 32-bits aligned words */
 	if ((bytes % 4) || (offset % 4))
 		return -EINVAL;
 
+	ret = stm32_bsec_ta_open_session(ctx, &session_id);
+	if (ret)
+		return ret;
+
 	memset(&arg, 0, sizeof(arg));
 	memset(&param, 0, sizeof(param));
 
Signature · 1 line
2.56.1.911.g883d8bcb20-goog