From patchwork Thu Aug 10 13:54:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1819859 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=O7IUDg2y; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RM7hW2cfpz1yYl for ; Thu, 10 Aug 2023 23:54:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 35DD1385771B for ; Thu, 10 Aug 2023 13:54:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 35DD1385771B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1691675673; bh=RziELOb69gGnWRPYjzvTs9KFHrZ6IroaYSsvwGN0aSA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=O7IUDg2yjKOjBrWLDqZucdI7wdlmw6HhT3kXEEBekAxf2w74XFKKRhzr/FN3x58B5 MUiS9PTTg+KcrWDIk3Hwa+WCjGdbLWI8q/64skjiByGP2yEx558Gbs+aquuv1SoP+P R/C3NNl5GUqzbQFTTP9R2Sjxt/i47qNiqNX6jlbk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org 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 Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6bcade59b24so887122a34.0 for ; Thu, 10 Aug 2023 06:54:17 -0700 (PDT) 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) 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 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 X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" 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