From patchwork Mon Feb 29 18:33:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 63236 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp1405317lbc; Mon, 29 Feb 2016 10:34:17 -0800 (PST) X-Received: by 10.98.14.2 with SMTP id w2mr5838928pfi.35.1456770857438; Mon, 29 Feb 2016 10:34:17 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id qc8si5379021pac.39.2016.02.29.10.34.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Feb 2016 10:34:17 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-67713-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; spf=pass (google.com: domain of libc-alpha-return-67713-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-67713-patch=linaro.org@sourceware.org; dkim=pass header.i=@sourceware.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:in-reply-to :references; q=dns; s=default; b=FhZH+cVgOnRFiPBNeoISLDub8N0rYZJ 6JGSH9ahHLbbshW0V8Wc171WHNSYCeliWD+DdWil2MbqMhZH5QMiyE2/3b58IkYT IIKH+IWNoKfzUQfNYfEDjvk4X4t0ctSJBNHT52vDipsPkqExkPpk+8lljI87DMYx u94AjYY+igkg= 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:in-reply-to :references; s=default; bh=6yFuMSeTNXp/a6gvaGltcVaet+E=; b=of84A 9XuxM/jc16z83LWNIw98IRQqpMWdyHFQaqPPlXjNXlAigB/ngMIhxfqWRxL5ZWxG L3F7bqWTQ0Qstd3dVb0R8pK1GBuk9Uiv7/LjvrVO7L2bvm5a7NUrvWI53AdTpH8K XzAMw+opi6LotnL9qpHGyaG+/8ddsHIUy2GRJs= Received: (qmail 112062 invoked by alias); 29 Feb 2016 18:33:52 -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 111951 invoked by uid 89); 29 Feb 2016 18:33:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=2632, environ X-HELO: mail-yk0-f176.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:in-reply-to :references; bh=Wk3cVjzRM+z2zdpUMnywUuV7HzGUqV/61JbbIBoBMzM=; b=LKgc7LGcnh7NtfvhGwNRoN3gqLOnwCTiZq+15wZxbmIX7xvGA1WkuXjXU3zZcITH5p yvZOKlkzQN0YjNDkaWSgjS1qQBvFR3BrIz+aO7zkc+6QS3crpMpZwZmYQqqmO7zUoCVc Yf7W0a+zhi7GAs5yhb8HZT3pSrQ6/9yZ+bWVIKCYmedlrXu125JnBZgELqWyco0gS/xY Cb1ZCRGVVYVREeYRUy3jdsQcYIxTTW1u5rP63gYGK7HK847FA7Von5xYj7Tkh/VII5vE C5/8KwYedmxzwoR4d647DS5aa83unN1JffcN3AS7CDd5QpDCKO0wzUYZwN3SgOY2Uk7q V/VA== X-Gm-Message-State: AD7BkJLNf8rOOzvxzSpPWW1Q4RhpzrrlSQ52Dt5wzH47OWQ9smFpVO4UEZdlzfWOvNsx4u1W X-Received: by 10.37.230.208 with SMTP id d199mr9916430ybh.122.1456770828370; Mon, 29 Feb 2016 10:33:48 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 1/3] posix: Remove dynamic memory allocation from execl{e, p} Date: Mon, 29 Feb 2016 15:33:38 -0300 Message-Id: <1456770820-21341-2-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1456770820-21341-1-git-send-email-adhemerval.zanella@linaro.org> References: <1456770820-21341-1-git-send-email-adhemerval.zanella@linaro.org> GLIBC execl{e,p} implementation might use malloc if the total number of arguments exceed initial assumption size (1024). This might lead to issues in two situations: 1. execl/execle is stated to be async-signal-safe by POSIX [1]. However if execl is used in a signal handler with a large argument set (that may call malloc internally) and if the resulting call fails it might lead malloc in the program in a bad state. 2. If the functions are used in a vfork/clone(VFORK) situation it also might issue malloc internal bad state. This patch fixes it by using stack allocation instead. It also fixes BZ#19534. Tested on x86_64. [1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html [BZ #19534] * posix/execl.c (execl): Remove dynamic memory allocation. * posix/execle.c (execle): Likewise. * posix/execlp.c (execlp): Likewise. --- posix/execl.c | 68 ++++++++++++++++++++++---------------------------------- posix/execle.c | 70 +++++++++++++++++++++++----------------------------------- posix/execlp.c | 66 +++++++++++++++++++++--------------------------------- 4 files changed, 86 insertions(+), 125 deletions(-) -- 1.9.1 diff --git a/posix/execl.c b/posix/execl.c index 102d19d..a765b43 100644 --- a/posix/execl.c +++ b/posix/execl.c @@ -16,58 +16,42 @@ . */ #include +#include #include -#include -#include -#include - -#include - +#include /* Execute PATH with all arguments after PATH until a NULL pointer and environment from `environ'. */ int execl (const char *path, const char *arg, ...) { -#define INITIAL_ARGV_MAX 1024 - size_t argv_max = INITIAL_ARGV_MAX; - const char *initial_argv[INITIAL_ARGV_MAX]; - const char **argv = initial_argv; - va_list args; - - argv[0] = arg; - - va_start (args, arg); - unsigned int i = 0; - while (argv[i++] != NULL) + ptrdiff_t argc; + va_list ap; + va_start (ap, arg); + for (argc = 1; va_arg (ap, const char *); argc++) { - if (i == argv_max) + if (argc == INT_MAX) { - argv_max *= 2; - const char **nptr = realloc (argv == initial_argv ? NULL : argv, - argv_max * sizeof (const char *)); - if (nptr == NULL) - { - if (argv != initial_argv) - free (argv); - va_end (args); - return -1; - } - if (argv == initial_argv) - /* We have to copy the already filled-in data ourselves. */ - memcpy (nptr, argv, i * sizeof (const char *)); - - argv = nptr; + va_end (ap); + errno = E2BIG; + return -1; } - - argv[i] = va_arg (args, const char *); } - va_end (args); - - int ret = __execve (path, (char *const *) argv, __environ); - if (argv != initial_argv) - free (argv); - - return ret; + va_end (ap); + + /* Avoid dynamic memory allocation due two main issues: + 1. The function should be async-signal-safe and a running on a signal + handler with a fail outcome might lead to malloc bad state. + 2. It might be used in a vfork/clone(VFORK) scenario where using + malloc also might lead to internal bad state. */ + ptrdiff_t i; + char *argv[argc + 1]; + va_start (ap, arg); + argv[0] = (char *) arg; + for (i = 1; i <= argc; i++) + argv[i] = va_arg (ap, char *); + va_end (ap); + + return __execve (path, argv, __environ); } libc_hidden_def (execl) diff --git a/posix/execle.c b/posix/execle.c index 8edc03a..b292fec 100644 --- a/posix/execle.c +++ b/posix/execle.c @@ -17,57 +17,43 @@ #include #include -#include -#include -#include - -#include +#include +#include /* Execute PATH with all arguments after PATH until a NULL pointer, and the argument after that for environment. */ int execle (const char *path, const char *arg, ...) { -#define INITIAL_ARGV_MAX 1024 - size_t argv_max = INITIAL_ARGV_MAX; - const char *initial_argv[INITIAL_ARGV_MAX]; - const char **argv = initial_argv; - va_list args; - argv[0] = arg; - - va_start (args, arg); - unsigned int i = 0; - while (argv[i++] != NULL) + ptrdiff_t argc; + va_list ap; + va_start (ap, arg); + for (argc = 1; va_arg (ap, const char *); argc++) { - if (i == argv_max) + if (argc == INT_MAX) { - argv_max *= 2; - const char **nptr = realloc (argv == initial_argv ? NULL : argv, - argv_max * sizeof (const char *)); - if (nptr == NULL) - { - if (argv != initial_argv) - free (argv); - va_end (args); - return -1; - } - if (argv == initial_argv) - /* We have to copy the already filled-in data ourselves. */ - memcpy (nptr, argv, i * sizeof (const char *)); - - argv = nptr; + va_end (ap); + errno = E2BIG; + return -1; } - - argv[i] = va_arg (args, const char *); } - - const char *const *envp = va_arg (args, const char *const *); - va_end (args); - - int ret = __execve (path, (char *const *) argv, (char *const *) envp); - if (argv != initial_argv) - free (argv); - - return ret; + va_end (ap); + + /* Avoid dynamic memory allocation due two main issues: + 1. The function should be async-signal-safe and a running on a signal + handler with a fail outcome might lead to malloc bad state. + 2. It might be used in a vfork/clone(VFORK) scenario where using + malloc also might lead to internal bad state. */ + ptrdiff_t i; + char *argv[argc + 1]; + char **envp; + va_start (ap, arg); + argv[0] = (char *) arg; + for (i = 1; i <= argc; i++) + argv[i] = va_arg (ap, char *); + envp = va_arg (ap, char **); + va_end (ap); + + return __execve (path, argv, envp); } libc_hidden_def (execle) diff --git a/posix/execlp.c b/posix/execlp.c index 6700994..77ac7d5 100644 --- a/posix/execlp.c +++ b/posix/execlp.c @@ -17,11 +17,8 @@ #include #include -#include -#include -#include - -#include +#include +#include /* Execute FILE, searching in the `PATH' environment variable if it contains no slashes, with all arguments after FILE until a @@ -29,45 +26,32 @@ int execlp (const char *file, const char *arg, ...) { -#define INITIAL_ARGV_MAX 1024 - size_t argv_max = INITIAL_ARGV_MAX; - const char *initial_argv[INITIAL_ARGV_MAX]; - const char **argv = initial_argv; - va_list args; - - argv[0] = arg; - - va_start (args, arg); - unsigned int i = 0; - while (argv[i++] != NULL) + ptrdiff_t argc; + va_list ap; + va_start (ap, arg); + for (argc = 1; va_arg (ap, const char *); argc++) { - if (i == argv_max) + if (argc == INT_MAX) { - argv_max *= 2; - const char **nptr = realloc (argv == initial_argv ? NULL : argv, - argv_max * sizeof (const char *)); - if (nptr == NULL) - { - if (argv != initial_argv) - free (argv); - va_end (args); - return -1; - } - if (argv == initial_argv) - /* We have to copy the already filled-in data ourselves. */ - memcpy (nptr, argv, i * sizeof (const char *)); - - argv = nptr; + va_end (ap); + errno = E2BIG; + return -1; } - - argv[i] = va_arg (args, const char *); } - va_end (args); - - int ret = execvp (file, (char *const *) argv); - if (argv != initial_argv) - free (argv); - - return ret; + va_end (ap); + + /* Although posix does not state execlp as an async-safe function + it can not use malloc to allocate the arguments since it might + be used in a vfork scenario and it may lead to malloc internal + bad state. */ + ptrdiff_t i; + char *argv[argc + 1]; + va_start (ap, arg); + argv[0] = (char *) arg; + for (i = 1; i <= argc; i++) + argv[i] = va_arg (ap, char *); + va_end (ap); + + return __execvpe (file, argv, __environ); } libc_hidden_def (execlp)