RAMOOPS: Don't overflow over non-allocated regions

The current code mis-calculates the ramoops header size, leading to an
overflow over the next record at best, or over a non-allocated region at
worst. Fix that calculation.

Signed-off-by: Ahmed S. Darwish <darwish.07@gmail.com>
Acked-by: Marco Stornelli <marco.stornelli@gmail.com>
Cc: stable@kernel.org
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Ahmed S. Darwish and committed by Linus Torvalds 1873bb81 ffc96d62

+7 -5
+7 -5
drivers/char/ramoops.c
··· 29 29 #include <linux/ramoops.h> 30 30 31 31 #define RAMOOPS_KERNMSG_HDR "====" 32 - #define RAMOOPS_HEADER_SIZE (5 + sizeof(struct timeval)) 33 32 34 33 #define RECORD_SIZE 4096 35 34 ··· 64 65 struct ramoops_context, dump); 65 66 unsigned long s1_start, s2_start; 66 67 unsigned long l1_cpy, l2_cpy; 67 - int res; 68 - char *buf; 68 + int res, hdr_size; 69 + char *buf, *buf_orig; 69 70 struct timeval timestamp; 70 71 71 72 /* Only dump oopses if dump_oops is set */ ··· 73 74 return; 74 75 75 76 buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE)); 77 + buf_orig = buf; 78 + 76 79 memset(buf, '\0', RECORD_SIZE); 77 80 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR); 78 81 buf += res; ··· 82 81 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec); 83 82 buf += res; 84 83 85 - l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE)); 86 - l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - RAMOOPS_HEADER_SIZE) - l2_cpy); 84 + hdr_size = buf - buf_orig; 85 + l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - hdr_size)); 86 + l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - hdr_size) - l2_cpy); 87 87 88 88 s2_start = l2 - l2_cpy; 89 89 s1_start = l1 - l1_cpy;