Skip to content

Commit f2531f1

Browse files
committed
pstore/ram: Do not use stack VLA for parity workspace
Instead of using a stack VLA for the parity workspace, preallocate a memory region. The preallocation is done to keep from needing to perform allocations during crash dump writing, etc. This also fixes a missed release of librs on free. Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent fe1d475 commit f2531f1

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

fs/pstore/ram_core.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,23 @@ static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
9898
uint8_t *data, size_t len, uint8_t *ecc)
9999
{
100100
int i;
101-
uint16_t par[prz->ecc_info.ecc_size];
102101

103102
/* Initialize the parity buffer */
104-
memset(par, 0, sizeof(par));
105-
encode_rs8(prz->rs_decoder, data, len, par, 0);
103+
memset(prz->ecc_info.par, 0,
104+
prz->ecc_info.ecc_size * sizeof(prz->ecc_info.par[0]));
105+
encode_rs8(prz->rs_decoder, data, len, prz->ecc_info.par, 0);
106106
for (i = 0; i < prz->ecc_info.ecc_size; i++)
107-
ecc[i] = par[i];
107+
ecc[i] = prz->ecc_info.par[i];
108108
}
109109

110110
static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
111111
void *data, size_t len, uint8_t *ecc)
112112
{
113113
int i;
114-
uint16_t par[prz->ecc_info.ecc_size];
115114

116115
for (i = 0; i < prz->ecc_info.ecc_size; i++)
117-
par[i] = ecc[i];
118-
return decode_rs8(prz->rs_decoder, data, par, len,
116+
prz->ecc_info.par[i] = ecc[i];
117+
return decode_rs8(prz->rs_decoder, data, prz->ecc_info.par, len,
119118
NULL, 0, NULL, 0, NULL);
120119
}
121120

@@ -228,6 +227,15 @@ static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
228227
return -EINVAL;
229228
}
230229

230+
/* allocate workspace instead of using stack VLA */
231+
prz->ecc_info.par = kmalloc_array(prz->ecc_info.ecc_size,
232+
sizeof(*prz->ecc_info.par),
233+
GFP_KERNEL);
234+
if (!prz->ecc_info.par) {
235+
pr_err("cannot allocate ECC parity workspace\n");
236+
return -ENOMEM;
237+
}
238+
231239
prz->corrected_bytes = 0;
232240
prz->bad_blocks = 0;
233241

@@ -514,6 +522,13 @@ void persistent_ram_free(struct persistent_ram_zone *prz)
514522
}
515523
prz->vaddr = NULL;
516524
}
525+
if (prz->rs_decoder) {
526+
free_rs(prz->rs_decoder);
527+
prz->rs_decoder = NULL;
528+
}
529+
kfree(prz->ecc_info.par);
530+
prz->ecc_info.par = NULL;
531+
517532
persistent_ram_free_old(prz);
518533
kfree(prz);
519534
}

include/linux/pstore_ram.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct persistent_ram_ecc_info {
3939
int ecc_size;
4040
int symsize;
4141
int poly;
42+
uint16_t *par;
4243
};
4344

4445
struct persistent_ram_zone {

0 commit comments

Comments
 (0)