From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oa1-x2e.google.com (mail-oa1-x2e.google.com [IPv6:2001:4860:4864:20::2e]) by sourceware.org (Postfix) with ESMTPS id 502A03857026 for ; Fri, 1 Sep 2023 12:23:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 502A03857026 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-x2e.google.com with SMTP id 586e51a60fabf-1c0fa9dd74fso1230248fac.3 for ; Fri, 01 Sep 2023 05:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693571000; x=1694175800; 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=Bge4yIkoRy6qn08emOwaHVNvEVXbkc/s4LgbgP+BMgGs6Xia+0t5UeijXpadeOVdk8 qvHfpGmPcwEzzFLL71L7SLj5/NPFAzYin2RwTEmOkW9SLL5lHkqAyRYxcRXfHz7EMfZw B90+4BdDxM9GaT/6e0Y4007urYpRUzEdITxmv92Kx7Ckx9phm0KXJZRzAQE10K8UB0Kr Pmg68EyGOxxNbRoXjBrpE59gja+YykJTI8B+UHzkymsqS+reIduSNp+ZzpPMp1NFDSMd jx3OXlf5O59tS0vjhLb+6lFWwhCJWNIJkmNdp4vk+TZtb/9qGPanlUxypi54jgEY57X3 97lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693571000; x=1694175800; 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=LXdmPF/L0Tpwi9zzDSk/kkRZo8A01dFs2aJPn+N2G4uPR1gpIqUK3YzvuL1ljS2i/8 nQlFLzWGhTAZmQv6wkbYw5UM5E659s+sCtpKbvajMQR9hDcdILhS6TfW49Y5rw0yaqdx +TSgkPizXa3exwd3/ayJrkQwX5i9padCinl19pmWYCtlAmpvOH/Qk4tCRy6pdMIMayf6 pyu9U8mD8owuiV5SWyC8aOYfvdZ8DzzPt3rrNP63DU/pPAWBF7YK/tltxQ2RGys6euNa pNFgHh9MYrEbrJ/C9D6ROqFPVOuYpE6V9D2NbMqrkiOKa3hqPOE4+25y3QDhZfvHlilT 7qeg== X-Gm-Message-State: AOJu0Yz+Z+pFudYiN84plt6Zih31Hy7j8VgqBDTy46kgR/Mo+9Lmmfqs X7YSEV8mzsljPzvaux2HFbD98mKlZyTOUOo9vitVcw== X-Google-Smtp-Source: AGHT+IECxo2djvowVoRhUXSWUUsrW/BXUss3gC7tRQYugxoEYT/Y7M5zEjVYYEIfcfwRtYwTpSyEpw== X-Received: by 2002:a05:6870:5688:b0:1d0:e0c9:f33d with SMTP id p8-20020a056870568800b001d0e0c9f33dmr2570230oao.46.1693571000209; Fri, 01 Sep 2023 05:23:20 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:578c:d83b:59f9:d5bf:7bf8]) by smtp.gmail.com with ESMTPSA id zl25-20020a0568716d9900b001babd036460sm2065377oab.19.2023.09.01.05.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 05:23:19 -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:23:15 -0300 Message-Id: <20230901122315.59212-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