Commit 5a18c531 authored by Jerome Marchand's avatar Jerome Marchand Committed by Greg Kroah-Hartman

staging: zram: prevent accessing an unallocated table when init fails early

When the allocation of zram->table fails, we set zram->disksize to zero
to prevent accessing the unallocated table entries during cleanup.
However, we currently don't take this precaution when the initialization
fails earlier.
Signed-off-by: default avatarJerome Marchand <jmarchan@redhat.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 0900beae
...@@ -651,24 +651,22 @@ int zram_init_device(struct zram *zram) ...@@ -651,24 +651,22 @@ int zram_init_device(struct zram *zram)
if (!zram->compress_workmem) { if (!zram->compress_workmem) {
pr_err("Error allocating compressor working memory!\n"); pr_err("Error allocating compressor working memory!\n");
ret = -ENOMEM; ret = -ENOMEM;
goto fail; goto fail_no_table;
} }
zram->compress_buffer = (void *)__get_free_pages(__GFP_ZERO, 1); zram->compress_buffer = (void *)__get_free_pages(__GFP_ZERO, 1);
if (!zram->compress_buffer) { if (!zram->compress_buffer) {
pr_err("Error allocating compressor buffer space\n"); pr_err("Error allocating compressor buffer space\n");
ret = -ENOMEM; ret = -ENOMEM;
goto fail; goto fail_no_table;
} }
num_pages = zram->disksize >> PAGE_SHIFT; num_pages = zram->disksize >> PAGE_SHIFT;
zram->table = vzalloc(num_pages * sizeof(*zram->table)); zram->table = vzalloc(num_pages * sizeof(*zram->table));
if (!zram->table) { if (!zram->table) {
pr_err("Error allocating zram address table\n"); pr_err("Error allocating zram address table\n");
/* To prevent accessing table entries during cleanup */
zram->disksize = 0;
ret = -ENOMEM; ret = -ENOMEM;
goto fail; goto fail_no_table;
} }
set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT); set_capacity(zram->disk, zram->disksize >> SECTOR_SHIFT);
...@@ -689,6 +687,9 @@ int zram_init_device(struct zram *zram) ...@@ -689,6 +687,9 @@ int zram_init_device(struct zram *zram)
pr_debug("Initialization done!\n"); pr_debug("Initialization done!\n");
return 0; return 0;
fail_no_table:
/* To prevent accessing table entries during cleanup */
zram->disksize = 0;
fail: fail:
__zram_reset_device(zram); __zram_reset_device(zram);
up_write(&zram->init_lock); up_write(&zram->init_lock);
......
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