From patchwork Thu Oct 7 11:18:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 45953 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 110B6385841D for ; Thu, 7 Oct 2021 11:18:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 110B6385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633605522; bh=fHlouUu2IU7sqYg8tjleT0fM/iTlLxJRrob5uUW+An0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Rj6Vu4Ae4gdLbeOifuLUZzWRmjLjuhCiav3O1E/uCL8puebhCh5bclARqb10zryFm 9iggWNe2+KrrqUKTYy5Qc12C+4UE2T79Vg/TzmAyOe3IT8SAf7gvXIxWqmX14Gwo6v KsYIZIhMSmyBi6TD4RsWe8UCtzI9Gx0zjxUGzW2k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by sourceware.org (Postfix) with ESMTPS id 813F33858C60 for ; Thu, 7 Oct 2021 11:18:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 813F33858C60 Received: by mail-qk1-x733.google.com with SMTP id q125so5496811qkd.12 for ; Thu, 07 Oct 2021 04:18:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fHlouUu2IU7sqYg8tjleT0fM/iTlLxJRrob5uUW+An0=; b=jMcXiwncxbZPdX+d+Ov/I1VgEDBr9xRzzaKMq9QDf+O6zqZKw/fad83UfLx8xP2oaB cgXWhffgsC6sW5+MxwYCm4+Ehb6YVjJISscNm99xxUZcb2MGCgeWETWKiP63t+4aSGSy NO0Kwn8apIt0yiJV5ZJeOdCQIGOH2Sz0YuW74NgXofLnSqHRpIy6vx0tp8DOFkKbhJZ2 lrsjca0ToZYRCu6cTU0DKT650Vk96UiZHJkl4bwxbdJP0Y+tHjDtiVnZsEFFmZFkSqdj XjIUARFQQTI/2GqLaKa393Ua8KEWcpS78CyU6wpBkliw73xpmPZ+Au7cpWgQ1JdO/tLs M9BQ== X-Gm-Message-State: AOAM531iiirLRnus3ndWDiH6cUrmuKXUHCdCPfOkQ78QfMNWLoIMPCck 7tSUNh+wa/csBtWZkVwICePUdAJ/W6Zyrw== X-Google-Smtp-Source: ABdhPJyu64hxHsp4CBWjP7W4qZNswKTXjyuNysCw8nvz6SGCh0sPsBPmGA3AUrQXL4Ypmt0a2ASY7w== X-Received: by 2002:a37:9481:: with SMTP id w123mr2736783qkd.75.1633605499844; Thu, 07 Oct 2021 04:18:19 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:807a:2864:3aef:e68:8698]) by smtp.gmail.com with ESMTPSA id t17sm15375078qtq.56.2021.10.07.04.18.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Oct 2021 04:18:19 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v7] io: Fix ftw internal realloc buffer (BZ #28126) Date: Thu, 7 Oct 2021 08:18:16 -0300 Message-Id: <20211007111816.2702850-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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. Reviewed-by: H.J. Lu --- v7: Move common ftw_data allocate logic to ftw_allocate(). --- io/Makefile | 1 + io/ftw.c | 39 +++++++++--------- io/tst-ftw-bz28126.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 20 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..fe953454f2 100644 --- a/io/ftw.c +++ b/io/ftw.c @@ -203,6 +203,20 @@ struct ftw_data void *known_objects; }; +static bool +ftw_allocate (struct ftw_data *data, size_t newsize) +{ + void *newp = realloc (data->dirstreams, data->maxdir + * sizeof (struct dir_data *) + + newsize); + if (newp == NULL) + return false; + data->dirstreams = newp; + data->dirbufsize = newsize; + data->dirbuf = (char *) data->dirstreams + + data->maxdir * sizeof (struct dir_data *); + return true; +} /* Internally we use the FTW_* constants used for `nftw'. When invoked as `ftw', map each flag to the subset of values used by `ftw'. */ @@ -388,17 +402,9 @@ process_entry (struct ftw_data *data, struct dir_data *dir, const char *name, return 0; new_buflen = data->ftw.base + namlen + 2; - if (data->dirbufsize < new_buflen) - { - /* Enlarge the buffer. */ - char *newp; - - data->dirbufsize = 2 * new_buflen; - newp = (char *) realloc (data->dirbuf, data->dirbufsize); - if (newp == NULL) - return -1; - data->dirbuf = newp; - } + if (data->dirbufsize < new_buflen + && !ftw_allocate (data, 2 * new_buflen)) + return -1; *((char *) __mempcpy (data->dirbuf + data->ftw.base, name, namlen)) = '\0'; @@ -628,7 +634,7 @@ __attribute ((noinline)) ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, int flags) { - struct ftw_data data; + struct ftw_data data = { .dirstreams = NULL }; struct STRUCT_STAT st; int result = 0; int save_err; @@ -646,16 +652,9 @@ ftw_startup (const char *dir, int is_nftw, void *func, int descriptors, data.maxdir = descriptors < 1 ? 1 : descriptors; data.actdir = 0; /* PATH_MAX is always defined when we get here. */ - data.dirbufsize = MAX (2 * strlen (dir), PATH_MAX); - data.dirstreams = malloc (data.maxdir * sizeof (struct dir_data *) - + data.dirbufsize); - if (data.dirstreams == NULL) + if (!ftw_allocate (&data, MAX (2 * strlen (dir), PATH_MAX))) return -1; - memset (data.dirstreams, '\0', data.maxdir * sizeof (struct dir_data *)); - - data.dirbuf = (char *) data.dirstreams - + data.maxdir * sizeof (struct dir_data *); cp = __stpcpy (data.dirbuf, dir); /* Strip trailing slashes. */ while (cp > data.dirbuf + 1 && cp[-1] == '/') 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