From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by sourceware.org (Postfix) with ESMTPS id 071DD3874C30 for ; Thu, 11 Mar 2021 13:58:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 071DD3874C30 Received: by mail-qt1-x829.google.com with SMTP id x9so1153535qto.8 for ; Thu, 11 Mar 2021 05:58:04 -0800 (PST) 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:mime-version :content-transfer-encoding; bh=B3EgBr+9Io3XSADwoGkgmjaxr7ABef9xOUGBMcSo3kw=; b=TQ0TSxH+LTFZsEOozdRjV9wP/gbLirVlNa5SKXwIrtycAyMIvdM9O2WVrbecGDfBMU wOFi2m7bjz3t++P41C29yPObfZK/+RMFM9PSKtxtkDldbZmiZvZZ2IAIlwrjUxox4Fws Agok5IL+FMrNsK5AlYGcCSuHkrgI6YoJD2ibtMaBwXuAz3myTCkhArA1UHOxANTtxtuO GjiRizn2B/T8G3gWKlqaKLTO4lCF2rTX8NXB4sm4c2emkyIxX+OApYCy0U9D5TfkO9uq /lc42oRkZv1R2FLsl5xxmK6MCLKLcJjPVp0PQpBjiQZ0dA70RBVoDLJHbJZzOiKrNEiD a9rg== X-Gm-Message-State: AOAM532LXoQIA9dQCA7i6oYrs6I+YDMclNPmxZb3aCwlFc1eWioURkTu +tbdpSPDMfnaeYrDAPPA6lganBqkbMVUYkQx X-Google-Smtp-Source: ABdhPJwsjv+/MgVK28qR28dDzsbESdTw/cC5W0wLWwGe2ec5Yhm0EdD2eHeGfs+Hwcy3afOgY966aQ== X-Received: by 2002:ac8:66d6:: with SMTP id m22mr7394505qtp.56.1615471083299; Thu, 11 Mar 2021 05:58:03 -0800 (PST) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id l5sm1749553qtj.21.2021.03.11.05.58.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Mar 2021 05:58:03 -0800 (PST) From: Adhemerval Zanella To: libc-stable@sourceware.org Subject: [2.33 COMMITTED] io: Return EBAFD for negative file descriptor on fstat (BZ #27559) Date: Thu, 11 Mar 2021 10:57:58 -0300 Message-Id: <20210311135758.3729554-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.4 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Mar 2021 13:58:06 -0000 Now that fstat is implemented on top fstatat we need to handle negative inputs. The implementation now rejects AT_FDCWD, which would otherwise be accepted by the kernel. Checked on x86_64-linux-gnu and on i686-linux-gnu. (cherry picked from commit 94caafa040e4b4289c968cd70d53041b1463ac4d) --- io/Makefile | 2 +- io/fstat.c | 6 ++ io/fstat64.c | 6 ++ io/tst-stat-lfs.c | 2 + io/tst-stat.c | 102 ++++++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/fstat.c | 6 ++ sysdeps/unix/sysv/linux/fstat64.c | 12 ++++ 7 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 io/tst-stat-lfs.c create mode 100644 io/tst-stat.c diff --git a/io/Makefile b/io/Makefile index b7bebe923f..d145d88f4e 100644 --- a/io/Makefile +++ b/io/Makefile @@ -68,7 +68,7 @@ tests := test-utime test-stat test-stat2 test-lfs tst-getcwd \ tst-fts tst-fts-lfs tst-open-tmpfile \ tst-copy_file_range tst-getcwd-abspath tst-lockf \ tst-ftw-lnk tst-file_change_detection tst-lchmod \ - tst-ftw-bz26353 + tst-ftw-bz26353 tst-stat tst-stat-lfs # Likewise for statx, but we do not need static linking here. tests-internal += tst-statx diff --git a/io/fstat.c b/io/fstat.c index dc117361ff..17f31bf3b3 100644 --- a/io/fstat.c +++ b/io/fstat.c @@ -16,10 +16,16 @@ . */ #include +#include int __fstat (int fd, struct stat *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } return __fstatat (fd, "", buf, AT_EMPTY_PATH); } diff --git a/io/fstat64.c b/io/fstat64.c index addf379775..618170695c 100644 --- a/io/fstat64.c +++ b/io/fstat64.c @@ -16,10 +16,16 @@ . */ #include +#include int __fstat64 (int fd, struct stat64 *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } return __fstatat64 (fd, "", buf, AT_EMPTY_PATH); } hidden_def (__fstat64) diff --git a/io/tst-stat-lfs.c b/io/tst-stat-lfs.c new file mode 100644 index 0000000000..b53f460ad5 --- /dev/null +++ b/io/tst-stat-lfs.c @@ -0,0 +1,2 @@ +#define _FILE_OFFSET_BITS 64 +#include "tst-stat.c" diff --git a/io/tst-stat.c b/io/tst-stat.c new file mode 100644 index 0000000000..445ac4176c --- /dev/null +++ b/io/tst-stat.c @@ -0,0 +1,102 @@ +/* Basic tests for stat, lstat, fstat, and fstatat. + 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 void +stat_check (int fd, const char *path, struct stat *st) +{ + TEST_COMPARE (stat (path, st), 0); +} + +static void +lstat_check (int fd, const char *path, struct stat *st) +{ + TEST_COMPARE (lstat (path, st), 0); +} + +static void +fstat_check (int fd, const char *path, struct stat *st) +{ + /* Test for invalid fstat input (BZ #27559). */ + TEST_COMPARE (fstat (AT_FDCWD, st), -1); + TEST_COMPARE (errno, EBADF); + + TEST_COMPARE (fstat (fd, st), 0); +} + +static void +fstatat_check (int fd, const char *path, struct stat *st) +{ + TEST_COMPARE (fstatat (fd, "", st, 0), -1); + TEST_COMPARE (errno, ENOENT); + + TEST_COMPARE (fstatat (fd, path, st, 0), 0); +} + +typedef void (*test_t)(int, const char *path, struct stat *); + +static int +do_test (void) +{ + char *path; + int fd = create_temp_file ("tst-fstat.", &path); + TEST_VERIFY_EXIT (fd >= 0); + support_write_file_string (path, "abc"); + + struct statx stx; + TEST_COMPARE (statx (fd, path, 0, STATX_BASIC_STATS, &stx), 0); + + test_t tests[] = { stat_check, lstat_check, fstat_check, fstatat_check }; + + for (int i = 0; i < array_length (tests); i++) + { + struct stat st; + tests[i](fd, path, &st); + + TEST_COMPARE (stx.stx_dev_major, major (st.st_dev)); + TEST_COMPARE (stx.stx_dev_minor, minor (st.st_dev)); + TEST_COMPARE (stx.stx_ino, st.st_ino); + TEST_COMPARE (stx.stx_mode, st.st_mode); + TEST_COMPARE (stx.stx_nlink, st.st_nlink); + TEST_COMPARE (stx.stx_uid, st.st_uid); + TEST_COMPARE (stx.stx_gid, st.st_gid); + TEST_COMPARE (stx.stx_rdev_major, major (st.st_rdev)); + TEST_COMPARE (stx.stx_rdev_minor, minor (st.st_rdev)); + TEST_COMPARE (stx.stx_blksize, st.st_blksize); + TEST_COMPARE (stx.stx_blocks, st.st_blocks); + + TEST_COMPARE (stx.stx_ctime.tv_sec, st.st_ctim.tv_sec); + TEST_COMPARE (stx.stx_ctime.tv_nsec, st.st_ctim.tv_nsec); + TEST_COMPARE (stx.stx_mtime.tv_sec, st.st_mtim.tv_sec); + TEST_COMPARE (stx.stx_mtime.tv_nsec, st.st_mtim.tv_nsec); + } + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/fstat.c b/sysdeps/unix/sysv/linux/fstat.c index fd64362205..31a172dcc8 100644 --- a/sysdeps/unix/sysv/linux/fstat.c +++ b/sysdeps/unix/sysv/linux/fstat.c @@ -19,11 +19,17 @@ #include #include #include +#include #if !XSTAT_IS_XSTAT64 int __fstat (int fd, struct stat *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } return __fstatat (fd, "", buf, AT_EMPTY_PATH); } diff --git a/sysdeps/unix/sysv/linux/fstat64.c b/sysdeps/unix/sysv/linux/fstat64.c index 993abcb445..46de80b663 100644 --- a/sysdeps/unix/sysv/linux/fstat64.c +++ b/sysdeps/unix/sysv/linux/fstat64.c @@ -22,10 +22,16 @@ #include #include #include +#include int __fstat64_time64 (int fd, struct __stat64_t64 *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } return __fstatat64_time64 (fd, "", buf, AT_EMPTY_PATH); } #if __TIMESIZE != 64 @@ -34,6 +40,12 @@ hidden_def (__fstat64_time64) int __fstat64 (int fd, struct stat64 *buf) { + if (fd < 0) + { + __set_errno (EBADF); + return -1; + } + struct __stat64_t64 st_t64; return __fstat64_time64 (fd, &st_t64) ?: __cp_stat64_t64_stat64 (&st_t64, buf); -- 2.25.1