<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sections SYSTEM "/dtd/book.dtd">

<!-- The content of this document is licensed under the CC-BY-SA license -->
<!-- See http://creativecommons.org/licenses/by-sa/1.0 -->

<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/proj/en/hardened/selinux/hb-intro-enhancingsecurity.xml,v 1.3 2011/06/07 19:46:52 klondike Exp $ -->

<sections>
<version>2</version>
<date>2011-05-25</date>

<section>
<title>Introduction</title>
<subsection>
<title>A Warm Welcome</title>
<body>

<p>
Welcome to the Gentoo SELinux handbook. In this resource, we will bring you up 
to speed with Gentoo Hardened's implementation of SELinux and the policies
involved. Part of this exercise is to help you understand why SELinux was
brought to life and which concept is behind the development of the SELinux
patches. We will cover the SELinux concepts, the reference policy that Gentoo
Hardened uses and elaborate on how to work with the various SELinux tools.
</p>

<p>
The purpose of this book is not to explain SELinux itself in great detail. There
are many references available on the Internet and in the better bookstores that
help you with the SELinux topic. Instead, we will focus on SELinux integration
within Gentoo Hardened. Of course, we will give a quick introduction to SELinux
to allow you to understand how it works, what it is and help you identify which
actions you will need to take in order to properly secure your system using the
SELinux tools.
</p>

</body>
</subsection>
</section>
<section>
<title>Securing Linux</title>
<subsection>
<title>Security In General</title>
<body>

<p>
Security is often seen as a vague concept. What is security in general? How do
you measure security? What is the benefit and how do you make sure you do not
put too much effort in securing your system?
</p>

<p>
Well, security zealots will tell you that there is no such thing as too much
security. If properly implemented, security does not restrict functionality or
performance. It does not give you too much overhead in order to do your tasks.
But implementing security properly is a different and time-consuming task. That
is also why you often hear that security is as good as its administrator.
</p>

<p>
So, how can you look at security? A good practice on security is to define your
security goals. List what you want to achieve and why. By tracking the threats
that you want to minimize, you build up a security model that is appropriate for
your environment. Such threats can be very broad, such as "Ensure no-one is able
to work around our security measures".
</p>

<p>
In case of a Linux system powered with SELinux, this would at least mean that
you want to protect critical system files, such as kernel image(s) and boot
loader configuration, passwords and the SELinux policy binary itself from being
written by anyone or anything except trusted processes.
</p>

</body>
</subsection>
<!-- Suggestion to remove from guide, more interesting for a generic security
document? 
<subsection>
<title>Security on Operating System Level</title>
<body>

<p>
So far for the high-level and theoretic explanation on security. What about
operating system security?
</p>

<p>
On the Internet, you will find a multitude of documents helping you secure your
system. Some of these documents are procedure-driven (execute this, delete that,
change permissions of this file and that file) and are often found as security
best practices for operating systems or distributions. You can find such
documents on the project sites themselves (such as the <uri
link="/doc/en/security/security-handbook.xml">Gentoo Security Handbook</uri>) or
on specialized sites of organizations that keep track of secure configuration
baselines in general (such as <uri
link="http://www.cisecurity.org">CISecurity</uri>'s benchmark documents).
Others are higher-level descriptions (often called frameworks) that help you
focus on the various fields in which security plays a role.
</p>

<p>
A simple example of such higher-level descriptions can be seen in the CoBIT
framework, which has a process called <e>Ensure Systems Security</e> which
defines (at least) the following control objectives:
</p>

<ol>
  <li>Manage Security Measures</li>
  <li>Identification, Authentication and Access</li>
  <li>Security of Online Access to Data</li>
  <li>User Account Management</li>
  <li>Management Review of User Accounts</li>
  <li>User Control of User Accounts</li>
  <li>Security Surveillance</li>
  <li>Data Classification</li>
  <li>Central Identification and Access Rights Management</li>
  <li>Violation and Security Activity Reports</li>
  <li>Incident Handling</li>
  <li>Re-accreditation</li>
  <li>Counterparty Trust</li>
  <li>Transaction Authorization</li>
  <li>Non-repudiation</li>
  <li>Trusted Path</li>
  <li>Protection of Security Functions</li>
  <li>Cryptographic Key Management</li>
  <li>Malicious Software Preventions, Detection and Correction</li>
  <li>Firewall Architectures and Connections with Public Networks</li>
  <li>Protection of Electronic Value</li>
</ol>

<p>
If your head is not spinning yet, then I suggest to dive deeper into these
subjects. However, for this handbook, we'll leave it at this and focus on those
topics that are relevant for further SELinux discussions.
</p>

</body>
</subsection>
<subsection>
<title>Operating System Security Best Practices</title>
<body>

<p>
To properly secure any operating system, there are a few best practices that you
might need to keep in mind. They do not cover the full spectrum of configuration
directives or requirements, but if you do not implement those properly, you risk
that the threats facing your system become reality faster.
</p>

<dl>
  <dt>Only run necessary services</dt>
  <dd>
    Only run services (scripts, daemons, jobs, servers ...) of which you know
    you need them. Regularly verify your system runtime behavior to see if no
    services are running that you don't need. The more services that run, the
    more <e>access vectors</e> intruders or malicious people have towards your
    system.
  </dd>
  <dt>Update your system regularly</dt>
  <dd>
    Updating your system will resolve all potential vulnerabilities of software
    you have if they were known by the developers and fixed in later versions.
    Some distributions, including Gentoo, allow you to pull in only
    security-related updates so that your upgrade cycle is not too time and
    resource consuming. See <c>glsa-check</c> for more information on how to do
    this with Gentoo.
  </dd>
  <dt>Do not use privileged accounts</dt>
  <dd>
    For each task you execute on a system, make sure that that task has the
    least amount of privileges needed to get to its goal. Only a few services
    will require root privileges (Unix/Linux' highest privileged account), but
    other accounts might also be seen as privileged (such as accounts that have
    password-less <c>sudo</c> rights, or accounts that are in the <e>wheel</e>
    group. The same is true for your regular day-to-day tasks.
  </dd>
  <dt>Implement a good password policy</dt>
  <dd>
    Make sure that your passwords are not easy to guess or to brute-force. If
    your system uses passwords for authentication and the password is
    compromised, security is completely compromised as well as, as far as the
    system knows, the malicious user that is using your password is you.
  </dd>
  <dt>Configure your services properly</dt>
  <dd>
    Do not trust services to come with a good, default configuration. Most
    default configurations are so that the majority of users can use the service
    (feature-wise), which might not always be the proper configuration for you.
  </dd>
  <dt>Use proper permissions</dt>
  <dd>
    Make sure your files are properly protected permission-wise. Never use
    world-readable files and only allow other accounts to read (or modify) your
    file(s) if they need to. Use group-permissions wisely and often validate
    group membership. If file systems can be used in a read-only fashion, do so.
    If you do not need to access a particular file system by default, don't
    mount it.
  </dd>
</dl>

<p>
This is just a subset of best practices. One of the aspects within an operating
system setup is the method of <e>accessing</e> the system, services or data.
Implementing a good access control is mandatory from a systems' security
point-of-view. 
</p>

</body>
</subsection>
-->
<subsection>
<title>Access Control</title>
<body>

<p>
A decent access control system (or group of systems) ensures that only
authorized individuals or processes are granted access to the resources they are
tring to work with.
</p>

<p>
Before one can implement an access control system, you first need to have proper
authentication in place. If your authentication schemes are flawed, your access
control system might not be able to differentiate legitimate users from 
malicious ones.
</p>

<p>
Authenticating users within Linux is often done through PAM (<e>Pluggable
Authentication Modules</e>), a powerful mechanism to integrate multiple
low-level authentication schemes into a high-level interface.
</p>

<p>
Authorizing access to resources however is often done through a simple
permission scheme. Most resources are not hidden by default, although 
patches and updates exist (such as those offered by Gentoo Hardened's 
kernel sources with grSecurity patches which includes support for this 
kind of measures). File-system wise, you can hide the existence of files 
by ensuring the directory in which the file resides is not readable nor 
"executable" by unauthorized accounts.
</p>

<p>
This default permission scheme has major drawbacks. It does not allow you to
define very flexible authorizations (it only allows permissions on three levels:
owner, group-owner and everybody else) and is limited to read/write/execute
rights (although a few additional attributes are supported nowadays as well).
</p>

<p>
Another drawback is that the permission scheme is <e>discretionary</e>, meaning
that users and processes are able to change the security policy in place.
</p>

<p>
For the majority of uses, this permission scheme is sufficient and has proven to
offer a decent method for managing access authorizations. But the drawbacks have
shown to be a major hole in the Linux' offering.
</p>

</body>
</subsection>
</section>
<section>
<title>Mandatory Access Control</title>
<subsection>
<title>Enter SELinux</title>
<body>

<p>
If the above mentioned discretionary access control, abbreviated to <e>DAC</e>,
is not sufficient (and if you are keen on security, you will not find it
sufficient), you need a <e>Mandatory</e> Access Control, or <e>MAC</e> system.
</p>

<p>
When using a MAC system, activities that a process wants to perform on another
resource need to be explicitly allowed. It offers a higher granularity on
permissions as well as resources. They often support not only files, but also
sockets, ports, memory segments, queues, processes, kernel services, system
calls, devices, file systems and more. The granularity of activities supported
is also quite large. For files, this can be append, create, execute, write,
link, ioctl, get- and setattr, read, rename, lock, ... whereas for sockets this
might be append, bind, connect, create, write, sendto, accept, ... Also, when
using a MAC system, no user or process can manipulate the security policy
itself: what the security administrator has defined cannot be overturned.
</p>

<p>
This is where SELinux comes to play. SELinux is a Linux kernel feature which
implements, amongst other things, a MAC system for controlling and governing
access to various resources. It uses a deny-by-default permission scheme, so any
access that a process wants to perform needs to be explicitly granted.
</p>

<p>
SELinux also allows you to put a finer-grained permission model <b>on top 
of</b> the traditional DAC system (which is still in use when using SELinux 
- in other words, if the traditional system does not allow certain activities,
it will not be allowed even if there are SELinux policies granting the 
permission).
</p>

</body>
</subsection>
<subsection>
<title>What is SELinux</title>
<body>

<p>
To support this finer-grained permission model, you would think that changes 
are needed to the Linux kernel. Yet thanks to the Linux kernel <e>LSM</e> 
interface (<e>Linux Security Modules</e>), support for SELinux was easily added
and since the 2.6 kernel series, SELinux has been integrated in the mainstream
kernel release. But supporting SELinux and using SELinux are very different topics.
</p>

<p>
In order to properly identify resources, SELinux needs to assign labels to these
resources. When the resources are in-memory, this is mostly supported by the
Linux kernel itself, but for persistent resources such as files, these labels
need to be placed somewhere. SELinux has chosen to use a file's extended
attributes (which is stored on the file system itself). The advantage here is
that a label remains on the file even if the file is renamed. A disadvantage of
this approach is that the file system must support <e>extended attributes</e>,
which not all file systems do (or have activated).
</p>

<p>
SELinux also uses roles to govern resource access. A user that does not have
access to the system administration role should never be allowed to execute any
system administration activities even if he is able to escalate its privileges
(say through a set-uid application). To support roles, SELinux requires changes
to the authentication services (PAM) and needs to store role definitions and
authorizations somewhere. 
</p>

<p>
Next to the kernel support and labels assigned to the resources and support
within the authorization system, SELinux also requires particular tools to
support the SELinux features. Examples are administrative tools to view and
manipulate labels, privilege management tools (like <c>sudo</c>), system
services (like SysVInit) etc. This is reflected in a set of patches
against these (and more) tools which are not always part of the applications'
main source code.
</p>

</body>
</subsection>
<subsection>
<title>Gentoo Hardened and SELinux</title>
<body>

<p>
What Gentoo Hardened offers is SELinux integrated in the distribution. When you
select SELinux support, Gentoo Hardened will apply the necessary patches against
the applications and help you (re)label your files and other resources to become
SELinux-manageable. Gentoo Hardened also integrates SELinux support inside 
Portage, allowing for newly installed files to be automatically labeled and to 
use a SELinux-supporting sandbox environment for
safe package building.
</p>

<p>
Next to the pure technological support, we hope that you will also find the
necessary supporting documents, guides, experience and on-line support for using
SELinux within Gentoo. Never hesitate to come and say hi on the
<c>#gentoo-hardened</c> chat channel in the Freenode IRC network or on our
mailing lists.
</p>

<p>
If you believe that SELinux is the right thing for you and you want to try it
out using Gentoo Hardened, please read on. The next chapter will inform you how
SELinux security is "designed" and how it is conceptually structured. Further
chapters will then help you with the authorization language and the "base"
policies that most distributions start from, and finally help you install,
run and manage a SELinux hardened Gentoo system.
</p>

</body>
</subsection>
</section>
</sections>
