selinux_set_callback for policy load not triggering
Subsystems (3)
Hi All,
I am currently working on an update for dbus-broker to trigger reload of its configuration whenever an SELinux policy load event is seen.
For some background dbus-broker is comprised of two major elements the launcher and the broker. To trigger a config reload you can either send a SIGHUP to the launcher or send a message to the launcher over dbus. In most cases the launcher will be the brokers parent.
Here is a link to my current PR:
https://github.com/bus1/dbus-broker/pull/379
In this current state things work. The broker will see the POLICY_LOAD event and properly send a SIGHUP to its parent, but as David pointed out my initial attempt at the fix is no good since there is no guarantee that the brokers parent will be the launcher.
My attempts at moving the callback registration into the launcher have been less successful. From what my debugging has told me is that the selinux_set_callback is going through successfully and the function pointer is correctly pointing to the callback function I define. But when I trigger a load_policy my callback function is never called.
I am not familiar with how the callbacks in libselinux work under the hood so I am unsure about what could be blocking them in this situation.
Here is my current diff with all of my debug taken out to make it easier to read:
Makefile +1 -0
@@ -66,6 +66,7 @@ FORCE: MESON_SETUP = \ meson \ setup \ + --prefix=/usr \ --buildtype "debugoptimized" \ --reconfigure \ --warnlevel "2" \
meson_options.txt +0 -0
@@ -4,6 +4,6 @@ option('docs', type: 'boolean', value: false, description: 'Build documentation' option('launcher', type: 'boolean', value: true, description: 'Build
compatibility launcher')
option('linux-4-17', type: 'boolean', value: false, description:
'Require linux-4.17 at runtime and make use of its features')
option('reference-test', type: 'boolean', value: false, description:
'Run test suite against reference implementation') -option('selinux', type: 'boolean', value: false, description: 'SELinux support') +option('selinux', type: 'boolean', value: true, description: 'SELinux support')
option('system-console-users', type: 'array', value: [], description:
'Additional set of names of system-users to be considered at-console')
option('tests', type: 'boolean', value: false, description: 'Include
tests in the distribution')
src/launch/main.c +3 -0
@@ -9,6 +9,7 @@#include <systemd/sd-daemon.h> #include "launch/launcher.h" #include "util/error.h" +#include "util/selinux.h" enum { _MAIN_SUCCESS,
@@ -164,6 +165,8 @@ int main(int argc, char **argv) {if (r) goto exit; +bus_selinux_register_load_cb (); +sigemptyset (&mask_new);sigaddset (&mask_new,SIGCHLD );sigaddset (&mask_new,SIGTERM );
src/util/selinux-fallback.c +4 -0
@@ -60,3 +60,7 @@ int bus_selinux_init_global(void) {void bus_selinux_deinit_global (void ) {return ; } + +void bus_selinux_register_load_cb (void ) { +return ; +}
src/util/selinux.c +48 -0
57 lines not on this page · raw message
src/util/selinux.h +2 -0
@@ -36,3 +36,5 @@ int bus_selinux_check_send(BusSELinuxRegistry *registry,int bus_selinux_init_global (void );void bus_selinux_deinit_global (void ); + +void bus_selinux_register_load_cb (void );
Note that I have tried moving where the callback registration happens in the launcher with no luck. I know I can go down the dbus message path but the SIGHUP from the launcher seems so much more straight forward.
Thanks in advance for any insight folks might have.
Stephen Smalley
maintainerQuoted 19 lines · Matthew Sheets
Hi All,
I am currently working on an update for dbus-broker to trigger reload of its configuration whenever an SELinux policy load event is seen.
For some background dbus-broker is comprised of two major elements the launcher and the broker. To trigger a config reload you can either send a SIGHUP to the launcher or send a message to the launcher over dbus. In most cases the launcher will be the brokers parent.
Here is a link to my current PR:
https://github.com/bus1/dbus-broker/pull/379In this current state things work. The broker will see the
POLICY_LOADevent and properly send a SIGHUP to its parent, but as David pointed out my initial attempt at the fix is no good since there is no guarantee that the brokers parent will be the launcher.
Quoted 8 lines
My attempts at moving the callback registration into the launcher have been less successful. From what my debugging has told me is that the
selinux_set_callbackis going through successfully and the function pointer is correctly pointing to the callback function I define. But when I trigger aload_policymy callback function is never called.I am not familiar with how the callbacks in libselinux work under the hood so I am unsure about what could be blocking them in this situation.
Caveat: I haven't looked deeply so take this with a grain of salt (or two). There are generally two ways of discovering when policy has been reloaded:
1. Create and receive notifications on a SELinux netlink socket, or
2. Map the SELinux status page and poll it for updates to the policy seqno.
Internally libselinux has switched to using the status page whenever the kernel supports it since doing so is more efficient (no syscall required to read it once you've mapped the page). As an aside, the status page is also more easily "virtualizable" for SELinux namespaces since it is per-SELinux state/namespace already (the netlink socket can also be virtualized via a separate network namespace if/when my namespace patches land but that requires you to unshare the network namespace too).
As far as libselinux APIs are concerned for the status page, you can check for a policy reload or enforcing mode change by calling selinux_status_updated() at any time after having done an initial selinux_status_open(). selinux_status_updated() will call any registered callbacks if enforcing mode or policy was changed, and, it returns an indicator as to whether anything changed since the last time it was called.
Or if you choose to use the netlink socket and want to use the libselinux APIs, you'd call avc_netlink_acquire_fd() to create and take ownership of the SELinux netlink socket and then poll/select on it for notifications, and upon receiving them, calling avc_netlink_check_nb() to process them, including calling your callbacks.
But you have to do one of those two things in order for your callback to be invoked. I assume dbus and dbus-broker are already doing one of them which is why it works for them but not for the launcher.
Stephen Smalley
maintainerQuoted 60 lines · Stephen Smalley
Quoted 19 lines · Matthew Sheets
Hi All,
I am currently working on an update for dbus-broker to trigger reload of its configuration whenever an SELinux policy load event is seen.
For some background dbus-broker is comprised of two major elements the launcher and the broker. To trigger a config reload you can either send a SIGHUP to the launcher or send a message to the launcher over dbus. In most cases the launcher will be the brokers parent.
Here is a link to my current PR:
https://github.com/bus1/dbus-broker/pull/379In this current state things work. The broker will see the
POLICY_LOADevent and properly send a SIGHUP to its parent, but as David pointed out my initial attempt at the fix is no good since there is no guarantee that the brokers parent will be the launcher.Quoted 8 lines
My attempts at moving the callback registration into the launcher have been less successful. From what my debugging has told me is that the
selinux_set_callbackis going through successfully and the function pointer is correctly pointing to the callback function I define. But when I trigger aload_policymy callback function is never called.I am not familiar with how the callbacks in libselinux work under the hood so I am unsure about what could be blocking them in this situation.
Caveat: I haven't looked deeply so take this with a grain of salt (or two). There are generally two ways of discovering when policy has been reloaded:
1. Create and receive notifications on a SELinux netlink socket, or
2. Map the SELinux status page and poll it for updates to the policy seqno.Internally libselinux has switched to using the status page whenever the kernel supports it since doing so is more efficient (no syscall required to read it once you've mapped the page). As an aside, the status page is also more easily "virtualizable" for SELinux namespaces since it is per-SELinux state/namespace already (the netlink socket can also be virtualized via a separate network namespace if/when my namespace patches land but that requires you to unshare the network namespace too).
As far as libselinux APIs are concerned for the status page, you can check for a policy reload or enforcing mode change by calling
selinux_status_updated()at any time after having done an initialselinux_status_open().selinux_status_updated()will call any registered callbacks if enforcing mode or policy was changed, and, it returns an indicator as to whether anything changed since the last time it was called.Or if you choose to use the netlink socket and want to use the libselinux APIs, you'd call
avc_netlink_acquire_fd()to create and take ownership of the SELinux netlink socket and then poll/select on it for notifications, and upon receiving them, callingavc_netlink_check_nb()to process them, including calling your callbacks.
Quoted 3 lines
But you have to do one of those two things in order for your callback to be invoked. I assume dbus and dbus-broker are already doing one of them which is why it works for them but not for the launcher.
Also, both avc_has_perm*() and selinux_check_access() internally call selinux_status_updated() to ensure that they are using the latest enforcing mode and policy. So anything using those libselinux functions would get the status update for free.
Quoted 62 lines · Stephen Smalley
Quoted 60 lines · Stephen Smalley
Quoted 19 lines · Matthew Sheets
Hi All,
I am currently working on an update for dbus-broker to trigger reload of its configuration whenever an SELinux policy load event is seen.
For some background dbus-broker is comprised of two major elements the launcher and the broker. To trigger a config reload you can either send a SIGHUP to the launcher or send a message to the launcher over dbus. In most cases the launcher will be the brokers parent.
Here is a link to my current PR:
https://github.com/bus1/dbus-broker/pull/379In this current state things work. The broker will see the
POLICY_LOADevent and properly send a SIGHUP to its parent, but as David pointed out my initial attempt at the fix is no good since there is no guarantee that the brokers parent will be the launcher.Quoted 8 lines
My attempts at moving the callback registration into the launcher have been less successful. From what my debugging has told me is that the
selinux_set_callbackis going through successfully and the function pointer is correctly pointing to the callback function I define. But when I trigger aload_policymy callback function is never called.I am not familiar with how the callbacks in libselinux work under the hood so I am unsure about what could be blocking them in this situation.
Caveat: I haven't looked deeply so take this with a grain of salt (or two). There are generally two ways of discovering when policy has been reloaded:
1. Create and receive notifications on a SELinux netlink socket, or
2. Map the SELinux status page and poll it for updates to the policy seqno.Internally libselinux has switched to using the status page whenever the kernel supports it since doing so is more efficient (no syscall required to read it once you've mapped the page). As an aside, the status page is also more easily "virtualizable" for SELinux namespaces since it is per-SELinux state/namespace already (the netlink socket can also be virtualized via a separate network namespace if/when my namespace patches land but that requires you to unshare the network namespace too).
As far as libselinux APIs are concerned for the status page, you can check for a policy reload or enforcing mode change by calling
selinux_status_updated()at any time after having done an initialselinux_status_open().selinux_status_updated()will call any registered callbacks if enforcing mode or policy was changed, and, it returns an indicator as to whether anything changed since the last time it was called.Or if you choose to use the netlink socket and want to use the libselinux APIs, you'd call
avc_netlink_acquire_fd()to create and take ownership of the SELinux netlink socket and then poll/select on it for notifications, and upon receiving them, callingavc_netlink_check_nb()to process them, including calling your callbacks.
Quoted 8 lines
Quoted 3 lines
But you have to do one of those two things in order for your callback to be invoked. I assume dbus and dbus-broker are already doing one of them which is why it works for them but not for the launcher.
Also, both avc_has_perm*() and
selinux_check_access()internally callselinux_status_updated()to ensure that they are using the latest enforcing mode and policy. So anything using those libselinux functions would get the status update for free.
Just to close the loop. With the info above I was able to trace down that selinux_status_updated is being called by when the broker calls selinux_check_access. Thus triggering the callback. The launcher has no such check access call. Thanks for your help Stephen.