writing zero bytes in bash
This message is intentionally sent to the fedora-devel-list and the SE Linux list not the fedora-selinux-list. It is not related to Fedora specific SE Linux functionality, but it is related to bash (a core part of Fedora) and SE Linux kernel code.
To unset the fscreate or exec context you have to write zero bytes to /proc/self/attr/fscreate or /proc/self/attr/exec respectively.
If you want to do this in a shell script you would do something like:
echo -n "" > /proc/self/attr/fscreate
However that shell command results in bash (both the version in rawhide and the version in RHEL4) performing the following system calls:
open("/proc/self/attr/exec", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
fcntl64(1, F_GETFD) = 0 fcntl64(1, F_DUPFD, 10) = 10 fcntl64(1, F_GETFD) = 0 fcntl64(10, F_SETFD, FD_CLOEXEC) = 0 dup2(3, 1) = 1 close(3) = 0 dup2(10, 1) = 1 fcntl64(10, F_GETFD) = 0x1 (flags FD_CLOEXEC) close(10) = 0
It opens the file with O_CREAT (so if you were to do echo -n "" > /tmp/flag to create a flag file then it would work as expected), but never calls the write(2) system call.
To unset the fscreate or exec context you have to call write(fd, X, 0) (the value of X doesn't seem to matter as the kernel code doesn't dereference it).
It seems likely to me that there may be other bits of kernel code exporting an interface under /proc where a write of 0 bytes may have a different meaning to merely opening a file with write access and then closing it.
So it seems reasonable to me to consider this to be a bug in bash where it's optimisation of shell code results in the action requested by the user not being performed correctly.
It took a surprising amount of time for me to realise that bash wasn't doing what I expected of it and ran strace to prove it. I expect that most sys-admins would take even longer to work it out (if they ever did).
I expect that the bash developers may disagree with this assessment so I would like some more input on the lists before I file a bug report.
Signature · 9 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.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Interesting comment. I'm replying with the SE Linux list CC'd so everyone who's involved can comment.
Maybe a change to make "\n" equivalent to "" in the SE Linux kernel interfaces in question would be appropriate then? For the SE Linux interfaces in question a '\n' character is not valid in any part of the input and the input must contain three fields separated by colons, so a special case of "\n" being equivalent to "" can not cause any confusion.
Also as a side note, in the normal use only libselinux.so has any code which does such zero byte writes. Doing it from bash is only needed for testing, debugging, and educational purposes.
Quoted 19 lines · Miloslav Trmac
Quoted 2 lines · Russell Coker
To unset the fscreate or exec context you have to write zero bytes to
/proc/self/attr/fscreateor/proc/self/attr/execrespectively.I personally think this is the problem - no interface should require writing 0 bytes.
To quote the POSIX spec:
| Before any action described below is taken, and if nbyte is zero and | the file is a regular file, the write() function may detect and return | errors as described below. In the absence of errors, or if error | detection is not performed, the write() function shall return zero and | have no other results. If nbyte is zero and the file is not a regular | file, the results are unspecified.Files in /proc _are_ regular files, as far as
stat()is concerned, so I don't think you can require that portable programs give you the possibility to write 0 bytes to these files.
Mirek
Signature · 9 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 9 lines · Russell Coker
Interesting comment. I'm replying with the SE Linux list CC'd so everyone who's involved can comment.
Maybe a change to make "\n" equivalent to "" in the SE Linux kernel interfaces in question would be appropriate then? For the SE Linux interfaces in question a '\n' character is not valid in any part of the input and the input must contain three fields separated by colons, so a special case of "\n" being equivalent to "" can not cause any confusion.
Quoted 3 lines
Also as a side note, in the normal use only libselinux.so has any code which does such zero byte writes. Doing it from bash is only needed for testing, debugging, and educational purposes.
Pity no one pointed out this deviation from POSIX when we originally redesigned the SELinux API to address the kernel developers' comments on the original syscall-based API. In any event, write(fd, NULL, 0); does work on /proc/pid/attr/<name> nodes despite what the write(2) man page says, and the SELinux kernel API is obviously specific to Linux anyway (although libselinux could be ported to SEBSD), and this API is never suitable for use by scripts regardless of whether or not this particular aspect is changed.
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.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 5 lines · Mihai Maties
Quoted 4 lines · Russell Coker
It opens the file with
O_CREAT(so if you were to doecho -n "" > /tmp/flagto create a flag file then it would work as expected), but never calls the write(2) system call.
Quoted 8 lines
Quoted 3 lines
To unset the fscreate or exec context you have to call
write(fd, X, 0)(the value of X doesn't seem to matter as the kernel code doesn't dereference it).As a sidenote, dd behaves the same. dd if=/dev/null of=something does not call write(2) because it doesn't need to write anything. Since no records were read it assumes that no records must be written. Again, the output file would be created because of the
open("something", O_CREAT)call.
However dd bs=0 is an error, so dd refuses to operate in a situation where the operater specifically requests a zero write.
Quoted 6 lines
Quoted 3 lines
So it seems reasonable to me to consider this to be a bug in bash where it's optimisation of shell code results in the action requested by the user not being performed correctly.
It's quite logical since if you do not have something to write, to not try to write it.
That's one approach to the issue. The other is that if the administrator requests an operation then it should be completed. In the majority of situations where a user requests a strange operation Unix complies and does as requested. The expectation of a Unix system is that what you request will be done even if it is bad for you, so why not do what is requested just because it seems redundant?
Someone who does "echo -n > foo" to create file foo could have just as easily done "touch foo".
Quoted 6 lines
Quoted 2 lines
I expect that the bash developers may disagree with this assessment so I would like some more input on the lists before I file a bug report.
If I were a bash developer I would disagree with you :) You would ask me to make a workaround in bash to solve an issue that has its roots somewhere else...
Actually I am not asking for a feature to be added, but for a feature to be removed. There is special-case code in bash if(size >0) write() and I am merely suggesting that the if statement be removed.
Signature · 9 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.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 8 lines · Alexandre Oliva
Quoted 3 lines · Russell Coker
Actually I am not asking for a feature to be added, but for a feature to be removed. There is special-case code in bash if(size >0)
write()and I am merely suggesting that the if statement be removed.Have you considered that it might just be writing to a FILE*, and that it's the stdio subsystem that's deciding it doesn't need to write anything at
close()time because nothing was actually written?
Good point. A quick test program shows that fwrite() doesn't support writing zero bytes.
In that case we have a choice between having a change in the interface (such as supporting the writing of "\n" for an empty setting), or just leaving such operations as being impossible in shell scripts.
Signature · 9 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.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 7 lines · Tim Waugh
Quoted 2 lines · Russell Coker
To unset the fscreate or exec context you have to write zero bytes to
/proc/self/attr/fscreateor/proc/self/attr/execrespectively.Oh dear, really? How badly designed. The man page for
write()says that writing no bytes causeswrite()to return 0 "without causing any other effect".
Is an entry under /proc a "regular file" or a "special file"?
Sym-links under /proc/*/fd are not regular sym-links, which seems to imply that files under /proc are not "regular files". That seems to indicate that the section "the results are not portable" applies.
Alexandre has a good point though, so it seems that changing the interface would be a good idea.
Signature · 9 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.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 6 lines · Jeff Pitman
Quoted 3 lines · Russell Coker
To unset the fscreate or exec context you have to call write(fd, X,
0) (the value of X doesn't seem to matter as the kernel code doesn't dereference it).dd
I'm not sure what you are saying here. Are you suggesting to use dd to write zero bytes or that bash should work in a similar way to dd?
dd does not seem capable of actually doing a write of 0. If you use bs=0 then it gives an error code, if you use count=0 then it does nothing (like bash). Maybe this should be considered a bug in dd as well, but let's concentrate on bash for the moment.
This isn't the type of issue that can be addressed with a one-word reply.
Signature · 9 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.
as an aside, which may be of indirect relevance: the bluetooth specification for audio devices requires that data be read every oh 9ms or so in order to determine that the device is still active.
there's a #define in the linux kernel which specifically mentions the words "bluetooth" "audio" "zero" and "bytes".
hopefully that's only in the kernel and not percolated up to user land, esp. as it's been noted by others on the selinux mailing list the posix requirements that no interface shall provide or require a zero byte write.
Quoted 26 lines · Russell Coker
[...]
To unset the fscreate or exec context you have to write zero bytes to
/proc/self/attr/fscreateor/proc/self/attr/execrespectively.If you want to do this in a shell script you would do something like:
echo -n "" >/proc/self/attr/fscreateHowever that shell command results in bash (both the version in rawhide and the version in RHEL4) performing the following system calls:
open("/proc/self/attr/exec", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666)= 3fcntl64(1, F_GETFD) = 0 fcntl64(1, F_DUPFD, 10) = 10 fcntl64(1, F_GETFD) = 0 fcntl64(10, F_SETFD, FD_CLOEXEC) = 0 dup2(3, 1) = 1 close(3) = 0 dup2(10, 1) = 1 fcntl64(10, F_GETFD) = 0x1 (flags FD_CLOEXEC) close(10) = 0It opens the file with
O_CREAT(so if you were to doecho -n "" > /tmp/flagto create a flag file then it would work as expected), but never calls the write(2) system call.
Quoted 8 lines
To unset the fscreate or exec context you have to call
write(fd, X, 0)(the value of X doesn't seem to matter as the kernel code doesn't dereference it).It seems likely to me that there may be other bits of kernel code exporting an interface under /proc where a write of 0 bytes may have a different meaning to merely opening a file with write access and then closing it.
[...]
--
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 · Russell Coker
This message is intentionally sent to the fedora-devel-list and the SE Linux list not the fedora-selinux-list. It is not related to Fedora specific SE Linux functionality, but it is related to bash (a core part of Fedora) and SE Linux kernel code.
To unset the fscreate or exec context you have to write zero bytes to
/proc/self/attr/fscreateor/proc/self/attr/execrespectively.If you want to do this in a shell script you would do something like:
echo -n "" >/proc/self/attr/fscreate
Using the /proc/self/attr API from a shell script makes little sense, as any programs run by the script are going to have their attributes reset upon execve anyway. It would only affect shell builtins, and likely just cause confusion.
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 7 lines · Stephen Smalley
Quoted 2 lines
If you want to do this in a shell script you would do something like:
echo -n "" >/proc/self/attr/fscreateUsing the
/proc/self/attrAPI from a shell script makes little sense, as any programs run by the script are going to have their attributes reset upon execve anyway. It would only affect shell builtins, and likely just cause confusion.
/proc/self/attr/exec will give the same result as runcon. /proc/self/attr/fscreate works for operations such as "program > file".
We need to have a way to test these things. If we can't make it possible to do these things through the shell then someone (maybe me) has to write an interactive program to exercise the SE Linux API.
Signature · 9 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 7 lines · Russell Coker
/proc/self/attr/execwill give the same result as runcon./proc/self/attr/fscreateworks for operations such as "program > file".We need to have a way to test these things. If we can't make it possible to do these things through the shell then someone (maybe me) has to write an interactive program to exercise the SE Linux API.
You can already test them via runcon, mkdir -Z, etc.
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 10 lines · Stephen Smalley
Quoted 7 lines · Russell Coker
/proc/self/attr/execwill give the same result as runcon./proc/self/attr/fscreateworks for operations such as "program > file".We need to have a way to test these things. If we can't make it possible to do these things through the shell then someone (maybe me) has to write an interactive program to exercise the SE Linux API.
You can already test them via runcon, mkdir -Z, etc.
BTW, we _could_ modify the kernel code to also accept a string containing only a newline as a variant way of expressing "clear attribute" for writes to /proc/pid/attr. But such a change will obviously take time to get into an upstream kernel, and I don't even want to think about changing libselinux to start doing that (with the attendant dependency on the new kernel).
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 6 lines · Stephen Smalley
BTW, we _could_ modify the kernel code to also accept a string containing only a newline as a variant way of expressing "clear attribute" for writes to
/proc/pid/attr. But such a change will obviously take time to get into an upstream kernel, and I don't even want to think about changing libselinux to start doing that (with the attendant dependency on the new kernel).
Sample patch below for the kernel, allows you to do:
echo system_u:object_r:etc_t > /proc/self/attr/fscreate; echo hello > tmpfile;
echo "" > /proc/self/attr/fscreate;
echo hello > tmpfile2;
ls -Z tmpfile tmpfile2
-rw-r--r-- root root system_u:object_r:etc_t tmpfile -rw-r--r-- root root root:object_r:user_home_t tmpfile2
Index: linux-2.6/security/selinux/hooks.c
===================================================================
RCS file: /nfshome/pal/CVS/linux-2.6/security/selinux/hooks.c,v retrieving revision 1.152
security/selinux/hooks.c +6 -2
@@ -4094,6 +4094,7 @@ static int selinux_setprocattr(struct tastruct task_security_struct *tsec;u32 sid =0 ;int error; +char *str = value;if (current != p) {/* SELinux only allows a process to change its own
@@ -4118,8 +4119,11 @@ static int selinux_setprocattr(struct tareturn error;/* Obtain a SID for the context, if one was specified. */ -if (size) { -int error; +if (size && str[1 ] && str[1 ] !='\n' ) { +if (str[size-1 ] =='\n' ) { + str[size-1 ] =0 ; + size--; + } error =security_context_to_sid (value, size, &sid);if (error)return error;
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.