2 * BSD Secure Levels LSM
5 * Michael A. Halcrow <mike@halcrow.us>
6 * Serge Hallyn <hallyn@cs.wm.edu>
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>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
18 #include <linux/config.h>
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>
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/gfp.h>
35 #include <linux/sysfs.h>
37 #define SHA1_DIGEST_SIZE 20
40 * Module parameter that defines the initial secure level.
42 * When built as a module, it defaults to seclvl 1, which is the
43 * behavior of BSD secure levels. Note that this default behavior
44 * wrecks havoc on a machine when the seclvl module is compiled into
45 * the kernel. In that case, we default to seclvl 0.
47 #ifdef CONFIG_SECURITY_SECLVL_MODULE
48 static int initlvl = 1;
52 module_param(initlvl, int, 0);
53 MODULE_PARM_DESC(initlvl, "Initial secure level (defaults to 1)");
55 /* Module parameter that defines the verbosity level */
57 module_param(verbosity, int, 0);
58 MODULE_PARM_DESC(verbosity, "Initial verbosity level (0 or 1; defaults to "
59 "0, which is Quiet)");
62 * Optional password which can be passed in to bring seclvl to 0
63 * (i.e., for halt/reboot). Defaults to NULL (the passwd attribute
64 * file will not be registered in sysfs).
66 * This gets converted to its SHA1 hash when stored. It's probably
67 * not a good idea to use this parameter when loading seclvl from a
68 * script; use sha1_passwd instead.
71 #define MAX_PASSWD_SIZE 32
72 static char passwd[MAX_PASSWD_SIZE];
73 module_param_string(passwd, passwd, sizeof(passwd), 0);
74 MODULE_PARM_DESC(passwd,
75 "Plaintext of password that sets seclvl=0 when written to "
76 "(sysfs mount point)/seclvl/passwd\n");
79 * SHA1 hashed version of the optional password which can be passed in
80 * to bring seclvl to 0 (i.e., for halt/reboot). Must be in
81 * hexadecimal format (40 characters). Defaults to NULL (the passwd
82 * attribute file will not be registered in sysfs).
84 * Use the sha1sum utility to generate the SHA1 hash of a password:
86 * echo -n "secret" | sha1sum
88 #define MAX_SHA1_PASSWD 41
89 static char sha1_passwd[MAX_SHA1_PASSWD];
90 module_param_string(sha1_passwd, sha1_passwd, sizeof(sha1_passwd), 0);
91 MODULE_PARM_DESC(sha1_passwd,
92 "SHA1 hash (40 hexadecimal characters) of password that "
93 "sets seclvl=0 when plaintext password is written to "
94 "(sysfs mount point)/seclvl/passwd\n");
96 static int hideHash = 1;
97 module_param(hideHash, int, 0);
98 MODULE_PARM_DESC(hideHash, "When set to 0, reading seclvl/passwd from sysfs "
99 "will return the SHA1-hashed value of the password that "
100 "lowers the secure level to 0.\n");
102 #define MY_NAME "seclvl"
105 * This time-limits log writes to one per second.
107 #define seclvl_printk(verb, type, fmt, arg...) \
109 if (verbosity >= verb) { \
110 static unsigned long _prior; \
111 unsigned long _now = jiffies; \
112 if ((_now - _prior) > HZ) { \
113 printk(type "%s: %s: " fmt, \
114 MY_NAME, __FUNCTION__ , \
125 struct subsystem seclvl_subsys;
129 struct list_head slot_list;
134 * There is a seclvl_attribute struct for each file in sysfs.
136 * In our case, we have one of these structs for "passwd" and another
139 struct seclvl_attribute {
140 struct attribute attr;
141 ssize_t(*show) (struct seclvl_obj *, char *);
142 ssize_t(*store) (struct seclvl_obj *, const char *, size_t);
146 * When this function is called, one of the files in sysfs is being
147 * written to. attribute->store is a function pointer to whatever the
148 * struct seclvl_attribute store function pointer points to. It is
149 * unique for "passwd" and "seclvl".
152 seclvl_attr_store(struct kobject *kobj,
153 struct attribute *attr, const char *buf, size_t len)
155 struct seclvl_obj *obj = container_of(kobj, struct seclvl_obj, kobj);
156 struct seclvl_attribute *attribute =
157 container_of(attr, struct seclvl_attribute, attr);
158 return attribute->store ? attribute->store(obj, buf, len) : -EIO;
162 seclvl_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
164 struct seclvl_obj *obj = container_of(kobj, struct seclvl_obj, kobj);
165 struct seclvl_attribute *attribute =
166 container_of(attr, struct seclvl_attribute, attr);
167 return attribute->show ? attribute->show(obj, buf) : -EIO;
171 * Callback function pointers for show and store
173 static struct sysfs_ops seclvlfs_sysfs_ops = {
174 .show = seclvl_attr_show,
175 .store = seclvl_attr_store,
178 static struct kobj_type seclvl_ktype = {
179 .sysfs_ops = &seclvlfs_sysfs_ops
182 decl_subsys(seclvl, &seclvl_ktype, NULL);
185 * The actual security level. Ranges between -1 and 2 inclusive.
190 * flag to keep track of how we were registered
192 static int secondary;
195 * Verifies that the requested secure level is valid, given the current
198 static int seclvl_sanity(int reqlvl)
200 if ((reqlvl < -1) || (reqlvl > 2)) {
201 seclvl_printk(1, KERN_WARNING, "Attempt to set seclvl out of "
202 "range: [%d]\n", reqlvl);
205 if ((seclvl == 0) && (reqlvl == -1))
207 if (reqlvl < seclvl) {
208 seclvl_printk(1, KERN_WARNING, "Attempt to lower seclvl to "
216 * Called whenever the user reads the sysfs handle to this kernel
219 static ssize_t seclvl_read_file(struct seclvl_obj *obj, char *buff)
221 return snprintf(buff, PAGE_SIZE, "%d\n", seclvl);
225 * security level advancement rules:
226 * Valid levels are -1 through 2, inclusive.
227 * From -1, stuck. [ in case compiled into kernel ]
228 * From 0 or above, can only increment.
230 static int do_seclvl_advance(int newlvl)
232 if (newlvl <= seclvl) {
233 seclvl_printk(1, KERN_WARNING, "Cannot advance to seclvl "
238 seclvl_printk(1, KERN_WARNING, "Cannot advance to seclvl "
243 seclvl_printk(1, KERN_WARNING, "Not allowed to advance to "
244 "seclvl [%d]\n", seclvl);
252 * Called whenever the user writes to the sysfs handle to this kernel
253 * object (seclvl/seclvl). It expects a single-digit number.
256 seclvl_write_file(struct seclvl_obj *obj, const char *buff, size_t count)
259 if (count > 2 || (count == 2 && buff[1] != '\n')) {
260 seclvl_printk(1, KERN_WARNING, "Invalid value passed to "
261 "seclvl: [%s]\n", buff);
265 if (seclvl_sanity(val)) {
266 seclvl_printk(1, KERN_WARNING, "Illegal secure level "
267 "requested: [%d]\n", (int)val);
270 if (do_seclvl_advance(val)) {
271 seclvl_printk(0, KERN_ERR, "Failure advancing security level "
277 /* Generate sysfs_attr_seclvl */
278 static struct seclvl_attribute sysfs_attr_seclvl =
279 __ATTR(seclvl, (S_IFREG | S_IRUGO | S_IWUSR), seclvl_read_file,
282 static unsigned char hashedPassword[SHA1_DIGEST_SIZE];
285 * Called whenever the user reads the sysfs passwd handle.
287 static ssize_t seclvl_read_passwd(struct seclvl_obj *obj, char *buff)
289 /* So just how good *is* your password? :-) */
294 /* Security through obscurity */
297 while (i < SHA1_DIGEST_SIZE) {
298 snprintf(tmp, 3, "%02x", hashedPassword[i]);
299 strncat(buff, tmp, 2);
303 return ((SHA1_DIGEST_SIZE * 2) + 1);
307 * Converts a block of plaintext of into its SHA1 hashed value.
309 * It would be nice if crypto had a wrapper to do this for us linear
313 plaintext_to_sha1(unsigned char *hash, const char *plaintext, int len)
316 struct crypto_tfm *tfm;
317 struct scatterlist sg[1];
318 if (len > PAGE_SIZE) {
319 seclvl_printk(0, KERN_ERR, "Plaintext password too large (%d "
320 "characters). Largest possible is %lu "
321 "bytes.\n", len, PAGE_SIZE);
324 tfm = crypto_alloc_tfm("sha1", 0);
326 seclvl_printk(0, KERN_ERR,
327 "Failed to load transform for SHA1\n");
330 // Just get a new page; don't play around with page boundaries
332 pgVirtAddr = (char *)__get_free_page(GFP_KERNEL);
333 sg[0].page = virt_to_page(pgVirtAddr);
336 strncpy(pgVirtAddr, plaintext, len);
337 crypto_digest_init(tfm);
338 crypto_digest_update(tfm, sg, 1);
339 crypto_digest_final(tfm, hash);
340 crypto_free_tfm(tfm);
341 free_page((unsigned long)pgVirtAddr);
346 * Called whenever the user writes to the sysfs passwd handle to this kernel
347 * object. It hashes the password and compares the hashed results.
350 seclvl_write_passwd(struct seclvl_obj *obj, const char *buff, size_t count)
353 unsigned char tmp[SHA1_DIGEST_SIZE];
356 if (!*passwd && !*sha1_passwd) {
357 seclvl_printk(0, KERN_ERR, "Attempt to password-unlock the "
358 "seclvl module, but neither a plain text "
359 "password nor a SHA1 hashed password was "
360 "passed in as a module parameter! This is a "
361 "bug, since it should not be possible to be in "
362 "this part of the module; please tell a "
363 "maintainer about this event.\n");
367 /* ``echo "secret" > seclvl/passwd'' includes a newline */
368 if (buff[len - 1] == '\n') {
371 /* Hash the password, then compare the hashed values */
372 if ((rc = plaintext_to_sha1(tmp, buff, len))) {
373 seclvl_printk(0, KERN_ERR, "Error hashing password: rc = "
377 for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
378 if (hashedPassword[i] != tmp[i]) {
382 seclvl_printk(0, KERN_INFO,
383 "Password accepted; seclvl reduced to 0.\n");
388 /* Generate sysfs_attr_passwd */
389 static struct seclvl_attribute sysfs_attr_passwd =
390 __ATTR(passwd, (S_IFREG | S_IRUGO | S_IWUSR), seclvl_read_passwd,
391 seclvl_write_passwd);
394 * Explicitely disallow ptrace'ing the init process.
396 static int seclvl_ptrace(struct task_struct *parent, struct task_struct *child)
399 if (child->pid == 1) {
400 seclvl_printk(1, KERN_WARNING, "Attempt to ptrace "
401 "the init process dissallowed in "
402 "secure level %d\n", seclvl);
410 * Capability checks for seclvl. The majority of the policy
411 * enforcement for seclvl takes place here.
413 static int seclvl_capable(struct task_struct *tsk, int cap)
415 /* init can do anything it wants */
423 if (cap == CAP_LINUX_IMMUTABLE) {
424 seclvl_printk(1, KERN_WARNING, "Attempt to modify "
425 "the IMMUTABLE and/or APPEND extended "
426 "attribute on a file with the IMMUTABLE "
427 "and/or APPEND extended attribute set "
428 "denied in seclvl [%d]\n", seclvl);
430 } else if (cap == CAP_SYS_RAWIO) { // Somewhat broad...
431 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
432 "raw I/O while in secure level [%d] "
435 } else if (cap == CAP_NET_ADMIN) {
436 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
437 "network administrative task while "
438 "in secure level [%d] denied\n", seclvl);
440 } else if (cap == CAP_SETUID) {
441 seclvl_printk(1, KERN_WARNING, "Attempt to setuid "
442 "while in secure level [%d] denied\n",
445 } else if (cap == CAP_SETGID) {
446 seclvl_printk(1, KERN_WARNING, "Attempt to setgid "
447 "while in secure level [%d] denied\n",
449 } else if (cap == CAP_SYS_MODULE) {
450 seclvl_printk(1, KERN_WARNING, "Attempt to perform "
451 "a module operation while in secure "
452 "level [%d] denied\n", seclvl);
460 if (cap_is_fs_cap(cap) ? tsk->fsuid == 0 : tsk->euid == 0)
461 return 0; /* capability granted */
462 seclvl_printk(1, KERN_WARNING, "Capability denied\n");
463 return -EPERM; /* capability denied */
467 * Disallow reversing the clock in seclvl > 1
469 static int seclvl_settime(struct timespec *tv, struct timezone *tz)
473 now = current_kernel_time();
474 if (tv->tv_sec < now.tv_sec ||
475 (tv->tv_sec == now.tv_sec && tv->tv_nsec < now.tv_nsec)) {
476 seclvl_printk(1, KERN_WARNING, "Attempt to decrement "
477 "time in secure level %d denied: "
478 "current->pid = [%d], "
479 "current->group_leader->pid = [%d]\n",
480 seclvl, current->pid,
481 current->group_leader->pid);
483 } /* if attempt to decrement time */
484 } /* if seclvl > 1 */
488 /* claim the blockdev to exclude mounters, release on file close */
489 static int seclvl_bd_claim(struct inode *inode)
492 struct block_device *bdev = NULL;
493 dev_t dev = inode->i_rdev;
494 bdev = open_by_devnum(dev, FMODE_WRITE);
496 if (bd_claim(bdev, &holder)) {
500 /* claimed, mark it to release on close */
501 inode->i_security = current;
506 /* release the blockdev if you claimed it */
507 static void seclvl_bd_release(struct inode *inode)
509 if (inode && S_ISBLK(inode->i_mode) && inode->i_security == current) {
510 struct block_device *bdev = inode->i_bdev;
514 inode->i_security = NULL;
520 * Security for writes to block devices is regulated by this seclvl
521 * function. Deny all writes to block devices in seclvl 2. In
522 * seclvl 1, we only deny writes to *mounted* block devices.
525 seclvl_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
527 if (current->pid != 1 && S_ISBLK(inode->i_mode) && (mask & MAY_WRITE)) {
530 seclvl_printk(1, KERN_WARNING, "Write to block device "
531 "denied in secure level [%d]\n", seclvl);
534 if (seclvl_bd_claim(inode)) {
535 seclvl_printk(1, KERN_WARNING,
536 "Write to mounted block device "
537 "denied in secure level [%d]\n",
547 * The SUID and SGID bits cannot be set in seclvl >= 1
549 static int seclvl_inode_setattr(struct dentry *dentry, struct iattr *iattr)
552 if (iattr->ia_valid & ATTR_MODE)
553 if (iattr->ia_mode & S_ISUID ||
554 iattr->ia_mode & S_ISGID) {
555 seclvl_printk(1, KERN_WARNING, "Attempt to "
556 "modify SUID or SGID bit "
557 "denied in seclvl [%d]\n",
565 /* release busied block devices */
566 static void seclvl_file_free_security(struct file *filp)
568 struct dentry *dentry = filp->f_dentry;
569 struct inode *inode = NULL;
572 inode = dentry->d_inode;
573 seclvl_bd_release(inode);
578 * Cannot unmount in secure level 2
580 static int seclvl_umount(struct vfsmount *mnt, int flags)
582 if (current->pid == 1) {
586 seclvl_printk(1, KERN_WARNING, "Attempt to unmount in secure "
587 "level %d\n", seclvl);
593 static struct security_operations seclvl_ops = {
594 .ptrace = seclvl_ptrace,
595 .capable = seclvl_capable,
596 .inode_permission = seclvl_inode_permission,
597 .inode_setattr = seclvl_inode_setattr,
598 .file_free_security = seclvl_file_free_security,
599 .settime = seclvl_settime,
600 .sb_umount = seclvl_umount,
604 * Process the password-related module parameters
606 static int processPassword(void)
609 hashedPassword[0] = '\0';
612 seclvl_printk(0, KERN_ERR, "Error: Both "
613 "passwd and sha1_passwd "
614 "were set, but they are mutually "
618 if ((rc = plaintext_to_sha1(hashedPassword, passwd,
620 seclvl_printk(0, KERN_ERR, "Error: SHA1 support not "
624 /* All static data goes to the BSS, which zero's the
625 * plaintext password out for us. */
626 } else if (*sha1_passwd) { // Base 16
628 i = strlen(sha1_passwd);
629 if (i != (SHA1_DIGEST_SIZE * 2)) {
630 seclvl_printk(0, KERN_ERR, "Received [%d] bytes; "
631 "expected [%d] for the hexadecimal "
632 "representation of the SHA1 hash of "
634 i, (SHA1_DIGEST_SIZE * 2));
637 while ((i -= 2) + 2) {
639 tmp = sha1_passwd[i + 2];
640 sha1_passwd[i + 2] = '\0';
641 hashedPassword[i / 2] = (unsigned char)
642 simple_strtol(&sha1_passwd[i], NULL, 16);
643 sha1_passwd[i + 2] = tmp;
650 * Sysfs registrations
652 static int doSysfsRegistrations(void)
655 if ((rc = subsystem_register(&seclvl_subsys))) {
656 seclvl_printk(0, KERN_WARNING,
657 "Error [%d] registering seclvl subsystem\n", rc);
660 sysfs_create_file(&seclvl_subsys.kset.kobj, &sysfs_attr_seclvl.attr);
661 if (*passwd || *sha1_passwd) {
662 sysfs_create_file(&seclvl_subsys.kset.kobj,
663 &sysfs_attr_passwd.attr);
669 * Initialize the seclvl module.
671 static int __init seclvl_init(void)
674 if (verbosity < 0 || verbosity > 1) {
675 printk(KERN_ERR "Error: bad verbosity [%d]; only 0 or 1 "
676 "are valid values\n", verbosity);
680 sysfs_attr_seclvl.attr.owner = THIS_MODULE;
681 sysfs_attr_passwd.attr.owner = THIS_MODULE;
682 if (initlvl < -1 || initlvl > 2) {
683 seclvl_printk(0, KERN_ERR, "Error: bad initial securelevel "
689 if ((rc = processPassword())) {
690 seclvl_printk(0, KERN_ERR, "Error processing the password "
691 "module parameter(s): rc = [%d]\n", rc);
694 /* register ourselves with the security framework */
695 if (register_security(&seclvl_ops)) {
696 seclvl_printk(0, KERN_ERR,
697 "seclvl: Failure registering with the "
699 /* try registering with primary module */
700 rc = mod_reg_security(MY_NAME, &seclvl_ops);
702 seclvl_printk(0, KERN_ERR, "seclvl: Failure "
703 "registering with primary security "
706 } /* if primary module registered */
708 } /* if we registered ourselves with the security framework */
709 if ((rc = doSysfsRegistrations())) {
710 seclvl_printk(0, KERN_ERR, "Error registering with sysfs\n");
713 seclvl_printk(0, KERN_INFO, "seclvl: Successfully initialized.\n");
716 printk(KERN_ERR "seclvl: Error during initialization: rc = "
723 * Remove the seclvl module.
725 static void __exit seclvl_exit(void)
727 sysfs_remove_file(&seclvl_subsys.kset.kobj, &sysfs_attr_seclvl.attr);
728 if (*passwd || *sha1_passwd) {
729 sysfs_remove_file(&seclvl_subsys.kset.kobj,
730 &sysfs_attr_passwd.attr);
732 subsystem_unregister(&seclvl_subsys);
733 if (secondary == 1) {
734 mod_unreg_security(MY_NAME, &seclvl_ops);
735 } else if (unregister_security(&seclvl_ops)) {
736 seclvl_printk(0, KERN_INFO,
737 "seclvl: Failure unregistering with the "
742 module_init(seclvl_init);
743 module_exit(seclvl_exit);
745 MODULE_AUTHOR("Michael A. Halcrow <mike@halcrow.us>");
746 MODULE_DESCRIPTION("LSM implementation of the BSD Secure Levels");
747 MODULE_LICENSE("GPL");