Skip to contentSkip to conversation

sysfs: check if one entry has been removed before freeing

PATCH In review Ming Lei 3 participants 7 messages Apr 3, 2013 – Apr 4, 2013 Driver core

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH 1/1 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 <[email protected]>
Cc Sasha Levin <[email protected]>
---
 fs/sysfs/dir.c |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
fs/sysfs/dir.c +11 -1
index 1bf016b..328ef9b 100644
@@ -268,6 +268,13 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
 	 */
 	parent_sd = sd->s_parent;
 
+	if (unlikely(!(sd->s_flags & SYSFS_FLAG_REMOVED))) {
+		printk(KERN_ERR "sysfs: free using entry: %s/%s\n",
+			parent_sd ? parent_sd->s_name : "",
+			sd->s_name);
+		BUG();
+	}
+
 	if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
 		sysfs_put(sd->s_symlink.target_sd);
 	if (sysfs_type(sd) & SYSFS_COPY_NAME)
@@ -386,7 +393,7 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
 
 	sd->s_name = name;
 	sd->s_mode = mode;
-	sd->s_flags = type;
+	sd->s_flags = type | SYSFS_FLAG_REMOVED;
 
 	return sd;
 
@@ -466,6 +473,9 @@ int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
 		ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
 	}
 
+	/* Mark the entry added into directory tree */
+	sd->s_flags &= ~SYSFS_FLAG_REMOVED;
+
 	return 0;
 }
 
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 5 lines · Ming Lei
fs/sysfs/dir.c +0 -0
index 1bf016b..328ef9b 100644

fs/sysfs/dir.c void release_sysfs_dirent(struct sysfs_dirent * sd) · line 276

Quoted 9 lines
fs/sysfs/dir.c +6 -0
@@ -268,6 +268,13 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
 	 */
 	parent_sd = sd->s_parent;
 
+	if (unlikely(!(sd->s_flags & SYSFS_FLAG_REMOVED))) {
+		printk(KERN_ERR "sysfs: free using entry: %s/%s\n",
+			parent_sd ? parent_sd->s_name : "",
+			sd->s_name);
+		BUG();
+	}

Please use WARN instead of BUG. For an in-ram filesystem like sysfs, there's no real reason to lock-up the machine in this way making it harder to debug.

Dave

author
#2 lore raw
Quoted 10 lines · Dave Jones
Quoted 8 lines · Ming Lei
fs/sysfs/dir.c +0 -0
index 1bf016b..328ef9b 100644
@@ -268,6 +268,13 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
      */
     parent_sd = sd->s_parent;
 
Quoted 10 lines

fs/sysfs/dir.c void release_sysfs_dirent(struct sysfs_dirent * sd) · line 276

Quoted 6 lines
fs/sysfs/dir.c +6 -0
+    if (unlikely(!(sd->s_flags & SYSFS_FLAG_REMOVED))) {
+            printk(KERN_ERR "sysfs: free using entry: %s/%s\n",
+                    parent_sd ? parent_sd->s_name : "",
+                    sd->s_name);
+            BUG();
+    }

Please use WARN instead of BUG. For an in-ram filesystem like sysfs, there's no real reason to lock-up the machine in this way making it harder to debug.

If WARN is used, the freed memory will be allocated to other kernel components, then sysfs may change the memory and cause destruction, so maybe it is better to use BUG to stop kernel.

Thanks,
--
Ming Lei

#3 lore raw
Quoted 18 lines · Ming Lei
Quoted 17 lines · Dave Jones
Quoted 14 lines · Ming Lei
fs/sysfs/dir.c +6 -0
index 1bf016b..328ef9b 100644
@@ -268,6 +268,13 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
      */
     parent_sd = sd->s_parent;
 
+    if (unlikely(!(sd->s_flags & SYSFS_FLAG_REMOVED))) {
+            printk(KERN_ERR "sysfs: free using entry: %s/%s\n",
+                    parent_sd ? parent_sd->s_name : "",
+                    sd->s_name);
+            BUG();
+    }
Quoted 7 lines
Quoted 3 lines

Please use WARN instead of BUG. For an in-ram filesystem like sysfs, there's no real reason to lock-up the machine in this way making it harder to debug.

If WARN is used, the freed memory will be allocated to other kernel components, then sysfs may change the memory and cause destruction, so maybe it is better to use BUG to stop kernel.

No, it's never ok to call BUG(), sorry, please fix this.

greg k-h

author
#4 lore raw
Quoted 23 lines · Greg Kroah-Hartman
Quoted 22 lines · Ming Lei
Quoted 20 lines · Dave Jones
Quoted 5 lines · Ming Lei
fs/sysfs/dir.c +0 -0
index 1bf016b..328ef9b 100644

fs/sysfs/dir.c void release_sysfs_dirent(struct sysfs_dirent * sd) · line 276

Quoted 9 lines
fs/sysfs/dir.c +6 -0
@@ -268,6 +268,13 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
      */
     parent_sd = sd->s_parent;
 
+    if (unlikely(!(sd->s_flags & SYSFS_FLAG_REMOVED))) {
+            printk(KERN_ERR "sysfs: free using entry: %s/%s\n",
+                    parent_sd ? parent_sd->s_name : "",
+                    sd->s_name);
+            BUG();
+    }

Please use WARN instead of BUG. For an in-ram filesystem like sysfs, there's no real reason to lock-up the machine in this way making it harder to debug.

Quoted 5 lines
Quoted 3 lines

If WARN is used, the freed memory will be allocated to other kernel components, then sysfs may change the memory and cause destruction, so maybe it is better to use BUG to stop kernel.

No, it's never ok to call BUG(), sorry, please fix this.

Sorry, could you explain it in a bit detail? IMO, it is really a bug when code runs here, and there are much similar BUG_ON() uses in current sysfs code too.

Thanks,
--
Ming Lei

#5 lore raw
Quoted 29 lines · Ming Lei
Quoted 27 lines · Greg Kroah-Hartman
Quoted 25 lines · Ming Lei
Quoted 10 lines · Dave Jones
Quoted 8 lines · Ming Lei
fs/sysfs/dir.c +0 -0
index 1bf016b..328ef9b 100644
@@ -268,6 +268,13 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
      */
     parent_sd = sd->s_parent;
 
Quoted 10 lines

fs/sysfs/dir.c void release_sysfs_dirent(struct sysfs_dirent * sd) · line 276

Quoted 6 lines
fs/sysfs/dir.c +6 -0
+    if (unlikely(!(sd->s_flags & SYSFS_FLAG_REMOVED))) {
+            printk(KERN_ERR "sysfs: free using entry: %s/%s\n",
+                    parent_sd ? parent_sd->s_name : "",
+                    sd->s_name);
+            BUG();
+    }

Please use WARN instead of BUG. For an in-ram filesystem like sysfs, there's no real reason to lock-up the machine in this way making it harder to debug.

If WARN is used, the freed memory will be allocated to other kernel components, then sysfs may change the memory and cause destruction, so maybe it is better to use BUG to stop kernel.

Quoted 5 lines
Quoted 1 line

No, it's never ok to call BUG(), sorry, please fix this.

Sorry, could you explain it in a bit detail? IMO, it is really a bug when code runs here, and there are much similar BUG_ON() uses in current sysfs code too.

Then make it a WARN() call, like David said, to give us a chance to get the report from a user so we can fix it. If the machine crashes after that, fine, but hopefully we will get a oops report out of it.

greg k-h

author
#6 lore raw
Quoted 4 lines · Greg Kroah-Hartman

Then make it a WARN() call, like David said, to give us a chance to get the report from a user so we can fix it. If the machine crashes after that, fine, but hopefully we will get a oops report out of it.

OK, got it, and console might not be available at that time.

Thanks,
--
Ming Lei