RE: [RESEND PATCH v2] kernfs: fix dentry unexpected skip

From: Hatayama, Daisuke
Date: Mon May 28 2018 - 09:09:12 EST


Hi,

I attach a reproducer for this issue.

Expand repro.tar.gz and run two commands:

# make
# ./repro.sh

Then, repro.sh will terminate when the issue is reproduced.

In this reproducer, we prepare AtvbAC0jwH.ko and U1cN9ZbARQ.ko having the same hash value.
Then, install U1cN9ZbARQ.ko and then repeating installing and removing AtvbAC0jwH.ko over and over.
Note that AtvbAC0jwH is smaller than U1cN9ZbARQ in the string comparison.
The issue is that output of ls -1 /sys/module contains NO U1cN9ZbARQ.ko.

I found a pair of AtvbAC0jwH and U1cN9ZbARQ using kernhash.c, retrieved from fs/kernfs/dirs.c,
contained in repro.tar.gz like:

# ./kernfshash
AtvbAC0jwH U1cN9ZbARQ 6b2609c5

> -----Original Message-----
> From: linux-kernel-owner@xxxxxxxxxxxxxxx
> [mailto:linux-kernel-owner@xxxxxxxxxxxxxxx] On Behalf Of Hatayama, Daisuke
> Sent: Monday, May 28, 2018 9:54 PM
> To: 'gregkh@xxxxxxxxxxxxxxxxxxx' <gregkh@xxxxxxxxxxxxxxxxxxx>;
> 'tj@xxxxxxxxxx' <tj@xxxxxxxxxx>
> Cc: Okajima, Toshiyuki <toshi.okajima@xxxxxxxxxxxxxx>;
> linux-kernel@xxxxxxxxxxxxxxx; 'ebiederm@xxxxxxxxxxxxxxxxxx'
> <ebiederm@xxxxxxxxxxxxxxxxxx>
> Subject: [RESEND PATCH v2] kernfs: fix dentry unexpected skip
>
> kernfs_dir_next_pos() overlooks the situation that the dentry
> corresponding to a given pos object has already been inactive. Hence,
> when kernfs_dir_pos() returns the dentry with a hash value larger than
> the original one, kernfs_dir_next_pos() returns the dentry next to the
> one returned by kernfs_dir_pos(). As a result, the dentry returned by
> kernfs_dir_pos() is skipped.
>
> To fix this issue, try to find a next node only when the returned
> object is less than or equal to the original one.
>
> Note that this implementation focuses on getting guarantee that the
> existing nodes are never skipped, not interested in the other nodes
> that are added or removed during the process.
>
> We found this issue during a stress test that repeatedly reads
> /sys/module directory to get a list of the currently loaded kernel
> modules while repeatedly loading and unloading kernel modules
> simultaneously.
>
> v2: Fix the case where nodes with the same hash but with the name
> larger than the original node could still be skipped. Use
> kernfs_sd_compare() to compare kernfs_node objects. Imporove patch
> description.
>
> Signed-off-by: HATAYAMA Daisuke <d.hatayama@xxxxxxxxxxxxxx>
> Suggested-by: Toshiyuki Okajima <toshi.okajima@xxxxxxxxxxxxxx>
> Cc: Eric W. Biederman <ebiederm@xxxxxxxxxxxxxxxxxx>
> ---
> fs/kernfs/dir.c | 4 +++-
> 1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c
> index 89d1dc1..3aeeb7a 100644
> --- a/fs/kernfs/dir.c
> +++ b/fs/kernfs/dir.c
> @@ -1621,8 +1621,10 @@ static int kernfs_dir_fop_release(struct inode *inode,
> struct file *filp)
> static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
> struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
> {
> + struct kernfs_node *orig = pos;
> +
> pos = kernfs_dir_pos(ns, parent, ino, pos);
> - if (pos) {
> + if (pos && kernfs_sd_compare(pos, orig) <= 0) {
> do {
> struct rb_node *node = rb_next(&pos->rb);
> if (!node)
> --
> 1.7.1
>
>
>
>
>
>

Attachment: repro.tar.gz
Description: repro.tar.gz