Skip to content

Commit 990ebe7

Browse files
[drivers][ivshmem] validate shared-memory config before ioremap
Fix #11992 When BAR2 size is 0 and ivshmem falls back to the JailHouse shared memory PCI configuration registers (0x40..0x4f), reading the 64-bit address directly into a pointer could overwrite adjacent fields on 32-bit platforms, and the address/size were mapped without validation. Read registers into 32-bit variables, assemble the 64-bit address and size, and validate that size is non-zero, the range does not overflow/wraparound, and 64-bit addresses/sizes fit within 32-bit architecture limits before calling rt_ioremap(). Also stop the probe when any of those four config reads fails: a failed read leaves a full-ones dword behind, and the assembled 64-bit address/size could still pass the range checks on a 64-bit target, leading to a bogus mapping.
1 parent ebf206d commit 990ebe7

1 file changed

Lines changed: 46 additions & 4 deletions

File tree

‎components/drivers/ivshmem/ivshmem.c‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,52 @@ rt_err_t ivshmem_pci_probe(struct rt_pci_device *pdev, struct ivshmem_device *iv
177177

178178
if (!ivdev->shmem_size)
179179
{
180-
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_LO, (rt_uint32_t *)&ivdev->shmem);
181-
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_HI, (rt_uint32_t *)&ivdev->shmem + 1);
182-
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_LO, (rt_uint32_t *)&ivdev->shmem_size);
183-
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_HI, (rt_uint32_t *)&ivdev->shmem_size + 1);
180+
rt_uint32_t addr_lo = 0, addr_hi = 0, size_lo = 0, size_hi = 0;
181+
rt_uint64_t raw_addr, raw_size;
182+
183+
/* Any failed config read leaves a full-ones dword behind, which could
184+
* still pass the range checks below on a 64-bit target. */
185+
if (rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_LO, &addr_lo) ||
186+
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_ADDR_HI, &addr_hi) ||
187+
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_LO, &size_lo) ||
188+
rt_pci_read_config_u32(pdev, PCI_CFG_SHARE_MEM_SIZE_HI, &size_hi))
189+
{
190+
LOG_E("ivshmem: failed to read shmem config");
191+
err = -RT_EIO;
192+
goto _fail;
193+
}
194+
195+
raw_addr = ((rt_uint64_t)addr_hi << 32) | addr_lo;
196+
raw_size = ((rt_uint64_t)size_hi << 32) | size_lo;
197+
198+
/* Validate: size must be non-zero and range must not overflow */
199+
if (raw_size == 0 || (raw_addr + raw_size < raw_addr))
200+
{
201+
LOG_E("ivshmem: invalid shmem config addr=0x%llx size=0x%llx",
202+
(unsigned long long)raw_addr, (unsigned long long)raw_size);
203+
err = -RT_EINVAL;
204+
goto _fail;
205+
}
206+
207+
/* Must fit in system pointer and size range without truncation */
208+
if (sizeof(void *) < sizeof(rt_uint64_t))
209+
{
210+
if (addr_hi != 0 || size_hi != 0 || ((raw_addr + raw_size) > 0xFFFFFFFFULL))
211+
{
212+
LOG_E("ivshmem: 64-bit shmem addr/size unsupported on 32-bit architecture");
213+
err = -RT_EINVAL;
214+
goto _fail;
215+
}
216+
}
217+
218+
ivdev->shmem = (void *)(rt_ubase_t)raw_addr;
219+
ivdev->shmem_size = (rt_size_t)raw_size;
220+
}
221+
222+
if (!ivdev->shmem || !ivdev->shmem_size)
223+
{
224+
err = -RT_EINVAL;
225+
goto _fail;
184226
}
185227

186228
ivdev->shmem = rt_ioremap(ivdev->shmem, ivdev->shmem_size);

0 commit comments

Comments
 (0)