selinux_set_file_paths function
PATCH 1/1 selinux_set_file_paths function
Attached is a patch to libselinux that moves the setting of file_paths[] out of init_selinux_config and into its own exported function. This allows a caller to reset those paths to an alternate policy type for getting paths of files in that alternate policy type. The patch also includes a patch to semanage_install_active in libsemanage to use this function rather than hacking the strings returned by libselinux.
Thanks,
Chad
Signature · 6 lines
---------------------- Chad Sellers Tresys Technology, LLC [email protected] (410)290-1411 x117 http://www.tresys.com
include/selinux/selinux.h +1 -0
@@ -360,6 +360,7 @@ extern const char *selinux_customizable_extern const char *selinux_users_path (void );extern const char *selinux_usersconf_path (void );extern const char *selinux_path (void ); +extern int selinux_set_file_paths (char *type);/* Check a permission in the passwd class. Return 0 if granted or -1 otherwise. */
src/selinux_config.c +36 -7
66 lines not on this page · raw message
src/semanage_store.c +16 -23
@@ -847,48 +847,40 @@ cleanup:static int semanage_install_active (semanage_handle_t *sh) {int retval =-3 , r, len;char *storepath =NULL ; +const char *store_fc; +const char *store_hd; +char store_pol[PATH_MAX ]; +const char *really_active_store;struct stat astore, istore;const char *active_kernel =semanage_path (SEMANAGE_ACTIVE ,SEMANAGE_KERNEL );const char *active_fc =semanage_path (SEMANAGE_ACTIVE ,SEMANAGE_FC );const char *active_hd =semanage_path (SEMANAGE_ACTIVE ,SEMANAGE_HOMEDIR_TMPL ); -const char *running_fc =selinux_file_context_path (); -const char *running_hd =selinux_homedir_context_path (); -const char *running_policy =selinux_binary_policy_path (); -const char *really_active_store =selinux_policy_root (); - -/* This is very unelegant, the right thing to do is export the path -* building code in libselinux so that you can get paths for a given -* POLICYTYPE and should probably be done in the future. */ -char store_fc[PATH_MAX ]; -char store_hd[PATH_MAX ]; -char store_pol[PATH_MAX ]; - - len =strlen (really_active_store); - running_fc += len; - running_hd += len; - running_policy += len; - + really_active_store =selinux_policy_root (); len =strlen (selinux_path ()) +strlen (sh->conf->store_path) +1 ; storepath = (char *)malloc (len); -if (!storepath) +if (!storepath) { goto cleanup; -snprintf (storepath,PATH_MAX ,"%s%s" ,selinux_path (), sh->conf->store_path); + } +snprintf (storepath,PATH_MAX ,"%s%s" ,selinux_path (), sh->conf->store_path); +if (selinux_set_file_paths (sh->conf->store_path) <0 ) { + goto cleanup; + } + store_fc =selinux_file_context_path (); + store_hd =selinux_homedir_context_path (); +snprintf (store_pol,PATH_MAX ,"%s.%d" ,selinux_binary_policy_path (), sh->conf->policyvers); + -snprintf (store_pol,PATH_MAX ,"%s%s.%d" , storepath, - running_policy, sh->conf->policyvers);if (semanage_copy_file (active_kernel, store_pol) ==-1 ) {ERR (sh,"Could not copy %s to %s." , active_kernel, store_pol); goto cleanup; } -snprintf (store_hd,PATH_MAX ,"%s%s" , storepath, running_hd);if (semanage_copy_file (active_hd, store_hd) ==-1 ) {ERR (sh,"Could not copy %s to %s." , active_hd, store_hd); goto cleanup; } -snprintf (store_fc,PATH_MAX ,"%s%s" , storepath, running_fc);if (semanage_copy_file (active_fc, store_fc) ==-1 ) {ERR (sh,"Could not copy %s to %s." , active_fc, store_fc); goto cleanup;
@@ -934,6 +926,7 @@ skip_reload: retval =0 ; cleanup:free (storepath); +selinux_set_file_paths (NULL );return retval; }
Quoted 6 lines · Chad Sellers
Attached is a patch to libselinux that moves the setting of file_paths[] out of
init_selinux_configand into its own exported function. This allows a caller to reset those paths to an alternate policy type for getting paths of files in that alternate policy type. The patch also includes a patch tosemanage_install_activein libsemanage to use this function rather than hacking the strings returned by libselinux.
I'm not sure about the approach. Some thoughts:
- You are changing the libselinux file paths _except_ for the policy root, which you preserve for restoring the original values. This leaves the paths in an inconsistent state. If the approach were retained, I'd suggest saving the original type for restoration purposes, but setting policy root consistent with the other paths. In that case, setting of selinux_policyroot moves into your new function as well and the code is a bit simpler.
- Since you are changing the paths, any libselinux function that internally opens one of these paths will use the new path after a set has been performed, so it has side effects beyond just the functions that get paths. Not sure if that is a feature or a bug ;)
- Since the paths are global state of libselinux, if the caller is multi-threaded, then all threads will be affected (and the transition period isn't safe).
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 · Stephen Smalley
Quoted 6 lines · Chad Sellers
Attached is a patch to libselinux that moves the setting of file_paths[] out of
init_selinux_configand into its own exported function. This allows a caller to reset those paths to an alternate policy type for getting paths of files in that alternate policy type. The patch also includes a patch tosemanage_install_activein libsemanage to use this function rather than hacking the strings returned by libselinux.
Quoted 8 lines
I'm not sure about the approach. Some thoughts:
- You are changing the libselinux file paths _except_ for the policy root, which you preserve for restoring the original values. This leaves the paths in an inconsistent state. If the approach were retained, I'd suggest saving the original type for restoration purposes, but setting policy root consistent with the other paths. In that case, setting ofselinux_policyrootmoves into your new function as well and the code is a bit simpler.
yes, I should have done this to begin with
Quoted 4 lines
- Since you are changing the paths, any libselinux function that internally opens one of these paths will use the new path after a set has been performed, so it has side effects beyond just the functions that get paths. Not sure if that is a feature or a bug ;)
I thought of this as a feature, as any libsemanage function could easily switch back and forth between operating on the currently configured policy path and any other policy path. This is useful for integration with package management.
Quoted 3 lines
- Since the paths are global state of libselinux, if the caller is multi-threaded, then all threads will be affected (and the transition period isn't safe).
aah, the kicker. I see 3 options to solving this problem.
1) add locking. This maintains the ability to reuse libselinux interfaces on other policy types, but adds complexity
2) have the function operate on a file_paths paramater. The init function could pass the global file_paths array to it, while other callers could pass their own arrays. This is a simpler solution, but would require the caller to parse the array structure itself.
3) return to the previous method of having the caller parse the string returned by the regular libselinux path functions and replace the type in the path with the type it wants. This is the simplest approach, and it meets our current needs. Unfortunately, it hardcodes a directory structure.
I think locking for this need is overkill, so I tend to lean toward 2 or 3. Perhaps 3 will serve our needs for the current time, and we can revisit this at a later time.
Chad
Signature · 11 lines
---------------------- Chad Sellers Tresys Technology, LLC [email protected] (410)290-1411 x117 http://www.tresys.com -- 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 9 lines · Chad Sellers
Quoted 4 lines · Stephen Smalley
- Since you are changing the paths, any libselinux function that internally opens one of these paths will use the new path after a set has been performed, so it has side effects beyond just the functions that get paths. Not sure if that is a feature or a bug ;)
I thought of this as a feature, as any libsemanage function could easily switch back and forth between operating on the currently configured policy path and any other policy path. This is useful for integration with package management.
I'm not certain how it would be used in practice, on two counts:
1) semanage presently does policy reloading and file contexts validation via helper programs (load_policy, setfiles), so it cannot directly affect the paths used by them via such a libselinux interface, and
2) semanage only does a reload when dealing with the active store, so it doesn't need such an interface for policy loading, and setfiles already supports specification of the file contexts path (and libselinux supports that via its matchpathcon_init() interface).
Quoted 4 lines
2) have the function operate on a
file_pathsparamater. The init function could pass the globalfile_pathsarray to it, while other callers could pass their own arrays. This is a simpler solution, but would require the caller to parse the array structure itself.
The delicate point here is ensuring that we can continue to extend that array or even re-arrange it without breaking the libselinux ABI. To achieve that, I think you would need to:
a) have the function internally allocate the array itself rather than having the caller allocate it (since only libselinux knows how large the array needs to be), and
b) have the caller only access the array elements via new libselinux functions that parallel the existing selinux_* functions but take the path array as an argument, so that the caller isn't tied to specific indices for specific paths.
Quoted 8 lines
3) return to the previous method of having the caller parse the string returned by the regular libselinux path functions and replace the type in the path with the type it wants. This is the simplest approach, and it meets our current needs. Unfortunately, it hardcodes a directory structure.
I think locking for this need is overkill, so I tend to lean toward 2 or 3. Perhaps 3 will serve our needs for the current time, and we can revisit this at a later time.
I'd vote for #2, with the suggestions above, but agree that this isn't necessarily the highest priority right now.
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.