diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c index 2fbdff6..87cceed 100644 --- a/fs/sysfs/dir.c +++ b/fs/sysfs/dir.c @@ -280,6 +280,11 @@ void release_sysfs_dirent(struct sysfs_dirent * sd) * sd->s_parent won't change beneath us. */ parent_sd = sd->s_parent; + if(!(sd->s_flags & SYSFS_FLAG_REMOVED)) { + printk("%s-%d sysfs_dirent use after free: %s-%s\n", + __func__, __LINE__, parent_sd->s_name, sd->s_name); + dump_stack(); + } if (sysfs_type(sd) == SYSFS_KOBJ_LINK) sysfs_put(sd->s_symlink.target_sd); @@ -962,6 +967,12 @@ static struct sysfs_dirent *sysfs_dir_pos(const void *ns, int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && pos->s_parent == parent_sd && hash == pos->s_hash; + + if ((atomic_read(&pos->s_count) == 1)) { + printk("%s-%d sysfs_dirent use after free: %s(%s)-%s, %lld-%u\n", + __func__, __LINE__, parent_sd->s_name, pos->s_parent->s_name, + pos->s_name, hash, pos->s_hash); + } sysfs_put(pos); if (!valid) pos = NULL; @@ -1008,7 +1019,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) { struct dentry *dentry = filp->f_path.dentry; struct sysfs_dirent * parent_sd = dentry->d_fsdata; - struct sysfs_dirent *pos = filp->private_data; + struct sysfs_dirent *pos; enum kobj_ns_type type; const void *ns; ino_t ino; @@ -1016,6 +1027,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) type = sysfs_ns_type(parent_sd); ns = sysfs_info(dentry->d_sb)->ns[type]; + mutex_lock(&sysfs_mutex); if (filp->f_pos == 0) { ino = parent_sd->s_ino; if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0) @@ -1029,7 +1041,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0) filp->f_pos++; } - mutex_lock(&sysfs_mutex); + pos = filp->private_data; for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos); pos; pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) { @@ -1044,17 +1056,15 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) filp->f_pos = pos->s_hash; filp->private_data = sysfs_get(pos); - mutex_unlock(&sysfs_mutex); ret = filldir(dirent, name, len, filp->f_pos, ino, type); - mutex_lock(&sysfs_mutex); if (ret < 0) break; } - mutex_unlock(&sysfs_mutex); if ((filp->f_pos > 1) && !pos) { /* EOF */ filp->f_pos = INT_MAX; filp->private_data = NULL; } + mutex_unlock(&sysfs_mutex); return 0; }