Skip to contentSkip to conversation

Context reorganization

PATCH In review Ivan Gyurdiev 3 participants 19 messages Jul 21, 2005 – Jul 26, 2005 6 messages not in the archive SELinux security module

lore
Share & tools
lore
#0 lore raw

cover Context reorganization

The following patch moves functions relating to context_struct_t into file contexts.c for better organization. It makes all of them take policydb as argument, and enforces the sepol_ prefix. It uses the new debugging system. It adds a structure for a high level representation of a context (sepol_ctxinfo_t), and a new function to convert from it (sepol_context_create), and makes use of that function to make the conversion from string (formerly sepol_context_to_sid) easier.

libsepol-1.1-context.diff

Disclaimer 1:

The following bit, as taken from the current policydb_context_isvalid fails for me currently. I don't understand the role of the cache here. Does genusers.c need to be modified to do something with the cache, when it loads the user? Do I need to correspondingly modify my own load_user function (which is based on genusers)?

+ if (!ebitmap_get_bit(&usrdatum->cache, c->role - 1))
+ /* user may not be associated with role */
+ return 0;

Disclaimer 2:

What purpose does the result_len serves in to_string/from_string functions? Shouldn't all those strings be null terminated? The former context_struct_to_string was done this way, so I didn't change it, but I don't see why this is necessary. Looking at mls_sid_to_context it would appear that it is null terminated (uses strcpy).

include/sepol/context.h +42 -4
diff -aru libsepol.work/include/sepol/context.h libsepol-1.1-context/include/sepol/context.h
@@ -16,12 +16,24 @@
  * clients of the security server.
  */
 
-#ifndef _CONTEXT_H_
-#define _CONTEXT_H_
+#ifndef _SEPOL_CONTEXT_H_
+#define _SEPOL_CONTEXT_H_
 
 #include <sepol/ebitmap.h>
 #include <sepol/mls_types.h>
 
+/* 
+ * High level representation of the context 
+ * This structure provides sufficient information
+ * to construct a security context. 
+ */
+typedef struct sepol_ctxinfo {
+	const char* user;
+	const char* role;
+	const char* type;
+	const char* mls;
+} sepol_ctxinfo_t;
+
 /*
  * A security context consists of an authenticated user
  * identity, a role, a type and a MLS range.
@@ -33,7 +45,6 @@
 	mls_range_t range;
 } context_struct_t;
 
-
 static inline void mls_context_init(context_struct_t * c)
 {
 	memset(&c->range, 0, sizeof(c->range));
@@ -111,7 +122,34 @@
 		mls_context_cmp(c1, c2));
 }
 
-#endif	/* _CONTEXT_H_ */
+struct policydb;
+
+/* Create a context structure from high level representation */
+extern int sepol_context_create(
+	struct policydb *policydb,
+	context_struct_t** cptr,
+	sepol_ctxinfo_t* data);
+
+/* Create a context structure from string representation */
+extern int sepol_context_from_string(
+	struct policydb* policydb,
+	context_struct_t** cptr,
+	const char* con_str,
+	size_t con_str_len);
+
+/* Check if the provided context is valid for this policy */
+extern int sepol_context_is_valid(
+	struct policydb *policydb, 
+	context_struct_t *context);
+
+/* Extract the context as string */
+extern int sepol_context_to_string(
+	struct policydb *policydb,
+	context_struct_t * context,
+	char ** result,
+	size_t *result_len);
+
+#endif	/* _SEPOL_CONTEXT_H_ */
 
 /* FLASK */
 
include/sepol/policydb.h +5 -1
diff -aru libsepol.work/include/sepol/policydb.h libsepol-1.1-context/include/sepol/policydb.h
@@ -452,7 +452,11 @@
 
 extern int policydb_load_isids(policydb_t *p, sidtab_t *s);
 
-extern int policydb_context_isvalid(policydb_t *p, context_struct_t *c);
+/* Deprecated */
+static inline int policydb_context_isvalid(policydb_t *p, context_struct_t *c) {
+	return sepol_context_is_valid(p,c);
+}
+
 extern void symtabs_destroy(symtab_t *symtab);
 extern int scope_destroy(hashtab_key_t key, hashtab_datum_t datum, void *p);
 typedef void (*hashtab_destroy_func_t) (hashtab_key_t k, hashtab_datum_t d, void *args);
src/context.c +281 -0

281 lines not on this page · raw message

src/policydb.c +0 -50

56 lines not on this page · raw message

src/services.c +24 -140

187 lines not on this page · raw message

#1 lore raw
Quoted 19 lines · Ivan Gyurdiev

The following patch moves functions relating to context_struct_t
into file contexts.c for better organization. It makes all of them take policydb as argument, and enforces the sepol_ prefix. It uses the new debugging system. It adds a structure for
a high level representation of a context (sepol_ctxinfo_t), and a new function to convert from it (sepol_context_create), and makes use of that function to make the conversion
from string (formerly sepol_context_to_sid) easier.

libsepol-1.1-context.diff

Disclaimer 1:

The following bit, as taken from the current policydb_context_isvalid fails for me currently. I don't understand the role of the cache here. Does genusers.c
need to be modified to do something with the cache,
when it loads the user? Do I need to correspondingly modify my own load_user function (which is based on genusers)?

src/context.c line 49

Quoted 3 lines
src/context.c +3 -0
+               if (!ebitmap_get_bit(&usrdatum->cache, c->role - 1))
+                       /* user may not be associated with role */
+                       return 0;

The purpose of the cache is so that we don't have to do expansion of the type sets every time we want to validate a context (since this happens before expansion). The cache is built from policydb_index_others so if you are't calling that it won't be built. This should work fine with an already expanded policy since type_set_expand will just copy the type bitmap to the cache.

--
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 7 lines

The purpose of the cache is so that we don't have to do expansion of the type sets every time we want to validate a context (since this happens before expansion). The cache is built from policydb_index_others so if you are't calling that it won't be built. This should work fine with an already expanded policy since type_set_expand will just copy the type bitmap to the cache.

policydb_index_others is called when I do a policydb_read to load the base policy, but at that point no users are loaded... then later when I load a user, and try to check a context, it fails on the cache check.

--
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 9 lines · Ivan Gyurdiev
Quoted 3 lines

The purpose of the cache is so that we don't have to do expansion of the type sets every time we want to validate a context (since this happens before expansion). The cache is built from policydb_index_others so if you are't calling that it won't be built. This should work fine with an already expanded policy since type_set_expand will just copy the type bitmap to the cache.

policydb_index_others is called when I do a policydb_read to
load the base policy, but at that point no users are loaded... then later when I load a user, and try to check a context, it fails on the cache check.

well, that is because the cache bitmap is empty. I'm not sure exactly what the order you do things in but you'll need to either call policydb_index_others after you've loaded all the users in or hashtab_map policydb_user_cache (not recommended), or fill in the cache bitmap yourself when loading the users in (really not recommended!)

--
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.

#4 lore raw
Quoted 4 lines

well, that is because the cache bitmap is empty. I'm not sure exactly what the order you do things in but you'll need to either call policydb_index_others after you've loaded all the users

I specifically don't want to do that - that's why I use realloc and index users one by one...

Notice how the new API deals with single users, and does not provide a way to load many users at once. It's supposed to be used as part of an iterator in libsemanage, and I don't want to loop over the entire set of users every time I modify a single one.

--
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 7 lines · Ivan Gyurdiev
Quoted 3 lines

well, that is because the cache bitmap is empty. I'm not sure exactly what the order you do things in but you'll need to either call policydb_index_others after you've loaded all the users

I specifically don't want to do that - that's why I use realloc and index users one by one...

Quoted 6 lines

Notice how the new API deals with single users, and does not provide a way to load many users at once. It's supposed to be used as part of an iterator in libsemanage, and I don't want to loop over the entire set of users every time I modify a single one.

Also, correct me if I'm wrong, but I don't think sepol_genusers_policydb ever calls index_others either - it calls reindex_users directly.

Does that mean that this cache business breaks sepol_genusers_policydb as is? Not sure how important that is, given that this function is never actually exported via the shared API...

--
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.

#6 lore raw
Quoted 17 lines · Ivan Gyurdiev
Quoted 13 lines · Ivan Gyurdiev
Quoted 3 lines

well, that is because the cache bitmap is empty. I'm not sure exactly what the order you do things in but you'll need to either call policydb_index_others after you've loaded all the users

I specifically don't want to do that - that's why I use realloc and index users one by one...

Notice how the new API deals with single users, and
does not provide a way to load many users at once. It's supposed to be used as part of an iterator in libsemanage, and I don't want to loop over
the entire set of users every time I modify a single
one.

Quoted 10 lines

Also, correct me if I'm wrong, but I don't think sepol_genusers_policydb ever calls index_others either - it calls reindex_users directly.

Does that mean that this cache business breaks sepol_genusers_policydb as is? Not sure how important that is, given that this function is never actually exported via the shared API...

it also doesn't call context_isvalid() which means it doesn't need the cache.

--
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 3 lines

it also doesn't call context_isvalid() which means it doesn't need the cache.

I think any functions exposed by libsepol (intended for external use, whether it's static linking, or shared linking), which manipulate the policydb should leave it in a valid state after they're done. This is especially true if we move to a model where all changes occur on the in-memory policydb,
instead of being written to disk (the new functions which I'm adding).

Basically, I want libsepol operations on the policydb to be transactional in themselves, and you're saying that currently sepol_genusers_policydb is broken, and so are the new user functions that I submitted.

--
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.

#8 lore raw
Quoted 4 lines · Ivan Gyurdiev
Quoted 3 lines

it also doesn't call context_isvalid() which means it doesn't need the cache.

Quoted 9 lines

I think any functions exposed by libsepol (intended for external use,
whether it's static linking, or shared linking), which manipulate the policydb should leave it in a valid state after they're done. This is especially true if we move to a model where all changes occur on the in-memory policydb, instead of being written to disk (the new functions which I'm adding).

Basically, I want libsepol operations on the policydb to be transactional in themselves, and you're saying that currently sepol_genusers_policydb is broken, and so are the new user functions that I submitted.

you have to index the policy if you want to validate contexts, it's a necessity that won't go away.

--
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.

#9 lore raw
Quoted 3 lines

you have to index the policy if you want to validate contexts, it's a necessity that won't go away

I maintain the reverse tables properly - I just don't call the functions you expect me to, and neither does sepol_genusers_policydb. It looks like it's necessary to add:

        /* This pre-expands the roles and users for context validity
checking */
        if (hashtab_map(p->p_roles.table, policydb_role_cache, p))
                return -1;

to policydb_reindex_users().

--
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.

#10 lore raw
Quoted 7 lines · Ivan Gyurdiev
Quoted 2 lines

you have to index the policy if you want to validate contexts, it's a necessity that won't go away

I maintain the reverse tables properly - I just don't call the functions you expect me to, and neither does sepol_genusers_policydb. It looks like it's necessary to add:

Quoted 6 lines
        /* This pre-expands the roles and users for context validity
checking */
        if (hashtab_map(p->p_roles.table, policydb_role_cache, p))
                return -1;

to policydb_reindex_users().

Well I meant the user cache one...
And then I'd have to expand roles manually when adding individual users.

--
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.

#11 lore raw
In reply to Ivan Gyurdiev
Quoted 4 lines · Ivan Gyurdiev
Quoted 3 lines

you have to index the policy if you want to validate contexts, it's a necessity that won't go away

Quoted 10 lines

I maintain the reverse tables properly - I just don't call the functions you expect me to, and neither does sepol_genusers_policydb. It looks like it's necessary to add:

       /* This pre-expands the roles and users for context validity
checking */
       if (hashtab_map(p->p_roles.table, policydb_role_cache, p))
               return -1;

to policydb_reindex_users().

No, this will expand the type_sets for each role, which should already be done by index_others. You'll want to use policydb_user_cache. I still don't recommend doing this but I'll have to look at the code later to see if there is something else that can be done.

--
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.

#12 lore raw
In reply to Ivan Gyurdiev

Why is the cache expansion performed in index_others... that function has to do with reconstructing reverse entry tables, and I understand your function has to do with expanding * and ~ - how is this related to maintaining the reverse entry tables?

Also, I think I'll move any code that has to do with the users into users.c, and make index_others call individual functions for roles, types, and users, that all look like reindex_users (and then those functions can be moved out of policydb.c)

Basically, I don't like how there is a big policydb.c file - this entire library is dedicated to maintaining the policydb structure, so we should split up functionality into manageable pieces.

--
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.

#13 lore raw
Quoted 6 lines · Ivan Gyurdiev

Why is the cache expansion performed in index_others... that function has to do with reconstructing reverse entry tables, and I understand your function has to do with expanding * and ~ - how is this related to maintaining the reverse entry tables?

Steve prefered I put it there rather than making policydb_read depend on another function

Quoted 6 lines

Also, I think I'll move any code that has to do with the users into users.c, and make index_others call individual functions for roles, types, and users, that all look like reindex_users (and then those functions can be moved out of policydb.c)

Quoted 7 lines

Basically, I don't like how there is a big policydb.c file - this entire library is dedicated to maintaining the policydb structure, so we should split up functionality into manageable
pieces.

--
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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw

[ libsepol 10 ] Bugfixes for 0-9

Quoted 5 lines · Stephen Smalley
Quoted 1 line · Ivan Gyurdiev

Attached fix...applies on top of everything else.

Hmm...doesn't seem to be relative - looks like a complete replacement for the original diff?

You're right, I need to do this properly, and post the relative patch on list - attached.

Depends on:

libsepol-1.1-context.diff
libsepol-1.2-ports.diff
libsepol-1.2-interfaces.diff
libsepol-1.2-fix_memory_leaks.diff
libsepol-3.1-write_services_printf.diff

src/context.c +8 -1
diff -p -aru libsepol.work/src/context.c libsepol-1.3-3.2-bugfixes/src/context.c
@@ -75,13 +75,17 @@ int sepol_context_to_string(
 	/* Compute the size of the context. */
 	scontext_len += strlen(policydb->p_user_val_to_name[context->user-1])+1;
 	scontext_len += strlen(policydb->p_role_val_to_name[context->role-1])+1;
-	scontext_len += strlen(policydb->p_type_val_to_name[context->type-1])+1;
+	scontext_len += strlen(policydb->p_type_val_to_name[context->type-1]);
 	scontext_len += mls_compute_context_len(policydb, context);
+	
+	/* We must null terminate the string */
+	scontext_len += 1;
 
 	/* Allocate space for the context; caller must free this space. */
 	scontext = malloc(scontext_len);
 	if (!scontext) 
 		goto omem;
+	scontext[scontext_len-1] = '\0';
 
 	/*
 	 * Copy the user name, role name and type name into the context.
@@ -99,6 +103,9 @@ int sepol_context_to_string(
 
 	mls_sid_to_context(policydb, context, &ptr);
 
+	/* MLS may not null terminate the string */
+	*ptr = '\0';
+
 	*result = scontext;
 	*result_len = scontext_len;
 	return STATUS_SUCCESS;
src/interfaces.c +0 -2
diff -p -aru libsepol.work/src/interfaces.c libsepol-1.3-3.2-bugfixes/src/interfaces.c
@@ -107,8 +107,6 @@ int sepol_iface_load(
 	if (rc < 0) 
 		goto err;
 
-#warning Is dup_match null terminated ??
-#warning if so, what's the purpose of dup_size
 	else if (rc != STATUS_NODATA) {
 		DEBUG(__FUNCTION__, "interface %s is already mapped to " 
 			"context %s with message context %s\n", 
src/ports.c +0 -2
diff -p -aru libsepol.work/src/ports.c libsepol-1.3-3.2-bugfixes/src/ports.c
@@ -143,8 +143,6 @@ int sepol_port_load(
 	if (rc < 0) 
 		goto err;
 
-#warning Is dup_match null terminated ??
-#warning if so, what's the purpose of dup_size
 	else if (rc != STATUS_NODATA) {
 		DEBUG(__FUNCTION__, "port entry for %s %d-%d "
 			"is already mapped to context %s\n",
src/services.c +1 -1
diff -p -aru libsepol.work/src/services.c libsepol-1.3-3.2-bugfixes/src/services.c
@@ -502,7 +502,7 @@ int sepol_sid_to_context(sepol_security_
 
 	context = sepol_sidtab_search(sidtab, sid);
 	if (!context) {
-		DEBUG("unrecognized SID %d\n", sid);
+		DEBUG(__FUNCTION__, "unrecognized SID %d\n", sid);
 		rc = -EINVAL;
 		goto out;
 	}
src/write.c +1 -0
diff -p -aru libsepol.work/src/write.c libsepol-1.3-3.2-bugfixes/src/write.c
@@ -40,6 +40,7 @@
 #include <sepol/policydb.h>
 #include <sepol/conditional.h>
 
+#include "debug.h"
 #include "private.h"
 
 static unsigned int policyvers = POLICYDB_VERSION_MAX;
#21 lore raw

Re: [ libsepol 10 ] Bugfixes for 0-9

Actually that null-terminates it twice...at least one should stay I think, because an mls_sid_to_context doesn't null-terminate.

--
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.

#22 lore raw

Re: [ libsepol 10 ] Bugfixes for 0-9

In reply to Ivan Gyurdiev
Quoted 10 lines · Ivan Gyurdiev

You're right, I need to do this properly, and post the relative patch on list - attached.

Depends on:

libsepol-1.1-context.diff
libsepol-1.2-ports.diff
libsepol-1.2-interfaces.diff
libsepol-1.2-fix_memory_leaks.diff
libsepol-3.1-write_services_printf.diff

Ok, all ten merged, plus the uninit variable patch posted by Dan.

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.
#23 lore raw

Re: [ libsepol 10 ] Bugfixes for 0-9

Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Ivan Gyurdiev

You're right, I need to do this properly, and post the relative patch on list - attached.

Depends on:

Quoted 7 lines
Quoted 5 lines

libsepol-1.1-context.diff
libsepol-1.2-ports.diff
libsepol-1.2-interfaces.diff
libsepol-1.2-fix_memory_leaks.diff
libsepol-3.1-write_services_printf.diff

Ok, all ten merged, plus the uninit variable patch posted by Dan.

Remove unnecessary null-termination as pointed out (priv mail)? If null termination is removed +1 strlen should also go away.

--
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.

#24 lore raw

Re: [ libsepol 10 ] Bugfixes for 0-9

Quoted 1 line · Ivan Gyurdiev

Remove unnecessary null-termination as pointed out (priv mail)?

Yes.

Quoted 1 line

If null termination is removed +1 strlen should also go away.

No. Still need that for the nul terminator after the type when ! mls_enabled. That is why it used to be included in that computation - it was either going to be for the field separator or the nul terminator after the type, depending on whether or not MLS was enabled.

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.