From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 3A7503857B8E for ; Thu, 10 Aug 2023 13:54:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3A7503857B8E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6bcade59b24so887122a34.0 for ; Thu, 10 Aug 2023 06:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691675656; x=1692280456; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=RziELOb69gGnWRPYjzvTs9KFHrZ6IroaYSsvwGN0aSA=; b=a1fHrgA/rSyCvqwcf/1/euZOcAXCO+9EyvtZ8mRepGEN6dFc/eQYm/v34Lj77pCsM7 dVULyrdr2bfMa4UC2ocU1h0OzLt65u4fGqjjKCB/hobT+qXzBdLdtm6HBY+JOHwhxeRO FV0Wm8Zg3VrR5fed8nt2E4jQKwAIJ26BMmz80Q7Q5Acl+mjcyN7LHlmz9J0f6Vlriace 9Wi+pUoOnAu6IDrQ7nkv75xLIZT6l3iL7ChqcXA4zvnjvnzkeF/kxY41awkvnjR8gawV /QTzzVomiUVr8I9fH2vZ2sXscp9xWnESGb1l4i2VBUg96j79G+mZIZJITliyGaAoxNUc /Vzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691675656; x=1692280456; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RziELOb69gGnWRPYjzvTs9KFHrZ6IroaYSsvwGN0aSA=; b=bIY1eOEAsxaKuwcLcSjoGc0Np2M4ffnIcUrkF8py2kPFmCQhwOO761XYxO7NRnmhOw 1iMbnZo5aDAqwR1Y6FQNM9ac1nQ2ky65CNq+2RA1cFK+XRsdt8EhZLPjHZAxbL+nL2Lr H7d4YnXEpaVTfndZ5DGwV5hIvBcaWjizABWE/CM4BKGHi94psBovl2XTV0i0LlmbN2Wy TuXVsx5aM33WDZFpUNVOGMM4/VJ6DYS3GxN2JlW4C9QjdZzVsHW7LxQizF/oa7X4NDU/ 0srBvNDDXzHpjCN/aNJRuuCSEZ0beUHsPMHb6/VMKde83IGDMcFqOTqkNaUECN1LkMhT 62ug== X-Gm-Message-State: AOJu0Yx/g1yRFH7mxYrCg8qWd/n5ZL21oEvVd1odSpuXS9heZjLyHscR rY41EhS3wI6sTsHFZN41CJQpXpcLc21cBgNG1S0= X-Google-Smtp-Source: AGHT+IGo7nPSh5g/QhO62Y0lD5wcVpvfF97aD1chO7+uiqFUcrGCy4qTZceozLxBcgN7m0G1ipnU9Q== X-Received: by 2002:a05:6830:1bf9:b0:6bc:e01e:d23c with SMTP id k25-20020a0568301bf900b006bce01ed23cmr2308081otb.35.1691675655727; Thu, 10 Aug 2023 06:54:15 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:365b:6c5f:dd93:495b:aaa8]) by smtp.gmail.com with ESMTPSA id c3-20020a9d6c83000000b006b92509e76esm650917otr.32.2023.08.10.06.54.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 06:54:15 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2] linux: Make fdopendir fail with O_PATH (BZ 30373) Date: Thu, 10 Aug 2023 10:54:11 -0300 Message-Id: <20230810135411.1000205-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.8 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: It is not strictly required by the POSIX, since O_PATH is a Linux extension, but it is QoI to fail early instead of at readdir. Also the check is free, since fdopendir already checks if the file descriptor is opened for read. Checked on x86_64-linux-gnu. --- sysdeps/unix/sysv/linux/Makefile | 1 + sysdeps/unix/sysv/linux/fdopendir.c | 8 +++- .../unix/sysv/linux/tst-fdopendir-o_path.c | 48 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index be801e3be4..ec0ba3eb05 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -199,6 +199,7 @@ tests += \ tst-clone3 \ tst-epoll \ tst-fanotify \ + tst-fdopendir-o_path \ tst-getauxval \ tst-gettid \ tst-gettid-kill \ diff --git a/sysdeps/unix/sysv/linux/fdopendir.c b/sysdeps/unix/sysv/linux/fdopendir.c index 861345396c..839690f4d3 100644 --- a/sysdeps/unix/sysv/linux/fdopendir.c +++ b/sysdeps/unix/sysv/linux/fdopendir.c @@ -37,10 +37,16 @@ __fdopendir (int fd) return NULL; } - /* Make sure the descriptor allows for reading. */ int flags = __fcntl64_nocancel (fd, F_GETFL); if (__glibc_unlikely (flags == -1)) return NULL; + /* Fail early for descriptors opened with O_PATH. */ + if (__glibc_unlikely ((flags & O_PATH) == O_PATH)) + { + __set_errno (EBADF); + return NULL; + } + /* Make sure the descriptor allows for reading. */ if (__glibc_unlikely ((flags & O_ACCMODE) == O_WRONLY)) { __set_errno (EINVAL); diff --git a/sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c b/sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c new file mode 100644 index 0000000000..2531cf8ddb --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-fdopendir-o_path.c @@ -0,0 +1,48 @@ +/* Check if fdopendir fails with file descriptor opened with O_PATH (BZ 30737) + Copyright (C) 2023 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 + +static int +do_test (void) +{ + char *dirname = support_create_temp_directory ("tst-fdopendir-o_path"); + + { + int fd = xopen (dirname, O_RDONLY | O_DIRECTORY, 0600); + DIR *dir = fdopendir (fd); + TEST_VERIFY_EXIT (dir != NULL); + closedir (dir); + } + + { + int fd = xopen (dirname, O_RDONLY | O_PATH | O_DIRECTORY, 0600); + TEST_VERIFY (fdopendir (fd) == NULL); + TEST_COMPARE (errno, EBADF); + xclose (fd); + } + + return 0; +} + +#include -- 2.34.1