From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by sourceware.org (Postfix) with ESMTPS id 0A90D385802F for ; Fri, 1 Sep 2023 12:26:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0A90D385802F 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-oa1-x36.google.com with SMTP id 586e51a60fabf-1ba5cda3530so1156298fac.3 for ; Fri, 01 Sep 2023 05:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693571216; x=1694176016; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=e8ZmpNDk2Ug7UJf7+jpDaTniupwzsV7JiJKDLcPsL8U=; b=g7707mN5ljSYKRD06OQoWUS7bhVDvBLFagkaL2CwdMJPI1K8zY4SPzAiSjj6kgAxLQ 6gVyCNN0mD5Zc/Qbym7ifTlUCD3b6eNjQMmBNR05X2n6OdldkzKhj85MPzzcDkTvofXL 4rDUadooQwx7QkJ5B2Z3Y5iCgKB8b60ENocOVh/avCdfP36wGTnFJxBv90QUM4bzZgS4 RvuW5BdNiTKod+X69UV93eFzFMJnii/E0zyiLiN3q26AHwVzoJPuxnd6BAADkGs+L4SM Coj38/i1U9+gFFr8IqknGq1OSD72h54DyIE3GYTEuCjlaR6CLKBv3ZwDYxxdW28+sDB/ i35w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693571216; x=1694176016; 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=e8ZmpNDk2Ug7UJf7+jpDaTniupwzsV7JiJKDLcPsL8U=; b=Oc+eUKXChIvdfHQW5WWIlfUO+1uhClQIsWpeQGqqEmjC61b0y+btgx2rF3KpQanL+Q lECgvBjKjWtQ5I2d5pYvZHKoJioBvaEkSqGXNaC1cygcyBOlNRynb5aNIOEEkt+eda+e n+9SUL45gfg69ohaDpyP5CIpWr+Nx9Q/7XCrC9SQUSKcrpna/ERxFMUNKVwVWiWWudN2 gIrt9xj+23/eFmxZ3kTulHp1iIdIC2h3RN3EzceSgYi+z2w1pIglllFOL95NmPUQSu9j YSKvVXc/lLo9+J8pERMcn80qgU+KnyQhiMm/CxB0W7XBHapyllBE7FiMbXOrE3h2M0LD oKeA== X-Gm-Message-State: AOJu0YzJBiNAa15iZ2nnxctO8FPAdoFTVaukD3diArXVOTMggjrABmnt EnLY6LjqoLMA6GFNhTIh+lSTwGM+KN6Fc6g87hp3Ow== X-Google-Smtp-Source: AGHT+IG7Z0BzFrVIxUNcAU2T/lkMK3yG260+jgZ5FY80RuRWuNPSNkAxYfQPB50HVwo1YCGgZLKNKQ== X-Received: by 2002:a05:6870:d60c:b0:1b3:6ea7:7475 with SMTP id a12-20020a056870d60c00b001b36ea77475mr2728522oaq.0.1693571216478; Fri, 01 Sep 2023 05:26:56 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:578c:d83b:59f9:d5bf:7bf8]) by smtp.gmail.com with ESMTPSA id v49-20020a056870957100b001c03d1a519fsm2077234oal.39.2023.09.01.05.26.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 05:26:55 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Paul Eggert Subject: [PATCH v2] linux: Make fdopendir fail with O_PATH (BZ 30373) Date: Fri, 1 Sep 2023 09:26:51 -0300 Message-Id: <20230901122651.59253-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..707fb5960e 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)) + { + __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