All of lore.kernel.org
 help / color / mirror / Atom feed
* [patch] reducing size of libselnux/libsepol for embedded
@ 2007-04-09  4:50 Yuichi Nakamura
  2007-04-09 12:34 ` Joshua Brindle
  0 siblings, 1 reply; 12+ messages in thread
From: Yuichi Nakamura @ 2007-04-09  4:50 UTC (permalink / raw)
  To: selinux; +Cc: ynakam, sds, busybox

[-- Attachment #1: Type: text/plain, Size: 2351 bytes --]

Hello.

As we discussed before, 
we would like to propose patch that reduces size of libselinux/libsepol.

1. Background
Current libselinux+libsepol size is big for embedded devices.
libselinux.so.1: 115420 byte
libsepol.so.1:302085
Total: 417505 byte
It uses more than 400kbyte.

Needs for embedded device is various, because hardware resource is various.
If device is rich enough, people may want full-featured SELinux.
If device is not rich, people want to reduce binary size by removing 
some features. They may not want modular policy, even may not want boolean.

We thought size of libselinux+libsepol can be reduced by removing functions .

2. About patch
We prepare 2 build options in libselinux/libsepol.
1) make EMBEDDED=1
This is for people who want to use only monolitic policy with boolean support.
* libselinux
  Files related to userland avc is not compiled
* libsepol
  libselinux uses sepol functions(such as sepol_genusers, sepol_genboolean). 
  If people do not need modular policy, we need only to compile such functions.

2) make DISABLE_SEPOL=1
This can be combined with "EMBEDDED=1".
This is for people who uses poor resource devices.
libsepol functions are removed from libselinux 
so libsepol can be removed.
By that, size of libse*  can be reduced more, 
but some features(such as boolean) can not be used.

More detail:
* libselinux 
  - selinux_mk_loadpolicy is replaced with limited version:
     - policy filename is assumed as /etc/selinux/<policy type>/policy/policy.<version in selinuxfs>
     - preserve boolean is not supported
         -- so it is not recommended to use boolean for such system
     - system.users, local.users are not supported
   - -lsepol is removed
* libsepol
  - It is not required by libselinux, so you do not have to compile this.

3. Size measurement
Compiled by gcc(x86)

* Before:
libselinux.so.1: 115420
libsepol.so.1:302085
Total: 417505

* make EMBEDDED=1
libselinux:87024
libsepol:201301
Total: 288325

* make EMBEDDED=1 DISABLE_BOOLEAN=1
libselinux: 77187
libsepol: 0 
Total: 77187 -> Diet about 80%!

And, this work is intended to be applied to BusyBox.
We will fix load_policy applet for BusyBox to support boolean option.

Regards,
-- 
Yuichi Nakamura
Hitachi Software Engineering Co., Ltd.
Japan SELinux Users Group(JSELUG)
SELinux Policy Editor: http://seedit.sourceforge.net/


[-- Attachment #2: libse_diet.v1.patch --]
[-- Type: application/octet-stream, Size: 4349 bytes --]

Index: libsepol/src/Makefile
===================================================================
--- libsepol/src/Makefile	(revision 2318)
+++ libsepol/src/Makefile	(working copy)
@@ -8,11 +8,18 @@
 LIBA=libsepol.a 
 TARGET=libsepol.so
 LIBSO=$(TARGET).$(LIBVERSION)
-OBJS= $(patsubst %.c,%.o,$(wildcard *.c))
-LOBJS= $(patsubst %.c,%.lo,$(wildcard *.c))
+
+SRCS=$(wildcard *.c)
+ifeq ($(EMBEDDED),1)
+UNUSED_SRCS=link.c nodes.c roles.c iface_record.c module.c port_record.c user_record.c interfaces.c  node_record.c ports.c users.c
+SRCS= $(filter-out $(UNUSED_SRCS), $(wildcard *.c))
+endif
+OBJS= $(patsubst %.c,%.o,$(SRCS))
+LOBJS= $(patsubst %.c,%.lo,$(SRCS))
 CFLAGS ?= -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute
 override CFLAGS += -I. -I../include -D_GNU_SOURCE
 
+
 all: $(LIBA) $(LIBSO)
 
 $(LIBA):  $(OBJS)
Index: libselinux/src/Makefile
===================================================================
--- libselinux/src/Makefile	(revision 2318)
+++ libselinux/src/Makefile	(working copy)
@@ -18,10 +18,27 @@
 SWIGSO=_selinux.so
 SWIGFILES=$(SWIGSO) selinux.py 
 LIBSO=$(TARGET).$(LIBVERSION)
-OBJS= $(patsubst %.c,%.o,$(filter-out $(SWIGCOUT),$(wildcard *.c))) 
-LOBJS= $(patsubst %.c,%.lo,$(filter-out $(SWIGCOUT),$(wildcard *.c)))
+
+LSEPOL=-lsepol
+SRCS=$(filter-out $(SWIGCOUT),$(wildcard *.c))
+ifeq ($(EMBEDDED),1)
+UNUSED_SRCS=avc.c avc_internal.c avc_sidtab.c
+SRCS= $(filter-out $(UNUSED_SRCS), $(filter-out $(SWIGCOUT),$(wildcard *.c)))
+endif
+ifeq ($(DISABLE_SEPOL),1)
+UNUSED_SRCS+=booleans.c
+LSEPOL=
+SRCS= $(filter-out $(UNUSED_SRCS), $(filter-out $(SWIGCOUT),$(wildcard *.c)))
+endif
+
+OBJS= $(patsubst %.c,%.o,$(SRCS))
+LOBJS= $(patsubst %.c,%.lo,$(SRCS))
 CFLAGS ?= -Wall -W -Wundef -Wmissing-noreturn -Wmissing-format-attribute
 override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
+ifeq ($(DISABLE_SEPOL),1)
+override CFLAGS += -DDISABLE_SEPOL
+endif
+
 RANLIB=ranlib
 
 ARCH := $(patsubst i%86,i386,$(shell uname -m))
@@ -48,7 +65,7 @@
 	$(CC) $(LDFLAGS) -shared -o $@ $< -L. -lselinux -L$(LIBDIR) -Wl,-soname,$@
 
 $(LIBSO): $(LOBJS)
-	$(CC) $(LDFLAGS) -shared -o $@ $^ -ldl -lsepol -L$(LIBDIR) -Wl,-soname,$(LIBSO),-z,defs,-z,relro
+	$(CC) $(LDFLAGS) -shared -o $@ $^ -ldl $(LSEPOL) -L$(LIBDIR) -Wl,-soname,$(LIBSO),-z,defs,-z,relro
 	ln -sf $@ $(TARGET) 
 
 %.o:  %.c policy.h
Index: libselinux/src/load_policy.c
===================================================================
--- libselinux/src/load_policy.c	(revision 2318)
+++ libselinux/src/load_policy.c	(working copy)
@@ -41,7 +41,56 @@
 
 int load_setlocaldefs hidden = 1;
 
-int selinux_mkload_policy(int preservebools)
+/* 
+  This function is used only if DISABLE_SEPOL is defined.
+  Size of libsepol is big, so you may want to disable libsepol for embedded devices.
+  This function is selinux_mkload_policy with limitations.
+  Limitations:
+  - Binary policy file name is assumed as "policy.<value in /selinux/policyvers>".
+  - Preserve boolean is not supported, so it is recommended not to use boolean,
+    if you want to disable sepol.
+  - system.users and local.users are not supported.
+*/
+static int selinux_mkload_policy_nosepol(int preservebools) {
+	int rc = -1;
+	char path[PATH_MAX];
+	size_t size;
+	void *data;
+	int fd;	
+	struct stat sb;
+
+	if (preservebools) {
+		return -1;
+	}
+
+	snprintf(path, sizeof(path), "%s", selinux_binary_policy_path());
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -1;
+	
+	if (fstat(fd, &sb) < 0)
+		goto close;
+
+	size = sb.st_size;
+	data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+	if (data == MAP_FAILED)
+		goto close;
+
+	rc = security_load_policy(data, size);
+
+ close:
+	close(fd);
+	return rc;
+	
+}
+
+#ifndef DISABLE_SEPOL
+/*
+  selinux_mkload_policy with full features.
+  This is used usually(when DISABLE_SEPOL is not defined).
+*/
+static int selinux_mkload_policy_sepol(int preservebools)
 {
 	int vers = sepol_policy_kern_vers_max();
 	int kernvers = security_policyvers();
@@ -154,7 +203,16 @@
 	close(fd);
 	return rc;
 }
+#endif /*ifndef DISABLE_SEPOL*/
 
+int selinux_mkload_policy(int preservebools) {
+#ifdef DISABLE_SEPOL
+	return selinux_mkload_policy_nosepol(preservebools);
+#else
+	return selinux_mkload_policy_sepol(preservebools);
+#endif
+}
+
 hidden_def(selinux_mkload_policy)
 
 /*

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-04-11 15:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-09  4:50 [patch] reducing size of libselnux/libsepol for embedded Yuichi Nakamura
2007-04-09 12:34 ` Joshua Brindle
2007-04-09 13:27   ` Stephen Smalley
2007-04-09 14:44   ` Stephen Smalley
2007-04-10  1:09     ` Yuichi Nakamura
2007-04-10 15:27       ` Joshua Brindle
2007-04-10 16:50         ` Stephen Smalley
2007-04-10 19:31           ` Stephen Smalley
2007-04-10 19:47             ` Karl MacMillan
2007-04-11  0:22               ` Joshua Brindle
2007-04-11 15:29                 ` Yuichi Nakamura
2007-04-10  1:05   ` Yuichi Nakamura

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.