File libconsole-Add-console-into-the-list-only-when-succe.patch of Package blog

From: Petr Mladek <pmladek@suse.com>
Date: Tue, 12 Dec 2017 11:41:19 +0100
Subject: libconsole: Add console into the list only when successfully allocated
Git-commit: 34a74a07c05d53f7c71bf7ba44a48f8be32942fd
References: bsc#1071568
Upstream: merged

consalloc() initializes the console when it is used from blogd (io = 1).
This operation might fail when the related tty device cannot be opened.
The structure is freed in this case but it was already added into
the list before. This might cause access to a freed memory.

This patch moves the initialization code into a separate function.
The structure is added to the list only when the initialization
succeeds. It is freed otherwise.

Signed-off-by: Petr Mladek <pmladek@suse.com>
---
 libconsole/console.c | 57 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 32 insertions(+), 25 deletions(-)

diff --git a/libconsole/console.c b/libconsole/console.c
index 600e22608630..47e95a4b9046 100644
--- a/libconsole/console.c
+++ b/libconsole/console.c
@@ -531,13 +531,39 @@ void closeIO(void)
     return;
 }
 
+static int consinitIO(struct console *newc)
+{
+    int tflags;
+
+    if ((newc->fd = open(newc->tty, O_WRONLY|O_NONBLOCK|O_NOCTTY)) < 0) {
+	if (errno == EACCES)
+	    error("can not open %s", newc->tty);
+	warn("can not open %s", newc->tty);
+	return 0;
+    }
+
+    newc->tlock = 0;
+    newc->max_canon = _POSIX_MAX_CANON;
+    memset(&newc->ltio, 0, sizeof(newc->ltio));
+    memset(&newc->otio, 0, sizeof(newc->otio));
+    memset(&newc->ctio, 0, sizeof(newc->ctio));
+    if ((tflags = fcntl(newc->fd, F_GETFL)) < 0)
+	warn("can not get terminal flags of %s", newc->tty);
+
+    tflags &= ~(O_NONBLOCK);
+    tflags |=   O_NOCTTY;
+    if (fcntl(newc->fd, F_SETFL, tflags) < 0)
+	warn("can not set terminal flags of %s", newc->tty);
+
+    return 1;
+}
+
 /* Allocate a console */
 static list_t lcons = { &(lcons), &(lcons) };
 static int consalloc(struct console **cons, char *name, const int cflags, const dev_t dev, int io)
 {
     struct console *newc;
     list_t *head;
-    int tflags;
 
     if (!cons)
 	error("missing console pointer");
@@ -551,6 +577,11 @@ static int consalloc(struct console **cons, char *name, const int cflags, const
     newc->dev = dev;
     newc->pid = -1;
 
+    if (io && !consinitIO(newc)) {
+	free(newc);
+	return 0;
+    }
+
     if (!*cons) {
 	head = &lcons;
 	*cons = (struct console*)head;
@@ -558,30 +589,6 @@ static int consalloc(struct console **cons, char *name, const int cflags, const
 	head = &(*cons)->node;
     insert(&newc->node, head);
 
-    if (!io)
-	return 1;
-
-    if ((newc->fd = open(newc->tty, O_WRONLY|O_NONBLOCK|O_NOCTTY)) < 0) {
-	if (errno == EACCES)
-	    error("can not open %s", newc->tty);
-	warn("can not open %s", newc->tty);
-	free(newc);
-	return 0;
-    }
-
-    newc->tlock = 0;
-    newc->max_canon = _POSIX_MAX_CANON;
-    memset(&newc->ltio, 0, sizeof(newc->ltio));
-    memset(&newc->otio, 0, sizeof(newc->otio));
-    memset(&newc->ctio, 0, sizeof(newc->ctio));
-    if ((tflags = fcntl(newc->fd, F_GETFL)) < 0)
-	warn("can not get terminal flags of %s", newc->tty);
-
-    tflags &= ~(O_NONBLOCK);
-    tflags |=   O_NOCTTY;
-    if (fcntl(newc->fd, F_SETFL, tflags) < 0)
-	warn("can not set terminal flags of %s", newc->tty);
-
     return 1;
 }
 
-- 
2.13.6

openSUSE Build Service is sponsored by