1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/module.h>
20#include <linux/moduleparam.h>
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/security.h>
24#include <linux/netlink.h>
25#include <linux/fs.h>
26#include <linux/namei.h>
27#include <linux/mount.h>
28#include <linux/capability.h>
29#include <linux/time.h>
30#include <linux/proc_fs.h>
31#include <linux/kobject.h>
32#include <linux/crypto.h>
33#include <asm/scatterlist.h>
34#include <linux/scatterlist.h>
35#include <linux/gfp.h>
36#include <linux/sysfs.h>
37
38#define SHA1_DIGEST_SIZE 20
39
40
41
42
43
44
45
46
47
48#ifdef CONFIG_SECURITY_SECLVL_MODULE
49static int initlvl = 1;
50#else
51static int initlvl;
52#endif
53module_param(initlvl, int, 0);
54MODULE_PARM_DESC(initlvl, "Initial secure level (defaults to 1)");
55
56
57static int verbosity;
58module_param(verbosity, int, 0);
59MODULE_PARM_DESC(verbosity, "Initial verbosity level (0 or 1; defaults to "
60 "0, which is Quiet)");
61
62
63
64
65
66
67
68
69
70
71
72#define MAX_PASSWD_SIZE 32
73static char passwd[MAX_PASSWD_SIZE];
74module_param_string(passwd, passwd, sizeof(passwd), 0);
75MODULE_PARM_DESC(passwd,
76 "Plaintext of password that sets seclvl=0 when written to "
77 "(sysfs mount point)/seclvl/passwd\n");
78
79
80
81
82
83
84
85
86
87
88
89#define MAX_SHA1_PASSWD 41
90static char sha1_passwd[MAX_SHA1_PASSWD];
91module_param_string(sha1_passwd, sha1_passwd, sizeof(sha1_passwd), 0);
92MODULE_PARM_DESC(sha1_passwd,
93 "SHA1 hash (40 hexadecimal characters) of password that "
94 "sets seclvl=0 when plaintext password is written to "
95 "(sysfs mount point)/seclvl/passwd\n");
96
97static int hideHash = 1;
98module_param(hideHash, int, 0);
99MODULE_PARM_DESC(hideHash, "When set to 0, reading seclvl/passwd from sysfs "
100 "will return the SHA1-hashed value of the password that "
101 "lowers the secure level to 0.\n");
102
103#define MY_NAME "seclvl"
104
105
106
107
108#define seclvl_printk(verb, type, fmt, arg...) \
109 do { \
110 if (verbosity >= verb) { \
111 static unsigned long _prior; \
112 unsigned long _now = jiffies; \
113 if ((_now - _prior) > HZ) { \
114 printk(type "%s: %s: " fmt, \
115 MY_NAME, __FUNCTION__ , \
116 ## arg); \
117 _prior = _now; \
118 } \
119 } \
120 } while (0)
121
122
123
124
125static int seclvl;
126
127
128
129
130static int secondary;
131
132
133
134
135
136static int seclvl_sanity(int reqlvl)
137{
138 if ((reqlvl < -1) || (reqlvl > 2)) {
139 seclvl_printk(1, KERN_WARNING, "Attempt to set seclvl out of "
140 "range: [%d]\n", reqlvl);
141 return -EINVAL;
142 }
143 if ((seclvl == 0) && (reqlvl == -1))
144 return 0;
145 if (reqlvl < seclvl) {
146 seclvl_printk(1, KERN_WARNING, "Attempt to lower seclvl to "
147 "[%d]\n", reqlvl);
148 return -EPERM;
149 }
150 return 0;
151}
152
153
154
155
156
157
158
159static void do_seclvl_advance(void *data, u64 val)
160{
161 int ret;
162 int newlvl = (int)val;
163
164 ret = seclvl_sanity(newlvl);
165 if (ret)
166 return;
167
168 if (newlvl > 2) {
169 seclvl_printk(1, KERN_WARNING, "Cannot advance to seclvl "
170 "[%d]\n", newlvl);
171 return;
172 }
173 if (seclvl == -1) {
174 seclvl_printk(1, KERN_WARNING, "Not allowed to advance to "
175 "seclvl [%d]\n", seclvl);
176 return;
177 }
178 seclvl = newlvl;
179 return;
180}
181
182static u64 seclvl_int_get(void *data)
183{
184 return *(int *)data;
185}
186
187DEFINE_SIMPLE_ATTRIBUTE(seclvl_file_ops, seclvl_int_get, do_seclvl_advance, "%lld\n");
188
189static unsigned char hashedPassword[SHA1_DIGEST_SIZE];
190
191
192
193
194
195
196
197static int
198plaintext_to_sha1(unsigned char *hash, const char *plaintext, unsigned int len)
199{
200 struct crypto_tfm *tfm;
201 struct scatterlist sg;
202 if (len > PAGE_SIZE) {
203 seclvl_printk(0, KERN_ERR, "Plaintext password too large (%d "
204 "characters). Largest possible is %lu "
205 "bytes.\n", len, PAGE_SIZE);
206 return -EINVAL;
207 }
208 tfm = crypto_alloc_tfm("sha1", CRYPTO_TFM_REQ_MAY_SLEEP);
209 if (tfm == NULL) {
210 seclvl_printk(0, KERN_ERR,
211 "Failed to load transform for SHA1\n");
212 return -EINVAL;
213 }
214 sg_init_one(&sg, (u8 *)plaintext, len);
215 crypto_digest_init(tfm);
216 crypto_digest_update(tfm, &sg, 1);
217 crypto_digest_final(tfm, hash);
218 crypto_free_tfm(tfm);
219 return 0;
220}
221
222
223
224
225
226static ssize_t
227passwd_write_file(struct file * file, const char __user * buf,
228 size_t count, loff_t *ppos)
229{
230 char *p;
231 int len;
232 unsigned char tmp[SHA1_DIGEST_SIZE];
233
234 if (!*passwd && !*sha1_passwd) {
235 seclvl_printk(0, KERN_ERR, "Attempt to password-unlock the "
236 "seclvl module, but neither a plain text "
237 "password nor a SHA1 hashed password was "
238 "passed in as a module parameter! This is a "
239 "bug, since it should not be possible to be in "
240 "this part of the module; please tell a "
241 "maintainer about this event.\n");
242 return -EINVAL;
243 }
244
245 if (count >= PAGE_SIZE)
246 return -EINVAL;
247 if (*ppos != 0)
248 return -EINVAL;
249 p = kmalloc(count, GFP_KERNEL);
250 if (!p)
251 return -ENOMEM;
252 len = -EFAULT;
253 if (copy_from_user(p, buf, count))
254 goto out;
255
256 len = count;
257
258 if (p[len - 1] == '\n')
259 len--;
260
261 if ((len = plaintext_to_sha1(tmp, p, len))) {
262 seclvl_printk(0, KERN_ERR, "Error hashing password: rc = "
263 "[%d]\n", len);
264 goto out;
265 }
266
267 len = -EPERM;
268 if (memcmp(hashedPassword, tmp, SHA1_DIGEST_SIZE))
269 goto out;
270
271 seclvl_printk(0, KERN_INFO,
272 "Password accepted; seclvl reduced to 0.\n");
273 seclvl = 0;
274 len = count;
275
276out:
277 kfree (p);
278 return len;
279}
280
281static struct file_operations passwd_file_ops = {
282 .write = passwd_write_file,
283};
284
285
286
287
288static int seclvl_ptrace(struct task_struct *parent, struct task_struct *child)
289{
290 if (seclvl >= 0 && child->pid == 1) {
291 seclvl_printk(1, KERN_WARNING, "Attempt to ptrace "
292 "the init process dissallowed in "
293 "secure level %d\n", seclvl);
294 return -EPERM;
295 }
296 return 0;
297}
298
299
300
301
302
303static int seclvl_capable(struct task_struct *tsk, int cap)
304{
305 int rc = 0;
306
307
308 if (tsk->pid == 1)
309 return 0;
310
311 if (seclvl > 0) {
312 rc = -EPERM;
313
314 if (cap == CAP_LINUX_IMMUTABLE)
315 seclvl_printk(1, KERN_WARNING, "Attempt to modify "
316 "the IMMUTABLE and/or APPEND extended "
317 "attribute on a file with the IMMUTABLE "
318 "and/or APPEND extended attribute set "
319 "denied in seclvl [%d]\n", seclvl);
320 else if (cap == CAP_SYS_RAWIO)
321 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
322 "raw I/O while in secure level [%d] "
323 "denied\n", seclvl);
324 else if (cap == CAP_NET_ADMIN)
325 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
326 "network administrative task while "
327 "in secure level [%d] denied\n", seclvl);
328 else if (cap == CAP_SETUID)
329 seclvl_printk(1, KERN_WARNING, "Attempt to setuid "
330 "while in secure level [%d] denied\n",
331 seclvl);
332 else if (cap == CAP_SETGID)
333 seclvl_printk(1, KERN_WARNING, "Attempt to setgid "
334 "while in secure level [%d] denied\n",
335 seclvl);
336 else if (cap == CAP_SYS_MODULE)
337 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
338 "a module operation while in secure "
339 "level [%d] denied\n", seclvl);
340 else
341 rc = 0;
342 }
343
344 if (!rc) {
345 if (!(cap_is_fs_cap(cap) ? tsk->fsuid == 0 : tsk->euid == 0))
346 rc = -EPERM;
347 }
348
349 if (rc)
350 seclvl_printk(1, KERN_WARNING, "Capability denied\n");
351
352 return rc;
353}
354
355
356
357
358static int seclvl_settime(struct timespec *tv, struct timezone *tz)
359{
360 if (tv && seclvl > 1) {
361 struct timespec now;
362 now = current_kernel_time();
363 if (tv->tv_sec < now.tv_sec ||
364 (tv->tv_sec == now.tv_sec && tv->tv_nsec < now.tv_nsec)) {
365 seclvl_printk(1, KERN_WARNING, "Attempt to decrement "
366 "time in secure level %d denied: "
367 "current->pid = [%d], "
368 "current->group_leader->pid = [%d]\n",
369 seclvl, current->pid,
370 current->group_leader->pid);
371 return -EPERM;
372 }
373 if (tv->tv_sec > 1924988400)
374 return -EPERM;
375 }
376 return 0;
377}
378
379
380static int seclvl_bd_claim(struct inode *inode)
381{
382 int holder;
383 struct block_device *bdev = NULL;
384 dev_t dev = inode->i_rdev;
385 bdev = open_by_devnum(dev, FMODE_WRITE);
386 if (bdev) {
387 if (bd_claim(bdev, &holder)) {
388 blkdev_put(bdev);
389 return -EPERM;
390 }
391
392 inode->i_security = current;
393 }
394 return 0;
395}
396
397
398static void seclvl_bd_release(struct inode *inode)
399{
400 if (inode && S_ISBLK(inode->i_mode) && inode->i_security == current) {
401 struct block_device *bdev = inode->i_bdev;
402 if (bdev) {
403 bd_release(bdev);
404 blkdev_put(bdev);
405 inode->i_security = NULL;
406 }
407 }
408}
409
410
411
412
413
414
415static int
416seclvl_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
417{
418 if (current->pid != 1 && S_ISBLK(inode->i_mode) && (mask & MAY_WRITE)) {
419 switch (seclvl) {
420 case 2:
421 seclvl_printk(1, KERN_WARNING, "Write to block device "
422 "denied in secure level [%d]\n", seclvl);
423 return -EPERM;
424 case 1:
425 if (seclvl_bd_claim(inode)) {
426 seclvl_printk(1, KERN_WARNING,
427 "Write to mounted block device "
428 "denied in secure level [%d]\n",
429 seclvl);
430 return -EPERM;
431 }
432 }
433 }
434 return 0;
435}
436
437
438
439
440static int seclvl_inode_setattr(struct dentry *dentry, struct iattr *iattr)
441{
442 if (seclvl > 0) {
443 if (iattr->ia_valid & ATTR_MODE)
444 if (iattr->ia_mode & S_ISUID ||
445 iattr->ia_mode & S_ISGID) {
446 seclvl_printk(1, KERN_WARNING, "Attempt to "
447 "modify SUID or SGID bit "
448 "denied in seclvl [%d]\n",
449 seclvl);
450 return -EPERM;
451 }
452 }
453 return 0;
454}
455
456
457static void seclvl_file_free_security(struct file *filp)
458{
459 struct dentry *dentry = filp->f_dentry;
460
461 if (dentry)
462 seclvl_bd_release(dentry->d_inode);
463}
464
465
466
467
468static int seclvl_umount(struct vfsmount *mnt, int flags)
469{
470 if (current->pid != 1 && seclvl == 2) {
471 seclvl_printk(1, KERN_WARNING, "Attempt to unmount in secure "
472 "level %d\n", seclvl);
473 return -EPERM;
474 }
475 return 0;
476}
477
478static struct security_operations seclvl_ops = {
479 .ptrace = seclvl_ptrace,
480 .capable = seclvl_capable,
481 .inode_permission = seclvl_inode_permission,
482 .inode_setattr = seclvl_inode_setattr,
483 .file_free_security = seclvl_file_free_security,
484 .settime = seclvl_settime,
485 .sb_umount = seclvl_umount,
486};
487
488
489
490
491static int processPassword(void)
492{
493 int rc = 0;
494 if (*passwd) {
495 char *p;
496
497 if (*sha1_passwd) {
498 seclvl_printk(0, KERN_ERR, "Error: Both "
499 "passwd and sha1_passwd "
500 "were set, but they are mutually "
501 "exclusive.\n");
502 return -EINVAL;
503 }
504
505 p = kstrdup(passwd, GFP_KERNEL);
506 if (p == NULL)
507 return -ENOMEM;
508
509 if ((rc = plaintext_to_sha1(hashedPassword, p, strlen(p))))
510 seclvl_printk(0, KERN_ERR, "Error: SHA1 support not "
511 "in kernel\n");
512
513 kfree (p);
514
515
516 } else if (*sha1_passwd) {
517 int i;
518 i = strlen(sha1_passwd);
519 if (i != (SHA1_DIGEST_SIZE * 2)) {
520 seclvl_printk(0, KERN_ERR, "Received [%d] bytes; "
521 "expected [%d] for the hexadecimal "
522 "representation of the SHA1 hash of "
523 "the password.\n",
524 i, (SHA1_DIGEST_SIZE * 2));
525 return -EINVAL;
526 }
527 while ((i -= 2) + 2) {
528 unsigned char tmp;
529 tmp = sha1_passwd[i + 2];
530 sha1_passwd[i + 2] = '\0';
531 hashedPassword[i / 2] = (unsigned char)
532 simple_strtol(&sha1_passwd[i], NULL, 16);
533 sha1_passwd[i + 2] = tmp;
534 }
535 }
536 return rc;
537}
538
539
540
541
542struct dentry *dir_ino, *seclvl_ino, *passwd_ino;
543
544static int seclvlfs_register(void)
545{
546 int rc = 0;
547
548 dir_ino = securityfs_create_dir("seclvl", NULL);
549
550 if (IS_ERR(dir_ino))
551 return PTR_ERR(dir_ino);
552
553 seclvl_ino = securityfs_create_file("seclvl", S_IRUGO | S_IWUSR,
554 dir_ino, &seclvl, &seclvl_file_ops);
555 if (IS_ERR(seclvl_ino)) {
556 rc = PTR_ERR(seclvl_ino);
557 goto out_deldir;
558 }
559 if (*passwd || *sha1_passwd) {
560 passwd_ino = securityfs_create_file("passwd", S_IRUGO | S_IWUSR,
561 dir_ino, NULL, &passwd_file_ops);
562 if (IS_ERR(passwd_ino)) {
563 rc = PTR_ERR(passwd_ino);
564 goto out_delf;
565 }
566 }
567 return rc;
568
569out_delf:
570 securityfs_remove(seclvl_ino);
571
572out_deldir:
573 securityfs_remove(dir_ino);
574
575 return rc;
576}
577
578static void seclvlfs_unregister(void)
579{
580 securityfs_remove(seclvl_ino);
581
582 if (*passwd || *sha1_passwd)
583 securityfs_remove(passwd_ino);
584
585 securityfs_remove(dir_ino);
586}
587
588
589
590
591static int __init seclvl_init(void)
592{
593 int rc = 0;
594 static char once;
595
596 if (verbosity < 0 || verbosity > 1) {
597 printk(KERN_ERR "Error: bad verbosity [%d]; only 0 or 1 "
598 "are valid values\n", verbosity);
599 rc = -EINVAL;
600 goto exit;
601 }
602 if (initlvl < -1 || initlvl > 2) {
603 seclvl_printk(0, KERN_ERR, "Error: bad initial securelevel "
604 "[%d].\n", initlvl);
605 rc = -EINVAL;
606 goto exit;
607 }
608 seclvl = initlvl;
609 if ((rc = processPassword())) {
610 seclvl_printk(0, KERN_ERR, "Error processing the password "
611 "module parameter(s): rc = [%d]\n", rc);
612 goto exit;
613 }
614
615 if ((rc = seclvlfs_register())) {
616 seclvl_printk(0, KERN_ERR, "Error registering with sysfs\n");
617 goto exit;
618 }
619
620 if (register_security(&seclvl_ops)) {
621 seclvl_printk(0, KERN_ERR,
622 "seclvl: Failure registering with the "
623 "kernel.\n");
624
625 rc = mod_reg_security(MY_NAME, &seclvl_ops);
626 if (rc) {
627 seclvl_printk(0, KERN_ERR, "seclvl: Failure "
628 "registering with primary security "
629 "module.\n");
630 seclvlfs_unregister();
631 goto exit;
632 }
633 secondary = 1;
634 }
635
636 seclvl_printk(0, KERN_INFO, "seclvl: Successfully initialized.\n");
637
638 if (once) {
639 once = 1;
640 seclvl_printk(0, KERN_INFO, "seclvl is going away. It has been "
641 "buggy for ages. Also, be warned that "
642 "Securelevels are useless.");
643 }
644 exit:
645 if (rc)
646 printk(KERN_ERR "seclvl: Error during initialization: rc = "
647 "[%d]\n", rc);
648 return rc;
649}
650
651
652
653
654static void __exit seclvl_exit(void)
655{
656 seclvlfs_unregister();
657
658 if (secondary)
659 mod_unreg_security(MY_NAME, &seclvl_ops);
660 else if (unregister_security(&seclvl_ops))
661 seclvl_printk(0, KERN_INFO,
662 "seclvl: Failure unregistering with the "
663 "kernel\n");
664}
665
666module_init(seclvl_init);
667module_exit(seclvl_exit);
668
669MODULE_AUTHOR("Michael A. Halcrow <mike@halcrow.us>");
670MODULE_DESCRIPTION("LSM implementation of the BSD Secure Levels");
671MODULE_LICENSE("GPL");
672