From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by sourceware.org (Postfix) with ESMTPS id 81C7A3858C39 for ; Wed, 6 Oct 2021 19:13:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81C7A3858C39 Received: by mail-qv1-xf30.google.com with SMTP id z15so2619319qvj.7 for ; Wed, 06 Oct 2021 12:13:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:from:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=qRwqKl5MDJuvHUMeiSjdq32tXvWpyglkdMOmv/TAONo=; b=nru9Ub8Zfr35i4ShntsGVeSJJTsw2//X6wyyW8b7S6IFMrSUywCb22GfZDihldQKur We5jka50GYsBVrDlkKKzkWpZ9zAHbcicN/rM8MpM7fNPs2Ser9+c7fTGb8sqDnL8LGW1 fdUf7tfZutHuP2rvjP0+CGkM+N+VU5ScwO1UAEGz+lFFCfWR3MQaJ4jmGRRAOS9fOQ1T vXGc+UGGOQfcmviNYD20AxNcUALe+LmLg2ZWGlOb919DRkhg2imscx8aI2JZI9Oohz5/ KYeg9WYWGNjclA0X6k+wRj/UxtQb9tT5iHqr6+pkwyCzeeQrBl/7fqt6DrGUll1hUzt9 g6xQ== X-Gm-Message-State: AOAM531N6C6mFeSgYAPzGE4Javd/+qLhuTjR1FRePs9Wf+JYnFykFDHC m5P82/B7RZ0Yh4Yr9JMR5RNTwkxT2OqAdA== X-Google-Smtp-Source: ABdhPJxavnjD8yxpJ45PcgV6XPpQTW8GGS0CD+DKT8JnxN/5FOr+el7aqpu9+AHPz3IXLZoS9pdYNw== X-Received: by 2002:a0c:b30f:: with SMTP id s15mr34537661qve.23.1633547622012; Wed, 06 Oct 2021 12:13:42 -0700 (PDT) Received: from ?IPv6:2804:431:c7cb:807a:2ebe:4b13:27bd:f11d? ([2804:431:c7cb:807a:2ebe:4b13:27bd:f11d]) by smtp.gmail.com with ESMTPSA id b19sm1391082qto.46.2021.10.06.12.13.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Oct 2021 12:13:41 -0700 (PDT) Subject: Re: [PATCH v6] io: Fix ftw internal realloc buffer (BZ #28126) From: Adhemerval Zanella To: libc-alpha@sourceware.org References: <20210927152613.1551692-1-adhemerval.zanella@linaro.org> Message-ID: Date: Wed, 6 Oct 2021 16:13:40 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20210927152613.1551692-1-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.8 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, 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: Wed, 06 Oct 2021 19:13:45 -0000 Ping. On 27/09/2021 12:26, 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. > --- > 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 >