Extract user records from binary policy
...and here's the same patch, with the obvious malloc bug fixed...tsk
include/sepol/users.h +4 -4
@@ -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
@@ -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
@@ -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
@@ -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
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
@@ -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
@@ -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
@@ -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
@@ -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
Quoted 1 line · Ivan Gyurdiev
...and here's the same patch, with the obvious malloc bug fixed...tsk
src/users.c +0 -0
+ /* 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.
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.
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.
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.
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.cinto one function inmls.c....Like this?
int
sepol_mls_struct_to_string(policydb_t* policydb, context_struct_t* mls, char** str).
intsepol_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.