seclvl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /**
  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>
  11. *
  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.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/security.h>
  23. #include <linux/netlink.h>
  24. #include <linux/fs.h>
  25. #include <linux/namei.h>
  26. #include <linux/mount.h>
  27. #include <linux/capability.h>
  28. #include <linux/time.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/kobject.h>
  31. #include <linux/crypto.h>
  32. #include <asm/scatterlist.h>
  33. #include <linux/gfp.h>
  34. #include <linux/sysfs.h>
  35. #define SHA1_DIGEST_SIZE 20
  36. /**
  37. * Module parameter that defines the initial secure level.
  38. *
  39. * When built as a module, it defaults to seclvl 1, which is the
  40. * behavior of BSD secure levels. Note that this default behavior
  41. * wrecks havoc on a machine when the seclvl module is compiled into
  42. * the kernel. In that case, we default to seclvl 0.
  43. */
  44. #ifdef CONFIG_SECURITY_SECLVL_MODULE
  45. static int initlvl = 1;
  46. #else
  47. static int initlvl;
  48. #endif
  49. module_param(initlvl, int, 0);
  50. MODULE_PARM_DESC(initlvl, "Initial secure level (defaults to 1)");
  51. /* Module parameter that defines the verbosity level */
  52. static int verbosity;
  53. module_param(verbosity, int, 0);
  54. MODULE_PARM_DESC(verbosity, "Initial verbosity level (0 or 1; defaults to "
  55. "0, which is Quiet)");
  56. /**
  57. * Optional password which can be passed in to bring seclvl to 0
  58. * (i.e., for halt/reboot). Defaults to NULL (the passwd attribute
  59. * file will not be registered in sysfs).
  60. *
  61. * This gets converted to its SHA1 hash when stored. It's probably
  62. * not a good idea to use this parameter when loading seclvl from a
  63. * script; use sha1_passwd instead.
  64. */
  65. #define MAX_PASSWD_SIZE 32
  66. static char passwd[MAX_PASSWD_SIZE];
  67. module_param_string(passwd, passwd, sizeof(passwd), 0);
  68. MODULE_PARM_DESC(passwd,
  69. "Plaintext of password that sets seclvl=0 when written to "
  70. "(sysfs mount point)/seclvl/passwd\n");
  71. /**
  72. * SHA1 hashed version of the optional password which can be passed in
  73. * to bring seclvl to 0 (i.e., for halt/reboot). Must be in
  74. * hexadecimal format (40 characters). Defaults to NULL (the passwd
  75. * attribute file will not be registered in sysfs).
  76. *
  77. * Use the sha1sum utility to generate the SHA1 hash of a password:
  78. *
  79. * echo -n "secret" | sha1sum
  80. */
  81. #define MAX_SHA1_PASSWD 41
  82. static char sha1_passwd[MAX_SHA1_PASSWD];
  83. module_param_string(sha1_passwd, sha1_passwd, sizeof(sha1_passwd), 0);
  84. MODULE_PARM_DESC(sha1_passwd,
  85. "SHA1 hash (40 hexadecimal characters) of password that "
  86. "sets seclvl=0 when plaintext password is written to "
  87. "(sysfs mount point)/seclvl/passwd\n");
  88. static int hideHash = 1;
  89. module_param(hideHash, int, 0);
  90. MODULE_PARM_DESC(hideHash, "When set to 0, reading seclvl/passwd from sysfs "
  91. "will return the SHA1-hashed value of the password that "
  92. "lowers the secure level to 0.\n");
  93. #define MY_NAME "seclvl"
  94. /**
  95. * This time-limits log writes to one per second.
  96. */
  97. #define seclvl_printk(verb, type, fmt, arg...) \
  98. do { \
  99. if (verbosity >= verb) { \
  100. static unsigned long _prior; \
  101. unsigned long _now = jiffies; \
  102. if ((_now - _prior) > HZ) { \
  103. printk(type "%s: %s: " fmt, \
  104. MY_NAME, __FUNCTION__ , \
  105. ## arg); \
  106. _prior = _now; \
  107. } \
  108. } \
  109. } while (0)
  110. /**
  111. * kobject stuff
  112. */
  113. struct subsystem seclvl_subsys;
  114. struct seclvl_obj {
  115. char *name;
  116. struct list_head slot_list;
  117. struct kobject kobj;
  118. };
  119. /**
  120. * There is a seclvl_attribute struct for each file in sysfs.
  121. *
  122. * In our case, we have one of these structs for "passwd" and another
  123. * for "seclvl".
  124. */
  125. struct seclvl_attribute {
  126. struct attribute attr;
  127. ssize_t(*show) (struct seclvl_obj *, char *);
  128. ssize_t(*store) (struct seclvl_obj *, const char *, size_t);
  129. };
  130. /**
  131. * When this function is called, one of the files in sysfs is being
  132. * written to. attribute->store is a function pointer to whatever the
  133. * struct seclvl_attribute store function pointer points to. It is
  134. * unique for "passwd" and "seclvl".
  135. */
  136. static ssize_t
  137. seclvl_attr_store(struct kobject *kobj,
  138. struct attribute *attr, const char *buf, size_t len)
  139. {
  140. struct seclvl_obj *obj = container_of(kobj, struct seclvl_obj, kobj);
  141. struct seclvl_attribute *attribute =
  142. container_of(attr, struct seclvl_attribute, attr);
  143. return attribute->store ? attribute->store(obj, buf, len) : -EIO;
  144. }
  145. static ssize_t
  146. seclvl_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
  147. {
  148. struct seclvl_obj *obj = container_of(kobj, struct seclvl_obj, kobj);
  149. struct seclvl_attribute *attribute =
  150. container_of(attr, struct seclvl_attribute, attr);
  151. return attribute->show ? attribute->show(obj, buf) : -EIO;
  152. }
  153. /**
  154. * Callback function pointers for show and store
  155. */
  156. static struct sysfs_ops seclvlfs_sysfs_ops = {
  157. .show = seclvl_attr_show,
  158. .store = seclvl_attr_store,
  159. };
  160. static struct kobj_type seclvl_ktype = {
  161. .sysfs_ops = &seclvlfs_sysfs_ops
  162. };
  163. decl_subsys(seclvl, &seclvl_ktype, NULL);
  164. /**
  165. * The actual security level. Ranges between -1 and 2 inclusive.
  166. */
  167. static int seclvl;
  168. /**
  169. * flag to keep track of how we were registered
  170. */
  171. static int secondary;
  172. /**
  173. * Verifies that the requested secure level is valid, given the current
  174. * secure level.
  175. */
  176. static int seclvl_sanity(int reqlvl)
  177. {
  178. if ((reqlvl < -1) || (reqlvl > 2)) {
  179. seclvl_printk(1, KERN_WARNING, "Attempt to set seclvl out of "
  180. "range: [%d]\n", reqlvl);
  181. return -EINVAL;
  182. }
  183. if ((seclvl == 0) && (reqlvl == -1))
  184. return 0;
  185. if (reqlvl < seclvl) {
  186. seclvl_printk(1, KERN_WARNING, "Attempt to lower seclvl to "
  187. "[%d]\n", reqlvl);
  188. return -EPERM;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * Called whenever the user reads the sysfs handle to this kernel
  194. * object
  195. */
  196. static ssize_t seclvl_read_file(struct seclvl_obj *obj, char *buff)
  197. {
  198. return snprintf(buff, PAGE_SIZE, "%d\n", seclvl);
  199. }
  200. /**
  201. * security level advancement rules:
  202. * Valid levels are -1 through 2, inclusive.
  203. * From -1, stuck. [ in case compiled into kernel ]
  204. * From 0 or above, can only increment.
  205. */
  206. static int do_seclvl_advance(int newlvl)
  207. {
  208. if (newlvl <= seclvl) {
  209. seclvl_printk(1, KERN_WARNING, "Cannot advance to seclvl "
  210. "[%d]\n", newlvl);
  211. return -EINVAL;
  212. }
  213. if (newlvl > 2) {
  214. seclvl_printk(1, KERN_WARNING, "Cannot advance to seclvl "
  215. "[%d]\n", newlvl);
  216. return -EINVAL;
  217. }
  218. if (seclvl == -1) {
  219. seclvl_printk(1, KERN_WARNING, "Not allowed to advance to "
  220. "seclvl [%d]\n", seclvl);
  221. return -EPERM;
  222. }
  223. seclvl = newlvl;
  224. return 0;
  225. }
  226. /**
  227. * Called whenever the user writes to the sysfs handle to this kernel
  228. * object (seclvl/seclvl). It expects a single-digit number.
  229. */
  230. static ssize_t
  231. seclvl_write_file(struct seclvl_obj *obj, const char *buff, size_t count)
  232. {
  233. unsigned long val;
  234. if (count > 2 || (count == 2 && buff[1] != '\n')) {
  235. seclvl_printk(1, KERN_WARNING, "Invalid value passed to "
  236. "seclvl: [%s]\n", buff);
  237. return -EINVAL;
  238. }
  239. val = buff[0] - 48;
  240. if (seclvl_sanity(val)) {
  241. seclvl_printk(1, KERN_WARNING, "Illegal secure level "
  242. "requested: [%d]\n", (int)val);
  243. return -EPERM;
  244. }
  245. if (do_seclvl_advance(val)) {
  246. seclvl_printk(0, KERN_ERR, "Failure advancing security level "
  247. "to %lu\n", val);
  248. }
  249. return count;
  250. }
  251. /* Generate sysfs_attr_seclvl */
  252. static struct seclvl_attribute sysfs_attr_seclvl =
  253. __ATTR(seclvl, (S_IFREG | S_IRUGO | S_IWUSR), seclvl_read_file,
  254. seclvl_write_file);
  255. static unsigned char hashedPassword[SHA1_DIGEST_SIZE];
  256. /**
  257. * Called whenever the user reads the sysfs passwd handle.
  258. */
  259. static ssize_t seclvl_read_passwd(struct seclvl_obj *obj, char *buff)
  260. {
  261. /* So just how good *is* your password? :-) */
  262. char tmp[3];
  263. int i = 0;
  264. buff[0] = '\0';
  265. if (hideHash) {
  266. /* Security through obscurity */
  267. return 0;
  268. }
  269. while (i < SHA1_DIGEST_SIZE) {
  270. snprintf(tmp, 3, "%02x", hashedPassword[i]);
  271. strncat(buff, tmp, 2);
  272. i++;
  273. }
  274. strcat(buff, "\n");
  275. return ((SHA1_DIGEST_SIZE * 2) + 1);
  276. }
  277. /**
  278. * Converts a block of plaintext of into its SHA1 hashed value.
  279. *
  280. * It would be nice if crypto had a wrapper to do this for us linear
  281. * people...
  282. */
  283. static int
  284. plaintext_to_sha1(unsigned char *hash, const char *plaintext, int len)
  285. {
  286. char *pgVirtAddr;
  287. struct crypto_tfm *tfm;
  288. struct scatterlist sg[1];
  289. if (len > PAGE_SIZE) {
  290. seclvl_printk(0, KERN_ERR, "Plaintext password too large (%d "
  291. "characters). Largest possible is %lu "
  292. "bytes.\n", len, PAGE_SIZE);
  293. return -ENOMEM;
  294. }
  295. tfm = crypto_alloc_tfm("sha1", CRYPTO_TFM_REQ_MAY_SLEEP);
  296. if (tfm == NULL) {
  297. seclvl_printk(0, KERN_ERR,
  298. "Failed to load transform for SHA1\n");
  299. return -ENOSYS;
  300. }
  301. // Just get a new page; don't play around with page boundaries
  302. // and scatterlists.
  303. pgVirtAddr = (char *)__get_free_page(GFP_KERNEL);
  304. sg[0].page = virt_to_page(pgVirtAddr);
  305. sg[0].offset = 0;
  306. sg[0].length = len;
  307. strncpy(pgVirtAddr, plaintext, len);
  308. crypto_digest_init(tfm);
  309. crypto_digest_update(tfm, sg, 1);
  310. crypto_digest_final(tfm, hash);
  311. crypto_free_tfm(tfm);
  312. free_page((unsigned long)pgVirtAddr);
  313. return 0;
  314. }
  315. /**
  316. * Called whenever the user writes to the sysfs passwd handle to this kernel
  317. * object. It hashes the password and compares the hashed results.
  318. */
  319. static ssize_t
  320. seclvl_write_passwd(struct seclvl_obj *obj, const char *buff, size_t count)
  321. {
  322. int i;
  323. unsigned char tmp[SHA1_DIGEST_SIZE];
  324. int rc;
  325. int len;
  326. if (!*passwd && !*sha1_passwd) {
  327. seclvl_printk(0, KERN_ERR, "Attempt to password-unlock the "
  328. "seclvl module, but neither a plain text "
  329. "password nor a SHA1 hashed password was "
  330. "passed in as a module parameter! This is a "
  331. "bug, since it should not be possible to be in "
  332. "this part of the module; please tell a "
  333. "maintainer about this event.\n");
  334. return -EINVAL;
  335. }
  336. len = strlen(buff);
  337. /* ``echo "secret" > seclvl/passwd'' includes a newline */
  338. if (buff[len - 1] == '\n') {
  339. len--;
  340. }
  341. /* Hash the password, then compare the hashed values */
  342. if ((rc = plaintext_to_sha1(tmp, buff, len))) {
  343. seclvl_printk(0, KERN_ERR, "Error hashing password: rc = "
  344. "[%d]\n", rc);
  345. return rc;
  346. }
  347. for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
  348. if (hashedPassword[i] != tmp[i]) {
  349. return -EPERM;
  350. }
  351. }
  352. seclvl_printk(0, KERN_INFO,
  353. "Password accepted; seclvl reduced to 0.\n");
  354. seclvl = 0;
  355. return count;
  356. }
  357. /* Generate sysfs_attr_passwd */
  358. static struct seclvl_attribute sysfs_attr_passwd =
  359. __ATTR(passwd, (S_IFREG | S_IRUGO | S_IWUSR), seclvl_read_passwd,
  360. seclvl_write_passwd);
  361. /**
  362. * Explicitely disallow ptrace'ing the init process.
  363. */
  364. static int seclvl_ptrace(struct task_struct *parent, struct task_struct *child)
  365. {
  366. if (seclvl >= 0) {
  367. if (child->pid == 1) {
  368. seclvl_printk(1, KERN_WARNING, "Attempt to ptrace "
  369. "the init process dissallowed in "
  370. "secure level %d\n", seclvl);
  371. return -EPERM;
  372. }
  373. }
  374. return 0;
  375. }
  376. /**
  377. * Capability checks for seclvl. The majority of the policy
  378. * enforcement for seclvl takes place here.
  379. */
  380. static int seclvl_capable(struct task_struct *tsk, int cap)
  381. {
  382. /* init can do anything it wants */
  383. if (tsk->pid == 1)
  384. return 0;
  385. switch (seclvl) {
  386. case 2:
  387. /* fall through */
  388. case 1:
  389. if (cap == CAP_LINUX_IMMUTABLE) {
  390. seclvl_printk(1, KERN_WARNING, "Attempt to modify "
  391. "the IMMUTABLE and/or APPEND extended "
  392. "attribute on a file with the IMMUTABLE "
  393. "and/or APPEND extended attribute set "
  394. "denied in seclvl [%d]\n", seclvl);
  395. return -EPERM;
  396. } else if (cap == CAP_SYS_RAWIO) { // Somewhat broad...
  397. seclvl_printk(1, KERN_WARNING, "Attempt to perform "
  398. "raw I/O while in secure level [%d] "
  399. "denied\n", seclvl);
  400. return -EPERM;
  401. } else if (cap == CAP_NET_ADMIN) {
  402. seclvl_printk(1, KERN_WARNING, "Attempt to perform "
  403. "network administrative task while "
  404. "in secure level [%d] denied\n", seclvl);
  405. return -EPERM;
  406. } else if (cap == CAP_SETUID) {
  407. seclvl_printk(1, KERN_WARNING, "Attempt to setuid "
  408. "while in secure level [%d] denied\n",
  409. seclvl);
  410. return -EPERM;
  411. } else if (cap == CAP_SETGID) {
  412. seclvl_printk(1, KERN_WARNING, "Attempt to setgid "
  413. "while in secure level [%d] denied\n",
  414. seclvl);
  415. } else if (cap == CAP_SYS_MODULE) {
  416. seclvl_printk(1, KERN_WARNING, "Attempt to perform "
  417. "a module operation while in secure "
  418. "level [%d] denied\n", seclvl);
  419. return -EPERM;
  420. }
  421. break;
  422. default:
  423. break;
  424. }
  425. /* from dummy.c */
  426. if (cap_is_fs_cap(cap) ? tsk->fsuid == 0 : tsk->euid == 0)
  427. return 0; /* capability granted */
  428. seclvl_printk(1, KERN_WARNING, "Capability denied\n");
  429. return -EPERM; /* capability denied */
  430. }
  431. /**
  432. * Disallow reversing the clock in seclvl > 1
  433. */
  434. static int seclvl_settime(struct timespec *tv, struct timezone *tz)
  435. {
  436. struct timespec now;
  437. if (seclvl > 1) {
  438. now = current_kernel_time();
  439. if (tv->tv_sec < now.tv_sec ||
  440. (tv->tv_sec == now.tv_sec && tv->tv_nsec < now.tv_nsec)) {
  441. seclvl_printk(1, KERN_WARNING, "Attempt to decrement "
  442. "time in secure level %d denied: "
  443. "current->pid = [%d], "
  444. "current->group_leader->pid = [%d]\n",
  445. seclvl, current->pid,
  446. current->group_leader->pid);
  447. return -EPERM;
  448. } /* if attempt to decrement time */
  449. } /* if seclvl > 1 */
  450. return 0;
  451. }
  452. /* claim the blockdev to exclude mounters, release on file close */
  453. static int seclvl_bd_claim(struct inode *inode)
  454. {
  455. int holder;
  456. struct block_device *bdev = NULL;
  457. dev_t dev = inode->i_rdev;
  458. bdev = open_by_devnum(dev, FMODE_WRITE);
  459. if (bdev) {
  460. if (bd_claim(bdev, &holder)) {
  461. blkdev_put(bdev);
  462. return -EPERM;
  463. }
  464. /* claimed, mark it to release on close */
  465. inode->i_security = current;
  466. }
  467. return 0;
  468. }
  469. /* release the blockdev if you claimed it */
  470. static void seclvl_bd_release(struct inode *inode)
  471. {
  472. if (inode && S_ISBLK(inode->i_mode) && inode->i_security == current) {
  473. struct block_device *bdev = inode->i_bdev;
  474. if (bdev) {
  475. bd_release(bdev);
  476. blkdev_put(bdev);
  477. inode->i_security = NULL;
  478. }
  479. }
  480. }
  481. /**
  482. * Security for writes to block devices is regulated by this seclvl
  483. * function. Deny all writes to block devices in seclvl 2. In
  484. * seclvl 1, we only deny writes to *mounted* block devices.
  485. */
  486. static int
  487. seclvl_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
  488. {
  489. if (current->pid != 1 && S_ISBLK(inode->i_mode) && (mask & MAY_WRITE)) {
  490. switch (seclvl) {
  491. case 2:
  492. seclvl_printk(1, KERN_WARNING, "Write to block device "
  493. "denied in secure level [%d]\n", seclvl);
  494. return -EPERM;
  495. case 1:
  496. if (seclvl_bd_claim(inode)) {
  497. seclvl_printk(1, KERN_WARNING,
  498. "Write to mounted block device "
  499. "denied in secure level [%d]\n",
  500. seclvl);
  501. return -EPERM;
  502. }
  503. }
  504. }
  505. return 0;
  506. }
  507. /**
  508. * The SUID and SGID bits cannot be set in seclvl >= 1
  509. */
  510. static int seclvl_inode_setattr(struct dentry *dentry, struct iattr *iattr)
  511. {
  512. if (seclvl > 0) {
  513. if (iattr->ia_valid & ATTR_MODE)
  514. if (iattr->ia_mode & S_ISUID ||
  515. iattr->ia_mode & S_ISGID) {
  516. seclvl_printk(1, KERN_WARNING, "Attempt to "
  517. "modify SUID or SGID bit "
  518. "denied in seclvl [%d]\n",
  519. seclvl);
  520. return -EPERM;
  521. }
  522. }
  523. return 0;
  524. }
  525. /* release busied block devices */
  526. static void seclvl_file_free_security(struct file *filp)
  527. {
  528. struct dentry *dentry = filp->f_dentry;
  529. struct inode *inode = NULL;
  530. if (dentry) {
  531. inode = dentry->d_inode;
  532. seclvl_bd_release(inode);
  533. }
  534. }
  535. /**
  536. * Cannot unmount in secure level 2
  537. */
  538. static int seclvl_umount(struct vfsmount *mnt, int flags)
  539. {
  540. if (current->pid == 1) {
  541. return 0;
  542. }
  543. if (seclvl == 2) {
  544. seclvl_printk(1, KERN_WARNING, "Attempt to unmount in secure "
  545. "level %d\n", seclvl);
  546. return -EPERM;
  547. }
  548. return 0;
  549. }
  550. static struct security_operations seclvl_ops = {
  551. .ptrace = seclvl_ptrace,
  552. .capable = seclvl_capable,
  553. .inode_permission = seclvl_inode_permission,
  554. .inode_setattr = seclvl_inode_setattr,
  555. .file_free_security = seclvl_file_free_security,
  556. .settime = seclvl_settime,
  557. .sb_umount = seclvl_umount,
  558. };
  559. /**
  560. * Process the password-related module parameters
  561. */
  562. static int processPassword(void)
  563. {
  564. int rc = 0;
  565. hashedPassword[0] = '\0';
  566. if (*passwd) {
  567. if (*sha1_passwd) {
  568. seclvl_printk(0, KERN_ERR, "Error: Both "
  569. "passwd and sha1_passwd "
  570. "were set, but they are mutually "
  571. "exclusive.\n");
  572. return -EINVAL;
  573. }
  574. if ((rc = plaintext_to_sha1(hashedPassword, passwd,
  575. strlen(passwd)))) {
  576. seclvl_printk(0, KERN_ERR, "Error: SHA1 support not "
  577. "in kernel\n");
  578. return rc;
  579. }
  580. /* All static data goes to the BSS, which zero's the
  581. * plaintext password out for us. */
  582. } else if (*sha1_passwd) { // Base 16
  583. int i;
  584. i = strlen(sha1_passwd);
  585. if (i != (SHA1_DIGEST_SIZE * 2)) {
  586. seclvl_printk(0, KERN_ERR, "Received [%d] bytes; "
  587. "expected [%d] for the hexadecimal "
  588. "representation of the SHA1 hash of "
  589. "the password.\n",
  590. i, (SHA1_DIGEST_SIZE * 2));
  591. return -EINVAL;
  592. }
  593. while ((i -= 2) + 2) {
  594. unsigned char tmp;
  595. tmp = sha1_passwd[i + 2];
  596. sha1_passwd[i + 2] = '\0';
  597. hashedPassword[i / 2] = (unsigned char)
  598. simple_strtol(&sha1_passwd[i], NULL, 16);
  599. sha1_passwd[i + 2] = tmp;
  600. }
  601. }
  602. return 0;
  603. }
  604. /**
  605. * Sysfs registrations
  606. */
  607. static int doSysfsRegistrations(void)
  608. {
  609. int rc = 0;
  610. if ((rc = subsystem_register(&seclvl_subsys))) {
  611. seclvl_printk(0, KERN_WARNING,
  612. "Error [%d] registering seclvl subsystem\n", rc);
  613. return rc;
  614. }
  615. sysfs_create_file(&seclvl_subsys.kset.kobj, &sysfs_attr_seclvl.attr);
  616. if (*passwd || *sha1_passwd) {
  617. sysfs_create_file(&seclvl_subsys.kset.kobj,
  618. &sysfs_attr_passwd.attr);
  619. }
  620. return 0;
  621. }
  622. /**
  623. * Initialize the seclvl module.
  624. */
  625. static int __init seclvl_init(void)
  626. {
  627. int rc = 0;
  628. if (verbosity < 0 || verbosity > 1) {
  629. printk(KERN_ERR "Error: bad verbosity [%d]; only 0 or 1 "
  630. "are valid values\n", verbosity);
  631. rc = -EINVAL;
  632. goto exit;
  633. }
  634. sysfs_attr_seclvl.attr.owner = THIS_MODULE;
  635. sysfs_attr_passwd.attr.owner = THIS_MODULE;
  636. if (initlvl < -1 || initlvl > 2) {
  637. seclvl_printk(0, KERN_ERR, "Error: bad initial securelevel "
  638. "[%d].\n", initlvl);
  639. rc = -EINVAL;
  640. goto exit;
  641. }
  642. seclvl = initlvl;
  643. if ((rc = processPassword())) {
  644. seclvl_printk(0, KERN_ERR, "Error processing the password "
  645. "module parameter(s): rc = [%d]\n", rc);
  646. goto exit;
  647. }
  648. /* register ourselves with the security framework */
  649. if (register_security(&seclvl_ops)) {
  650. seclvl_printk(0, KERN_ERR,
  651. "seclvl: Failure registering with the "
  652. "kernel.\n");
  653. /* try registering with primary module */
  654. rc = mod_reg_security(MY_NAME, &seclvl_ops);
  655. if (rc) {
  656. seclvl_printk(0, KERN_ERR, "seclvl: Failure "
  657. "registering with primary security "
  658. "module.\n");
  659. goto exit;
  660. } /* if primary module registered */
  661. secondary = 1;
  662. } /* if we registered ourselves with the security framework */
  663. if ((rc = doSysfsRegistrations())) {
  664. seclvl_printk(0, KERN_ERR, "Error registering with sysfs\n");
  665. goto exit;
  666. }
  667. seclvl_printk(0, KERN_INFO, "seclvl: Successfully initialized.\n");
  668. exit:
  669. if (rc) {
  670. printk(KERN_ERR "seclvl: Error during initialization: rc = "
  671. "[%d]\n", rc);
  672. }
  673. return rc;
  674. }
  675. /**
  676. * Remove the seclvl module.
  677. */
  678. static void __exit seclvl_exit(void)
  679. {
  680. sysfs_remove_file(&seclvl_subsys.kset.kobj, &sysfs_attr_seclvl.attr);
  681. if (*passwd || *sha1_passwd) {
  682. sysfs_remove_file(&seclvl_subsys.kset.kobj,
  683. &sysfs_attr_passwd.attr);
  684. }
  685. subsystem_unregister(&seclvl_subsys);
  686. if (secondary == 1) {
  687. mod_unreg_security(MY_NAME, &seclvl_ops);
  688. } else if (unregister_security(&seclvl_ops)) {
  689. seclvl_printk(0, KERN_INFO,
  690. "seclvl: Failure unregistering with the "
  691. "kernel\n");
  692. }
  693. }
  694. module_init(seclvl_init);
  695. module_exit(seclvl_exit);
  696. MODULE_AUTHOR("Michael A. Halcrow <mike@halcrow.us>");
  697. MODULE_DESCRIPTION("LSM implementation of the BSD Secure Levels");
  698. MODULE_LICENSE("GPL");