From nobody Mon Apr 11 13:44:04 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id C6FCF1A85F8C; Mon, 11 Apr 2022 13:44:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4KcVSh3WS9z4vRm; Mon, 11 Apr 2022 13:44:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649684644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Y8o51T7CwCSOFLF7oBdl9gUau1hYxhbSgZIo6lPsvDM=; b=iGnVyfNZnC+M9KxAqrD1FJ9QTaAK3XLUx8wB6t4RoVvyI6/wwQ8DWjj4H0qgbQ6glAIN+E oBGQnTtg2khOH3hKEf81Gq+/WPCGxwdM7PpAeHLYIpSws0uAGTwBH+y6mebhjMBsu01vud 9Nc6AMqlJhVNtHzwYbxyRMC17yYJJIK2DZyssZ+5tKCv8AFSgC+4guJ7mk6p9pYmBLHQrk vvYU7abQFrP5lIwAIf6gKci109brate1pW+gK5luOyPnnKJhsme3X+JU9p1RPLyY9KzhMS 5FRQB6KEQLqFbgku5dY5iYR0/5sNB4ZrPXr66CZuRZ6Bf/bOhHi0x2QGdZuS8w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 3E6EE24117; Mon, 11 Apr 2022 13:44:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 23BDi40T029042; Mon, 11 Apr 2022 13:44:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23BDi4xD029041; Mon, 11 Apr 2022 13:44:04 GMT (envelope-from git) Date: Mon, 11 Apr 2022 13:44:04 GMT Message-Id: <202204111344.23BDi4xD029041@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: e66bbe6e02b4 - stable/13 - libc: Check for readdir(2) errors in fts(3) List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: e66bbe6e02b48d1fa57c52321a517dcf42d8eb69 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1649684644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Y8o51T7CwCSOFLF7oBdl9gUau1hYxhbSgZIo6lPsvDM=; b=UJs+CWffl4GHcaKY4YFTQa15GYxNuBcq3GjnZu/QE4SB0u9VHTAaj9VRd9Rfim0IncC73z B4EunAldqxFPfVi5xCUsU8vV1Rbd+/XO0ajVMg/cqjP0RT/Du7jBunyYSLFjLg0TapsVi1 luKoaiBxWknxaJLdE4dBp8SNS012v66psKfEVfH1iz/1eU+qvd5V7Q+7wIbLYIokGjnT3X vUzxHO7BFnoiXF0cHuJfRZY2jUcCsxb0NTJiRm6Bp/dgNkyZe1kVZCTn0nSS7zcORwgdIi OB32qsH1PAf1YKO9LoedDJZFGhySsCtJpLlljW7YRngcnQTjyv+1os5ycvRvTw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1649684644; a=rsa-sha256; cv=none; b=gUzDPB9Zpt7xZ0RTDHLz38QCf6luohjCiGlr6hQOiAqWpyfdWs1Sy7jXDRCDLDFmBgE55e tXClpA6wC08lOUbfFW7/YGqd3+qar6MXTRhjj/UwO9D00HUWp9JOnUo3w99TkVR3pw6aL7 wVJVUvAOjH2TOrDdgTu1EbqDmIu/LEbyg2K4bSRcx/WOQp85iloFV0YY2H82D7XGDKECh/ 2fnbTM82nynmaRPVgcSLfx6H7uEp2+NlPIica0fHXxtC7fQxZYX5Sy1eXxBzySdPFqEqo7 6E6B0Zpr2qTGjeMhrySXwbHQq3yoAo/gFVwQPIhKbyOZFdcfERJX6137l8zXcA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=e66bbe6e02b48d1fa57c52321a517dcf42d8eb69 commit e66bbe6e02b48d1fa57c52321a517dcf42d8eb69 Author: Ganael LAPLANCHE AuthorDate: 2022-03-28 14:54:02 +0000 Commit: Mark Johnston CommitDate: 2022-04-11 13:43:28 +0000 libc: Check for readdir(2) errors in fts(3) Previously, such errors were not distinguished from the end-of-directory condition. With improvements from Mahmoud Abumandour . Reviewed by: markj PR: 262038 (cherry picked from commit 0cff70ca66547ca5b04030ef07e6a0b9759a0184) --- lib/libc/gen/fts-compat.c | 31 ++++++++++++++++++++++++++++--- lib/libc/gen/fts-compat11.c | 32 +++++++++++++++++++++++++++++--- lib/libc/gen/fts.c | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/lib/libc/gen/fts-compat.c b/lib/libc/gen/fts-compat.c index ccdd4f15905b..9f295110f1c7 100644 --- a/lib/libc/gen/fts-compat.c +++ b/lib/libc/gen/fts-compat.c @@ -610,6 +610,19 @@ __fts_set_clientptr_44bsd(FTS *sp, void *clientptr) sp->fts_clientptr = clientptr; } +static struct freebsd11_dirent * +fts_safe_readdir(DIR *dirp, int *readdir_errno) +{ + struct freebsd11_dirent *ret; + + errno = 0; + if (!dirp) + return (NULL); + ret = freebsd11_readdir(dirp); + *readdir_errno = errno; + return (ret); +} + /* * This is the tricky part -- do not casually change *anything* in here. The * idea is to build the linked list of entries that are used by fts_children @@ -634,7 +647,7 @@ fts_build(FTS *sp, int type) DIR *dirp; void *oldaddr; int cderrno, descend, len, level, maxlen, nlinks, oflag, saved_errno, - nostat, doadjust, dnamlen; + nostat, doadjust, dnamlen, readdir_errno; char *cp; /* Set current node pointer. */ @@ -738,8 +751,9 @@ fts_build(FTS *sp, int type) /* Read the directory, attaching each entry to the `link' pointer. */ doadjust = 0; + readdir_errno = 0; for (head = tail = NULL, nitems = 0; - dirp && (dp = freebsd11_readdir(dirp));) { + (dp = fts_safe_readdir(dirp, &readdir_errno));) { dnamlen = dp->d_namlen; if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) continue; @@ -839,6 +853,16 @@ mem1: saved_errno = errno; } ++nitems; } + + if (readdir_errno) { + cur->fts_errno = readdir_errno; + /* + * If we've not read any items yet, treat + * the error as if we can't access the dir. + */ + cur->fts_info = nitems ? FTS_ERR : FTS_DNR; + } + if (dirp) (void)closedir(dirp); @@ -877,7 +901,8 @@ mem1: saved_errno = errno; /* If didn't find anything, return NULL. */ if (!nitems) { - if (type == BREAD) + if (type == BREAD && + cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR) cur->fts_info = FTS_DP; return (NULL); } diff --git a/lib/libc/gen/fts-compat11.c b/lib/libc/gen/fts-compat11.c index 288351d2008b..a18ce3f00a6a 100644 --- a/lib/libc/gen/fts-compat11.c +++ b/lib/libc/gen/fts-compat11.c @@ -607,6 +607,19 @@ freebsd11_fts_set_clientptr(FTS11 *sp, void *clientptr) sp->fts_clientptr = clientptr; } +static struct freebsd11_dirent * +fts_safe_readdir(DIR *dirp, int *readdir_errno) +{ + struct freebsd11_dirent *ret; + + errno = 0; + if (!dirp) + return (NULL); + ret = freebsd11_readdir(dirp); + *readdir_errno = errno; + return (ret); +} + /* * This is the tricky part -- do not casually change *anything* in here. The * idea is to build the linked list of entries that are used by fts_children @@ -630,7 +643,8 @@ fts_build(FTS11 *sp, int type) DIR *dirp; void *oldaddr; char *cp; - int cderrno, descend, oflag, saved_errno, nostat, doadjust; + int cderrno, descend, oflag, saved_errno, nostat, doadjust, + readdir_errno; long level; long nlinks; /* has to be signed because -1 is a magic value */ size_t dnamlen, len, maxlen, nitems; @@ -736,8 +750,9 @@ fts_build(FTS11 *sp, int type) /* Read the directory, attaching each entry to the `link' pointer. */ doadjust = 0; + readdir_errno = 0; for (head = tail = NULL, nitems = 0; - dirp && (dp = freebsd11_readdir(dirp));) { + (dp = fts_safe_readdir(dirp, &readdir_errno));) { dnamlen = dp->d_namlen; if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) continue; @@ -823,6 +838,16 @@ mem1: saved_errno = errno; } ++nitems; } + + if (readdir_errno) { + cur->fts_errno = readdir_errno; + /* + * If we've not read any items yet, treat + * the error as if we can't access the dir. + */ + cur->fts_info = nitems ? FTS_ERR : FTS_DNR; + } + if (dirp) (void)closedir(dirp); @@ -859,7 +884,8 @@ mem1: saved_errno = errno; /* If didn't find anything, return NULL. */ if (!nitems) { - if (type == BREAD) + if (type == BREAD && + cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR) cur->fts_info = FTS_DP; return (NULL); } diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c index d0705e123775..5186ae047a3b 100644 --- a/lib/libc/gen/fts.c +++ b/lib/libc/gen/fts.c @@ -604,6 +604,19 @@ fts_set_clientptr(FTS *sp, void *clientptr) sp->fts_clientptr = clientptr; } +static struct dirent * +fts_safe_readdir(DIR *dirp, int *readdir_errno) +{ + struct dirent *ret; + + errno = 0; + if (!dirp) + return (NULL); + ret = readdir(dirp); + *readdir_errno = errno; + return (ret); +} + /* * This is the tricky part -- do not casually change *anything* in here. The * idea is to build the linked list of entries that are used by fts_children @@ -627,7 +640,8 @@ fts_build(FTS *sp, int type) DIR *dirp; void *oldaddr; char *cp; - int cderrno, descend, oflag, saved_errno, nostat, doadjust; + int cderrno, descend, oflag, saved_errno, nostat, doadjust, + readdir_errno; long level; long nlinks; /* has to be signed because -1 is a magic value */ size_t dnamlen, len, maxlen, nitems; @@ -733,7 +747,9 @@ fts_build(FTS *sp, int type) /* Read the directory, attaching each entry to the `link' pointer. */ doadjust = 0; - for (head = tail = NULL, nitems = 0; dirp && (dp = readdir(dirp));) { + readdir_errno = 0; + for (head = tail = NULL, nitems = 0; + (dp = fts_safe_readdir(dirp, &readdir_errno));) { dnamlen = dp->d_namlen; if (!ISSET(FTS_SEEDOT) && ISDOT(dp->d_name)) continue; @@ -819,6 +835,16 @@ mem1: saved_errno = errno; } ++nitems; } + + if (readdir_errno) { + cur->fts_errno = readdir_errno; + /* + * If we've not read any items yet, treat + * the error as if we can't access the dir. + */ + cur->fts_info = nitems ? FTS_ERR : FTS_DNR; + } + if (dirp) (void)closedir(dirp); @@ -855,7 +881,8 @@ mem1: saved_errno = errno; /* If didn't find anything, return NULL. */ if (!nitems) { - if (type == BREAD) + if (type == BREAD && + cur->fts_info != FTS_DNR && cur->fts_info != FTS_ERR) cur->fts_info = FTS_DP; return (NULL); }