From patchwork Sun Dec 15 18:58:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13908875 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 C3A02E77183 for ; Sun, 15 Dec 2024 18:58:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 943AA6B008A; Sun, 15 Dec 2024 13:58:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A7276B008C; Sun, 15 Dec 2024 13:58:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D3696B0092; Sun, 15 Dec 2024 13:58:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 43DC96B008A for ; Sun, 15 Dec 2024 13:58:27 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B429E143D98 for ; Sun, 15 Dec 2024 18:58:26 +0000 (UTC) X-FDA: 82898103960.14.09D59AC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf22.hostedemail.com (Postfix) with ESMTP id 419C0C000A for ; Sun, 15 Dec 2024 18:57:55 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TFNIkygx; spf=pass (imf22.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=1734289085; 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=aAvguEMWO+oTZARsOnJKICf5vN1eH41dlsezwlFxfZ8=; b=Ev0kigNGl9SgcZ/FfpLfKqEzjSeckAyGjZevwxAzfVzblYLgMqHu2SJgOAPKOOCvkHglum Lnr4YNE0INYdfQaJEjjvGJZgUvBfccZ1F5ErLCsxIQZ9LldKhCwG1BWlOJPQR0lbBZ8Mvl thaVZBdA1a4HoBCQpPopz1H4xKkQSvc= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=TFNIkygx; spf=pass (imf22.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734289085; a=rsa-sha256; cv=none; b=rQTTJjH/zKmjkOq65Wr8x5UNPOw7T946WMHrS0ZXCakYSOLnUdjGWtLEAM6gEjgx3ZC3Me aNwaYTZxRQG1uBpejdKEJNtUFLbBeHFWEhhRsNeaRfP8yQmMihNzWnNv6O8WKxujxck7Xu nNC12hc1vhuAcvcNGxDKfWPBXGiWT6s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 381655C5BCF; Sun, 15 Dec 2024 18:57:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67B3AC4CEDD; Sun, 15 Dec 2024 18:58:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734289104; bh=Tq/R8xawPLnGo8zVwtB0/wJRvW6e6MlVfxOtKQzQZB4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TFNIkygxYg9NNwhU5zG2gWvQ5bGYpwy0Q4sSDv94OIAKu0Zzn6kHknu10sOtrBnPF bcv3Z58YLBc0XHfJUNBHPIXXbuSHCc82SIN2ugR5NEaoivReeP+HFz+oeUpMpKJnoo 9eTOdX2fDuQ3SzqU0IeI4lfY4cukyYUFDFGqdBYRDXZsDabXj+ALcbOD6PwA0ObMdQ NjrkEf7rJrQARagY2MyMnIdzFTQ9najo79jQhN/Swb8v02DaKvUVJm8W6pBldI83j2 i9BgRNXB86h8PVQJzlW88rEyHX+AWQG8bl5SHkg/Dr/5n472H+1f5mv2+vHWGJ0/r7 a4YEOykfeIWaw== From: cel@kernel.org To: Hugh Dickins , Christian Brauner , Al Viro Cc: , , yukuai3@huawei.com, yangerkun@huaweicloud.com, Chuck Lever Subject: [PATCH v5 4/5] libfs: Replace simple_offset end-of-directory detection Date: Sun, 15 Dec 2024 13:58:15 -0500 Message-ID: <20241215185816.1826975-5-cel@kernel.org> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241215185816.1826975-1-cel@kernel.org> References: <20241215185816.1826975-1-cel@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Stat-Signature: 45u9k9duekij9nz3juaxh7nhb6n7sa54 X-Rspamd-Queue-Id: 419C0C000A X-Rspam-User: X-HE-Tag: 1734289075-899110 X-HE-Meta: U2FsdGVkX18Jvy039aPUTEk/Or8NDyFno1xOB0NM7idDGcZHo0n27mp5Ly8MkzBM2NUHnCyex1d98V2GAMmU1vTle75jWSMPEcTP4hQpoQMySXEAfip7Gj+P76L5Cc4PWK6LRrb3habUtDQbuuVMZItqfFNn5GBh6Iw/+K2brSYJkzuVdR1h7tVkZI4rRGc2R/tMjDi+RTW4kGiIwkX9XvNDlb9sZxhmE+obdoT3QWMeXnCQQvshac0HOqvRKqr66GcIPIiAsZVegV7EjjJ0yjGOGF+k8DiU2nWh25v3OqRl1oNbSOQy5/J8R9o5jC6n73eC1ix5xd/7fRw3AmQZ7LQFZu2iEnGkqDxyhTOcfFT0mYNRLimGGBSuifIXb/KqRXlbNS8v+j+0O476FZliWHJb+4FQbshW+YJ1VBuVsJbi1Bzrs7lkloHmxz/4MjS3+sEGSZ6jQflj7li6J2Z5jRQB4tnePNlbB/gihBELE/aDxGGCMyXonNCS/atY4HOs3rQGmrOQfDp3X45dWjD0V+iA/MpMSHp9pcrOSiHARvp8TtE7Ify7g0lVnFGiLCikvWsb7BSGKJugOpzGMzclA8Y4aWkskzy63D2xGESG2hbhuEW37wJLdRtJ2sYYFJ6wxQPu9dK2c7qcQWqRcQChmNYwwB9BqRDYqsPR+nhK2jFJQWCgve4GZG4hdH9IZQSe38CxexCV8oo3ziKh0OfT7dZFEn0KGYqSqdlwURdrKjErvuhVA5y90qeQpSOCQzNUi5ppkygS7CwaeJeYXzY1rGhJsmszJ6mYJaamA8Op38QCjeKDtGvG2hklzqDibqZwJJV+/rl8K13NtAycWBWb8tarK8Cc2oFn2hUx5ZUGWeQDVaecb9d+1ckO4Cacs3HtuFCKUY+vaHo7iiulJz2Bfo8JucB5/j3Y3rtCl8WW1mJc9swEsCaNc9Bq0N/biLCh+Kh3FKvl7nAN3Nyaf7e DZt1nEYI uQDnqC6hPx42cfJ6u+5k8988IO0Ky21rEkHxGS6ntEnhPkQ8EW2nmquhy+nEnZ6TmB5h2xMXwj8yz4SMJa37vEQwDTNvlUIEV55NoiD39RsS6aL/ZLmy4PBryiMu3HbMV+dfTGu0nE0IWUpoDukyaq5bXNfFbguCSHcRmoXVWaxjfojViPW2+rpQcF5EzpwC2NAhTH3pOGkA4gfBcWYGmy9cTA29QfbOKvIfuWTi4nF4ThBT9UXwuimFO8akAjfZpyef6GCQbE+wTC2jIpkd2T3ZuAZU5q08zZeq7dEOhanh4Yrc4BMZRGiFbfYmUjjLVupxy 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 fdf6ba305112..00dfcfa97edf 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -245,9 +245,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) @@ -291,7 +298,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)) @@ -449,8 +457,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); } @@ -460,7 +466,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); @@ -481,7 +487,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; @@ -489,7 +495,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); @@ -499,7 +505,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; } /** @@ -519,6 +528,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 @@ -531,13 +542,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; }