From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 0823A385DC19 for ; Mon, 2 Oct 2023 11:09:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0823A385DC19 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-pl1-x629.google.com with SMTP id d9443c01a7336-1c60778a3bfso115870515ad.1 for ; Mon, 02 Oct 2023 04:09:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696244982; x=1696849782; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:references:to :from:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=SrAMjtbB1/cEPlo+Kfdf5vxCnKVeBY61wGoOO+QTujw=; b=e+jmx03CA1NXuENh3RDURrGPGH7kO3OFWj9k0Xfy2gFMwTk7hpOo6suJ+yEhXRHdXy RYQlDXsFPfk3lD4geL5dmMXtaM5g9n8cjf5CiauPx7G8ftSxqSfVArjMz3YWV6JfHlHE YaL/cF4IduK7VvFOcxzSvKx0EZhWFM6AYBO0+x2rCHg5IDR4IHTiD1g3gQrYZR7OOl10 cZCBNtM1SKmFd+294q/oe2b4xrlFTc+FyBgbarJih1MnsiAGxcLwG9Rs4x1I1TLwGB2m X34EbnorA8eCnxcLaf5RJoELKoVb6emAF68aF9gVzpO7J6BTrd3jG9erfeB1TXPh7HRp sueg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696244982; x=1696849782; h=content-transfer-encoding:in-reply-to:organization:references:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SrAMjtbB1/cEPlo+Kfdf5vxCnKVeBY61wGoOO+QTujw=; b=NuNSCvdjZk/rufA5lVBPeYdRVdc9/SMj4zXBTiOR/GBeBS6/LSF4BqKHOSXY8NZFHD FMCYjzwtOrIHZ6BkXn5QMxpe3nDLtFi+V3coLTU2GOJgJxQpzsA8ehLFkTcWJVxFYk1j METbXZWHNljvfqKf9rLm7GyA4PerQdAlAdxDU9l8hHMFplgNASTqgKAN9FU43/4mJy3l I+QDVXQ1/6hR5RFVMg7VPikVBLFaAd5F52ZBHuALUJzyZ8seJReGcfpMkYsaZFuAVZpG 25VTnNQqYTe4uYKtvf8hfWecLb6RT2mGTNSBSClstaHw8m64naABoD3a5r2IOpaYAAU5 C9sQ== X-Gm-Message-State: AOJu0Yy/Ge6umnfFJXtixMTAXmUfziWNs8cTIHXLF5akUGrWFANbhK5/ 5inMHgsPdkOnGzMeyVVLSy3u4DBmZCIbTQAuHZqY9Q== X-Google-Smtp-Source: AGHT+IHpmJb4GPCunf/gAxx/VOZnVJjJowCnjMGUoWrxdRYc/azVIVApySUhCAft2wtIZCT5cXxyGw== X-Received: by 2002:a17:902:9b8e:b0:1c0:cbaf:6930 with SMTP id y14-20020a1709029b8e00b001c0cbaf6930mr10406963plp.54.1696244982243; Mon, 02 Oct 2023 04:09:42 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c1:feaf:9c9c:46fb:1bba:fa15? ([2804:1b3:a7c1:feaf:9c9c:46fb:1bba:fa15]) by smtp.gmail.com with ESMTPSA id 12-20020a170902e9cc00b001c613091aeesm12056269plk.293.2023.10.02.04.09.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Oct 2023 04:09:41 -0700 (PDT) Message-ID: <6629b456-24f6-4dc5-a04a-16d7daf5206f@linaro.org> Date: Mon, 2 Oct 2023 08:09:39 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] linux: Make fdopendir fail with O_PATH (BZ 30373) Content-Language: en-US From: Adhemerval Zanella Netto To: libc-alpha@sourceware.org, Paul Eggert References: <20230901122651.59253-1-adhemerval.zanella@linaro.org> Organization: Linaro In-Reply-To: <20230901122651.59253-1-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 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: On 01/09/23 09:26, Adhemerval Zanella wrote: > 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. Ping. > --- > 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