Chris PeBenito 3d76be

Getting Started

Chris PeBenito 3d76be

Chris PeBenito 3d76be
This guide will walk you through the basics of creating a new reference policy module. This will also serve as an introduction to the basics concepts and philosophy of refpolicy.
Chris PeBenito 3d76be

Chris PeBenito 3d76be

Creating A Module

Chris PeBenito 3d76be

Chris PeBenito 3d76be
Modules are the principal organizing component in refpolicy. They are conceptually similar to the source modules in the current strict and targeted policy, but have additional meaning. A module contains the policy for an application or related group of applications, private and shared resources, labeling information, and interfaces that allow other modules access to the modules resources. The majority of the global policy has been eliminated in refpolicy. Certain policy components, like users and object classes, are still global in refpolicy, but almost all TE policy is now contained within a module. 
Chris PeBenito 3d76be

Chris PeBenito 3d76be

Chris PeBenito 3d76be
Modules should be placed in refpolicy/policy/LAYER, where LAYER is the name of the appropriate layer. The current layers can be seen in the interface documentation. Let's create a new module in the services layer called myapp. This is done by creating three files: myapp.te, mayapp.fc, and myapp.if, all in directory refpolicy/policy/services. The file myapp.te file will contain all of the policy private to this module, including any types or attributes. The file myapp.fc file will containt the file context labeling statement for this module. Finally, the file myapp.if will contain the inferfaces for this module (interfaces will be explained below).
Chris PeBenito 3d76be

Chris PeBenito 3d76be

Module TE Policy

Chris PeBenito 3d76be

Chris PeBenito 3d76be
First, let's create myapp.te and add the following:
Chris PeBenito 3d76be
Chris PeBenito 3d76be
Chris PeBenito 3d76be
# Private type declarations
Chris PeBenito 3d76be
type myapp_t;
Chris PeBenito 3d76be
type myapp_exec_t;
Chris PeBenito 3d76be
type myapp_log_t;
Chris PeBenito 3d76be
type myapp_tmp_t;
Chris PeBenito 3d76be
Chris PeBenito 3d76be
domain_type(myapp_t)
Chris PeBenito 3d76be
domain_entry_file(myapp_t, myapp_exec_t)
Chris PeBenito 3d76be
logging_log_file(myapp_log_t)
Chris PeBenito 3d76be
files_tmp_file(myapp_tmp_t)
Chris PeBenito 3d76be
Chris PeBenito 3d76be
Chris PeBenito 3d76be

Chris PeBenito 3d76be

Chris PeBenito 3d76be
This creates all fo the types needed for this module, including a type for the process, executables, log files, and temporary files. The first thing to notice is that there are no attributes applied to any of these types. In refpolicy all types and attributes can only be referred to in the module that declares them. This means that it is not possible, for example, to directly refer to the domain attribute. Instead, macros in other modules are used to declare that a type will be used for a certain purpose. These macros will likely use attributes (but not necessarily), but it allows the module that declared the attribute to strictly control how it can be used. In this example interfaces are used to transform the types into a domain, entry file, log file, and temporary file.
Chris PeBenito 3d76be

Chris PeBenito 3d76be

Chris PeBenito 3d76be
Let's expand this example further by allowing some access for these types. My application needs access between it's own types and access to read random numbers. The access between private types is written exactly the same way current policy rules are written, i.e.:
Chris PeBenito 3d76be
Chris PeBenito 3d76be
Chris PeBenito 3d76be
allow myapp_t myapp_log_t : file ra_file_perms;
Chris PeBenito 3d76be
allow myapp_t myapp_tmp_t : file rw_file_perms;
Chris PeBenito 3d76be
Chris PeBenito 3d76be
Chris PeBenito 3d76be