Skip to content

Commit 0e191c6

Browse files
chuckleversmb49
authored andcommitted
libfs: Replace simple_offset end-of-directory detection
BugLink: https://bugs.launchpad.net/bugs/2109640 [ Upstream commit 68a3a65 ] 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 valid 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). For broad compatibility with 32-bit user space, the largest tmpfs directory cookie value is now S32_MAX. Fixes: 796432e ("libfs: getdents() should return 0 after reaching EOD") Signed-off-by: Chuck Lever <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]> [ cel: adjusted to apply to origin/linux-6.6.y ] Signed-off-by: Chuck Lever <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> [diewald: applied backport from v6.12.12 instead] Signed-off-by: Manuel Diewald <[email protected]> Signed-off-by: Stefan Bader <[email protected]>
1 parent 593cd05 commit 0e191c6

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

fs/libfs.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,15 @@ const struct inode_operations simple_dir_inode_operations = {
240240
};
241241
EXPORT_SYMBOL(simple_dir_inode_operations);
242242

243-
/* 0 is '.', 1 is '..', so always start with offset 2 or more */
243+
/* simple_offset_add() never assigns these to a dentry */
244244
enum {
245-
DIR_OFFSET_MIN = 2,
245+
DIR_OFFSET_EOD = S32_MAX,
246+
};
247+
248+
/* simple_offset_add() allocation range */
249+
enum {
250+
DIR_OFFSET_MIN = 2,
251+
DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1,
246252
};
247253

248254
static void offset_set(struct dentry *dentry, long offset)
@@ -286,7 +292,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
286292
return -EBUSY;
287293

288294
ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
289-
LONG_MAX, &octx->next_offset, GFP_KERNEL);
295+
DIR_OFFSET_MAX, &octx->next_offset,
296+
GFP_KERNEL);
290297
if (ret < 0)
291298
return ret;
292299

@@ -474,8 +481,6 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
474481
return -EINVAL;
475482
}
476483

477-
/* In this case, ->private_data is protected by f_pos_lock */
478-
file->private_data = NULL;
479484
return vfs_setpos(file, offset, LONG_MAX);
480485
}
481486

@@ -485,7 +490,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset)
485490
struct dentry *child, *found = NULL;
486491

487492
rcu_read_lock();
488-
child = mas_find(&mas, LONG_MAX);
493+
child = mas_find(&mas, DIR_OFFSET_MAX);
489494
if (!child)
490495
goto out;
491496
spin_lock(&child->d_lock);
@@ -506,15 +511,15 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
506511
inode->i_ino, fs_umode_to_dtype(inode->i_mode));
507512
}
508513

509-
static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
514+
static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
510515
{
511516
struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
512517
struct dentry *dentry;
513518

514519
while (true) {
515520
dentry = offset_find_next(octx, ctx->pos);
516521
if (!dentry)
517-
return ERR_PTR(-ENOENT);
522+
goto out_eod;
518523

519524
if (!offset_dir_emit(ctx, dentry)) {
520525
dput(dentry);
@@ -524,7 +529,10 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
524529
ctx->pos = dentry2offset(dentry) + 1;
525530
dput(dentry);
526531
}
527-
return NULL;
532+
return;
533+
534+
out_eod:
535+
ctx->pos = DIR_OFFSET_EOD;
528536
}
529537

530538
/**
@@ -544,6 +552,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
544552
*
545553
* On return, @ctx->pos contains an offset that will read the next entry
546554
* in this directory when offset_readdir() is called again with @ctx.
555+
* Caller places this value in the d_off field of the last entry in the
556+
* user's buffer.
547557
*
548558
* Return values:
549559
* %0 - Complete
@@ -556,13 +566,8 @@ static int offset_readdir(struct file *file, struct dir_context *ctx)
556566

557567
if (!dir_emit_dots(file, ctx))
558568
return 0;
559-
560-
/* In this case, ->private_data is protected by f_pos_lock */
561-
if (ctx->pos == DIR_OFFSET_MIN)
562-
file->private_data = NULL;
563-
else if (file->private_data == ERR_PTR(-ENOENT))
564-
return 0;
565-
file->private_data = offset_iterate_dir(d_inode(dir), ctx);
569+
if (ctx->pos != DIR_OFFSET_EOD)
570+
offset_iterate_dir(d_inode(dir), ctx);
566571
return 0;
567572
}
568573

0 commit comments

Comments
 (0)