Skip to content

Commit

Permalink
pstore/ram: Fix possible NULL dereference
Browse files Browse the repository at this point in the history
We can dereference 'cxt->cprz' if console and dump logging are disabled
(which is unlikely, but still possible to do). This patch fixes the issue
by changing the code so that we don't dereference przs at all, we can
just calculate bufsize from console_size and record_size values.

Plus, while at it, the patch improves the buffer size calculation.

After Kay's printk rework, we know the optimal buffer size for console
logging -- it is LOG_LINE_MAX (defined privately in printk.c). Previously,
if only console logging was enabled, we would allocate unnecessary large
buffer in pstore, while we only need LOG_LINE_MAX. (Pstore console logging
is still capable of handling buffers > LOG_LINE_MAX, it will just do
multiple calls to psinfo->write).

Note that I don't export the constant, since we will do even a better
thing soon: we will switch console logging to a new write_buf API, which
will eliminate the need for the additional buffer; and so we won't need
the constant.

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Anton Vorontsov <anton.vorontsov@linaro.org>
Acked-by: Kees Cook <keescook@chromium.org>
  • Loading branch information
Anton Vorontsov committed Aug 4, 2012
1 parent 0d7614f commit a384f64
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions fs/pstore/ram.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,14 @@ static int __devinit ramoops_probe(struct platform_device *pdev)

cxt->pstore.data = cxt;
/*
* Console can handle any buffer size, so prefer dumps buffer
* size since usually it is smaller.
* Console can handle any buffer size, so prefer LOG_LINE_MAX. If we
* have to handle dumps, we must have at least record_size buffer. And
* for ftrace, bufsize is irrelevant (if bufsize is 0, buf will be
* ZERO_SIZE_PTR).
*/
if (cxt->przs)
cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
else
cxt->pstore.bufsize = cxt->cprz->buffer_size;
if (cxt->console_size)
cxt->pstore.bufsize = 1024; /* LOG_LINE_MAX */
cxt->pstore.bufsize = max(cxt->record_size, cxt->pstore.bufsize);
cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
spin_lock_init(&cxt->pstore.buf_lock);
if (!cxt->pstore.buf) {
Expand Down

0 comments on commit a384f64

Please sign in to comment.