Skip to contentSkip to conversation

ACPI: custom_method: fix potential use-after-free issue

PATCH Applied Mark Langsdorf 2 participants 2 messages Apr 23, 2021 – Apr 28, 2021 ACPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI: custom_method: fix potential use-after-free issue

In cm_write(), buf is always freed when reaching the end of the function. If the requested count is less than table.length, the allocated buffer will be freed but subsequent calls to cm_write() will still try to access it.

Remove the unconditional kfree(buf) at the end of the function and set the buf to NULL in the -EINVAL error path to match the rest of function.

Fixes 03d1571d9513 ("ACPI: custom_method: fix memory leaks")
---
 drivers/acpi/custom_method.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/custom_method.c +1 -1
index 443fdf62dd22..8844f895f9be 100644
@@ -55,6 +55,7 @@ static ssize_t cm_write(struct file *file, const char __user *user_buf,
 	    (*ppos + count < count) ||
 	    (count > uncopied_bytes)) {
 		kfree(buf);
+		buf = NULL;
 		return -EINVAL;
 	}
 
@@ -76,7 +77,6 @@ static ssize_t cm_write(struct file *file, const char __user *user_buf,
 		add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
 	}
 
-	kfree(buf);
 	return count;
 }
 
Signature · 1 line
2.26.3
maintainer Applied
#1 lore raw
Quoted 29 lines · Mark Langsdorf

In cm_write(), buf is always freed when reaching the end of the function. If the requested count is less than table.length, the allocated buffer will be freed but subsequent calls to cm_write() will still try to access it.

Remove the unconditional kfree(buf) at the end of the function and set the buf to NULL in the -EINVAL error path to match the rest of function.

Fixes 03d1571d9513 ("ACPI: custom_method: fix memory leaks")
---
 drivers/acpi/custom_method.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/acpi/custom_method.c +1 -0
index 443fdf62dd22..8844f895f9be 100644
@@ -55,6 +55,7 @@ static ssize_t cm_write(struct file *file, const char __user *user_buf,
            (*ppos + count < count) ||
            (count > uncopied_bytes)) {
                kfree(buf);
+               buf = NULL;
                return -EINVAL;
        }
 

drivers/acpi/custom_method.c static ssize_t cm_write(struct file *file, const char __user *user_buf,

Quoted 8 lines
drivers/acpi/custom_method.c +0 -2
@@ -76,7 +77,6 @@ static ssize_t cm_write(struct file *file, const char __user *user_buf,
                add_taint(TAINT_OVERRIDDEN_ACPI_TABLE, LOCKDEP_NOW_UNRELIABLE);
        }
 
-       kfree(buf);
        return count;
 }
 
--

Applied as 5.13-rc material, thanks!