From: Steven Smith <sos22@archy.org.uk>
To: util-linux@vger.kernel.org
Cc: sos22@srcf.ucam.org
Subject: [PATCH] Fix a use of uninitialised memory in an agetty error path.
Date: Fri, 17 Nov 2017 08:44:33 -0800 [thread overview]
Message-ID: <20171117164433.xtsgma2vdppjpio4@adrian> (raw)
get_logname() assumes that when it calls read() it initializes c and
errno, which isn't always true if we hit a whitelisted error or end of
file. This occasionally shows up as agetty going into an infinite
loop. Fix it by just delaying ten seconds and exiting when things go
wrong, similarly to the behavior after a non-whitelisted error.
Signed-off-by: Steven Smith <sos22@srcf.ucam.org>
---
term-utils/agetty.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/term-utils/agetty.c b/term-utils/agetty.c
index 9763fcd30..bc848a25a 100644
--- a/term-utils/agetty.c
+++ b/term-utils/agetty.c
@@ -317,6 +317,7 @@ static void termio_final(struct options *op,
static int caps_lock(char *s);
static speed_t bcode(char *s);
static void usage(void) __attribute__((__noreturn__));
+static void exit_slowly(int code) __attribute__((__noreturn__));
static void log_err(const char *, ...) __attribute__((__noreturn__))
__attribute__((__format__(printf, 1, 2)));
static void log_warn (const char *, ...)
@@ -1983,9 +1984,11 @@ static char *get_logname(struct options *op, struct termios *tp, struct chardata
while (cp->eol == '\0') {
char key;
+ ssize_t readres;
debug("read from FD\n");
- if (read(STDIN_FILENO, &c, 1) < 1) {
+ readres = read(STDIN_FILENO, &c, 1);
+ if (readres < 0) {
debug("read failed\n");
/* The terminal could be open with O_NONBLOCK when
@@ -2000,12 +2003,15 @@ static char *get_logname(struct options *op, struct termios *tp, struct chardata
case ESRCH:
case EINVAL:
case ENOENT:
- break;
+ exit_slowly(EXIT_SUCCESS);
default:
log_err(_("%s: read: %m"), op->tty);
}
}
+ if (readres == 0)
+ exit_slowly(EXIT_SUCCESS);
+
/* Do parity bit handling. */
if (eightbit)
ascval = c;
@@ -2317,6 +2323,13 @@ static void dolog(int priority, const char *fmt, va_list ap)
#endif /* USE_SYSLOG */
}
+static void exit_slowly(int code)
+{
+ /* Be kind to init(8). */
+ sleep(10);
+ exit(code);
+}
+
static void log_err(const char *fmt, ...)
{
va_list ap;
@@ -2325,9 +2338,7 @@ static void log_err(const char *fmt, ...)
dolog(LOG_ERR, fmt, ap);
va_end(ap);
- /* Be kind to init(8). */
- sleep(10);
- exit(EXIT_FAILURE);
+ exit_slowly(EXIT_FAILURE);
}
static void log_warn(const char *fmt, ...)
--
2.11.0
next reply other threads:[~2017-11-17 17:03 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-11-17 16:44 Steven Smith [this message]
2017-11-20 11:02 ` [PATCH] Fix a use of uninitialised memory in an agetty error path Karel Zak
2017-11-20 19:06 ` Steven Smith
2017-11-21 10:23 ` Karel Zak
2017-11-22 16:15 ` Steven Smith
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171117164433.xtsgma2vdppjpio4@adrian \
--to=sos22@archy.org.uk \
--cc=sos22@srcf.ucam.org \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox