From patchwork Fri Aug 11 14:50:33 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 109887 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp975628obb; Fri, 11 Aug 2017 07:52:11 -0700 (PDT) X-Received: by 10.99.96.216 with SMTP id u207mr15288049pgb.423.1502463131396; Fri, 11 Aug 2017 07:52:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502463131; cv=none; d=google.com; s=arc-20160816; b=cjSg9gdrG/IwI3t6spoZ/z0ER0GGEAQ8xn7qPZwXWNITOVp3gIRZtfvl+AdKAI5GPT hA4D39+DzAY9GmFPp0Tqqmh47G63famIV+NkvVDWm5YtxBY/6PjNNMVQgwyMmq+NN7Ec LlCgGRF/1E6WWjfOF3EPw9PeGrBDVOC3BT9P99WoGW1i0iEzypcFX84kejDk94Nne7ED B48EIvc+3O5I+dVzj2Xb9ROEdWuE5iTjch+nbqHg9TLKyZZ9xptq+EKwy17avrdZ1Lm7 Fq3Lo2+riofn2vd5fQfhh9jqClwKRvle4qFpjbY991AeHooz7M2cXZpHRIp5LgKQ51iM yTfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=hgI8mCdZO7niRZO5Xi/yg0LrO/UhsGjpAf/EX563VkM=; b=NkfTLzlljZmNvfD+QlHXJDv56vHHGiPQ8oEZWxAFgKO7aLQfN9xOd0qocC2j1fNuz8 QDfZ5vJ0Mm7YVw5fda9zl4RK8LOnydjnIiE4DLi+0971bMdWEvUfk9md1yvLF+vu1pV7 OLD7yzFtacQCWdCOUUfxvVvSe/YfzMkqsK+uwaJVD+WHwhUvrQlJj5pGZAipthVqOEl4 davPDAVy1vRB/hkGncDjLa2BAk4xoJGoGn5mLn/xYKZ21fs35EPlmG8keWHWZSRC1rjm TrebtqhfhCck8FtxVAu3FQnqHL4N9vSQs6IhROyuNfNJBd4SwNwtXM65VFw7Yn2rjtDl nk/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=t/DS8eam; spf=pass (google.com: domain of libc-alpha-return-83029-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83029-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id b4si657724plb.264.2017.08.11.07.52.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Aug 2017 07:52:11 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83029-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 header.s=default header.b=t/DS8eam; spf=pass (google.com: domain of libc-alpha-return-83029-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83029-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=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:in-reply-to :references; q=dns; s=default; b=LmTua5GhRe5GHPOzD85sbbrkJjZXjO9 aVGCHUBeAoiqiu6sQIpu2DpjMhjMfinH8V9vVnxCphpHPp4oGoRxvQaphkon1XTm 1oKs8eB7M43cSR8s2bL7YMFofTlU0dX1KTRT48VS1EvxnpTOK/5NGxjnVWbGIWkY VlWGFtH+dSP0= 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=VRFrTQdXlb7rsCnAIEcgWHYBzuA=; b=t/DS8 eamnpgFPMQjXIgiOqwjOj7ugWrBrB2PtQWybpStp54yt8WOabYR+AACa3gKw2urB aoIfChWLHD/OzZ75hD+4zpdVcROaeYdA+x3ALizJcDW8AXY/HsoAPU4WM7MHLboM i5p7ap/qhI8qeVSNTk2jIFY7G3eIGHWDPXgbtQ= Received: (qmail 72197 invoked by alias); 11 Aug 2017 14:51:03 -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 71818 invoked by uid 89); 11 Aug 2017 14:51:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:2497 X-HELO: mail-qt0-f174.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=hgI8mCdZO7niRZO5Xi/yg0LrO/UhsGjpAf/EX563VkM=; b=XQIrqWucOgrnXwYMlrnms+q7wVKkBKjf7PEKM/X50cSOCv5GsYH7vqYxmU6/l/BXlo 4DwySL2m51bdQj/SXtk9ikT+Kk/T2MkPdCv5/m3IBkQiztkW4Id2lUZb0yNtGZOYHhRw 81hTU4NpBhxAzTiZyQjdikCOFNsZW2vP8+Q+jbKhT0nxuqY9fF2DOXCIp5R92wbDzTpL OyJDJR7K+tPaogLGBwAWGvd5NEpJNIbWhWVHpfkcGYJIhJ2yCFMZxsV7SQ2lmPK4upo2 KcWdegXZegfjpzti8wuoTDv3ufi+bzsRTOUojetKG5ML3Q7u0MJVn2HQ0Q3I6+lKjDdj BHrA== X-Gm-Message-State: AHYfb5it+PpTHXY8Vb/Sz66JXRzi+3jpEyq2mQFxhjEM9dVSBK5dPglA Ravw/pwynmbqHi54UM1UCA== X-Received: by 10.237.36.111 with SMTP id s44mr22908231qtc.317.1502463059616; Fri, 11 Aug 2017 07:50:59 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 07/18] posix: User LOGIN_NAME_MAX for all user names in glob Date: Fri, 11 Aug 2017 11:50:33 -0300 Message-Id: <1502463044-4042-8-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1502463044-4042-1-git-send-email-adhemerval.zanella@linaro.org> References: <1502463044-4042-1-git-send-email-adhemerval.zanella@linaro.org> This patch limits all user name obtained for GLOB_TILDE to max of LOGIN_NAME_MAX (256 on glibc) and remove all stack/malloc buffer handling boilerplate. Checked on x86_64-linux-gnu. * posix/glob.c (glob): Remove alloca usage on user_name for GLOB_TILDE. --- posix/glob.c | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 3a74758..554d558 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -775,8 +776,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), else { char *end_name = strchr (dirname, '/'); - char *user_name; - int malloc_user_name = 0; + char user_name[LOGIN_NAME_MAX]; char *unescape = NULL; if (!(flags & GLOB_NOESCAPE)) @@ -791,26 +791,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), unescape = memchr (dirname, '\\', end_name - dirname); } if (end_name == NULL) - user_name = dirname + 1; + strncpy (user_name, dirname + 1, LOGIN_NAME_MAX - 1); else { - char *newp; - if (glob_use_alloca (alloca_used, end_name - dirname)) - newp = alloca_account (end_name - dirname, alloca_used); - else - { - newp = malloc (end_name - dirname); - if (newp == NULL) - { - retval = GLOB_NOSPACE; - goto out; - } - malloc_user_name = 1; - } if (unescape != NULL) { - char *p = mempcpy (newp, dirname + 1, - unescape - dirname - 1); + ptrdiff_t name_len = unescape - dirname - 1; + name_len = MIN (name_len, LOGIN_NAME_MAX - 1); + char *p = mempcpy (user_name, dirname + 1, name_len); char *q = unescape; while (*q != '\0') { @@ -832,9 +820,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), *p = '\0'; } else - *((char *) mempcpy (newp, dirname + 1, end_name - dirname)) - = '\0'; - user_name = newp; + { + ptrdiff_t name_len = end_name - dirname; + name_len = MIN (name_len, LOGIN_NAME_MAX - 1); + *((char *) mempcpy (user_name, dirname + 1, name_len)) + = '\0'; + } } /* Look up specific user's home directory. */ @@ -866,9 +857,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), p = getpwnam (user_name); # endif - if (__glibc_unlikely (malloc_user_name)) - free (user_name); - /* If we found a home directory use this. */ if (p != NULL) {