Skip to contentSkip to conversation

Android Fork

PATCH Applied [email protected] 4 participants 9 messages Sep 26, 2016 – Sep 27, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

Below, are the last two majore patches to close the Android fork.

Patch "libselinux: add ifdef'ing for ANDROID and BUILD_HOST" I combined into 1 patch since some ANDROID and BUILD_HOST defines are on the same line, I can split it appart if its really needed.

Note, that you need the Android make recipe for some of these configurations to work as Android doesn't use all the src files.

The Build files are left out, for now, so AOSP can pull from upstream without it breaking the AOSP build.

Once merged AOSP can take the build files, once they drop external/libselinux, and then the last patch u[stream will be the enabling patch with the build files.

[PATCH 1/2] libselinux: introduce configurable backends [PATCH 2/2] libselinux: add ifdef'ing for ANDROID and BUILD_HOST

#1 lore raw

PATCH 1/2 libselinux: introduce configurable backends

From: William Roberts <[email protected]>

On Android for both the host build, and the target, certain backends are not needed:
- X Backend
- DB Backend
- Media Backend

Introduce the following defines for removing them from the built library:

- NO_X_BACKEND
- NO_DB_BACKEND
- NO_MEDIA_BACKEND

When configured with these options and an attempt
is made to use them, selabel_open() will return
ENOTSUP.

---
 libselinux/src/label.c | 29 ++++++++++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)
libselinux/src/label.c +26 -3
index 963bfcb..96a4ff1 100644
@@ -17,15 +17,33 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
+#ifdef NO_MEDIA_BACKEND
+#define CONFIG_MEDIA_BACKEND(fnptr) NULL
+#else
+#define CONFIG_MEDIA_BACKEND(fnptr) &fnptr
+#endif
+
+#ifdef NO_X_BACKEND
+#define CONFIG_X_BACKEND(fnptr) NULL
+#else
+#define CONFIG_X_BACKEND(fnptr) &fnptr
+#endif
+
+#ifdef NO_DB_BACKEND
+#define CONFIG_DB_BACKEND(fnptr) NULL
+#else
+#define CONFIG_DB_BACKEND(fnptr) &fnptr
+#endif
+
 typedef int (*selabel_initfunc)(struct selabel_handle *rec,
 				const struct selinux_opt *opts,
 				unsigned nopts);
 
 static selabel_initfunc initfuncs[] = {
 	&selabel_file_init,
-	&selabel_media_init,
-	&selabel_x_init,
-	&selabel_db_init,
+	CONFIG_MEDIA_BACKEND(selabel_media_init),
+	CONFIG_X_BACKEND(selabel_x_init),
+	CONFIG_DB_BACKEND(selabel_db_init),
 	&selabel_property_init,
 };
 
@@ -325,6 +343,11 @@ struct selabel_handle *selabel_open(unsigned int backend,
 		goto out;
 	}
 
+	if (!initfuncs[backend]) {
+		errno = ENOTSUP;
+		goto out;
+	}
+
 	rec = (struct selabel_handle *)malloc(sizeof(*rec));
 	if (!rec)
 		goto out;
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/2 libselinux: add ifdef'ing for ANDROID and BUILD_HOST

In reply to [email protected]

From: William Roberts <[email protected]>

On Android, certain discrepancies arise for unused functionality or for dealing with the differences in Bionic libc. This patch includes all the "ifdef'ing" required and introduces the BUILD_HOST define.

The BUILD_HOST define removes functionality not needed when building libselinux for the Android build host machine.

Note that not all the libselinux src files are used to build the host and target libraries on Android.

Change-Id I7984e7b769c4dfa627d6cf311411fa2c93bb7ef7
---
 libselinux/src/callbacks.c      |   5 ++
 libselinux/src/label_file.c     |   2 +
 libselinux/src/label_internal.h |   5 ++
 libselinux/src/load_policy.c    |   4 ++
 libselinux/src/matchpathcon.c   | 116 ++++++++++++++++++++--------------------
 libselinux/src/procattr.c       |   3 ++
 6 files changed, 78 insertions(+), 57 deletions(-)
libselinux/src/callbacks.c +5 -0
index c3cf98b..c18ccc5 100644
@@ -34,7 +34,12 @@ default_selinux_audit(void *ptr __attribute__((unused)),
 static int
 default_selinux_validate(char **ctx)
 {
+#ifndef BUILD_HOST
 	return security_check_context(*ctx);
+#else
+	(void) ctx;
+	return 0;
+#endif
 }
 
 static int
libselinux/src/label_file.c +2 -0
index 8ff1170..5ba6a22 100644
@@ -543,6 +543,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 			break;
 		}
 
+#if !defined(BUILD_HOST) && !defined(ANDROID)
 	/* Process local and distribution substitution files */
 	if (!path) {
 		rec->dist_subs =
@@ -560,6 +561,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 							    rec->digest);
 	}
 
+#endif
 	rec->spec_file = strdup(path);
 
 	/*
libselinux/src/label_internal.h +5 -0
index 0827ef6..7c55531 100644
@@ -16,6 +16,11 @@
 #include "dso.h"
 #include "sha1.h"
 
+#ifdef ANDROID
+// Android does not have fgets_unlocked()
+#define fgets_unlocked(buf, size, fp) fgets(buf, size, fp)
+#endif
+
 /*
  * Installed backends
  */
libselinux/src/load_policy.c +4 -0
index 4f39fc7..249f82f 100644
@@ -11,8 +11,10 @@
 #include <string.h>
 #include <errno.h>
 #include "selinux_internal.h"
+#ifndef ANDROID
 #include <sepol/sepol.h>
 #include <sepol/policydb.h>
+#endif
 #include <dlfcn.h>
 #include "policy.h"
 #include <limits.h>
@@ -45,6 +47,7 @@ int security_load_policy(void *data, size_t len)
 
 hidden_def(security_load_policy)
 
+#ifndef ANDROID
 int load_setlocaldefs hidden = 1;
 
 #undef max
@@ -465,3 +468,4 @@ int selinux_init_load_policy(int *enforce)
 	 */
 	return -1;
 }
+#endif
libselinux/src/matchpathcon.c +59 -57

137 lines not on this page · raw message

libselinux/src/procattr.c +3 -0
index eee4612..7efcd7e 100644
@@ -22,10 +22,13 @@ static pthread_key_t destructor_key;
 static int destructor_key_initialized = 0;
 static __thread char destructor_initialized;
 
+#ifndef ANDROID
+/* Android declares this in unistd.h and has a definition for it */
 static pid_t gettid(void)
 {
 	return syscall(__NR_gettid);
 }
+#endif
 
 static void procattr_thread_destructor(void __attribute__((unused)) *unused)
 {
Signature · 1 line
1.9.1
#3 lore raw

Re: [PATCH 2/2] libselinux: add ifdef'ing for ANDROID and BUILD_HOST

Quoted 6 lines · On 09/26/2016 01:33 PM, [email protected] wrote:

From: William Roberts <[email protected]>

On Android, certain discrepancies arise for unused functionality or for dealing with the differences in Bionic libc. This patch includes all the "ifdef'ing" required and introduces the BUILD_HOST define.

Quoted 8 lines

The BUILD_HOST define removes functionality not needed when building libselinux for the Android build host machine.

Note that not all the libselinux src files are used to build the host and target libraries on Android.

Change-Id I7984e7b769c4dfa627d6cf311411fa2c93bb7ef7

Thanks, applied both.

Quoted 254 lines
---
 libselinux/src/callbacks.c      |   5 ++
 libselinux/src/label_file.c     |   2 +
 libselinux/src/label_internal.h |   5 ++
 libselinux/src/load_policy.c    |   4 ++
 libselinux/src/matchpathcon.c   | 116 ++++++++++++++++++++--------------------
 libselinux/src/procattr.c       |   3 ++
 6 files changed, 78 insertions(+), 57 deletions(-)
libselinux/src/callbacks.c +5 -0
index c3cf98b..c18ccc5 100644
@@ -34,7 +34,12 @@ default_selinux_audit(void *ptr __attribute__((unused)),
 static int
 default_selinux_validate(char **ctx)
 {
+#ifndef BUILD_HOST
 	return security_check_context(*ctx);
+#else
+	(void) ctx;
+	return 0;
+#endif
 }
 
 static int
libselinux/src/label_file.c +2 -0
index 8ff1170..5ba6a22 100644
@@ -543,6 +543,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 			break;
 		}
 
+#if !defined(BUILD_HOST) && !defined(ANDROID)
 	/* Process local and distribution substitution files */
 	if (!path) {
 		rec->dist_subs =
@@ -560,6 +561,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,
 							    rec->digest);
 	}
 
+#endif
 	rec->spec_file = strdup(path);
 
 	/*
libselinux/src/label_internal.h +5 -0
index 0827ef6..7c55531 100644
@@ -16,6 +16,11 @@
 #include "dso.h"
 #include "sha1.h"
 
+#ifdef ANDROID
+// Android does not have fgets_unlocked()
+#define fgets_unlocked(buf, size, fp) fgets(buf, size, fp)
+#endif
+
 /*
  * Installed backends
  */
libselinux/src/load_policy.c +4 -0
index 4f39fc7..249f82f 100644
@@ -11,8 +11,10 @@
 #include <string.h>
 #include <errno.h>
 #include "selinux_internal.h"
+#ifndef ANDROID
 #include <sepol/sepol.h>
 #include <sepol/policydb.h>
+#endif
 #include <dlfcn.h>
 #include "policy.h"
 #include <limits.h>
@@ -45,6 +47,7 @@ int security_load_policy(void *data, size_t len)
 
 hidden_def(security_load_policy)
 
+#ifndef ANDROID
 int load_setlocaldefs hidden = 1;
 
 #undef max
@@ -465,3 +468,4 @@ int selinux_init_load_policy(int *enforce)
 	 */
 	return -1;
 }
+#endif
libselinux/src/matchpathcon.c +59 -57
index 4764ab7..724eb65 100644
@@ -7,6 +7,64 @@
 #include "callbacks.h"
 #include <limits.h>
 
+static int (*myinvalidcon) (const char *p, unsigned l, char *c) = NULL;
+static int (*mycanoncon) (const char *p, unsigned l, char **c) =  NULL;
+
+static void
+#ifdef __GNUC__
+    __attribute__ ((format(printf, 1, 2)))
+#endif
+    default_printf(const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	vfprintf(stderr, fmt, ap);
+	va_end(ap);
+}
+
+void
+#ifdef __GNUC__
+    __attribute__ ((format(printf, 1, 2)))
+#endif
+    (*myprintf) (const char *fmt,...) = &default_printf;
+int myprintf_compat = 0;
+
+void set_matchpathcon_printf(void (*f) (const char *fmt, ...))
+{
+	myprintf = f ? f : &default_printf;
+	myprintf_compat = 1;
+}
+
+int compat_validate(struct selabel_handle *rec,
+		    struct selabel_lookup_rec *contexts,
+		    const char *path, unsigned lineno)
+{
+	int rc;
+	char **ctx = &contexts->ctx_raw;
+
+	if (myinvalidcon)
+		rc = myinvalidcon(path, lineno, *ctx);
+	else if (mycanoncon)
+		rc = mycanoncon(path, lineno, ctx);
+	else {
+		rc = selabel_validate(rec, contexts);
+		if (rc < 0) {
+			if (lineno) {
+				COMPAT_LOG(SELINUX_WARNING,
+					    "%s: line %u has invalid context %s\n",
+						path, lineno, *ctx);
+			} else {
+				COMPAT_LOG(SELINUX_WARNING,
+					    "%s: has invalid context %s\n", path, *ctx);
+			}
+		}
+	}
+
+	return rc ? -1 : 0;
+}
+
+#ifndef BUILD_HOST
+
 static __thread struct selabel_handle *hnd;
 
 /*
@@ -54,33 +112,6 @@ static void free_array_elts(void)
 	con_array = NULL;
 }
 
-static void
-#ifdef __GNUC__
-    __attribute__ ((format(printf, 1, 2)))
-#endif
-    default_printf(const char *fmt, ...)
-{
-	va_list ap;
-	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
-	va_end(ap);
-}
-
-void
-#ifdef __GNUC__
-    __attribute__ ((format(printf, 1, 2)))
-#endif
-    (*myprintf) (const char *fmt,...) = &default_printf;
-int myprintf_compat = 0;
-
-void set_matchpathcon_printf(void (*f) (const char *fmt, ...))
-{
-	myprintf = f ? f : &default_printf;
-	myprintf_compat = 1;
-}
-
-static int (*myinvalidcon) (const char *p, unsigned l, char *c) = NULL;
-
 void set_matchpathcon_invalidcon(int (*f) (const char *p, unsigned l, char *c))
 {
 	myinvalidcon = f;
@@ -104,9 +135,6 @@ static int default_canoncon(const char *path, unsigned lineno, char **context)
 	return 0;
 }
 
-static int (*mycanoncon) (const char *p, unsigned l, char **c) =
-    NULL;
-
 void set_matchpathcon_canoncon(int (*f) (const char *p, unsigned l, char **c))
 {
 	if (f)
@@ -536,30 +564,4 @@ int selinux_lsetfilecon_default(const char *path)
 	return rc;
 }
 
-int compat_validate(struct selabel_handle *rec,
-		    struct selabel_lookup_rec *contexts,
-		    const char *path, unsigned lineno)
-{
-	int rc;
-	char **ctx = &contexts->ctx_raw;
-
-	if (myinvalidcon)
-		rc = myinvalidcon(path, lineno, *ctx);
-	else if (mycanoncon)
-		rc = mycanoncon(path, lineno, ctx);
-	else {
-		rc = selabel_validate(rec, contexts);
-		if (rc < 0) {
-			if (lineno) {
-				COMPAT_LOG(SELINUX_WARNING,
-					    "%s: line %u has invalid context %s\n",
-						path, lineno, *ctx);
-			} else {
-				COMPAT_LOG(SELINUX_WARNING,
-					    "%s: has invalid context %s\n", path, *ctx);
-			}
-		}
-	}
-
-	return rc ? -1 : 0;
-}
+#endif
libselinux/src/procattr.c +3 -0
index eee4612..7efcd7e 100644
@@ -22,10 +22,13 @@ static pthread_key_t destructor_key;
 static int destructor_key_initialized = 0;
 static __thread char destructor_initialized;
 
+#ifndef ANDROID
+/* Android declares this in unistd.h and has a definition for it */
 static pid_t gettid(void)
 {
 	return syscall(__NR_gettid);
 }
+#endif
 
 static void procattr_thread_destructor(void __attribute__((unused)) *unused)
 {
#4 lore raw
In reply to [email protected]
Quoted 12 lines · On Mon, Sep 26, 2016 at 10:33 AM, <[email protected]> wrote:

Below, are the last two majore patches to close the Android fork.

Patch "libselinux: add ifdef'ing for ANDROID and BUILD_HOST" I combined into 1 patch since some ANDROID and BUILD_HOST defines are on the same line, I can split it appart if its really needed.

Note, that you need the Android make recipe for some of these configurations to work as Android doesn't use all the src files.

The Build files are left out, for now, so AOSP can pull from upstream without it breaking the AOSP build.

Quoted 6 lines

Once merged AOSP can take the build files, once they drop external/libselinux, and then the last patch u[stream will be the enabling patch with the build files.

[PATCH 1/2] libselinux: introduce configurable backends [PATCH 2/2] libselinux: add ifdef'ing for ANDROID and BUILD_HOST

FYI: AOSP changes:
https://android-review.googlesource.com/#/q/topic:fork-kill+(status:open+OR+status:merged)

#5 lore raw
In reply to [email protected]
Quoted 9 lines · On 09/26/2016 01:33 PM, [email protected] wrote:

Below, are the last two majore patches to close the Android fork.

Patch "libselinux: add ifdef'ing for ANDROID and BUILD_HOST" I combined into 1 patch since some ANDROID and BUILD_HOST defines are on the same line, I can split it appart if its really needed.

Note, that you need the Android make recipe for some of these configurations to work as Android doesn't use all the src files.

Quoted 6 lines

The Build files are left out, for now, so AOSP can pull from upstream without it breaking the AOSP build.

Once merged AOSP can take the build files, once they drop external/libselinux, and then the last patch u[stream will be the enabling patch with the build files.

I was wondering whether the build files belong in upstream. I know we have them for libsepol and checkpolicy currently, but essentially they are just always out of date, any changes will be fed through Android first anyway, and they aren't getting any testing or updates from upstream. Might be simpler to just keep those in Android and drop the ones we already have from upstream.

Quoted 9 lines

[PATCH 1/2] libselinux: introduce configurable backends
[PATCH 2/2] libselinux: add ifdef'ing for ANDROID and BUILD_HOST
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

#6 lore raw
Quoted 17 lines · Stephen Smalley
Quoted 9 lines · On 09/26/2016 01:33 PM, [email protected] wrote:

Below, are the last two majore patches to close the Android fork.

Patch "libselinux: add ifdef'ing for ANDROID and BUILD_HOST" I combined into 1 patch since some ANDROID and BUILD_HOST defines are on the same line, I can split it appart if its really needed.

Note, that you need the Android make recipe for some of these configurations to work as Android doesn't use all the src files.

Quoted 6 lines

The Build files are left out, for now, so AOSP can pull from upstream without it breaking the AOSP build.

Once merged AOSP can take the build files, once they drop external/libselinux, and then the last patch u[stream will be the enabling patch with the build files.

Quoted 6 lines

I was wondering whether the build files belong in upstream. I know we have them for libsepol and checkpolicy currently, but essentially they are just always out of date, any changes will be fed through Android first anyway, and they aren't getting any testing or updates from upstream. Might be simpler to just keep those in Android and drop the ones we already have from upstream.

+ AOSP folks.

I've been pondering the same thing, so I am glad one of us started this discussion. Based on what your stating, I think it makes sense to kill the Android.mk and Android.bp files upstream, but ill let the AOSP folks chime in as well.

I think it also makes sense to add ANDROID_HOST=y option for the build so we can at least test the Android host recipe within the upstream Makefiles, thoughts?

<snip>

#7 lore raw
In reply to [email protected]

I just remembered that travis-ci has OSX stuff now.
https://docs.travis-ci.com/user/osx-ci-environment/

Maybe we should setup a .travis.yml for selinux to build all these possible configurations going forward?

Quoted 23 lines · On Mon, Sep 26, 2016 at 10:33:37AM -0700, [email protected] wrote:

Below, are the last two majore patches to close the Android fork.

Patch "libselinux: add ifdef'ing for ANDROID and BUILD_HOST" I combined into 1 patch since some ANDROID and BUILD_HOST defines are on the same line, I can split it appart if its really needed.

Note, that you need the Android make recipe for some of these configurations to work as Android doesn't use all the src files.

The Build files are left out, for now, so AOSP can pull from upstream without it breaking the AOSP build.

Once merged AOSP can take the build files, once they drop external/libselinux, and then the last patch u[stream will be the enabling patch with the build files.

[PATCH 1/2] libselinux: introduce configurable backends
[PATCH 2/2] libselinux: add ifdef'ing for ANDROID and BUILD_HOST
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

#8 lore raw
Quoted 6 lines · Jason Zaman

I just remembered that travis-ci has OSX stuff now.
https://docs.travis-ci.com/user/osx-ci-environment/

Maybe we should setup a .travis.yml for selinux to build all these possible configurations going forward?

At least the Android config would be nice, because certain things will never build on mac.

Quoted 3 lines

On Mon, Sep 26, 2016 at 10:33:37AM -0700, [email protected]

Quoted 16 lines · wrote:
Quoted 16 lines

Below, are the last two majore patches to close the Android fork.

Patch "libselinux: add ifdef'ing for ANDROID and BUILD_HOST" I combined into 1 patch since some ANDROID and BUILD_HOST defines are on the same line, I can split it appart if its really needed.

Note, that you need the Android make recipe for some of these configurations to work as Android doesn't use all the src files.

The Build files are left out, for now, so AOSP can pull from upstream without it breaking the AOSP build.

Once merged AOSP can take the build files, once they drop external/libselinux, and then the last patch u[stream will be the enabling patch with the build files.

Quoted 7 lines
Quoted 7 lines

[PATCH 1/2] libselinux: introduce configurable backends [PATCH 2/2] libselinux: add ifdef'ing for ANDROID and BUILD_HOST
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

Quoted 5 lines

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].