From patchwork Thu Mar 2 14:57:30 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: 65905 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 0BCA83858439 for ; Thu, 2 Mar 2023 14:58:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0BCA83858439 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1677769126; bh=sbVapDEdL6qaQx2ZlaGHEmhbuaUvb2kNgUO3WTy/rDQ=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=na91fvMWvDGel/LhyQfpqz1T7KWHiIWyTMaN9LVt1nUFI27pxgcFmjBItl+1Kvzfu O6ugfQTswIwsVo2modmPFcJCu82AtZEUQI3mnraMoeJCz7b3mMOTN7kKjMfRR/KYPZ sYG5LCadiJOJK0IWL3DAVzqfGJL1YwOfgOvu7eKE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by sourceware.org (Postfix) with ESMTPS id 939E7385840F for ; Thu, 2 Mar 2023 14:57:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 939E7385840F Received: by mail-oi1-x231.google.com with SMTP id e21so13755951oie.1 for ; Thu, 02 Mar 2023 06:57:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677769059; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sbVapDEdL6qaQx2ZlaGHEmhbuaUvb2kNgUO3WTy/rDQ=; b=iXfnRs719q41ZcEUfVooXbDNDlT6MhU7a0Yj42007sNW7q4l4oTtahjcavmQvpsm7O P9n0HM3AuaX7RYSSOggotAx9JR4Ju/WUCSEeSNTudPjuPXoI2xQDgW5VVPuHOZSJCxoZ NQntEtFDaXyF9zE/palfqJyOXVwKVQ3mOMAsjqjwv4F3O9aQUk2tRlYeZlB5+Kt4A/L9 9Ilfv2zreXuQx+1b4KKM53qQbQZDPI9qwltYZ0imP80StCNYopqGogNfprRSo7vuEv8t 9/gvDIYPwZpQssXb+wjK7m6PZp1108DWkJB96q3mADdn6bJbbDnumDXQfJlR0FlsV0zn dMuw== X-Gm-Message-State: AO0yUKXCVu58Oq+KAmPxJLe2Mq+m3LN2uaEJvkcV8OfTzKRg7A4Yjjx6 2NIn18V06KJxXbiw0FEgHpNbyVFfphBMQRxjrKY= X-Google-Smtp-Source: AK7set+Bg1GRuZwdI/cWF8Bp+E155EMPCMyUtPLfx2nFuO45LLKe4W+dc3DIGxoQIJtGo+wed2LF5A== X-Received: by 2002:a05:6808:3c7:b0:384:ec1:cc6c with SMTP id o7-20020a05680803c700b003840ec1cc6cmr5334497oie.54.1677769059259; Thu, 02 Mar 2023 06:57:39 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c3:d849:953:d4b:62c6:5aa0]) by smtp.gmail.com with ESMTPSA id t26-20020a056808159a00b00383cc29d6b2sm7001652oiw.51.2023.03.02.06.57.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 06:57:38 -0800 (PST) To: libc-alpha@sourceware.org, "Andreas K . Huettel" , Paul Eggert , Florian Weimer Subject: [PATCH v6 1/3] linux: Use getdents64 on non-LFS readdir Date: Thu, 2 Mar 2023 11:57:30 -0300 Message-Id: <20230302145732.2293756-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230302145732.2293756-1-adhemerval.zanella@linaro.org> References: <20230302145732.2293756-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" It is similar to what non-LFS getdents do (including overflow check). Checked on x86_64-linux-gnu and i686-linux-gnu. --- sysdeps/unix/sysv/linux/readdir.c | 97 +++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 24 deletions(-) diff --git a/sysdeps/unix/sysv/linux/readdir.c b/sysdeps/unix/sysv/linux/readdir.c index 4a4c00ea07..72ba895afe 100644 --- a/sysdeps/unix/sysv/linux/readdir.c +++ b/sysdeps/unix/sysv/linux/readdir.c @@ -20,43 +20,92 @@ #if !_DIRENT_MATCHES_DIRENT64 #include +#include + +# ifndef DIRENT_SET_DP_INO +# define DIRENT_SET_DP_INO(dp, value) (dp)->d_ino = (value) +# endif /* Read a directory entry from DIRP. */ struct dirent * __readdir_unlocked (DIR *dirp) { - struct dirent *dp; int saved_errno = errno; - if (dirp->offset >= dirp->size) + while (1) { - /* We've emptied out our buffer. Refill it. */ - - size_t maxread = dirp->allocation; - ssize_t bytes; - - bytes = __getdents (dirp->fd, dirp->data, maxread); - if (bytes <= 0) + if (dirp->offset >= dirp->size) { - /* Linux may fail with ENOENT on some file systems if the - directory inode is marked as dead (deleted). POSIX - treats this as a regular end-of-directory condition, so - do not set errno in that case, to indicate success. */ - if (bytes == 0 || errno == ENOENT) - __set_errno (saved_errno); + ssize_t bytes = __getdents64 (dirp->fd, dirp->data, + dirp->allocation); + if (bytes <= 0) + { + /* Linux may fail with ENOENT on some file systems if the + directory inode is marked as dead (deleted). POSIX + treats this as a regular end-of-directory condition, so + do not set errno in that case, to indicate success. */ + if (bytes < 0 && errno == ENOENT) + __set_errno (saved_errno); + return NULL; + } + dirp->size = bytes; + + /* Reset the offset into the buffer. */ + dirp->offset = 0; + } + + /* These two pointers might alias the same memory buffer. Standard C + requires that we always use the same type for them, so we must use the + union type. */ + union + { + struct dirent64 dp64; + struct dirent dp; + char *b; + } *inp, *outp; + inp = (void*) &dirp->data[dirp->offset]; + outp = (void*) &dirp->data[dirp->offset]; + + const size_t size_diff = offsetof (struct dirent64, d_name) + - offsetof (struct dirent, d_name); + + /* Since inp->dp64.d_reclen is already aligned for the kernel structure + this may compute a value that is bigger than necessary. */ + size_t old_reclen = inp->dp64.d_reclen; + size_t new_reclen = ALIGN_UP (old_reclen - size_diff, + _Alignof (struct dirent)); + + if (!in_ino_t_range (inp->dp64.d_ino) + || !in_off_t_range (inp->dp64.d_off)) + { + /* Overflow. If there was at least one entry before this one, + return them without error, otherwise signal overflow. */ + if (dirp->offset != 0) + { + __lseek64 (dirp->fd, dirp->offset, SEEK_SET); + outp = (void*)(outp->b - dirp->data); + return &outp->dp; + } + __set_errno (EOVERFLOW); return NULL; } - dirp->size = (size_t) bytes; - /* Reset the offset into the buffer. */ - dirp->offset = 0; + /* Copy the data from INP and access only OUTP. */ + const uint64_t d_ino = inp->dp64.d_ino; + const int64_t d_off = inp->dp64.d_off; + const uint8_t d_type = inp->dp64.d_type; + outp->dp.d_ino = d_ino; + outp->dp.d_off = d_off; + outp->dp.d_reclen = new_reclen; + outp->dp.d_type = d_type; + memmove (outp->dp.d_name, inp->dp64.d_name, + old_reclen - offsetof (struct dirent64, d_name)); + + dirp->filepos = d_off; + dirp->offset += old_reclen; + + return &outp->dp; } - - dp = (struct dirent *) &dirp->data[dirp->offset]; - dirp->offset += dp->d_reclen; - dirp->filepos = dp->d_off; - - return dp; } struct dirent *