libsepol: clean up the CIL API
PATCH 1/2 libsepol: clean up the CIL API
- No longer require the caller to create a sepol_policydb. CIL is now responsible for that
- Since the user is no longer responsible for creating the policydb, two functions are added to let CIL know how it should configure the policydb, to set the policy version and the target platform
- Some functions, like cil_compile, do not need a policydb. Additionally some functions, like cil_filecons_to_string use the policydb, but could be rewritten to not require it. In these cases, remove the policydb from the API, and rewrite functions so they don't depend on it. The only function that uses a policydb is cil_build_policydb
- Add functions and symbolic versioning to maintain binary backwards compatability. API backwards compatability is not maintained
--- libsepol/cil/include/cil/cil.h | 12 ++-- libsepol/cil/secilc.c | 21 ++---- libsepol/cil/src/cil.c | 155 ++++++++++++++++++++++++++++++++++------ libsepol/cil/src/cil_binary.c | 67 ++++++++++++++++- libsepol/cil/src/cil_binary.h | 18 ++++- libsepol/cil/src/cil_internal.h | 8 ++- libsepol/src/libsepol.map.in | 14 +++- 7 files changed, 246 insertions(+), 49 deletions(-)
libsepol/cil/include/cil/cil.h +7 -5
@@ -40,16 +40,18 @@ extern void cil_db_destroy(cil_db_t **db);extern int cil_add_file (cil_db_t *db,char *name,char *data,size_t size); -extern int cil_compile (cil_db_t *db,sepol_policydb_t *sepol_db); -extern int cil_build_policydb (cil_db_t *db,sepol_policydb_t *sepol_db); -extern int cil_userprefixes_to_string (cil_db_t *db,sepol_policydb_t *sepol_db,char **out,size_t *size); -extern int cil_selinuxusers_to_string (cil_db_t *db,sepol_policydb_t *sepol_db,char **out,size_t *size); -extern int cil_filecons_to_string (cil_db_t *db,sepol_policydb_t *sepol_db,char **out,size_t *size); +extern int cil_compile (cil_db_t *db); +extern int cil_build_policydb (cil_db_t *db,sepol_policydb_t **sepol_db); +extern int cil_userprefixes_to_string (cil_db_t *db,char **out,size_t *size); +extern int cil_selinuxusers_to_string (cil_db_t *db,char **out,size_t *size); +extern int cil_filecons_to_string (cil_db_t *db,char **out,size_t *size);extern void cil_set_disable_dontaudit (cil_db_t *db,int disable_dontaudit);extern void cil_set_disable_neverallow (cil_db_t *db,int disable_neverallow);extern void cil_set_preserve_tunables (cil_db_t *db,int preserve_tunables);extern int cil_set_handle_unknown (cil_db_t *db,int handle_unknown);extern void cil_set_mls (cil_db_t *db,int mls); +extern void cil_set_target_platform (cil_db_t *db,int target_platform); +extern void cil_set_policy_version (cil_db_t *db,int policy_version);enum cil_log_level {CIL_ERR =1 ,
libsepol/cil/secilc.c +5 -16
@@ -204,6 +204,8 @@ int main(int argc, char *argv[]) }cil_set_mls (db, mls); +cil_set_target_platform (db, target); +cil_set_policy_version (db, policyvers);for (i = optind; i < argc; i++) { file =fopen (argv[i],"r" );
@@ -238,26 +240,13 @@ int main(int argc, char *argv[]) buffer =NULL ; } - rc =sepol_policydb_create (&pdb); -if (rc <0 ) { -cil_log (CIL_ERR ,"Failed to create policy db\n" ); - goto exit; - } - pdb->p.policy_type =POLICY_KERN ; - pdb->p.target_platform = target; - - rc =sepol_policydb_set_vers (pdb, policyvers); -if (rc !=0 ) { -cil_log (CIL_ERR ,"Failed to set policy version: %d\n" , rc); - goto exit; - } - rc =cil_compile (db, pdb); + rc =cil_compile (db);if (rc !=SEPOL_OK ) {cil_log (CIL_ERR ,"Failed to compile cildb: %d\n" , rc); goto exit; } - rc =cil_build_policydb (db, pdb); + rc =cil_build_policydb (db, &pdb);if (rc !=SEPOL_OK ) {cil_log (CIL_ERR ,"Failed to build policydb\n" ); goto exit;
@@ -306,7 +295,7 @@ int main(int argc, char *argv[])cil_log (CIL_INFO ,"Writing file contexts\n" ); - rc =cil_filecons_to_string (db, pdb, &fc_buf, &fc_size); + rc =cil_filecons_to_string (db, &fc_buf, &fc_size);if (rc !=SEPOL_OK ) {cil_log (CIL_ERR ,"Failed to get file context data\n" ); goto exit;
libsepol/cil/src/cil.c +135 -20
255 lines not on this page · raw message
libsepol/cil/src/cil_binary.c +64 -3
106 lines not on this page · raw message
libsepol/cil/src/cil_binary.h +17 -1
@@ -44,7 +44,23 @@* * @return SEPOL_OK upon success or an error otherwise. */ -int cil_binary_create (const struct cil_db *db,sepol_policydb_t *pdb); +int cil_binary_create (const struct cil_db *db,sepol_policydb_t **pdb); + +/** +* Create a pre allocated binary policydb from the cil db. +* +* It is assumed that pdb has been allocated and initialzed so that fields such +* as policy type and version are set appropriately. It is reccomended that +* instead of calling this, one instead calls cil_binary_create, which will +* properly allocate and initialize the pdb and then calls this function. This +* funcion is used to maintain binary backwards compatability. +* +* @param[in] db The cil database. +* @param[in] pdb The policy database. +* +* @return SEPOL_OK upon success or an error otherwise. +*/ +int cil_binary_create_allocated_pdb (const struct cil_db *db,sepol_policydb_t *pdb);/** * Insert cil common structure into sepol policydb.
libsepol/cil/src/cil_internal.h +5 -3
@@ -287,6 +287,8 @@ struct cil_db {int preserve_tunables;int handle_unknown;int mls; +int target_platform; +int policy_version; };struct cil_root {
@@ -871,9 +873,9 @@ void cil_destroy_data(void **data, enum cil_flavor flavor);int cil_flavor_to_symtab_index (enum cil_flavor flavor,enum cil_sym_index *index);const char *cil_node_to_string (struct cil_tree_node *node); -int cil_userprefixes_to_string (struct cil_db *db,sepol_policydb_t *sepol_db,char **out,size_t *size); -int cil_selinuxusers_to_string (struct cil_db *db,sepol_policydb_t *sepol_db,char **out,size_t *size); -int cil_filecons_to_string (struct cil_db *db,sepol_policydb_t *sepol_db,char **out,size_t *size); +int cil_userprefixes_to_string (struct cil_db *db,char **out,size_t *size); +int cil_selinuxusers_to_string (struct cil_db *db,char **out,size_t *size); +int cil_filecons_to_string (struct cil_db *db,char **out,size_t *size);void cil_symtab_array_init (symtab_t symtab[],int symtab_sizes[CIL_SYM_NUM ]);void cil_symtab_array_destroy (symtab_t symtab[]);
libsepol/src/libsepol.map.in +13 -1
@@ -1,4 +1,4 @@ -{ +LIBSEPOL_1.0 { global: expand_module_avrules; sepol_module_package_*; sepol_link_modules; sepol_expand_module; sepol_link_packages;
@@ -34,3 +34,15 @@ cil_set_malloc_error_handler; local: *; }; + +LIBSEPOL_1.1 { + global: + cil_build_policydb; + cil_compile; + cil_userprefixes_to_string; + cil_selinuxusers_to_string; + cil_filecons_to_string; + cil_set_target_platform; + cil_set_policy_version; + local: *; +} LIBSEPOL_1.0;
Signature · 1 line
2.1.0
PATCH 2/2 libsemanage: use the new CIL API
- Do not pass in the policydb where not necessary
- Tell CIL what policy version and target platform to use when building the policydb
--- libsemanage/src/direct_api.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
libsemanage/src/direct_api.c +12 -14
@@ -557,7 +557,7 @@ static int write_file(semanage_handle_t * sh,return 0 ; } -static int semanage_direct_update_user_extra (semanage_handle_t * sh,cil_db_t *cildb,sepol_policydb_t *policydb) +static int semanage_direct_update_user_extra (semanage_handle_t * sh,cil_db_t *cildb) {const char *ofilename =NULL ;int retval =-1 ;
@@ -566,7 +566,7 @@ static int semanage_direct_update_user_extra(semanage_handle_t * sh, cil_db_t *cdbase_config_t *pusers_extra =semanage_user_extra_dbase_policy (sh); - retval =cil_userprefixes_to_string (cildb, policydb, &data, &size); + retval =cil_userprefixes_to_string (cildb, &data, &size);if (retval !=SEPOL_OK ) { goto cleanup; }
@@ -592,7 +592,7 @@ cleanup:return retval; } -static int semanage_direct_update_seuser (semanage_handle_t * sh,cil_db_t *cildb,sepol_policydb_t *policydb) +static int semanage_direct_update_seuser (semanage_handle_t * sh,cil_db_t *cildb) {const char *ofilename =NULL ;int retval =-1 ;
@@ -601,7 +601,7 @@ static int semanage_direct_update_seuser(semanage_handle_t * sh, cil_db_t *cildbdbase_config_t *pseusers =semanage_seuser_dbase_policy (sh); - retval =cil_selinuxusers_to_string (cildb, policydb, &data, &size); + retval =cil_selinuxusers_to_string (cildb, &data, &size);if (retval !=SEPOL_OK ) { goto cleanup; }
@@ -1180,6 +1180,9 @@ static int semanage_direct_commit(semanage_handle_t * sh)cil_set_disable_dontaudit (cildb, disable_dontaudit);cil_set_disable_neverallow (cildb, !(sh->conf->expand_check));cil_set_preserve_tunables (cildb, preserve_tunables); +cil_set_target_platform (cildb, sh->conf->target_platform); +cil_set_policy_version (cildb, sh->conf->policyvers); +if (sh->conf->handle_unknown !=-1 ) {cil_set_handle_unknown (cildb, sh->conf->handle_unknown); }
@@ -1188,22 +1191,17 @@ static int semanage_direct_commit(semanage_handle_t * sh)if (retval <0 ) { goto cleanup; } - -sepol_policydb_create (&out); - out->p.policy_type =POLICY_KERN ; -sepol_policydb_set_vers (out, sh->conf->policyvers); -sepol_policydb_set_target_platform (out, sh->conf->target_platform); - retval =cil_compile (cildb, out); + retval =cil_compile (cildb);if (retval <0 ) goto cleanup; - retval =cil_build_policydb (cildb, out); + retval =cil_build_policydb (cildb, &out);if (retval <0 ) goto cleanup;/* File Contexts */ - retval =cil_filecons_to_string (cildb, out, &fc_buffer, &fc_buffer_len); + retval =cil_filecons_to_string (cildb, &fc_buffer, &fc_buffer_len);if (retval <0 ) goto cleanup;
@@ -1225,12 +1223,12 @@ static int semanage_direct_commit(semanage_handle_t * sh) pfcontexts->dtable->drop_cache (pfcontexts->dbase);/* SEUsers */ - retval =semanage_direct_update_seuser (sh, cildb, out); + retval =semanage_direct_update_seuser (sh, cildb);if (retval <0 ) goto cleanup;/* User Extra */ - retval =semanage_direct_update_user_extra (sh, cildb, out); + retval =semanage_direct_update_user_extra (sh, cildb);if (retval <0 ) goto cleanup;
Signature · 1 line
2.1.0