mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: James Morris <jmorris@namei.org>
To: linux-security-module@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 02/20] SELinux: fix sleeping allocation in security_context_to_sid
Date: Tue,  8 Jul 2008 01:42:04 +0900	[thread overview]
Message-ID: <1215448942-17581-3-git-send-email-jmorris@namei.org> (raw)
In-Reply-To: <1215448942-17581-1-git-send-email-jmorris@namei.org>

From: Stephen Smalley <sds@tycho.nsa.gov>

Fix a sleeping function called from invalid context bug by moving allocation
to the callers prior to taking the policy rdlock.

Signed-off-by:  Stephen Smalley <sds@tycho.nsa.gov>
Signed-off-by: James Morris <jmorris@namei.org>
---
 security/selinux/ss/services.c |   70 +++++++++++++++++++++++-----------------
 1 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c
index b86ac9d..2d5e5a3 100644
--- a/security/selinux/ss/services.c
+++ b/security/selinux/ss/services.c
@@ -730,15 +730,16 @@ int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
 	return security_sid_to_context_core(sid, scontext, scontext_len, 1);
 }
 
+/*
+ * Caveat:  Mutates scontext.
+ */
 static int string_to_context_struct(struct policydb *pol,
 				    struct sidtab *sidtabp,
-				    const char *scontext,
+				    char *scontext,
 				    u32 scontext_len,
 				    struct context *ctx,
-				    u32 def_sid,
-				    gfp_t gfp_flags)
+				    u32 def_sid)
 {
-	char *scontext2 = NULL;
 	struct role_datum *role;
 	struct type_datum *typdatum;
 	struct user_datum *usrdatum;
@@ -747,19 +748,10 @@ static int string_to_context_struct(struct policydb *pol,
 
 	context_init(ctx);
 
-	/* Copy the string so that we can modify the copy as we parse it. */
-	scontext2 = kmalloc(scontext_len+1, gfp_flags);
-	if (!scontext2) {
-		rc = -ENOMEM;
-		goto out;
-	}
-	memcpy(scontext2, scontext, scontext_len);
-	scontext2[scontext_len] = 0;
-
 	/* Parse the security context. */
 
 	rc = -EINVAL;
-	scontextp = (char *) scontext2;
+	scontextp = (char *) scontext;
 
 	/* Extract the user. */
 	p = scontextp;
@@ -809,7 +801,7 @@ static int string_to_context_struct(struct policydb *pol,
 	if (rc)
 		goto out;
 
-	if ((p - scontext2) < scontext_len) {
+	if ((p - scontext) < scontext_len) {
 		rc = -EINVAL;
 		goto out;
 	}
@@ -822,7 +814,6 @@ static int string_to_context_struct(struct policydb *pol,
 	}
 	rc = 0;
 out:
-	kfree(scontext2);
 	return rc;
 }
 
@@ -830,6 +821,7 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 					u32 *sid, u32 def_sid, gfp_t gfp_flags,
 					int force)
 {
+	char *scontext2, *str = NULL;
 	struct context context;
 	int rc = 0;
 
@@ -839,27 +831,38 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 		for (i = 1; i < SECINITSID_NUM; i++) {
 			if (!strcmp(initial_sid_to_string[i], scontext)) {
 				*sid = i;
-				goto out;
+				return 0;
 			}
 		}
 		*sid = SECINITSID_KERNEL;
-		goto out;
+		return 0;
 	}
 	*sid = SECSID_NULL;
 
+	/* Copy the string so that we can modify the copy as we parse it. */
+	scontext2 = kmalloc(scontext_len+1, gfp_flags);
+	if (!scontext2)
+		return -ENOMEM;
+	memcpy(scontext2, scontext, scontext_len);
+	scontext2[scontext_len] = 0;
+
+	if (force) {
+		/* Save another copy for storing in uninterpreted form */
+		str = kstrdup(scontext2, gfp_flags);
+		if (!str) {
+			kfree(scontext2);
+			return -ENOMEM;
+		}
+	}
+
 	POLICY_RDLOCK;
 	rc = string_to_context_struct(&policydb, &sidtab,
-				      scontext, scontext_len,
-				      &context, def_sid, gfp_flags);
+				      scontext2, scontext_len,
+				      &context, def_sid);
 	if (rc == -EINVAL && force) {
-		context.str = kmalloc(scontext_len+1, gfp_flags);
-		if (!context.str) {
-			rc = -ENOMEM;
-			goto out;
-		}
-		memcpy(context.str, scontext, scontext_len);
-		context.str[scontext_len] = 0;
+		context.str = str;
 		context.len = scontext_len;
+		str = NULL;
 	} else if (rc)
 		goto out;
 	rc = sidtab_context_to_sid(&sidtab, &context, sid);
@@ -867,6 +870,8 @@ static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
 		context_destroy(&context);
 out:
 	POLICY_RDUNLOCK;
+	kfree(scontext2);
+	kfree(str);
 	return rc;
 }
 
@@ -1339,9 +1344,14 @@ static int convert_context(u32 key,
 
 	if (c->str) {
 		struct context ctx;
-		rc = string_to_context_struct(args->newp, NULL, c->str,
-					      c->len, &ctx, SECSID_NULL,
-					      GFP_KERNEL);
+		s = kstrdup(c->str, GFP_KERNEL);
+		if (!s) {
+			rc = -ENOMEM;
+			goto out;
+		}
+		rc = string_to_context_struct(args->newp, NULL, s,
+					      c->len, &ctx, SECSID_NULL);
+		kfree(s);
 		if (!rc) {
 			printk(KERN_INFO
 		       "SELinux:  Context %s became valid (mapped).\n",
-- 
1.5.5.1


  parent reply	other threads:[~2008-07-07 16:54 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-07 16:42 SELinux patches for 2.6.26 James Morris
2008-07-07 16:42 ` [PATCH 01/20] selinux: support deferred mapping of contexts James Morris
2008-07-07 16:42 ` James Morris [this message]
2008-07-07 16:42 ` [PATCH 03/20] SELinux: keep the code clean formating and syntax James Morris
2008-07-07 16:42 ` [PATCH 04/20] SELinux: reorder inode_security_struct to increase objs/slab on 64bit James Morris
2008-07-07 16:42 ` [PATCH 05/20] SELinux: remove inherit field from inode_security_struct James Morris
2008-07-07 16:42 ` [PATCH 06/20] Security: split proc ptrace checking into read vs. attach James Morris
2008-07-07 16:42 ` [PATCH 07/20] SELinux: enable processes with mac_admin to get the raw inode contexts James Morris
2008-07-07 16:42 ` [PATCH 08/20] selinux: simplify ioctl checking James Morris
2008-07-07 16:42 ` [PATCH 09/20] selinux: fix endianness bug in network node address handling James Morris
2008-07-07 16:42 ` [PATCH 10/20] SELinux: open code policy_rwlock James Morris
2008-07-07 16:42 ` [PATCH 11/20] SELinux: open code load_mutex James Morris
2008-07-07 16:42 ` [PATCH 12/20] SELinux: open code sidtab lock James Morris
2008-07-07 16:42 ` [PATCH 13/20] SELinux: fix off by 1 reference of class_to_string in context_struct_compute_av James Morris
2008-07-07 16:42 ` [PATCH 14/20] SELinux: drop load_mutex in security_load_policy James Morris
2008-07-07 16:42 ` [PATCH 15/20] selinux: change handling of invalid classes (Was: Re: 2.6.26-rc5-mm1 selinux whine) James Morris
2008-07-07 16:42 ` [PATCH 16/20] SELinux: more user friendly unknown handling printk James Morris
2008-07-07 16:42 ` [PATCH 17/20] SELinux: remove unused and shadowed addrlen variable James Morris
2008-07-07 16:42 ` [PATCH 18/20] SELinux: use do_each_thread as a proper do/while block James Morris
2008-07-07 16:42 ` [PATCH 19/20] security: fix return of void-valued expressions James Morris
2008-07-07 16:42 ` [PATCH 20/20] SELinux: allow fstype unknown to policy to use xattrs if present James Morris
2008-07-07 18:23 ` SELinux patches for 2.6.26 Theodore Tso
2008-07-07 22:49   ` James Morris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1215448942-17581-3-git-send-email-jmorris@namei.org \
    --to=jmorris@namei.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®