All of lore.kernel.org
 help / color / mirror / Atom feed
From: KaiGai Kohei <kaigai@ak.jp.nec.com>
To: Stephen Smalley <sds@tycho.nsa.gov>
Cc: jmorris@namei.org, paul.moore@hp.com, jbrindle@tresys.com,
	selinux@tycho.nsa.gov
Subject: [PATCH 3/3] Thread/Child-Domain Assignment
Date: Fri, 25 Jul 2008 22:04:03 +0900	[thread overview]
Message-ID: <4889CF43.2070407@ak.jp.nec.com> (raw)
In-Reply-To: <4889CC5F.3030500@ak.jp.nec.com>

[3/3] thread-context-libsepol.1.patch
  This patch add support of new policy version of POLICYDB_VERSION_HIERARCHY.
  It keeps child-parent relationship between two types, and used to make
  a decision whether required dynamic type transition within multithreaded
  process can be allowed, or not, in the kernel space.

Signed-off-by: KaiGai Kohei <kaigai@ak.jp.nec.com>
----
 include/sepol/policydb/policydb.h |    7 +++-
 src/expand.c                      |   33 +++++++++++++++++++++
 src/hierarchy.c                   |   50 ++++++++++++++++++++++++++------
 src/link.c                        |   33 +++++++++++++++++++++
 src/policydb.c                    |   59 +++++++++++++++++++++++++++++---------
 src/write.c                       |    9 +++++
 6 files changed, 166 insertions(+), 25 deletions(-)

Index: libsepol/include/sepol/policydb/policydb.h
===================================================================
--- libsepol/include/sepol/policydb/policydb.h	(revision 2928)
+++ libsepol/include/sepol/policydb/policydb.h	(working copy)
@@ -145,6 +145,7 @@
 	ebitmap_t types;	/* types with this attribute */
 #define TYPE_FLAGS_PERMISSIVE	0x01
 	uint32_t flags;
+	uint32_t parent;	/* parent type in hierarchical representation */
 } type_datum_t;

 /* User attributes */
@@ -595,10 +596,11 @@
 #define POLICYDB_VERSION_RANGETRANS	21
 #define POLICYDB_VERSION_POLCAP		22
 #define POLICYDB_VERSION_PERMISSIVE	23
+#define POLICYDB_VERSION_HIERARCHY	24

 /* Range of policy versions we understand*/
 #define POLICYDB_VERSION_MIN	POLICYDB_VERSION_BASE
-#define POLICYDB_VERSION_MAX	POLICYDB_VERSION_PERMISSIVE
+#define POLICYDB_VERSION_MAX	POLICYDB_VERSION_HIERARCHY

 /* Module versions and specific changes*/
 #define MOD_POLICYDB_VERSION_BASE	   4
@@ -608,9 +610,10 @@
 #define MOD_POLICYDB_VERSION_MLS_USERS	   6
 #define MOD_POLICYDB_VERSION_POLCAP	   7
 #define MOD_POLICYDB_VERSION_PERMISSIVE	   8
+#define MOD_POLICYDB_VERSION_HIERARCHY	   9

 #define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE
-#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_PERMISSIVE
+#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_HIERARCHY

 #define POLICYDB_CONFIG_MLS    1

Index: libsepol/src/policydb.c
===================================================================
--- libsepol/src/policydb.c	(revision 2928)
+++ libsepol/src/policydb.c	(working copy)
@@ -111,6 +111,12 @@
 	 .ocon_num = OCON_NODE6 + 1,
 	 },
 	{
+	 .type = POLICY_KERN,
+	 .version = POLICYDB_VERSION_HIERARCHY,
+	 .sym_num = SYM_NUM,
+	 .ocon_num = OCON_NODE6 + 1,
+	 },
+	{
 	 .type = POLICY_BASE,
 	 .version = MOD_POLICYDB_VERSION_BASE,
 	 .sym_num = SYM_NUM,
@@ -141,6 +147,12 @@
 	 .ocon_num = OCON_NODE6 + 1,
 	 },
 	{
+	 .type = POLICY_BASE,
+	 .version = MOD_POLICYDB_VERSION_HIERARCHY,
+	 .sym_num = SYM_NUM,
+	 .ocon_num = OCON_NODE6 + 1,
+	},
+	{
 	 .type = POLICY_MOD,
 	 .version = MOD_POLICYDB_VERSION_BASE,
 	 .sym_num = SYM_NUM,
@@ -170,6 +182,12 @@
 	 .sym_num = SYM_NUM,
 	 .ocon_num = 0
 	 },
+	{
+	 .type = POLICY_MOD,
+	 .version = MOD_POLICYDB_VERSION_HIERARCHY,
+	 .sym_num = SYM_NUM,
+	 .ocon_num = 0
+	},
 };

 #if 0
@@ -1918,30 +1936,45 @@
 	type_datum_t *typdatum;
 	uint32_t buf[5];
 	size_t len;
-	int rc, to_read;
+	int rc, items, to_read;

 	typdatum = calloc(1, sizeof(type_datum_t));
 	if (!typdatum)
 		return -1;

-	if (p->policy_type == POLICY_KERN)
-		to_read = 3;
-	else if (p->policyvers >= MOD_POLICYDB_VERSION_PERMISSIVE)
-		to_read = 5;
-	else
-		to_read = 4;
+	if (p->policy_type == POLICY_KERN) {
+		if (p->policyvers >= POLICYDB_VERSION_HIERARCHY)
+			to_read = 4;
+		else
+			to_read = 3;
+	} else {
+		if (p->policyvers >= MOD_POLICYDB_VERSION_HIERARCHY)
+			to_read = 6;
+		else if (p->policyvers >= MOD_POLICYDB_VERSION_PERMISSIVE)
+			to_read = 5;
+		else
+			to_read = 4;
+	}

 	rc = next_entry(buf, fp, sizeof(uint32_t) * to_read);
 	if (rc < 0)
 		goto bad;

-	len = le32_to_cpu(buf[0]);
-	typdatum->s.value = le32_to_cpu(buf[1]);
-	typdatum->primary = le32_to_cpu(buf[2]);
-	if (p->policy_type != POLICY_KERN) {
-		typdatum->flavor = le32_to_cpu(buf[3]);
+	items = 0;
+	len = le32_to_cpu(buf[items++]);
+	typdatum->s.value = le32_to_cpu(buf[items++]);
+	typdatum->primary = le32_to_cpu(buf[items++]);
+
+	if (p->policy_type == POLICY_KERN) {
+		if (p->policyvers >= POLICYDB_VERSION_HIERARCHY)
+			typdatum->parent = le32_to_cpu(buf[items++]);
+	} else {
+		if (p->policyvers >= MOD_POLICYDB_VERSION_HIERARCHY)
+			typdatum->parent = le32_to_cpu(buf[items++]);
+
+		typdatum->flavor = le32_to_cpu(buf[items++]);
 		if (p->policyvers >= MOD_POLICYDB_VERSION_PERMISSIVE)
-			typdatum->flags = le32_to_cpu(buf[4]);
+			typdatum->flags = le32_to_cpu(buf[items++]);
 		if (ebitmap_read(&typdatum->types, fp))
 			goto bad;
 	}
Index: libsepol/src/hierarchy.c
===================================================================
--- libsepol/src/hierarchy.c	(revision 2928)
+++ libsepol/src/hierarchy.c	(working copy)
@@ -47,7 +47,7 @@
  *
  * Caller must free parent after use.
  */
-static int find_parent(char *type, char **parent)
+static int find_parent_namebased(char *type, char **parent)
 {
 	char *tmp;
 	int len;
@@ -73,6 +73,34 @@
 	return 0;
 }

+static int find_parent(policydb_t *p, char *child, char **parent)
+{
+	type_datum_t *child_type;
+
+	/* legacy name based hierarchy relationship */
+	if ((p->policy_type == POLICY_KERN
+	     && p->policyvers < POLICYDB_VERSION_HIERARCHY) ||
+	    (p->policy_type != POLICY_KERN
+	     && p->policyvers < MOD_POLICYDB_VERSION_HIERARCHY))
+		return find_parent_namebased(child, parent);
+
+	child_type = hashtab_search(p->p_types.table, child);
+	if (!child_type)
+		return -1;
+
+	/* no hierarchy relationship */
+	if (!child_type->parent) {
+		*parent = NULL;
+		return 0;
+	}
+
+	*parent = strdup(p->p_type_val_to_name[child_type->parent - 1]);
+	if (!(*parent))
+		return -1;
+
+	return 0;
+}
+
 /* This function verifies that the type passed in either has a parent or is in the
  * root of the namespace, 0 on success, 1 on orphan and -1 on error
  */
@@ -93,7 +121,7 @@
 		return 0;
 	}

-	if (find_parent(key, &parent))
+	if (find_parent(a->p, key, &parent))
 		return -1;

 	if (!parent) {
@@ -126,7 +154,7 @@
 static int check_avtab_hierarchy_callback(avtab_key_t * k, avtab_datum_t * d,
 					  void *args)
 {
-	char *parent;
+	char *type_name, *parent;
 	avtab_key_t key;
 	avtab_datum_t *avdatump;
 	hierarchy_args_t *a;
@@ -139,7 +167,8 @@
 	}

 	a = (hierarchy_args_t *) args;
-	if (find_parent(a->p->p_type_val_to_name[k->source_type - 1], &parent))
+	type_name = a->p->p_type_val_to_name[k->source_type - 1];
+	if (find_parent(a->p, type_name, &parent))
 		return -1;

 	/* search for parent first */
@@ -178,7 +207,8 @@
 	}

 	/* next we try type 1 and type 2's parent */
-	if (find_parent(a->p->p_type_val_to_name[k->target_type - 1], &parent))
+	type_name = a->p->p_type_val_to_name[k->target_type - 1];
+	if (find_parent(a->p, type_name, &parent))
 		return -1;

 	if (parent) {
@@ -317,14 +347,15 @@
 					 __attribute__ ((unused)),
 					 hashtab_datum_t d, void *args)
 {
-	char *parent;
+	char *role_name, *parent;
 	hierarchy_args_t *a;
 	role_datum_t *r, *rp;

 	a = (hierarchy_args_t *) args;
 	r = (role_datum_t *) d;

-	if (find_parent(a->p->p_role_val_to_name[r->s.value - 1], &parent))
+	role_name = a->p->p_role_val_to_name[r->s.value - 1];
+	if (find_parent_namebased(role_name, &parent))
 		return -1;

 	if (!parent) {
@@ -362,14 +393,15 @@
 					 __attribute__ ((unused)),
 					 hashtab_datum_t d, void *args)
 {
-	char *parent;
+	char *user_name, *parent;
 	hierarchy_args_t *a;
 	user_datum_t *u, *up;

 	a = (hierarchy_args_t *) args;
 	u = (user_datum_t *) d;

-	if (find_parent(a->p->p_user_val_to_name[u->s.value - 1], &parent))
+	user_name = a->p->p_user_val_to_name[u->s.value - 1];
+	if (find_parent_namebased(user_name, &parent))
 		return -1;

 	if (!parent) {
Index: libsepol/src/expand.c
===================================================================
--- libsepol/src/expand.c	(revision 2928)
+++ libsepol/src/expand.c	(working copy)
@@ -538,6 +538,35 @@
 	return 0;
 }

+static int hierarchy_type_callback(hashtab_key_t key, hashtab_datum_t datum, void *data)
+{
+	expand_state_t *state = (expand_state_t *) data;
+	type_datum_t *type = (type_datum_t *) datum;
+	type_datum_t *target;
+	uint32_t parent_val;
+
+	if (!type->parent)
+		return 0;
+
+	if (!is_id_enabled((char *)key, state->base, SYM_TYPES))
+		return 0;
+
+	parent_val = state->typemap[type->parent - 1];
+
+	target = hashtab_search(state->out->p_types.table, (char *)key);
+	if (!target) {
+		ERR(state->handle, "Type lookup failed for %s", (char *)key);
+		return -1;
+	}
+	if (target->parent > 0 && target->parent != parent_val) {
+		ERR(state->handle, "Inconsistent hierarchy for %s", (char *)key);
+		return -1;
+	}
+	target->parent = parent_val;
+
+	return 0;
+}
+
 static int role_remap_dominates(hashtab_key_t key __attribute__ ((unused)), hashtab_datum_t datum, void *data)
 {
 	ebitmap_t mapped_roles;
@@ -2393,6 +2422,10 @@
 		goto cleanup;
 	}

+	/* copy hierarchy */
+	if (hashtab_map(state.base->p_types.table, hierarchy_type_callback, &state))
+		goto cleanup;
+
 	/* copy aliases */
 	if (hashtab_map(state.base->p_types.table, alias_copy_callback, &state))
 		goto cleanup;
Index: libsepol/src/write.c
===================================================================
--- libsepol/src/write.c	(revision 2928)
+++ libsepol/src/write.c	(working copy)
@@ -957,7 +957,14 @@
 	buf[items++] = cpu_to_le32(len);
 	buf[items++] = cpu_to_le32(typdatum->s.value);
 	buf[items++] = cpu_to_le32(typdatum->primary);
-	if (p->policy_type != POLICY_KERN) {
+
+	if (p->policy_type == POLICY_KERN) {
+		if (p->policyvers >= POLICYDB_VERSION_HIERARCHY)
+			buf[items++] = cpu_to_le32(typdatum->parent);
+	} else {
+		if (p->policyvers >= MOD_POLICYDB_VERSION_HIERARCHY)
+			buf[items++] = cpu_to_le32(typdatum->parent);
+
 		buf[items++] = cpu_to_le32(typdatum->flavor);
 		if (p->policyvers >= MOD_POLICYDB_VERSION_PERMISSIVE)
 			buf[items++] = cpu_to_le32(typdatum->flags);
Index: libsepol/src/link.c
===================================================================
--- libsepol/src/link.c	(revision 2928)
+++ libsepol/src/link.c	(working copy)
@@ -770,6 +770,35 @@
 	return -1;
 }

+static int hierarchy_type_callback(hashtab_key_t key, hashtab_datum_t datum, void *data)
+{
+	link_state_t *state = (link_state_t *) data;
+	type_datum_t *type = (type_datum_t *) datum;
+	type_datum_t *target;
+	uint32_t parent_val;
+
+	if (!type->parent)
+		return 0;
+
+	parent_val = state->cur->map[SYM_TYPES][type->parent - 1];
+
+	target = hashtab_search(state->base->p_types.table, key);
+	if (!target) {
+		ERR(state->handle,
+		    "Type lookup failed for %s", (char *)key);
+		return -1;
+	}
+	if (target->parent > 0 && target->parent != parent_val) {
+		ERR(state->handle,
+		    "Inconsistent domain hierarchy for %s", (char *)key);
+		return -1;
+	}
+	target->parent = parent_val;
+
+	return 0;
+}
+
+
 /*********** callbacks that fix bitmaps ***********/

 static int type_set_convert(type_set_t * types, type_set_t * dst,
@@ -1362,6 +1391,10 @@
 		}
 	}

+	if (hashtab_map(src_symtab[SYM_TYPES].table,
+			hierarchy_type_callback, state))
+		return -1;
+
 	if (hashtab_map
 	    (src_symtab[SYM_TYPES].table, alias_copy_callback, state)) {
 		return -1;

-- 
OSS Platform Development Division, NEC
KaiGai Kohei <kaigai@ak.jp.nec.com>

--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to majordomo@tycho.nsa.gov with
the words "unsubscribe selinux" without quotes as the message.

  parent reply	other threads:[~2008-07-25 13:04 UTC|newest]

Thread overview: 97+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-07-15 10:06 [RFC] An idea of thread/child-domain assignment KaiGai Kohei
2008-07-15 13:38 ` Stephen Smalley
2008-07-16  2:17   ` KaiGai Kohei
2008-07-16  6:08     ` KaiGai Kohei
2008-07-16 12:00       ` Stephen Smalley
2008-07-16 12:18     ` Stephen Smalley
2008-07-18  6:21       ` KaiGai Kohei
2008-07-23  3:58         ` KaiGai Kohei
2008-07-25 12:51           ` [PATCH 0/3] Thread/Child-Domain Assignment KaiGai Kohei
2008-07-25 13:03             ` [PATCH 1/3] " KaiGai Kohei
2008-07-25 13:44               ` Stephen Smalley
2008-07-25 17:06                 ` Joshua Brindle
2008-07-26  8:24                   ` KaiGai Kohei
2008-07-25 17:07                 ` Joshua Brindle
2008-07-26  7:55                 ` KaiGai Kohei
2008-07-26 17:28                   ` Stephen Smalley
2008-07-26 18:14                     ` Joshua Brindle
2008-07-28  3:06                       ` KaiGai Kohei
2008-07-28 17:31                       ` Stephen Smalley
2008-07-29  6:51                         ` KaiGai Kohei
2008-07-29 12:06                           ` Stephen Smalley
2008-07-30 14:10                             ` Joshua Brindle
2008-07-30 14:57                               ` Stephen Smalley
2008-08-01  6:26                             ` KaiGai Kohei
2008-07-25 13:03             ` [PATCH 2/3] " KaiGai Kohei
2008-07-29  7:15               ` KaiGai Kohei
2008-07-29 12:25                 ` Scott Schmit
2008-07-29 13:28                   ` Stephen Smalley
2008-07-25 13:04             ` KaiGai Kohei [this message]
2008-07-25 13:04             ` [PATCH 4/3] " KaiGai Kohei
2008-08-05  5:47             ` [PATCH 0/3] Thread/Child-Domain Assignment (rev.2) KaiGai Kohei
2008-08-05  5:55               ` [PATCH 1/3] " KaiGai Kohei
2008-08-05 12:53                 ` Stephen Smalley
2008-08-06 10:05                   ` KaiGai Kohei
2008-08-06 10:13                   ` [PATCH 1/3] Thread/Child-Domain Assignment (rev.3) KaiGai Kohei
2008-08-14  7:38                     ` [PATCH 1/3] Thread/Child-Domain Assignment (rev.4) KaiGai Kohei
2008-08-15 18:13                       ` Stephen Smalley
2008-08-20  9:41                         ` KaiGai Kohei
2008-08-25 12:32                         ` [PATCH 1/3] Thread/Child-Domain Assignment (rev.6) KaiGai Kohei
2008-08-25 12:57                           ` Stephen Smalley
2008-08-25 13:45                             ` KaiGai Kohei
2008-08-26  7:11                             ` KaiGai Kohei
2008-08-26  9:01                           ` James Morris
2008-08-26 10:29                           ` James Morris
2008-08-26 10:47                             ` James Morris
2008-08-27  1:15                               ` KaiGai Kohei
2008-08-27  8:04                               ` [LTP][PATCH 1/2] Replacement of deprecated interfaces KaiGai Kohei
2008-08-27 12:14                                 ` Stephen Smalley
2008-08-28  6:26                                   ` KaiGai Kohei
2008-08-28 12:10                                     ` Subrata Modak
2008-08-28 12:52                                       ` KaiGai Kohei
2008-08-28 13:34                                         ` Subrata Modak
2008-10-23  9:48                                     ` Subrata Modak
2008-08-27  8:05                               ` [LTP][PATCH 2/2] Add a new test case for bounds types KaiGai Kohei
2008-10-22 13:00                                 ` Subrata Modak
2008-10-23  8:10                                   ` KaiGai Kohei
2008-10-23  9:30                                     ` Subrata Modak
2008-08-27  1:11                             ` [PATCH 1/3] Thread/Child-Domain Assignment (rev.6) KaiGai Kohei
2008-08-28  7:35                             ` [PATCH] SELinux: add boundary support and thread context assignment KaiGai Kohei
2008-08-28 12:43                               ` Stephen Smalley
2008-08-28 15:06                               ` James Morris
2008-08-05  5:55               ` [PATCH 2/3] Thread/Child-Domain Assignment (rev.2) KaiGai Kohei
2008-08-06 10:14                 ` [PATCH 2/3] Thread/Child-Domain Assignment (rev.3) KaiGai Kohei
2008-10-09 17:10                 ` [PATCH 2/3] Thread/Child-Domain Assignment (rev.2) Joshua Brindle
2008-10-10  1:19                   ` KaiGai Kohei
2008-10-10  1:22                     ` Joshua Brindle
2008-08-05  5:55               ` [PATCH 3/3] " KaiGai Kohei
2008-08-06 10:13                 ` [PATCH 3/3] Thread/Child-Domain Assignment (rev.3) KaiGai Kohei
2008-08-25 12:32                 ` [PATCH 3/3] Thread/Child-Domain Assignment (rev.4) KaiGai Kohei
2008-08-28 15:51                   ` Joshua Brindle
2008-08-29  1:54                     ` KaiGai Kohei
2008-08-29  3:01                       ` Joshua Brindle
2008-09-01  6:26                         ` KaiGai Kohei
2008-09-01  9:08                           ` [PATCH] libsepol : Add support for a new policy version (POLICYDB_VERSION_BOUNDARY) KaiGai Kohei
2008-09-01 14:47                           ` [PATCH 3/3] Thread/Child-Domain Assignment (rev.4) Joshua Brindle
2008-09-01 16:11                             ` KaiGai Kohei
2008-09-09  2:04                               ` [PATCH 3/3] Thread/Child-Domain Assignment (rev.6) KaiGai Kohei
2008-09-12 18:17                                 ` Joshua Brindle
2008-09-12 23:20                                   ` KaiGai Kohei
2008-09-15 13:44                                     ` Joshua Brindle
2008-09-16  1:50                                       ` KaiGai Kohei
2008-09-30 14:00                                     ` Joshua Brindle
2008-10-01  7:53                                       ` KaiGai Kohei
2008-10-01 19:56                                         ` Joshua Brindle
2008-10-04 23:30                                         ` Joshua Brindle
2008-10-06  9:19                                           ` KaiGai Kohei
2008-10-06 19:13                                             ` Joshua Brindle
2008-10-07  6:39                                               ` KaiGai Kohei
2008-10-09 15:30                                                 ` Joshua Brindle
2008-10-09 17:00                                                   ` Joshua Brindle
2008-10-10  0:57                                                   ` KaiGai Kohei
2008-10-09 17:11                                                 ` Joshua Brindle
2008-10-06 12:30                                           ` Stephen Smalley
2008-10-06 19:13                                             ` Joshua Brindle
2008-08-11 17:58               ` [PATCH 0/3] Thread/Child-Domain Assignment (rev.2) Joshua Brindle
2008-08-13  5:53                 ` KaiGai Kohei
2008-08-14  8:55             ` A toy of SQL injection (Re: [PATCH 0/3] Thread/Child-Domain Assignment) KaiGai Kohei

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=4889CF43.2070407@ak.jp.nec.com \
    --to=kaigai@ak.jp.nec.com \
    --cc=jbrindle@tresys.com \
    --cc=jmorris@namei.org \
    --cc=paul.moore@hp.com \
    --cc=sds@tycho.nsa.gov \
    --cc=selinux@tycho.nsa.gov \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.