From patchwork Thu Jun 8 21:13:24 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: 103414 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632766qgd; Thu, 8 Jun 2017 14:15:47 -0700 (PDT) X-Received: by 10.99.119.130 with SMTP id s124mr28787141pgc.33.1496956547728; Thu, 08 Jun 2017 14:15:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956547; cv=none; d=google.com; s=arc-20160816; b=zS/+Rp542fxeDMc7mBDNGMRAHZ1YTaD10SnOKGpIqUzK/pIINGW07y/mHr2Cu0sWHb WzxnqEWzoxTKWZmOW5dSsuTXOWNmxdePPlQsWANGkSxirGDapFXavnuzpNycedewMbYQ 4t6YjdO8z/LCdOATvIZ/XCVpReVt8d1thsL4fX3HEi3B2DmxUjcOWIWqYovDQrRanrWU xDCnmkaFsGpVFCh6OuBMuV5DuYSf+WnxIokJArHJfu75DZsLuZNOrrsu2S/NB846onMd 0hf9TtPZCBcRKJwuPQ7nu+qQpDeKyqnkAdbO3PZQvJmZViRCuTkkNOSC99rtM/wGPab1 43Ug== 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=hWLPhNGA1OphlblzJZTf7Ncaug5wYh6ew27rVHeNLWc=; b=rTYdZYNAT0KlYqer2ihLnV+Udvm9WhGPQTVmOGrmXafgpZ46W6Gr/zwwf8lnAm7tfg 71c/CDRc7DvbDP+ZPb2itqbonz9zWsSSxqdF4S9tBCwDX4+H/YYJnORBovs3uzmGUxyS np+t1PUENWG9NzASTxMsyy/bQjH0CVUVOgZYVePLe6/QynOJzR0rxW9ty97g/QTo5knd vEKZQKQJ+TvY74FHBufDpQLn5Du7lESUwXw0Xsy3TldkXAJrR0rAnE6vmkp5FEychATV 28ds+mbf3UQLBFWTupD0Nbl2Zg0TFB+4JVatqlew5O5ILZqd9RcKq9DPCjO2caTBLlmB 4q1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80208-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 31si5168333ply.191.2017.06.08.14.15.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:47 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80208-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-80208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80208-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=u+dZ7Sl724SyNaUSWh4CbT43WN3rNTb SQq3MABymyISaqRXbT/BjTJJYKz4rNIOUzTCrpJtt0S52Z2/3yQK9dRQI2Ow+s8v s0SeLa38F69WUGQe5i4MzcrrqOoOb9sMZZbnRLtwtUCilwmKpVQ7xJuVsvP4J1QT f+3DkpOPuARE= 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=My+C6GlA4pGmdiMqIaicPu6K6jo=; b=E71gD aW8qooyTH3ETQ/zsCr7/k5FJpQFidYGrL4tMOJ5UjQ56RYSRi8yGoJhwDZGJ2nAa UqLiGAW7nJYUreGcGNOTEXGYhr1Z8qgFirVRddFaJv95KTzWTnwpj2AyNLnjIj7e na8zbEgUt7HfHWPHFpnwXsFvoaXoq1WhUQhkLI= Received: (qmail 39712 invoked by alias); 8 Jun 2017 21:14:09 -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 39522 invoked by uid 89); 8 Jun 2017 21:14:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f173.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=hWLPhNGA1OphlblzJZTf7Ncaug5wYh6ew27rVHeNLWc=; b=k5S/K/KHYtowqqnMQGqzMedm5OG+KcS4UPTObN3/mnXSkW0tZAFMGooh3JzEmLiPB/ udffOdAAxaGxyL/cAjuSX5+fAGd+M16iI1+wptxrpCkap8ynscbGux4rOLTusp81KNwV ok/Js580Sq7gPD9Dud3g+ZHO+rqo2UWPglYqTZ+L9eHMu+IwwQzK0xzn9IvAzDQ+YG/8 kL4JhUwP5db64tlU9PyhZ9klcjAX6q8yYFbnlSDuLkSnj9HlIATVWKZjajRVZFgkv21C hH+g4UDbDdEBcFuaEMKoLyHOy4JR0+8TiUc0bZXSThBCdueVuabDL7A7xXWGH6rlkl5Q j/iQ== X-Gm-Message-State: AODbwcCaqvrG0m3y6rfbkO/0AFvonvhCefUbMz5/0n/JIPqjoV78OUZy 3DU1lvZpnFayPAM7iEpuKw== X-Received: by 10.237.37.74 with SMTP id w10mr42161408qtc.14.1496956447525; Thu, 08 Jun 2017 14:14:07 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 10/17] posix: User LOGIN_NAME_MAX for all user name in glob Date: Thu, 8 Jun 2017 18:13:24 -0300 Message-Id: <1496956411-25594-11-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-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 | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 14a502c..9e3050a 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -754,8 +754,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { char *dirnamestr = char_array_at (&dirname, 0); char *end_name = strchr (dirnamestr, '/'); - char *user_name; - int malloc_user_name = 0; + char user_name[LOGIN_NAME_MAX]; char *unescape = NULL; if (!(flags & GLOB_NOESCAPE)) @@ -770,26 +769,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), unescape = memchr (dirnamestr, '\\', end_name - dirnamestr); } if (end_name == NULL) - user_name = dirnamestr + 1; + strncpy (user_name, dirnamestr + 1, LOGIN_NAME_MAX - 1); else { - char *newp; - if (glob_use_alloca (alloca_used, end_name - dirnamestr)) - newp = alloca_account (end_name - dirnamestr, alloca_used); - else - { - newp = malloc (end_name - dirnamestr); - if (newp == NULL) - { - retval = GLOB_NOSPACE; - goto out; - } - malloc_user_name = 1; - } if (unescape != NULL) { - char *p = mempcpy (newp, dirnamestr + 1, - unescape - dirnamestr - 1); + ptrdiff_t name_len = unescape - dirnamestr - 1; + name_len = MIN (name_len, LOGIN_NAME_MAX - 1); + char *p = mempcpy (user_name, dirnamestr + 1, name_len); char *q = unescape; while (*q != '\0') { @@ -811,9 +798,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), *p = '\0'; } else - *((char *) mempcpy (newp, dirnamestr + 1, end_name - dirnamestr)) - = '\0'; - user_name = newp; + { + ptrdiff_t name_len = end_name - dirnamestr; + name_len = MIN (name_len, LOGIN_NAME_MAX - 1); + *((char *) mempcpy (user_name, dirnamestr + 1, name_len)) + = '\0'; + } } /* Look up specific user's home directory. */ @@ -845,9 +835,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) {