]> granicus.if.org Git - procps-ng/commitdiff
procio: fix potential out-of-bounds access when write fails
authorPatrick Steinhardt <ps@pks.im>
Tue, 29 May 2018 11:20:00 +0000 (13:20 +0200)
committerCraig Small <csmall@enc.com.au>
Mon, 4 Mar 2019 10:44:52 +0000 (21:44 +1100)
When writing to procfs via `proc_write` fails, we try to chunk the
buffer into smaller pieces to work around that issue. When searching for
the next location to split the buffer, though, we can underflow the
buffer in case the current offset is smaller than `LINELEN`. Fix the
issue by passing `cookie->offset` instead of `LINELEN` into `memrchr` in
case `cookie->offset` is smaller than `LINELEN`.

This bug can be triggered on musl-based systems, e.g. by executing

    $ sysctl kernel.printk_ratelimit=1000000000000000

As the value is out-of-range, `write` will return an error and set
`errno` to `EINVAL`. As we're only trying to write a smallish buffer
with a length smaller than `LINELEN` and as the buffer does not contain
any newlines, the call

    token = (char*)memrchr(cookie->buf+offset, '\n', LINELEN);

will underflow the buffer and crash the program.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
procio.c

index 2813cd51a6638820ac304f44bca51c6ef2bf6a36..f3258ff6bcf67fe80cd18fd5bc45288a5c0c7097 100644 (file)
--- a/procio.c
+++ b/procio.c
@@ -251,7 +251,7 @@ ssize_t proc_write(void *c, const char *buf, size_t count)
                                if (cookie->offset > LINELEN)
                                        token = (char*)memrchr(cookie->buf+offset, cookie->delim, LINELEN);
                                else
-                                       token = (char*)memrchr(cookie->buf+offset, '\n', LINELEN);
+                                       token = (char*)memrchr(cookie->buf+offset, '\n', cookie->offset);
                                if (token)
                                        *token = '\n';
                                else {