blob: 441beaf1bbc1d104ea7c51706bd2f777feac44e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * BSD Secure Levels LSM
3 *
4 * Maintainers:
5 * Michael A. Halcrow <mike@halcrow.us>
6 * Serge Hallyn <hallyn@cs.wm.edu>
7 *
8 * Copyright (c) 2001 WireX Communications, Inc <chris@wirex.com>
9 * Copyright (c) 2001 Greg Kroah-Hartman <greg@kroah.com>
10 * Copyright (c) 2002 International Business Machines <robb@austin.ibm.com>
Davi Arnaut6bb08da2006-03-23 02:59:25 -080011 * Copyright (c) 2006 Davi E. M. Arnaut <davi.arnaut@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 */
18
19#include <linux/config.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/security.h>
25#include <linux/netlink.h>
26#include <linux/fs.h>
27#include <linux/namei.h>
28#include <linux/mount.h>
29#include <linux/capability.h>
30#include <linux/time.h>
31#include <linux/proc_fs.h>
32#include <linux/kobject.h>
33#include <linux/crypto.h>
34#include <asm/scatterlist.h>
Davi Arnaut6bb08da2006-03-23 02:59:25 -080035#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/gfp.h>
37#include <linux/sysfs.h>
38
39#define SHA1_DIGEST_SIZE 20
40
41/**
42 * Module parameter that defines the initial secure level.
43 *
44 * When built as a module, it defaults to seclvl 1, which is the
45 * behavior of BSD secure levels. Note that this default behavior
46 * wrecks havoc on a machine when the seclvl module is compiled into
47 * the kernel. In that case, we default to seclvl 0.
48 */
49#ifdef CONFIG_SECURITY_SECLVL_MODULE
50static int initlvl = 1;
51#else
52static int initlvl;
53#endif
54module_param(initlvl, int, 0);
55MODULE_PARM_DESC(initlvl, "Initial secure level (defaults to 1)");
56
57/* Module parameter that defines the verbosity level */
58static int verbosity;
59module_param(verbosity, int, 0);
60MODULE_PARM_DESC(verbosity, "Initial verbosity level (0 or 1; defaults to "
61 "0, which is Quiet)");
62
63/**
64 * Optional password which can be passed in to bring seclvl to 0
65 * (i.e., for halt/reboot). Defaults to NULL (the passwd attribute
66 * file will not be registered in sysfs).
67 *
68 * This gets converted to its SHA1 hash when stored. It's probably
69 * not a good idea to use this parameter when loading seclvl from a
70 * script; use sha1_passwd instead.
71 */
72
73#define MAX_PASSWD_SIZE 32
74static char passwd[MAX_PASSWD_SIZE];
75module_param_string(passwd, passwd, sizeof(passwd), 0);
76MODULE_PARM_DESC(passwd,
77 "Plaintext of password that sets seclvl=0 when written to "
78 "(sysfs mount point)/seclvl/passwd\n");
79
80/**
81 * SHA1 hashed version of the optional password which can be passed in
82 * to bring seclvl to 0 (i.e., for halt/reboot). Must be in
83 * hexadecimal format (40 characters). Defaults to NULL (the passwd
84 * attribute file will not be registered in sysfs).
85 *
86 * Use the sha1sum utility to generate the SHA1 hash of a password:
87 *
88 * echo -n "secret" | sha1sum
89 */
90#define MAX_SHA1_PASSWD 41
91static char sha1_passwd[MAX_SHA1_PASSWD];
92module_param_string(sha1_passwd, sha1_passwd, sizeof(sha1_passwd), 0);
93MODULE_PARM_DESC(sha1_passwd,
94 "SHA1 hash (40 hexadecimal characters) of password that "
95 "sets seclvl=0 when plaintext password is written to "
96 "(sysfs mount point)/seclvl/passwd\n");
97
98static int hideHash = 1;
99module_param(hideHash, int, 0);
100MODULE_PARM_DESC(hideHash, "When set to 0, reading seclvl/passwd from sysfs "
101 "will return the SHA1-hashed value of the password that "
102 "lowers the secure level to 0.\n");
103
104#define MY_NAME "seclvl"
105
106/**
107 * This time-limits log writes to one per second.
108 */
109#define seclvl_printk(verb, type, fmt, arg...) \
110 do { \
111 if (verbosity >= verb) { \
112 static unsigned long _prior; \
113 unsigned long _now = jiffies; \
114 if ((_now - _prior) > HZ) { \
115 printk(type "%s: %s: " fmt, \
116 MY_NAME, __FUNCTION__ , \
117 ## arg); \
118 _prior = _now; \
119 } \
120 } \
121 } while (0)
122
123/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * The actual security level. Ranges between -1 and 2 inclusive.
125 */
126static int seclvl;
127
128/**
129 * flag to keep track of how we were registered
130 */
131static int secondary;
132
133/**
134 * Verifies that the requested secure level is valid, given the current
135 * secure level.
136 */
137static int seclvl_sanity(int reqlvl)
138{
139 if ((reqlvl < -1) || (reqlvl > 2)) {
140 seclvl_printk(1, KERN_WARNING, "Attempt to set seclvl out of "
141 "range: [%d]\n", reqlvl);
142 return -EINVAL;
143 }
144 if ((seclvl == 0) && (reqlvl == -1))
145 return 0;
146 if (reqlvl < seclvl) {
147 seclvl_printk(1, KERN_WARNING, "Attempt to lower seclvl to "
148 "[%d]\n", reqlvl);
149 return -EPERM;
150 }
151 return 0;
152}
153
154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * security level advancement rules:
156 * Valid levels are -1 through 2, inclusive.
157 * From -1, stuck. [ in case compiled into kernel ]
158 * From 0 or above, can only increment.
159 */
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500160static void do_seclvl_advance(void *data, u64 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500162 int ret;
163 int newlvl = (int)val;
164
165 ret = seclvl_sanity(newlvl);
166 if (ret)
167 return;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (newlvl > 2) {
170 seclvl_printk(1, KERN_WARNING, "Cannot advance to seclvl "
171 "[%d]\n", newlvl);
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 if (seclvl == -1) {
175 seclvl_printk(1, KERN_WARNING, "Not allowed to advance to "
176 "seclvl [%d]\n", seclvl);
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500179 seclvl = newlvl; /* would it be more "correct" to set *data? */
180 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500183static u64 seclvl_int_get(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500185 return *(int *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500188DEFINE_SIMPLE_ATTRIBUTE(seclvl_file_ops, seclvl_int_get, do_seclvl_advance, "%lld\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190static unsigned char hashedPassword[SHA1_DIGEST_SIZE];
191
192/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * Converts a block of plaintext of into its SHA1 hashed value.
194 *
195 * It would be nice if crypto had a wrapper to do this for us linear
196 * people...
197 */
198static int
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800199plaintext_to_sha1(unsigned char *hash, const char *plaintext, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct crypto_tfm *tfm;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800202 struct scatterlist sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (len > PAGE_SIZE) {
204 seclvl_printk(0, KERN_ERR, "Plaintext password too large (%d "
205 "characters). Largest possible is %lu "
206 "bytes.\n", len, PAGE_SIZE);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800207 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Herbert Xueb6f1162005-09-01 17:43:25 -0700209 tfm = crypto_alloc_tfm("sha1", CRYPTO_TFM_REQ_MAY_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (tfm == NULL) {
211 seclvl_printk(0, KERN_ERR,
212 "Failed to load transform for SHA1\n");
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800215 sg_init_one(&sg, (u8 *)plaintext, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 crypto_digest_init(tfm);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800217 crypto_digest_update(tfm, &sg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 crypto_digest_final(tfm, hash);
219 crypto_free_tfm(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
221}
222
223/**
224 * Called whenever the user writes to the sysfs passwd handle to this kernel
225 * object. It hashes the password and compares the hashed results.
226 */
227static ssize_t
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500228passwd_write_file(struct file * file, const char __user * buf,
229 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800231 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int len;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800233 unsigned char tmp[SHA1_DIGEST_SIZE];
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!*passwd && !*sha1_passwd) {
236 seclvl_printk(0, KERN_ERR, "Attempt to password-unlock the "
237 "seclvl module, but neither a plain text "
238 "password nor a SHA1 hashed password was "
239 "passed in as a module parameter! This is a "
240 "bug, since it should not be possible to be in "
241 "this part of the module; please tell a "
242 "maintainer about this event.\n");
243 return -EINVAL;
244 }
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500245
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800246 if (count >= PAGE_SIZE)
Serge Hallyn9afa57b2005-09-16 19:27:57 -0700247 return -EINVAL;
Andrew Mortond15c5742005-09-16 19:27:56 -0700248 if (*ppos != 0)
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500249 return -EINVAL;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800250 p = kmalloc(count, GFP_KERNEL);
251 if (!p)
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500252 return -ENOMEM;
253 len = -EFAULT;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800254 if (copy_from_user(p, buf, count))
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500255 goto out;
256
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800257 len = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* ``echo "secret" > seclvl/passwd'' includes a newline */
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800259 if (p[len - 1] == '\n')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 len--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* Hash the password, then compare the hashed values */
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800262 if ((len = plaintext_to_sha1(tmp, p, len))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 seclvl_printk(0, KERN_ERR, "Error hashing password: rc = "
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800264 "[%d]\n", len);
265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800267
268 len = -EPERM;
269 if (memcmp(hashedPassword, tmp, SHA1_DIGEST_SIZE))
270 goto out;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 seclvl_printk(0, KERN_INFO,
273 "Password accepted; seclvl reduced to 0.\n");
274 seclvl = 0;
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500275 len = count;
276
277out:
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800278 kfree (p);
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500279 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500282static struct file_operations passwd_file_ops = {
283 .write = passwd_write_file,
284};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/**
287 * Explicitely disallow ptrace'ing the init process.
288 */
289static int seclvl_ptrace(struct task_struct *parent, struct task_struct *child)
290{
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800291 if (seclvl >= 0 && child->pid == 1) {
292 seclvl_printk(1, KERN_WARNING, "Attempt to ptrace "
293 "the init process dissallowed in "
294 "secure level %d\n", seclvl);
295 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 return 0;
298}
299
300/**
301 * Capability checks for seclvl. The majority of the policy
302 * enforcement for seclvl takes place here.
303 */
304static int seclvl_capable(struct task_struct *tsk, int cap)
305{
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800306 int rc = 0;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* init can do anything it wants */
309 if (tsk->pid == 1)
310 return 0;
311
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800312 if (seclvl > 0) {
313 rc = -EPERM;
314
315 if (cap == CAP_LINUX_IMMUTABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 seclvl_printk(1, KERN_WARNING, "Attempt to modify "
317 "the IMMUTABLE and/or APPEND extended "
318 "attribute on a file with the IMMUTABLE "
319 "and/or APPEND extended attribute set "
320 "denied in seclvl [%d]\n", seclvl);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800321 else if (cap == CAP_SYS_RAWIO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
323 "raw I/O while in secure level [%d] "
324 "denied\n", seclvl);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800325 else if (cap == CAP_NET_ADMIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
327 "network administrative task while "
328 "in secure level [%d] denied\n", seclvl);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800329 else if (cap == CAP_SETUID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 seclvl_printk(1, KERN_WARNING, "Attempt to setuid "
331 "while in secure level [%d] denied\n",
332 seclvl);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800333 else if (cap == CAP_SETGID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 seclvl_printk(1, KERN_WARNING, "Attempt to setgid "
335 "while in secure level [%d] denied\n",
336 seclvl);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800337 else if (cap == CAP_SYS_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
339 "a module operation while in secure "
340 "level [%d] denied\n", seclvl);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800341 else
342 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800344
345 if (!rc) {
346 if (!(cap_is_fs_cap(cap) ? tsk->fsuid == 0 : tsk->euid == 0))
347 rc = -EPERM;
348 }
349
350 if (rc)
351 seclvl_printk(1, KERN_WARNING, "Capability denied\n");
352
353 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356/**
357 * Disallow reversing the clock in seclvl > 1
358 */
359static int seclvl_settime(struct timespec *tv, struct timezone *tz)
360{
Linus Torvalds951069e2006-01-31 10:16:55 -0800361 if (tv && seclvl > 1) {
362 struct timespec now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 now = current_kernel_time();
364 if (tv->tv_sec < now.tv_sec ||
365 (tv->tv_sec == now.tv_sec && tv->tv_nsec < now.tv_nsec)) {
366 seclvl_printk(1, KERN_WARNING, "Attempt to decrement "
367 "time in secure level %d denied: "
368 "current->pid = [%d], "
369 "current->group_leader->pid = [%d]\n",
370 seclvl, current->pid,
371 current->group_leader->pid);
372 return -EPERM;
373 } /* if attempt to decrement time */
374 } /* if seclvl > 1 */
375 return 0;
376}
377
378/* claim the blockdev to exclude mounters, release on file close */
379static int seclvl_bd_claim(struct inode *inode)
380{
381 int holder;
382 struct block_device *bdev = NULL;
383 dev_t dev = inode->i_rdev;
384 bdev = open_by_devnum(dev, FMODE_WRITE);
385 if (bdev) {
386 if (bd_claim(bdev, &holder)) {
387 blkdev_put(bdev);
388 return -EPERM;
389 }
390 /* claimed, mark it to release on close */
391 inode->i_security = current;
392 }
393 return 0;
394}
395
396/* release the blockdev if you claimed it */
397static void seclvl_bd_release(struct inode *inode)
398{
399 if (inode && S_ISBLK(inode->i_mode) && inode->i_security == current) {
400 struct block_device *bdev = inode->i_bdev;
401 if (bdev) {
402 bd_release(bdev);
403 blkdev_put(bdev);
404 inode->i_security = NULL;
405 }
406 }
407}
408
409/**
410 * Security for writes to block devices is regulated by this seclvl
411 * function. Deny all writes to block devices in seclvl 2. In
412 * seclvl 1, we only deny writes to *mounted* block devices.
413 */
414static int
415seclvl_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
416{
417 if (current->pid != 1 && S_ISBLK(inode->i_mode) && (mask & MAY_WRITE)) {
418 switch (seclvl) {
419 case 2:
420 seclvl_printk(1, KERN_WARNING, "Write to block device "
421 "denied in secure level [%d]\n", seclvl);
422 return -EPERM;
423 case 1:
424 if (seclvl_bd_claim(inode)) {
425 seclvl_printk(1, KERN_WARNING,
426 "Write to mounted block device "
427 "denied in secure level [%d]\n",
428 seclvl);
429 return -EPERM;
430 }
431 }
432 }
433 return 0;
434}
435
436/**
437 * The SUID and SGID bits cannot be set in seclvl >= 1
438 */
439static int seclvl_inode_setattr(struct dentry *dentry, struct iattr *iattr)
440{
441 if (seclvl > 0) {
442 if (iattr->ia_valid & ATTR_MODE)
443 if (iattr->ia_mode & S_ISUID ||
444 iattr->ia_mode & S_ISGID) {
445 seclvl_printk(1, KERN_WARNING, "Attempt to "
446 "modify SUID or SGID bit "
447 "denied in seclvl [%d]\n",
448 seclvl);
449 return -EPERM;
450 }
451 }
452 return 0;
453}
454
455/* release busied block devices */
456static void seclvl_file_free_security(struct file *filp)
457{
458 struct dentry *dentry = filp->f_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800460 if (dentry)
461 seclvl_bd_release(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/**
465 * Cannot unmount in secure level 2
466 */
467static int seclvl_umount(struct vfsmount *mnt, int flags)
468{
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800469 if (current->pid != 1 && seclvl == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 seclvl_printk(1, KERN_WARNING, "Attempt to unmount in secure "
471 "level %d\n", seclvl);
472 return -EPERM;
473 }
474 return 0;
475}
476
477static struct security_operations seclvl_ops = {
478 .ptrace = seclvl_ptrace,
479 .capable = seclvl_capable,
480 .inode_permission = seclvl_inode_permission,
481 .inode_setattr = seclvl_inode_setattr,
482 .file_free_security = seclvl_file_free_security,
483 .settime = seclvl_settime,
484 .sb_umount = seclvl_umount,
485};
486
487/**
488 * Process the password-related module parameters
489 */
490static int processPassword(void)
491{
492 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (*passwd) {
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800494 char *p;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (*sha1_passwd) {
497 seclvl_printk(0, KERN_ERR, "Error: Both "
498 "passwd and sha1_passwd "
499 "were set, but they are mutually "
500 "exclusive.\n");
501 return -EINVAL;
502 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800503
504 p = kstrdup(passwd, GFP_KERNEL);
505 if (p == NULL)
506 return -ENOMEM;
507
508 if ((rc = plaintext_to_sha1(hashedPassword, p, strlen(p))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 seclvl_printk(0, KERN_ERR, "Error: SHA1 support not "
510 "in kernel\n");
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800511
512 kfree (p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* All static data goes to the BSS, which zero's the
514 * plaintext password out for us. */
515 } else if (*sha1_passwd) { // Base 16
516 int i;
517 i = strlen(sha1_passwd);
518 if (i != (SHA1_DIGEST_SIZE * 2)) {
519 seclvl_printk(0, KERN_ERR, "Received [%d] bytes; "
520 "expected [%d] for the hexadecimal "
521 "representation of the SHA1 hash of "
522 "the password.\n",
523 i, (SHA1_DIGEST_SIZE * 2));
524 return -EINVAL;
525 }
526 while ((i -= 2) + 2) {
527 unsigned char tmp;
528 tmp = sha1_passwd[i + 2];
529 sha1_passwd[i + 2] = '\0';
530 hashedPassword[i / 2] = (unsigned char)
531 simple_strtol(&sha1_passwd[i], NULL, 16);
532 sha1_passwd[i + 2] = tmp;
533 }
534 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800535 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/**
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500539 * securityfs registrations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500541struct dentry *dir_ino, *seclvl_ino, *passwd_ino;
542
543static int seclvlfs_register(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800545 int rc = 0;
546
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500547 dir_ino = securityfs_create_dir("seclvl", NULL);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800548
549 if (IS_ERR(dir_ino))
550 return PTR_ERR(dir_ino);
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500551
552 seclvl_ino = securityfs_create_file("seclvl", S_IRUGO | S_IWUSR,
553 dir_ino, &seclvl, &seclvl_file_ops);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800554 if (IS_ERR(seclvl_ino)) {
555 rc = PTR_ERR(seclvl_ino);
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500556 goto out_deldir;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (*passwd || *sha1_passwd) {
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500559 passwd_ino = securityfs_create_file("passwd", S_IRUGO | S_IWUSR,
560 dir_ino, NULL, &passwd_file_ops);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800561 if (IS_ERR(passwd_ino)) {
562 rc = PTR_ERR(passwd_ino);
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500563 goto out_delf;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800566 return rc;
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500567
serue@us.ibm.com5a73c302005-07-08 15:44:19 -0500568out_delf:
569 securityfs_remove(seclvl_ino);
570
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800571out_deldir:
572 securityfs_remove(dir_ino);
573
574 return rc;
575}
576
577static void seclvlfs_unregister(void)
578{
579 securityfs_remove(seclvl_ino);
580
581 if (*passwd || *sha1_passwd)
582 securityfs_remove(passwd_ino);
583
584 securityfs_remove(dir_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/**
588 * Initialize the seclvl module.
589 */
590static int __init seclvl_init(void)
591{
592 int rc = 0;
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800593 static char once;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (verbosity < 0 || verbosity > 1) {
596 printk(KERN_ERR "Error: bad verbosity [%d]; only 0 or 1 "
597 "are valid values\n", verbosity);
598 rc = -EINVAL;
599 goto exit;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (initlvl < -1 || initlvl > 2) {
602 seclvl_printk(0, KERN_ERR, "Error: bad initial securelevel "
603 "[%d].\n", initlvl);
604 rc = -EINVAL;
605 goto exit;
606 }
607 seclvl = initlvl;
608 if ((rc = processPassword())) {
609 seclvl_printk(0, KERN_ERR, "Error processing the password "
610 "module parameter(s): rc = [%d]\n", rc);
611 goto exit;
612 }
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800613
614 if ((rc = seclvlfs_register())) {
615 seclvl_printk(0, KERN_ERR, "Error registering with sysfs\n");
616 goto exit;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* register ourselves with the security framework */
619 if (register_security(&seclvl_ops)) {
620 seclvl_printk(0, KERN_ERR,
621 "seclvl: Failure registering with the "
622 "kernel.\n");
623 /* try registering with primary module */
624 rc = mod_reg_security(MY_NAME, &seclvl_ops);
625 if (rc) {
626 seclvl_printk(0, KERN_ERR, "seclvl: Failure "
627 "registering with primary security "
628 "module.\n");
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800629 seclvlfs_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 goto exit;
631 } /* if primary module registered */
632 secondary = 1;
633 } /* if we registered ourselves with the security framework */
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 seclvl_printk(0, KERN_INFO, "seclvl: Successfully initialized.\n");
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800636
637 if (once) {
638 once = 1;
639 seclvl_printk(0, KERN_INFO, "seclvl is going away. It has been "
640 "buggy for ages. Also, be warned that "
641 "Securelevels are useless.");
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 exit:
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800644 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 printk(KERN_ERR "seclvl: Error during initialization: rc = "
646 "[%d]\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return rc;
648}
649
650/**
651 * Remove the seclvl module.
652 */
653static void __exit seclvl_exit(void)
654{
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800655 seclvlfs_unregister();
656
657 if (secondary)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 mod_unreg_security(MY_NAME, &seclvl_ops);
Davi Arnaut6bb08da2006-03-23 02:59:25 -0800659 else if (unregister_security(&seclvl_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 seclvl_printk(0, KERN_INFO,
661 "seclvl: Failure unregistering with the "
662 "kernel\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665module_init(seclvl_init);
666module_exit(seclvl_exit);
667
668MODULE_AUTHOR("Michael A. Halcrow <mike@halcrow.us>");
669MODULE_DESCRIPTION("LSM implementation of the BSD Secure Levels");
670MODULE_LICENSE("GPL");