All of lore.kernel.org
 help / color / mirror / Atom feed
From: Miao Wang <shankerwangmiao@gmail.com>
To: Binbin Zhou <zhoubinbin@loongson.cn>,
	 Chong Qiao <qiaochong@loongson.cn>, Lee Jones <lee@kernel.org>,
	 Huacai Chen <chenhuacai@kernel.org>,
	Corey Minyard <corey@minyard.net>,
	 Linus Walleij <linusw@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>
Cc: Xi Ruoyao <xry111@xry111.site>, WANG Xuerui <kernel@xen0n.name>,
	 Yinbo Zhu <zhuyinbo@loongson.cn>,
	Jiaxun Yang <jiaxun.yang@flygoat.com>,
	 mfd@lists.linux.dev, linux-kernel@vger.kernel.org,
	 linux-gpio@vger.kernel.org,
	openipmi-developer@lists.sourceforge.net,
	 Miao Wang <shankerwangmiao@gmail.com>
Subject: [PATCH RFC v6 1/7] mfd: ls2kbmc: Make a copy when parsing mode string
Date: Wed, 05 Aug 2026 03:14:19 +0800	[thread overview]
Message-ID: <20260805-ls2kbmc-mod-v6-1-16ccde412d86@gmail.com> (raw)
In-Reply-To: <20260805-ls2kbmc-mod-v6-0-16ccde412d86@gmail.com>

When parsing the mode string from BMC, the string is manipulated
in-place with strsep(), preventing from parsing it again. Make a copy of
the original string and manipulate the copy instead to fix this.

Fixes: 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")
Signed-off-by: Miao Wang <shankerwangmiao@gmail.com>
---
 drivers/mfd/ls2k-bmc-core.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/drivers/mfd/ls2k-bmc-core.c b/drivers/mfd/ls2k-bmc-core.c
index 408056bfb2fe757a5bde43775a483a48352e706d..fc9695eedd3662ac92ff116811bb07bff1c994ea 100644
--- a/drivers/mfd/ls2k-bmc-core.c
+++ b/drivers/mfd/ls2k-bmc-core.c
@@ -427,34 +427,54 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	/* Assume 64 bytes is enough for the resolution string */
+	char mode_buf[64], mode_buf_orig[64];
+	char *mode = mode_buf;
+	const void __iomem *mode_base;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
-	if (!mode)
+	mode_base = ioremap(pci_resource_start(pdev, 0) + SZ_16M,
+			    sizeof(mode_buf));
+	if (!mode_base)
 		return -ENOMEM;
+	memcpy_fromio(mode_buf, mode_base, sizeof(mode_buf) - 1);
+	mode_buf[sizeof(mode_buf) - 1] = '\0';
+	iounmap((void __iomem *)mode_base);
+	memcpy(mode_buf_orig, mode_buf, sizeof(mode_buf_orig));
 
 	/* The resolution field starts with the flag "video=". */
 	if (!strncmp(mode, "video=", 6))
 		mode = mode + 6;
 
-	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
+	ret = kstrtouint(strsep(&mode, "x"), 10, &pd->width);
 	if (ret)
-		return ret;
+		goto invalid_mode;
 
-	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
+	if (mode == NULL) {
+		ret = -EINVAL;
+		goto invalid_mode;
+	}
+	ret = kstrtouint(strsep(&mode, "-"), 10, &pd->height);
 	if (ret)
-		return ret;
+		goto invalid_mode;
 
+	if (mode == NULL) {
+		ret = -EINVAL;
+		goto invalid_mode;
+	}
 	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
 	if (ret)
-		return ret;
+		goto invalid_mode;
 
 	pd->stride = pd->width * depth / 8;
 	pd->format = depth == 32 ? "a8r8g8b8" : "r5g6b5";
 
 	return 0;
+
+invalid_mode:
+	dev_err(&pdev->dev, "Invalid resolution string: %s\n", mode_buf_orig);
+	return ret;
 }
 
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)

-- 
2.49.0


WARNING: multiple messages have this Message-ID (diff)
From: Miao Wang via B4 Relay <devnull+shankerwangmiao.gmail.com@kernel.org>
To: Binbin Zhou <zhoubinbin@loongson.cn>,
	 Chong Qiao <qiaochong@loongson.cn>, Lee Jones <lee@kernel.org>,
	 Huacai Chen <chenhuacai@kernel.org>,
	Corey Minyard <corey@minyard.net>,
	 Linus Walleij <linusw@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>
Cc: Xi Ruoyao <xry111@xry111.site>, WANG Xuerui <kernel@xen0n.name>,
	 Yinbo Zhu <zhuyinbo@loongson.cn>,
	Jiaxun Yang <jiaxun.yang@flygoat.com>,
	 mfd@lists.linux.dev, linux-kernel@vger.kernel.org,
	 linux-gpio@vger.kernel.org,
	openipmi-developer@lists.sourceforge.net,
	 Miao Wang <shankerwangmiao@gmail.com>
Subject: [PATCH RFC v6 1/7] mfd: ls2kbmc: Make a copy when parsing mode string
Date: Wed, 05 Aug 2026 03:14:19 +0800	[thread overview]
Message-ID: <20260805-ls2kbmc-mod-v6-1-16ccde412d86@gmail.com> (raw)
In-Reply-To: <20260805-ls2kbmc-mod-v6-0-16ccde412d86@gmail.com>

From: Miao Wang <shankerwangmiao@gmail.com>

When parsing the mode string from BMC, the string is manipulated
in-place with strsep(), preventing from parsing it again. Make a copy of
the original string and manipulate the copy instead to fix this.

Fixes: 0d64f6d1ffe9 ("mfd: ls2kbmc: Introduce Loongson-2K BMC core driver")
Signed-off-by: Miao Wang <shankerwangmiao@gmail.com>
---
 drivers/mfd/ls2k-bmc-core.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/drivers/mfd/ls2k-bmc-core.c b/drivers/mfd/ls2k-bmc-core.c
index 408056bfb2fe757a5bde43775a483a48352e706d..fc9695eedd3662ac92ff116811bb07bff1c994ea 100644
--- a/drivers/mfd/ls2k-bmc-core.c
+++ b/drivers/mfd/ls2k-bmc-core.c
@@ -427,34 +427,54 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
  */
 static int ls2k_bmc_parse_mode(struct pci_dev *pdev, struct simplefb_platform_data *pd)
 {
-	char *mode;
+	/* Assume 64 bytes is enough for the resolution string */
+	char mode_buf[64], mode_buf_orig[64];
+	char *mode = mode_buf;
+	const void __iomem *mode_base;
 	int depth, ret;
 
 	/* The last 16M of PCI BAR0 is used to store the resolution string. */
-	mode = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0) + SZ_16M, SZ_16M);
-	if (!mode)
+	mode_base = ioremap(pci_resource_start(pdev, 0) + SZ_16M,
+			    sizeof(mode_buf));
+	if (!mode_base)
 		return -ENOMEM;
+	memcpy_fromio(mode_buf, mode_base, sizeof(mode_buf) - 1);
+	mode_buf[sizeof(mode_buf) - 1] = '\0';
+	iounmap((void __iomem *)mode_base);
+	memcpy(mode_buf_orig, mode_buf, sizeof(mode_buf_orig));
 
 	/* The resolution field starts with the flag "video=". */
 	if (!strncmp(mode, "video=", 6))
 		mode = mode + 6;
 
-	ret = kstrtoint(strsep(&mode, "x"), 10, &pd->width);
+	ret = kstrtouint(strsep(&mode, "x"), 10, &pd->width);
 	if (ret)
-		return ret;
+		goto invalid_mode;
 
-	ret = kstrtoint(strsep(&mode, "-"), 10, &pd->height);
+	if (mode == NULL) {
+		ret = -EINVAL;
+		goto invalid_mode;
+	}
+	ret = kstrtouint(strsep(&mode, "-"), 10, &pd->height);
 	if (ret)
-		return ret;
+		goto invalid_mode;
 
+	if (mode == NULL) {
+		ret = -EINVAL;
+		goto invalid_mode;
+	}
 	ret = kstrtoint(strsep(&mode, "@"), 10, &depth);
 	if (ret)
-		return ret;
+		goto invalid_mode;
 
 	pd->stride = pd->width * depth / 8;
 	pd->format = depth == 32 ? "a8r8g8b8" : "r5g6b5";
 
 	return 0;
+
+invalid_mode:
+	dev_err(&pdev->dev, "Invalid resolution string: %s\n", mode_buf_orig);
+	return ret;
 }
 
 static int ls2k_bmc_probe(struct pci_dev *dev, const struct pci_device_id *id)

-- 
2.49.0



  reply	other threads:[~2026-08-04 19:14 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-04 19:14 [PATCH RFC v6 0/7] mfd: ls2kbmc: multiple fixes for this driver Miao Wang
2026-08-04 19:14 ` Miao Wang via B4 Relay
2026-08-04 19:14 ` Miao Wang [this message]
2026-08-04 19:14   ` [PATCH RFC v6 1/7] mfd: ls2kbmc: Make a copy when parsing mode string Miao Wang via B4 Relay
2026-08-04 19:14 ` [PATCH RFC v6 2/7] mfd: ls2kbmc: Sanity check for the connected pci port Miao Wang
2026-08-04 19:14   ` Miao Wang via B4 Relay
2026-08-04 19:14 ` [PATCH RFC v6 3/7] mfd: ls2kbmc: Redraw using exported functions Miao Wang
2026-08-04 19:14   ` Miao Wang via B4 Relay
2026-08-04 19:14 ` [PATCH RFC v6 4/7] mfd: ls2kbmc: Cancel the work queue on removal Miao Wang
2026-08-04 19:14   ` Miao Wang via B4 Relay
2026-08-04 19:14 ` [PATCH RFC v6 5/7] ipmi: ls2k: adjust dependency to its mfd driver Miao Wang
2026-08-04 19:14   ` Miao Wang via B4 Relay
2026-08-04 19:34   ` Corey Minyard
2026-08-04 19:14 ` [PATCH RFC v6 6/7] mfd: ls2kbmc: Able to be compiled as a module Miao Wang
2026-08-04 19:14   ` Miao Wang via B4 Relay
2026-08-04 19:14 ` [PATCH RFC v6 7/7] mfd: ls2kbmc: Capture the reset event of BMC through GPIO Miao Wang
2026-08-04 19:14   ` Miao Wang via B4 Relay
2026-08-10 10:14   ` Bartosz Golaszewski
2026-08-10 11:28     ` Miao Wang
2026-08-10 11:30       ` Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260805-ls2kbmc-mod-v6-1-16ccde412d86@gmail.com \
    --to=shankerwangmiao@gmail.com \
    --cc=brgl@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=corey@minyard.net \
    --cc=jiaxun.yang@flygoat.com \
    --cc=kernel@xen0n.name \
    --cc=lee@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mfd@lists.linux.dev \
    --cc=openipmi-developer@lists.sourceforge.net \
    --cc=qiaochong@loongson.cn \
    --cc=xry111@xry111.site \
    --cc=zhoubinbin@loongson.cn \
    --cc=zhuyinbo@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.