linux-mmc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mtk-sd: Prevent memory corruption from DMA map failure
@ 2025-06-12 11:26 Masami Hiramatsu (Google)
  2025-06-19  2:05 ` Masami Hiramatsu
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Masami Hiramatsu (Google) @ 2025-06-12 11:26 UTC (permalink / raw)
  To: Chaotian Jing, Ulf Hansson, Matthias Brugger,
	AngeloGioacchino Del Regno
  Cc: mhiramat, Tomasz Figa, Sergey Senozhatsky, linux-mmc,
	linux-kernel, linux-arm-kernel, linux-mediatek

From: Masami Hiramatsu (Google) <mhiramat@kernel.org>

If msdc_prepare_data() fails to map the DMA region, the request is
not prepared for data receiving, but msdc_start_data() proceeds
the DMA with previous setting.
Since this will lead a memory corruption, we have to stop the
request operation soon after the msdc_prepare_data() fails to
prepare it.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
 drivers/mmc/host/mtk-sd.c |   17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/drivers/mmc/host/mtk-sd.c b/drivers/mmc/host/mtk-sd.c
index 3594010bc229..56b4999fe58f 100644
--- a/drivers/mmc/host/mtk-sd.c
+++ b/drivers/mmc/host/mtk-sd.c
@@ -834,6 +834,11 @@ static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data)
 	}
 }
 
+static bool msdc_data_prepared(struct mmc_data *data)
+{
+	return data->host_cookie & MSDC_PREPARE_FLAG;
+}
+
 static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data)
 {
 	if (data->host_cookie & MSDC_ASYNC_FLAG)
@@ -1466,8 +1471,18 @@ static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
 	WARN_ON(!host->hsq_en && host->mrq);
 	host->mrq = mrq;
 
-	if (mrq->data)
+	if (mrq->data) {
 		msdc_prepare_data(host, mrq->data);
+		if (!msdc_data_prepared(mrq->data)) {
+			/*
+			 * Failed to prepare DMA area, fail fast before
+			 * starting any commands.
+			 */
+			mrq->cmd->error = -ENOSPC;
+			mmc_request_done(mmc_from_priv(host), mrq);
+			return;
+		}
+	}
 
 	/* if SBC is required, we have HW option and SW option.
 	 * if HW option is enabled, and SBC does not have "special" flags,


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2025-06-25 12:46 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-12 11:26 [PATCH] mtk-sd: Prevent memory corruption from DMA map failure Masami Hiramatsu (Google)
2025-06-19  2:05 ` Masami Hiramatsu
2025-06-19 11:20 ` Ulf Hansson
2025-06-25  3:56 ` Sergey Senozhatsky
2025-06-25  4:13   ` Sergey Senozhatsky
2025-06-25  4:42     ` Masami Hiramatsu
2025-06-25  5:20 ` [PATCH] mtk-sd: reset host->mrq on prepare_data() error Sergey Senozhatsky
2025-06-25  8:22   ` Masami Hiramatsu
2025-06-25 12:45   ` Ulf Hansson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).