[PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()

Dan Carpenter posted 1 patch 3 months, 2 weeks ago
There is a newer version of this series
drivers/acpi/apei/einj-core.c | 4 ++++
1 file changed, 4 insertions(+)
[PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
Posted by Dan Carpenter 3 months, 2 weeks ago
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
Re: [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
Posted by Ira Weiny 3 months, 2 weeks ago
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]
Re: [PATCH next] ACPI: APEI: EINJ: prevent memory corruption in error_type_set()
Posted by Rafael J. Wysocki 3 months, 1 week ago
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!