util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tobias Stoeckmann <tobias@stoeckmann.org>
To: util-linux@vger.kernel.org
Subject: [PATCH 1/1] sfdisk: Avoid out of boundary read with readline
Date: Sun, 17 Feb 2019 22:55:29 +0100	[thread overview]
Message-ID: <20190217215529.zh3rn57hqu7rruze@localhost> (raw)

It is not guaranteed that the returned string of readline() actually
contains as many bytes as buf can contain.

If bufsz is larger than the allocated memory by readline, an out of
boundary read occurs and leads to undefined behaviour. Most likely
that will be a crash.

This can be reproduced when readline-support is compiled in and when
you directly enter "quit" and "n" (to not write changes back to disk)
when sfdisk was called with any given device.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
 disk-utils/sfdisk.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c
index 3911dda85..52ccc5251 100644
--- a/disk-utils/sfdisk.c
+++ b/disk-utils/sfdisk.c
@@ -133,7 +133,9 @@ static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
 		p = readline(prompt);
 		if (!p)
 			return 1;
-		memcpy(buf, p, bufsz);
+		strncpy(buf, p, bufsz);
+		if (bufsz != 0)
+			buf[bufsz - 1] = '\0';
 		free(p);
 	} else
 #endif
-- 
2.20.1


             reply	other threads:[~2019-02-17 21:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-17 21:55 Tobias Stoeckmann [this message]
2019-02-18 12:06 ` [PATCH 1/1] sfdisk: Avoid out of boundary read with readline Karel Zak

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=20190217215529.zh3rn57hqu7rruze@localhost \
    --to=tobias@stoeckmann.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;
as well as URLs for NNTP newsgroup(s).