Skip to content

Commit 68a3a65

Browse files
chuckleverbrauner
authored andcommitted
libfs: Replace simple_offset end-of-directory detection
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]>
1 parent b662d85 commit 68a3a65

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
@@ -245,9 +245,15 @@ const struct inode_operations simple_dir_inode_operations = {
245245
};
246246
EXPORT_SYMBOL(simple_dir_inode_operations);
247247

248-
/* 0 is '.', 1 is '..', so always start with offset 2 or more */
248+
/* simple_offset_add() never assigns these to a dentry */
249249
enum {
250-
DIR_OFFSET_MIN = 2,
250+
DIR_OFFSET_EOD = S32_MAX,
251+
};
252+
253+
/* simple_offset_add() allocation range */
254+
enum {
255+
DIR_OFFSET_MIN = 2,
256+
DIR_OFFSET_MAX = DIR_OFFSET_EOD - 1,
251257
};
252258

253259
static void offset_set(struct dentry *dentry, long offset)
@@ -291,7 +297,8 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
291297
return -EBUSY;
292298

293299
ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
294-
LONG_MAX, &octx->next_offset, GFP_KERNEL);
300+
DIR_OFFSET_MAX, &octx->next_offset,
301+
GFP_KERNEL);
295302
if (unlikely(ret < 0))
296303
return ret == -EBUSY ? -ENOSPC : ret;
297304

@@ -447,8 +454,6 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
447454
return -EINVAL;
448455
}
449456

450-
/* In this case, ->private_data is protected by f_pos_lock */
451-
file->private_data = NULL;
452457
return vfs_setpos(file, offset, LONG_MAX);
453458
}
454459

@@ -458,7 +463,7 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset)
458463
struct dentry *child, *found = NULL;
459464

460465
rcu_read_lock();
461-
child = mas_find(&mas, LONG_MAX);
466+
child = mas_find(&mas, DIR_OFFSET_MAX);
462467
if (!child)
463468
goto out;
464469
spin_lock(&child->d_lock);
@@ -479,15 +484,15 @@ static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
479484
inode->i_ino, fs_umode_to_dtype(inode->i_mode));
480485
}
481486

482-
static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
487+
static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
483488
{
484489
struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
485490
struct dentry *dentry;
486491

487492
while (true) {
488493
dentry = offset_find_next(octx, ctx->pos);
489494
if (!dentry)
490-
return ERR_PTR(-ENOENT);
495+
goto out_eod;
491496

492497
if (!offset_dir_emit(ctx, dentry)) {
493498
dput(dentry);
@@ -497,7 +502,10 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
497502
ctx->pos = dentry2offset(dentry) + 1;
498503
dput(dentry);
499504
}
500-
return NULL;
505+
return;
506+
507+
out_eod:
508+
ctx->pos = DIR_OFFSET_EOD;
501509
}
502510

503511
/**
@@ -517,6 +525,8 @@ static void *offset_iterate_dir(struct inode *inode, struct dir_context *ctx)
517525
*
518526
* On return, @ctx->pos contains an offset that will read the next entry
519527
* in this directory when offset_readdir() is called again with @ctx.
528+
* Caller places this value in the d_off field of the last entry in the
529+
* user's buffer.
520530
*
521531
* Return values:
522532
* %0 - Complete
@@ -529,13 +539,8 @@ static int offset_readdir(struct file *file, struct dir_context *ctx)
529539

530540
if (!dir_emit_dots(file, ctx))
531541
return 0;
532-
533-
/* In this case, ->private_data is protected by f_pos_lock */
534-
if (ctx->pos == DIR_OFFSET_MIN)
535-
file->private_data = NULL;
536-
else if (file->private_data == ERR_PTR(-ENOENT))
537-
return 0;
538-
file->private_data = offset_iterate_dir(d_inode(dir), ctx);
542+
if (ctx->pos != DIR_OFFSET_EOD)
543+
offset_iterate_dir(d_inode(dir), ctx);
539544
return 0;
540545
}
541546

0 commit comments

Comments
 (0)