Skip to content

Commit bb2b005

Browse files
Ming Leigregkh
authored andcommitted
sysfs: check if one entry has been removed before freeing
It might be a kernel disaster if one sysfs entry is freed but still referenced by sysfs tree. Recently Dave and Sasha reported one use-after-free problem on sysfs entry, and the problem has been troubleshooted with help of debug message added in this patch. Given sysfs_get_dirent/sysfs_put are exported APIs, even inside sysfs they are called in many contexts(kobject/attribe add/delete, inode init/drop, dentry lookup/release, readdir, ...), it is healthful to check the removed flag before freeing one entry and dump message if it is freeing without being removed first. Cc: Dave Jones <davej@redhat.com> Cc: Sasha Levin <levinsasha928@gmail.com> Signed-off-by: Ming Lei <ming.lei@canonical.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bcfb87f commit bb2b005

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎fs/sysfs/dir.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
268268
*/
269269
parent_sd = sd->s_parent;
270270

271+
WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED),
272+
"sysfs: free using entry: %s/%s\n",
273+
parent_sd ? parent_sd->s_name : "", sd->s_name);
274+
271275
if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
272276
sysfs_put(sd->s_symlink.target_sd);
273277
if (sysfs_type(sd) & SYSFS_COPY_NAME)
@@ -386,7 +390,7 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
386390

387391
sd->s_name = name;
388392
sd->s_mode = mode;
389-
sd->s_flags = type;
393+
sd->s_flags = type | SYSFS_FLAG_REMOVED;
390394

391395
return sd;
392396

@@ -466,6 +470,9 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
466470
ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
467471
}
468472

473+
/* Mark the entry added into directory tree */
474+
sd->s_flags &= ~SYSFS_FLAG_REMOVED;
475+
469476
return 0;
470477
}
471478

0 commit comments

Comments
 (0)