Skip to contentSkip to conversation

What provides _policy?

DISCUSSION Sven Vermeulen 2 participants 6 messages May 1, 2013 – May 7, 2013 SELinux security module

lore
Share & tools
lore
#0 lore raw

Recent policycoreutils' sepolicy imports "_policy", but I am having troubles finding where _policy is provided?

Wkr,
Sven Vermeulen

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

#1 lore raw
Quoted 2 lines · Sven Vermeulen

Recent policycoreutils' sepolicy imports "_policy", but I am having troubles finding where _policy is provided?

Okay so I know that _policy comes from policy.c which is build through "setup.py build" and is provided through a _policy.so file (I was looking for _policy.py which isn't created).

However, I'm having a hard time debugging why I get the following:

File "/usr/bin/sepolicy-2.7", line 63, in __call__
from sepolicy.network import domains
File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 44, in <module>
portrecs, portrecsbynum = _gen_port_dict()
File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 31, in _gen_port_dict
for i in info(sepolicy.PORT):
File "/usr/lib64/python2.7/site-packages/sepolicy/__init__.py", line 182, in info
dict_list = _policy.info(setype, name)
RuntimeError: No such file or directory

What could this error be about? I get this when I try "sepolicy communicate -s portage_t".

Wkr,
Sven Vermeulen

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 23 lines · Sven Vermeulen
Quoted 2 lines · Sven Vermeulen

Recent policycoreutils' sepolicy imports "_policy", but I am having troubles finding where _policy is provided?

Okay so I know that _policy comes from policy.c which is build through "setup.py build" and is provided through a _policy.so file (I was looking for _policy.py which isn't created).

However, I'm having a hard time debugging why I get the following:

File "/usr/bin/sepolicy-2.7", line 63, in __call__ from sepolicy.network import domains File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 44, in <module> portrecs, portrecsbynum = _gen_port_dict() File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 31, in _gen_port_dict for i in info(sepolicy.PORT): File "/usr/lib64/python2.7/site-packages/sepolicy/__init__.py", line 182, in info dict_list = _policy.info(setype, name) RuntimeError: No such file or directory

What could this error be about? I get this when I try "sepolicy communicate -s portage_t".

Quoted 7 lines

Wkr, Sven Vermeulen

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

Where are you executing this?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.13 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlGH37cACgkQrlYvE4MpobPTOgCghoYfGmhPd9px6ptcOmy+ZdXt OHwAn3dJ1KkPHldmbtgbA1LlPjppvCW2
=/abh
-----END PGP SIGNATURE-----

--
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
Quoted 12 lines · Daniel J Walsh
Quoted 12 lines

However, I'm having a hard time debugging why I get the following:

File "/usr/bin/sepolicy-2.7", line 63, in __call__ from sepolicy.network import domains File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 44, in <module> portrecs, portrecsbynum = _gen_port_dict() File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 31, in _gen_port_dict for i in info(sepolicy.PORT): File "/usr/lib64/python2.7/site-packages/sepolicy/__init__.py", line 182, in info dict_list = _policy.info(setype, name) RuntimeError: No such file or directory

Quoted 4 lines
Quoted 3 lines

What could this error be about? I get this when I try "sepolicy communicate -s portage_t".

Where are you executing this?

If you mean distribution: Gentoo Hardened.
If you mean location: anywhere (like in /root).
If you mean context: I'm staff_u:sysadm_r:sysadm_t at the time of executing.

I get the same stacktrace with "sepolicy transition".

I notice that there is some port specific stuff in the stack trace; "semanage port -l" works ok here.

Wkr,
Sven Vermeulen

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 27 lines · Sven Vermeulen
Quoted 12 lines · Daniel J Walsh
Quoted 12 lines

However, I'm having a hard time debugging why I get the following:

File "/usr/bin/sepolicy-2.7", line 63, in __call__ from sepolicy.network import domains File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 44, in <module> portrecs, portrecsbynum = _gen_port_dict() File "/usr/lib64/python2.7/site-packages/sepolicy/network.py", line 31, in _gen_port_dict for i in info(sepolicy.PORT): File "/usr/lib64/python2.7/site-packages/sepolicy/__init__.py", line 182, in info dict_list = _policy.info(setype, name) RuntimeError: No such file or directory

Quoted 4 lines
Quoted 3 lines

What could this error be about? I get this when I try "sepolicy communicate -s portage_t".

Where are you executing this?

If you mean distribution: Gentoo Hardened. If you mean location: anywhere (like in /root). If you mean context: I'm staff_u:sysadm_r:sysadm_t at the time of executing.

I get the same stacktrace with "sepolicy transition".

I notice that there is some port specific stuff in the stack trace; "semanage port -l" works ok here.

Quoted 8 lines

Wkr, Sven Vermeulen

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

You might have found a bug in the current upstream, we don't have this problem in Fedora.

I have attached the current fedora-sepolicy patch. -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.13 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlGH+/cACgkQrlYvE4MpobMwSgCfebsEWOfLhXoRi12gIwmAToij XQgAmQFg/0J2lXvWaBNmSWnYDdv+2Bie
=vUg0
-----END PGP SIGNATURE-----

policycoreutils/sepolicy/Makefile +6 -2
index 11b534f..ae064c4 100644
@@ -7,7 +7,7 @@ SBINDIR ?= $(PREFIX)/sbin
 MANDIR ?= $(PREFIX)/share/man
 LOCALEDIR ?= /usr/share/locale
 PYTHON ?= /usr/bin/python
-BASHCOMPLETIONDIR ?= $(DESTDIR)/etc/bash_completion.d/
+BASHCOMPLETIONDIR ?= $(DESTDIR)/usr/share/bash-completion/completions
 SHAREDIR ?= $(PREFIX)/share/sandbox
 override CFLAGS = $(LDFLAGS) -I$(PREFIX)/include -DPACKAGE="policycoreutils" -Wall -Werror -Wextra -W  -DSHARED -shared
 
@@ -22,11 +22,15 @@ clean:
 	$(PYTHON) setup.py clean
 	-rm -rf build *~ \#* *pyc .#*
 
+sepolgen:
+	ln -sf sepolicy sepolgen
+
 install:
 	$(PYTHON) setup.py install `test -n "$(DESTDIR)" && echo --root $(DESTDIR)`
 	[ -d $(BINDIR) ] || mkdir -p $(BINDIR)
 	install -m 755 sepolicy.py $(BINDIR)/sepolicy
+	(cd $(BINDIR); ln -sf sepolicy sepolgen)
 	-mkdir -p $(MANDIR)/man8
 	install -m 644 *.8 $(MANDIR)/man8
 	-mkdir -p $(BASHCOMPLETIONDIR)
-	install -m 644 $(BASHCOMPLETIONS) $(BASHCOMPLETIONDIR)
+	install -m 644 $(BASHCOMPLETIONS) $(BASHCOMPLETIONDIR)/sepolicy
policycoreutils/sepolicy/info.c +57 -0

69 lines not on this page · raw message

policycoreutils/sepolicy/policy.c +0 -1
index 4eca22d..eeee0ab 100644
@@ -66,7 +66,6 @@ PyObject *wrap_policy(PyObject *UNUSED(self), PyObject *args){
     }
     apol_vector_destroy(&mod_paths);
     
-    policy_load_options |= QPOL_POLICY_OPTION_MATCH_SYSTEM;
     policy = apol_policy_create_from_policy_path(pol_path, policy_load_options, NULL, NULL);
     apol_policy_path_destroy(&pol_path);
     if (!policy) {
policycoreutils/sepolicy/sepolgen.8 new file +1 -0
new file mode 100644
index 0000000..3ecf3eb
@@ -0,0 +1 @@
+.so man8/sepolicy-generate.8
policycoreutils/sepolicy/sepolicy-bash-completion.sh +10 -9
index 82fea52..6efd463 100644
@@ -1,6 +1,6 @@
 # This file is part of systemd.
 #
-# Copyright 2012 Dan Walsh
+# Copyright 2012-2013 Dan Walsh
 #
 # systemd is free software; you can redistribute it and/or modify it
 # under the terms of the GNU General Public License as published by
@@ -80,8 +80,8 @@ _sepolicy () {
                [booleans]='-h --help -p --path -a -all -b --boolean'
                [communicate]='-h --help -s --source -t --target -c --class -S --sourceaccess -T --targetaccess'
                [generate]='-a --admin --admin_user --application --cgi --confined_admin --customize  -d --domain --dbus --desktop_user -h --help --inetd --init -n --name --newtype -p --path --sandbox -T --test --term_user -u --user -w --writepath --x_user'
-               [interface]='-h --help -a --list_admin" -u --list_user -l --list'
-               [manpage]='-h --help -p --path -a -all -o --os -d --domain -w --web'
+               [interface]='-h --help -a --list_admin -c --compile -i --interface -l --list -u --list_user -u --list_user -v --verbose'
+               [manpage]='-h --help -p --path -a -all -o --os -d --domain -w --web -r --root'
                [network]='-h --help -d --domain -l --list -p --port -t --type '
                [transition]='-h --help -s --source -t --target'
         )
@@ -130,9 +130,6 @@ _sepolicy () {
                 COMPREPLY=( $( compgen -d -- "$cur") )
                 compopt -o filenames
                 return 0
-            elif [ "$prev" = "--type" -o "$prev" = "-t" ]; then
-                COMPREPLY=( $(compgen -W '0 1 2 3 4 5 6 7 8 9 10 11' -- "$cur") )
-                return 0
             elif [ "$prev" = "--domain" -o "$prev" = "-d" ]; then
                 COMPREPLY=( $(compgen -W "$( __get_all_domain_types ) " -- "$cur") )
                 return 0
@@ -140,7 +137,7 @@ _sepolicy () {
                 COMPREPLY=( $(compgen -W "$( __get_all_admin_interaces ) " -- "$cur") )
                 return 0
             elif [ "$prev" = "--user" -o "$prev" = "-u" ]; then
-                COMPREPLY=( $(compgen -W "$( __get_all_users ) " -- "$cur") )
+                COMPREPLY=( $(compgen -W "$( __get_all_users )" -- "$cur") )
                 return 0
             elif [[ "$cur" == "$verb" || "$cur" == "" || "$cur" == -* ]]; then
                 COMPREPLY=( $(compgen -W '${OPTS[$verb]}' -- "$cur") )
@@ -156,6 +153,10 @@ _sepolicy () {
             if [ "$prev" = "-d" -o "$prev" = "--domain" ]; then
                 COMPREPLY=( $(compgen -W "$( __get_all_domains ) " -- "$cur") )
                 return 0
+            elif test "$prev" = "-r" || test "$prev" = "--root" ; then
+                COMPREPLY=( $( compgen -d -- "$cur") )
+                compopt -o filenames
+                return 0
             elif [ "$prev" = "-o" -o "$prev" = "--os" ]; then
                 return 0
             elif test "$prev" = "-p" || test "$prev" = "--path" ; then
@@ -167,11 +168,11 @@ _sepolicy () {
             return 0
         elif [ "$verb" = "network" ]; then
             if [ "$prev" = "-t" -o "$prev" = "--type" ]; then
-                COMPREPLY=( $(compgen -W "$( __get_all_port_types ) " -- "$cur") )
+                COMPREPLY=( $(compgen -W "$( __get_all_port_types )" -- "$cur") )
                 return 0
             fi
             if [ "$prev" = "-d" -o "$prev" = "--domain" ]; then
-                COMPREPLY=( $(compgen -W "$( __get_all_domain_types ) " -- "$cur") )
+                COMPREPLY=( $(compgen -W "$( __get_all_domain_types )" -- "$cur") )
                 return 0
             fi
             COMPREPLY=( $(compgen -W '${OPTS[$verb]}' -- "$cur") )
policycoreutils/sepolicy/sepolicy-generate.8 +8 -2
index fb84af6..c2fa601 100644
@@ -8,12 +8,18 @@ sepolicy-generate \- Generate an initial SELinux policy module template.
 .B sepolicy generate [\-h] [\-d DOMAIN] [\-u USER] [\-w WRITE_PATH ] [\-a ADMIN_DOMAIN] [\-n NAME] [\-p PATH] [\-\-admin_user | \-\-application | \-\-cgi | \-\-confined_admin | \-\-customize | \-\-dbus | \-\-desktop_user | \-\-inetd | \-\-newtype | \-\-init | \-\-sandbox | \-\-term_user | \-\-x_user]
 
 .SH "DESCRIPTION"
-Use sepolicy generate to generate an SELinux policy Module.  sepolicy generate will generate 4 files.
+Use \fBsepolicy generate\fP to generate an SELinux policy Module.  \fBsepolicy generate\fP will create 5 files.
+
+If you specify a binary path, \fBsepolicy generate\fP will use the rpm payload of the binary along with \fBnm -D BINARY\fP to discover types and policy rules to generate these template files.
+
 
 .B Type Enforcing File NAME.te
 .br
 This file can be used to define all the types rules for a particular domain.
 
+.I Note:
+Policy generated by \fBsepolicy generate\fP will automatically add a permissive DOMAIN to your te file.  When you are satisfied that your policy works, you need to remove the permissive line from the te file to run your domain in enforcing mode.
+
 .B Interface File NAME.if
 .br
 This file defines the interfaces for the types generated in the te file, which can be used by other policy domains.
@@ -25,7 +31,7 @@ file paths to the types.  Tools like restorecon and RPM will use these paths to
 
 .B RPM Spec File NAME_selinux.spec
 .br
-This file is an RPM SPEC file that can be used to install the SELinux policy on to machines and setup the labeling. The spec file also installs the interface file and a man page describing the policy.  You can use sepolicy manpage -d NAME to generate the man page.
+This file is an RPM SPEC file that can be used to install the SELinux policy on to machines and setup the labeling. The spec file also installs the interface file and a man page describing the policy.  You can use \fBsepolicy manpage -d NAME\fP to generate the man page.
 
 .B Shell File NAME.sh
 .br
policycoreutils/sepolicy/sepolicy-interface.8 +10 -1
index 4fc9792..02c4c1a 100644
@@ -5,7 +5,7 @@ sepolicy-interface \- Print interface information based on the installed SELinux
 .SH "SYNOPSIS"
 
 .br
-.B sepolicy interface [\-h] [\-a | \-u | \-l ]
+.B sepolicy interface  [\-h] [\-c] [\-v] [\-a | \-u | \-l | \-i INTERFACE [INTERFACE ... ]]
 
 .SH "DESCRIPTION"
 Use sepolicy interface to print interfaces information based on SELinux Policy.
@@ -15,14 +15,23 @@ Use sepolicy interface to print interfaces information based on SELinux Policy.
 .I                \-a, \-\-list_admin
 List all domains with admin interface
 .TP
+.I                \-c, \-\-compile
+Test compile of interfaces
+.TP
 .I                \-h, \-\-help       
 Display help message
 .TP
+.I                \-i, \-\-interface
+Interface(s) to be displayed
+.TP
 .I                \-l, \-\-list
 List all interfaces
 .TP
 .I                \-u, \-\-list_user
 List all domains with SELinux user role interface
+.TP
+.I                \-v, \-\-verbose
+Display extended information about the interface including parameters and desctiprion if available.
 
 .SH "AUTHOR"
 This man page was written by Daniel Walsh <[email protected]>
policycoreutils/sepolicy/sepolicy-manpage.8 +4 -1
index b6abdf5..c05c943 100644
@@ -5,7 +5,7 @@ sepolicy-manpage \- Generate a man page based on the installed SELinux Policy
 .SH "SYNOPSIS"
 
 .br
-.B sepolicy manpage [\-w] [\-h] [\-p PATH ]  [\-a | \-d ]
+.B sepolicy manpage [\-w] [\-h] [\-p PATH ] [\-r ROOTDIR ] [\-a | \-d ]
 
 .SH "DESCRIPTION"
 Use sepolicy manpage to generate manpages based on SELinux Policy.
@@ -24,6 +24,9 @@ Display help message
 .I                \-p, \-\-path
 Specify the directory to store the created man pages. (Default to /tmp)
 .TP
+.I                \-r, \-\-root
+Specify alternate root directory to generate man pages from. (Default to /)
+.TP
 .I                \-w, \-\-web
 Generate an additional HTML man pages for the specified domain(s).
 
policycoreutils/sepolicy/sepolicy.py +178 -72

414 lines not on this page · raw message

policycoreutils/sepolicy/sepolicy/__init__.py +104 -28

196 lines not on this page · raw message

policycoreutils/sepolicy/sepolicy/communicate.py +1 -1
index a179d95..9b9a09a 100755
@@ -40,7 +40,7 @@ def expand_attribute(attribute):
 def get_types(src, tclass, perm):
     allows=search([sepolicy.ALLOW],{sepolicy.SOURCE:src,sepolicy.CLASS:tclass, sepolicy.PERMS:perm})
     if not allows:
-        raise TypeError("The %s type is not allowed to %s any types" % (src, ",".join(perm)))
+        raise ValueError("The %s type is not allowed to %s any types" % (src, ",".join(perm)))
 
     tlist = []
     for l in map(lambda y: y[sepolicy.TARGET], filter(lambda x: set(perm).issubset(x[sepolicy.PERMS]), allows)):
policycoreutils/sepolicy/sepolicy/generate.py +34 -32

140 lines not on this page · raw message

policycoreutils/sepolicy/sepolicy/interface.py +85 -23

133 lines not on this page · raw message

policycoreutils/sepolicy/sepolicy/manpage.py +60 -73

245 lines not on this page · raw message

policycoreutils/sepolicy/sepolicy/network.py +1 -21
index 66efe26..a446d68 100755
@@ -25,27 +25,6 @@ import sepolicy
 search=sepolicy.search
 info=sepolicy.info
 
-def _gen_port_dict():
-    portrecsbynum = {}
-    portrecs = {}
-    for i in info(sepolicy.PORT):
-        if i['low'] == i['high']:
-            port = str(i['low'])
-        else:
-            port = "%s-%s" % (str(i['low']), str(i['high']))
-
-        if (i['type'], i['protocol']) in portrecs:
-            portrecs [(i['type'], i['protocol'])].append(port)
-        else:
-            portrecs [(i['type'], i['protocol'])] = [port]
-
-        portrecsbynum[(i['low'], i['high'],i['protocol'])] = (i['type'], i['range'])
-    return ( portrecs, portrecsbynum )
-portrecs, portrecsbynum = _gen_port_dict()
-
-port_types =  sepolicy.info(sepolicy.ATTRIBUTE,"port_type")[0]["types"]
-domains =  sepolicy.info(sepolicy.ATTRIBUTE,"domain")[0]["types"]
-
 def get_types(src, tclass, perm):
     allows=search([sepolicy.ALLOW],{sepolicy.SOURCE:src,sepolicy.CLASS:tclass, sepolicy.PERMS:perm})
     nlist=[]
@@ -57,6 +36,7 @@ def get_types(src, tclass, perm):
    
 
 def get_network_connect(src, protocol, perm):
+    portrecs, portrecsbynum = sepolicy.gen_port_dict()
     d={}
     tlist = get_types(src, "%s_socket" % protocol, [perm])
     if len(tlist) > 0:
policycoreutils/sepolicy/sepolicy/templates/test_module.py new file +119 -0

119 lines not on this page · raw message

#5 lore raw
Quoted 4 lines · Daniel J Walsh

You might have found a bug in the current upstream, we don't have this problem in Fedora.

I have attached the current fedora-sepolicy patch.

Apparently, the port information code (in info.c) assumes that MLS is enabled (it calls apol_context_get_range) and errors out if no range is found.

The below "patch" fixes that behavior.

sepolicy/info.c +7 -9
diff -ur policycoreutils-2.1.14.orig/sepolicy/info.c policycoreutils-2.1.14/sepolicy/info.c
@@ -827,16 +827,14 @@
 		if (py_insert_string(dict, "type", type))
 			goto err;
 
-		if((range = apol_context_get_range(c)) == NULL) {
-			goto err;
-		}
-			
-		range_str = apol_mls_range_render(policydb, range);
-		if (range_str == NULL) {
-			goto err;
+		if((range = apol_context_get_range(c)) != NULL) {
+			range_str = apol_mls_range_render(policydb, range);
+			if (range_str == NULL) {
+				goto err;
+			}
+			if (py_insert_string(dict, "range", range_str))
+				goto err;
 		}
-		if (py_insert_string(dict, "range", range_str))
-			goto err;
 
 		if (py_insert_string(dict, "protocol", proto_str))
 			goto err;

Only in policycoreutils-2.1.14/sepolicy/sepolicy: __init__.pyc

sepolicy/sepolicy/network.py +5 -1
diff -ur policycoreutils-2.1.14.orig/sepolicy/sepolicy/network.py policycoreutils-2.1.14/sepolicy/sepolicy/network.py
@@ -39,7 +39,11 @@
         else:
             portrecs [(i['type'], i['protocol'])] = [port]
 
-        portrecsbynum[(i['low'], i['high'],i['protocol'])] = (i['type'], i['range'])
+        if 'range' in i:
+            portrecsbynum[(i['low'], i['high'],i['protocol'])] = (i['type'], i['range'])
+        else:
+            portrecsbynum[(i['low'], i['high'],i['protocol'])] = (i['type'])
+
     return ( portrecs, portrecsbynum )
 portrecs, portrecsbynum = _gen_port_dict()
 

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