ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / security / security.c
1 /*
2  * Security plug functions
3  *
4  * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com>
5  * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
6  * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com>
7  *
8  *      This program is free software; you can redistribute it and/or modify
9  *      it under the terms of the GNU General Public License as published by
10  *      the Free Software Foundation; either version 2 of the License, or
11  *      (at your option) any later version.
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/security.h>
20
21 #define SECURITY_SCAFFOLD_VERSION       "1.0.0"
22
23 /* things that live in dummy.c */
24 extern struct security_operations dummy_security_ops;
25 extern void security_fixup_ops (struct security_operations *ops);
26
27 struct security_operations *security_ops;       /* Initialized to NULL */
28
29 static inline int verify (struct security_operations *ops)
30 {
31         /* verify the security_operations structure exists */
32         if (!ops) {
33                 printk (KERN_INFO "Passed a NULL security_operations "
34                         "pointer, %s failed.\n", __FUNCTION__);
35                 return -EINVAL;
36         }
37         security_fixup_ops (ops);
38         return 0;
39 }
40
41 static void __init do_security_initcalls(void)
42 {
43         initcall_t *call;
44         call = &__security_initcall_start;
45         while (call < &__security_initcall_end) {
46                 (*call)();
47                 call++;
48         }
49 }
50
51 /**
52  * security_scaffolding_startup - initialzes the security scaffolding framework
53  *
54  * This should be called early in the kernel initialization sequence.
55  */
56 int __init security_scaffolding_startup (void)
57 {
58         printk (KERN_INFO "Security Scaffold v" SECURITY_SCAFFOLD_VERSION
59                 " initialized\n");
60
61         if (verify (&dummy_security_ops)) {
62                 printk (KERN_ERR "%s could not verify "
63                         "dummy_security_ops structure.\n", __FUNCTION__);
64                 return -EIO;
65         }
66
67         security_ops = &dummy_security_ops;
68         do_security_initcalls();
69
70         return 0;
71 }
72
73 /**
74  * register_security - registers a security framework with the kernel
75  * @ops: a pointer to the struct security_options that is to be registered
76  *
77  * This function is to allow a security module to register itself with the
78  * kernel security subsystem.  Some rudimentary checking is done on the @ops
79  * value passed to this function.  A call to unregister_security() should be
80  * done to remove this security_options structure from the kernel.
81  *
82  * If there is already a security module registered with the kernel,
83  * an error will be returned.  Otherwise 0 is returned on success.
84  */
85 int register_security (struct security_operations *ops)
86 {
87         if (verify (ops)) {
88                 printk (KERN_INFO "%s could not verify "
89                         "security_operations structure.\n", __FUNCTION__);
90                 return -EINVAL;
91         }
92
93         if (security_ops != &dummy_security_ops) {
94                 printk (KERN_INFO "There is already a security "
95                         "framework initialized, %s failed.\n", __FUNCTION__);
96                 return -EINVAL;
97         }
98
99         security_ops = ops;
100
101         return 0;
102 }
103
104 /**
105  * unregister_security - unregisters a security framework with the kernel
106  * @ops: a pointer to the struct security_options that is to be registered
107  *
108  * This function removes a struct security_operations variable that had
109  * previously been registered with a successful call to register_security().
110  *
111  * If @ops does not match the valued previously passed to register_security()
112  * an error is returned.  Otherwise the default security options is set to the
113  * the dummy_security_ops structure, and 0 is returned.
114  */
115 int unregister_security (struct security_operations *ops)
116 {
117         if (ops != security_ops) {
118                 printk (KERN_INFO "%s: trying to unregister "
119                         "a security_opts structure that is not "
120                         "registered, failing.\n", __FUNCTION__);
121                 return -EINVAL;
122         }
123
124         security_ops = &dummy_security_ops;
125
126         return 0;
127 }
128
129 /**
130  * mod_reg_security - allows security modules to be "stacked"
131  * @name: a pointer to a string with the name of the security_options to be registered
132  * @ops: a pointer to the struct security_options that is to be registered
133  *
134  * This function allows security modules to be stacked if the currently loaded
135  * security module allows this to happen.  It passes the @name and @ops to the
136  * register_security function of the currently loaded security module.
137  *
138  * The return value depends on the currently loaded security module, with 0 as
139  * success.
140  */
141 int mod_reg_security (const char *name, struct security_operations *ops)
142 {
143         if (verify (ops)) {
144                 printk (KERN_INFO "%s could not verify "
145                         "security operations.\n", __FUNCTION__);
146                 return -EINVAL;
147         }
148
149         if (ops == security_ops) {
150                 printk (KERN_INFO "%s security operations "
151                         "already registered.\n", __FUNCTION__);
152                 return -EINVAL;
153         }
154
155         return security_ops->register_security (name, ops);
156 }
157
158 /**
159  * mod_unreg_security - allows a security module registered with mod_reg_security() to be unloaded
160  * @name: a pointer to a string with the name of the security_options to be removed
161  * @ops: a pointer to the struct security_options that is to be removed
162  *
163  * This function allows security modules that have been successfully registered
164  * with a call to mod_reg_security() to be unloaded from the system.
165  * This calls the currently loaded security module's unregister_security() call
166  * with the @name and @ops variables.
167  *
168  * The return value depends on the currently loaded security module, with 0 as
169  * success.
170  */
171 int mod_unreg_security (const char *name, struct security_operations *ops)
172 {
173         if (ops == security_ops) {
174                 printk (KERN_INFO "%s invalid attempt to unregister "
175                         " primary security ops.\n", __FUNCTION__);
176                 return -EINVAL;
177         }
178
179         return security_ops->unregister_security (name, ops);
180 }
181
182 /**
183  * capable - calls the currently loaded security module's capable() function with the specified capability
184  * @cap: the requested capability level.
185  *
186  * This function calls the currently loaded security module's cabable()
187  * function with a pointer to the current task and the specified @cap value.
188  *
189  * This allows the security module to implement the capable function call
190  * however it chooses to.
191  */
192 int capable (int cap)
193 {
194         if (security_ops->capable (current, cap)) {
195                 /* capability denied */
196                 return 0;
197         }
198
199         /* capability granted */
200         current->flags |= PF_SUPERPRIV;
201         return 1;
202 }
203
204 EXPORT_SYMBOL_GPL(register_security);
205 EXPORT_SYMBOL_GPL(unregister_security);
206 EXPORT_SYMBOL_GPL(mod_reg_security);
207 EXPORT_SYMBOL_GPL(mod_unreg_security);
208 EXPORT_SYMBOL(capable);
209 EXPORT_SYMBOL(security_ops);