blob: cc05b7397ffbcbd0d3d116e79a596798ae67e3e4 [file] [log] [blame]
lh758261d2023-07-13 05:52:04 -07001SUMMARY = "MLS (Multi Level Security) variant of the SELinux policy"
2DESCRIPTION = "\
3This is the reference policy for SE Linux built with MLS support. \
4It allows giving data labels such as \"Top Secret\" and preventing \
5such data from leaking to processes or files with lower classification. \
6"
7
8DEFAULT_ENFORCING ??= "enforcing"
9# DEFAULT_ENFORCING ??= "permissive"
10# DEFAULT_ENFORCING ??= "disabled"
11
12SECTION = "admin"
13LICENSE = "GPLv2"
14LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/zte;md5=c075689d1d1e06d4ab5bbe53623a6808"
15PROVIDES = "virtual/refpolicy"
16RPROVIDES:${PN} = "refpolicy"
17
18POLICY_TYPE = "mls"
19
20SRC_URI = "${@bb.utils.contains('DISTRO_FEATURES', 'procd', 'file://procd-mls', 'file://policy-mls', d)} \
21 "
22# Specific config files for Poky
23SRC_URI += "file://customizable_types \
24 file://setrans-mls.conf \
25 file://setrans-mcs.conf \
26 "
27
28##S = "${WORKDIR}/procd-mls"
29S = "${@bb.utils.contains('DISTRO_FEATURES', 'procd', '${WORKDIR}/procd-mls', '${WORKDIR}/policy-mls', d)}"
30CONFFILES:${PN} += "${sysconfdir}/selinux/config"
31FILES:${PN} += " \
32 ${sysconfdir}/selinux/${POLICY_NAME}/ \
33 ${localstatedir}/lib/selinux/${POLICY_NAME}/ \
34 "
35
36EXTRANATIVEPATH += "bzip2-native"
37
38DEPENDS += "bzip2-replacement-native checkpolicy-native policycoreutils-native semodule-utils-native m4-native"
39
40RDEPENDS:${PN}-dev =+ " \
41 python3-core \
42"
43
44PACKAGE_ARCH = "${MACHINE_ARCH}"
45
46inherit python3native
47
48PARALLEL_MAKE = ""
49POLICY_NAME ?= "${POLICY_TYPE}"
50POLICY_DISTRO ?= "redhat"
51POLICY_UBAC ?= "n"
52POLICY_UNK_PERMS ?= "allow"
53POLICY_DIRECT_INITRC ?= "y"
54POLICY_SYSTEMD ?= "y"
55POLICY_MONOLITHIC ?= "n"
56POLICY_CUSTOM_BUILDOPT ?= ""
57POLICY_QUIET ?= "n"
58POLICY_MLS_SENS ?= "16"
59POLICY_MLS_CATS ?= "1024"
60POLICY_MCS_CATS ?= "1024"
61
62EXTRA_OEMAKE += "NAME=${POLICY_NAME} \
63 TYPE=${POLICY_TYPE} \
64 DISTRO=${POLICY_DISTRO} \
65 UBAC=${POLICY_UBAC} \
66 UNK_PERMS=${POLICY_UNK_PERMS} \
67 DIRECT_INITRC=${POLICY_DIRECT_INITRC} \
68 SYSTEMD=${POLICY_SYSTEMD} \
69 MONOLITHIC=${POLICY_MONOLITHIC} \
70 CUSTOM_BUILDOPT=${POLICY_CUSTOM_BUILDOPT} \
71 QUIET=${POLICY_QUIET} \
72 MLS_SENS=${POLICY_MLS_SENS} \
73 MLS_CATS=${POLICY_MLS_CATS} \
74 MCS_CATS=${POLICY_MCS_CATS}"
75
76EXTRA_OEMAKE += "tc_usrbindir=${STAGING_BINDIR_NATIVE}"
77EXTRA_OEMAKE += "OUTPUT_POLICY=`${STAGING_BINDIR_NATIVE}/checkpolicy -V | cut -d' ' -f1`"
78EXTRA_OEMAKE += "CC='${BUILD_CC}' CFLAGS='${BUILD_CFLAGS}' PYTHON='${PYTHON}'"
79
80python __anonymous () {
81 import re
82
83 # make sure DEFAULT_ENFORCING is something sane
84 if not re.match('^(enforcing|permissive|disabled)$',
85 d.getVar('DEFAULT_ENFORCING'),
86 flags=0):
87 d.setVar('DEFAULT_ENFORCING', 'permissive')
88}
89
90disable_policy_modules () {
91 for module in ${PURGE_POLICY_MODULES} ; do
92 sed -i "s/^\(\<${module}\>\) *= *.*$/\1 = off/" ${S}/policy/modules.conf
93 done
94}
95
96do_compile() {
97 if [ -f "${WORKDIR}/modules.conf" ] ; then
98 cp -f ${WORKDIR}/modules.conf ${S}/policy/modules.conf
99 fi
100 oe_runmake conf
101 disable_policy_modules
102 oe_runmake policy
103}
104
105prepare_policy_store () {
106 oe_runmake 'DESTDIR=${D}' 'prefix=${D}${prefix}' install
107 POL_PRIORITY=100
108 POL_SRC=${D}${datadir}/selinux/${POLICY_NAME}
109 POL_STORE=${D}${localstatedir}/lib/selinux/${POLICY_NAME}
110 POL_ACTIVE_MODS=${POL_STORE}/active/modules/${POL_PRIORITY}
111
112 # Prepare to create policy store
113 mkdir -p ${POL_STORE}
114 mkdir -p ${POL_ACTIVE_MODS}
115
116 # get hll type from suffix on base policy module
117 HLL_TYPE=$(echo ${POL_SRC}/base.* | awk -F . '{if (NF>1) {print $NF}}')
118 HLL_BIN=${STAGING_DIR_NATIVE}${prefix}/libexec/selinux/hll/${HLL_TYPE}
119
120 for i in ${POL_SRC}/*.${HLL_TYPE}; do
121 MOD_NAME=$(basename $i | sed "s/\.${HLL_TYPE}$//")
122 MOD_DIR=${POL_ACTIVE_MODS}/${MOD_NAME}
123 mkdir -p ${MOD_DIR}
124 echo -n "${HLL_TYPE}" > ${MOD_DIR}/lang_ext
125 if ! bzip2 -t $i >/dev/null 2>&1; then
126 ${HLL_BIN} $i | bzip2 --stdout > ${MOD_DIR}/cil
127 bzip2 -f $i && mv -f $i.bz2 $i
128 else
129 bunzip2 --stdout $i | \
130 ${HLL_BIN} | \
131 bzip2 --stdout > ${MOD_DIR}/cil
132 fi
133 cp $i ${MOD_DIR}/hll
134 done
135}
136
137rebuild_policy () {
138 cat <<-EOF > ${D}${sysconfdir}/selinux/semanage.conf
139module-store = direct
140[setfiles]
141path = ${STAGING_DIR_NATIVE}${base_sbindir_native}/setfiles
142args = -q -c \$@ \$<
143[end]
144[sefcontext_compile]
145path = ${STAGING_DIR_NATIVE}${sbindir_native}/sefcontext_compile
146args = \$@
147[end]
148
149policy-version = 31
150EOF
151
152 # Create policy store and build the policy
153 semodule -p ${D} -s ${POLICY_NAME} -n -B
154 rm -f ${D}${sysconfdir}/selinux/semanage.conf
155 # no need to leave final dir created by semanage laying around
156 rm -rf ${D}${localstatedir}/lib/selinux/final
157}
158
159install_misc_files () {
160 cat ${WORKDIR}/customizable_types >> \
161 ${D}${sysconfdir}/selinux/${POLICY_NAME}/contexts/customizable_types
162
163 # install setrans.conf for mls/mcs policy
164 if [ -f ${WORKDIR}/setrans-${POLICY_TYPE}.conf ]; then
165 install -m 0644 ${WORKDIR}/setrans-${POLICY_TYPE}.conf \
166 ${D}${sysconfdir}/selinux/${POLICY_NAME}/setrans.conf
167 fi
168
169 # install policy headers
170 oe_runmake 'DESTDIR=${D}' 'prefix=${D}${prefix}' install-headers
171}
172
173install_config () {
174 echo "\
175# This file controls the state of SELinux on the system.
176# SELINUX= can take one of these three values:
177# enforcing - SELinux security policy is enforced.
178# permissive - SELinux prints warnings instead of enforcing.
179# disabled - No SELinux policy is loaded.
180SELINUX=${DEFAULT_ENFORCING}
181# SELINUXTYPE= can take one of these values:
182# minimum - Minimum Security protection.
183# standard - Standard Security protection.
184# mls - Multi Level Security protection.
185# targeted - Targeted processes are protected.
186# mcs - Multi Category Security protection.
187SELINUXTYPE=${POLICY_NAME}
188" > ${WORKDIR}/config
189 install -d ${D}/${sysconfdir}/selinux
190 install -m 0644 ${WORKDIR}/config ${D}/${sysconfdir}/selinux/
191}
192
193do_install () {
194 prepare_policy_store
195 rebuild_policy
196 install_misc_files
197 install_config
198 echo "${S}/image${datadir}/selinux/${POLICY_NAME}/*.pp "
199
200 rm -fr ${WORKDIR}/image/usr
201 rm -fr ${WORKDIR}/image/var
202}
203
204sysroot_stage_all:append () {
205 sysroot_stage_dir ${D}${sysconfdir} ${SYSROOT_DESTDIR}${sysconfdir}
206}