Improve simple directory offset wrap behavior
Subsystems (4)
v4 cover Improve simple directory offset wrap behavior
From: Chuck Lever <[email protected]>
The purpose of this series is to construct a set of upstream fixes that can be backported to v6.6 to address CVE-2024-46701.
My original plan was to add a cursor dentry. However, I've found a solution that does not need one. In fact, most or all of the reported issues are gone with 4/5. Thus I'm not sure 5/5 is necessary, but it seems like a robust improvement.
Changes since v3:
- Series is no longer RFC
- Series passes xfstests locally and via NFS export
- Patch 2/5 was replaced; it now removes simple_offset_empty()
- 4/5 and 5/5 were rewritten based on test results
- Patch descriptions have been clarified
This series (still against v6.12) has been pushed to:
https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=tmpfs-fixes
Next step is to try backporting these to v6.6 to see if anything else is needed.
Chuck Lever (5):
libfs: Return ENOSPC when the directory offset range is exhausted Revert "libfs: Add simple_offset_empty()"
Revert "libfs: fix infinite directory reads for offset dir"
libfs: Replace simple_offset end-of-directory detection
libfs: Use d_children list to iterate simple_offset directories
fs/libfs.c | 158 ++++++++++++++++++++++----------------------- include/linux/fs.h | 1 - mm/shmem.c | 4 +- 3 files changed, 81 insertions(+), 82 deletions(-)
Signature · 1 line
2.47.0
PATCH v4 1/5 libfs: Return ENOSPC when the directory offset range is exhausted
From: Chuck Lever <[email protected]>
Testing shows that the EBUSY error return from mtree_alloc_cyclic() leaks into user space. The ERRORS section of "man creat(2)" says:
Quoted 3 lines
EBUSY
O_EXCLwas specified in flags and pathname refers
to a block device that is in use by the system (e.g., it is mounted).
ENOSPC is closer to what applications expect in this situation.
Note that the normal range of simple directory offset values is 2..2^63, so hitting this error is going to be rare to impossible.
--- fs/libfs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/libfs.c +3 -1
@@ -288,7 +288,9 @@ int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) ret =mtree_alloc_cyclic (&octx->mt, &offset, dentry,DIR_OFFSET_MIN ,LONG_MAX , &octx->next_offset,GFP_KERNEL ); -if (ret <0 ) +if (unlikely (ret == -EBUSY )) +return -ENOSPC ; +if (unlikely (ret <0 ))return ret;offset_set (dentry, offset);
Signature · 1 line
2.47.0
PATCH v4 2/5 Revert "libfs: Add simple_offset_empty()"
In reply to [email protected]From: Chuck Lever <[email protected]>
simple_empty() and simple_offset_empty() perform the same task. The latter's use as a canary to find bugs has not found any new issues. A subsequent patch will remove the use of the mtree for iterating directory contents, so revert back to using a similar mechanism for determining whether a directory is indeed empty.
Only one such mechanism is ever needed.
--- fs/libfs.c | 32 -------------------------------- include/linux/fs.h | 1 - mm/shmem.c | 4 ++-- 3 files changed, 2 insertions(+), 35 deletions(-)
fs/libfs.c +0 -32
@@ -327,38 +327,6 @@ void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry)offset_set (dentry,0 ); } -/** -* simple_offset_empty - Check if a dentry can be unlinked -* @dentry: dentry to be tested -* -* Returns 0 if @dentry is a non-empty directory; otherwise returns 1. -*/ -int simple_offset_empty (struct dentry *dentry) -{ -struct inode *inode =d_inode (dentry); -struct offset_ctx *octx; -struct dentry *child; -unsigned long index; -int ret =1 ; - -if (!inode || !S_ISDIR (inode->i_mode)) -return ret; - - index =DIR_OFFSET_MIN ; - octx = inode->i_op->get_offset_ctx (inode); -mt_for_each (&octx->mt, child, index,LONG_MAX ) { -spin_lock (&child->d_lock); -if (simple_positive (child)) { -spin_unlock (&child->d_lock); - ret =0 ; -break ; - } -spin_unlock (&child->d_lock); - } - -return ret; -} -/** * simple_offset_rename - handle directory offsets for rename * @old_dir: parent directory of source entry
include/linux/fs.h +0 -1
@@ -3434,7 +3434,6 @@ struct offset_ctx {void simple_offset_init (struct offset_ctx *octx);int simple_offset_add (struct offset_ctx *octx,struct dentry *dentry);void simple_offset_remove (struct offset_ctx *octx,struct dentry *dentry); -int simple_offset_empty (struct dentry *dentry);int simple_offset_rename (struct inode *old_dir,struct dentry *old_dentry,struct inode *new_dir,struct dentry *new_dentry); int simple_offset_rename_exchange(struct inode *old_dir,
mm/shmem.c +2 -2
@@ -3697,7 +3697,7 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry)static int shmem_rmdir (struct inode *dir,struct dentry *dentry) { -if (!simple_offset_empty (dentry)) +if (!simple_empty (dentry))return -ENOTEMPTY ;drop_nlink (d_inode (dentry));
@@ -3754,7 +3754,7 @@ static int shmem_rename2(struct mnt_idmap *idmap,return simple_offset_rename_exchange (old_dir, old_dentry, new_dir, new_dentry); -if (!simple_offset_empty (new_dentry)) +if (!simple_empty (new_dentry))return -ENOTEMPTY ;if (flags &RENAME_WHITEOUT ) {
Signature · 1 line
2.47.0
PATCH v4 3/5 Revert "libfs: fix infinite directory reads for offset dir"
In reply to [email protected]From: Chuck Lever <[email protected]>
The current directory offset allocator (based on mtree_alloc_cyclic) stores the next offset value to return in octx->next_offset. This mechanism typically returns values that increase monotonically over time. Eventually, though, the newly allocated offset value wraps back to a low number (say, 2) which is smaller than other already- allocated offset values.
Yu Kuai <[email protected]> reports that, after commit 64a7ce76fb90 ("libfs: fix infinite directory reads for offset dir"), if a directory's offset allocator wraps, existing entries are no longer visible via readdir/getdents because offset_readdir() stops listing entries once an entry's offset is larger than octx->next_offset. These entries vanish persistently -- they can be looked up, but will never again appear in readdir(3) output.
The reason for this is that the commit treats directory offsets as monotonically increasing integer values rather than opaque cookies, and introduces this comparison:
if (dentry2offset(dentry) >= last_index) {On 64-bit platforms, the directory offset value upper bound is 2^63 - 1. Directory offsets will monotonically increase for millions of years without wrapping.
On 32-bit platforms, however, LONG_MAX is 2^31 - 1. The allocator can wrap after only a few weeks (at worst).
Revert commit 64a7ce76fb90 ("libfs: fix infinite directory reads for offset dir") to prepare for a fix that can work properly on 32-bit systems and might apply to recent LTS kernels where shmem employs the simple_offset mechanism.
--- fs/libfs.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-)
fs/libfs.c +11 -24
@@ -420,14 +420,6 @@ void simple_offset_destroy(struct offset_ctx *octx)mtree_destroy (&octx->mt); } -static int offset_dir_open (struct inode *inode,struct file *file) -{ -struct offset_ctx *ctx = inode->i_op->get_offset_ctx (inode); - - file->private_data = (void *)ctx->next_offset; -return 0 ; -} -/** * offset_dir_llseek - Advance the read position of a directory descriptor * @file: an open directory whose position is to be updated
@@ -441,9 +433,6 @@ static int offset_dir_open(struct inode *inode, struct file *file)*/ static loff_t offset_dir_llseek (struct file *file,loff_t offset,int whence) { -struct inode *inode = file->f_inode; -struct offset_ctx *ctx = inode->i_op->get_offset_ctx (inode); -switch (whence) {case SEEK_CUR : offset += file->f_pos;
@@ -457,8 +446,7 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) }/* In this case, ->private_data is protected by f_pos_lock */ -if (!offset) - file->private_data = (void *)ctx->next_offset; + file->private_data =NULL ;return vfs_setpos (file, offset,LONG_MAX ); }
@@ -489,7 +477,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,long last_index) +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;
@@ -497,21 +485,17 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lonwhile (true) { dentry =offset_find_next (octx, ctx->pos);if (!dentry) -return ; - -if (dentry2offset (dentry) >= last_index) { -dput (dentry); -return ; - } +return ERR_PTR (-ENOENT );if (!offset_dir_emit (ctx, dentry)) {dput (dentry); -return ; +break ; } ctx->pos =dentry2offset (dentry) +1 ;dput (dentry); } +return NULL ; }/**
@@ -538,19 +522,22 @@ static void offset_iterate_dir(struct inode *inode, struct dir_context *ctx, lonstatic int offset_readdir (struct file *file,struct dir_context *ctx) {struct dentry *dir = file->f_path.dentry; -long last_index = (long )file->private_data;lockdep_assert_held (&d_inode (dir)->i_rwsem);if (!dir_emit_dots (file, ctx))return 0 ; -offset_iterate_dir (d_inode (dir), ctx, last_index); +/* 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);return 0 ; }const struct file_operations simple_offset_dir_operations = { - .open = offset_dir_open, .llseek = offset_dir_llseek, .iterate_shared = offset_readdir, .read = generic_read_dir,
Signature · 1 line
2.47.0
PATCH v4 4/5 libfs: Replace simple_offset end-of-directory detection
In reply to [email protected]From: Chuck Lever <[email protected]>
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).
--- fs/libfs.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-)
fs/libfs.c +22 -16
@@ -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 ; }
Signature · 1 line
2.47.0
PATCH v4 5/5 libfs: Use d_children list to iterate simple_offset directories
In reply to [email protected]From: Chuck Lever <[email protected]>
The mtree mechanism has been effective at creating directory offsets that are stable over multiple opendir instances. However, it has not been able to handle the subtleties of renames that are concurrent with readdir.
Instead of using the mtree to emit entries in the order of their offset values, use it only to map incoming ctx->pos to a starting entry. Then use the directory's d_children list, which is already maintained properly by the dcache, to find the next child to emit.
One of the sneaky things about this is that when the mtree-allocated offset value wraps (which is very rare), looking up ctx->pos++ is not going to find the next entry; it will return NULL. Instead, by following the d_children list, the offset values can appear in any order but all of the entries in the directory will be visited eventually.
Note also that the readdir() is guaranteed to reach the tail of this list. Entries are added only at the head of d_children, and readdir walks from its current position in that list towards its tail.
--- fs/libfs.c | 77 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 20 deletions(-)
fs/libfs.c +57 -20
@@ -243,12 +243,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations);/* simple_offset_add() allocation range */ enum { -DIR_OFFSET_MIN =2 , +DIR_OFFSET_MIN =3 ,DIR_OFFSET_MAX =LONG_MAX -1 , };/* simple_offset_add() never assigns these to a dentry */ enum { +DIR_OFFSET_FIRST =2 ,/* Find first real entry */ DIR_OFFSET_EOD =LONG_MAX ,/* Marks EOD */ };
@@ -456,19 +457,43 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)return vfs_setpos (file, offset,LONG_MAX ); } -static struct dentry *offset_find_next (struct offset_ctx *octx,loff_t offset) +/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked (struct dentry *parent, +struct dentry *dentry) { -MA_STATE (mas, &octx->mt, offset, offset); +struct dentry *found =NULL ; + +hlist_for_each_entry_from (dentry, d_sib) { +if (!simple_positive (dentry)) +continue ; +spin_lock_nested (&dentry->d_lock,DENTRY_D_LOCK_NESTED ); +if (simple_positive (dentry)) + found =dget_dlock (dentry); +spin_unlock (&dentry->d_lock); +if (likely (found)) +break ; + } +return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup (struct file *file,loff_t offset) +{ +struct dentry *parent = file->f_path.dentry;struct dentry *child, *found =NULL ; +struct inode *inode =d_inode (parent); +struct offset_ctx *octx = inode->i_op->get_offset_ctx (inode); + +MA_STATE (mas, &octx->mt, offset, offset);rcu_read_lock (); child =mas_find (&mas,DIR_OFFSET_MAX );if (!child) goto out; -spin_lock (&child->d_lock); -if (simple_positive (child)) - found =dget_dlock (child); -spin_unlock (&child->d_lock); + +spin_lock (&parent->d_lock); + found =find_next_sibling_locked (parent, child); +spin_unlock (&parent->d_lock); out:rcu_read_unlock ();return found;
@@ -477,30 +502,42 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset)static bool offset_dir_emit (struct dir_context *ctx,struct dentry *dentry) {struct inode *inode =d_inode (dentry); -long offset =dentry2offset (dentry); -return ctx->actor (ctx, dentry->d_name.name, dentry->d_name.len, offset, - inode->i_ino,fs_umode_to_dtype (inode->i_mode)); +return dir_emit (ctx, dentry->d_name.name, dentry->d_name.len, + 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 file *file,struct dir_context *ctx) { -struct offset_ctx *octx = inode->i_op->get_offset_ctx (inode); +struct dentry *dir = file->f_path.dentry;struct dentry *dentry; +if (ctx->pos ==DIR_OFFSET_FIRST ) { +spin_lock (&dir->d_lock); + dentry =find_next_sibling_locked (dir,d_first_child (dir)); +spin_unlock (&dir->d_lock); + }else + dentry =offset_dir_lookup (file, ctx->pos); +if (!dentry) + goto out_eod; +while (true) { - dentry =offset_find_next (octx, ctx->pos); -if (!dentry) - goto out_eod; +struct dentry *next; -if (!offset_dir_emit (ctx, dentry)) { -dput (dentry); + ctx->pos =dentry2offset (dentry); +if (!offset_dir_emit (ctx, dentry))break ; - } - ctx->pos =dentry2offset (dentry) +1 ; +spin_lock (&dir->d_lock); + next =find_next_sibling_locked (dir,d_next_sibling (dentry)); +spin_unlock (&dir->d_lock);dput (dentry); + +if (!next) + goto out_eod; + dentry = next; } +dput (dentry);return ; out_eod:
@@ -539,7 +576,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx)if (!dir_emit_dots (file, ctx))return 0 ;if (ctx->pos !=DIR_OFFSET_EOD ) -offset_iterate_dir (d_inode (dir), ctx); +offset_iterate_dir (file, ctx);return 0 ; }
Signature · 1 line
2.47.0
Re: [PATCH v4 5/5] libfs: Use d_children list to iterate simple_offset directories
Quoted 48 lines · On 12/4/24 10:52 AM, [email protected] wrote:
From: Chuck Lever <[email protected]>
The mtree mechanism has been effective at creating directory offsets that are stable over multiple opendir instances. However, it has not been able to handle the subtleties of renames that are concurrent with readdir.
Instead of using the mtree to emit entries in the order of their offset values, use it only to map incoming
ctx->posto a starting entry. Then use the directory'sd_childrenlist, which is already maintained properly by the dcache, to find the next child to emit.One of the sneaky things about this is that when the mtree-allocated offset value wraps (which is very rare), looking up ctx->pos++ is not going to find the next entry; it will return NULL. Instead, by following the
d_childrenlist, the offset values can appear in any order but all of the entries in the directory will be visited eventually.Note also that the
readdir()is guaranteed to reach the tail of this list. Entries are added only at the head ofd_children, and readdir walks from its current position in that list towards its tail.Signed-off-by Chuck Lever <[email protected]>--- fs/libfs.c | 77 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 20 deletions(-)fs/libfs.c +2
-1@@ -243,12 +243,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations);/* simple_offset_add() allocation range */ enum { -DIR_OFFSET_MIN =2 , +DIR_OFFSET_MIN =3 ,DIR_OFFSET_MAX =LONG_MAX -1 , };/* simple_offset_add() never assigns these to a dentry */ enum { +DIR_OFFSET_FIRST =2 ,/* Find first real entry */ DIR_OFFSET_EOD =LONG_MAX ,/* Marks EOD */ };
fs/libfs.c static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) · line 462
Quoted 7 lines
fs/libfs.c +3
-1@@ -456,19 +457,43 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)return vfs_setpos (file, offset,LONG_MAX ); } -static struct dentry *offset_find_next (struct offset_ctx *octx,loff_t offset) +/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked (struct dentry *parent, +struct dentry *dentry)
There might be a better name for this function.
It looks a lot like find_next_child(), but it acts more like scan_positives(). It starts looking for positive dentries starting at @dentry, thus it can return the dentry that was passed in @dentry.
find_positive_from_locked() ??
Quoted 106 lines
fs/libfs.c +52
-18{ -MA_STATE (mas, &octx->mt, offset, offset); +struct dentry *found =NULL ; + +hlist_for_each_entry_from (dentry, d_sib) { +if (!simple_positive (dentry)) +continue ; +spin_lock_nested (&dentry->d_lock,DENTRY_D_LOCK_NESTED ); +if (simple_positive (dentry)) + found =dget_dlock (dentry); +spin_unlock (&dentry->d_lock); +if (likely (found)) +break ; + } +return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup (struct file *file,loff_t offset) +{ +struct dentry *parent = file->f_path.dentry;struct dentry *child, *found =NULL ; +struct inode *inode =d_inode (parent); +struct offset_ctx *octx = inode->i_op->get_offset_ctx (inode); + +MA_STATE (mas, &octx->mt, offset, offset);rcu_read_lock (); child =mas_find (&mas,DIR_OFFSET_MAX );if (!child) goto out; -spin_lock (&child->d_lock); -if (simple_positive (child)) - found =dget_dlock (child); -spin_unlock (&child->d_lock); + +spin_lock (&parent->d_lock); + found =find_next_sibling_locked (parent, child); +spin_unlock (&parent->d_lock); out:rcu_read_unlock ();return found;@@ -477,30 +502,42 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset)static bool offset_dir_emit (struct dir_context *ctx,struct dentry *dentry) {struct inode *inode =d_inode (dentry); -long offset =dentry2offset (dentry); -return ctx->actor (ctx, dentry->d_name.name, dentry->d_name.len, offset, - inode->i_ino,fs_umode_to_dtype (inode->i_mode)); +return dir_emit (ctx, dentry->d_name.name, dentry->d_name.len, + 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 file *file,struct dir_context *ctx) { -struct offset_ctx *octx = inode->i_op->get_offset_ctx (inode); +struct dentry *dir = file->f_path.dentry;struct dentry *dentry; +if (ctx->pos ==DIR_OFFSET_FIRST ) { +spin_lock (&dir->d_lock); + dentry =find_next_sibling_locked (dir,d_first_child (dir)); +spin_unlock (&dir->d_lock); + }else + dentry =offset_dir_lookup (file, ctx->pos); +if (!dentry) + goto out_eod; +while (true) { - dentry =offset_find_next (octx, ctx->pos); -if (!dentry) - goto out_eod; +struct dentry *next; -if (!offset_dir_emit (ctx, dentry)) { -dput (dentry); + ctx->pos =dentry2offset (dentry); +if (!offset_dir_emit (ctx, dentry))break ; - } - ctx->pos =dentry2offset (dentry) +1 ; +spin_lock (&dir->d_lock); + next =find_next_sibling_locked (dir,d_next_sibling (dentry)); +spin_unlock (&dir->d_lock);dput (dentry); + +if (!next) + goto out_eod; + dentry = next; } +dput (dentry);return ; out_eod:@@ -539,7 +576,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx)if (!dir_emit_dots (file, ctx))return 0 ;if (ctx->pos !=DIR_OFFSET_EOD ) -offset_iterate_dir (d_inode (dir), ctx); +offset_iterate_dir (file, ctx);return 0 ; }
Signature · 1 line
Chuck Lever
Re: [PATCH v4 5/5] libfs: Use d_children list to iterate simple_offset directories
Quoted 141 lines · Chuck Lever
Quoted 45 lines · On 12/4/24 10:52 AM, [email protected] wrote:
From: Chuck Lever <[email protected]>
The mtree mechanism has been effective at creating directory offsets that are stable over multiple opendir instances. However, it has not been able to handle the subtleties of renames that are concurrent with readdir.
Instead of using the mtree to emit entries in the order of their offset values, use it only to map incoming
ctx->posto a starting entry. Then use the directory'sd_childrenlist, which is already maintained properly by the dcache, to find the next child to emit.One of the sneaky things about this is that when the mtree-allocated offset value wraps (which is very rare), looking up ctx->pos++ is not going to find the next entry; it will return NULL. Instead, by following the
d_childrenlist, the offset values can appear in any order but all of the entries in the directory will be visited eventually.Note also that the
readdir()is guaranteed to reach the tail of this list. Entries are added only at the head ofd_children, and readdir walks from its current position in that list towards its tail.Signed-off-by Chuck Lever <[email protected]>---
fs/libfs.c| 77 ++++++++++++++++++++++++++++++++++++++++--------------1 file changed, 57 insertions(+), 20 deletions(-)fs/libfs.c +2
-1@@ -243,12 +243,13 @@ EXPORT_SYMBOL(simple_dir_inode_operations); /* simple_offset_add() allocation range */ enum { - DIR_OFFSET_MIN = 2, + DIR_OFFSET_MIN = 3, DIR_OFFSET_MAX = LONG_MAX - 1, }; /* simple_offset_add() never assigns these to a dentry */ enum { + DIR_OFFSET_FIRST = 2, /* Find first real entry */DIR_OFFSET_EOD = LONG_MAX, /* Marks EOD */ };
fs/libfs.c static loff_t offset_dir_llseek(struct file · line 463
Quoted 8 lines
fs/libfs.c +3
-1@@ -456,19 +457,43 @@ static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, offset, LONG_MAX); } -static struct dentry *offset_find_next (struct offset_ctx *octx, loff_t offset) +/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked (struct dentry *parent, + struct dentry *dentry)There might be a better name for this function.
It looks a lot like
find_next_child(), but it acts more likescan_positives(). It starts looking for positive dentries starting at @dentry, thus it can return the dentry that was passed in @dentry.find_positive_from_locked() ??
Quoted 77 lines
fs/libfs.c +27
-5{ - MA_STATE(mas, &octx->mt, offset, offset); + struct dentry *found = NULL; + + hlist_for_each_entry_from(dentry, d_sib) { + if (!simple_positive(dentry)) + continue; + spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(dentry)) + found = dget_dlock(dentry); + spin_unlock(&dentry->d_lock); + if (likely(found)) + break; + } + return found; +} + +static noinline_for_stack struct dentry * +offset_dir_lookup (struct file *file,loff_t offset) +{ + struct dentry *parent = file->f_path.dentry; struct dentry *child, *found = NULL; + struct inode *inode = d_inode(parent); + struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + + MA_STATE(mas, &octx->mt, offset, offset); rcu_read_lock(); child = mas_find(&mas, DIR_OFFSET_MAX); if (!child) goto out; - spin_lock(&child->d_lock); - if (simple_positive(child)) - found = dget_dlock(child); - spin_unlock(&child->d_lock); + + spin_lock(&parent->d_lock); + found = find_next_sibling_locked(parent, child); + spin_unlock(&parent->d_lock);out:
rcu_read_unlock();
return found;fs/libfs.c +14
-10@@ -477,30 +502,42 @@ static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) { struct inode *inode = d_inode(dentry); - long offset = dentry2offset(dentry); - return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset, - inode->i_ino, fs_umode_to_dtype(inode->i_mode)); + return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len, + 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 file *file,struct dir_context *ctx) { - struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode); + struct dentry *dir = file->f_path.dentry; struct dentry *dentry; + if (ctx->pos == DIR_OFFSET_FIRST) { + spin_lock(&dir->d_lock); + dentry = find_next_sibling_locked(dir, d_first_child(dir)); + spin_unlock(&dir->d_lock); + } else + dentry = offset_dir_lookup(file, ctx->pos); + if (!dentry) + goto out_eod; + while (true) { - dentry = offset_find_next(octx, ctx->pos); - if (!dentry) - goto out_eod; + struct dentry *next; - if (!offset_dir_emit(ctx, dentry)) { - dput(dentry);
Quoted 8 lines
fs/libfs.c static struct dentry *offset_find_next(struct offset_ctx *octx, loff_t offset) · line 533
Quoted 8 lines
fs/libfs.c +5
-2+ ctx->pos = dentry2offset(dentry); + if (!offset_dir_emit(ctx, dentry)) break; - } - ctx->pos = dentry2offset(dentry) + 1; + spin_lock(&dir->d_lock); + next = find_next_sibling_locked(dir, d_next_sibling(dentry)); + spin_unlock(&dir->d_lock);
Recent coverity report:
*** CID 1602474: Concurrent data access violations (ATOMICITY) /fs/libfs.c: 536 in offset_iterate_dir()
530
531 ctx->pos = dentry2offset(dentry); 532 if (!offset_dir_emit(ctx, dentry)) 533 break; 534 535 spin_lock(&dir->d_lock); >>> CID 1602474: Concurrent data access violations (ATOMICITY) >>> Using an unreliable value of "dentry" inside the second locked section. If the data that "dentry" depends on was changed by another thread, this use might be incorrect. 536 next = find_next_sibling_locked(dir, d_next_sibling(dentry)); 537 spin_unlock(&dir->d_lock); 538 dput(dentry);
539
540 if (!next)
541 goto out_eod;
As far as I can tell, @dentry's list fields, which are the only fields accessed in find_next_sibling_locked(), are protected by dir->d_lock. We don't care about the other fields.
Not sure if this is a false positive. Is there an annotation that will help clarify this situation?
Quoted 20 lines
Quoted 18 lines
fs/libfs.c +5
-0dput(dentry); + + if (!next) + goto out_eod; + dentry = next; } + dput(dentry); return;
out_eod:fs/libfs.c +1
-1@@ -539,7 +576,7 @@ static int offset_readdir(struct file *file, struct dir_context *ctx) if (!dir_emit_dots(file, ctx)) return 0; if (ctx->pos != DIR_OFFSET_EOD) - offset_iterate_dir(d_inode(dir), ctx); + offset_iterate_dir(file, ctx); return 0; }
Signature · 1 line
Chuck Lever
Al Viro
maintainerRe: [PATCH v4 5/5] libfs: Use d_children list to iterate simple_offset directories
Quoted 5 lines · Chuck Lever
Quoted 5 lines
fs/libfs.c static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) · line 462
Quoted 3 lines
fs/libfs.c +3
-0+/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked (struct dentry *parent, + struct dentry *dentry)There might be a better name for this function.
There might be better calling conventions for it, TBH. AFAICS, all callers are directly surrounded by grabbing/releasing ->d_lock on parent. Why not fold that in, and to hell with any mentionings of "locked" in the name...
Re: [PATCH v4 5/5] libfs: Use d_children list to iterate simple_offset directories
Quoted 11 lines · Al Viro
Quoted 5 lines · Chuck Lever
Quoted 5 lines
fs/libfs.c static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) · line 462
Quoted 3 lines
fs/libfs.c +3
-0+/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked (struct dentry *parent, + struct dentry *dentry)There might be a better name for this function.
There might be better calling conventions for it, TBH. AFAICS, all callers are directly surrounded by grabbing/releasing ->d_lock on parent. Why not fold that in, and to hell with any mentionings of "locked" in the name...
I've tried it both ways, couldn't make up my mind. I'll try it again.
Signature · 1 line
Chuck Lever
Al Viro
maintainerRe: [PATCH v4 5/5] libfs: Use d_children list to iterate simple_offset directories
Quoted 8 lines · Chuck Lever
Quoted 7 lines · Al Viro
Quoted 5 lines · Chuck Lever
Quoted 5 lines
fs/libfs.c static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) · line 462
Quoted 3 lines
fs/libfs.c +3
-0+/* Cf. find_next_child() */ +static struct dentry *find_next_sibling_locked (struct dentry *parent, + struct dentry *dentry)There might be a better name for this function.
Quoted 6 lines
Quoted 4 lines
There might be better calling conventions for it, TBH. AFAICS, all callers are directly surrounded by grabbing/releasing ->d_lock on parent. Why not fold that in, and to hell with any mentionings of "locked" in the name...
I've tried it both ways, couldn't make up my mind. I'll try it again.
Single return there, so it's just a single spin_lock() on entry and spin_unlock() on exit... No idea if it'll make coverity any happier, but it would be easier on human readers.
Quoted 11 lines · On Wed, 04 Dec 2024 10:52:51 -0500, [email protected] wrote:
From: Chuck Lever <[email protected]>
The purpose of this series is to construct a set of upstream fixes that can be backported to v6.6 to address CVE-2024-46701.
My original plan was to add a cursor dentry. However, I've found a solution that does not need one. In fact, most or all of the reported issues are gone with 4/5. Thus I'm not sure 5/5 is necessary, but it seems like a robust improvement.
[...]
Applied to the vfs-6.14.misc branch of the vfs/vfs.git tree. Patches in the vfs-6.14.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-6.14.misc
[1/5] libfs: Return ENOSPC when the directory offset range is exhausted
https://git.kernel.org/vfs/vfs/c/3569cc5260ac [2/5] Revert "libfs: Add simple_offset_empty()" https://git.kernel.org/vfs/vfs/c/06ed2dfc3234 [3/5] Revert "libfs: fix infinite directory reads for offset dir" https://git.kernel.org/vfs/vfs/c/29bc7ff8920d [4/5] libfs: Replace simple_offset end-of-directory detection https://git.kernel.org/vfs/vfs/c/d4849629a4b7 [5/5] libfs: Use d_children list to iterate simple_offset directories https://git.kernel.org/vfs/vfs/c/5ba9a91ae23f
Quoted 31 lines · On 12/4/24 10:52 AM, [email protected] wrote:
From: Chuck Lever <[email protected]>
The purpose of this series is to construct a set of upstream fixes that can be backported to v6.6 to address CVE-2024-46701.
My original plan was to add a cursor dentry. However, I've found a solution that does not need one. In fact, most or all of the reported issues are gone with 4/5. Thus I'm not sure 5/5 is necessary, but it seems like a robust improvement.
Changes since v3:
- Series is no longer RFC
- Series passes xfstests locally and via NFS export
- Patch 2/5 was replaced; it now removessimple_offset_empty()
- 4/5 and 5/5 were rewritten based on test results
- Patch descriptions have been clarifiedThis series (still against v6.12) has been pushed to:
https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=tmpfs-fixes
Next step is to try backporting these to v6.6 to see if anything else is needed.
Chuck Lever (5):
libfs: Return ENOSPC when the directory offset range is exhausted Revert "libfs: Addsimple_offset_empty()"
Revert "libfs: fix infinite directory reads for offset dir"
libfs: Replacesimple_offsetend-of-directory detection
libfs: Used_childrenlist to iteratesimple_offsetdirectories
Quoted 5 lines
fs/libfs.c | 158 ++++++++++++++++++++++----------------------- include/linux/fs.h | 1 - mm/shmem.c | 4 +- 3 files changed, 81 insertions(+), 82 deletions(-)
I've backported these, as a proof of concept, to origin/linux-6.6.y. You can find that here:
https://git.kernel.org/pub/scm/linux/kernel/git/cel/linux.git/log/?h=nfsd-6.6.y
This series passes xfstests, including generic/736.
It would be a little cleaner if I could also backport da549bdd15c2 ("dentry: switch the lists of children to hlist"), but that has similar risks as backporting the Maple tree patches.
Signature · 1 line
Chuck Lever