Linux ACPI
 help / color / mirror / Atom feed
* [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
@ 2025-06-25 15:23 Dan Carpenter
  2025-06-25 15:58 ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2025-06-25 15:23 UTC (permalink / raw)
  To: Zaid Alali
  Cc: Rafael J. Wysocki, Len Brown, James Morse, Tony Luck,
	Borislav Petkov, Ira Weiny, Jonathan Cameron,
	Uwe Kleine-König, Al Viro, Sudeep Holla, linux-acpi,
	linux-kernel, kernel-janitors

The "einj_buf" buffer is 32 chars.  Verify that "count" is not too large
for that.  Also leave the last character as a NUL terminator to ensure
the string is properly terminated.

Fixes: 0c6176e1e186 ("ACPI: APEI: EINJ: Enable the discovery of EINJv2 capabilities")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/acpi/apei/einj-core.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/acpi/apei/einj-core.c b/drivers/acpi/apei/einj-core.c
index d6d7e36e3647..e77c0d4b4ee5 100644
--- a/drivers/acpi/apei/einj-core.c
+++ b/drivers/acpi/apei/einj-core.c
@@ -826,8 +826,11 @@ static ssize_t error_type_set(struct file *file, const char __user *buf,
 	int rc;
 	u64 val;
 
+	if (count > sizeof(einj_buf))
+		return -EINVAL;
+
 	memset(einj_buf, 0, sizeof(einj_buf));
-	if (copy_from_user(einj_buf, buf, count))
+	if (copy_from_user(einj_buf, buf, min(count, sizeof((einj_buf) - 1))))
 		return -EFAULT;
 
 	if (strncmp(einj_buf, "V2_", 3) == 0) {
-- 
2.47.2


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

* Re: [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
  2025-06-25 15:23 Dan Carpenter
@ 2025-06-25 15:58 ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2025-06-25 15:58 UTC (permalink / raw)
  To: Zaid Alali
  Cc: Rafael J. Wysocki, Len Brown, James Morse, Tony Luck,
	Borislav Petkov, Ira Weiny, Jonathan Cameron,
	Uwe Kleine-König, Al Viro, Sudeep Holla, linux-acpi,
	linux-kernel, kernel-janitors

On Wed, Jun 25, 2025 at 10:23:26AM -0500, Dan Carpenter wrote:
> The "einj_buf" buffer is 32 chars.  Verify that "count" is not too large
> for that.  Also leave the last character as a NUL terminator to ensure
> the string is properly terminated.
> 
> Fixes: 0c6176e1e186 ("ACPI: APEI: EINJ: Enable the discovery of EINJv2 capabilities")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
>  drivers/acpi/apei/einj-core.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/acpi/apei/einj-core.c b/drivers/acpi/apei/einj-core.c
> index d6d7e36e3647..e77c0d4b4ee5 100644
> --- a/drivers/acpi/apei/einj-core.c
> +++ b/drivers/acpi/apei/einj-core.c
> @@ -826,8 +826,11 @@ static ssize_t error_type_set(struct file *file, const char __user *buf,
>  	int rc;
>  	u64 val;
>  
> +	if (count > sizeof(einj_buf))
> +		return -EINVAL;
> +
>  	memset(einj_buf, 0, sizeof(einj_buf));
> -	if (copy_from_user(einj_buf, buf, count))
> +	if (copy_from_user(einj_buf, buf, min(count, sizeof((einj_buf) - 1))))

Nope.  I put the parentheses in the wrong place...  Let me resend.

regards,
dan carpenter

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

* [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
@ 2025-06-25 18:57 Dan Carpenter
  2025-06-25 21:21 ` Ira Weiny
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2025-06-25 18:57 UTC (permalink / raw)
  To: Zaid Alali
  Cc: Rafael J. Wysocki, Len Brown, James Morse, Tony Luck,
	Borislav Petkov, Ira Weiny, Jonathan Cameron, Al Viro,
	Sudeep Holla, Jon Hunter, linux-acpi, linux-kernel,
	kernel-janitors

The "einj_buf" buffer is 32 chars.  If "count" is larger than that it
results in memory corruption.  Cap it at 31 so that we leave the last
character as a NUL terminator.  By the way, the highest reasonable value
for "count" is 24.

Fixes: 0c6176e1e186 ("ACPI: APEI: EINJ: Enable the discovery of EINJv2 capabilities")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
v3: Style improvements.  Add a comment.
v2: I introduces a bug in v1 because I put parentheses in the wrong place.

 drivers/acpi/apei/einj-core.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/acpi/apei/einj-core.c b/drivers/acpi/apei/einj-core.c
index d6d7e36e3647..f5cfa6310f0e 100644
--- a/drivers/acpi/apei/einj-core.c
+++ b/drivers/acpi/apei/einj-core.c
@@ -826,6 +826,10 @@ static ssize_t error_type_set(struct file *file, const char __user *buf,
 	int rc;
 	u64 val;
 
+	/* Leave the last character for the NUL terminator */
+	if (count > sizeof(einj_buf) - 1)
+		return -EINVAL;
+
 	memset(einj_buf, 0, sizeof(einj_buf));
 	if (copy_from_user(einj_buf, buf, count))
 		return -EFAULT;
-- 
2.47.2


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

* Re: [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
  2025-06-25 18:57 [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set() Dan Carpenter
@ 2025-06-25 21:21 ` Ira Weiny
  2025-06-26 18:47   ` Rafael J. Wysocki
  0 siblings, 1 reply; 5+ messages in thread
From: Ira Weiny @ 2025-06-25 21:21 UTC (permalink / raw)
  To: Dan Carpenter, Zaid Alali
  Cc: Rafael J. Wysocki, Len Brown, James Morse, Tony Luck,
	Borislav Petkov, Ira Weiny, Jonathan Cameron, Al Viro,
	Sudeep Holla, Jon Hunter, linux-acpi, linux-kernel,
	kernel-janitors

Dan Carpenter wrote:
> The "einj_buf" buffer is 32 chars.  If "count" is larger than that it
> results in memory corruption.  Cap it at 31 so that we leave the last
> character as a NUL terminator.  By the way, the highest reasonable value
> for "count" is 24.
> 
> Fixes: 0c6176e1e186 ("ACPI: APEI: EINJ: Enable the discovery of EINJv2 capabilities")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

Reviewed-by: Ira Weiny <ira.weiny@intel.com>

[snip]

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

* Re: [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
  2025-06-25 21:21 ` Ira Weiny
@ 2025-06-26 18:47   ` Rafael J. Wysocki
  0 siblings, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2025-06-26 18:47 UTC (permalink / raw)
  To: Ira Weiny, Dan Carpenter
  Cc: Zaid Alali, Len Brown, James Morse, Tony Luck, Borislav Petkov,
	Jonathan Cameron, Al Viro, Sudeep Holla, Jon Hunter, linux-acpi,
	linux-kernel, kernel-janitors

On Wed, Jun 25, 2025 at 11:19 PM Ira Weiny <ira.weiny@intel.com> wrote:
>
> Dan Carpenter wrote:
> > The "einj_buf" buffer is 32 chars.  If "count" is larger than that it
> > results in memory corruption.  Cap it at 31 so that we leave the last
> > character as a NUL terminator.  By the way, the highest reasonable value
> > for "count" is 24.
> >
> > Fixes: 0c6176e1e186 ("ACPI: APEI: EINJ: Enable the discovery of EINJv2 capabilities")
> > Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
>
> Reviewed-by: Ira Weiny <ira.weiny@intel.com>

Applied, thanks!

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

end of thread, other threads:[~2025-06-26 18:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-25 18:57 [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set() Dan Carpenter
2025-06-25 21:21 ` Ira Weiny
2025-06-26 18:47   ` Rafael J. Wysocki
  -- strict thread matches above, loose matches on Subject: below --
2025-06-25 15:23 Dan Carpenter
2025-06-25 15:58 ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox