Commit e163fdb3 authored by Kees Cook's avatar Kees Cook

pstore/ram: Regularize prz label allocation lifetime

In my attempt to fix a memory leak, I introduced a double-free in the
pstore error path. Instead of trying to manage the allocation lifetime
between persistent_ram_new() and its callers, adjust the logic so
persistent_ram_new() always takes a kstrdup() copy, and leaves the
caller's allocation lifetime up to the caller. Therefore callers are
_always_ responsible for freeing their label. Before, it only needed
freeing when the prz itself failed to allocate, and not in any of the
other prz failure cases, which callers would have no visibility into,
which is the root design problem that lead to both the leak and now
double-free bugs.
Reported-by: default avatarCengiz Can <cengiz@kernel.wtf>
Link: https://lore.kernel.org/lkml/d4ec59002ede4aaf9928c7f7526da87c@kernel.wtf
Fixes: 8df955a3 ("pstore/ram: Fix error-path memory leak in persistent_ram_new() callers")
Cc: stable@vger.kernel.org
Signed-off-by: default avatarKees Cook <keescook@chromium.org>
parent 9e5f1c19
...@@ -583,12 +583,12 @@ static int ramoops_init_przs(const char *name, ...@@ -583,12 +583,12 @@ static int ramoops_init_przs(const char *name,
prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig, prz_ar[i] = persistent_ram_new(*paddr, zone_sz, sig,
&cxt->ecc_info, &cxt->ecc_info,
cxt->memtype, flags, label); cxt->memtype, flags, label);
kfree(label);
if (IS_ERR(prz_ar[i])) { if (IS_ERR(prz_ar[i])) {
err = PTR_ERR(prz_ar[i]); err = PTR_ERR(prz_ar[i]);
dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n", dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
name, record_size, name, record_size,
(unsigned long long)*paddr, err); (unsigned long long)*paddr, err);
kfree(label);
while (i > 0) { while (i > 0) {
i--; i--;
...@@ -629,12 +629,12 @@ static int ramoops_init_prz(const char *name, ...@@ -629,12 +629,12 @@ static int ramoops_init_prz(const char *name,
label = kasprintf(GFP_KERNEL, "ramoops:%s", name); label = kasprintf(GFP_KERNEL, "ramoops:%s", name);
*prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info, *prz = persistent_ram_new(*paddr, sz, sig, &cxt->ecc_info,
cxt->memtype, PRZ_FLAG_ZAP_OLD, label); cxt->memtype, PRZ_FLAG_ZAP_OLD, label);
kfree(label);
if (IS_ERR(*prz)) { if (IS_ERR(*prz)) {
int err = PTR_ERR(*prz); int err = PTR_ERR(*prz);
dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n", dev_err(dev, "failed to request %s mem region (0x%zx@0x%llx): %d\n",
name, sz, (unsigned long long)*paddr, err); name, sz, (unsigned long long)*paddr, err);
kfree(label);
return err; return err;
} }
......
...@@ -574,7 +574,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size, ...@@ -574,7 +574,7 @@ struct persistent_ram_zone *persistent_ram_new(phys_addr_t start, size_t size,
/* Initialize general buffer state. */ /* Initialize general buffer state. */
raw_spin_lock_init(&prz->buffer_lock); raw_spin_lock_init(&prz->buffer_lock);
prz->flags = flags; prz->flags = flags;
prz->label = label; prz->label = kstrdup(label, GFP_KERNEL);
ret = persistent_ram_buffer_map(start, size, prz, memtype); ret = persistent_ram_buffer_map(start, size, prz, memtype);
if (ret) if (ret)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment