Skip to contentSkip to conversation

Replace semanage debugging system

SEMANAGE Ivan Gyurdiev 2 participants 19 messages Oct 11, 2005 SELinux security module

lore
Share & tools
lore
#0 lore raw

The attached patch replaces the current semanage debugging system, with one based on callbacks. It breaks API, so the libsemanage.map file needs to be updated, but I'm not sure how to do that properly, so I left that part out...

Functions removed:
semanage_strerror

Functions that need to be exposed:
Everything in include/semanage/debug.h

Changes since last time we discussed this:
- added argument level (which is set in macros WARN, ERR, and INFO)
- added argument channel (auto-set by the macros to "libsemanage") - this will be helpful to separate out sepol messages
- replaced use of __FUNCTION__ with __func__, which should not be GNU specific
- function name argument is now auto-set by macros, so we don't have to worry about it.
- replaced existing usage of semanage_write_error with those macros

Unrelated changes in this patch:
- Renames policy_connection.h to policy.h
- moves conn into policy.h, which is a good place for it, I think.
- some #if0'ed code paths.

libsemanage/include/semanage/debug.h +62 -0
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/include/semanage/debug.h exp/libsemanage/include/semanage/debug.h
@@ -0,0 +1,62 @@
+/* Author: Joshua Brindle <[email protected]>
+ *         Jason Tang     <[email protected]>
+ *         Ivan Gyurdiev  <[email protected]>
+ *
+ * Copyright (C) 2005 Tresys Technology, LLC
+ * Copyright (C) 2005 Red Hat Inc.
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _SEMANAGE_DEBUG_H_
+#define _SEMANAGE_DEBUG_H_
+
+#include <semanage/handle.h>
+
+struct semanage_message;
+typedef struct semanage_message semanage_message_t;
+
+#define SEMANAGE_MSG_ERR  1
+#define SEMANAGE_MSG_WARN 2
+#define SEMANAGE_MSG_INFO 3
+
+extern const char* semanage_msg_get_message(
+	semanage_message_t* msg);
+
+extern int semanage_msg_get_level(
+	semanage_message_t* msg);
+
+extern const char* semanage_msg_get_channel(
+	semanage_message_t* msg);
+
+extern const char* semanage_msg_get_fname(
+	semanage_message_t* msg);
+
+extern void semanage_msg_free(
+	semanage_message_t* msg);
+
+/* Set the messaging callback. 
+ * By the default, the callback will print
+ * the message on standard output, in a 
+ * particular format. Passing NULL here
+ * indicates that messaging should be suppressed */
+extern void semanage_msg_set_callback(
+	semanage_handle_t* handle,
+	void (*callback) (
+		void* varg,
+		semanage_message_t* msg),
+	void* callback_arg);
+
+#endif 
libsemanage/include/semanage/handle.h +0 -22
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/include/semanage/handle.h exp/libsemanage/include/semanage/handle.h
@@ -51,14 +51,6 @@
  * error. */
 int semanage_disconnect(semanage_handle_t *);
 
-/* Return a string describing the most recently encountered error
- * associated with a semanage_handle_t.	 The returned string must not
- * be modified by the caller.  Be aware that this string is not
- * persistent; future calls to this library may alter the buffer
- * contents, so make a copy of it if necessary.
- */
-const char *semanage_strerror(semanage_handle_t *);
-
 /* Attempt to obtain a transaction lock on the manager.	 If another
  * process has the lock then this function may block, depending upon
  * the timeout value in the handle.
@@ -75,18 +67,4 @@
  */
 int semanage_commit(semanage_handle_t *);
 
-/* META NOTES
- *
- * All of the other interfaces expect a semanage_handle as its first
- * parameter.  If an error occurs then the function returns a negative
- * value.  Call semanage_strerror() to retrieve a string that fully
- * describes the error.
- *
- * For all functions a non-negative number indicates success. For query 
- * fucntions a >=0 returned value is the "policy sequence number".  This
- * number keeps tracks of policy revisions and is used to detect if
- * one semanage client has committed policy changes while another is
- * still connected.
- */
-
 #endif
libsemanage/include/semanage/semanage.h +1 -0
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/include/semanage/semanage.h exp/libsemanage/include/semanage/semanage.h
@@ -23,6 +23,7 @@
 
 #include <semanage/handle.h>
 #include <semanage/modules.h>
+#include <semanage/debug.h>
 
 /*
  * Explicit libsemanage initialization.
libsemanage/src/database_direct.c +1 -0
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/database_direct.c exp/libsemanage/src/database_direct.c
@@ -3,6 +3,7 @@
 #define DBASE_DEFINED
 
 #include <stdlib.h>
+#include <string.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <sys/types.h>
libsemanage/src/debug.c +97 -6

124 lines not on this page · raw message

libsemanage/src/debug.h +27 -5
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/debug.h exp/libsemanage/src/debug.h
@@ -20,20 +20,42 @@
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-#ifndef _SEPOL_INTERNAL_DEBUG_H_
-#define _SEPOL_INTERNAL_DEBUG_H_
+#ifndef _SEMANAGE_INTERNAL_DEBUG_H_
+#define _SEMANAGE_INTERNAL_DEBUG_H_
 
 #include "handle.h"
+#include <stdio.h>
+#include <semanage/debug.h>
 
 #define STATUS_SUCCESS 0
 #define STATUS_ERR -1
 #define STATUS_NODATA 1
 
-/* FIXME: redesign with level argument ? */
+#define ERR(handle, ...) \
+	msg_write(handle, SEMANAGE_MSG_ERR, "libsemanage", \
+	__func__, __VA_ARGS__)
+
+#define INFO(handle, fmt, ...) \
+	msg_write(handle, SEMANAGE_MSG_INFO, "libsemanage", \
+	__func__, __VA_ARGS__)
+
+#define WARN(handle, fmt, ...) \
+	msg_write(handle, SEMANAGE_MSG_WARN, "libsemanage", \
+	__func__, __VA_ARGS__)
 
 #ifdef __GNUC__
-__attribute__ ((format (printf, 2, 3)))
+__attribute__ ((format (printf, 5, 6)))
 #endif
-extern void semanage_write_error(semanage_handle_t *sh, char *fmt, ...);
+extern void msg_write(
+	semanage_handle_t* handle,
+	int level,
+	const char* channel,
+	const char* fname,
+	char* fmt,
+	...);
+
+extern void msg_default_handler(
+	void* varg,
+	semanage_message_t* msg);
 
 #endif 
libsemanage/src/direct_api.c +37 -25

186 lines not on this page · raw message

libsemanage/src/handle.c +9 -12
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/handle.c exp/libsemanage/src/handle.c
@@ -41,11 +41,11 @@
 #define SEMANAGE_COMMIT_READ_WAIT 5
 
 semanage_handle_t *semanage_handle_create(void) {
-	semanage_handle_t *sh;
-	const char *conf_name;
+	semanage_handle_t *sh = NULL;
+	const char *conf_name = NULL;
 
 	/* Allocate handle */
-	if ((sh = calloc(1, sizeof(*sh))) == NULL)
+	if ((sh = calloc(1, sizeof(semanage_handle_t))) == NULL)
 		goto omem;
 
 	/* Policy root */
@@ -66,6 +66,10 @@
 	/* Set timeout: some default value for now, later use config */
 	sh->timeout = SEMANAGE_COMMIT_READ_WAIT; 
 
+	/* Set callback */
+	sh->callback = msg_default_handler;
+	sh->callback_arg = NULL;
+
 	/* Configure object databases 
 	 * Hardcore DATA FILE backend for now */
 	if (user_file_dbase_init(&sh->dbase[DBASE_USERS]) < 0)
@@ -130,19 +134,12 @@
 	free(sh);
 }
 
-const char *semanage_strerror(semanage_handle_t *sh) {
-	if (sh == NULL) {
-		return "Could not parse semange.conf or out of memory.";
-	}
-	return sh->err_buf;
-}
-
 /********************* public transaction functions *********************/
 
 int semanage_begin_transaction(semanage_handle_t *sh) {
 	assert(sh != NULL && sh->funcs != NULL && sh->funcs->begin_trans != NULL);
 	if (!sh->is_connected) {
-		semanage_write_error(sh, "Not connected.");
+		ERR(sh, "Not connected.");
 		return -1;
 	}
 	if (sh->is_in_transaction) {
@@ -160,7 +157,7 @@
 	int retval;
 	assert(sh != NULL && sh->funcs != NULL && sh->funcs->commit != NULL);
 	if (!sh->is_in_transaction) {
-		semanage_write_error(sh, "Will not commit because caller does not have a tranaction lock yet.");
+		ERR(sh, "Will not commit because caller does not have a tranaction lock yet.");
 		return -1;
 	}
 	retval = sh->funcs->commit(sh);
libsemanage/src/handle.h +7 -5
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/handle.h exp/libsemanage/src/handle.h
@@ -25,19 +25,21 @@
 
 #include <stddef.h>
 #include <semanage/handle.h>
+#include <semanage/debug.h>
 #include "modules.h"
 #include "semanage_conf.h"
-#include "policy_connection.h"
+#include "policy.h"
 #include "database.h"
 
 struct semanage_handle {
 	int con_id;             /* Connection ID */
 	int policy_serial;      /* Policy serial number at connect time */
 
-	/* Error management */
-	/* FIXME: re-design error system using callbacks (?) */
-#define SEMANAGE_ERRBUFSZ 1024
-	char err_buf[SEMANAGE_ERRBUFSZ];
+	/* Error callback */
+	void (*callback) (
+		void* varg,
+		semanage_message_t* msg);
+	void* callback_arg;
 
 	/* one of these connections will actually be used while
 	 * working with the module store -- the particular one if
libsemanage/src/modules.c +10 -10
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/modules.c exp/libsemanage/src/modules.c
@@ -37,11 +37,11 @@
 int semanage_module_install(semanage_handle_t *sh,
 			    char *module_data, size_t data_len) {
 	if (sh->funcs->install == NULL) {
-		semanage_write_error(sh, "No install function defined for this connection type.");
+		ERR(sh, "No install function defined for this connection type.");
 		return -1;
 	}
 	else if (!sh->is_connected) {
-		semanage_write_error(sh, "Not connected.");
+		ERR(sh, "Not connected.");
 		return -1;
 	}
 	else if (!sh->is_in_transaction) {
@@ -55,11 +55,11 @@
 int semanage_module_upgrade(semanage_handle_t *sh,
 			    char *module_data, size_t data_len) {
 	if (sh->funcs->upgrade == NULL) {
-		semanage_write_error(sh, "No upgrade function defined for this connection type.");
+		ERR(sh, "No upgrade function defined for this connection type.");
 		return -1;
 	}
 	else if (!sh->is_connected) {
-		semanage_write_error(sh, "Not connected.");
+		ERR(sh, "Not connected.");
 		return -1;
 	}
 	else if (!sh->is_in_transaction) {
@@ -73,11 +73,11 @@
 int semanage_module_install_base(semanage_handle_t *sh,
 				 char *module_data, size_t data_len) {
 	if (sh->funcs->install_base == NULL) {
-		semanage_write_error(sh, "No install base function defined for this connection type.");
+		ERR(sh, "No install base function defined for this connection type.");
 		return -1;
 	}
 	else if (!sh->is_connected) {
-		semanage_write_error(sh, "Not connected.");
+		ERR(sh, "Not connected.");
 		return -1;
 	}
 	else if (!sh->is_in_transaction) {
@@ -91,11 +91,11 @@
 int semanage_module_remove(semanage_handle_t *sh,
 			   char *module_name) {
 	if (sh->funcs->remove == NULL) {
-		semanage_write_error(sh, "No remove function defined for this connection type.");
+		ERR(sh, "No remove function defined for this connection type.");
 		return -1;
 	}
 	else if (!sh->is_connected) {
-		semanage_write_error(sh, "Not connected.");
+		ERR(sh, "Not connected.");
 		return -1;
 	}
 	else if (!sh->is_in_transaction) {
@@ -109,11 +109,11 @@
 int semanage_module_list(semanage_handle_t *sh,
 			 semanage_module_info_t **modinfo, int *num_modules) {
 	if (sh->funcs->list == NULL) {
-		semanage_write_error(sh, "No list function defined for this connection type.");
+		ERR(sh, "No list function defined for this connection type.");
 		return -1;
 	}
 	else if (!sh->is_connected) {
-		semanage_write_error(sh, "Not connected.");
+		ERR(sh, "Not connected.");
 		return -1;
 	}
 	return sh->funcs->list(sh, modinfo, num_modules);
libsemanage/src/modules.h +0 -5
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/modules.h exp/libsemanage/src/modules.h
@@ -23,11 +23,6 @@
 
 #include <semanage/modules.h>
 
-struct semanage_module_conn {
-	int translock_file_fd;
-	int readlock_file_fd;
-};
-
 struct semanage_module_info {
 	char *name;             /* Key */
 	char *version;
libsemanage/src/policy_connection.h +0 -55

55 lines not on this page · raw message

libsemanage/src/policy.h +77 -0

77 lines not on this page · raw message

libsemanage/src/semanage_store.c +71 -50

386 lines not on this page · raw message

#1 lore raw

Re: [ SEPOL ] Another debugging system

Quoted 1 line · Ivan Gyurdiev

The attached patch replaces the current semanage debugging system, with one based on callbacks. It breaks API, so the libsemanage.map file needs to be updated, but I'm not sure how to do that properly, so I left that part out...

Attached patch introduces the same debug system to sepol, except it does not break compatibility, or replace old usage here yet - this is a bit more difficult to do than in semanage, so I'd rather split it up in several patches. This one introduces a handle, but doesn't allow the handle to be passed in anywhere yet. A global handle is used for handling the compatibility functions. I've exported those interfaces via the map file, and included them in sepol.h.

Bugfix: can you delete the '\n' from the default handler in the semanage patch - it prints an extra newline.

Question: should sepol.h be including all (external api) sepol headers ? It does not do that currently. I added debug.h, but the others are still not included.

include/sepol/debug.h +40 -1
diff -Naur --exclude CVS libsepol/include/sepol/debug.h libsepol.new/include/sepol/debug.h
@@ -1,9 +1,48 @@
 #ifndef _SEPOL_DEBUG_H_
 #define _SEPOL_DEBUG_H_
 
+#include <sepol/handle.h>
+
+/* Deprecated */
 extern void sepol_enable_debug(
 	void (*fn)(const char* fname, const char *fmt, ...));
 
 extern void sepol_disable_debug();
+extern void sepol_debug(int on);
+/* End deprecated */
+
+struct sepol_message;
+typedef struct sepol_message sepol_message_t;
+
+#define SEPOL_MSG_ERR  1
+#define SEPOL_MSG_WARN 2
+#define SEPOL_MSG_INFO 3
+
+extern const char* sepol_msg_get_message(
+	sepol_message_t* msg);
+
+extern int sepol_msg_get_level(
+	sepol_message_t* msg);
+
+extern const char* sepol_msg_get_channel(
+	sepol_message_t* msg);
+
+extern const char* sepol_msg_get_fname(
+	sepol_message_t* msg);
+
+extern void sepol_msg_free(
+	sepol_message_t* msg);
+
+/* Set the messaging callback. 
+ * By the default, the callback will print
+ * the message on standard output, in a 
+ * particular format. Passing NULL here
+ * indicates that messaging should be suppressed */
+extern void sepol_msg_set_callback(
+	sepol_handle_t* handle,
+	void (*callback) (
+		void* varg,
+		sepol_message_t* msg),
+	void* callback_arg);
 
-#endif /* _SEPOL_DEBUG_H_ */
+#endif 
include/sepol/handle.h +13 -0
diff -Naur --exclude CVS libsepol/include/sepol/handle.h libsepol.new/include/sepol/handle.h
@@ -0,0 +1,13 @@
+#ifndef _SEPOL_HANDLE_H_
+#define _SEPOL_HANDLE_H_
+
+struct sepol_handle;
+typedef struct sepol_handle sepol_handle_t;
+
+/* Create and return a sepol handle. */
+sepol_handle_t *sepol_handle_create(void);
+
+/* Destroy a sepol handle. */
+void sepol_handle_destroy(sepol_handle_t *);
+
+#endif
include/sepol/sepol.h +2 -2
diff -Naur --exclude CVS libsepol/include/sepol/sepol.h libsepol.new/include/sepol/sepol.h
@@ -4,6 +4,8 @@
 #include <stddef.h>
 #include <stdio.h>
 
+#include <sepol/debug.h>
+
 /* Given an existing binary policy (starting at 'data', with length 'len')
    and a boolean configuration file named by 'boolpath', rewrite the binary
    policy for the boolean settings in the boolean configuration file.
@@ -37,6 +39,4 @@
 /* Check context validity against currently set binary policy. */
 extern int sepol_check_context(char *context);
 
-/* Turn on or off sepol error messages. */
-extern void sepol_debug(int on);
 #endif
src/debug.c +126 -33

176 lines not on this page · raw message

src/debug.h +40 -5

57 lines not on this page · raw message

src/handle.c +21 -0
diff -Naur --exclude CVS libsepol/src/handle.c libsepol.new/src/handle.c
@@ -0,0 +1,21 @@
+#include <stdlib.h>
+#include "handle.h"
+#include "debug.h"
+
+sepol_handle_t *sepol_handle_create(void) {
+
+	sepol_handle_t *sh = malloc(sizeof(sepol_handle_t));
+	if (sh == NULL)
+		return NULL;
+	
+	/* Set callback */
+	sh->callback = msg_default_handler;
+	sh->callback_arg = NULL;
+
+	return sh;
+}
+
+void sepol_handle_destroy(sepol_handle_t *sh) {
+	free(sh);
+}
+
src/handle.h +17 -0
diff -Naur --exclude CVS libsepol/src/handle.h libsepol.new/src/handle.h
@@ -0,0 +1,17 @@
+#ifndef _SEPOL_INTERNAL_HANDLE_H_
+#define _SEPOL_INTERNAL_HANDLE_H_
+
+#include <sepol/handle.h>
+#include <sepol/debug.h>
+
+struct sepol_handle {
+
+	/* Error callback */
+	void (*callback) (
+		void* varg,
+		sepol_message_t* msg);
+	void* callback_arg;
+
+};
+
+#endif
src/libsepol.map +1 -0
diff -Naur --exclude CVS libsepol/src/libsepol.map libsepol.new/src/libsepol.map
@@ -17,5 +17,6 @@
 	sepol_bool*; sepol_context*; sepol_disable_debug; sepol_enable_debug;
 	sepol_iface*; sepol_port*; sepol_user*; sepol_clear_unused_users;
 	sepol_role_is_valid; sepol_set_delusers;
+	sepol_msg_*; sepol_handle_*;
   local: *;
 };
#2 lore raw

Re: [ SEPOL ] Another debugging system

Quoted 8 lines · Ivan Gyurdiev

Attached patch introduces the same debug system to sepol, except it does not break compatibility, or replace old usage here yet - this is a bit more difficult to do than in semanage, so I'd rather split it up in several patches. This one introduces a handle, but doesn't allow the handle to be passed in anywhere yet. A global handle is used for handling the compatibility functions. I've exported those interfaces via the map file, and included them in sepol.h.

Quoted 6 lines

Bugfix: can you delete the '\n' from the default handler in the semanage patch - it prints an extra newline.

Question: should sepol.h be including all (external api) sepol headers ? It does not do that currently. I added debug.h, but the others are still not included.

- Same comments apply as for the semanage patch.

- Yes, sepol.h should likely include all external API sepol headers, and the current interfaces in sepol.h should likely be moved to separate headers that are then included.

- The natural first target for adding the handles would be those interfaces that already take error buffer arguments, i.e. sepol_link_packages, sepol_link_modules, and sepol_expand_modules.

- I'm not sure it is worthwhile to add the handles to primitive interfaces like sepol_policydb_create, where the only failure case is out of memory. In contrast, sepol_policydb_read/write and sepol_policydb_from_image/to_image are more likely candidates.

- We'll need to preserve versions of the old sepol.h interfaces without handles for backward compatibility, since there have been official releases with those interfaces. Some of those interfaces will be obsoleted (genusers/genbools) but others will continue to be used (genbools_array for load_policy, set_policydb_from_file and check_context for setfiles -c).

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.
#3 lore raw

Re: [ SEPOL ] Another debugging system

Quoted 3 lines

- Same comments apply as for the semanage patch.

Allright, I'll redo both of those patches for tomorrow (no time today). Thanks for the comments.

Quoted 4 lines

- Yes, sepol.h should likely include all external API sepol headers, and the current interfaces in sepol.h should likely be moved to separate headers that are then included.

Will change that in a later patch..

Quoted 7 lines

- We'll need to preserve versions of the old sepol.h interfaces without handles for backward compatibility, since there have been official releases with those interfaces. Some of those interfaces will be obsoleted (genusers/genbools) but others will continue to be used (genbools_array for load_policy, set_policydb_from_file and check_context for setfiles -c).

I'm confused - what's the difference between an "old" interface that has a corresponding "new" interface... and an obsoleted interface. Seems like the same thing to me. Yes, we'll need need to preserve compatibility until all users are fixed, and then we can get rid of the old interfaces..right?

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

Re: [ SEPOL ] Another debugging system

Quoted 5 lines · Ivan Gyurdiev

I'm confused - what's the difference between an "old" interface that has a corresponding "new" interface... and an obsoleted interface. Seems like the same thing to me. Yes, we'll need need to preserve compatibility until all users are fixed, and then we can get rid of the old interfaces..right?

No, we should provide ABI stability, as discussed in section 3 of
http://people.redhat.com/drepper/dsohowto.pdf

Updated users can be limited to the new interfaces, but old binaries should continue to work even with the new libsepol, i.e. the libsepol DSO must continue to provide versions of those interfaces that are consistent with its previously exported ABI.

Since some interfaces will be obsoleted by the transition to regenerating policy on all local customizations, it won't be necessary to provide new versions of those interfaces at all.

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.
#5 lore raw

Re: [ SEPOL ] Another debugging system

Quoted 7 lines · Stephen Smalley

No, we should provide ABI stability, as discussed in section 3 of
http://people.redhat.com/drepper/dsohowto.pdf

Updated users can be limited to the new interfaces, but old binaries should continue to work even with the new libsepol, i.e. the libsepol DSO must continue to provide versions of those interfaces that are consistent with its previously exported ABI.

Example: Suppose that we wanted to introduce an explicit policydb argument to sepol_check_context() rather than requiring use of a global policydb within libsepol for that purpose. We might modify libsepol as in the following patch to achieve this without disturbing existing binaries. Any programs rebuilt against the new libsepol will be forced to be updated to the new interface (public interface definition changed in the header file), but existing binaries built against the old libsepol will continue to fallback to the old interface. Note that this patch isn't complete (doesn't update chkcon example utility to the new interface, or deal with the need to also have a non-global sidtab), but is functional (I built libsepol with this patch, and then ran an existing setfiles binary against it with the -c option to check contexts against a binary policy, and setfiles continued to use the old interface and work correctly).

Signature · 2 lines
Stephen Smalley
National Security Agency

Index: libsepol/include/sepol/sepol.h
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/include/sepol/sepol.h,v retrieving revision 1.8

include/sepol/sepol.h +2 -1
diff -u -p -r1.8 sepol.h
@@ -3,6 +3,7 @@
 
 #include <stddef.h>
 #include <stdio.h>
+#include <sepol/policydb.h>
 
 /* Given an existing binary policy (starting at 'data', with length 'len')
    and a boolean configuration file named by 'boolpath', rewrite the binary
@@ -35,7 +36,7 @@ extern void sepol_set_delusers(int on);
 extern int sepol_set_policydb_from_file(FILE *fp);
 
 /* Check context validity against currently set binary policy. */
-extern int sepol_check_context(char *context);
+extern int sepol_check_context(sepol_policydb_t *p, char *context);
 
 /* Turn on or off sepol error messages. */
 extern void sepol_debug(int on);

Index: libsepol/include/sepol/policydb/services.h
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/include/sepol/policydb/services.h,v retrieving revision 1.1

include/sepol/policydb/services.h +1 -0
diff -u -p -r1.1 services.h
@@ -108,6 +108,7 @@ extern int sepol_sid_to_context(
  * has the string representation specified by `scontext'.
  */
 extern int sepol_context_to_sid(
+	policydb_t *p,
 	sepol_security_context_t scontext,		/* IN */
 	size_t  scontext_len,			/* IN */
 	sepol_security_id_t *out_sid);		/* OUT */

Index: libsepol/src/libsepol.map
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/src/libsepol.map,v retrieving revision 1.6

src/libsepol.map +9 -3
diff -u -p -r1.6 libsepol.map
@@ -1,4 +1,11 @@
-{
+LIBSEPOL_1.0 {
+  global: 
+	sepol_genbools*; sepol_set_policydb_from_file; sepol_check_context; sepol_genusers; sepol_debug; sepol_set_delusers;
+ local: 
+	*;
+};
+
+LIBSEPOL_1.1 {
   global: 
 	sepol_genbools*; sepol_set_policydb_from_file; sepol_check_context; sepol_genusers; sepol_debug; sepol_set_delusers;
 	sepol_policy_file_create; sepol_policy_file_free;
@@ -17,5 +24,4 @@
 	sepol_bool*; sepol_context*; sepol_disable_debug; sepol_enable_debug;
 	sepol_iface*; sepol_port*; sepol_user*; sepol_clear_unused_users;
 	sepol_role_is_valid; sepol_set_delusers;
-  local: *;
-};
+} LIBSEPOL_1.0;

Index: libsepol/src/services.c
===================================================================
RCS file: /nfshome/pal/CVS/selinux-usr/libsepol/src/services.c,v retrieving revision 1.23

src/services.c +11 -3
diff -u -p -r1.23 services.c
@@ -538,7 +538,8 @@ out:
  * Return a SID associated with the security context that
  * has the string representation specified by `scontext'.
  */
-int sepol_context_to_sid(sepol_security_context_t scontext,
+int sepol_context_to_sid(policydb_t *policydb,
+	                    sepol_security_context_t scontext,
 			    size_t scontext_len,
 			    sepol_security_id_t * sid)
 {
@@ -566,10 +567,17 @@ int sepol_context_to_sid(sepol_security_
 	return STATUS_ERR;
 }
 
-int sepol_check_context(char *context) 
+int sepol_check_context1__ (char *context) 
 {
-	return sepol_context_to_sid(context, strlen(context)+1, NULL);
+	return sepol_context_to_sid(policydb, context, strlen(context)+1, NULL);
 }
+asm(".symver sepol_check_context1__,sepol_check_context@LIBSEPOL_1.0");
+
+int sepol_check_context2__ (sepol_policydb_t *p, char *context) 
+{
+	return sepol_context_to_sid(&p->p, context, strlen(context)+1, NULL);
+}
+asm(".symver sepol_check_context2__,sepol_check_context@@LIBSEPOL_1.1");
 
 static inline int compute_sid_handle_invalid_context(
 	context_struct_t *scontext,
#6 lore raw
In reply to Ivan Gyurdiev
Quoted 4 lines · Ivan Gyurdiev

The attached patch replaces the current semanage debugging system, with one based on callbacks. It breaks API, so the libsemanage.map file needs to be updated, but I'm not sure how to do that properly, so I left that part out...

At present, you can just modify it without introducing a new interface version, because the shared library interface has not been officially released in any form yet.

Quoted 2 lines

- replaced use of __FUNCTION__ with __func__, which should not be GNU specific

OTOH, __func__ is not known to older versions of gcc, and we already build with -D_GNU_SOURCE.

An obvious concern about the new msg_write function is that it requires memory allocation itself. Thus, on a memory allocation failure in the caller, we might easily end up dropping the out of memory error message due to lack of available memory at this point.

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.
#7 lore raw
Quoted 4 lines · Stephen Smalley

An obvious concern about the new msg_write function is that it requires memory allocation itself. Thus, on a memory allocation failure in the caller, we might easily end up dropping the out of memory error message due to lack of available memory at this point.

More generally, I'm not sure why msg_write writes the formatted string to a private buffer and passes the new msg type to the callback, requiring the callback to call *_get methods, versus just having the callback be a stdarg function that takes all of the arguments (plus auxiliary data) directly.

Other comments:

libsemanage/src/direct_api.c +16 -4
diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/direct_api.c exp/libsemanage/src/direct_api.c
@@ -268,11 +268,23 @@
 		goto cleanup;
 	}
 
-	/* expand and verify the resulting policy */
-	if (semanage_expand_sandbox(sh, base) < 0 ||
-	    semanage_verify_kernel(sh) != 0) {
+	/* Expand the resulting policy */
+	if (semanage_expand_sandbox(sh, base) < 0)
+		goto cleanup;
+
+#if 0
+	/* Link components into base policy */
+	if (semanage_base_merge_components(sh, NULL /* FIXME */) < 0)
+		goto cleanup;
+
+	/* Commit changes to components */
+	if (semanage_commit_components(sh) < 0)
+		goto cleanup;
+#endif
+
+	/* Verify policy */
+	if (semanage_verify_kernel(sh) != 0)
 		goto cleanup;
-	}
 

Why wouldn't the above steps occur as part of semanage_expand_sandbox()?

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

An obvious concern about the new msg_write function is that it requires memory allocation itself. Thus, on a memory allocation failure in the caller, we might easily end up dropping the out of memory error message due to lack of available memory at this point.

Quoted 6 lines

More generally, I'm not sure why msg_write writes the formatted string to a private buffer and passes the new msg type to the callback, requiring the callback to call *_get methods, versus just having the callback be a stdarg function that takes all of the arguments (plus auxiliary data) directly.

Taking all of the arguments directly is bad - does not respond well to change, and I've already changed this at least 3 times. However, we could take 1) the void* arg, 2) the message structure (auxilary data) 2) the fmt, and 3) the variadic list..and I think that would be flexible enough. I changed it, because I thought this would be a simpler, and more intuitive interface, but I can change it back..

Quoted 31 lines

Other comments:

diff -Naur --exclude CVS --exclude ChangeLog --exclude VERSION --exclude libselinux --exclude policy_components.c old/libsemanage/src/direct_api.c exp/libsemanage/src/direct_api.c

libsemanage/src/direct_api.c +16 -4
@@ -268,11 +268,23 @@
 		goto cleanup;
 	}
 
-	/* expand and verify the resulting policy */
-	if (semanage_expand_sandbox(sh, base) < 0 ||
-	    semanage_verify_kernel(sh) != 0) {
+	/* Expand the resulting policy */
+	if (semanage_expand_sandbox(sh, base) < 0)
+		goto cleanup;
+
+#if 0
+	/* Link components into base policy */
+	if (semanage_base_merge_components(sh, NULL /* FIXME */) < 0)
+		goto cleanup;
+
+	/* Commit changes to components */
+	if (semanage_commit_components(sh) < 0)
+		goto cleanup;
+#endif
+
+	/* Verify policy */
+	if (semanage_verify_kernel(sh) != 0)
 		goto cleanup;
-	}
 
Quoted 2 lines

Why wouldn't the above steps occur as part of semanage_expand_sandbox()?

...I suppose it could go there as well..

--
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
Quoted 6 lines · Ivan Gyurdiev

Taking all of the arguments directly is bad - does not respond well to change, and I've already changed this at least 3 times. However, we could take 1) the void* arg, 2) the message structure (auxilary data) 2) the fmt, and 3) the variadic list..and I think that would be flexible enough. I changed it, because I thought this would be a simpler, and more intuitive interface, but I can change it back..

The auxiliary data could be directly embedded in the handle, avoiding any need to introduce a new structure (or allocation), with the get methods operating directly on the handle much like the old strerror interface. I don't think you want msg_write needing to allocate any memory or performing any formatting string writing, as that should all be done in the callback if desired.

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

The auxiliary data could be directly embedded in the handle, avoiding any need to introduce a new structure (or allocation), with the get methods operating directly on the handle much like the old strerror interface. I don't think you want msg_write needing to allocate any memory or performing any formatting string writing, as that should all be done in the callback if desired.

Okay, so it sounds like the optimal setup is:
- pass variadic list to callback
- remove msg field from message structure (rename?)
- embed message structure into handle to avoid allocation

Then handler takes four arguments instead of the current two:

- void* arg
- message structure (rename?)
- fmt
- variadic list

--
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
Quoted 11 lines · Ivan Gyurdiev

Okay, so it sounds like the optimal setup is:
- pass variadic list to callback
- remove msg field from message structure (rename?)
- embed message structure into handle to avoid allocation

Then handler takes four arguments instead of the current two:

- void* arg
- message structure (rename?)
- fmt
- variadic list

Why not just use the handle directly, and drop the separate message structure entirely?

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

Why not just use the handle directly, and drop the separate message structure entirely?

It's disorganized... the handle contains all kinds of things - I don't like adding random fields to it. It'd be more orderly to add a structure, and functions to work with it, in a separate file. I would think that nested structs have no runtime overhead...

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

#13 lore raw
Quoted 7 lines · Ivan Gyurdiev
Quoted 3 lines

Why not just use the handle directly, and drop the separate message structure entirely?

It's disorganized... the handle contains all kinds of things - I don't like adding random fields to it. It'd be more orderly to add a structure, and functions to work with it, in a separate file. I would think that nested structs have no runtime overhead...

Hmmm...well, I thought that one of the purposes of the handle was for error handling, and you are adding the callback and callback arg there in place of the old error buffer. More generally, I was wondering if the callback might want the handle without needing to explicitly specify it as part of the callback arg.

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.
#14 lore raw
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines

Why not just use the handle directly, and drop the separate message structure entirely?

It's disorganized... the handle contains all kinds of things - I don't like adding random fields to it. It'd be more orderly to add a structure, and functions to work with it, in a separate file. I would think that nested structs have no runtime overhead...

Hmmm...well, I thought that one of the purposes of the handle was for error handling, and you are adding the callback and callback arg there in place of the old error buffer.

The handle's kind of used for everything right now.... It's a central merge point for anything that might need (or benefit from)
preserved state over multiple function calls. I don't feel very strongly about this - I can pass back the handle if you prefer that - it's just an organizational preference (I might change my mind tomorrow when I see how it looks :)

Quoted 4 lines

More generally, I was wondering if
the callback might want the handle without needing to explicitly specify it as part of the callback arg.

The callback's job should be to print out the error. If it wants the handle, it's certainly free to pass it as part of the argument, but I'm not sure what it would be doing with it. I imagine the callback doing error _reporting_, while the normal function stack handles error _response_.

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

#15 lore raw
In reply to Ivan Gyurdiev
Quoted 6 lines

Okay, so it sounds like the optimal setup is:
- pass variadic list to callback
- remove msg field from message structure (rename?)
- embed message structure into handle to avoid allocation

Quoted 6 lines

Then handler takes four arguments instead of the current two:

- void* arg
- message structure (rename?)
- fmt
- variadic list

Wait.... how do I invoke a variadic function from a variadic function? Should I pass the va_list as an argument?
I knew there was a reason I got rid of this in the first place...

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

#16 lore raw
Quoted 7 lines · Ivan Gyurdiev
Quoted 7 lines

Okay, so it sounds like the optimal setup is:
- pass variadic list to callback
- remove msg field from message structure (rename?)
- embed message structure into handle to avoid allocation

Then handler takes four arguments instead of the current two:

Quoted 7 lines
Quoted 4 lines

- void* arg
- message structure (rename?)
- fmt
- variadic list

Wait.... how do I invoke a variadic function from a variadic function? Should I pass the va_list as an argument?
I knew there was a reason I got rid of this in the first place...

Why can't msg_write be a macro?

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

OTOH, __func__ is not known to older versions of gcc, and we already build with -D_GNU_SOURCE.

How much older? From the gcc webpage, I see it supported for 3.0+. IMHO we should compile against a standard, not against a compiler..

Quoted 5 lines

An obvious concern about the new msg_write function is that it requires memory allocation itself. Thus, on a memory allocation failure in the caller, we might easily end up dropping the out of memory error message due to lack of available memory at this point.

By the way, does the printf family require such a memory allocation?

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

#18 lore raw
Quoted 5 lines · Ivan Gyurdiev
Quoted 3 lines

OTOH, __func__ is not known to older versions of gcc, and we already build with -D_GNU_SOURCE.

How much older? From the gcc webpage, I see it supported for 3.0+. IMHO we should compile against a standard, not against a compiler..

info gcc says:

__FUNCTION__' is another name for __func__'. Older versions of GCC recognize only this name. However, it is not standardized. For maximum portability, we recommend you use `__func__', but provide a fallback definition with the preprocessor:

     #if __STDC_VERSION__ < 199901L
     # if __GNUC__ >= 2
     #  define __func__ __FUNCTION__
     # else
     #  define __func__ "<unknown>"
     # endif
     #endif

In any event, we have been using gcc and glibc specific extensions in our code for some time, and I don't expect that to change.

Quoted 1 line

By the way, does the printf family require such a memory allocation?

I don't believe so, at least not in the common case.

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.