mmc: sdhci: Fix potential ADMA descriptor table overflow
PATCH 1/1 mmc: sdhci: Fix potential ADMA descriptor table overflow
From: Ian Roberts <[email protected]>
Change ADMA_TABLE_NO_ENTRIES to round the division up to fully contain CONFIG_SYS_MMC_MAX_BLK_COUNT, fixing potential buffer overflow of the ADMA descriptor table.
sdhci_prepare_adma_table() expecitily states it does _not_ check for overflow as the descriptor table size is dependent on CONFIG_SYS_MMC_MAX_BLK_COUNT. However, the ADMA_TABLE_NO_ENTRIES calculation does not round up the divison, so with the current u-boot
defaults:max_mmc_transfer = (CONFIG_SYS_MMC_MAX_BLK_COUNT * MMC_MAX_BLOCK_LEN) = 65535 * 512 = 33553920 bytes.ADMA_TABLE_NO_ENTRIES = max_mmc_transfer / ADMA_MAX_LEN = 33553920 / 65532, which does not divide cleanly. actual_max_transfer = ADMA_TABLE_NO_ENTRIES * ADMA_MAX_LEN = 512 * 65532 = 33552384, which is smaller than max_mmc_transfer. This can cause sdhci_prepare_adma_table() to write one extra descriptor, overflowing the table when a transaction larger than actual_max_transfer is issued.
---
--- include/sdhci.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
include/sdhci.h +3 -2
@@ -11,6 +11,7 @@#include <linux/bitops.h> #include <linux/types.h> +#include <linux/kernel.h> #include <asm/io.h> #include <mmc.h> #include <asm/gpio.h>
@@ -299,8 +300,8 @@ struct sdhci_ops {#else #define ADMA_DESC_LEN 8#endif -#define ADMA_TABLE_NO_ENTRIES (CONFIG_SYS_MMC_MAX_BLK_COUNT * \ - MMC_MAX_BLOCK_LEN) / ADMA_MAX_LEN +#define ADMA_TABLE_NO_ENTRIES DIV_ROUND_UP(CONFIG_SYS_MMC_MAX_BLK_COUNT * \ + MMC_MAX_BLOCK_LEN, ADMA_MAX_LEN)#define ADMA_TABLE_SZ (ADMA_TABLE_NO_ENTRIES * ADMA_DESC_LEN)
Signature · 1 line
2.43.2
Quoted 32 lines
-----Original Message-----
From: Greg Malysa <[email protected]>
Sent: Tuesday, March 26, 2024 11:23 AM
To: [email protected]; Peng Fan <[email protected]>; Jaehoon Chung <[email protected]>
Cc: Ian Roberts <[email protected]>; Nathan Barrett-Morrison <[email protected]>; Greg Malysa <[email protected]>; Sean Anderson <[email protected]>; Tom Rini <[email protected]>
Subject: [PATCH] mmc: sdhci: Fix potential ADMA descriptor table overflowFrom: Ian Roberts <[email protected]>
Change
ADMA_TABLE_NO_ENTRIESto round the division up to fully containCONFIG_SYS_MMC_MAX_BLK_COUNT, fixing potential buffer overflow of the ADMA descriptor table.
sdhci_prepare_adma_table()expecitily states it does _not_ check for overflow as the descriptor table size is dependent onCONFIG_SYS_MMC_MAX_BLK_COUNT. However, theADMA_TABLE_NO_ENTRIEScalculation does not round up the divison, so with the current u-boot
defaults:max_mmc_transfer= (CONFIG_SYS_MMC_MAX_BLK_COUNT*MMC_MAX_BLOCK_LEN) = 65535 * 512 = 33553920 bytes.ADMA_TABLE_NO_ENTRIES=max_mmc_transfer/ADMA_MAX_LEN= 33553920 / 65532, which does not divide cleanly.actual_max_transfer=ADMA_TABLE_NO_ENTRIES*ADMA_MAX_LEN= 512 * 65532 = 33552384, which is smaller thanmax_mmc_transfer. This can causesdhci_prepare_adma_table()to write one extra descriptor, overflowing the table when a transaction larger thanactual_max_transferis issued.
Quoted 4 lines
Co-developed-by Nathan Barrett-Morrison <[email protected]>Signed-off-by Nathan Barrett-Morrison <[email protected]>Signed-off-by Greg Malysa <[email protected]>Signed-off-by Ian Roberts <[email protected]>
Best Regards,
Jaehoon Chung
Quoted 33 lines
---
--- include/sdhci.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)include/sdhci.h +3
-3@@ -11,6 +11,7 @@#include <linux/bitops.h> #include <linux/types.h> +#include <linux/kernel.h> #include <asm/io.h> #include <mmc.h> #include <asm/gpio.h> @@ -299,8 +300,8 @@ struct sdhci_ops {#else #define ADMA_DESC_LEN 8#endif -#define ADMA_TABLE_NO_ENTRIES (CONFIG_SYS_MMC_MAX_BLK_COUNT * \ - MMC_MAX_BLOCK_LEN) / ADMA_MAX_LEN +#define ADMA_TABLE_NO_ENTRIES DIV_ROUND_UP(CONFIG_SYS_MMC_MAX_BLK_COUNT * \ + MMC_MAX_BLOCK_LEN, ADMA_MAX_LEN)#define ADMA_TABLE_SZ (ADMA_TABLE_NO_ENTRIES * ADMA_DESC_LEN) --2.43.2