Commit 1756ddea authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Kees Cook

pstore: Remove worst-case compression size logic

The worst case compression size used by pstore gives an upper bound for
how much the data might inadvertently *grow* due to encapsulation
overhead if the input is not compressible at all.

Given that pstore records have individual 'compressed' flags, we can
simply store the uncompressed data if compressing it would end up using
more space, making the worst case identical to the uncompressed case.

This means we can just drop all the elaborate logic that reasons about
upper bounds for each individual compression algorithm, and just store
the uncompressed data directly if compression fails for any reason.
Co-developed-by: default avatarKees Cook <keescook@chromium.org>
Tested-by: "Guilherme G. Piccoli" <gpiccoli@igalia.com> # Steam Deck
Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
Reviewed-by: default avatarEric Biggers <ebiggers@google.com>
Link: https://lore.kernel.org/r/20230712162332.2670437-2-ardb@kernel.orgSigned-off-by: default avatarKees Cook <keescook@chromium.org>
parent fdf0eaf1
...@@ -16,15 +16,6 @@ ...@@ -16,15 +16,6 @@
#include <linux/console.h> #include <linux/console.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/pstore.h> #include <linux/pstore.h>
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
#include <linux/lzo.h>
#endif
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
#include <linux/lz4.h>
#endif
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
#include <linux/zstd.h>
#endif
#include <linux/crypto.h> #include <linux/crypto.h>
#include <linux/string.h> #include <linux/string.h>
#include <linux/timer.h> #include <linux/timer.h>
...@@ -97,13 +88,7 @@ MODULE_PARM_DESC(kmsg_bytes, "amount of kernel log to snapshot (in bytes)"); ...@@ -97,13 +88,7 @@ MODULE_PARM_DESC(kmsg_bytes, "amount of kernel log to snapshot (in bytes)");
/* Compression parameters */ /* Compression parameters */
static struct crypto_comp *tfm; static struct crypto_comp *tfm;
struct pstore_zbackend {
int (*zbufsize)(size_t size);
const char *name;
};
static char *big_oops_buf; static char *big_oops_buf;
static size_t big_oops_buf_sz;
void pstore_set_kmsg_bytes(int bytes) void pstore_set_kmsg_bytes(int bytes)
{ {
...@@ -168,105 +153,6 @@ static bool pstore_cannot_block_path(enum kmsg_dump_reason reason) ...@@ -168,105 +153,6 @@ static bool pstore_cannot_block_path(enum kmsg_dump_reason reason)
} }
} }
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
static int zbufsize_deflate(size_t size)
{
size_t cmpr;
switch (size) {
/* buffer range for efivars */
case 1000 ... 2000:
cmpr = 56;
break;
case 2001 ... 3000:
cmpr = 54;
break;
case 3001 ... 3999:
cmpr = 52;
break;
/* buffer range for nvram, erst */
case 4000 ... 10000:
cmpr = 45;
break;
default:
cmpr = 60;
break;
}
return (size * 100) / cmpr;
}
#endif
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
static int zbufsize_lzo(size_t size)
{
return lzo1x_worst_compress(size);
}
#endif
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS) || IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
static int zbufsize_lz4(size_t size)
{
return LZ4_compressBound(size);
}
#endif
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
static int zbufsize_842(size_t size)
{
return size;
}
#endif
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
static int zbufsize_zstd(size_t size)
{
return zstd_compress_bound(size);
}
#endif
static const struct pstore_zbackend *zbackend __ro_after_init;
static const struct pstore_zbackend zbackends[] = {
#if IS_ENABLED(CONFIG_PSTORE_DEFLATE_COMPRESS)
{
.zbufsize = zbufsize_deflate,
.name = "deflate",
},
#endif
#if IS_ENABLED(CONFIG_PSTORE_LZO_COMPRESS)
{
.zbufsize = zbufsize_lzo,
.name = "lzo",
},
#endif
#if IS_ENABLED(CONFIG_PSTORE_LZ4_COMPRESS)
{
.zbufsize = zbufsize_lz4,
.name = "lz4",
},
#endif
#if IS_ENABLED(CONFIG_PSTORE_LZ4HC_COMPRESS)
{
.zbufsize = zbufsize_lz4,
.name = "lz4hc",
},
#endif
#if IS_ENABLED(CONFIG_PSTORE_842_COMPRESS)
{
.zbufsize = zbufsize_842,
.name = "842",
},
#endif
#if IS_ENABLED(CONFIG_PSTORE_ZSTD_COMPRESS)
{
.zbufsize = zbufsize_zstd,
.name = "zstd",
},
#endif
{ }
};
static int pstore_compress(const void *in, void *out, static int pstore_compress(const void *in, void *out,
unsigned int inlen, unsigned int outlen) unsigned int inlen, unsigned int outlen)
{ {
...@@ -287,50 +173,46 @@ static int pstore_compress(const void *in, void *out, ...@@ -287,50 +173,46 @@ static int pstore_compress(const void *in, void *out,
static void allocate_buf_for_compression(void) static void allocate_buf_for_compression(void)
{ {
struct crypto_comp *ctx; struct crypto_comp *ctx;
int size;
char *buf; char *buf;
/* Skip if not built-in or compression backend not selected yet. */ /* Skip if not built-in or compression backend not selected yet. */
if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !zbackend) if (!IS_ENABLED(CONFIG_PSTORE_COMPRESS) || !compress)
return; return;
/* Skip if no pstore backend yet or compression init already done. */ /* Skip if no pstore backend yet or compression init already done. */
if (!psinfo || tfm) if (!psinfo || tfm)
return; return;
if (!crypto_has_comp(zbackend->name, 0, 0)) { if (!crypto_has_comp(compress, 0, 0)) {
pr_err("Unknown compression: %s\n", zbackend->name); pr_err("Unknown compression: %s\n", compress);
return;
}
size = zbackend->zbufsize(psinfo->bufsize);
if (size <= 0) {
pr_err("Invalid compression size for %s: %d\n",
zbackend->name, size);
return; return;
} }
buf = kmalloc(size, GFP_KERNEL); /*
* The compression buffer only needs to be as large as the maximum
* uncompressed record size, since any record that would be expanded by
* compression is just stored uncompressed.
*/
buf = kmalloc(psinfo->bufsize, GFP_KERNEL);
if (!buf) { if (!buf) {
pr_err("Failed %d byte compression buffer allocation for: %s\n", pr_err("Failed %zu byte compression buffer allocation for: %s\n",
size, zbackend->name); psinfo->bufsize, compress);
return; return;
} }
ctx = crypto_alloc_comp(zbackend->name, 0, 0); ctx = crypto_alloc_comp(compress, 0, 0);
if (IS_ERR_OR_NULL(ctx)) { if (IS_ERR_OR_NULL(ctx)) {
kfree(buf); kfree(buf);
pr_err("crypto_alloc_comp('%s') failed: %ld\n", zbackend->name, pr_err("crypto_alloc_comp('%s') failed: %ld\n", compress,
PTR_ERR(ctx)); PTR_ERR(ctx));
return; return;
} }
/* A non-NULL big_oops_buf indicates compression is available. */ /* A non-NULL big_oops_buf indicates compression is available. */
tfm = ctx; tfm = ctx;
big_oops_buf_sz = size;
big_oops_buf = buf; big_oops_buf = buf;
pr_info("Using crash dump compression: %s\n", zbackend->name); pr_info("Using crash dump compression: %s\n", compress);
} }
static void free_buf_for_compression(void) static void free_buf_for_compression(void)
...@@ -341,33 +223,6 @@ static void free_buf_for_compression(void) ...@@ -341,33 +223,6 @@ static void free_buf_for_compression(void)
} }
kfree(big_oops_buf); kfree(big_oops_buf);
big_oops_buf = NULL; big_oops_buf = NULL;
big_oops_buf_sz = 0;
}
/*
* Called when compression fails, since the printk buffer
* would be fetched for compression calling it again when
* compression fails would have moved the iterator of
* printk buffer which results in fetching old contents.
* Copy the recent messages from big_oops_buf to psinfo->buf
*/
static size_t copy_kmsg_to_buffer(int hsize, size_t len)
{
size_t total_len;
size_t diff;
total_len = hsize + len;
if (total_len > psinfo->bufsize) {
diff = total_len - psinfo->bufsize + hsize;
memcpy(psinfo->buf, big_oops_buf, hsize);
memcpy(psinfo->buf + hsize, big_oops_buf + diff,
psinfo->bufsize - hsize);
total_len = psinfo->bufsize;
} else
memcpy(psinfo->buf, big_oops_buf, total_len);
return total_len;
} }
void pstore_record_init(struct pstore_record *record, void pstore_record_init(struct pstore_record *record,
...@@ -426,13 +281,8 @@ static void pstore_dump(struct kmsg_dumper *dumper, ...@@ -426,13 +281,8 @@ static void pstore_dump(struct kmsg_dumper *dumper,
record.part = part; record.part = part;
record.buf = psinfo->buf; record.buf = psinfo->buf;
if (big_oops_buf) { dst = big_oops_buf ?: psinfo->buf;
dst = big_oops_buf; dst_size = psinfo->bufsize;
dst_size = big_oops_buf_sz;
} else {
dst = psinfo->buf;
dst_size = psinfo->bufsize;
}
/* Write dump header. */ /* Write dump header. */
header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why, header_size = snprintf(dst, dst_size, "%s#%d Part%u\n", why,
...@@ -453,8 +303,8 @@ static void pstore_dump(struct kmsg_dumper *dumper, ...@@ -453,8 +303,8 @@ static void pstore_dump(struct kmsg_dumper *dumper,
record.compressed = true; record.compressed = true;
record.size = zipped_len; record.size = zipped_len;
} else { } else {
record.size = copy_kmsg_to_buffer(header_size, record.size = header_size + dump_size;
dump_size); memcpy(psinfo->buf, dst, record.size);
} }
} else { } else {
record.size = header_size + dump_size; record.size = header_size + dump_size;
...@@ -703,8 +553,7 @@ static void decompress_record(struct pstore_record *record) ...@@ -703,8 +553,7 @@ static void decompress_record(struct pstore_record *record)
} }
/* Allocate enough space to hold max decompression and ECC. */ /* Allocate enough space to hold max decompression and ECC. */
unzipped_len = big_oops_buf_sz; workspace = kmalloc(psinfo->bufsize + record->ecc_notice_size,
workspace = kmalloc(unzipped_len + record->ecc_notice_size,
GFP_KERNEL); GFP_KERNEL);
if (!workspace) if (!workspace)
return; return;
...@@ -818,27 +667,10 @@ static void pstore_timefunc(struct timer_list *unused) ...@@ -818,27 +667,10 @@ static void pstore_timefunc(struct timer_list *unused)
pstore_timer_kick(); pstore_timer_kick();
} }
static void __init pstore_choose_compression(void)
{
const struct pstore_zbackend *step;
if (!compress)
return;
for (step = zbackends; step->name; step++) {
if (!strcmp(compress, step->name)) {
zbackend = step;
return;
}
}
}
static int __init pstore_init(void) static int __init pstore_init(void)
{ {
int ret; int ret;
pstore_choose_compression();
/* /*
* Check if any pstore backends registered earlier but did not * Check if any pstore backends registered earlier but did not
* initialize compression because crypto was not ready. If so, * initialize compression because crypto was not ready. If so,
......
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