From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from eastern.birch.relay.mailchannels.net (eastern.birch.relay.mailchannels.net [23.83.209.55]) by sourceware.org (Postfix) with ESMTPS id 7C5F53858C39 for ; Thu, 7 Oct 2021 09:20:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7C5F53858C39 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 8851A2E0DFC; Thu, 7 Oct 2021 09:20:11 +0000 (UTC) Received: from pdx1-sub0-mail-a74.g.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 5DBDB2E0CA3; Thu, 7 Oct 2021 09:20:00 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a74.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.99.42.170 (trex/6.4.3); Thu, 07 Oct 2021 09:20:11 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Tart-Reaction: 30afafda0403987a_1633598401134_1240259133 X-MC-Loop-Signature: 1633598401134:571352619 X-MC-Ingress-Time: 1633598401133 Received: from pdx1-sub0-mail-a74.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a74.g.dreamhost.com (Postfix) with ESMTP id C331484144; Thu, 7 Oct 2021 02:19:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gotplt.org; h=message-id :date:mime-version:subject:to:references:from:in-reply-to :content-type:content-transfer-encoding; s=gotplt.org; bh=ozZM+p cSUty20Bf3uaaxI9Ea4n4=; b=f8ZtGa9GcPXom1KVZnjbngSccdIkIfJX680Gm+ tRc1UFnC7YPNFUqHa6VzyaKA1NnR9CVMKpIyU1HQm8g7o+Cuzt806rascogSfA/L QwJeMf3ojZvC29fAsdmP33Gw0LsnSVxidayDQE/fpFFsfvGo0pYcLPXEqoGYa8Dx iStjY= Received: from [192.168.1.174] (unknown [1.186.223.58]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a74.g.dreamhost.com (Postfix) with ESMTPSA id BFC067F071; Thu, 7 Oct 2021 02:19:57 -0700 (PDT) Message-ID: Date: Thu, 7 Oct 2021 14:49:51 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH v6] io: Fix ftw internal realloc buffer (BZ #28126) Content-Language: en-US To: Adhemerval Zanella , libc-alpha@sourceware.org References: <20210927152613.1551692-1-adhemerval.zanella@linaro.org> X-DH-BACKEND: pdx1-sub0-mail-a74 From: Siddhesh Poyarekar In-Reply-To: <20210927152613.1551692-1-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3040.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 07 Oct 2021 09:20:15 -0000 On 9/27/21 20:56, Adhemerval Zanella wrote: > The 106ff08526d3ca did not take in consideration the buffer might be > reallocated if the total path is larger than PATH_MAX. The realloc > uses 'dirbuf', where 'dirstreams' is the allocated buffer. > > Checked on x86_64-linux-gnu. LGTM. Reviewed-by: Siddhesh Poyarekar > --- > io/Makefile | 1 + > io/ftw.c | 13 +++--- > io/tst-ftw-bz28126.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 106 insertions(+), 5 deletions(-) > create mode 100644 io/tst-ftw-bz28126.c > > diff --git a/io/Makefile b/io/Makefile > index 9871ecbc74..ecf65aba60 100644 > --- a/io/Makefile > +++ b/io/Makefile > @@ -79,6 +79,7 @@ tests := test-utime test-stat test-stat2 test-lfs tst-getcwd \ > tst-futimens \ > tst-utimensat \ > tst-closefrom \ > + tst-ftw-bz28126 > > tests-time64 := \ > tst-futimens-time64 \ > diff --git a/io/ftw.c b/io/ftw.c > index f0db173727..3964c07bbb 100644 > --- a/io/ftw.c > +++ b/io/ftw.c > @@ -391,13 +391,16 @@ process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, > if (data->dirbufsize < new_buflen) > { > /* Enlarge the buffer. */ > - char *newp; > - > - data->dirbufsize = 2 * new_buflen; > - newp = (char *) realloc (data->dirbuf, data->dirbufsize); > + size_t newsize = 2 * new_buflen; > + void *newp = realloc (data->dirstreams, data->maxdir > + * sizeof (struct dir_data *) > + + newsize); > if (newp == NULL) > return -1; > - data->dirbuf = newp; > + data->dirstreams = newp; > + data->dirbufsize = newsize; > + data->dirbuf = (char *) data->dirstreams > + + data->maxdir * sizeof (struct dir_data *); > } > > *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0'; > diff --git a/io/tst-ftw-bz28126.c b/io/tst-ftw-bz28126.c > new file mode 100644 > index 0000000000..94044ab9d1 > --- /dev/null > +++ b/io/tst-ftw-bz28126.c > @@ -0,0 +1,97 @@ > +/* Check if internal buffer reallocation work for large paths (BZ #28126) > + Copyright (C) 2021 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static int > +my_func (const char *file, const struct stat *sb, int flag) > +{ > + return 0; > +} > + > +static const char folder[NAME_MAX] = { [0 ... 253] = 'a', [254] = '\0' }; > + > +#define NSUBFOLDERS 16 > +static int nsubfolders; > + > +static void > +do_cleanup (void) > +{ > + xchdir (".."); > + for (int i = 0; i < nsubfolders; i++) > + { > + remove (folder); > + xchdir (".."); > + } > + remove (folder); > +} > +#define CLEANUP_HANDLER do_cleanup > + > +static void > +check_mkdir (const char *path) > +{ > + int r = mkdir (path, 0777); > + /* Some filesystem such as overlayfs does not support larger path required > + to trigger the internal buffer reallocation. */ > + if (r != 0) > + { > + if (errno == ENAMETOOLONG) > + FAIL_UNSUPPORTED ("the filesystem does not support the required" > + "large path"); > + else > + FAIL_EXIT1 ("mkdir (\"%s\", 0%o): %m", folder, 0777); > + } > +} > + > +static int > +do_test (void) > +{ > + char *tempdir = support_create_temp_directory ("tst-bz28126"); > + > + /* Create path with various subfolders to force an internal buffer > + reallocation within ntfw. */ > + char *path = xasprintf ("%s/%s", tempdir, folder); > + check_mkdir (path); > + xchdir (path); > + free (path); > + for (int i = 0; i < NSUBFOLDERS - 1; i++) > + { > + check_mkdir (folder); > + xchdir (folder); > + nsubfolders++; > + } > + > + TEST_COMPARE (ftw (tempdir, my_func, 20), 0); > + > + free (tempdir); > + > + do_cleanup (); > + > + return 0; > +} > + > +#include >