From patchwork Wed Nov 23 13:38:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 83673 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp2640421qge; Wed, 23 Nov 2016 05:38:38 -0800 (PST) X-Received: by 10.99.127.72 with SMTP id p8mr5233483pgn.183.1479908318199; Wed, 23 Nov 2016 05:38:38 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id j130si3067353pgc.309.2016.11.23.05.38.37 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Nov 2016 05:38:38 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-75079-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-75079-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-75079-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; q=dns; s= default; b=qV6MBT/71MwD+JL2wRBr0rRXw9Ihg/79a5V1Jf9b0H5WZ4Ypg0Phb eopWIWm6R0YvNqxuX4M3Z3VYz7o1ePhuWytw6B71awtxN9Az34HkCmVfxaVJI+YO hOcuRWWGr01/SvQeXdUVhcTNHtod1tr8Ke0dz/grmkHGUU8vvPaRpI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id; s=default; bh=ZsIe5I8lZTiR4Q5+0GNwaMMsrvI=; b=LQytIoCFq+fNr8t4m3vrP8E9VXo7 2/45LM5w7ZA5/YxCDmQIdUKX051CjQYEGBReNueeSb91y4vIuPKD9dNe+qV7Ka6m 3thLdOKvgJ+SIF8Gu2vlCbh/TJc1Sch5BJnwV6uBkW67rsn3MgUcXhTPa/yAFmS4 oVHbJymUbiEY8so= Received: (qmail 130397 invoked by alias); 23 Nov 2016 13:38:27 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 130363 invoked by uid 89); 23 Nov 2016 13:38:26 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=*contents, (unknown), states X-HELO: mail-vk0-f44.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=VnSPxIVtIU/95hAJJBEfHW0xaLRLNopUhBk5UNrXcPM=; b=d9OSNbKDhJ+2iyZ9gmZ5k7pxGjaKPoF6A0Ym9jCJ2a2lXKOtgtE23AT+EdpdkuvWTr fsZOfl218u657RjB5IJ0w+mpo0Zxdo7MksJIX0H50R5BCJ3De2W99CFS42DJC90rFxOE bGR7SoJ5ZzvKV6lJiXgrqcQJdnck7OuNeHqUFZUGN6MR2urZT0D8Bpz1hvUR5f77bCzq DjH34FtdVx4Pg0XdSm15UK5aJqa9dfCopr3cSqntp6H7lAKij/2H2GSkSHn072of3/s7 JTTQcaDvrVsYHsaAlbikIJYij+ZsBFE8ksP6vCdhyfE59eq3LJ2F3I5sNxG0Iur168SS ZNww== X-Gm-Message-State: AKaTC02MnR9Im4tRGOJugLuLtDQSsou9PyscTESvCDxm2J/JhrBRXC5egSqB1vFnkgx8cAs1 X-Received: by 10.31.11.145 with SMTP id 139mr1123554vkl.141.1479908293843; Wed, 23 Nov 2016 05:38:13 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v3] Fix writes past the allocated array bounds in execvpe (BZ#20847) Date: Wed, 23 Nov 2016 11:38:05 -0200 Message-Id: <1479908285-7461-1-git-send-email-adhemerval.zanella@linaro.org> Commit 6c9e1be87a37bf wrongly fixes BZ#20847 by lefting the else branch on maybe_script_execute to still being able to invalid write on stack allocated buffer. It happens if execvp{e} is executed with an empty arguments list ({ NULL }) and although manual states first argument should be the script name itself, by convention, old and current implementation allows it. This patch fixes the issue by just account for arguments and not the final 'NULL' (since the 'argv + 1' will indeed ignored the script name). The empty argument list is handled in a special case with a minimum allocated size. The patch also adds extra tests for such case in tst-vfork3. Tested on x86_64. [BZ #20847] * posix/execvpe.c (maybe_script_execute): Remove write past allocated array bounds for else branch. (__execvpe): Style fixes. * posix/tst-vfork3.c (run_script): New function. (create_script): Likewise. (do_test): Use run_script internal function. (do_prepare): Use create_script internal function. --- ChangeLog | 12 ++++ posix/execvpe.c | 19 ++++-- posix/tst-vfork3.c | 185 +++++++++++++++++++---------------------------------- 3 files changed, 91 insertions(+), 125 deletions(-) -- 2.7.4 diff --git a/posix/execvpe.c b/posix/execvpe.c index 7cdb06a..a2d0145 100644 --- a/posix/execvpe.c +++ b/posix/execvpe.c @@ -38,8 +38,8 @@ static void maybe_script_execute (const char *file, char *const argv[], char *const envp[]) { - ptrdiff_t argc = 0; - while (argv[argc++] != NULL) + ptrdiff_t argc; + for (argc = 0; argv[argc] != NULL; argc++) { if (argc == INT_MAX - 1) { @@ -48,13 +48,18 @@ maybe_script_execute (const char *file, char *const argv[], char *const envp[]) } } - /* Construct an argument list for the shell. It will contain at minimum 3 - arguments (current shell, script, and an ending NULL. */ - char *new_argv[argc + 1]; + /* Construct an argument list for the shell based on original arguments: + 1. Empty list (argv = { NULL }, argc = 1 }: new argv will contain 3 + arguments - default shell, script to execute, and ending NULL. + 2. Non empty argument list (argc = { ..., NULL }, argc > 1}: new argv + will contain also the default shell and the script to execute. It + will also skip the script name in arguments and only copy script + arguments. */ + char *new_argv[argc > 1 ? 2 + argc : 3]; new_argv[0] = (char *) _PATH_BSHELL; new_argv[1] = (char *) file; if (argc > 1) - memcpy (new_argv + 2, argv + 1, (argc - 1) * sizeof(char *)); + memcpy (new_argv + 2, argv + 1, argc * sizeof(char *)); else new_argv[2] = NULL; @@ -96,7 +101,7 @@ __execvpe (const char *file, char *const argv[], char *const envp[]) size_t path_len = __strnlen (path, PATH_MAX - 1) + 1; /* NAME_MAX does not include the terminating null character. */ - if (((file_len-1) > NAME_MAX) + if ((file_len - 1 > NAME_MAX) || !__libc_alloca_cutoff (path_len + file_len + 1)) { errno = ENAMETOOLONG; diff --git a/posix/tst-vfork3.c b/posix/tst-vfork3.c index 05edc5a..f5fe5c3 100644 --- a/posix/tst-vfork3.c +++ b/posix/tst-vfork3.c @@ -33,84 +33,64 @@ char *tmpdirname; #define PREPARE(argc, argv) do_prepare () #include "../test-skeleton.c" -static int -do_test (void) +static void +run_script (const char *script, char *const argv[]) { - mtrace (); - - const char *path = getenv ("PATH"); - if (path == NULL) - path = "/bin"; - char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1]; - strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path); - if (setenv ("PATH", pathbuf, 1) < 0) - { - puts ("setenv failed"); - return 1; - } - - size_t i; - char *argv[3] = { (char *) "script1.sh", (char *) "1", NULL }; - for (i = 0; i < 5; i++) + for (size_t i = 0; i < 5; i++) { pid_t pid = vfork (); if (pid < 0) - { - printf ("vfork failed: %m\n"); - return 1; - } + FAIL_EXIT1 ("vfork failed: %m"); else if (pid == 0) { - execvp ("script1.sh", argv); + execvp (script, argv); _exit (errno); } + int status; if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) - { - puts ("waitpid failed"); - return 1; - } + FAIL_EXIT1 ("waitpid failed"); else if (status != 0) { if (WIFEXITED (status)) - printf ("script1.sh failed with status %d\n", - WEXITSTATUS (status)); + FAIL_EXIT1 ("%s failed with status %d\n", script, + WEXITSTATUS (status)); else - printf ("script1.sh kill by signal %d\n", - WTERMSIG (status)); - return 1; + FAIL_EXIT1 ("%s killed by signal %d\n", script, + WTERMSIG (status)); } } +} - argv[0] = (char *) "script2.sh"; - argv[1] = (char *) "2"; - for (i = 0; i < 5; i++) +static int +do_test (void) +{ + mtrace (); + + const char *path = getenv ("PATH"); + if (path == NULL) + path = "/bin"; + char pathbuf[strlen (tmpdirname) + 1 + strlen (path) + 1]; + strcpy (stpcpy (stpcpy (pathbuf, tmpdirname), ":"), path); + if (setenv ("PATH", pathbuf, 1) < 0) { - pid_t pid = vfork (); - if (pid < 0) - { - printf ("vfork failed: %m\n"); - return 1; - } - else if (pid == 0) - { - execvp ("script2.sh", argv); - _exit (errno); - } - int status; - if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) - { - puts ("waitpid failed"); - return 1; - } - else if (status != 0) - { - printf ("script2.sh failed with status %d\n", status); - return 1; - } + puts ("setenv failed"); + return 1; } - for (i = 0; i < 5; i++) + char *argv00[] = { NULL }; + run_script ("script0.sh", argv00); + char *argv01[] = { (char*) "script0.sh", NULL }; + run_script ("script0.sh", argv01); + + char *argv1[] = { (char *) "script1.sh", (char *) "1", NULL }; + run_script ("script1.sh", argv1); + + char *argv2[] = { (char *) "script2.sh", (char *) "2", NULL }; + run_script ("script2.sh", argv2); + + /* Same as before but with execlp. */ + for (size_t i = 0; i < 5; i++) { pid_t pid = vfork (); if (pid < 0) @@ -137,87 +117,56 @@ do_test (void) } unsetenv ("PATH"); - argv[0] = (char *) "echo"; - argv[1] = (char *) "script 4"; - for (i = 0; i < 5; i++) - { - pid_t pid = vfork (); - if (pid < 0) - { - printf ("vfork failed: %m\n"); - return 1; - } - else if (pid == 0) - { - execvp ("echo", argv); - _exit (errno); - } - int status; - if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) - { - puts ("waitpid failed"); - return 1; - } - else if (status != 0) - { - printf ("echo failed with status %d\n", status); - return 1; - } - } + char *argv4[] = { (char *) "echo", (char *) "script 4", NULL }; + run_script ("echo", argv4); return 0; } static void +create_script (const char *script, const char *contents, size_t size) +{ + int fd = open (script, O_WRONLY | O_CREAT, 0700); + if (fd < 0 + || TEMP_FAILURE_RETRY (write (fd, contents, size)) != size + || fchmod (fd, S_IRUSR | S_IXUSR) < 0) + FAIL_EXIT1 ("could not write %s\n", script); + close (fd); +} + +static void do_prepare (void) { size_t len = strlen (test_dir) + sizeof ("/tst-vfork3.XXXXXX"); tmpdirname = malloc (len); - char *script1 = malloc (len + sizeof "/script1.sh"); - char *script2 = malloc (len + sizeof "/script2.sh"); - if (tmpdirname == NULL || script1 == NULL || script2 == NULL) - { - puts ("out of memory"); - exit (1); - } + if (tmpdirname == NULL) + FAIL_EXIT1 ("out of memory"); strcpy (stpcpy (tmpdirname, test_dir), "/tst-vfork3.XXXXXX"); tmpdirname = mkdtemp (tmpdirname); if (tmpdirname == NULL) - { - puts ("could not create temporary directory"); - exit (1); - } + FAIL_EXIT1 ("could not create temporary directory"); + + char script0[len + sizeof "/script0.sh"]; + char script1[len + sizeof "/script1.sh"]; + char script2[len + sizeof "/script2.sh"]; + strcpy (stpcpy (script0, tmpdirname), "/script0.sh"); strcpy (stpcpy (script1, tmpdirname), "/script1.sh"); strcpy (stpcpy (script2, tmpdirname), "/script2.sh"); - /* Need to make sure tmpdirname is at the end of the linked list. */ + add_temp_file (script0); add_temp_file (script1); - add_temp_file (tmpdirname); add_temp_file (script2); + /* Need to make sure tmpdirname is at the end of the linked list. */ + add_temp_file (tmpdirname); + + const char content0[] = "#!/bin/sh\necho empty\n"; + create_script (script0, content0, sizeof content0); const char content1[] = "#!/bin/sh\necho script $1\n"; - int fd = open (script1, O_WRONLY | O_CREAT, 0700); - if (fd < 0 - || TEMP_FAILURE_RETRY (write (fd, content1, sizeof content1)) - != sizeof content1 - || fchmod (fd, S_IRUSR | S_IXUSR) < 0) - { - printf ("Could not write %s\n", script1); - exit (1); - } - close (fd); + create_script (script1, content1, sizeof content1); const char content2[] = "echo script $1\n"; - fd = open (script2, O_WRONLY | O_CREAT, 0700); - if (fd < 0 - || TEMP_FAILURE_RETRY (write (fd, content2, sizeof content2)) - != sizeof content2 - || fchmod (fd, S_IRUSR | S_IXUSR) < 0) - { - printf ("Could not write %s\n", script2); - exit (1); - } - close (fd); + create_script (script2, content2, sizeof content2); }