fork and security context transitions
Dear all,
I have just started using SELinux and so far I am really satisfied with the possibilities and quality of the project, but I have found what is either a missing feature or, otherwise, a feature.
I working in the development of a server application whose client users must have an Unix Account (for authentication purposes) and an associated SELinux user account (every user is included in its own security context after login in).
My intention was to fork a child of the server for every client successfully authenticated, assigning to that child a client specific security context different from the one of the server.
It is at this point that my problem arose. After reading through the documentation, some white-papers and FAQs, I concluded that the only way to achieve this was using the "execve" function, which can be used to make a domain transition if the "setexeccon" has been previously executed and the adequate permissions have been granted in the policy. I understand that a child process inherits the parent's security context and that it can only be changed with the use of "execve". Furthermore, since a process cannot change its own security context I do not now any other way to reach my goal.
Due to the latter, I have started to split my server into two executables so that there's a listening server that creates children in the required security context (using "execve").
Anyway, I soon found that the resulting code is not as easy to maintain as the original one and that I have to make a fairly big amount of changes. Besides, I think this is not the right approach, since in this case, the use of SELinux is really having a big impact in the projects code.
I was wandering if allowing to fork a child in a security context different from the parent's (without using execve) is a good idea or not, since I find it useful and, anyway, it can be achieved splitting a program in two and using "execv".
I apologise in case I'm completely missing the point and I have misunderstood any of the underlying basic SELinux concepts.
Best wishes from a SELinux newbie,
Fernando Vázquez
Signature · 10 lines
NTT Data Corporation, R&D Headquarters Open Systems Architecture Group Fernando Vázquez E-Mail: <[email protected]> -- 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 3 lines · Fernando Luis Vázquez Cao
Due to the latter, I have started to split my server into two executables so that there's a listening server that creates children in the required security context (using "execve").
Good.
Quoted 5 lines
Anyway, I soon found that the resulting code is not as easy to maintain as the original one and that I have to make a fairly big amount of changes. Besides, I think this is not the right approach, since in this case, the use of SELinux is really having a big impact in the projects code.
Restructuring applications to support least privilege and isolation is a good thing.
Quoted 4 lines
I was wandering if allowing to fork a child in a security context different from the parent's (without using execve) is a good idea or not, since I find it useful and, anyway, it can be achieved splitting a program in two and using "execv".
It isn't a good idea. Some prior discussions of this issue can be found at the below links:
http://marc.theaimsgroup.com/?l=selinux&m=107272253121858&w=2
http://marc.theaimsgroup.com/?l=selinux&m=106910931928255&w=2
http://marc.theaimsgroup.com/?l=selinux&m=104939744006086&w=2
http://marc.theaimsgroup.com/?l=selinux&m=102587211414608&w=2
http://marc.theaimsgroup.com/?l=selinux&m=101343633303503&w=2
Signature · 8 lines
Stephen Smalley <[email protected]> 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.
Thank you for your prompt reply.
I have read through the links you provided me with (I'm sorry you have had to answer so many times to the same question) and I would like to make some further comments.
In one of your early replies you say that "SELinux only supports process SID transitions upon program execution so that inheritance of state and the initialization of the process in the new SID can be controlled". I agree with this statement, but what I was planning to do I feel that is slightly different.
I was thinking of implementing a new function analogical to "setexeccon", that could be called something like "setforkcon". "setforkcon" would be invoked before the execution of fork (just as its model function) and the child would be given the security context specified in the call IF, AND ONLY IF, the parent process has been granted the transition to the requested domain.
"fork" would be modified to be aware of the use of "setforkcon" (maybe via /proc/*/attr/fork), but the compatibility with the original version of fork would be preserved, since, in case "setforkcon" has not been used, it is functionally identical. Of course, some changes would have to be made in both the kernel and user space parts of "SELinux" to add support to a new kind of operation (let's say "ftransition"). The application requiring this functionality could be granted a "ftransition" permission with a command such as:
allow d1_t d2_t:process ftransition;
Wouldn't the "inheritance of state and the initialization of the process in the new SID" be controlled as effectively as it is with the use of "setexeccon" and "execv"?. This is, in once sentence, the source of all my doubts.
I think that such a functionality would ease the adoption of SELinux by new applications. Of course, if my proposal makes no sense and something like "setforkcon" is potentially insecure it SHOULD not be used, regardless of its ease of use or user-friendliness.
As an example, if we wanted to make Samba and Apache SELinux aware so that, in the event of a user session, a child process would be created in a user-specific domain, what should we do? With SELinux you are forced to split the servers in two executables, eventually being forced to make a lot of application specific changes (once more, if this is the only right approach let's stick with it).
I think that the previous examples represent a case different from that of inetd, crond or ssh (to cite some), since the latter have to fork third party applications that are completely unrelated to themselves and whose trustworthiness is unknown.
In the case of Samba, Apache and my own application, we are talking about ONE SINGLE application that forks itself. I think that splitting these programs into a listening server and a service application that is launched via execve is very similar, from a security point of view, to my proposal. Why do I think this?
* Before changing the security context functionally identical functions have to be executed ("setexeccon" and "setforkcon" respectively).
* Only if the right permissions (analogical in both cases) were granted in the policy, would the requested transition be performed.
* The "initialization of the process" is equally controlled with both solutions, since in both cases the children could be spawned in a set of contexts (one per user) that is predefined in the security policy.
* There's no need to make the child process execute a program from a type different from the father's, since both programs belong to the same application and, therefore, it is trusted. Any way, with the proposed extension I think that the same restrictions could be applied to the child processes.
With the proposed approach I think that not new vulnerabilities (I apologize if I am completely missing the point!!!!!) are introduced and that the migration to a SELinux aware application is easier.
I'm sorry for such a long mail and hope that what I have said makes any sense.
Best regards,
Fernando Vázquez
--
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 · Fernando Luis Vázquez Cao
As an example, if we wanted to make Samba and Apache SELinux aware so that, in the event of a user session, a child process would be created in a user-specific domain, what should we do? With SELinux you are forced to split the servers in two executables, eventually being forced to make a lot of application specific changes (once more, if this is the only right approach let's stick with it).
For Samba this will not work.
The SMB protocol supports multiple SMB identities on the same SMB connection. Currently this is only really used in Windows Terminal Server edition, but it should be used in future workstation products when some bugs in the MS client code are fixed.
Because of this such code that you might write for Samba will only work for a while (until the next release of Windows maybe).
Of course in the development version of Samba things are a bit different and you can write plug-ins to do these sorts of things. It would be possible to have the main Samba process execute helper programs to do the actual file IO and then communicate with the parent process by shared memory and pipes. Then a smbd which is serving for two SMB identities can have two helper children running in different contexts.
The other option is something like setfsuid() for SE Linux as previously discussed.
Signature · 11 lines
http://www.coker.com.au/selinux/ My NSA Security Enhanced Linux packages http://www.coker.com.au/bonnie++/ Bonnie++ hard drive benchmark http://www.coker.com.au/postal/ Postal SMTP/POP benchmark http://www.coker.com.au/~russell/ My home page -- 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.
I have read two previous messages somehow related to the problem I'm trying to address:
http://marc.theaimsgroup.com/?l=selinux&m=107397046201084&w=2
http://marc.theaimsgroup.com/?l=selinux&m=107400517101821&w=2
They deal with the nonexistence of setfsuid/setuid peers in SELinux in applications such as NFS or Samba, and I would like to know whether there's any ongoing work on that matter or, otherwise, it has been completely dismissed as harmful. In the former case I would be interested in its use/development.
I understand that you don't want to "allow wide and arbitrary changing of context as setuid()", or the setforkcon I suggested in a preceding email, does, but the arbitrariness of such functions would be constrained to a set of user-specific domains that allow access to user-specific types of files. Besides, a new "process control" could be created to limit the use of such new functions.
If all the NFS/Samba users used the same samba_staff_t domain for their IO, any user could access another user's files in the event of a bug in the code of the servers.
Wouldn't the existence of functions such as the previously mentioned improve the security?
As you say in one of the messages linked above, "it would be really difficult to change the smbd code to make it re-exec itself every time someone authenticates". Furthermore, I feel it is not a good way to approach the problem, since it would require a lot of application-specific changes.
Best regards
Fernando Vázquez
2004/02/03 (火) 22:19 に Russell Coker さんは書きました:
Quoted 23 lines
Quoted 6 lines · Fernando Luis Vázquez Cao
As an example, if we wanted to make Samba and Apache SELinux aware so that, in the event of a user session, a child process would be created in a user-specific domain, what should we do? With SELinux you are forced to split the servers in two executables, eventually being forced to make a lot of application specific changes (once more, if this is the only right approach let's stick with it).
For Samba this will not work.
The SMB protocol supports multiple SMB identities on the same SMB connection. Currently this is only really used in Windows Terminal Server edition, but it should be used in future workstation products when some bugs in the MS client code are fixed.
Of course in the development version of Samba things are a bit different and you can write plug-ins to do these sorts of things. It would be possible to have the main Samba process execute helper programs to do the actual file IO and then communicate with the parent process by shared memory and pipes. Then a smbd which is serving for two SMB identities can have two helper children running in different contexts.
Quoted 2 lines
The other option is something like
setfsuid()for SE Linux as previously discussed.
--
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 · Fernando Luis Vázquez Cao
http://marc.theaimsgroup.com/?l=selinux&m=107400517101821&w=2
I understand that you don't want to "allow wide and arbitrary changing of context as
setuid()", or the setforkcon I suggested in a preceding email, does, but the arbitrariness of such functions would be constrained to a set of user-specific domains that allow access to user-specific types of files. Besides, a new "process control" could be created to limit the use of such new functions.
I think that some sort of setfsuid() type operation makes sense.
Quoted 3 lines
If all the NFS/Samba users used the same
samba_staff_tdomain for their IO, any user could access another user's files in the event of a bug in the code of the servers.
In the case of a bug in the core server code then the setfsuid() type operation could be performed by the exploiting code so SE Linux would provide little protection against a cracked server process.
Signature · 11 lines
http://www.coker.com.au/selinux/ My NSA Security Enhanced Linux packages http://www.coker.com.au/bonnie++/ Bonnie++ hard drive benchmark http://www.coker.com.au/postal/ Postal SMTP/POP benchmark http://www.coker.com.au/~russell/ My home page -- 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 10 lines · Fernando Luis Vázquez Cao
I have read two previous messages somehow related to the problem I'm trying to address:
http://marc.theaimsgroup.com/?l=selinux&m=107397046201084&w=2
http://marc.theaimsgroup.com/?l=selinux&m=107400517101821&w=2They deal with the nonexistence of setfsuid/setuid peers in SELinux in applications such as NFS or Samba, and I would like to know whether there's any ongoing work on that matter or, otherwise, it has been completely dismissed as harmful. In the former case I would be interested in its use/development.
There is work in progress on NFS support, but that doesn't require exporting the interface to a "fs SID" to userspace, as Jim Carter noted earlier. But I expect that this will ultimately be exported to userspace for things like Samba, even though I don't think it offers much security benefit.
Signature · 8 lines
Stephen Smalley <[email protected]> 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 4 lines · Fernando Luis Vázquez Cao
Wouldn't the "inheritance of state and the initialization of the process in the new SID" be controlled as effectively as it is with the use of "setexeccon" and "execv"?. This is, in once sentence, the source of all my doubts.
No. On an exec-based domain transition, SELinux controls:
1) The ability of the source domain to transition to the target domain (which you can do on a fork, as you note).
2) The ability of the target domain to be entered via a particular entrypoint program. This binds the initialization of the process in that target domain to the startup code for that program, and binds the target domain to separate code from the source domain. It is important to be able to control what code can be executed with what set of permissions (i.e. domain) and how a process can be initialized (i.e. startup code) when run with a particular set of permissions. A fork-based domain transition can't enforce such controls; from the kernel's perspective, both the source and target domain execute the same code and the target domain can be initialized from anywhere within the program being executed by the source domain when it does the fork. This weakens the guarantees that can be provided by the kernel, and increases the assurance burden on the application.
3) The ability of the target domain to inherit state from the source domain, or stated differently, the ability of the source domain to influence the target domain by passing state to it. While we could implement specific forms of inheritance control and state sanitization for a domain-changing fork, these would increasingly diverge from the natural semantics of fork (whereas exec already has parallels for at least some of these controls), and you can't implement some at all without turning fork into exec - e.g. think about memory inheritance. If we implemented a domain-changing fork and provided as much inheritance control as feasible, it isn't clear that the resulting fork would be any more useable by existing applications than an exec-based transition.
Signature · 8 lines
Stephen Smalley <[email protected]> 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.