]> git.ipfire.org Git - people/stevee/selinux-policy.git/blame - refpolicy/Makefile
initial commit
[people/stevee/selinux-policy.git] / refpolicy / Makefile
CommitLineData
88d14a22
CP
1#
2# Makefile for the security policy.
3#
4# Targets:
5#
6# install - compile and install the policy configuration, and context files.
7# load - compile, install, and load the policy configuration.
8# reload - compile, install, and load/reload the policy configuration.
9# relabel - relabel filesystems based on the file contexts configuration.
10# checklabels - check filesystems against the file context configuration
11# restorelabels - check filesystems against the file context configuration
12# and restore the label of files with incorrect labels
13# policy - compile the policy configuration locally for testing/development.
14#
15# The default target is 'policy'.
16#
17
b4cd1533
CP
18########################################
19#
20# Configurable portions of the Makefile
21#
22
b67488e3
CP
23# Override default policy version
24OUTPUT_POLICY = 18
134191be 25
b67488e3
CP
26# Policy Type
27# strict, targeted, strict-mls, targeted-mls
28# strict and strict-mls are currently supported.
29TYPE = strict
b4cd1533 30
b67488e3
CP
31# If set, this will be used as the policy
32# name. Otherwise the policy type will be
33# used for the name.
34NAME =
35
36# Build monolithic policy. Putting n here
37# will build a loadable module policy.
38# Only monolithic policies are currently supported.
39MONOLITHIC=y
b4cd1533
CP
40
41# Uncomment this to disable command echoing
42#QUIET:=@
43
44########################################
45#
b67488e3 46# NO OPTIONS BELOW HERE
b4cd1533
CP
47#
48
49# executable paths
50PREFIX := /usr
51BINDIR := $(PREFIX)/bin
52SBINDIR := $(PREFIX)/sbin
53CHECKPOLICY := $(BINDIR)/checkpolicy
547283e2 54LOADPOLICY := $(SBINDIR)/load_policy
b4cd1533 55SETFILES := $(SBINDIR)/setfiles
0447352a 56
3a80ec29
CP
57SUPPORT := support
58GENDOC := $(SUPPORT)/sedoctool.py
254bbc7b 59FCSORT := $(SUPPORT)/fc_sort
84eb353c 60SETTUN := $(SUPPORT)/set_tunables
3a80ec29 61
004db90d 62XMLLINT := $(BINDIR)/xmllint
b4cd1533 63
134191be 64# enable MLS if requested.
b67488e3 65ifneq ($(findstring mls,$(TYPE)),)
134191be
CP
66 override M4PARAM += -D enable_mls
67 CHECKPOLICY += -M
68endif
69
b67488e3
CP
70# compile targeted policy if requested.
71ifneq ($(findstring targeted,$(TYPE)),)
72 override M4PARAM += -D targeted_policy
73endif
74
75ifeq ($(MONOLITHIC),y)
76 override M4PARAM += -D monolithic_policy
77endif
78
79ifneq ($(OUTPUT_POLICY),)
80 CHECKPOLICY += -c $(OUTPUT_POLICY)
81endif
82
83ifeq ($(NAME),)
84 NAME := $(TYPE)
134191be
CP
85endif
86
b4cd1533 87# determine the policy version and current kernel version if possible
134191be 88PV := $(shell $(CHECKPOLICY) -V |cut -f 1 -d ' ')
b4cd1533
CP
89KV := $(shell cat /selinux/policyvers)
90
91# dont print version warnings if we are unable to determine
92# the currently running kernel's policy version
93ifeq ($(KV),)
134191be 94 KV := $(PV)
b4cd1533
CP
95endif
96
97FC := file_contexts
98POLVER := policy.$(PV)
b4cd1533
CP
99
100# install paths
101TOPDIR = $(DESTDIR)/etc/selinux
b67488e3 102INSTALLDIR = $(TOPDIR)/$(NAME)
b4cd1533
CP
103POLICYPATH = $(INSTALLDIR)/policy
104SRCPATH = $(INSTALLDIR)/src
105USERPATH = $(INSTALLDIR)/users
106CONTEXTPATH = $(INSTALLDIR)/contexts
107LOADPATH = $(POLICYPATH)/$(POLVER)
108FCPATH = $(CONTEXTPATH)/files/file_contexts
109HOMEDIRPATH = $(CONTEXTPATH)/files/homedir_template
110
b67488e3
CP
111POLDIR = policy
112MODDIR = $(POLDIR)/modules
113
114BASE_MODULE = $(MODDIR)/kernel
115FLASKDIR = $(POLDIR)/flask
09693356 116APPCONF = config/appconfig
0350b1dc 117M4SUPPORT = $(wildcard $(POLDIR)/support/*.spt)
b4cd1533 118
84eb353c 119GLOBALTUN := $(POLDIR)/global_tunables
0350b1dc 120MOD_DISABLE := $(POLDIR)/modules.conf
84eb353c 121TUNABLES := $(POLDIR)/tunables.conf
0fbe15dc 122
c9a26b3e
CP
123APPDIR := $(CONTEXTPATH)
124APPFILES := $(addprefix $(APPDIR)/,default_contexts default_type initrc_context failsafe_context userhelper_context removable_context dbus_contexts customizable_types) $(CONTEXTPATH)/files/media
2926f9c7 125CONTEXTFILES += $(wildcard $(APPCONF)/*_context*) $(APPCONF)/media
b67488e3 126USER_FILES := $(POLDIR)/users
c9a26b3e 127
e12e5738
CP
128DISABLEMOD := $(foreach mod,$(shell egrep -v '^[[:blank:]]*\#' $(MOD_DISABLE)),$(subst ./,,$(shell find -iname $(mod).te)))
129ALL_LAYERS := $(shell find $(wildcard policy/modules/*) -maxdepth 0 -type d)
130
131GENERATED_TE := $(basename $(foreach dir,$(ALL_LAYERS),$(wildcard $(dir)/*.te.in)))
132GENERATED_IF := $(basename $(foreach dir,$(ALL_LAYERS),$(wildcard $(dir)/*.if.in)))
133GENERATED_FC := $(basename $(foreach dir,$(ALL_LAYERS),$(wildcard $(dir)/*.fc.in)))
134
135DETECTED_MODS := $(foreach dir,$(ALL_LAYERS),$(wildcard $(dir)/*.te)) $(GENERATED_TE)
0fbe15dc 136ALL_MODULES := $(filter-out $(DISABLEMOD),$(DETECTED_MODS))
b4cd1533 137
b67488e3 138PRE_TE_FILES := $(addprefix $(FLASKDIR)/,security_classes initial_sids access_vectors) $(M4SUPPORT) $(POLDIR)/mls
0fbe15dc
CP
139ALL_INTERFACES := $(ALL_MODULES:.te=.if)
140ALL_TE_FILES := $(ALL_MODULES)
b67488e3 141POST_TE_FILES := $(POLDIR)/users $(POLDIR)/constraints
b4cd1533 142
0fbe15dc 143ALL_FC_FILES := $(ALL_MODULES:.te=.fc)
92e928e1 144HOMEDIR_TEMPLATE = tmp/homedir_template
b4cd1533 145
84eb353c 146POLICY_SECTIONS := tmp/pre_te_files.conf tmp/generated_definitions.conf tmp/all_interfaces.conf tmp/all_attrs_types.conf $(GLOBALTUN) tmp/only_te_rules.conf tmp/all_post.conf
b4cd1533 147
20030ef5 148DOCS = doc
12b559a4 149POLXML = $(DOCS)/policy.xml
20030ef5
CP
150XMLDTD = $(DOCS)/policy.dtd
151HTMLDIR = $(DOCS)/html
152DOCTEMPLATE = $(DOCS)/templates
36e54b81 153
b4cd1533
CP
154########################################
155#
156# default action: build policy locally
157#
158default: policy
159
160policy: $(POLVER)
161
44cda51b 162install: $(LOADPATH) $(FCPATH) $(APPFILES) $(USERPATH)/local.users
c9a26b3e
CP
163
164load: tmp/load
b4cd1533
CP
165
166########################################
167#
168# Build a binary policy locally
169#
170$(POLVER): policy.conf
b67488e3 171 @echo "Compiling $(NAME) $(POLVER)"
b4cd1533
CP
172ifneq ($(PV),$(KV))
173 @echo
b67488e3 174 @echo "WARNING: Policy version mismatch! Is your OUTPUT_POLICY set correctly?"
b4cd1533
CP
175 @echo
176endif
134191be 177 $(QUIET) $(CHECKPOLICY) $^ -o $@
b4cd1533
CP
178
179########################################
180#
181# Install a binary policy
182#
183$(LOADPATH): policy.conf
184 @mkdir -p $(POLICYPATH)
b67488e3 185 @echo "Compiling and installing $(NAME) $(LOADPATH)"
b4cd1533
CP
186ifneq ($(PV),$(KV))
187 @echo
b67488e3 188 @echo "WARNING: Policy version mismatch! Is your OUTPUT_POLICY set correctly?"
b4cd1533
CP
189 @echo
190endif
134191be 191 $(QUIET) $(CHECKPOLICY) $^ -o $@
b4cd1533 192
88d14a22
CP
193########################################
194#
195# Load the binary policy
196#
197reload tmp/load: $(LOADPATH) $(FCPATH)
b67488e3 198 @echo "Loading $(NAME) $(LOADPATH)"
88d14a22
CP
199 $(QUIET) $(LOADPOLICY) -q $(LOADPATH)
200 @touch tmp/load
201
b4cd1533
CP
202########################################
203#
204# Construct a monolithic policy.conf
205#
206policy.conf: $(POLICY_SECTIONS)
b67488e3 207 @echo "Creating $(NAME) policy.conf"
84eb353c 208 # checkpolicy can use the #line directives provided by -s for error reporting:
b67488e3 209 $(QUIET) m4 $(M4PARAM) -s $^ > tmp/$@.tmp
b4cd1533 210 $(QUIET) sed -e /^portcon/d -e /^nodecon/d -e /^netifcon/d < tmp/$@.tmp > $@
84eb353c 211 # the ordering of these ocontexts matters:
b4cd1533
CP
212 $(QUIET) grep ^portcon tmp/$@.tmp >> $@ || true
213 $(QUIET) grep ^netifcon tmp/$@.tmp >> $@ || true
214 $(QUIET) grep ^nodecon tmp/$@.tmp >> $@ || true
215
216tmp/pre_te_files.conf: $(PRE_TE_FILES)
217 @test -d tmp || mkdir -p tmp
218 $(QUIET) cat $^ > $@
219
e12e5738 220tmp/generated_definitions.conf: $(ALL_LAYERS) $(ALL_TE_FILES) $(TUNABLES)
84eb353c 221 # per-userdomain templates:
b4cd1533
CP
222 @test -d tmp || mkdir -p tmp
223 $(QUIET) echo "define(\`per_userdomain_templates',\`" > $@
0fbe15dc 224 $(QUIET) for i in $(patsubst %.te,%,$(notdir $(ALL_MODULES))); do \
b4cd1533
CP
225 echo "ifdef(\`""$$i""_per_userdomain_template',\`""$$i""_per_userdomain_template("'$$1'")')" \
226 >> $@ ;\
227 done
228 $(QUIET) echo "')" >> $@
84eb353c 229 # define foo.te
0fbe15dc 230 $(QUIET) for i in $(notdir $(ALL_MODULES)); do \
b4cd1533
CP
231 echo "define(\`$$i')" >> $@ ;\
232 done
84eb353c 233 $(QUIET) $(SETTUN) $(TUNABLES) >> $@
b4cd1533 234
3a80ec29 235tmp/all_interfaces.conf: $(ALL_INTERFACES)
b4cd1533 236 @test -d tmp || mkdir -p tmp
3a80ec29 237 $(QUIET) cat $^ > $@
b4cd1533 238
3a80ec29 239tmp/all_te_files.conf: $(ALL_TE_FILES)
b4cd1533 240 @test -d tmp || mkdir -p tmp
3a80ec29 241 $(QUIET) cat $^ > $@
b4cd1533
CP
242
243tmp/post_te_files.conf: $(POST_TE_FILES)
244 @test -d tmp || mkdir -p tmp
245 $(QUIET) cat $^ > $@
246
247# extract attributes and put them first. extract post te stuff
248# like genfscon and put last. portcon, nodecon, and netifcon
249# is delayed since they are generated by m4
f66a1af9
CP
250tmp/all_attrs_types.conf tmp/only_te_rules.conf tmp/all_post.conf: tmp/all_te_files.conf tmp/post_te_files.conf
251 $(QUIET) grep ^attribute tmp/all_te_files.conf > tmp/all_attrs_types.conf || true
252 $(QUIET) grep '^type ' tmp/all_te_files.conf >> tmp/all_attrs_types.conf
b4cd1533 253 $(QUIET) cat tmp/post_te_files.conf > tmp/all_post.conf
cabfa520
CP
254 $(QUIET) grep '^sid ' tmp/all_te_files.conf >> tmp/all_post.conf || true
255 $(QUIET) egrep '^fs_use_(xattr|task|trans)' tmp/all_te_files.conf >> tmp/all_post.conf || true
b4cd1533 256 $(QUIET) grep ^genfscon tmp/all_te_files.conf >> tmp/all_post.conf || true
cabfa520
CP
257 $(QUIET) sed -r -e /^attribute/d -e '/^type /d' -e /^genfscon/d \
258 -e '/^sid /d' -e '/^fs_use_(xattr|task|trans)/d' \
259 < tmp/all_te_files.conf > tmp/only_te_rules.conf
b4cd1533 260
f2e4ab3a 261$(MODDIR)/kernel/corenetwork.if: $(MODDIR)/kernel/corenetwork.if.m4 $(MODDIR)/kernel/corenetwork.if.in
61bbe531
CP
262 @echo "#" > $@
263 @echo "# This is a generated file! Instead of modifying this file, the" >> $@
264 @echo "# $(notdir $@).in or $(notdir $@).m4 file should be modified." >> $@
265 @echo "#" >> $@
266 $(QUIET) cat $(MODDIR)/kernel/corenetwork.if.in >> $@
f2e4ab3a
CP
267 $(QUIET) egrep "^[[:blank:]]*network_(interface|node|port)\(.*\)" $(@:.if=.te).in \
268 | m4 $(M4PARAM) -D monolithic_policy $(M4SUPPORT) $(MODDIR)/kernel/corenetwork.if.m4 - \
269 | sed -e 's/dollarsone/\$$1/g' -e 's/dollarszero/\$$0/g' >> $@
270 $(QUIET) echo "## </module>" >> $@
271
272$(MODDIR)/kernel/corenetwork.te: $(MODDIR)/kernel/corenetwork.te.m4 $(MODDIR)/kernel/corenetwork.te.in
61bbe531
CP
273 @echo "#" > $@
274 @echo "# This is a generated file! Instead of modifying this file, the" >> $@
275 @echo "# $(notdir $@).in or $(notdir $@).m4 file should be modified." >> $@
276 @echo "#" >> $@
f2e4ab3a 277 $(QUIET) m4 $(M4PARAM) -D monolithic_policy $(M4SUPPORT) $^ \
61bbe531 278 | sed -e 's/dollarsone/\$$1/g' -e 's/dollarszero/\$$0/g' >> $@
f2e4ab3a 279
88d14a22
CP
280########################################
281#
282# Remove the dontaudit rules from the policy.conf
283#
284enableaudit: policy.conf
285 @test -d tmp || mkdir -p tmp
286 @echo "Removing dontaudit rules from policy.conf"
287 $(QUIET) grep -v dontaudit policy.conf > tmp/policy.audit
288 $(QUIET) mv tmp/policy.audit policy.conf
289
6d314fd3
CP
290########################################
291#
292# Construct file_contexts
293#
92e928e1 294$(FC): $(M4SUPPORT) tmp/generated_definitions.conf $(ALL_FC_FILES) $(FCSORT)
6d314fd3 295 @test -d tmp || mkdir -p tmp
92e928e1
CP
296 $(QUIET) m4 $(M4PARAM) $(M4SUPPORT) tmp/generated_definitions.conf $(ALL_FC_FILES) > tmp/$@.tmp
297 $(QUIET) grep -e HOME -e ROLE tmp/$@.tmp > $(HOMEDIR_TEMPLATE)
298 $(QUIET) sed -i -e /HOME/d -e /ROLE/d tmp/$@.tmp
299 $(QUIET) $(FCSORT) tmp/$@.tmp $@
6d314fd3 300
547283e2
CP
301########################################
302#
303# Install file_contexts
304#
3a80ec29 305$(FCPATH): $(FC) $(USERPATH)/system.users
547283e2
CP
306 @mkdir -p $(CONTEXTPATH)/files
307 $(QUIET) install -m 644 $(FC) $(FCPATH)
92e928e1
CP
308 $(QUIET) install -m 644 $(HOMEDIR_TEMPLATE) $(HOMEDIRPATH)
309 $(QUIET) $(GENHOMEDIRCON) -d $(TOPDIR) -t $(NAME) $(USEPWD)
547283e2 310
b4cd1533
CP
311########################################
312#
313# Filesystem labeling
314#
88d14a22 315FILESYSTEMS := `mount | grep -v "context=" | egrep -v '\((|.*,)bind(,.*|)\)' | awk '/(ext[23]| xfs| jfs).*rw/{print $$3}';`
b4cd1533 316
88d14a22
CP
317checklabels: $(FC) $(SETFILES)
318 @if test -z "$(FILESYSTEMS)"; then \
319 echo "No filesystems with extended attributes found!" ;\
320 false ;\
321 fi
b4cd1533
CP
322 $(QUIET) $(SETFILES) -v -n $(FC) $(FILESYSTEMS)
323
88d14a22
CP
324restorelabels: $(FC) $(SETFILES)
325 @if test -z "$(FILESYSTEMS)"; then \
326 echo "No filesystems with extended attributes found!" ;\
327 false ;\
328 fi
b4cd1533
CP
329 $(QUIET) $(SETFILES) -v $(FC) $(FILESYSTEMS)
330
331relabel: $(FC) $(SETFILES)
88d14a22
CP
332 @if test -z "$(FILESYSTEMS)"; then \
333 echo "No filesystems with extended attributes found!" ;\
334 false ;\
335 fi
b4cd1533
CP
336 $(QUIET) $(SETFILES) $(FC) $(FILESYSTEMS)
337
84eb353c
CP
338########################################
339#
340# Create config files
341#
12b559a4 342conf $(MOD_DISABLE) $(TUNABLES): $(POLXML)
84eb353c 343 @echo "Creating $(MOD_DISABLE) and $(TUNABLES)"
12b559a4 344 $(QUIET) cd $(DOCS) && ../$(GENDOC) -t ../$(TUNABLES) -m ../$(MOD_DISABLE) -x ../$(POLXML)
84eb353c 345
6d314fd3
CP
346########################################
347#
348# Documentation generation
349#
350
84eb353c
CP
351# no dependencies here, because we don't want to rebuild
352# this and its dependents every time the dependencies
353# change
12b559a4 354$(POLXML): $(GENERATED_IF)
88c72f44 355 @echo "Creating $@"
84eb353c 356 @mkdir -p tmp
004db90d
CP
357 $(QUIET) echo '<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>' > $@
358 $(QUIET) echo '<!DOCTYPE policy SYSTEM "policy.dtd">' >> $@
359 $(QUIET) echo "<policy>" >> $@
44cda51b 360# process this through m4 to eliminate the generated definitions templates.
3c62aa31 361# currently these are only in corenetwork.if
84eb353c 362 $(QUIET) m4 $(ALL_INTERFACES) $(GLOBALTUN) | egrep -h "^##[[:blank:]]" | sed -e 's/^##[[:blank:]]//g' >> $@
88c72f44 363 $(QUIET) echo "</policy>" >> $@
0447352a
CP
364 $(QUIET) if test -x $(XMLLINT) && test -f $(XMLDTD); then \
365 $(XMLLINT) --noout --dtdvalid $(XMLDTD) $@ ;\
004db90d 366 fi
6d314fd3 367
12b559a4 368html: $(POLXML)
20030ef5 369 @mkdir -p $(HTMLDIR)
12b559a4 370 $(QUIET) cd $(DOCS) && ../$(GENDOC) -d ../$(HTMLDIR) -T ../$(DOCTEMPLATE) -x ../$(POLXML)
20030ef5
CP
371 $(QUIET) cp $(DOCTEMPLATE)/*.css $(HTMLDIR)
372
c9a26b3e
CP
373########################################
374#
375# Runtime binary policy patching of users
376#
377$(USERPATH)/system.users: $(USER_FILES) tmp/generated_definitions.conf
378 @mkdir -p $(USERPATH)
379 @echo "Installing system.users"
380 @echo "# " > tmp/system.users
381 @echo "# Do not edit this file. " >> tmp/system.users
382 @echo "# This file is replaced on reinstalls of this policy." >> tmp/system.users
383 @echo "# Please edit local.users to make local changes." >> tmp/system.users
384 @echo "#" >> tmp/system.users
385 $(QUIET) m4 $(M4PARAM) tmp/generated_definitions.conf $(USER_FILES) | \
386 egrep -v "^[[:space:]]*($$|#)" >> tmp/system.users
387 $(QUIET) install -m 644 tmp/system.users $@
388
389$(USERPATH)/local.users: local.users
390 @mkdir -p $(USERPATH)
391 @echo "Installing local.users"
392 $(QUIET) install -C -b -m 644 $< $@
393
394########################################
395#
396# Appconfig files
397#
398install-appconfig: $(APPFILES)
399
2926f9c7 400$(CONTEXTPATH)/files/media: $(APPCONF)/media
c9a26b3e
CP
401 mkdir -p $(CONTEXTPATH)/files/
402 install -m 644 $< $@
403
2926f9c7 404$(APPDIR)/default_contexts: $(APPCONF)/default_contexts
c9a26b3e
CP
405 mkdir -p $(APPDIR)
406 install -m 644 $< $@
407
2926f9c7 408$(APPDIR)/removable_context: $(APPCONF)/removable_context
c9a26b3e
CP
409 mkdir -p $(APPDIR)
410 install -m 644 $< $@
411
412$(APPDIR)/customizable_types: policy.conf
413 mkdir -p $(APPDIR)
414 @grep "^type .*customizable" $< | cut -d',' -f1 | cut -d' ' -f2 > tmp/customizable_types
415 install -m 644 tmp/customizable_types $@
416
2926f9c7 417$(APPDIR)/default_type: $(APPCONF)/default_type
c9a26b3e
CP
418 mkdir -p $(APPDIR)
419 install -m 644 $< $@
420
2926f9c7 421$(APPDIR)/userhelper_context: $(APPCONF)/userhelper_context
c9a26b3e
CP
422 mkdir -p $(APPDIR)
423 install -m 644 $< $@
424
2926f9c7 425$(APPDIR)/initrc_context: $(APPCONF)/initrc_context
c9a26b3e
CP
426 mkdir -p $(APPDIR)
427 install -m 644 $< $@
428
2926f9c7 429$(APPDIR)/failsafe_context: $(APPCONF)/failsafe_context
c9a26b3e
CP
430 mkdir -p $(APPDIR)
431 install -m 644 $< $@
432
2926f9c7 433$(APPDIR)/dbus_contexts: $(APPCONF)/dbus_contexts
c9a26b3e
CP
434 mkdir -p $(APPDIR)
435 install -m 644 $< $@
436
2926f9c7 437$(APPDIR)/users/root: $(APPCONF)/root_default_contexts
c9a26b3e
CP
438 mkdir -p $(APPDIR)/users
439 install -m 644 $< $@
440
b4cd1533
CP
441clean:
442 rm -fR tmp
443 rm -f policy.conf
444 rm -f policy.$(PV)
445 rm -f $(FC)
446
0fbe15dc 447bare: clean
12b559a4 448 rm -f $(POLXML)
254bbc7b
CP
449 rm -f $(SUPPORT)/*.pyc
450 rm -f $(FCSORT)
0fbe15dc
CP
451 rm -f $(MOD_DISABLE)
452 rm -f $(TUNABLES)
20030ef5 453 rm -fR $(HTMLDIR)
e12e5738
CP
454ifneq ($(GENERATED_TE),)
455 rm -f $(GENERATED_TE)
456endif
457ifneq ($(GENERATED_IF),)
458 rm -f $(GENERATED_IF)
459endif
460ifneq ($(GENERATED_FC),)
461 rm -f $(GENERATED_FC)
462endif
0fbe15dc
CP
463
464.PHONY: default policy install reload enableaudit checklabels restorelabels relabel conf clean bare