Skip to contentSkip to conversation

media: saa7164: check values from the card before using them

PATCH New Maoyi Xie 1 participant 4 messages Dec 4 Media (V4L/DVB)

lore
Share & tools
lore

Maoyi Xie

author
#0 lore raw

cover media: saa7164: check values from the card before using them

A bad ring position or firmware reply from the SAA7164 card can make the driver access memory outside a buffer. The patches are independent.

Found with a CodeQL-based static analysis tool that we are developing. Each case was reproduced in a userspace harness under AddressSanitizer. Not tested on hardware. Compile-tested with W=1 on next-20261116.

I used an LLM to help write the harness and these patches.

Maoyi Xie (3):
media: saa7164: check the ring positions before using them
media: saa7164: check the register access length from the firmware
media: saa7164: check the video input in the firmware reply

 drivers/media/pci/saa7164/saa7164-api.c |  9 +++++++++
 drivers/media/pci/saa7164/saa7164-bus.c | 10 ++++++++++
 2 files changed, 19 insertions(+)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/3 media: saa7164: check the ring positions before using them

saa7164_bus_set() and saa7164_bus_get() read the ring positions again after saa7164_bus_verify() checked them. They use the new values without a check. A position above the ring size makes space_rem wrap around. memcpy_toio() in saa7164_bus_set() then writes past the 4 KiB command ring and reads up to 4 GiB from the caller's buffer. memcpy_fromio() in saa7164_bus_get() copies up to 4 GiB into the 11-byte msg_tmp on the stack.

Check the positions after they are read.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 443c1228d505 ("V4L/DVB (12923): SAA7164: Add support for the NXP SAA7164 silicon")

---
I used an LLM to help write the harness and this patch.

 drivers/media/pci/saa7164/saa7164-bus.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/media/pci/saa7164/saa7164-bus.c +10 -0
index 16739895af75..3d5ef6694e3b 100644
@@ -154,6 +154,11 @@ int saa7164_bus_set(struct saa7164_dev *dev, struct tmComResInfo* msg,
 	timeout = SAA_BUS_TIMEOUT;
 	curr_srp = saa7164_readl(bus->m_dwSetReadPos);
 	curr_swp = saa7164_readl(bus->m_dwSetWritePos);
+	if (curr_srp > bus->m_dwSizeSetRing ||
+	    curr_swp > bus->m_dwSizeSetRing) {
+		ret = SAA_ERR_INVALID_COMMAND;
+		goto out;
+	}
 
 	/* Deal with ring wrapping issues */
 	if (curr_srp > curr_swp)
@@ -336,6 +341,11 @@ int saa7164_bus_get(struct saa7164_dev *dev, struct tmComResInfo* msg,
 	 */
 	curr_gwp = saa7164_readl(bus->m_dwGetWritePos);
 	curr_grp = saa7164_readl(bus->m_dwGetReadPos);
+	if (curr_gwp > bus->m_dwSizeGetRing ||
+	    curr_grp > bus->m_dwSizeGetRing) {
+		ret = SAA_ERR_INVALID_COMMAND;
+		goto out;
+	}
 
 	if (curr_gwp == curr_grp) {
 		ret = SAA_ERR_EMPTY;
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/3 media: saa7164: check the register access length from the firmware

In reply to Maoyi Xie

saa7164_api_i2c_read(), saa7164_api_i2c_write() and saa7164_api_set_dif() ask the firmware for the length of a register access with GET_LEN. They then send and receive that many bytes in the 256-byte stack buffer buf. A length above 256 makes saa7164_cmd_set() read past buf, and a reply of that length is written past it.

Return -EIO when the length does not fit in buf.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 443c1228d505 ("V4L/DVB (12923): SAA7164: Add support for the NXP SAA7164 silicon")
Fixes 7615e434aefd ("[media] saa7164: add various encoder message functions")

---
I used an LLM to help write the harness and this patch.

 drivers/media/pci/saa7164/saa7164-api.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/media/pci/saa7164/saa7164-api.c +6 -0
index 965d285a9240..6eef1e98b4e6 100644
@@ -650,6 +650,8 @@ static int saa7164_api_set_dif(struct saa7164_port *port, u8 reg, u8 val)
 		printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
 		return -EIO;
 	}
+	if (len > sizeof(buf))
+		return -EIO;
 
 	ret = saa7164_cmd_send(dev, port->ifunit.unitid, SET_CUR,
 		EXU_REGISTER_ACCESS_CONTROL, len, &buf);
@@ -1387,6 +1389,8 @@ int saa7164_api_i2c_read(struct saa7164_i2c *bus, u8 addr, u32 reglen, u8 *reg,
 		printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
 		return -EIO;
 	}
+	if (len > sizeof(buf))
+		return -EIO;
 
 	dprintk(DBGLVL_API, "%s() len = %d bytes\n", __func__, len);
 
@@ -1449,6 +1453,8 @@ int saa7164_api_i2c_write(struct saa7164_i2c *bus, u8 addr, u32 datalen,
 		printk(KERN_ERR "%s() error, ret(1) = 0x%x\n", __func__, ret);
 		return -EIO;
 	}
+	if (len > sizeof(buf))
+		return -EIO;
 
 	dprintk(DBGLVL_API, "%s() len = %d bytes unitid=0x%x\n", __func__,
 		len, unitid);
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 3/3 media: saa7164: check the video input in the firmware reply

In reply to Maoyi Xie

saa7164_api_set_videomux() passes &port->mux_input as the buffer of the video mux command. saa7164_cmd_send() copies the reply into that buffer, so the reply replaces the input that saa7164_s_input() checked. The new value then indexes inputs[], which has 7 entries. A reply of 0 or above 7 reads and writes a stack byte outside the array.

Return an error when the value is not a valid input. This skips the audio mux and the unmute.

Found with a CodeQL-based static analysis tool. Reproduced in a userspace harness under AddressSanitizer. Not tested on hardware.

Fixes 7615e434aefd ("[media] saa7164: add various encoder message functions")

---
I used an LLM to help write the harness and this patch.

 drivers/media/pci/saa7164/saa7164-api.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/media/pci/saa7164/saa7164-api.c +3 -0
index 6eef1e98b4e6..05b6a4217349 100644
@@ -439,6 +439,9 @@ int saa7164_api_set_videomux(struct saa7164_port *port)
 	if (ret != SAA_OK)
 		printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__, ret);
 
+	if (port->mux_input < 1 || port->mux_input > ARRAY_SIZE(inputs))
+		return SAA_ERR_BAD_PARAMETER;
+
 	/* Audio Mux */
 	ret = saa7164_cmd_send(port->dev, port->audfeat.sourceid, SET_CUR,
 		SU_INPUT_SELECT_CONTROL, sizeof(u8),
Signature · 1 line
2.34.1