From patchwork Mon Mar 13 19:34:17 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: 66318 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 248363857C48 for ; Mon, 13 Mar 2023 19:35:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 248363857C48 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1678736135; 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=nAnDGj9UjvxamxL1HO5HaO3GCpNAAQmPOQ9DvVOebIrJVJppnLozzaoHRUo/z9H6x RPqav2jHlpiaHXDlPKzkAxCFsSgdoYIsWHKLJLvac3CPJqExziTD2blo5c792LlHB0 HNssRkWBuwviyQoOdN9rnXj00jt23Sy51HV9sB5o= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id CB3C23858D32 for ; Mon, 13 Mar 2023 19:34:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CB3C23858D32 Received: by mail-oi1-x22f.google.com with SMTP id bp19so10261556oib.4 for ; Mon, 13 Mar 2023 12:34:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678736066; 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=Q9S/UPKmZfHRD3kV70WpNJUqGEWTb66u8KfMeYFTKKwfkDUN9Ckx3k4zIRumBQRocs 2AAlriLyJ2ur3jxUe4+Afyhaelj9clBfyYH4qBc3l2T0MwHRtmjLPJw4I4XQFn4rXg// KvmSmRKvLKDRYYtWz8AxGmZI8bvVqmA8DNL+/sT06pMyBvC3KBCcEwKGgOdaEXrS//Nw wXL/u7wvW9m1GKRpB7oAWI/DFZI5m8HYQT+9V6nqlPmFkVUyF9WczOe2UWqZ2gcZUdHV pwt14Yjgw+CyFkaL3Fs7J4oYD+eCLZcmFfZ8JxTzvRJOyakyXt3FADX12iiIi7OBK7eb koaA== X-Gm-Message-State: AO0yUKUsSt1Ci31Z34pg2KUWV6p62dGMrrZ2BXce0La/I3/mTEJS+ueO 0krPU2odmgCk24EOBHE05jRYkFXN1lsO6JX/ONjYfA== X-Google-Smtp-Source: AK7set+FbEwNfjN1QVtH4BozTCkqz5gYp1ymei4u7JHwehTy+J8J0h0KXtkD0kaALCbpxniYVqDuGA== X-Received: by 2002:a05:6808:2014:b0:383:e169:cf6 with SMTP id q20-20020a056808201400b00383e1690cf6mr6314690oiw.24.1678736066391; Mon, 13 Mar 2023 12:34:26 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:544b:9bad:e673:fbfa:30eb]) by smtp.gmail.com with ESMTPSA id zf9-20020a0568716a8900b00172426ebe58sm325724oab.27.2023.03.13.12.34.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 12:34:25 -0700 (PDT) To: libc-alpha@sourceware.org, "Andreas K . Huettel" , Paul Eggert , Florian Weimer Subject: [PATCH v7 1/3] linux: Use getdents64 on non-LFS readdir Date: Mon, 13 Mar 2023 16:34:17 -0300 Message-Id: <20230313193419.36985-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230313193419.36985-1-adhemerval.zanella@linaro.org> References: <20230313193419.36985-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 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 *