From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D29E2E7716E for ; Wed, 4 Dec 2024 15:53:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1AE6E6B008A; Wed, 4 Dec 2024 10:53:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 13BC46B008C; Wed, 4 Dec 2024 10:53:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA4086B0092; Wed, 4 Dec 2024 10:53:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id C75CB6B008A for ; Wed, 4 Dec 2024 10:53:09 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 72485A10D5 for ; Wed, 4 Dec 2024 15:53:09 +0000 (UTC) X-FDA: 82857720078.12.9B7A52A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 0B8128000C for ; Wed, 4 Dec 2024 15:52:39 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Q0AZ2/Kj"; spf=pass (imf30.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733327578; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ffFX21GwnmplEQCgvZNu31M5tQ/BDnnEO+F2yArezAA=; b=vWjinAOGt6hyyX069QhElkIYPOVsrNybZd97947l1ieaXZN94zNv1qEgi6zFbuKT+bn/ey Mlji8GBIzvZbMpSmW1S0MC4AMvWkimjx8onqOB+Fp1QUTnj5L60my7ZBB1CgyWkDbIA4SD EHLPAyEKAngRe4P40WNHRUslPUduW1c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733327578; a=rsa-sha256; cv=none; b=2yPWoK22zklfaWoB5Dgt5/jmJrmedyedEVK+X7i+D/MEl4Y995PPcmVAVy24ciAshFSYI0 rYRnAqVeQ3hQzTini/WqNjabq3/CgTTlPnuFRMdbcMMpabqFWvpK6uMIXbTwFnkCOc1qzL /lJmKJJkxqd3iXyNU6DSce1PwiH3kv8= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Q0AZ2/Kj"; spf=pass (imf30.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 037BE5C6EAD; Wed, 4 Dec 2024 15:52:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6D28C4CED1; Wed, 4 Dec 2024 15:53:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733327586; bh=fRN9vGJ7LROdP4ek3V8O3+9HR95gl8BzVkAX4o0+JlY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q0AZ2/KjCsuROd6V7x56n3hoEBUBchfYFAuCF0++umRnEOWMv1+sbIet4cMY14itF CkKZCOq38eR6EpzttfTgzJxVeu4h+0skR0eQYD8Uy9XDds3uM7zFubgpceYxrri4FQ pgaSr49+I+W9cbchnRNbxPOGAPx6VvfzQ9NcuOqaZ2Wu5dZrTtfAjCgDvd7jMj0vE4 kFHhIY3GM7CKMEvGUB+D7RZpa0hvZ1UekvVZJMU5Qcp6tOyr9CqbqwhUpYKSepqKw1 5vtvTPM/y5JDwPHkU9ZMMxg0leBr/36t8gvxufaTUrzb+tbrDvButD4IMg3wmrbo4K M/7DW3b5h+rUw== From: cel@kernel.org To: Hugh Dickens , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v4 4/5] libfs: Replace simple_offset end-of-directory detection Date: Wed, 4 Dec 2024 10:52:55 -0500 Message-ID: <20241204155257.1110338-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241204155257.1110338-1-cel@kernel.org> References: <20241204155257.1110338-1-cel@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 0B8128000C X-Stat-Signature: 4zbgpubajk4qo7rgkfyc4jgkhaxf8ckk X-Rspam-User: X-HE-Tag: 1733327559-65995 X-HE-Meta: U2FsdGVkX19/OjPalIFgczLerrRB8D/gF7GyvoFHSn3//DQJUXeIQUY+FpURpFM9tHAlYqm8W5w3ks5yV691Zrj8YrynG7NzSIxfVFdaq7b0I6VxtNLlGyJ/zntwfN6Sg+KrTwUI/K47YwXypezd3H2rJU8xEs09WkIHq+TaV+B2zbpiVPCgGFrAE519RhBxz78lm24a6NR5blPdBYZFuhA5xKm1epSQwW6ixjegl7Oj84QpQo9DlWM0Fc8WTc+bHjUnAW/Iv0QufvIcMMWHAi99qc1TB53Bx/rhVsuGiGqkB+VTmNrUkLDU7IbWtk3Yj/PZi8NXGw0Ktm+MkVZ2wdSasJlp/9BNEdB8aeIc1sKGdjLvZ+S0SjP/Ob00vSJmuGrIgowXugflr+7UeZmQDMlgxkuxkq1/JdCW2PVcn8hE0rR6UgbILWo+iMrGWvRuUbqHhfhnlrGmDnjwpvbYWMHdpjWvAgTWU79ET49oKKwpKkyItSB8zPauDZCEYNa1jRt1uh8vHzoafOs3YvrYG55I7/EZraA+y4Gw2FhL9R9srACt2iKNCNyan6TSra5M1Em/pgj5TT/H7dahc1IyhixboUsYoz4w7rzyYHTKJXoQHxQLO0q7fYV8BNBBWqhejyU/2NuVZEcPXSPUcyADz8g9/tNwijIH5C0OaKQdXI+V5SdcwGgiLEh81arelAm13H/yAPRIv0GgApVWHjQhndx9MVn25SjfQkkNrDDuzcGGCdX8fZXYiym/aEZqu3CAvjO+yFd8wJB21xKx6juh8ShMc0PvAsdWoO/b8CpP/mlKiK9rh41iyTvdK9inZhuBycg2ENB7OEwknpatMRJV+tSKfvQr5z1u9t1FjNvyTtfv6mns0PyVil0u0ShNG8GRLJdDH2kAmg5y3IK7LZ47/rQWzfR9smYd8XX1lcPMG53OrdVKm1JfTyyjNbbM5TF7iDBotC4SDAq4+O5FKZH JDREX4RS fWOyaCxGRS2UGOCPtjYebWbiQy6RMTv9c6hL03qlkpGYlkP3MDOU4WRwcov7R5s2XcrrQcCqpIur3VpCQJpRF4Yty9ORHxXcNS83Vp7DKlyVLmmCX+KZ+r5Yf2VCUxpFwAiMEtGcuL/mVqXO1YF46aBpDKPPfKz3dSv1tHcfJjy0sF0fluDlp6/qZd7LAIryWifGhckZjavRXhdmt1klVi7XP+40Td1pI58TUgv03iraWVLYFyvH0nnoZYPkeU2dqq9sgSmcfJ9R6wruRsHEIhrEWJwzD+sRMfq9rAIbsJTdX3Q93oSC5gk8xuThyHiZWWkF+ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Chuck Lever According to getdents(3), the d_off field in each returned directory entry points to the next entry in the directory. The d_off field in the last returned entry in the readdir buffer must contain a valid offset value, but if it points to an actual directory entry, then readdir/getdents can loop. This patch introduces a specific fixed offset value that is placed in the d_off field of the last entry in a directory. Some user space applications assume that the EOD offset value is larger than the offsets of real directory entries, so the largest possible offset value is reserved for this purpose. This new value is never allocated by simple_offset_add(). When ->iterate_dir() returns, getdents{64} inserts the ctx->pos value into the d_off field of the last valid entry in the readdir buffer. When it hits EOD, offset_readdir() sets ctx->pos to the EOD offset value so the last entry is updated to point to the EOD marker. When trying to read the entry at the EOD offset, offset_readdir() terminates immediately. It is worth noting that using a Maple tree for directory offset value allocation does not guarantee a 63-bit range of values -- on platforms where "long" is a 32-bit type, the directory offset value range is still 0..(2^31 - 1). Fixes: 796432efab1e ("libfs: getdents() should return 0 after reaching EOD") Signed-off-by: Chuck Lever --- fs/libfs.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 461384fb6119..fcb2cdf6e3f3 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -241,9 +241,16 @@ const struct inode_operations simple_dir_inode_operations = { }; EXPORT_SYMBOL(simple_dir_inode_operations); -/* 0 is '.', 1 is '..', so always start with offset 2 or more */ +/* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = 2, + DIR_OFFSET_MAX = LONG_MAX - 1, +}; + +/* simple_offset_add() never assigns these to a dentry */ +enum { + DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */ + }; static void offset_set(struct dentry *dentry, long offset) @@ -287,7 +294,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) return -EBUSY; ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN, - LONG_MAX, &octx->next_offset, GFP_KERNEL); + DIR_OFFSET_MAX, &octx->next_offset, + GFP_KERNEL); if (unlikely(ret == -EBUSY)) return -ENOSPC; if (unlikely(ret < 0)) @@ -445,8 +453,6 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return -EINVAL; } - /* In this case, ->private_data is protected by f_pos_lock */ - file->private_data = NULL; return vfs_setpos(file, offset, LONG_MAX); } @@ -456,7 +462,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) struct dentry *child, *found = NULL; rcu_read_lock(); - child = mas_find(&mas, LONG_MAX); + child = mas_find(&mas, DIR_OFFSET_MAX); if (!child) goto out; spin_lock(&child->d_lock); @@ -477,7 +483,7 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) inode->i_ino, fs_umode_to_dtype(inode->i_mode)); } -static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) +static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx) { struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); struct dentry *dentry; @@ -485,7 +491,7 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) while (true) { dentry = offset_find_next(octx, ctx->pos); if (!dentry) - return ERR_PTR(-ENOENT); + goto out_eod; if (!offset_dir_emit(ctx, dentry)) { dput(dentry); @@ -495,7 +501,10 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) ctx->pos = dentry2offset(dentry) + 1; dput(dentry); } - return NULL; + return; + +out_eod: + ctx->pos = DIR_OFFSET_EOD; } /** @@ -515,6 +524,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx) * * On return, @ctx->pos contains an offset that will read the next entry * in this directory when offset_readdir() is called again with @ctx. + * Caller places this value in the d_off field of the last entry in the + * user's buffer. * * Return values: * %0 - Complete @@ -527,13 +538,8 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; - - /* In this case, ->private_data is protected by f_pos_lock */ - if (ctx->pos == DIR_OFFSET_MIN) - file->private_data = NULL; - else if (file->private_data == ERR_PTR(-ENOENT)) - return 0; - file->private_data = offset_iterate_dir(d_inode(dir), ctx); + if (ctx->pos != DIR_OFFSET_EOD) + offset_iterate_dir(d_inode(dir), ctx); return 0; } -- 2.47.0