Skip to contentSkip to conversation

libselinux lazy init

PATCH In review Steve G 2 participants 9 messages Mar 18, 2007 – Mar 19, 2007 SELinux security module

lore
Share & tools
lore

Steve G

author
#0 lore raw

PATCH 1/1 libselinux lazy init

Hi,

Attached is the re-worked (and tested) patch that changes libselinux's startup behavior of reading /etc/selinux/config to be on-demand. This improves performance measurably for small programs that do not need to know the details of policy. (Examples are cp, ls, id, install, ...) Please apply.

-Steve

____________________________________________________________________________________
No need to miss a message. Get email on-the-go
with Yahoo! Mail for Mobile. Get started.
http://mobile.yahoo.com/mail

src/selinux_config.c +23 -4
diff -urp libselinux-2.0.7.orig/src/selinux_config.c libselinux-2.0.7/src/selinux_config.c
@@ -7,6 +7,7 @@
 #include <stdlib.h>
 #include <limits.h>
 #include <unistd.h>
+#include <pthread.h>
 #include "selinux_internal.h"
 #include "get_default_type_internal.h"
 
@@ -40,6 +41,24 @@
 #define SECURETTY_TYPES   18
 #define NEL               19
 
+/* Make pthread_once optional */
+#pragma weak pthread_once
+
+/* Part of one-time lazy init */
+static pthread_once_t once = PTHREAD_ONCE_INIT;
+static void init_selinux_config(void);
+
+/* Call handler iff the first call.  */
+#define __selinux_once(ONCE_CONTROL, INIT_FUNCTION)	\
+	do {						\
+		if (pthread_once != NULL)		\
+			pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION));  \
+		else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) {		  \
+			INIT_FUNCTION ();		\
+			(ONCE_CONTROL) = 2;		\
+		}					\
+	} while (0)
+
 /* New layout is relative to SELINUXDIR/policytype. */
 static char *file_paths[NEL];
 #define L1(l) L2(l)
@@ -115,6 +134,7 @@ static char *selinux_policytype;
 
 int selinux_getpolicytype(char **type)
 {
+	__selinux_once(once, init_selinux_config);
 	if (!selinux_policytype)
 		return -1;
 	*type = strdup(selinux_policytype);
@@ -124,9 +144,7 @@ int selinux_getpolicytype(char **type)
 hidden_def(selinux_getpolicytype)
 
 static char *selinux_policyroot = NULL;
-static char *selinux_rootpath = NULL;
-
-static void init_selinux_config(void) __attribute__ ((constructor));
+static const char *selinux_rootpath = SELINUXDIR;
 
 static void init_selinux_config(void)
 {
@@ -139,7 +157,6 @@ static void init_selinux_config(void)
 	if (selinux_policyroot)
 		return;
 
-	selinux_rootpath = SELINUXDIR;
 	fp = fopen(SELINUXCONFIG, "r");
 	if (fp) {
 		__fsetlocking(fp, FSETLOCKING_BYCALLER);
@@ -224,6 +241,7 @@ static void fini_selinux_policyroot(void
 
 static const char *get_path(int idx)
 {
+	__selinux_once(once, init_selinux_config);
 	return file_paths[idx];
 }
 
@@ -236,6 +254,7 @@ hidden_def(selinux_default_type_path)
 
 const char *selinux_policy_root()
 {
+	__selinux_once(once, init_selinux_config);
 	return selinux_policyroot;
 }
 
#1 lore raw
Quoted 8 lines · Steve G

Hi,

Attached is the re-worked (and tested) patch that changes libselinux's startup behavior of reading /etc/selinux/config to be on-demand. This improves performance measurably for small programs that do not need to know the details of policy. (Examples are cp, ls, id, install, ...) Please apply.

Did you test the impact on a multi-threaded user of libselinux, like dbusd?

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.
#2 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 7 lines · Steve G

Hi,

Attached is the re-worked (and tested) patch that changes libselinux's startup behavior of reading /etc/selinux/config to be on-demand. This improves performance measurably for small programs that do not need to know the details of policy. (Examples are cp, ls, id, install, ...) Please apply.

Quoted 4 lines
Quoted 1 line

Did you test the impact on a multi-threaded user of libselinux, like dbusd?

Also, can you comment on why you chose not to pursue Ulrich's suggested approach? Just lack of time or was there a technical reason?

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.
author
#4 lore raw
In reply to Stephen Smalley
Quoted 3 lines

Did you test the impact on a multi-threaded user of libselinux, like dbusd?

Yes, I have run an X desktop and didn't notice anything different.

-Steve

____________________________________________________________________________________
No need to miss a message. Get email on-the-go
with Yahoo! Mail for Mobile. Get started.
http://mobile.yahoo.com/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.

#5 lore raw
In reply to Steve G
Quoted 8 lines · Steve G

Hi,

Attached is the re-worked (and tested) patch that changes libselinux's startup behavior of reading /etc/selinux/config to be on-demand. This improves performance measurably for small programs that do not need to know the details of policy. (Examples are cp, ls, id, install, ...) Please apply.

src/selinux_config.c +18 -0
diff -urp libselinux-2.0.7.orig/src/selinux_config.c libselinux-2.0.7/src/selinux_config.c
@@ -40,6 +41,24 @@
 #define SECURETTY_TYPES   18
 #define NEL               19
 
+/* Make pthread_once optional */
+#pragma weak pthread_once
+
+/* Part of one-time lazy init */
+static pthread_once_t once = PTHREAD_ONCE_INIT;
+static void init_selinux_config(void);
+
+/* Call handler iff the first call.  */
+#define __selinux_once(ONCE_CONTROL, INIT_FUNCTION)	\
+	do {						\
+		if (pthread_once != NULL)		\
+			pthread_once (&(ONCE_CONTROL), (INIT_FUNCTION));  \
+		else if ((ONCE_CONTROL) == PTHREAD_ONCE_INIT) {		  \
+			INIT_FUNCTION ();		\
+			(ONCE_CONTROL) = 2;		\
+		}					\
+	} while (0)
+
 
 

Looking back at Roland's advice, I notice that his sample code introduced an explicit bool flag rather than overloading the pthread_once_t variable. To me, that suggested that he thought that we shouldn't make assumptions about the pthread_once_t variable being an integer, which the above code does assume.

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.
author
#3 lore raw
In reply to Stephen Smalley
Quoted 3 lines

Also, can you comment on why you chose not to pursue Ulrich's suggested approach? Just lack of time or was there a technical reason?

There is allocation of multiple strings in the init function. From that point of view, I thought the safest thing is to just use pthread_once. I also wasn't sure about putting atomic compare and exchange code into libselinux.

-Steve

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

author
#6 lore raw
In reply to Stephen Smalley
Quoted 4 lines

Looking back at Roland's advice, I notice that his sample code introduced an explicit bool flag rather than overloading the pthread_once_t variable.

It doesn't look like overloading to me. The man page says:

pthread_once_t once_control = PTHREAD_ONCE_INIT;

The Single Unix Specification says its a constant:

The constant PTHREAD_ONCE_INIT is defined by the header <pthread.h>.

The header file says:

#define PTHREAD_ONCE_INIT 0

So, it is a constant integer. I would imagine there is breakage for commercial binary apps if that ever changed.

-Steve

____________________________________________________________________________________
We won't tell. Get more on shows you hate to love (and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265

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

#7 lore raw
Quoted 7 lines · Steve G
Quoted 3 lines

Looking back at Roland's advice, I notice that his sample code introduced an explicit bool flag rather than overloading the pthread_once_t variable.

It doesn't look like overloading to me. The man page says:

pthread_once_t once_control = PTHREAD_ONCE_INIT;

...which could be a struct.

Quoted 4 lines

The Single Unix Specification says its a constant:

The constant PTHREAD_ONCE_INIT is defined by the header <pthread.h>.

Constant-valued struct would meet that spec.

Quoted 6 lines

The header file says:

#define PTHREAD_ONCE_INIT 0

So, it is a constant integer. I would imagine there is breakage for commercial binary apps if that ever changed.

Looking around with google, I found:
http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/1time_initializations.htm
which says that it is a structure in many systems.

So possibly it is always an integer in Linux, but the spec and the API don't guarantee that, and I did notice that Roland avoided that assumption in his code snippet. I can't see why we would make that assumption.

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.
author
#8 lore raw
Quoted 4 lines

So possibly it is always an integer in Linux, but the spec and the API don't guarantee that, and I did notice that Roland avoided that assumption in his code snip

Nor does it guarantee stdbool.h is on every platform. I also wanted to avoid the overhead of making it into a function rather than a macro. I can code it up like Roland's code if that's what you want.

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