xref: /OK3568_Linux_fs/kernel/Documentation/security/lsm.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun========================================================
2*4882a593SmuzhiyunLinux Security Modules: General Security Hooks for Linux
3*4882a593Smuzhiyun========================================================
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun:Author: Stephen Smalley
6*4882a593Smuzhiyun:Author: Timothy Fraser
7*4882a593Smuzhiyun:Author: Chris Vance
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun.. note::
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun   The APIs described in this book are outdated.
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunIntroduction
14*4882a593Smuzhiyun============
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunIn March 2001, the National Security Agency (NSA) gave a presentation
17*4882a593Smuzhiyunabout Security-Enhanced Linux (SELinux) at the 2.5 Linux Kernel Summit.
18*4882a593SmuzhiyunSELinux is an implementation of flexible and fine-grained
19*4882a593Smuzhiyunnondiscretionary access controls in the Linux kernel, originally
20*4882a593Smuzhiyunimplemented as its own particular kernel patch. Several other security
21*4882a593Smuzhiyunprojects (e.g. RSBAC, Medusa) have also developed flexible access
22*4882a593Smuzhiyuncontrol architectures for the Linux kernel, and various projects have
23*4882a593Smuzhiyundeveloped particular access control models for Linux (e.g. LIDS, DTE,
24*4882a593SmuzhiyunSubDomain). Each project has developed and maintained its own kernel
25*4882a593Smuzhiyunpatch to support its security needs.
26*4882a593Smuzhiyun
27*4882a593SmuzhiyunIn response to the NSA presentation, Linus Torvalds made a set of
28*4882a593Smuzhiyunremarks that described a security framework he would be willing to
29*4882a593Smuzhiyunconsider for inclusion in the mainstream Linux kernel. He described a
30*4882a593Smuzhiyungeneral framework that would provide a set of security hooks to control
31*4882a593Smuzhiyunoperations on kernel objects and a set of opaque security fields in
32*4882a593Smuzhiyunkernel data structures for maintaining security attributes. This
33*4882a593Smuzhiyunframework could then be used by loadable kernel modules to implement any
34*4882a593Smuzhiyundesired model of security. Linus also suggested the possibility of
35*4882a593Smuzhiyunmigrating the Linux capabilities code into such a module.
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunThe Linux Security Modules (LSM) project was started by WireX to develop
38*4882a593Smuzhiyunsuch a framework. LSM was a joint development effort by several security
39*4882a593Smuzhiyunprojects, including Immunix, SELinux, SGI and Janus, and several
40*4882a593Smuzhiyunindividuals, including Greg Kroah-Hartman and James Morris, to develop a
41*4882a593SmuzhiyunLinux kernel patch that implements this framework. The work was
42*4882a593Smuzhiyunincorporated in the mainstream in December of 2003. This technical
43*4882a593Smuzhiyunreport provides an overview of the framework and the capabilities
44*4882a593Smuzhiyunsecurity module.
45*4882a593Smuzhiyun
46*4882a593SmuzhiyunLSM Framework
47*4882a593Smuzhiyun=============
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunThe LSM framework provides a general kernel framework to support
50*4882a593Smuzhiyunsecurity modules. In particular, the LSM framework is primarily focused
51*4882a593Smuzhiyunon supporting access control modules, although future development is
52*4882a593Smuzhiyunlikely to address other security needs such as sandboxing. By itself, the
53*4882a593Smuzhiyunframework does not provide any additional security; it merely provides
54*4882a593Smuzhiyunthe infrastructure to support security modules. The LSM framework is
55*4882a593Smuzhiyunoptional, requiring `CONFIG_SECURITY` to be enabled. The capabilities
56*4882a593Smuzhiyunlogic is implemented as a security module.
57*4882a593SmuzhiyunThis capabilities module is discussed further in
58*4882a593Smuzhiyun`LSM Capabilities Module`_.
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunThe LSM framework includes security fields in kernel data structures and
61*4882a593Smuzhiyuncalls to hook functions at critical points in the kernel code to
62*4882a593Smuzhiyunmanage the security fields and to perform access control.
63*4882a593SmuzhiyunIt also adds functions for registering security modules.
64*4882a593SmuzhiyunAn interface `/sys/kernel/security/lsm` reports a comma separated list
65*4882a593Smuzhiyunof security modules that are active on the system.
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunThe LSM security fields are simply ``void*`` pointers.
68*4882a593SmuzhiyunThe data is referred to as a blob, which may be managed by
69*4882a593Smuzhiyunthe framework or by the individual security modules that use it.
70*4882a593SmuzhiyunSecurity blobs that are used by more than one security module are
71*4882a593Smuzhiyuntypically managed by the framework.
72*4882a593SmuzhiyunFor process and
73*4882a593Smuzhiyunprogram execution security information, security fields are included in
74*4882a593Smuzhiyun:c:type:`struct task_struct <task_struct>` and
75*4882a593Smuzhiyun:c:type:`struct cred <cred>`.
76*4882a593SmuzhiyunFor filesystem
77*4882a593Smuzhiyunsecurity information, a security field is included in :c:type:`struct
78*4882a593Smuzhiyunsuper_block <super_block>`. For pipe, file, and socket security
79*4882a593Smuzhiyuninformation, security fields are included in :c:type:`struct inode
80*4882a593Smuzhiyun<inode>` and :c:type:`struct file <file>`.
81*4882a593SmuzhiyunFor System V IPC security information,
82*4882a593Smuzhiyunsecurity fields were added to :c:type:`struct kern_ipc_perm
83*4882a593Smuzhiyun<kern_ipc_perm>` and :c:type:`struct msg_msg
84*4882a593Smuzhiyun<msg_msg>`; additionally, the definitions for :c:type:`struct
85*4882a593Smuzhiyunmsg_msg <msg_msg>`, struct msg_queue, and struct shmid_kernel
86*4882a593Smuzhiyunwere moved to header files (``include/linux/msg.h`` and
87*4882a593Smuzhiyun``include/linux/shm.h`` as appropriate) to allow the security modules to
88*4882a593Smuzhiyunuse these definitions.
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunFor packet and
91*4882a593Smuzhiyunnetwork device security information, security fields were added to
92*4882a593Smuzhiyun:c:type:`struct sk_buff <sk_buff>` and
93*4882a593Smuzhiyun:c:type:`struct scm_cookie <scm_cookie>`.
94*4882a593SmuzhiyunUnlike the other security module data, the data used here is a
95*4882a593Smuzhiyun32-bit integer. The security modules are required to map or otherwise
96*4882a593Smuzhiyunassociate these values with real security attributes.
97*4882a593Smuzhiyun
98*4882a593SmuzhiyunLSM hooks are maintained in lists. A list is maintained for each
99*4882a593Smuzhiyunhook, and the hooks are called in the order specified by CONFIG_LSM.
100*4882a593SmuzhiyunDetailed documentation for each hook is
101*4882a593Smuzhiyunincluded in the `include/linux/lsm_hooks.h` header file.
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunThe LSM framework provides for a close approximation of
104*4882a593Smuzhiyungeneral security module stacking. It defines
105*4882a593Smuzhiyunsecurity_add_hooks() to which each security module passes a
106*4882a593Smuzhiyun:c:type:`struct security_hooks_list <security_hooks_list>`,
107*4882a593Smuzhiyunwhich are added to the lists.
108*4882a593SmuzhiyunThe LSM framework does not provide a mechanism for removing hooks that
109*4882a593Smuzhiyunhave been registered. The SELinux security module has implemented
110*4882a593Smuzhiyuna way to remove itself, however the feature has been deprecated.
111*4882a593Smuzhiyun
112*4882a593SmuzhiyunThe hooks can be viewed as falling into two major
113*4882a593Smuzhiyuncategories: hooks that are used to manage the security fields and hooks
114*4882a593Smuzhiyunthat are used to perform access control. Examples of the first category
115*4882a593Smuzhiyunof hooks include the security_inode_alloc() and security_inode_free()
116*4882a593SmuzhiyunThese hooks are used to allocate
117*4882a593Smuzhiyunand free security structures for inode objects.
118*4882a593SmuzhiyunAn example of the second category of hooks
119*4882a593Smuzhiyunis the security_inode_permission() hook.
120*4882a593SmuzhiyunThis hook checks permission when accessing an inode.
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunLSM Capabilities Module
123*4882a593Smuzhiyun=======================
124*4882a593Smuzhiyun
125*4882a593SmuzhiyunThe POSIX.1e capabilities logic is maintained as a security module
126*4882a593Smuzhiyunstored in the file ``security/commoncap.c``. The capabilities
127*4882a593Smuzhiyunmodule uses the order field of the :c:type:`lsm_info` description
128*4882a593Smuzhiyunto identify it as the first security module to be registered.
129*4882a593SmuzhiyunThe capabilities security module does not use the general security
130*4882a593Smuzhiyunblobs, unlike other modules. The reasons are historical and are
131*4882a593Smuzhiyunbased on overhead, complexity and performance concerns.
132