evm_secfs.c 7.1 KB

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