Skip to contentSkip to conversation

sysfs: Don't leak secdata when a sysfs_dirent is freed.

PATCH In review Eric W. Biederman 3 participants 4 messages Nov 4, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysfs: Don't leak secdata when a sysfs_dirent is freed.

While refreshing my sysfs patches I noticed a leak in the secdata implementation. We don't free the secdata when we free the sysfs dirent.

This is a bug in 2.6.32-rc5 that we really should close.

---

 fs/sysfs/dir.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/sysfs/dir.c +4 -0
index 5fad489..e020183 100644
@@ -21,6 +21,7 @@
 #include <linux/completion.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/security.h>
 #include "sysfs.h"
 
 DEFINE_MUTEX(sysfs_mutex);
@@ -285,6 +286,9 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
 		sysfs_put(sd->s_symlink.target_sd);
 	if (sysfs_type(sd) & SYSFS_COPY_NAME)
 		kfree(sd->s_name);
+	if (sd->s_iattr && sd->s_iattr->ia_secdata)
+		security_release_secctx(sd->s_iattr->ia_secdata,
+					sd->s_iattr->ia_secdata_len);
 	kfree(sd->s_iattr);
 	sysfs_free_ino(sd->s_ino);
 	kmem_cache_free(sysfs_dir_cachep, sd);
Signature · 1 line
1.6.5.2.143.g8cc62
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Eric W. Biederman

While refreshing my sysfs patches I noticed a leak in the secdata implementation. We don't free the secdata when we free the sysfs dirent.

This is a bug in 2.6.32-rc5 that we really should close.

I'm surprised no kmemleak or anything has found this - but I guess not many sites hand-set custom selinux contexts on sysfs?

Quoted 34 lines

---

 fs/sysfs/dir.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/sysfs/dir.c +4 -0
index 5fad489..e020183 100644
@@ -21,6 +21,7 @@
 #include <linux/completion.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/security.h>
 #include "sysfs.h"
 
 DEFINE_MUTEX(sysfs_mutex);
@@ -285,6 +286,9 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
 		sysfs_put(sd->s_symlink.target_sd);
 	if (sysfs_type(sd) & SYSFS_COPY_NAME)
 		kfree(sd->s_name);
+	if (sd->s_iattr && sd->s_iattr->ia_secdata)
+		security_release_secctx(sd->s_iattr->ia_secdata,
+					sd->s_iattr->ia_secdata_len);
 	kfree(sd->s_iattr);
 	sysfs_free_ino(sd->s_ino);
 	kmem_cache_free(sysfs_dir_cachep, sd);
Signature · 6 lines
1.6.5.2.143.g8cc62

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
Quoted 6 lines · Serge E. Hallyn
Quoted 5 lines · Eric W. Biederman

While refreshing my sysfs patches I noticed a leak in the secdata implementation. We don't free the secdata when we free the sysfs dirent.

Quoted 6 lines
Quoted 3 lines

This is a bug in 2.6.32-rc5 that we really should close.

I'm surprised no kmemleak or anything has found this - but I guess not many sites hand-set custom selinux contexts on sysfs?

I don't expect so since you couldn't customize it in 2.6.31. This is brand new code, and this corner case was just overlooked in the review.

Eric

Quoted 36 lines
Quoted 34 lines

---

 fs/sysfs/dir.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/sysfs/dir.c +4 -0
index 5fad489..e020183 100644
@@ -21,6 +21,7 @@
 #include <linux/completion.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
+#include <linux/security.h>
 #include "sysfs.h"
 
 DEFINE_MUTEX(sysfs_mutex);
@@ -285,6 +286,9 @@ void release_sysfs_dirent(struct sysfs_dirent * sd)
 		sysfs_put(sd->s_symlink.target_sd);
 	if (sysfs_type(sd) & SYSFS_COPY_NAME)
 		kfree(sd->s_name);
+	if (sd->s_iattr && sd->s_iattr->ia_secdata)
+		security_release_secctx(sd->s_iattr->ia_secdata,
+					sd->s_iattr->ia_secdata_len);
 	kfree(sd->s_iattr);
 	sysfs_free_ino(sd->s_ino);
 	kmem_cache_free(sysfs_dir_cachep, sd);
Signature · 6 lines
1.6.5.2.143.g8cc62

--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
In reply to Serge E. Hallyn

Hi Linus,

This fixes a memory leak introduced in 2.6.32-rc5, please pull.

The following changes since commit 91d3f9bacdb4950d2f79fe2ba296aa249f60d06c:
Linus Torvalds (1):
Merge branch 'for-linus' of git://git.kernel.org/.../anholt/drm-intel

are available in the git repository at:

git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/security-testing-2.6 for-linus

Eric W. Biederman (1):
sysfs: Don't leak secdata when a sysfs_dirent is freed.

 fs/sysfs/dir.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
Signature · 2 lines
James Morris
<[email protected]>