Fix all the bugs
This is the part where I fix all the bugs, after enhancing my test case to test everything.
At present, I think it covers pretty much all the new things that I've added.
It passes valgrind, and works great with one memory leak, which I'm pretty sure has been there for a while - I'll track it down eventually.
Bugs:
- mls.c: fix invalid free() in mls_from_string (ugh... surprising it hasn't been found)
- context.c: fix false omem error in context.c, introduced by [ SEPOL 4 ]
- interfaces.c: fix out-of-order if statement, caused by [ SEPOL 5 ]
- interfaces.c: fix potential null dereference in error path of iface:add()
- ports.c: handle failure in ipproto2sepol
- booleans.c: fix invalid string being copied for booleans [ SEPOL 5 ]
- users.c: MLS: fix improper use of memcpy in users.c - introduce new functions mls_level_cpy, mls_level_destroy, mls_range_cpy, and use those instead of memcpy (and handle failure)
- users/ports/interfaces: call context_init/context_destroy where appropriate to fix memory leaks
New things (covered by test case):
- rework add() function as modify(), which is the planned way for iface/port updates
- implement exists() function for interfaces and ports
- simplify the port key check a bit - I'll probably have to have another look at this, because I don't think it will do the right thing on modify() - only on query. When you modify a range with a key as the subset of that range, we want it to punch a hole in the range, and split the declaration, but it will replace it entirely. This is easy to fix in sepol, but I also have to fix it in semanage, where it is not as easy - I'd rather think about this some more than do it the wrong way right now.
libsepol/include/sepol/interfaces.h +7 -1
@@ -5,6 +5,12 @@#include <sepol/iface_record.h> #include <stddef.h> +/* Check if an interface exists */ +extern int sepol_iface_exists ( +sepol_policydb_t * policydb, +sepol_iface_key_t * key, +int * response); +/* Query an interface */ extern int sepol_iface_query( sepol_policydb_t* policydb,
@@ -12,7 +18,7 @@ extern int sepol_iface_query( sepol_iface_t** response);/* Add an interface to policy */ -extern int sepol_iface_add ( +extern int sepol_iface_modify (sepol_policydb_t * policydb,sepol_iface_key_t * key,sepol_iface_t * data);
libsepol/include/sepol/policydb/context.h +16 -23
@@ -36,40 +36,33 @@ typedef struct context_struct {static inline void mls_context_init (context_struct_t * c) { -memset (&c->range,0 ,sizeof (c->range)); +mls_level_init (&c->range.level[0 ]); +mls_level_init (&c->range.level[1 ]); } -static inline int mls_context_cpy (context_struct_t * dst, -context_struct_t * src) -{ -int rc; - - dst->range.level[0 ].sens = src->range.level[0 ].sens; - rc =ebitmap_cpy (&dst->range.level[0 ].cat, &src->range.level[0 ].cat); -if (rc) - goto out; - - dst->range.level[1 ].sens = src->range.level[1 ].sens; - rc =ebitmap_cpy (&dst->range.level[1 ].cat, &src->range.level[1 ].cat); -if (rc) -ebitmap_destroy (&dst->range.level[0 ].cat); -out: -return rc; +static inline int mls_context_cpy ( +context_struct_t * dst, +context_struct_t * src) { + +if (mls_range_cpy (&dst->range, &src->range) <0 ) +return -1 ; + +return 0 ; }static inline int mls_context_cmp (context_struct_t * c1,context_struct_t * c2) + { -return ((c1->range.level[0 ].sens == c2->range.level[0 ].sens) && -ebitmap_cmp (&c1->range.level[0 ].cat,&c2->range.level[0 ].cat) && - (c1->range.level[1 ].sens == c2->range.level[1 ].sens) && -ebitmap_cmp (&c1->range.level[1 ].cat,&c2->range.level[1 ].cat)); +return (mls_level_eq (&c1->range.level[0 ], &c2->range.level[0 ]) && +mls_level_eq (&c1->range.level[1 ], &c2->range.level[1 ])); + }static inline void mls_context_destroy (context_struct_t * c) { -ebitmap_destroy (&c->range.level[0 ].cat); -ebitmap_destroy (&c->range.level[1 ].cat); +mls_level_destroy (&c->range.level[0 ]); +mls_level_destroy (&c->range.level[1 ]);mls_context_init (c); }
libsepol/include/sepol/policydb/mls_types.h +41 -2
54 lines not on this page · raw message
libsepol/include/sepol/ports.h +8 -2
@@ -5,14 +5,20 @@#include <sepol/port_record.h> #include <stddef.h> +/* Check if a port exists */ +extern int sepol_port_exists ( +sepol_policydb_t * policydb, +sepol_port_key_t * key, +int * response); +/* Query a port */ extern int sepol_port_query (sepol_policydb_t * policydb,sepol_port_key_t * key,sepol_port_t ** response); -/* Add a port into policy */ -extern int sepol_port_add ( +/* Modify a port into policy */ +extern int sepol_port_modify (sepol_policydb_t * policydb,sepol_port_key_t * key,sepol_port_t * data);
libsepol/src/booleans.c +1 -1
@@ -22,7 +22,7 @@ static int bool_update (int value;sepol_bool_key_unpack (key, &cname); - name =strdup (name); + name =strdup (cname); value =sepol_bool_get_value (data);if (!name) {
libsepol/src/context.c +1 -1
@@ -144,7 +144,7 @@ int context_from_record(const char * mls =sepol_context_get_mls (record); scontext = (context_struct_t *)malloc (sizeof (context_struct_t)); -if (!user || !role || !type || !mls || !scontext) { +if (!user || !role || !type || !scontext) {DEBUG (__FUNCTION__,"out of memory\n" ); goto err; }
libsepol/src/interfaces.c +52 -19
114 lines not on this page · raw message
libsepol/src/mls.c +2 -1
@@ -79,12 +79,13 @@ int mls_from_string( context_struct_t* mls) {char * tmp =strdup (str); +char * tmp_cp = tmp;if (!tmp) {DEBUG (__FUNCTION__,"out of memory\n" );return STATUS_ERR ; } -if (mls_context_to_sid (policydb,'$' , &tmp, mls)) { +if (mls_context_to_sid (policydb,'$' , &tmp_cp, mls)) {DEBUG (__FUNCTION__,"invalid MLS context %s\n" , str);free (tmp);return STATUS_ERR ;
libsepol/src/ports.c +73 -24
162 lines not on this page · raw message
libsepol/src/users.c +44 -17
104 lines not on this page · raw message
Quoted 7 lines · Ivan Gyurdiev
- simplify the port key check a bit - I'll probably have to have another look at this, because I don't think it will do the right thing on
modify()- only on query. When you modify a range with a key as the subset of that range, we want it to punch a hole in the range, and split the declaration, but it will replace it entirely. This is easy to fix in sepol, but I also have to fix it in semanage, where it is not as easy - I'd rather think about this some more than do it the wrong way right now.
The current approach is to simply put the more specific entries at the beginning of the list, and to use the first matching entry. Thus, you don't need to modify the existing larger range at all - you just need to insert your more specific entry before it in the list. checkpolicy simply preserves the matching order specified in the net_contexts configuration, and we order those entries manually to keep the more specific ones at the front. checkpolicy does treat it as an error if it finds that one of the later entries would be completely hidden (or duplicated) by an earlier entry, but overlap is permitted.
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 6 lines · Stephen Smalley
Quoted 3 lines · Ivan Gyurdiev
- simplify the port key check a bit - I'll probably have to have another
look at this, because I don't think it will do the right thing onmodify()- only on query. When you modify a range with a key as the subset of that range, we want it to punch a hole in the range, and split the declaration, but it will replace it entirely. This is easy to fix in sepol, but I also have to fix it in semanage, where it is not as easy - I'd rather think about this some more than do it the wrong way right now.
Quoted 10 lines
The current approach is to simply put the more specific entries at the beginning of the list, and to use the first matching entry. Thus, you don't need to modify the existing larger range at all - you just need to insert your more specific entry before it in the list. checkpolicy simply preserves the matching order specified in the
net_contextsconfiguration, and we order those entries manually to keep the more specific ones at the front. checkpolicy does treat it as an error if it finds that one of the later entries would be completely hidden (or duplicated) by an earlier entry, but overlap is permitted.
The same ordering approach may not be maintained in other backends. Even currently the file linked list is reversed every time the file is read, making this scheme not work for local files. I suppose I could change it, but I think there's better ways to solve the problem than rely on ordering - ordering is fragile.
--
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 5 lines · Ivan Gyurdiev
The same ordering approach may not be maintained in other backends. Even currently the file linked list is reversed every time the file is read, making this scheme not work for local files. I suppose I could change it, but I think there's better ways to solve the problem than rely on ordering - ordering is fragile.
Manual ordering is fragile, but checkpolicy and libsemanage/libsepol could explicitly order the list by specificity, as checkpolicy already does for node contexts (that also originally relied on the specified order, but was later enhanced by TCS to explicitly order the entries by specificity). In any event, putting a more specific range earlier in the list and letting it take precedence seems far cleaner to me than splitting the existing range.
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.