Skip to contentSkip to conversation

suggestion to help transition to selinux: dynamic loading of libselinux

DISCUSSION Luke Kenneth Casson Leighton 4 participants 12 messages May 17, 2004 – May 18, 2004 SELinux security module

lore
Share & tools
lore
#0 lore raw

dear nsa et al,

love selinux principle. it's _got_ to be made easier.

idea: make all the modifications to e.g. coreutils, login etc. compile with the header files but not necessarily _need_ libselinux at run-time.

way to do that is the same way that libdvdcss is loaded by
libdvdread: dynamically [there are plenty of other programs that use the same technique].

the suggestion is based on the premise that if you have a non-selinux kernel, and no libselinux installed, the programs (pam, coreutils, login etc.) will attempt to dynamically load libselinux (with dlopen), find it missing, _won't care_ and will adapt and proceed knowing that there's no security enhancements - i.e. as if they had never been patched to deal with selinux in the first place;

whilst if they _do_ detect that libselinux is available, an init function is called which returns a pointer to a vector table of all the functions needed, which are then used;

and i presume that if such programs are compromised, and someone attempts to _stop_ libselinux from being loaded, well then those programs will not be capable of making domain transitions etc. and they will be useless [am i right?]

the technique of having one function available from a dynamic library, which returns a table of all functions available in the library, and which is accessed via dlsym is very common, i am sure you have heard of it:

/* change all functions listed in this vector table from
extern to static: they are made accessible _only_ through the vector table, instead
*/

static libselinux_vector_table fns =

{
    is_selinux_enabled,
	freecon,
	freeconary,
	getcon,
	getpidcon,
	getprevcon,
	getexeccon,
	....
	....
	fsetfilecon
}

extern libselinux_vector_table *libselinux_init_fn(void);

then you do this, in every program that requires access to libselinux functions:

void *handle = dlopen("/usr/lib/libselinux.so", blah); void *(void) fn = dlsym(handle, "libselinux_init_fn");

libselinux_vector_table *sel = (libselinux_vector_table*)(*fn)();

then, for example, in the patch to coreutils,
http://selinux.lemuria.org/newselinux/coreutils/coreutils.diff

in coreutils-5.0.91/src/copy.c line 1444 approx, instead of ++#ifdef WITH_SELINUX
++ if (x->preserve_security_context)
++ setfscreatecon(NULL);
++#endif

you have:

if (sel != NULL && x->preserve_security_context)

	sel->setfscreatecon(NULL);

yes, correct: no #ifdef WITH_SELINUX :)

i.e. you must compile always with the header files, but you must NOT require the library libselinux to be present in order for the program to actually work.

this is a standard programming technique, i am sure that someone could provide better examples of how it is deployed.

the advantage of deploying it is that at present there is completely justifiable resistance upstream in accepting patches to the core functionality of linux useability that makes it impossible to use the core utilities for a non-selinux environment (!), and this suggestion makes the reason for that resistance go away.

i hope that you will consider adapting libselinux for use in this manner, because the sooner the dual-maintenance of the "alternate" versions of the core utils disappears, the better.

sincerely,

l.

p.s. please ask me for clarifications if needed or even for patches rather than go "uhn? weirdo, weird language, can't even touch a shift key, don't talk to him". i _want_ selinux to be adapted mainstream by debian, quickly, so talk to me.

Signature · 15 lines
-- 
expecting email to be received and understood is a bit like
picking up the telephone and immediately dialing without
checking for a dial-tone; speaking immediately without listening
for either an answer or ring-tone; hanging up immediately and
believing that you have actually started a conversation.
--
<a href="http://lkcl.net">      lkcl.net      </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />


--
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.
#1 lore raw
Quoted 4 lines · Luke Kenneth Casson Leighton

and i presume that if such programs are compromised, and someone attempts to _stop_ libselinux from being loaded, well then those programs will not be capable of making domain transitions etc. and they will be useless [am i right?]

The first candidate for such changes would be init. We would have to hard-code a check for selinux=0 in the init source to avoid having init just not enable SE Linux if file system corruption causes libselinux.so to disappear.

I think that init is the program that needs such changes most, the current situation makes libselinux one of the most important packages in the system (can't boot without it).

Quoted 9 lines

then, for example, in the patch to coreutils,
http://selinux.lemuria.org/newselinux/coreutils/coreutils.diff

in coreutils-5.0.91/src/copy.c line 1444 approx, instead of ++#ifdef WITH_SELINUX
++ if (x->preserve_security_context)
++ setfscreatecon(NULL);
++#endif

Quoted 7 lines

you have:

if (sel != NULL && x->preserve_security_context)

	sel->setfscreatecon(NULL);

yes, correct: no #ifdef WITH_SELINUX :)

In any case I think that we want #ifdef WITH_SELINUX. People who don't want to use SE Linux should have the option to compile without any of that code for small memory systems.

Quoted 6 lines

the advantage of deploying it is that at present there is completely justifiable resistance upstream in accepting patches to the core functionality of linux useability that makes it impossible to use the core utilities for a non-selinux environment (!), and this suggestion makes the reason for that resistance go away.

Of course we can do this on a per-application basis. If there is some application for which we can't get the current patches included but for which changes such as you suggest would be accepted then we should definitely do it for that app.

Of course that doesn't require changing libselinux, we could just call dlsym() for every function we want to use. Do any applications use so many functions that calling dlsym() repeatedly is excessive overhead?

Quoted 4 lines

p.s. please ask me for clarifications if needed or even for patches rather than go "uhn? weirdo, weird language, can't even touch a shift key, don't talk to him". i _want_ selinux to be adapted mainstream by debian, quickly, so talk to me.

The main thing that is needed for better SE Linux support in Debian is to have more people working on it. Doing this sort of thing properly requires that most than one person work on it.

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.
#2 lore raw
Quoted 10 lines · Russell Coker
Quoted 4 lines · Luke Kenneth Casson Leighton

and i presume that if such programs are compromised, and someone attempts to _stop_ libselinux from being loaded, well then those programs will not be capable of making domain transitions etc. and they will be useless [am i right?]

The first candidate for such changes would be init. We would have to hard-code a check for selinux=0 in the init source to avoid having init just not enable SE Linux if file system corruption causes libselinux.so to disappear.

oh. eek! of course :) cause libselinux.so to be corrupted and you're in.

hmmm.... physical security is assumed already (i.e. no boot from floppy)...

... modification of initscripts "by corruption" an outside chance...

... replacement of initscripts (apt-get install sysvinit) _cannot_ be ruled out...

... therefore the startup process must be secure (no dynamic loading)

i don't know enough to be able to say it would be good, or bad, to make _some_ programs do dynamic loading of libselinux and some not do it.

l.

--
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.

#5 lore raw
In reply to Russell Coker
Quoted 8 lines · Russell Coker

Of course we can do this on a per-application basis. If there is some application for which we can't get the current patches included but for which changes such as you suggest would be accepted then we should definitely do it for that app.

Of course that doesn't require changing libselinux, we could just call dlsym() for every function we want to use.

ah, you then need to check every pointer to every function for not being NULL.

it'd be aesthetically better to cut/paste sel-> in front of every function used rather than have if (fn != NULL) ...

plus you have to check for the success of each dlsym() rather than just the one.

you can just search for calls to is_selinux_enabled() and add the code to do one dlsym() at that point, in fact you could probably get away with adding a small macro into libselinux.h that'd do the hard work.

Quoted 8 lines
Quoted 4 lines

p.s. please ask me for clarifications if needed or even for patches rather than go "uhn? weirdo, weird language, can't even touch a shift key, don't talk to him". i _want_ selinux to be adapted mainstream by debian, quickly, so talk to me.

The main thing that is needed for better SE Linux support in Debian is to have more people working on it. Doing this sort of thing properly requires that most than one person work on it.

count me as a wobbly "in" :)

l.

--
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.

#6 lore raw
Quoted 5 lines · Luke Kenneth Casson Leighton
Quoted 2 lines

Of course that doesn't require changing libselinux, we could just call dlsym() for every function we want to use.

ah, you then need to check every pointer to every function for not being NULL.

That's easy to do. Most of the programs that might be re-written in that way don't use many of the libselinux functions anyway.

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.
#3 lore raw
In reply to Luke Kenneth Casson Leighton
Quoted 2 lines · Luke Kenneth Casson Leighton

... replacement of initscripts (apt-get install sysvinit) _cannot_ be ruled out...

If you install a non-SE version of sysvinit then you will entirely turn off SE Linux. There is no solution to this.

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.
#4 lore raw
In reply to Luke Kenneth Casson Leighton
Quoted 2 lines · Luke Kenneth Casson Leighton

oh. eek! of course :) cause libselinux.so to be corrupted and you're in.

Equally important - the sequence has to be able to DTRT if the fsck of the root filesystem fails. Remember - for dynamic linking to work, you need a *lot* more uncorrupted inodes. If the library you need is in /lost+found, you have some issues to work out.. ;)

(Note - DTRT doesn't necessarily mean "recover" - although a statically linked /sbin/fsck.ext3 helps a bunch there. Being able to drop the system to a known fail-safe state and then yell for help is probably sufficient).

#7 lore raw
In reply to Russell Coker
Quoted 8 lines · Russell Coker

The first candidate for such changes would be init. We would have to hard-code a check for selinux=0 in the init source to avoid having init just not enable SE Linux if file system corruption causes libselinux.so to disappear.

I think that init is the program that needs such changes most, the current situation makes libselinux one of the most important packages in the system (can't boot without it).

I'm surprised that /sbin/init isn't statically linked in Fedora. Rationale?

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.
#8 lore raw
Quoted 6 lines · Stephen Smalley
Quoted 5 lines · Russell Coker

The first candidate for such changes would be init. We would have to hard-code a check for selinux=0 in the init source to avoid having init just not enable SE Linux if file system corruption causes libselinux.so to disappear.

Quoted 6 lines
Quoted 3 lines

I think that init is the program that needs such changes most, the current situation makes libselinux one of the most important packages in the system (can't boot without it).

I'm surprised that /sbin/init isn't statically linked in Fedora. Rationale?

When fsck, getty, login, /bin/sh, cat etc, and all the other programs that are necessary for booting and allowing a root login are dynamically linked it doesn't seem to provide much benefit to have init statically linked.

To have all those programs dynamically linked would significantly increase disk space for small systems, or require having two versions of each with the static version being optional. So in ancient Linux history it was decided to use dynamically linked programs for all of those and almost everyone has followed. Some people have built custom distributions at various times to have statically linked /sbin programs but have not apparently received a benefit to justify the effort.

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.
#9 lore raw
In reply to Luke Kenneth Casson Leighton
Quoted 3 lines · Luke Kenneth Casson Leighton

idea: make all the modifications to e.g. coreutils, login etc. compile with the header files but not necessarily _need_ libselinux at run-time.

Interestingly, this issue just came up on the userlinux mailing list; someone noticed that upon doing an apt-get update, they pulled in libselinux1. After a bit of investigation, they found that this was due to updating gdm, because the SELinux patch for gdm is already upstreamed.

Note of caution: Some people still build /sbin/init statically, I think, and we don't want to prevent people from doing so just for SELinux. The current patches allow for that; if we rewrite them to use dlopen, won't that be a problem?

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.
#10 lore raw
Quoted 10 lines · Stephen Smalley
Quoted 3 lines · Luke Kenneth Casson Leighton

idea: make all the modifications to e.g. coreutils, login etc. compile with the header files but not necessarily _need_ libselinux at run-time.

Interestingly, this issue just came up on the userlinux mailing list; someone noticed that upon doing an apt-get update, they pulled in libselinux1. After a bit of investigation, they found that this was due to updating gdm, because the SELinux patch for gdm is already upstreamed.

ah ha! great! so it's begun.

Quoted 4 lines

Note of caution: Some people still build /sbin/init statically, I think, and we don't want to prevent people from doing so just for SELinux. The current patches allow for that; if we rewrite them to use dlopen, won't that be a problem?

hm.

yes.

you'd need two packages with the same name - two compile-time #ifdef things - to solve that one.

all in all, i'd say the pros and cons weigh against a rewrite.

l.

--
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.

#11 lore raw
In reply to Stephen Smalley
Quoted 5 lines · Stephen Smalley

Interestingly, this issue just came up on the userlinux mailing list; someone noticed that upon doing an apt-get update, they pulled in libselinux1. After a bit of investigation, they found that this was due to updating gdm, because the SELinux patch for gdm is already upstreamed.

I don't think that is a problem. If you have gdm installed then you have lots of other libraries dragged in by gdm itself and by all the other GNOME components. libselinux should be lost in the noise.

Using dlopen for SE Linux alone is not worth-while for gdm (IMHO) unless we find a more generic use for the mechanism (IE supporting things other than SE Linux).

Quoted 4 lines

Note of caution: Some people still build /sbin/init statically, I think, and we don't want to prevent people from doing so just for SELinux. The current patches allow for that; if we rewrite them to use dlopen, won't that be a problem?

To support that we would want to have #ifdef STATIC_BUILD or something similar. That should not be hard to do.

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.