Signals upon avc denial
People working on Oculus (and from my past experience and current discussions with the Googlers CCed here, people working on Android) have been frustrated with the difficulty of tracking down the precise caller that triggers an selinux denial, since it might be multiple layers deep in transitive dependencies of the process that actually gets blamed. Thiébaud implemented a tracepoint which can be used to interactively track down a denial, but it has some shortcomings:
- it requires a tracer running as root (or some equivalently scary set of capabilities), so it's not really feasible to run it across our deployment of dogfood devices
- trace output doesn't provide the full context of the process, so AFAIK, it's not possible to do things like unwind through JIT compiled functions, or print syscall arguments that are on the heap
- it's asynchronous, so you can't go in with a debugger and inspect program state to figure out why the failing syscall was made in the first place
In an ideal world, an selinux failure would have the same sort of diagnostic tooling as a SIGSEGV. To do this, I propose that we let processes opt-in to receiving a signal when an avc denial occurs. If a user wants to debug interactively, they can set it to SIGSTOP manually; on systems like Android where libc registers a signal handler that collects a bug report and forwards it through a crash reporting pipeline, the system can set it to that signal automatically.
I'm guessing that the best interface for this is a file in procfs at somewhere along the lines of /proc/$PID/selinux/audit_signal, taking a signal number. It's probably useful to be able to correlate this with an audit serial number, so perhaps we should add an si_code value for this, with a field in siginfo_t?
To keep this from inadvertently allowing signals to be sent on systems that would otherwise prevent them entirely, we can add a new access vectors in process2 to get/set the audit signal.
I think the setting should be maintained across fork; I'm not sure whether it should be reset on exec. AT_SECURE transitions almost definitely should, but I'm not sure about the utility of inheriting it in the first place: if it's configured to a signal that's set by libc, and libc itself triggers an audit before it gets around to registering a signal handler, the process will mysteriously disappear.
I have a patch against 4.9 that implements most of this [1], but I wanted to run this by the upstream mailing list to get some feedback before reimplementing it on linux-next.
Thanks,
Josh
1: https://gist.github.com/jmgao/9c17bcc93d65472d9dd2a17362ed4075
Stephen Smalley
maintainerQuoted 46 lines · Josh Gao
People working on Oculus (and from my past experience and current discussions with the Googlers CCed here, people working on Android) have been frustrated with the difficulty of tracking down the precise caller that triggers an selinux denial, since it might be multiple layers deep in transitive dependencies of the process that actually gets blamed. Thiébaud implemented a tracepoint which can be used to interactively track down a denial, but it has some shortcomings:
- it requires a tracer running as root (or some equivalently scary set of capabilities), so it's not really feasible to run it across our deployment of dogfood devices
- trace output doesn't provide the full context of the process, so AFAIK, it's not possible to do things like unwind through JIT compiled functions, or print syscall arguments that are on the heap
- it's asynchronous, so you can't go in with a debugger and inspect program state to figure out why the failing syscall was made in the first place
In an ideal world, an selinux failure would have the same sort of diagnostic tooling as a SIGSEGV. To do this, I propose that we let processes opt-in to receiving a signal when an avc denial occurs. If a user wants to debug interactively, they can set it to SIGSTOP manually; on systems like Android where libc registers a signal handler that collects a bug report and forwards it through a crash reporting pipeline, the system can set it to that signal automatically.
I'm guessing that the best interface for this is a file in procfs at somewhere along the lines of
/proc/$PID/selinux/audit_signal, taking a signal number. It's probably useful to be able to correlate this with an audit serial number, so perhaps we should add ansi_codevalue for this, with a field insiginfo_t?To keep this from inadvertently allowing signals to be sent on systems that would otherwise prevent them entirely, we can add a new access vectors in process2 to get/set the audit signal.
I think the setting should be maintained across fork; I'm not sure whether it should be reset on exec.
AT_SECUREtransitions almost definitely should, but I'm not sure about the utility of inheriting it in the first place: if it's configured to a signal that's set by libc, and libc itself triggers an audit before it gets around to registering a signal handler, the process will mysteriously disappear.
Quoted 8 lines
I have a patch against 4.9 that implements most of this [1], but I wanted to run this by the upstream mailing list to get some feedback before reimplementing it on linux-next.
Thanks,
Josh1: https://gist.github.com/jmgao/9c17bcc93d65472d9dd2a17362ed4075
We already have a /proc/$PID/attr directory for LSM-related process attributes. Simple enough to add a new attribute there if we want it to be general/applicable to other
LSMs. That interface only allows a task to modify its own attributes (see proc_pid_attr_write);
I assume that suffices here? Other attributes set that way (e.g. fscreate / create_sid) are similarly
inherited across fork and unconditionally reset on execve.
Currently in the case of SELinux those attribute values are stored in the cred security blob, which
used to be the task security blob (hence the legacy task_security_struct name) before creds existed.
Later LSM revived support for a separate task security blob for the sake of TOMOYO so that does exist
if needed but SELinux doesn't currently allocate or use it. AppArmor switched to using it for its task-specific
state after it was restored; SELinux could split its current task_security_struct into two parts (per-cred and per-task) and do likewise if that is worthwhile (not sure).
With respect to the implementation, it is generally frowned upon to embed SELinux-specific fields and code
in the core kernel (versus wrapping in a LSM security field and hooks) and I think most if not all of what
you want is achievable through existing security fields and hooks once you add a new attribute to /proc/pid/attr.
Quoted 9 lines · Stephen Smalley
We already have a
/proc/$PID/attrdirectory for LSM-related process attributes.
Simple enough to add a new attribute there if we want it to be general/applicable to other
LSMs. That interface only allows a task to modify its own attributes (seeproc_pid_attr_write);
I assume that suffices here? Other attributes set that way (e.g. fscreate /create_sid) are similarly
inherited across fork and unconditionally reset on execve.
Thanks, that seems like a good place for it.
Quoted 11 lines
Currently in the case of SELinux those attribute values are stored in the cred security blob, which
used to be the task security blob (hence the legacytask_security_structname) before creds existed.
Later LSM revived support for a separate task security blob for the sake of TOMOYO so that does exist
if needed but SELinux doesn't currently allocate or use it. AppArmor switched to using it for its task-specific
state after it was restored; SELinux could split its currenttask_security_structinto two parts (per-cred and per-task) and do likewise if that is worthwhile (not sure).
My understanding is that struct cred is a per-task attribute, not per-thread-group, so different threads can have different task security blobs. Do you know if this is possible without doing something weird like setuid in a multithreaded process?
Quoted 8 lines
With respect to the implementation, it is generally frowned upon to embed SELinux-specific fields and code
in the core kernel (versus wrapping in a LSM security field and hooks)
and I think most if not all of what
you want is achievable through existing security fields and hooks once
you add a new attribute to/proc/pid/attr.
Yeah, I originally implemented this generally for all users of audit, but made it selinux specific after some discussion about how to turn the feature on/off globally. Your suggestion definitely seems like the right way to implement this.
Thanks,
Josh
Stephen Smalley
maintainerQuoted 14 lines · Josh Gao
Quoted 11 lines · Stephen Smalley
Currently in the case of SELinux those attribute values are stored in the cred security blob, which
used to be the task security blob (hence the legacytask_security_structname) before creds existed.
Later LSM revived support for a separate task security blob for the sake of TOMOYO so that does exist
if needed but SELinux doesn't currently allocate or use it. AppArmor switched to using it for its task-specific
state after it was restored; SELinux could split its currenttask_security_structinto two parts (per-cred and per-task) and do likewise if that is worthwhile (not sure).
Quoted 4 lines
My understanding is that
struct credis a per-task attribute, not per-thread-group, so different threads can have different task security blobs. Do you know if this is possible without doing something weird like setuid in a multithreaded process?
You can't just set the audit signal once in the first thread before creating multiple threads and let the child threads inherit it across clone(2)?
Currently ->exit_signal and ->pdeath_signal are part of task_struct, so it seems consistent to put this into something per-task, whether part of task->security or task->cred->security. If not and it needs to be shared across a thread group, I guess you could try to get a new security field added to the appropriate structure instead.