linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Breno Leitao <leitao@debian.org>
To: linuxppc-dev@lists.ozlabs.org
Cc: Breno Leitao <leitao@debian.org>,
	Segher Boessenkool <segher@kernel.crashing.org>,
	Anshuman Khandual <khandual@linux.vnet.ibm.com>
Subject: [PATCH v2] selftests/powerpc: Fix strncpy usage
Date: Mon, 25 Jun 2018 18:30:49 -0300	[thread overview]
Message-ID: <1529962249-24774-1-git-send-email-leitao@debian.org> (raw)
In-Reply-To: <1529535071-14555-1-git-send-email-leitao@debian.org>

There is a buffer overflow in dscr_inherit_test.c test. In main(), strncpy()'s
third argument is the length of the source, not the size of the destination
buffer, which makes strncpy() behaves like strcpy(), causing a buffer overflow
if argv[0] is bigger than LEN_MAX (100).

This patch allocates 'prog' according to the argv[0] length, avoiding LEN_MAX
restriction.

CC: Segher Boessenkool <segher@kernel.crashing.org>
CC: Anshuman Khandual <khandual@linux.vnet.ibm.com>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c b/tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c
index 08a8b95e3bc1..ecac4900c7dd 100644
--- a/tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c
+++ b/tools/testing/selftests/powerpc/dscr/dscr_inherit_exec_test.c
@@ -19,7 +19,7 @@
  */
 #include "dscr.h"
 
-static char prog[LEN_MAX];
+static char *prog;
 
 static void do_exec(unsigned long parent_dscr)
 {
@@ -104,6 +104,13 @@ int main(int argc, char *argv[])
 		exit(1);
 	}
 
-	strncpy(prog, argv[0], strlen(argv[0]));
+	prog = malloc(strlen(argv[0]) + 1);
+	if (prog == NULL) {
+		fprintf(stderr, "Unable to allocate enough memory\n");
+		exit(1);
+	}
+
+	strcpy(prog, argv[0]);
+
 	return test_harness(dscr_inherit_exec, "dscr_inherit_exec_test");
 }
-- 
2.16.3

  parent reply	other threads:[~2018-06-25 21:30 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-20 22:51 [PATCH] selftests/powerpc: Fix strncpy usage Breno Leitao
2018-06-21 23:18 ` Segher Boessenkool
2018-06-22 14:43   ` Breno Leitao
2018-06-22 14:51     ` Christophe LEROY
2018-06-23  1:00       ` Segher Boessenkool
2018-06-22 15:15     ` Paul Clarke
2018-06-22 21:01       ` Al Dunsmuir
2018-06-23  1:10     ` Segher Boessenkool
2018-06-25 21:21       ` Breno Leitao
2018-06-25 21:30 ` Breno Leitao [this message]
2018-06-26  5:24   ` [PATCH v2] " Michael Ellerman
2018-06-26 13:13     ` Breno Leitao
2018-06-26 13:20 ` [PATCH v3 1/2] " Breno Leitao
2018-06-26 13:20   ` [PATCH v3 2/2] selftests/powerpc: Fix typos Breno Leitao
2018-07-11 13:24   ` [v3,1/2] selftests/powerpc: Fix strncpy usage Michael Ellerman

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=1529962249-24774-1-git-send-email-leitao@debian.org \
    --to=leitao@debian.org \
    --cc=khandual@linux.vnet.ibm.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=segher@kernel.crashing.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).