diff options
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | kern/printf.c | 28 | ||||
-rw-r--r-- | linux/dev/kernel/printk.c | 5 |
3 files changed, 41 insertions, 2 deletions
@@ -1,3 +1,13 @@ +2004-09-08 Neal H. Walfield <neal@cs.uml.edu> + + * linux/dev/kernel/printk.c: Include <kern/assert.h>. + (printk): Use vsnprintf, not linux_vsprintf to avoid buffer + overruns. + + * kern/printf.c (struct vsnprintf_cookie): New structure. + (snputc): New function. + (vsnprintf): Likewise. + 2004-11-22 Guillem Jover <guillem@hadrons.org> * bogus/mach_assert.h: Change #ifdef DEBUG to #ifndef NDEBUG diff --git a/kern/printf.c b/kern/printf.c index 85e6278..e6478ae 100644 --- a/kern/printf.c +++ b/kern/printf.c @@ -579,6 +579,34 @@ sprintf(char *buf, const char *fmt, ...) return (buf - start); } +struct vsnprintf_cookie +{ + char *buf; + int index; + int max_len; +}; + +static void +snputc(char c, vm_offset_t arg) +{ + struct vsnprintf_cookie *cookie = (void *) arg; + + if (cookie->index < cookie->max_len) + cookie->buf[cookie->index ++] = c; +} + +int +vsnprintf(char *buf, int size, const char *fmt, va_list args) +{ + struct vsnprintf_cookie cookie + = { .buf = buf, .index = 0, .max_len = size }; + + _doprnt (fmt, &args, snputc, 16, (vm_offset_t)&cookie); + cookie.buf[cookie.index] = '\0'; + + return cookie.index; +} + void safe_gets(str, maxlen) char *str; diff --git a/linux/dev/kernel/printk.c b/linux/dev/kernel/printk.c index 2ff52d1..9dc86cb 100644 --- a/linux/dev/kernel/printk.c +++ b/linux/dev/kernel/printk.c @@ -26,6 +26,7 @@ #define MACH_INCLUDE #include <stdarg.h> #include <asm/system.h> +#include <kern/assert.h> static char buf[2048]; @@ -40,14 +41,14 @@ printk (char *fmt, ...) va_list args; int n, flags; extern void cnputc (); - extern int linux_vsprintf (char *buf, char *fmt,...); char *p, *msg, *buf_end; static int msg_level = -1; save_flags (flags); cli (); va_start (args, fmt); - n = linux_vsprintf (buf + 3, fmt, args); + n = vsnprintf (buf + 3, sizeof (buf) - 3, fmt, args); + assert (n <= sizeof (buf) - 3); buf_end = buf + 3 + n; va_end (args); for (p = buf + 3; p < buf_end; p++) |