aboutsummaryrefslogtreecommitdiffstats
path: root/fs/sysfs
diff options
context:
space:
mode:
authorMing Lei <ming.lei@canonical.com>2013-04-04 10:22:37 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2013-04-05 18:35:52 -0400
commitbb2b0051d7b0772ea9d0b4be900c2d965093f5d7 (patch)
treeea77b283a68b2b2a5cbcf29706d8c2d6361d18e6 /fs/sysfs
parentbcfb87fb75fa3a9b96c8a73d19166897d167fe3f (diff)
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>
Diffstat (limited to 'fs/sysfs')
-rw-r--r--fs/sysfs/dir.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
index 1bf016b5e88f..e8e0e71b29d5 100644
--- a/fs/sysfs/dir.c
+++ b/fs/sysfs/dir.c
@@ -268,6 +268,10 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
268 */ 268 */
269 parent_sd = sd->s_parent; 269 parent_sd = sd->s_parent;
270 270
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
271 if (sysfs_type(sd) == SYSFS_KOBJ_LINK) 275 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
272 sysfs_put(sd->s_symlink.target_sd); 276 sysfs_put(sd->s_symlink.target_sd);
273 if (sysfs_type(sd) & SYSFS_COPY_NAME) 277 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)
386 390
387 sd->s_name = name; 391 sd->s_name = name;
388 sd->s_mode = mode; 392 sd->s_mode = mode;
389 sd->s_flags = type; 393 sd->s_flags = type | SYSFS_FLAG_REMOVED;
390 394
391 return sd; 395 return sd;
392 396
@@ -466,6 +470,9 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
466 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 470 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
467 } 471 }
468 472
473 /* Mark the entry added into directory tree */
474 sd->s_flags &= ~SYSFS_FLAG_REMOVED;
475
469 return 0; 476 return 0;
470} 477}
471 478