libselinux patch
This patch initializes all swig parameters to prevent possible crashes in python.
Also changes is_selinux_enabled to work even if signaled and not have short reads.
src/selinuxswig_wrap.c renamed from src/selinuxswig_wrap.c.rhat +19 -19
@@ -2832,7 +2832,7 @@security_context_t *arg1 = (security_context_t *)0 ;int result;security_context_t temp1 =NULL ; -char *temp10 ; +char *temp10 =NULL ; { arg1 = &temp1;
@@ -2879,7 +2879,7 @@int val1 ;int ecode1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -2911,7 +2911,7 @@security_context_t *arg1 = (security_context_t *)0 ;int result;security_context_t temp1 =NULL ; -char *temp10 ; +char *temp10 =NULL ; { arg1 = &temp1;
@@ -2937,7 +2937,7 @@security_context_t *arg1 = (security_context_t *)0 ;int result;security_context_t temp1 =NULL ; -char *temp10 ; +char *temp10 =NULL ; { arg1 = &temp1;
@@ -2981,7 +2981,7 @@security_context_t *arg1 = (security_context_t *)0 ;int result;security_context_t temp1 =NULL ; -char *temp10 ; +char *temp10 =NULL ; { arg1 = &temp1;
@@ -3025,7 +3025,7 @@security_context_t *arg1 = (security_context_t *)0 ;int result;security_context_t temp1 =NULL ; -char *temp10 ; +char *temp10 =NULL ; { arg1 = &temp1;
@@ -3069,7 +3069,7 @@security_context_t *arg1 = (security_context_t *)0 ;int result;security_context_t temp1 =NULL ; -char *temp10 ; +char *temp10 =NULL ; { arg1 = &temp1;
@@ -3117,7 +3117,7 @@char *buf1 =0 ;int alloc1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -3155,7 +3155,7 @@char *buf1 =0 ;int alloc1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -3192,7 +3192,7 @@int val1 ;int ecode1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -3314,7 +3314,7 @@int val1 ;int ecode1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -3475,7 +3475,7 @@security_context_t *arg2 = (security_context_t *)0 ;int result;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -3735,7 +3735,7 @@unsigned int val2 ;int ecode2 =0 ;security_context_t temp3 =NULL ; -char *temp30 ; +char *temp30 =NULL ;PyObject * obj0 =0 ;PyObject * obj1 =0 ;
@@ -3779,7 +3779,7 @@char *buf1 =0 ;int alloc1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -4213,7 +4213,7 @@char *buf1 =0 ;int alloc1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -4251,7 +4251,7 @@char *buf1 =0 ;int alloc1 =0 ;security_context_t temp2 =NULL ; -char *temp20 ; +char *temp20 =NULL ;PyObject * obj0 =0 ; {
@@ -4284,7 +4284,7 @@PyObject *resultobj =0 ;char **arg1 = (char **)0 ;int result; -char *temp1 ; +char *temp1 =NULL ; { arg1 = &temp1;
@@ -4310,8 +4310,8 @@int res1 ;char *buf1 =0 ;int alloc1 =0 ; -char *temp2 ; -char *temp3 ; +char *temp2 =NULL ; +char *temp3 =NULL ;PyObject * obj0 =0 ; {
src/enabled.c renamed from src/enabled.c.rhat +39 -14
75 lines not on this page · raw message
src/selinuxswig.i renamed from src/selinuxswig.i.rhat +2 -2
@@ -28,7 +28,7 @@ %typemap(in, numinputs=0) security_context_t *(security_context_t temp=NULL) { $1 = &temp; } -%typemap(argout) security_context_t * (char *temp) { +%typemap(argout) security_context_t * (char *temp=NULL) { if (*$1) temp = *$1; else
@@ -126,7 +126,7 @@ extern int selinux_raw_to_trans_context(char *raw, security_context_t *transp); -%typemap(in, numinputs=0) char **(char *temp) { +%typemap(in, numinputs=0) char **(char *temp=NULL) { $1 = &temp; }
src/enabled.c line 14
Quoted 4 lines
src/enabled.c renamed from src/enabled.c.rhat +4
-0+static int readFD (int fd,char **buf) +{ +char *p; +size_t size =16384 ;
Why this number? Should it be a define somewhere or based off a kernel parameter?
+ int s, filesize;
+
+ *buf = calloc (16384, sizeof (char));
Why calloc? Its not as efficient as plain old malloc.
+ if (*buf == 0)
+ return -1;
0 or NULL? There's a difference on 64 bit machines.
+
+ filesize = 0;
+ do {
+ p = &(*buf) [filesize];
+ do {
+ s = read (fd, p, 16384);
+ } while (s < 0 && errno == EINTR);
+ if (s < 0)
+ break;
+ filesize += s; + /* only exit for empty reads */ + if (s == 0)
+ break;
+ size += s;
+ *buf = realloc (*buf, size);
So, if you get a short read, why are we playing with memory? This is inefficiant for the _normal_ case. The normal case is that you get about 400 bytes for a succesful read. I'd say you are better off adjusting the read size on the fly and only realloc when you have less that 512 bytes left in the buffer so that there is some hysteresis.
+ } while (1);
+
+ if (filesize == 0 && s < 0) {
+ free (*buf); + *buf = NULL; + return -1;
+ }
+
+ return filesize;
+}
+
____________________________________________________________________________________
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html
--
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 41 lines · Steve G
src/enabled.c line 14
Quoted 4 lines
src/enabled.c renamed from src/enabled.c.rhat +4
-0+static int readFD (int fd,char **buf) +{ +char *p; +size_t size =16384 ;Why this number? Should it be a define somewhere or based off a kernel parameter?
src/enabled.c renamed from src/enabled.c.rhat +3
-0+int s, filesize; + + *buf =calloc (16384 ,sizeof (char ));Why calloc? Its not as efficient as plain old malloc.
(quoted diff) +2
-0+ if (*buf == 0) + return -1;0 or NULL? There's a difference on 64 bit machines.
src/enabled.c renamed from src/enabled.c.rhat +15
-0+ + filesize =0 ; + do { + p = &(*buf) [filesize]; + do { + s = read (fd, p, 16384); + } while (s < 0 && errno == EINTR); + if (s < 0) + break; + filesize += s; + /* only exit for empty reads */ + if (s == 0) + break; + size += s; + *buf = realloc (*buf, size);So, if you get a short read, why are we playing with memory? This is inefficiant for the _normal_ case. The normal case is that you get about 400 bytes for a succesful read. I'd say you are better off adjusting the read size on the fly and only realloc when you have less that 512 bytes left in the buffer so that there is some hysteresis.
src/enabled.c renamed from src/enabled.c.rhat +1
-0+ }while (1 );
src/enabled.c line 45
Quoted 10 lines
src/enabled.c renamed from src/enabled.c.rhat +10
-0+ +if (filesize ==0 && s <0 ) { +free (*buf); + *buf =NULL ; +return -1 ; + } + +return filesize; +} +
I also have to wonder whether we'd be better off just switching from direct open+read of /proc/filesystems to just using fopen+getline, thereby automatically picking up glibc's internal support of dynamic buffer allocation and EINTR handling. Just read the file line-by-line and bail out if we find selinuxfs or hit the end of file.
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 6 lines
I also have to wonder whether we'd be better off just switching from direct open+read of
/proc/filesystemsto just using fopen+getline, thereby automatically picking up glibc's internal support of dynamic buffer allocation and EINTR handling. Just read the file line-by-line and bail out if we find selinuxfs or hit the end of file.
There might be a more robust, higher performance way of doing this. Is there ever a situation where the selinux filesystem is _not_ "/selinux" ? If there never is a case where this happens, then we could change over to statfs and check for SELINUX_MAGIC in the f_type field of the structure. Its only 1 syscall instead of several.
-Steve
____________________________________________________________________________________
Be a PS3 game guru.
Get your game face on with the latest PS3 news and previews at Yahoo! Games.
http://videogames.yahoo.com/platform?platform=120121
--
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 11 lines · Steve G
Quoted 5 lines
I also have to wonder whether we'd be better off just switching from direct open+read of
/proc/filesystemsto just using fopen+getline, thereby automatically picking up glibc's internal support of dynamic buffer allocation and EINTR handling. Just read the file line-by-line and bail out if we find selinuxfs or hit the end of file.There might be a more robust, higher performance way of doing this. Is there ever a situation where the selinux filesystem is _not_ "/selinux" ? If there never is a case where this happens, then we could change over to statfs and check for
SELINUX_MAGICin thef_typefield of the structure. Its only 1 syscall instead of several.
At present, libselinux dynamically determines where selinuxfs is mounted via /proc/mounts in init_selinuxmnt() and uses that path for all calls that operate on selinuxfs. Also, statfs'ing /selinux requires selinuxfs to already be mounted before you can call is_selinux_enabled(), whereas checking /proc/filesystems only requires that the selinux kernel code have registered selinuxfs as a filesystem (much earlier) and that proc be mounted.
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 4 lines
At present, libselinux dynamically determines where selinuxfs is mounted via
/proc/mountsininit_selinuxmnt()and uses that path for all calls that operate on selinuxfs.
I guess that path could be used for statfs?
Quoted 2 lines
Also, statfs'ing /selinux requires selinuxfs to already be mounted before you can call
is_selinux_enabled(),
Is selinux really enabled if its not available for use (mounted)?
-Steve
____________________________________________________________________________________
It's here! Your new message!
Get new email alerts with the free Yahoo! Toolbar.
http://tools.search.yahoo.com/toolbar/features/mail/
--
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 5 lines · Steve G
Quoted 3 lines
At present, libselinux dynamically determines where selinuxfs is mounted via
/proc/mountsininit_selinuxmnt()and uses that path for all calls that operate on selinuxfs.I guess that path could be used for statfs?
That still leaves you with a dependency on successfully reading /proc/mounts and allocating memory, so no different than reading /proc/filesystems as far as robustness goes. It only saves you time for that portion of is_selinux_enabled(), and is_selinux_enabled() also does a getcon() call (a hack) to check whether a policy has been loaded. is_selinux_enabled() should never be on a critical path (should always be called during startup by a program and saved for later use in a variable).
Quoted 4 lines
Quoted 2 lines
Also, statfs'ing /selinux requires selinuxfs to already be mounted before you can call
is_selinux_enabled(),Is selinux really enabled if its not available for use (mounted)?
Depends on what "enabled" means. If it just means enabled in the kernel, then checking /proc/filesystems for selinuxfs is the right test. Given that we also have it check for a loaded policy, and one has to mount selinuxfs to load policy, I suppose it might not make any difference.
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 10 lines
Quoted 6 lines
Quoted 4 lines
At present, libselinux dynamically determines where selinuxfs is mounted via
/proc/mountsininit_selinuxmnt()and uses that path for all calls that operate on selinuxfs.I guess that path could be used for statfs?
That still leaves you with a dependency on successfully reading
/proc/mountsand allocating memory, so no different than reading/proc/filesystemsas far as robustness goes.
Actually, you could guess "/selinux" and drop back to dynamically determining if that failed. that would get that call down to 1 syscall, too. I'd say 99.99% of the time it would be the right guess. Only at boot, shutdown, or a strange chroot config would there be a possibility of something different.
Quoted 3 lines
is_selinux_enabled()should never be on a critical path (should always be called during startup by a program and saved for later use in a variable).
Every selinux aware app checks it and it could short circuit seeveral syscalls to get the right answer.
-Steve
____________________________________________________________________________________
TV dinner still cooling?
Check out "Tonight's Picks" on Yahoo! TV.
http://tv.yahoo.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 2 lines
Actually, you could guess "/selinux" and drop back to dynamically determining if that failed.
This approach works and saves about 7 syscalls. This is the diff of running selinuxenabled with and without the attached patch.
close(3) = 0
munmap(0x2aaaaaaac000, 4096) = 0
-open("/proc/mounts", O_RDONLY) = 3-fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
-mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2aaaaaaac000
-read(3, "rootfs / rootfs rw 0 0\n/dev/root"..., 4096) = 673
-close(3) = 0
-munmap(0x2aaaaaaac000, 4096) = 0
+statfs("/selinux", {f_type=0xf97cff8c, f_bsize=4096, f_blocks=0, f_bfree=0, f_bavail=0, f_files=0, f_ffree=0, f_fsid={0, 0}, f_namelen=255, f_frsize=4096}) = 0
open("/selinux/mls", O_RDONLY) = 3
read(3, "1", 19) = 1
@@ -71,12 +66,10 @@
readv(3, [{"\0", 1}], 1) = 1
close(3) = 0
-open("/proc/filesystems", O_RDONLY) = 3-read(3, "nodev\tsysfs\nnodev\trootfs\nnodev\tb"..., 4095) = 311
-close(3) = 0
+statfs("/selinux", {f_type=0xf97cff8c, f_bsize=4096, f_blocks=0, f_bfree=0, f_bavail=0, f_files=0, f_ffree=0, f_fsid={0, 0}, f_namelen=255, f_frsize=4096}) = 0
gettid() = 14365
open("/proc/self/task/14365/attr/current", O_RDONLY) = 3
This improves performance, falls back to the old method when the guess is wrong, and checks that /selinux really is an selinuxfs.
While reading through the is_enabled code, I realized something. Right before the call to getcon_raw(), we decide that its enabled. If the getcon_raw() fails, we still consider it enabled. Only if the call return success do we do a test and consider it disabled. I don't know if that's good or bad, but I copied the behavior. Seems suspicious to me.
____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
include/selinux/selinux.h +2 -0
@@ -8,6 +8,8 @@extern "C" {#endif +#define SELINUX_MAGIC 0xf97cff8c +/* Return 1 if we are running on a SELinux kernel, or 0 if not or -1 if we get an error. */ extern int is_selinux_enabled (void );/* Return 1 if we are running on a SELinux MLS kernel, or 0 otherwise. */
src/enabled.c +19 -1
@@ -6,16 +6,34 @@#include <errno.h> #include <limits.h> #include <stdio.h> +#include <sys/vfs.h> +#include "policy.h" int is_selinux_enabled (void ) {char *buf;size_t size; -int fd; +int fd, rc;ssize_t ret;int enabled =0 ;security_context_t con; +struct statfs sfbuf; + +do { + rc =statfs ("/selinux" , &sfbuf); + }while (rc <0 && errno ==EINTR ); +if (rc ==0 ) { +if ((u_int32_t )sfbuf.f_type == (u_int32_t )SELINUX_MAGIC ) { + enabled =1 ; +if (getcon_raw (&con) ==0 ) { +if (!strcmp (con,"kernel" )) + enabled =0 ; +freecon (con); + } +return enabled; + } + } fd =open ("/proc/filesystems" ,O_RDONLY );if (fd <0 )
src/init.c +13 -1
@@ -6,7 +6,7 @@#include <ctype.h> #include <stdio.h> #include <dlfcn.h> -#include <unistd.h> +#include <sys/vfs.h> #include "dso.h" #include "policy.h"
@@ -21,10 +21,22 @@ static void init_selinuxmnt(void)char *buf, *bufp, *p;size_t size;FILE *fp; +struct statfs sfbuf; +int rc;if (selinux_mnt)return ; +do { + rc =statfs ("/selinux" , &sfbuf); + }while (rc <0 && errno ==EINTR ); +if (rc ==0 ) { +if ((u_int32_t )sfbuf.f_type == (u_int32_t )SELINUX_MAGIC ) { + selinux_mnt =strdup ("/selinux" ); +return ; + } + } + fp =fopen ("/proc/mounts" ,"r" );if (!fp)return ;
Quoted 5 lines · Steve G
Quoted 2 lines
Actually, you could guess "/selinux" and drop back to dynamically determining if that failed.
This approach works and saves about 7 syscalls. This is the diff of running selinuxenabled with and without the attached patch.
<snip>
Quoted 8 lines
This improves performance, falls back to the old method when the guess is wrong, and checks that /selinux really is an selinuxfs.
While reading through the
is_enabledcode, I realized something. Right before the call togetcon_raw(), we decide that its enabled. If thegetcon_raw()fails, we still consider it enabled. Only if the call return success do we do a test and consider it disabled. I don't know if that's good or bad, but I copied the behavior. Seems suspicious to me.
That was intentional; likely should add a comment. The presence of selinuxfs in /proc/filesystems is a strong indicator that SELinux is enabled; the getcon test was added later to detect no-policy-loaded, but could fail for other reasons and I didn't want a failure there to lead to a selinux-disabled status (particularly given that programs today usually only check for is_selinux_enabled() > 0). The only case where a SELinux system today should have selinuxfs in /proc/filesystems but no-policy-loaded is if SELINUX=permissive and init wasn't able to load policy for some reason (e.g. no policy or corrupted policy file); if SELINUX=enforcing, then init should halt if it can't load policy, and if SELINUX=disabled, init will use /selinux/disable to turn off SELinux, which unregisters selinuxfs too.
Patch looks basically sane; a couple of minor points:
- Let's move the existing SELINUXMNT definition used by src/load_policy.c for mounting selinuxfs for initial policy load to a private header (e.g. src/policy.h) and use it in this code rather than repeating the "/selinux" string each time.
- SELINUX_MAGIC doesn't need to be exposed outside of the library, so I'd put it into src/policy.h too instead of a public header.
- Why u_int32_t instead of just long (per the statfs man page)? And if we need to use a fixed size type, we should use C99 style i.e. uint32_t going forward - a recently merged patch converted all occurrences in libselinux over to C99 style.
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 2 lines
That was intentional; likely should add a comment.
OK, explanation makes sense. I'll add a comment to the patch.
Quoted 1 line
Patch looks basically sane;
Something else I was thinking about. When the library starts up, it checks for the mounts then it checks for enabled. Would there be any situation where an selinuxfs would be mounted and selinux is considered disabled? If not, we can short circuit the redundant check for /selinux and assume that if selinux_mnt != NULL then selinux is enabled. Does this make sense?
Quoted 4 lines
- Let's move the existing SELINUXMNT definition used by
src/load_policy.cfor mounting selinuxfs for initial policy load to a private header (e.g.src/policy.h) and use it in this code rather than repeating the "/selinux" string each time.
Sure.
Quoted 2 lines
-
SELINUX_MAGICdoesn't need to be exposed outside of the library, so I'd put it intosrc/policy.htoo instead of a public header.
I have a feeling that people want this. Almost all filesystems have the magic exposed or well known. SELINUX_MAGIC isn't even listed in statfs man page while most other filesystems are. If you search /usr/include for SELINUX_MAGIC, you'll find it defined in:
/usr/include/sepol/policydb/flask_types.h
So, why not have a definition in libselinux and let other subsystems use it?
Quoted 1 line
- Why
u_int32_tinstead of just long (per the statfs man page)?
SELINUX_MAGIC is 0xf97cff8c and the upper bit is 1, so it gets sign extended in 64 bit kernels. Then the direct comparison fails. No other MAGIC has a 1 in the upper bit so selinux is unique in that way. Try this little program and you'll see:
#include <stdio.h>
#include <sys/vfs.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(void)
{
DIR *d;
struct dirent *e;
d = opendir("/");
while ((e = readdir(d))) {
char path[PATH_MAX];
struct stat buf;
if (e->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "/%s", e->d_name);
stat(path, &buf);
if (S_ISDIR(buf.st_mode)) {
struct statfs sfbuf;
statfs(path, &sfbuf);
printf("%s: 0x%lx\n", path, sfbuf.f_type);
}
}
closedir(d);
return 0;
}Quoted 3 lines
And if we need to use a fixed size type, we should use C99 style i.e.
uint32_tgoing forward - a recently merged patch converted all occurrences in libselinux over to C99 style.
Sure. I discovered this after making the patch and was looking for a built-in type so I wouldn't have to re-diff the patch. Long term we should probably fix the sign-extension if possible.
-Steve
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.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 11 lines · Steve G
Quoted 1 line
That was intentional; likely should add a comment.
OK, explanation makes sense. I'll add a comment to the patch.
Quoted 1 line
Patch looks basically sane;
Something else I was thinking about. When the library starts up, it checks for the mounts then it checks for enabled. Would there be any situation where an selinuxfs would be mounted and selinux is considered disabled? If not, we can short circuit the redundant check for /selinux and assume that if
selinux_mnt!= NULL then selinux is enabled. Does this make sense?
Yes.
Quoted 9 lines
Quoted 2 lines
-
SELINUX_MAGICdoesn't need to be exposed outside of the library, so I'd put it intosrc/policy.htoo instead of a public header.I have a feeling that people want this. Almost all filesystems have the magic exposed or well known.
SELINUX_MAGICisn't even listed in statfs man page while most other filesystems are. If you search/usr/includeforSELINUX_MAGIC, you'll find it defined in:
/usr/include/sepol/policydb/flask_types.h
There it is used as a magic number in the binary policy file. Note btw that sepol/policydb headers aren't part of the shared library interface, just the static library.
Quoted 1 line
So, why not have a definition in libselinux and let other subsystems use it?
If a program actually needed the value for some purpose, I'd rather wrap that in a libselinux interface in some manner rather than directly
expose the value as a fixed definition. I also think that the
superblock magic number is somewhat meaningless for pseudo filesystems like selinuxfs, and there isn't any real guarantee of uniqueness.
Quoted 6 lines
Quoted 1 line
- Why
u_int32_tinstead of just long (per the statfs man page)?
SELINUX_MAGICis0xf97cff8cand the upper bit is 1, so it gets sign extended in 64 bit kernels. Then the direct comparison fails. No other MAGIC has a 1 in the upper bit so selinux is unique in that way. Try this little program and you'll see:
Hmm...well, (unsigned long) would seem adequate then.
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.
Hi,
OK, I think the attached patch does _everything _ we discussed. It:
- removes 8 syscalls for the normal path
- ensures /selinux is trully an selinuxfs
- drops back to detecting the old way when /selinux is missing
- changes the old way in is_enabled to use fopen for glibc internal retries
- adds retry for EINTR in mls_enabled
- keeps SELINUX_MAGIC private
____________________________________________________________________________________
Cheap talk?
Check out Yahoo! Messenger's low PC-to-Phone call rates.
http://voice.yahoo.com
src/enabled.c +36 -14
@@ -10,15 +10,33 @@int is_selinux_enabled (void ) { -char *buf; +char *buf, *bufp;size_t size; -int fd; +FILE *fp;ssize_t ret; -int enabled =0 ; +int rc, enabled =0 ;security_context_t con; - fd =open ("/proc/filesystems" ,O_RDONLY ); -if (fd <0 ) +/* init_selinuxmnt() gets called before this function. We +* will assume that if a selinux file system is mounted, then +* selinux is enabled. */ +if (selinux_mnt) { + +/* Since a file system is mounted, we consider selinux +* enabled. If getcon_raw fails, selinux is still enabled. +* We only consider it disabled if no policy is loaded. */ + enabled =1 ; +if (getcon_raw (&con) ==0 ) { +if (!strcmp (con,"kernel" )) + enabled =0 ; +freecon (con); + } +return enabled; + } + +/* Drop back to detecting it the long way. */ + fp =fopen ("/proc/filesystems" ,"r" ); +if (!fp)return -1 ; size = selinux_page_size;
@@ -30,17 +48,19 @@ int is_selinux_enabled(void)memset (buf,0 , size); - ret =read (fd, buf, size -1 ); -if (ret <0 ) { - enabled =-1 ; - goto out2; +while ((bufp =fgets_unlocked (buf, size, fp))) { +if (strstr (buf,"selinuxfs" )) { + enabled =1 ; +break ; + } } -if (!strstr (buf,"selinuxfs" )) +if (!bufp) goto out2; - enabled =1 ; - +/* Since an selinux file system is available, we consider +* selinux enabled. If getcon_raw fails, selinux is still +* enabled. We only consider it disabled if no policy is loaded. */ if (getcon_raw (&con) ==0 ) {if (!strcmp (con,"kernel" )) enabled =0 ;
@@ -49,7 +69,7 @@ int is_selinux_enabled(void) out2:free (buf); out: -close (fd); +fclose (fp);return enabled; }
@@ -75,7 +95,9 @@ int is_selinux_mls_enabled(void)memset (buf,0 ,sizeof buf); - ret =read (fd, buf,sizeof buf -1 ); +do { + ret =read (fd, buf,sizeof buf -1 ); + }while (ret <0 && errno ==EINTR );close (fd);if (ret <0 )return enabled;
src/init.c +14 -2
@@ -6,7 +6,8 @@#include <ctype.h> #include <stdio.h> #include <dlfcn.h> -#include <unistd.h> +#include <sys/vfs.h> +#include <stdint.h> #include "dso.h" #include "policy.h"
@@ -21,10 +22,22 @@ static void init_selinuxmnt(void)char *buf, *bufp, *p;size_t size;FILE *fp; +struct statfs sfbuf; +int rc;if (selinux_mnt)return ; +do { + rc =statfs (SELINUXMNT , &sfbuf); + }while (rc <0 && errno ==EINTR ); +if (rc ==0 ) { +if ((uint32_t )sfbuf.f_type == (uint32_t )SELINUX_MAGIC ) { + selinux_mnt =strdup (SELINUXMNT ); +return ; + } + } + fp =fopen ("/proc/mounts" ,"r" );if (!fp)return ;
@@ -62,7 +75,6 @@ static void init_selinuxmnt(void) out:fclose (fp);return ; - }static void fini_selinuxmnt (void )
src/load_policy.c +0 -1
@@ -165,7 +165,6 @@ hidden_def(selinux_mkload_policy)* We only need the hardcoded definition for the initial mount * required for the initial policy load. */ -#define SELINUXMNT "/selinux/"int selinux_init_load_policy (int *enforce) {int rc =0 , orig_enforce =0 , seconfig =-2 , secmdline =-1 ;
src/policy.h +6 -0
@@ -9,6 +9,12 @@/* Initial length guess for getting contexts. */ #define INITCONTEXTLEN 255 +/* selinuxfs magic number */ +#define SELINUX_MAGIC 0xf97cff8c + +/* Preferred selinux mount location */ +#define SELINUXMNT "/selinux" +/* selinuxfs mount point */ extern char *selinux_mnt;
Quoted 4 lines · Steve G
Hi,
OK, I think the attached patch does
_everything_ we discussed. It:
Quoted 8 lines
- removes 8 syscalls for the normal path
- ensures /selinux is trully an selinuxfs
- drops back to detecting the old way when /selinux is missing
- changes the old way inis_enabledto use fopen for glibc internal retries
- adds retry for EINTR inmls_enabled
- keepsSELINUX_MAGICprivateSigned-off-by Steve Grubb <[email protected]>
- Can drop the no-longer-used ret and rc local vars.
- Booting with the patched libselinux reveals some permission denials due to the new attempts to statfs /selinux, e.g. audit2allow after boot generates:
allow mount_t security_t : filesystem getattr; allow restorecon_t security_t : filesystem getattr; allow setrans_t security_t : filesystem getattr;
Hence, we'd need to add that permission to selinux_get_fs_mount() in refpolicy. Fortunately, since it just falls back to the old logic if the statfs fails, it doesn't change behavior so we don't need to coordinate closely with the policy update.
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.
OK, I think the attached patch does _everything _ we discussed. It:
- removes 8 syscalls for the normal path
- ensures /selinux is trully an selinuxfs
- drops back to detecting the old way when /selinux is missing
- changes the old way in is_enabled to use fopen & getline for glibc internal
retries
- adds retry for EINTR in mls_enabled
- keeps SELINUX_MAGIC private
____________________________________________________________________________________
Now that's room service! Choose from over 150,000 hotels in 45,000 destinations on Yahoo! Travel to find your fit.
http://farechase.yahoo.com/promo-generic-14795097
src/enabled.c +40 -27
@@ -10,46 +10,57 @@int is_selinux_enabled (void ) { -char *buf; -size_t size; -int fd; -ssize_t ret; +char *buf=NULL ; +FILE *fp; +ssize_t num; +size_t len;int enabled =0 ;security_context_t con; - fd =open ("/proc/filesystems" ,O_RDONLY ); -if (fd <0 ) -return -1 ; - - size = selinux_page_size; - buf =malloc (size); -if (!buf) { - enabled =-1 ; - goto out; - } +/* init_selinuxmnt() gets called before this function. We +* will assume that if a selinux file system is mounted, then +* selinux is enabled. */ +if (selinux_mnt) { + +/* Since a file system is mounted, we consider selinux +* enabled. If getcon_raw fails, selinux is still enabled. +* We only consider it disabled if no policy is loaded. */ + enabled =1 ; +if (getcon_raw (&con) ==0 ) { +if (!strcmp (con,"kernel" )) + enabled =0 ; +freecon (con); + } +return enabled; + } -memset (buf,0 , size); +/* Drop back to detecting it the long way. */ + fp =fopen ("/proc/filesystems" ,"r" ); +if (!fp) +return -1 ; - ret =read (fd, buf, size -1 ); -if (ret <0 ) { - enabled =-1 ; - goto out2; +while ((num =getline (&buf, &len, fp)) !=-1 ) { +if (strstr (buf,"selinuxfs" )) { + enabled =1 ; +break ; + } } -if (!strstr (buf,"selinuxfs" )) - goto out2; - - enabled =1 ; +if (num <0 ) + goto out; +/* Since an selinux file system is available, we consider +* selinux enabled. If getcon_raw fails, selinux is still +* enabled. We only consider it disabled if no policy is loaded. */ if (getcon_raw (&con) ==0 ) {if (!strcmp (con,"kernel" )) enabled =0 ;freecon (con); } - out2: -free (buf); + out: -close (fd); +free (buf); +fclose (fp);return enabled; }
@@ -75,7 +86,9 @@ int is_selinux_mls_enabled(void)memset (buf,0 ,sizeof buf); - ret =read (fd, buf,sizeof buf -1 ); +do { + ret =read (fd, buf,sizeof buf -1 ); + }while (ret <0 && errno ==EINTR );close (fd);if (ret <0 )return enabled;
src/init.c +28 -21
86 lines not on this page · raw message
src/load_policy.c +0 -1
@@ -165,7 +165,6 @@ hidden_def(selinux_mkload_policy)* We only need the hardcoded definition for the initial mount * required for the initial policy load. */ -#define SELINUXMNT "/selinux/"int selinux_init_load_policy (int *enforce) {int rc =0 , orig_enforce =0 , seconfig =-2 , secmdline =-1 ;
src/policy.h +6 -0
@@ -9,6 +9,12 @@/* Initial length guess for getting contexts. */ #define INITCONTEXTLEN 255 +/* selinuxfs magic number */ +#define SELINUX_MAGIC 0xf97cff8c + +/* Preferred selinux mount location */ +#define SELINUXMNT "/selinux" +/* selinuxfs mount point */ extern char *selinux_mnt;
Quoted 11 lines · Steve G
OK, I think the attached patch does
_everything_ we discussed. It:- removes 8 syscalls for the normal path
- ensures /selinux is trully an selinuxfs
- drops back to detecting the old way when /selinux is missing
- changes the old way inis_enabledto use fopen & getline for glibc internal
retries
- adds retry for EINTR inmls_enabled
- keepsSELINUX_MAGICprivateSigned-off-by Steve Grubb <[email protected]>
Thanks, applied the revised (with fsetlocking calls) patch on the trunk, libselinux 2.0.5. Note to Chris/Dan: "allow $1 security_t:filesystem getattr;" should be added to selinux_get_fs_mount() in the policy to allow the statfs() call introduced by this patch; it won't break anything if it is missing (since it falls back to the old logic upon failure), but it should be allowed.
I have not merged this patch onto the stable branch (libselinux 1.x) since the patch does include a user-visible change (the security_t:filesystem getattr permission check upon the statfs call), even though it does fall back gracefully upon failure there. If desired, we could merge the rest of the changes that have no user-visible impact on the stable branch to pick up the use of getline, EINTR handling, and checking of selinux_mnt by is_selinux_enabled().
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 11 lines · Stephen Smalley
Quoted 10 lines · Steve G
OK, I think the attached patch does
_everything_ we discussed. It:- removes 8 syscalls for the normal path
- ensures /selinux is trully an selinuxfs
- drops back to detecting the old way when /selinux is missing
- changes the old way inis_enabledto use fopen & getline for glibc internal
retries
- adds retry for EINTR inmls_enabled
- keepsSELINUX_MAGICprivate
Quoted 8 lines
Quoted 1 line
Signed-off-by Steve Grubb <[email protected]>Thanks, applied the revised (with fsetlocking calls) patch on the trunk, libselinux 2.0.5. Note to Chris/Dan: "allow $1 security_t:filesystem getattr;" should be added to
selinux_get_fs_mount()in the policy to allow thestatfs()call introduced by this patch; it won't break anything if it is missing (since it falls back to the old logic upon failure), but it should be allowed.
Added to refpolicy trunk, rev 2198.
Signature · 9 lines
Chris PeBenito Tresys Technology, LLC (410) 290-1411 x150 -- 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 5 lines · Steve G
Quoted 2 lines
- Why
u_int32_tinstead of just long (per the statfs man page)?
SELINUX_MAGICis0xf97cff8cand the upper bit is 1, so it gets sign extended in 64 bit kernels. Then the direct comparison fails.
Why not just have the define as:
#define SELINUX_MAGIC 0xf97cff8cU
Signature · 4 lines
James Antill - <[email protected]> setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION, ...); setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, ...); setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, ...);
Quoted 10 lines · Steve G
Quoted 3 lines
At present, libselinux dynamically determines where selinuxfs is mounted via
/proc/mountsininit_selinuxmnt()and uses that path for all calls that operate on selinuxfs.I guess that path could be used for statfs?
Quoted 2 lines
Also, statfs'ing /selinux requires selinuxfs to already be mounted before you can call
is_selinux_enabled(),Is selinux really enabled if its not available for use (mounted)?
When working under chroot(), /proc may be available but /selinux may not be.
In that case /proc/mounts will list /selinux but you will not be able to stat() it.
<<V13>>
--
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 2 lines · Daniel J Walsh
This patch initializes all swig parameters to prevent possible crashes in python.
I merged this part.
Quoted 2 lines
Also changes
is_selinux_enabledto work even if signaled and not have short reads.
But not this one. Still working out the best solution for is_selinux_enabled, as per the ongoing discussion on the list.
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.