evm_secfs.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2010 IBM Corporation
  3. *
  4. * Authors:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * File: evm_secfs.c
  12. * - Used to signal when key is on keyring
  13. * - Get the key and enable EVM
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/audit.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include "evm.h"
  21. static struct dentry *evm_dir;
  22. static struct dentry *evm_init_tpm;
  23. static struct dentry *evm_symlink;
  24. #ifdef CONFIG_EVM_ADD_XATTRS
  25. static struct dentry *evm_xattrs;
  26. static DEFINE_MUTEX(xattr_list_mutex);
  27. static int evm_xattrs_locked;
  28. #endif
  29. /**
  30. * evm_read_key - read() for <securityfs>/evm
  31. *
  32. * @filp: file pointer, not actually used
  33. * @buf: where to put the result
  34. * @count: maximum to send along
  35. * @ppos: where to start
  36. *
  37. * Returns number of bytes read or error code, as appropriate
  38. */
  39. static ssize_t evm_read_key(struct file *filp, char __user *buf,
  40. size_t count, loff_t *ppos)
  41. {
  42. char temp[80];
  43. ssize_t rc;
  44. if (*ppos != 0)
  45. return 0;
  46. sprintf(temp, "%d", (evm_initialized & ~EVM_SETUP_COMPLETE));
  47. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  48. return rc;
  49. }
  50. /**
  51. * evm_write_key - write() for <securityfs>/evm
  52. * @file: file pointer, not actually used
  53. * @buf: where to get the data from
  54. * @count: bytes sent
  55. * @ppos: where to start
  56. *
  57. * Used to signal that key is on the kernel key ring.
  58. * - get the integrity hmac key from the kernel key ring
  59. * - create list of hmac protected extended attributes
  60. * Returns number of bytes written or error code, as appropriate
  61. */
  62. static ssize_t evm_write_key(struct file *file, const char __user *buf,
  63. size_t count, loff_t *ppos)
  64. {
  65. int i, ret;
  66. if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_SETUP_COMPLETE))
  67. return -EPERM;
  68. ret = kstrtoint_from_user(buf, count, 0, &i);
  69. if (ret)
  70. return ret;
  71. /* Reject invalid values */
  72. if (!i || (i & ~EVM_INIT_MASK) != 0)
  73. return -EINVAL;
  74. /* Don't allow a request to freshly enable metadata writes if
  75. * keys are loaded.
  76. */
  77. if ((i & EVM_ALLOW_METADATA_WRITES) &&
  78. ((evm_initialized & EVM_KEY_MASK) != 0) &&
  79. !(evm_initialized & EVM_ALLOW_METADATA_WRITES))
  80. return -EPERM;
  81. if (i & EVM_INIT_HMAC) {
  82. ret = evm_init_key();
  83. if (ret != 0)
  84. return ret;
  85. /* Forbid further writes after the symmetric key is loaded */
  86. i |= EVM_SETUP_COMPLETE;
  87. }
  88. evm_initialized |= i;
  89. /* Don't allow protected metadata modification if a symmetric key
  90. * is loaded
  91. */
  92. if (evm_initialized & EVM_INIT_HMAC)
  93. evm_initialized &= ~(EVM_ALLOW_METADATA_WRITES);
  94. return count;
  95. }
  96. static const struct file_operations evm_key_ops = {
  97. .read = evm_read_key,
  98. .write = evm_write_key,
  99. };
  100. #ifdef CONFIG_EVM_ADD_XATTRS
  101. /**
  102. * evm_read_xattrs - read() for <securityfs>/evm_xattrs
  103. *
  104. * @filp: file pointer, not actually used
  105. * @buf: where to put the result
  106. * @count: maximum to send along
  107. * @ppos: where to start
  108. *
  109. * Returns number of bytes read or error code, as appropriate
  110. */
  111. static ssize_t evm_read_xattrs(struct file *filp, char __user *buf,
  112. size_t count, loff_t *ppos)
  113. {
  114. char *temp;
  115. int offset = 0;
  116. ssize_t rc, size = 0;
  117. struct xattr_list *xattr;
  118. if (*ppos != 0)
  119. return 0;
  120. rc = mutex_lock_interruptible(&xattr_list_mutex);
  121. if (rc)
  122. return -ERESTARTSYS;
  123. list_for_each_entry(xattr, &evm_config_xattrnames, list)
  124. size += strlen(xattr->name) + 1;
  125. temp = kmalloc(size + 1, GFP_KERNEL);
  126. if (!temp) {
  127. mutex_unlock(&xattr_list_mutex);
  128. return -ENOMEM;
  129. }
  130. list_for_each_entry(xattr, &evm_config_xattrnames, list) {
  131. sprintf(temp + offset, "%s\n", xattr->name);
  132. offset += strlen(xattr->name) + 1;
  133. }
  134. mutex_unlock(&xattr_list_mutex);
  135. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  136. kfree(temp);
  137. return rc;
  138. }
  139. /**
  140. * evm_write_xattrs - write() for <securityfs>/evm_xattrs
  141. * @file: file pointer, not actually used
  142. * @buf: where to get the data from
  143. * @count: bytes sent
  144. * @ppos: where to start
  145. *
  146. * Returns number of bytes written or error code, as appropriate
  147. */
  148. static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
  149. size_t count, loff_t *ppos)
  150. {
  151. int len, err;
  152. struct xattr_list *xattr, *tmp;
  153. struct audit_buffer *ab;
  154. struct iattr newattrs;
  155. struct inode *inode;
  156. if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
  157. return -EPERM;
  158. if (*ppos != 0)
  159. return -EINVAL;
  160. if (count > XATTR_NAME_MAX)
  161. return -E2BIG;
  162. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_EVM_XATTR);
  163. if (!ab)
  164. return -ENOMEM;
  165. xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
  166. if (!xattr) {
  167. err = -ENOMEM;
  168. goto out;
  169. }
  170. xattr->name = memdup_user_nul(buf, count);
  171. if (IS_ERR(xattr->name)) {
  172. err = PTR_ERR(xattr->name);
  173. xattr->name = NULL;
  174. goto out;
  175. }
  176. /* Remove any trailing newline */
  177. len = strlen(xattr->name);
  178. if (len && xattr->name[len-1] == '\n')
  179. xattr->name[len-1] = '\0';
  180. if (strcmp(xattr->name, ".") == 0) {
  181. evm_xattrs_locked = 1;
  182. newattrs.ia_mode = S_IFREG | 0440;
  183. newattrs.ia_valid = ATTR_MODE;
  184. inode = evm_xattrs->d_inode;
  185. inode_lock(inode);
  186. err = simple_setattr(evm_xattrs, &newattrs);
  187. inode_unlock(inode);
  188. audit_log_format(ab, "locked");
  189. if (!err)
  190. err = count;
  191. goto out;
  192. }
  193. audit_log_format(ab, "xattr=");
  194. audit_log_untrustedstring(ab, xattr->name);
  195. if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
  196. XATTR_SECURITY_PREFIX_LEN) != 0) {
  197. err = -EINVAL;
  198. goto out;
  199. }
  200. /* Guard against races in evm_read_xattrs */
  201. mutex_lock(&xattr_list_mutex);
  202. list_for_each_entry(tmp, &evm_config_xattrnames, list) {
  203. if (strcmp(xattr->name, tmp->name) == 0) {
  204. err = -EEXIST;
  205. mutex_unlock(&xattr_list_mutex);
  206. goto out;
  207. }
  208. }
  209. list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
  210. mutex_unlock(&xattr_list_mutex);
  211. audit_log_format(ab, " res=0");
  212. audit_log_end(ab);
  213. return count;
  214. out:
  215. audit_log_format(ab, " res=%d", err);
  216. audit_log_end(ab);
  217. if (xattr) {
  218. kfree(xattr->name);
  219. kfree(xattr);
  220. }
  221. return err;
  222. }
  223. static const struct file_operations evm_xattr_ops = {
  224. .read = evm_read_xattrs,
  225. .write = evm_write_xattrs,
  226. };
  227. static int evm_init_xattrs(void)
  228. {
  229. evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
  230. &evm_xattr_ops);
  231. if (!evm_xattrs || IS_ERR(evm_xattrs))
  232. return -EFAULT;
  233. return 0;
  234. }
  235. #else
  236. static int evm_init_xattrs(void)
  237. {
  238. return 0;
  239. }
  240. #endif
  241. int __init evm_init_secfs(void)
  242. {
  243. int error = 0;
  244. evm_dir = securityfs_create_dir("evm", integrity_dir);
  245. if (!evm_dir || IS_ERR(evm_dir))
  246. return -EFAULT;
  247. evm_init_tpm = securityfs_create_file("evm", 0660,
  248. evm_dir, NULL, &evm_key_ops);
  249. if (!evm_init_tpm || IS_ERR(evm_init_tpm)) {
  250. error = -EFAULT;
  251. goto out;
  252. }
  253. evm_symlink = securityfs_create_symlink("evm", NULL,
  254. "integrity/evm/evm", NULL);
  255. if (!evm_symlink || IS_ERR(evm_symlink)) {
  256. error = -EFAULT;
  257. goto out;
  258. }
  259. if (evm_init_xattrs() != 0) {
  260. error = -EFAULT;
  261. goto out;
  262. }
  263. return 0;
  264. out:
  265. securityfs_remove(evm_symlink);
  266. securityfs_remove(evm_init_tpm);
  267. securityfs_remove(evm_dir);
  268. return error;
  269. }