From patchwork Fri Aug 30 19:54:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 96827 X-Patchwork-Delegate: dj@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 83B383865464 for ; Fri, 30 Aug 2024 19:55:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 254383864C65 for ; Fri, 30 Aug 2024 19:54:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 254383864C65 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 254383864C65 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047660; cv=none; b=i8/HggZ+FIOcJB6HrVlUZ2J6o0gEITdDI0kid136expRD24onVHkGUHDacjNT7TsS3GBgdz394T+X2nsRtl8aoVb8CUzDFCCJcCUB3srPVs+oxZ0aVsAqvkONB8NthgYnwdZDo8x2Iit8qRNUa1yW+QrZbECVHJ7qk7JHp/C/Ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725047660; c=relaxed/simple; bh=I2UpkLyDBXVg/2owbfovqlOOPx3S/eTluHjleeOsCIY=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=Uv/eTNEHE+l0ft7FXWTOvuSjNgfo1MRSiqpXGOUyYe+GFFctAAAGqtR3mbRRlFwSU/eVR43NLc151I+Rd2RrZNFA6h3m+mmLGZ56DcS28NH5UJ27oPS/Mcptong6cNP+9/SixFWKw5pxX5NIOR5KPifU8WXh+9aJ++E0WL6ouPw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725047654; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WkY5qh2VSSde1qnM3XMF2jh0Hm80GjuOubvs+/3DojY=; b=cl18ozuGVP0sEL/v9vmVbHfGSEpO2Rdb/pIA8GNIhg3sWcKQuI97+9I8wa16P85SsSGW3G 5q21VhNdkRj4rlvCsPbkwDD1p0QX5x3B8w5756gAop9H3SnWm61pqvRBTMHcZnQ3H4hvRt qjMsESKLBcbX2lpsdJCZaZXqXw3CFqQ= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-529-WNUoHJgaOx6vSrAwMKJtwA-1; Fri, 30 Aug 2024 15:54:13 -0400 X-MC-Unique: WNUoHJgaOx6vSrAwMKJtwA-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 93E0E1955D53 for ; Fri, 30 Aug 2024 19:54:12 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.7]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5225819560AA for ; Fri, 30 Aug 2024 19:54:11 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v4 13/13] Linux: readdir64_r should not skip d_ino == 0 entries (bug 32126) In-Reply-To: Message-ID: <45939a6bcc66e746a672a92a72e12e46b772490b.1725047142.git.fweimer@redhat.com> References: X-From-Line: 45939a6bcc66e746a672a92a72e12e46b772490b Mon Sep 17 00:00:00 2001 Date: Fri, 30 Aug 2024 21:54:08 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This is the same bug as bug 12165, but for readdir_r. The regression test covers both bug 12165 and bug 32126. Reviewed-by: DJ Delorie --- dirent/Makefile | 1 + dirent/tst-readdir-zero-inode.c | 134 ++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/readdir64_r.c | 29 +++--- 3 files changed, 147 insertions(+), 17 deletions(-) create mode 100644 dirent/tst-readdir-zero-inode.c diff --git a/dirent/Makefile b/dirent/Makefile index 045c786575..11b772e3ab 100644 --- a/dirent/Makefile +++ b/dirent/Makefile @@ -62,6 +62,7 @@ tests := \ tst-fdopendir \ tst-fdopendir2 \ tst-readdir-long \ + tst-readdir-zero-inode \ tst-rewinddir \ tst-scandir \ tst-scandir64 \ diff --git a/dirent/tst-readdir-zero-inode.c b/dirent/tst-readdir-zero-inode.c new file mode 100644 index 0000000000..af9fb946ab --- /dev/null +++ b/dirent/tst-readdir-zero-inode.c @@ -0,0 +1,134 @@ +/* Test that readdir does not skip entries with d_ino == 0 (bug 12165). + Copyright (C) 2024 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 + +/* Add the directory entry at OFFSET to the stream D. */ +static uint64_t +add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset) +{ + bool added = false; + ++offset; + switch (offset - 1) + { + case 0: + added = support_fuse_dirstream_add (d, 1, offset, DT_DIR, "."); + break; + case 1: + added = support_fuse_dirstream_add (d, 1, offset, DT_DIR, ".."); + break; + case 2: + added = support_fuse_dirstream_add (d, 2, offset, DT_REG, "before"); + break; + case 3: + added = support_fuse_dirstream_add (d, 0, offset, DT_REG, "zero"); + break; + case 4: + added = support_fuse_dirstream_add (d, 3, offset, DT_REG, "after"); + break; + } + if (added) + return offset; + else + return 0; +} + +/* Set to true if getdents64 should produce only one entry. */ +static bool one_entry_per_getdents64; + +static void +fuse_thread (struct support_fuse *f, void *closure) +{ + struct fuse_in_header *inh; + while ((inh = support_fuse_next (f)) != NULL) + { + if (support_fuse_handle_mountpoint (f) + || (inh->nodeid == 1 && support_fuse_handle_directory (f))) + continue; + switch (inh->opcode) + { + case FUSE_READDIR: + if (inh->nodeid == 1) + { + uint64_t offset = support_fuse_cast (READ, inh)->offset; + struct support_fuse_dirstream *d + = support_fuse_prepare_readdir (f); + while (true) + { + offset = add_directory_entry (d, offset); + if (offset == 0 || one_entry_per_getdents64) + break; + } + support_fuse_reply_prepared (f); + } + else + support_fuse_reply_error (f, EIO); + break; + default: + FAIL ("unexpected event %s", support_fuse_opcode (inh->opcode)); + support_fuse_reply_error (f, EIO); + } + } +} + +static int +do_test (void) +{ + support_fuse_init (); + + for (enum support_readdir_op op = 0; op <= support_readdir_op_last (); ++op) + { + struct support_fuse *f = support_fuse_mount (fuse_thread, NULL); + DIR *dir = xopendir (support_fuse_mountpoint (f)); + struct support_dirent e = { 0, }; + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "."); + TEST_COMPARE (e.d_ino, 1); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, ".."); + TEST_COMPARE (e.d_ino, 1); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "before"); + TEST_COMPARE (e.d_ino, 2); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "zero"); + TEST_COMPARE (e.d_ino, 0); + + TEST_VERIFY (support_readdir (dir, op, &e)); + TEST_COMPARE_STRING (e.d_name, "after"); + TEST_COMPARE (e.d_ino, 3); + + TEST_VERIFY (!support_readdir (dir, op, &e)); + + free (e.d_name); + xclosedir (dir); + support_fuse_unmount (f); + } + + return 0; +} + +#include diff --git a/sysdeps/unix/sysv/linux/readdir64_r.c b/sysdeps/unix/sysv/linux/readdir64_r.c index 7ad7e5945b..c42a161ffc 100644 --- a/sysdeps/unix/sysv/linux/readdir64_r.c +++ b/sysdeps/unix/sysv/linux/readdir64_r.c @@ -37,7 +37,7 @@ __readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result) __libc_lock_lock (dirp->lock); - do + while (1) { if (dirp->offset >= dirp->size) { @@ -79,26 +79,21 @@ __readdir64_r (DIR *dirp, struct dirent64 *entry, struct dirent64 **result) dirp->filepos = dp->d_off; - if (reclen > offsetof (struct dirent64, d_name) + NAME_MAX + 1) + if (reclen <= offsetof (struct dirent64, d_name) + NAME_MAX + 1) + break; + + /* The record is very long. It could still fit into the + caller-supplied buffer if we can skip padding at the end. */ + size_t namelen = _D_EXACT_NAMLEN (dp); + if (namelen <= NAME_MAX) { - /* The record is very long. It could still fit into the - caller-supplied buffer if we can skip padding at the - end. */ - size_t namelen = _D_EXACT_NAMLEN (dp); - if (namelen <= NAME_MAX) - reclen = offsetof (struct dirent64, d_name) + namelen + 1; - else - { - /* The name is too long. Ignore this file. */ - dirp->errcode = ENAMETOOLONG; - dp->d_ino = 0; - continue; - } + reclen = offsetof (struct dirent64, d_name) + namelen + 1; + break; } - /* Skip deleted and ignored files. */ + /* The name is too long. Ignore this file. */ + dirp->errcode = ENAMETOOLONG; } - while (dp->d_ino == 0); if (dp != NULL) {