Skip to contentSkip to conversation

SELinux: memory leak in security_context_to_sid_core

PATCH In review Eric Paris 3 participants 10 messages Sep 2, 2008 – Sep 3, 2008 SELinux security module

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/1 SELinux: memory leak in security_context_to_sid_core

Memory leak in security_context_to_sid_core() as a result of the deferred context patches. Code audit found another possible leak in string_to_context_struct() so I fixed that error path as well.

This is a regression since 2.6.26.

---

 security/selinux/ss/services.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +6 -5
index b52f923..e1090c1 100644
@@ -805,18 +805,20 @@ static int string_to_context_struct(struct policydb *pol,
 
 	if ((p - scontext) < scontext_len) {
 		rc = -EINVAL;
-		goto out;
+		goto out_destroy;
 	}
 
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
-		goto out;
+		goto out_destroy;
 	}
 	rc = 0;
 out:
 	return rc;
+out_destroy:
+	context_destroy(ctx);
+	goto out;
 }
 
 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
@@ -868,10 +870,9 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
 out:
 	read_unlock(&policy_rwlock);
+	context_destroy(&context);
 	kfree(scontext2);
 	kfree(str);
 	return rc;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 27 lines · Eric Paris

Memory leak in security_context_to_sid_core() as a result of the deferred context patches. Code audit found another possible leak in string_to_context_struct() so I fixed that error path as well.

This is a regression since 2.6.26.

---

 security/selinux/ss/services.c |   11 ++++++-----
 1 files changed, 6 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +1 -1
index b52f923..e1090c1 100644
@@ -805,18 +805,20 @@ static int string_to_context_struct(struct policydb *pol,
 
 	if ((p - scontext) < scontext_len) {
 		rc = -EINVAL;
-		goto out;
+		goto out_destroy;
 	}
 
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {

security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 820

Quoted 10 lines
security/selinux/ss/services.c +3 -2
 		rc = -EINVAL;
-		context_destroy(ctx);
-		goto out;
+		goto out_destroy;
 	}
 	rc = 0;
 out:
 	return rc;
+out_destroy:
+	context_destroy(ctx);

Doesn't this introduce a double free on that code path (caller will also call context_destroy on the same context, which it passed in).

Quoted 17 lines
security/selinux/ss/services.c +2 -2
+	goto out;
 }
 
 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
@@ -868,10 +870,9 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
 out:
 	read_unlock(&policy_rwlock);
+	context_destroy(&context);
 	kfree(scontext2);
 	kfree(str);
 	return rc;
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#2 lore raw
Quoted 4 lines · Stephen Smalley

security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 820

Quoted 1 line
security/selinux/ss/services.c +1 -0
+	context_destroy(ctx);

Doesn't this introduce a double free on that code path (caller will also call context_destroy on the same context, which it passed in).

Indeed, context_destroy will be called twice and it's a bug, although kfree() will not be called twice on the same pointer (e.g. c->str is set to NULL, as is e->node).

It needs to be fixed, but it's not a double free, unless I'm missing something.

- James

Signature · 7 lines
James Morris
<[email protected]>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#3 lore raw
Quoted 7 lines · James Morris
Quoted 4 lines · Stephen Smalley

security/selinux/ss/services.c static int string_to_context_struct(struct policydb *pol, · line 820

Quoted 1 line
security/selinux/ss/services.c +1 -0
+	context_destroy(ctx);

Doesn't this introduce a double free on that code path (caller will also call context_destroy on the same context, which it passed in).

Quoted 6 lines

Indeed, context_destroy will be called twice and it's a bug, although kfree() will not be called twice on the same pointer (e.g. c->str is set to NULL, as is e->node).

It needs to be fixed, but it's not a double free, unless I'm missing something.

Ok, makes sense.

We need to decide who is responsible for freeing ctx under what conditions. It seems reasonable that if the function encounters an error, it would free any transient storage and the caller would not try to free the context; whereas, if the function succeeds and returns the result via ctx, then the caller will free when done using it.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#4 lore raw

PATCH 1/1 SELinux: memory leak in security_context_to_sid_core

Fix a bug and a philosophical decision about who handles errors.

security_context_to_sid_core() was leaking a context in the common case. This was causing problems on fedora systems which recently have started making extensive use of this function.

In discussion it was decided that if string_to_context_struct() had an error it was its own responsibility to clean up any mess it created along the way. Remember that it is perfectly safe to call context_destroy() on the same context twice and so this will not introduce any double frees or cause issues with the other change.

---

Note that the last version was safe, didn't break anything, and fixed the problem. This one is just cleaner and makes the choice to not force callers to handle errors.

 security/selinux/ss/services.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
security/selinux/ss/services.c +3 -3
index b52f923..736a066 100644
@@ -811,11 +811,12 @@ static int string_to_context_struct(struct policydb *pol,
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
 		goto out;
 	}
 	rc = 0;
 out:
+	if (rc)
+		context_destroy(ctx);
 	return rc;
 }
 
@@ -868,10 +869,9 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
 out:
 	read_unlock(&policy_rwlock);
+	context_destroy(&context);
 	kfree(scontext2);
 	kfree(str);
 	return rc;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#5 lore raw
Quoted 11 lines · Eric Paris

Fix a bug and a philosophical decision about who handles errors.

security_context_to_sid_core() was leaking a context in the common case. This was causing problems on fedora systems which recently have started making extensive use of this function.

In discussion it was decided that if string_to_context_struct() had an error it was its own responsibility to clean up any mess it created along the way. Remember that it is perfectly safe to call context_destroy() on the same context twice and so this will not introduce any double frees or cause issues with the other change.

Well, there are two possibilities. Either:
1) we are fine with multiple calls to context_destroy(), in which case the original patch stands as is, or
2) we want to avoid multiple calls to context_destroy(), in which case this patch is still wrong - the latter context_destroy() call should only occur _before_ the out label (i.e. same place it was, only without the conditional).

Quoted 43 lines

---

Note that the last version was safe, didn't break anything, and fixed the problem. This one is just cleaner and makes the choice to not force callers to handle errors.

 security/selinux/ss/services.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
security/selinux/ss/services.c +3 -3
index b52f923..736a066 100644
@@ -811,11 +811,12 @@ static int string_to_context_struct(struct policydb *pol,
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
 		goto out;
 	}
 	rc = 0;
 out:
+	if (rc)
+		context_destroy(ctx);
 	return rc;
 }
 
@@ -868,10 +869,9 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
 out:
 	read_unlock(&policy_rwlock);
+	context_destroy(&context);
 	kfree(scontext2);
 	kfree(str);
 	return rc;
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#6 lore raw
Quoted 13 lines · Stephen Smalley
Quoted 11 lines · Eric Paris

Fix a bug and a philosophical decision about who handles errors.

security_context_to_sid_core() was leaking a context in the common case. This was causing problems on fedora systems which recently have started making extensive use of this function.

In discussion it was decided that if string_to_context_struct() had an error it was its own responsibility to clean up any mess it created along the way. Remember that it is perfectly safe to call context_destroy() on the same context twice and so this will not introduce any double frees or cause issues with the other change.

Quoted 3 lines

Well, there are two possibilities. Either:
1) we are fine with multiple calls to context_destroy(), in which case the original patch stands as is, or

I'm fine with it standing, but I rewrote it really because I didn't want yet another needless goto that I added.

Quoted 4 lines

2) we want to avoid multiple calls to context_destroy(), in which case this patch is still wrong - the latter context_destroy() call should only occur _before_ the out label (i.e. same place it was, only without the conditional).

Lets go this route just to have some consistency. I put it where I did so it was done without the policy_rwlock. Since we are now seeing hundreds to thousands of calls to this code path I think its worth it to be a slight bit more picky with the locking. So how about a version 3 like this? If you ack I'll send it cleanly for jmorris....

---

 security/selinux/ss/services.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +7 -5
index b52f923..a0a73e7 100644
@@ -811,11 +811,12 @@ static int string_to_context_struct(struct policydb *pol,
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
 		goto out;
 	}
 	rc = 0;
 out:
+	if (rc)
+		context_destroy(ctx);
 	return rc;
 }
 
@@ -865,13 +866,14 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 		context.str = str;
 		context.len = scontext_len;
 		str = NULL;
-	} else if (rc)
+	} else if (rc) {
+		read_unlock(&policy_rwlock);
 		goto out;
+	}
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
-out:
 	read_unlock(&policy_rwlock);
+	context_destroy(&context);
+out:
 	kfree(scontext2);
 	kfree(str);
 	return rc;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#7 lore raw
Quoted 26 lines · Eric Paris
Quoted 13 lines · Stephen Smalley
Quoted 11 lines · Eric Paris

Fix a bug and a philosophical decision about who handles errors.

security_context_to_sid_core() was leaking a context in the common case. This was causing problems on fedora systems which recently have started making extensive use of this function.

In discussion it was decided that if string_to_context_struct() had an error it was its own responsibility to clean up any mess it created along the way. Remember that it is perfectly safe to call context_destroy() on the same context twice and so this will not introduce any double frees or cause issues with the other change.

Quoted 3 lines

Well, there are two possibilities. Either:
1) we are fine with multiple calls to context_destroy(), in which case the original patch stands as is, or

I'm fine with it standing, but I rewrote it really because I didn't want yet another needless goto that I added.

Quoted 4 lines

2) we want to avoid multiple calls to context_destroy(), in which case this patch is still wrong - the latter context_destroy() call should only occur _before_ the out label (i.e. same place it was, only without the conditional).

Quoted 5 lines

Lets go this route just to have some consistency. I put it where I did so it was done without the policy_rwlock. Since we are now seeing hundreds to thousands of calls to this code path I think its worth it to be a slight bit more picky with the locking. So how about a version 3 like this? If you ack I'll send it cleanly for jmorris....

I'm not sure it is justified - we are only talking about a readlock there, and centralizing the lock/unlock sequence seems generally good.

Quoted 50 lines

---

 security/selinux/ss/services.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)
security/selinux/ss/services.c +7 -6
index b52f923..a0a73e7 100644
@@ -811,11 +811,12 @@ static int string_to_context_struct(struct policydb *pol,
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
 		goto out;
 	}
 	rc = 0;
 out:
+	if (rc)
+		context_destroy(ctx);
 	return rc;
 }
 
@@ -865,13 +866,14 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 		context.str = str;
 		context.len = scontext_len;
 		str = NULL;
-	} else if (rc)
+	} else if (rc) {
+		read_unlock(&policy_rwlock);
 		goto out;
+	}
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
-out:
 	read_unlock(&policy_rwlock);
+	context_destroy(&context);
+out:
 	kfree(scontext2);
 	kfree(str);
 	return rc;
 
 
 
--

This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#8 lore raw

PATCH 1/1 SELinux: memory leak in security_context_to_sid_core

In reply to Stephen Smalley

Fix a bug and a philosophical decision about who handles errors.

security_context_to_sid_core() was leaking a context in the common case. This was causing problems on fedora systems which recently have started making extensive use of this function.

In discussion it was decided that if string_to_context_struct() had an error it was its own responsibility to clean up any mess it created along the way.

---

 security/selinux/ss/services.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
security/selinux/ss/services.c +3 -3
index b52f923..d11a815 100644
@@ -811,11 +811,12 @@ static int string_to_context_struct(struct policydb *pol,
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
 		goto out;
 	}
 	rc = 0;
 out:
+	if (rc)
+		context_destroy(ctx);
 	return rc;
 }
 
@@ -868,8 +869,7 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
+	context_destroy(&context);
 out:
 	read_unlock(&policy_rwlock);
 	kfree(scontext2);

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Acked-by on v1 1/1
#9 lore raw
Quoted 11 lines · Eric Paris

Fix a bug and a philosophical decision about who handles errors.

security_context_to_sid_core() was leaking a context in the common case. This was causing problems on fedora systems which recently have started making extensive use of this function.

In discussion it was decided that if string_to_context_struct() had an error it was its own responsibility to clean up any mess it created along the way.

Quoted 35 lines

---

 security/selinux/ss/services.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
security/selinux/ss/services.c +3 -3
index b52f923..d11a815 100644
@@ -811,11 +811,12 @@ static int string_to_context_struct(struct policydb *pol,
 	/* Check the validity of the new context. */
 	if (!policydb_context_isvalid(pol, ctx)) {
 		rc = -EINVAL;
-		context_destroy(ctx);
 		goto out;
 	}
 	rc = 0;
 out:
+	if (rc)
+		context_destroy(ctx);
 	return rc;
 }
 
@@ -868,8 +869,7 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
-	if (rc)
-		context_destroy(&context);
+	context_destroy(&context);
 out:
 	read_unlock(&policy_rwlock);
 	kfree(scontext2);
Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.