Skip to contentSkip to conversation

File Contexts APIs (part 1)

SEMANAGE Ivan Gyurdiev 2 participants 3 messages Dec 27, 2005 – Jan 2, 2006 SELinux security module

lore
Share & tools
lore
#0 lore raw

Hi, this patch implemens an API for working with file_contexts.local.

A file context record is represented as a (regexp, type, context) triple. A key is (regexp, type), and matches only if regexp and type match exactly. type is block, char, pipe, regular file, all files, etc..

This key scheme might be too simple, but (as in ports), it seems like more complex operations with the key should be pushed into the client.

The parser is compatible with the current file_contexts format. It also allows multi-line records, like all other semanage parsers.

Note:
Unlike ports and interfaces, semanage_fcontext_get_con() can and
will
return NULL on file specifications without a context (<<none>>). Interfaces and ports do not support <<none>>.

Note2:
Like in ports, I am concerned here about the order of
iterate()/list()
traversal - I will likely have to reverse it in the dbase code,
because it
seems backwards.

Caveats:
- there's currently no support for working with the overall file_contexts file, this is just the .local file

- validation is missing - needs additional sepol functionality, just like seuser validation

- .local file is not installed yet, it stays in the sandbox -
needs code to merge
.local into the other file_contexts file somehow (or
alternatively, the file_contexts.local path
should be exposed by libselinux)

libsemanage/include/semanage/fcontext_record.h +86 -0
diff -Naurp --exclude-from excludes old/libsemanage/include/semanage/fcontext_record.h new/libsemanage/include/semanage/fcontext_record.h
@@ -0,0 +1,86 @@
+/* Copyright (C) 2005 Red Hat, Inc. */
+
+#ifndef _SEMANAGE_FCONTEXT_RECORD_H_
+#define _SEMANAGE_FCONTEXT_RECORD_H_ 
+
+#include <semanage/context_record.h>
+#include <semanage/handle.h>
+
+#ifndef _SEMANAGE_FCONTEXT_DEFINED_
+struct semanage_fcontext;
+struct semanage_fcontext_key;
+typedef struct semanage_fcontext semanage_fcontext_t;
+typedef struct semanage_fcontext_key semanage_fcontext_key_t;
+#define _SEMANAGE_FCONTEXT_DEFINED_
+#endif
+
+/* Key */
+extern int semanage_fcontext_compare(
+	semanage_fcontext_t* fcontext, 
+	semanage_fcontext_key_t* key);
+
+extern int semanage_fcontext_key_create(
+	semanage_handle_t* handle,
+	const char* expr,
+	int type,
+	semanage_fcontext_key_t** key_ptr);
+
+extern int semanage_fcontext_key_extract(
+	semanage_handle_t* handle,
+	semanage_fcontext_t* fcontext,
+	semanage_fcontext_key_t** key_ptr);
+
+extern void semanage_fcontext_key_free(
+	semanage_fcontext_key_t* key);
+
+/* Regexp */
+extern const char* semanage_fcontext_get_expr(
+	semanage_fcontext_t* fcontext);
+
+extern int semanage_fcontext_set_expr(
+	semanage_handle_t* handle,
+	semanage_fcontext_t* fcontext, 
+	const char* expr);
+
+/* Type */
+#define SEMANAGE_FCONTEXT_ALL   0
+#define SEMANAGE_FCONTEXT_REG   1
+#define SEMANAGE_FCONTEXT_DIR   2
+#define SEMANAGE_FCONTEXT_CHAR  3
+#define SEMANAGE_FCONTEXT_BLOCK 4
+#define SEMANAGE_FCONTEXT_SOCK  5
+#define SEMANAGE_FCONTEXT_LINK  6
+#define SEMANAGE_FCONTEXT_PIPE  7
+
+extern int semanage_fcontext_get_type(
+	semanage_fcontext_t* fcontext);
+
+extern const char* semanage_fcontext_get_type_str(
+	semanage_fcontext_t* fcontext);
+
+extern void semanage_fcontext_set_type(
+	semanage_fcontext_t* fcontext,
+	int type);
+
+/* Context */
+extern semanage_context_t* semanage_fcontext_get_con(
+	semanage_fcontext_t* fcontext);
+
+extern void semanage_fcontext_set_con(
+	semanage_fcontext_t* fcontext, 
+	semanage_context_t* con);
+
+/* Create/Clone/Destroy */
+extern int semanage_fcontext_create(
+	semanage_handle_t* handle,
+	semanage_fcontext_t** fcontext_ptr);
+
+extern int semanage_fcontext_clone(
+	semanage_handle_t* handle,
+	semanage_fcontext_t* fcontext, 
+	semanage_fcontext_t** fcontext_ptr);
+
+extern void semanage_fcontext_free(
+	semanage_fcontext_t* fcontext);
+
+#endif
libsemanage/include/semanage/fcontexts_local.h +54 -0

54 lines not on this page · raw message

libsemanage/include/semanage/semanage.h +1 -0
diff -Naurp --exclude-from excludes old/libsemanage/include/semanage/semanage.h new/libsemanage/include/semanage/semanage.h
@@ -40,6 +40,7 @@
 #include <semanage/booleans_active.h>
 #include <semanage/users_local.h>
 #include <semanage/users_policy.h>
+#include <semanage/fcontexts_local.h>
 #include <semanage/seusers.h>
 #include <semanage/ports_local.h>
 #include <semanage/ports_policy.h>
libsemanage/src/direct_api.c +5 -0
diff -Naurp --exclude-from excludes old/libsemanage/src/direct_api.c new/libsemanage/src/direct_api.c
@@ -36,6 +36,7 @@
 #include "port_internal.h"
 #include "iface_internal.h"
 #include "boolean_internal.h"
+#include "fcontext_internal.h"
 
 #include "debug.h"
 #include "handle.h"
@@ -120,6 +121,9 @@ int semanage_direct_connect(semanage_han
 	if (bool_file_dbase_init(sh, semanage_bool_dbase_local(sh)) < 0)
 		goto err;
 
+	if (fcontext_file_dbase_init(sh, semanage_fcontext_dbase_local(sh)) < 0)
+		goto err;
+
 	if (seuser_file_dbase_init(sh, semanage_seuser_dbase(sh)) < 0)
 		goto err;
 
@@ -167,6 +171,7 @@ static int semanage_direct_disconnect(se
 	port_file_dbase_release(semanage_port_dbase_local(sh));
 	iface_file_dbase_release(semanage_iface_dbase_local(sh));
 	bool_file_dbase_release(semanage_bool_dbase_local(sh));
+	fcontext_file_dbase_release(semanage_fcontext_dbase_local(sh));
 	seuser_file_dbase_release(semanage_seuser_dbase(sh));
 
 	user_policydb_dbase_release(semanage_user_dbase_policy(sh));
libsemanage/src/fcontext_internal.h +22 -0
diff -Naurp --exclude-from excludes old/libsemanage/src/fcontext_internal.h new/libsemanage/src/fcontext_internal.h
@@ -0,0 +1,22 @@
+#ifndef _SEMANAGE_FCONTEXT_INTERNAL_H_
+#define _SEMANAGE_FCONTEXT_INTERNAL_H_
+
+#include <semanage/fcontext_record.h>
+#include <semanage/fcontexts_local.h>
+#include "database.h"
+#include "handle.h"
+#include "dso.h"
+
+/* TODO: hidden prototypes */
+
+/* FCONTEXT RECORD: metod table */
+extern record_table_t SEMANAGE_FCONTEXT_RTABLE;
+
+extern int fcontext_file_dbase_init(
+	semanage_handle_t* handle,
+	dbase_config_t* dconfig);
+
+extern void fcontext_file_dbase_release(
+	dbase_config_t* dconfig);
+
+#endif
libsemanage/src/fcontext_record.c +220 -0

220 lines not on this page · raw message

libsemanage/src/fcontexts_file.c +185 -0

185 lines not on this page · raw message

libsemanage/src/fcontexts_local.c +92 -0

92 lines not on this page · raw message

libsemanage/src/handle.h +13 -7
diff -Naurp --exclude-from excludes old/libsemanage/src/handle.h new/libsemanage/src/handle.h
@@ -77,20 +77,21 @@ struct semanage_handle {
 	struct semanage_policy_table* funcs;
 
 	/* Object databases */
-#define DBASE_COUNT      10
+#define DBASE_COUNT      11
 
 #define DBASE_LOCAL_USERS       0
 #define DBASE_LOCAL_PORTS       1
 #define DBASE_LOCAL_INTERFACES  2
 #define DBASE_LOCAL_BOOLEANS    3
-#define DBASE_SEUSERS           4
+#define DBASE_LOCAL_FCONTEXTS	4
+#define DBASE_SEUSERS           5
 
-#define DBASE_POLICY_USERS      5
-#define DBASE_POLICY_PORTS      6
-#define DBASE_POLICY_INTERFACES 7
-#define DBASE_POLICY_BOOLEANS   8
+#define DBASE_POLICY_USERS      6
+#define DBASE_POLICY_PORTS      7
+#define DBASE_POLICY_INTERFACES 8
+#define DBASE_POLICY_BOOLEANS   9
 
-#define DBASE_ACTIVE_BOOLEANS   9
+#define DBASE_ACTIVE_BOOLEANS   10
 	dbase_config_t dbase[DBASE_COUNT];
 };
 
@@ -115,6 +116,11 @@ dbase_config_t* semanage_bool_dbase_loca
 }
 
 static inline
+dbase_config_t* semanage_fcontext_dbase_local(semanage_handle_t* handle) {
+	return &handle->dbase[DBASE_LOCAL_FCONTEXTS];
+}
+
+static inline
 dbase_config_t* semanage_seuser_dbase(semanage_handle_t* handle) {
 	return &handle->dbase[DBASE_SEUSERS];
 }
libsemanage/src/libsemanage.map +1 -0
diff -Naurp --exclude-from excludes old/libsemanage/src/libsemanage.map new/libsemanage/src/libsemanage.map
@@ -11,5 +11,6 @@ LIBSEMANAGE_1.0 {
 	  semanage_reload_policy; semanage_set_reload; semanage_set_rebuild;
 	  semanage_user_*; semanage_bool_*; semanage_seuser_*;
 	  semanage_iface_*; semanage_port_*; semanage_context_*;
+	  semanage_fcontext_*;
   local: *;
 };
libsemanage/src/policy_components.c +1 -0
diff -Naurp --exclude-from excludes old/libsemanage/src/policy_components.c new/libsemanage/src/policy_components.c
@@ -188,6 +188,7 @@ int semanage_commit_components(
 		semanage_bool_dbase_local(handle),
 		semanage_user_dbase_local(handle),
 		semanage_port_dbase_local(handle),
+		semanage_fcontext_dbase_local(handle),
 		semanage_seuser_dbase(handle),
 		semanage_bool_dbase_active(handle),
 	};
libsemanage/src/semanageswig.i +32 -0
diff -Naurp --exclude-from excludes old/libsemanage/src/semanageswig.i new/libsemanage/src/semanageswig.i
@@ -38,6 +38,8 @@
 	#include "semanage/ports_local.h"
 	#include "semanage/ports_policy.h"
 	#include "semanage/seuser_record.h"
+	#include "semanage/fcontext_record.h"
+	#include "semanage/fcontexts_local.h"
 	#include "semanage/seusers.h"	
 	#include "semanage/semanage.h"
 %}
@@ -141,6 +143,34 @@
 	$1 = &temp;
 }
 
+/** fcontext typemaps **/
+
+/* the wrapper will setup this parameter for passing... the resulting python functions
+   will not take the semanage_fcontext_t *** parameter */
+%typemap(in, numinputs=0) semanage_fcontext_t ***(semanage_fcontext_t **temp) {
+        $1 = &temp;
+}
+
+%typemap(argout) semanage_fcontext_t *** {
+        $result = t_output_helper($result, SWIG_NewPointerObj(*$1, SWIGTYPE_p_p_semanage_fcontext, 0));
+}
+
+%typemap(in, numinputs=0) semanage_fcontext_t **(semanage_fcontext_t *temp) {
+        $1 = &temp;
+}
+
+%typemap(argout) semanage_fcontext_t ** {
+        $result = t_output_helper($result, SWIG_NewPointerObj(*$1, SWIGTYPE_p_semanage_fcontext, 0));
+}
+
+%typemap(argout) semanage_fcontext_key_t ** {
+        $result = t_output_helper($result, SWIG_NewPointerObj(*$1, SWIGTYPE_p_semanage_fcontext_key, 0));
+}
+
+%typemap(in, numinputs=0) semanage_fcontext_key_t **(semanage_fcontext_key_t *temp) {
+        $1 = &temp;
+}
+
 /** interface typemaps **/
 
 /* the wrapper will setup this parameter for passing... the resulting python functions
@@ -271,6 +301,8 @@
 %include "../include/semanage/port_record.h"
 %include "../include/semanage/ports_local.h"
 %include "../include/semanage/ports_policy.h"
+%include "../include/semanage/fcontext_record.h"
+%include "../include/semanage/fcontexts_local.h"
 %include "../include/semanage/seuser_record.h"
 %include "../include/semanage/seusers.h"
 %include "../include/semanage/semanage.h"
#1 lore raw
Quoted 18 lines · Ivan Gyurdiev

Hi, this patch implemens an API for working with file_contexts.local.

A file context record is represented as a (regexp, type, context) triple. A key is (regexp, type), and matches only if regexp and type match exactly. type is block, char, pipe, regular file, all files, etc..

This key scheme might be too simple, but (as in ports), it seems like more complex operations with the key should be pushed into the client.

The parser is compatible with the current file_contexts format. It also allows multi-line records, like all other semanage parsers.

Note:
Unlike ports and interfaces, semanage_fcontext_get_con() can and
will
return NULL on file specifications without a context (<<none>>). Interfaces and ports do not support <<none>>.

Quoted 6 lines

Note2:
Like in ports, I am concerned here about the order of
iterate()/list()
traversal - I will likely have to reverse it in the dbase code,
because it
seems backwards.

Matchpathcon already re-sorts contexts (based on exact/inexact matches now, and stem specificity once we get some issues resolved). That said, the sorts we do are stable, which means if 2 keys have the exact same specificity they'll stay in order. This would be an issue with your reversal. Just out of curiosity, why not prepend entries to the linked list so that they are always in the order of the file (likewise for every other record type). It seems strange that the list could potentially get reversed on every write (reads in reverse and writes in order?)

Quoted 5 lines

Caveats:
- there's currently no support for working with the overall file_contexts file, this is just the .local file

Quoted 9 lines

- validation is missing - needs additional sepol functionality, just like seuser validation

- .local file is not installed yet, it stays in the sandbox -
needs code to merge
.local into the other file_contexts file somehow (or
alternatively, the file_contexts.local path
should be exposed by libselinux)

we already have a little infrastructure for dealing with file_contexts files in libsemanage, however matchpathcon is currently looking for a .local file in POLICYTYPE/contexts/files/file_contexts.local so you should probably install it there.

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

Just out of curiosity, why not prepend entries to the linked list so
that they are always in the order of the file (likewise for every other record type). It seems strange that the list could potentially get reversed on every write (reads in reverse and writes in order?)

There's two kinds of reads going on here - there's read-from-file to populate the dbase, and iterate/list type of read. Read-from-file prepends at the beginning of the list like you say. Write-to-file to flush the data is back-to-front. Together those two things should guarantee stability of the file ordering.

That's completely independent from ordering of the iterate/list operation. That used to be front-to-back, and one of the 21 patches I sent flips it to back-to-front. That agrees with the in-file order, but that's not important. The important thing is that things are iterated in the order in which they are added (add also prepends), which would be expected behavior. Since ports are merged into policy in the iterate() order, this also guarantees that the port specs added later take precedence (because they're appended at the front in libsepol as well). So I think it should work. I might have to take another look though, because Dan is having some problems with it - not sure if he applied the reversal patch.

Another note - the ordering in the semanage sandbox files is an implementation detail, and does not have to be the same as it used to be in policy. For file_contexts it has to be the same (and it is) if we want to just install the contexts file as is. For ports, it's actually exactly backwards to net_contexts, but that doesn't matter, because the ports file is just used for internal storage within the library, it's flipped when it goes into policy.

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