kmsg: format back to previous for /dev/kmsg
authorMichal Bloch <m.bloch@samsung.com>
Tue, 20 Sep 2016 14:05:51 +0000 (16:05 +0200)
committerSeung-Woo Kim <sw0312.kim@samsung.com>
Wed, 14 Dec 2016 04:52:18 +0000 (13:52 +0900)
 * no binary characters and no \0 at the end
 * done because the new format breaks various tools (such as sd-journal)
 * only affects prime /dev/kmsg, the additional /dev/kmsg12 etc unaffected

Signed-off-by: Michal Bloch <m.bloch@samsung.com>
Change-Id: Ideadbeef08f960fa7a2766b91ab2a72e8d2891b6

kernel/printk/printk.c

index 20ebf82..175cb13 100644 (file)
@@ -923,6 +923,7 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file,
        char cont = '-';
        size_t len;
        ssize_t ret;
+       const int prime = (log_b == &log_buf);
 
        p = user->buf;
        e = user->buf + sizeof(user->buf);
@@ -930,6 +931,7 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file,
        ret = mutex_lock_interruptible(&user->lock);
        if (ret)
                return ret;
+
        raw_spin_lock_irq(&log_b->lock);
        while (user->seq == log_b->next_seq) {
                if (file->f_flags & O_NONBLOCK) {
@@ -992,7 +994,10 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file,
        for (i = 0; i < msg->text_len; i++) {
                unsigned char c = log_text(msg)[i];
 
-               append_char(&p, e, c);
+               if (prime && (c < ' ' || c >= 127 || c == '\\'))
+                       p += scnprintf(p, e - p, "\\x%02x", c);
+               else
+                       append_char(&p, e, c);
        }
 
        /*
@@ -1001,7 +1006,8 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file,
         * to security: else one could forge dictionary tags through the message
         * such as "text\n _PID=123"
         */
-       append_char(&p, e, '\0');
+       if (!prime)
+               append_char(&p, e, '\0');
        append_char(&p, e, '\n');
 
        if (msg->dict_len) {
@@ -1021,6 +1027,11 @@ static ssize_t kmsg_read(struct log_buffer *log_b, struct file *file,
                                continue;
                        }
 
+                       if (prime && (c < ' ' || c >= 127 || c == '\\')) {
+                               p += scnprintf(p, e - p, "\\x%02x", c);
+                               continue;
+                       }
+
                        append_char(&p, e, c);
                }
                append_char(&p, e, '\n');