Skip to contentSkip to conversation

Extract user records from binary policy

SEPOL Ivan Gyurdiev 2 participants 7 messages Sep 26, 2005 – Sep 27, 2005 SELinux security module

lore
Share & tools
lore
#1 lore raw
In reply to Ivan Gyurdiev

...and here's the same patch, with the obvious malloc bug fixed...tsk

include/sepol/users.h +4 -4
diff -Nrua libsepol.new/include/sepol/users.h libsepol/include/sepol/users.h
@@ -32,10 +32,10 @@
 	policydb_t* policydb,
 	const char* role);
 
-/* Obtain an array of all valid users/roles */
-extern int sepol_get_valid_users(
+/* Obtain an array of all valid users */
+extern int sepol_user_list(
 	policydb_t* policydb,
-	char*** users,
+	sepol_user_t** users,
 	size_t* nusers);
 
 extern int sepol_get_valid_roles(
@@ -43,4 +43,4 @@
 	char*** roles, 
 	size_t* nroles);
 
-#endif /* _SEPOL_USERS_H_ */
+#endif 
src/booleans.c +1 -1
diff -Nrua libsepol.new/src/booleans.c libsepol/src/booleans.c
@@ -11,7 +11,7 @@
 #include <sepol/conditional.h>
 #include <sepol/boolean_record.h>
 
-static inline int bool_update (
+static int bool_update (
 	policydb_t* policydb,
 	sepol_bool_t boolean) {
 
src/interfaces.c +1 -1
diff -Nrua libsepol.new/src/interfaces.c libsepol/src/interfaces.c
@@ -12,7 +12,7 @@
 
 /* Create a low level interface structure from
  * a high level representation */
-int sepol_iface_struct_create(
+static int sepol_iface_struct_create(
 	policydb_t* policydb,
 	ocontext_t** iface,
 	sepol_iface_t data) {
src/ports.c +1 -1
diff -Nrua libsepol.new/src/ports.c libsepol/src/ports.c
@@ -25,7 +25,7 @@
 
 /* Create a low level port structure from
  * a high level representation */
-int sepol_port_struct_create(
+static int sepol_port_struct_create(
 	policydb_t* policydb,
 	ocontext_t** port,
 	sepol_port_t data) {
src/users.c +80 -14

125 lines not on this page · raw message

#0 lore raw

The attached patch does the following:

- makes a couple of internal functions static
- un-inlines a boolean function

- changes existing sepol_valid_users_list, which returns tha names of valid users, to sepol_list_users, which returns an array of records, more suitable for semanage dbase operations (plus that includes all the data, not just names). This is fine for a short list users - may not be the appropriate thing to do for something like allow rules.

I did actually try this, so it should work :)

Here's the old genusers loading the MLS users, and then list_users extracting the info back in record format:

[phantom@cobra src]$ ./test /etc/selinux/mls/policy/policy.19
User: system_u with default MLS level s2, range s0-s9:c0.c127, roles: system_r
User: user_u with default MLS level s2, range s2, roles: user_r
User: root with default MLS level s2, range s0-s9:c0.c127, roles: system_r sysadm_r staff_r secadm_r ./test: Warning! unable to get boolean names: No such file or directory

include/sepol/users.h +4 -4
diff -Nrua libsepol.new/include/sepol/users.h libsepol/include/sepol/users.h
@@ -32,10 +32,10 @@
 	policydb_t* policydb,
 	const char* role);
 
-/* Obtain an array of all valid users/roles */
-extern int sepol_get_valid_users(
+/* Obtain an array of all valid users */
+extern int sepol_user_list(
 	policydb_t* policydb,
-	char*** users,
+	sepol_user_t** users,
 	size_t* nusers);
 
 extern int sepol_get_valid_roles(
@@ -43,4 +43,4 @@
 	char*** roles, 
 	size_t* nroles);
 
-#endif /* _SEPOL_USERS_H_ */
+#endif 
src/booleans.c +1 -1
diff -Nrua libsepol.new/src/booleans.c libsepol/src/booleans.c
@@ -11,7 +11,7 @@
 #include <sepol/conditional.h>
 #include <sepol/boolean_record.h>
 
-static inline int bool_update (
+static int bool_update (
 	policydb_t* policydb,
 	sepol_bool_t boolean) {
 
src/interfaces.c +1 -1
diff -Nrua libsepol.new/src/interfaces.c libsepol/src/interfaces.c
@@ -12,7 +12,7 @@
 
 /* Create a low level interface structure from
  * a high level representation */
-int sepol_iface_struct_create(
+static int sepol_iface_struct_create(
 	policydb_t* policydb,
 	ocontext_t** iface,
 	sepol_iface_t data) {
src/ports.c +1 -1
diff -Nrua libsepol.new/src/ports.c libsepol/src/ports.c
@@ -25,7 +25,7 @@
 
 /* Create a low level port structure from
  * a high level representation */
-int sepol_port_struct_create(
+static int sepol_port_struct_create(
 	policydb_t* policydb,
 	ocontext_t** port,
 	sepol_port_t data) {
src/users.c +77 -14

122 lines not on this page · raw message

#2 lore raw
In reply to Ivan Gyurdiev
Quoted 1 line · Ivan Gyurdiev

...and here's the same patch, with the obvious malloc bug fixed...tsk

src/users.c +0 -0
diff -Nrua libsepol.new/src/users.c libsepol/src/users.c

+ /* Extract MLS info */
+ if (mls_enabled) {
+ context_struct_t context;
+ char *str;
+ int len;
+
+ context_init(&context);
+ memcpy(&context.range.level[0],
+ &usrdatum->dfltlevel, sizeof(mls_level_t));
+ memcpy(&context.range.level[1],
+ &usrdatum->dfltlevel, sizeof(mls_level_t));
+ len = mls_compute_context_len(policydb, &context);
+ str = (char*) malloc(len);
+ if (str == NULL)
+ goto omem;
+ mls_sid_to_context(policydb, &context, &str);
+ str -= len;
+
+ if ( sepol_user_set_mlslevel(tmp_users[i], str + 1) < 0 ) {
+ free(str);
+ goto err;
+ }
+ free(str);
+
+ context_init(&context);
+ memcpy(&context.range, &usrdatum->range, sizeof(mls_range_t));
+ len = mls_compute_context_len(policydb, &context);
+ str = (char*) malloc(len);
+ if (str == NULL)
+ goto omem;
+ mls_sid_to_context(policydb, &context, &str);
+ str -= len;
+
+ if ( sepol_user_set_mlsrange(tmp_users[i], str + 1) < 0) {
+ free(str);
+ goto err;
+ }
+ free(str);
+ }
}

I understand that you are trying to just re-use the existing mls_ functions here, but I think it would be better to alter them or add new ones that provide a saner interface for this kind of manipulation. In particular, the manipulation of str above makes me nervous, even if it is technically correct.

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.
#3 lore raw
Quoted 8 lines

I understand that you are trying to just re-use the existing mls_ functions here, but I think it would be better to alter them or add new ones that provide a saner interface for this kind of manipulation. In particular, the manipulation of str above makes me nervous, even if it is technically correct.

Yes, I agree the mls interface should be improved... so I don't have to write things like:

               if (mls_context_to_sid(policydb, '$', &mls_level, 
&context)) {
                       DEBUG(__FUNCTION__, "invalid level %s for user 
%s\n",
                               mls_level, name);
                       goto err;
               }

Is this patch vetoed for now?

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

I understand that you are trying to just re-use the existing mls_ functions here, but I think it would be better to alter them or add new ones that provide a saner interface for this kind of manipulation. In particular, the manipulation of str above makes me nervous, even if it is technically correct.

Yes, I agree the mls interface should be improved... so I don't have to write things like:

Quoted 7 lines
                if (mls_context_to_sid(policydb, '$', &mls_level, 
&context)) {
                        DEBUG(__FUNCTION__, "invalid level %s for user 
%s\n",
                                mls_level, name);
                        goto err;
                }

Yes. The interface did make sense for its original usage, but not for the way in which it is being used here.

Quoted 1 line

Is this patch vetoed for now?

Yes.

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.
#5 lore raw
Quoted 24 lines · Stephen Smalley
Quoted 11 lines · Ivan Gyurdiev
Quoted 9 lines

I understand that you are trying to just re-use the existing mls_ functions here, but I think it would be better to alter them or add new ones that provide a saner interface for this kind of manipulation. In particular, the manipulation of str above makes me nervous, even if it is technically correct.

Yes, I agree the mls interface should be improved... so I don't have to write things like:

Quoted 9 lines
               if (mls_context_to_sid(policydb, '$', &mls_level, 
&context)) {
                       DEBUG(__FUNCTION__, "invalid level %s for user 
%s\n",
                               mls_level, name);
                       goto err;
               }
Quoted 4 lines

Yes. The interface did make sense for its original usage, but not for the way in which it is being used here.

Well, I could define a new function which allocates the string (after computing proper length), writes to it, and returns a valid ptr... however that's just moving the potential point of failure from two calls in users.c into one function in mls.c....

Like this?

int sepol_mls_struct_to_string(policydb_t* policydb, context_struct_t* mls, char** str). int sepol_mls_struct_from_string(policydb_t* policydb, char* str, context_struct_t** mls).

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

Well, I could define a new function which allocates the string (after computing proper length), writes to it, and returns a valid ptr... however that's just moving the potential point of failure from two calls in users.c into one function in mls.c....

Like this?

int sepol_mls_struct_to_string(policydb_t* policydb, context_struct_t* mls, char** str).
int sepol_mls_struct_from_string(policydb_t* policydb, char* str, context_struct_t** mls).

Yes, although I think that context_struct_t* is fine in both cases (i.e. require the caller to provide one), especially as the caller may be building up a complete context structure and just want the MLS fields filled in by the mls function.

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.