evm_secfs.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 IBM Corporation
  4. *
  5. * Authors:
  6. * Mimi Zohar <[email protected]>
  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. if (!xattr->enabled)
  122. continue;
  123. size += strlen(xattr->name) + 1;
  124. }
  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. if (!xattr->enabled)
  132. continue;
  133. sprintf(temp + offset, "%s\n", xattr->name);
  134. offset += strlen(xattr->name) + 1;
  135. }
  136. mutex_unlock(&xattr_list_mutex);
  137. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  138. kfree(temp);
  139. return rc;
  140. }
  141. /**
  142. * evm_write_xattrs - write() for <securityfs>/evm_xattrs
  143. * @file: file pointer, not actually used
  144. * @buf: where to get the data from
  145. * @count: bytes sent
  146. * @ppos: where to start
  147. *
  148. * Returns number of bytes written or error code, as appropriate
  149. */
  150. static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
  151. size_t count, loff_t *ppos)
  152. {
  153. int len, err;
  154. struct xattr_list *xattr, *tmp;
  155. struct audit_buffer *ab;
  156. struct iattr newattrs;
  157. struct inode *inode;
  158. if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
  159. return -EPERM;
  160. if (*ppos != 0)
  161. return -EINVAL;
  162. if (count > XATTR_NAME_MAX)
  163. return -E2BIG;
  164. ab = audit_log_start(audit_context(), GFP_KERNEL,
  165. AUDIT_INTEGRITY_EVM_XATTR);
  166. if (!ab && IS_ENABLED(CONFIG_AUDIT))
  167. return -ENOMEM;
  168. xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
  169. if (!xattr) {
  170. err = -ENOMEM;
  171. goto out;
  172. }
  173. xattr->enabled = true;
  174. xattr->name = memdup_user_nul(buf, count);
  175. if (IS_ERR(xattr->name)) {
  176. err = PTR_ERR(xattr->name);
  177. xattr->name = NULL;
  178. goto out;
  179. }
  180. /* Remove any trailing newline */
  181. len = strlen(xattr->name);
  182. if (len && xattr->name[len-1] == '\n')
  183. xattr->name[len-1] = '\0';
  184. audit_log_format(ab, "xattr=");
  185. audit_log_untrustedstring(ab, xattr->name);
  186. if (strcmp(xattr->name, ".") == 0) {
  187. evm_xattrs_locked = 1;
  188. newattrs.ia_mode = S_IFREG | 0440;
  189. newattrs.ia_valid = ATTR_MODE;
  190. inode = evm_xattrs->d_inode;
  191. inode_lock(inode);
  192. err = simple_setattr(&init_user_ns, evm_xattrs, &newattrs);
  193. inode_unlock(inode);
  194. if (!err)
  195. err = count;
  196. goto out;
  197. }
  198. if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
  199. XATTR_SECURITY_PREFIX_LEN) != 0) {
  200. err = -EINVAL;
  201. goto out;
  202. }
  203. /*
  204. * xattr_list_mutex guards against races in evm_read_xattrs().
  205. * Entries are only added to the evm_config_xattrnames list
  206. * and never deleted. Therefore, the list is traversed
  207. * using list_for_each_entry_lockless() without holding
  208. * the mutex in evm_calc_hmac_or_hash(), evm_find_protected_xattrs()
  209. * and evm_protected_xattr().
  210. */
  211. mutex_lock(&xattr_list_mutex);
  212. list_for_each_entry(tmp, &evm_config_xattrnames, list) {
  213. if (strcmp(xattr->name, tmp->name) == 0) {
  214. err = -EEXIST;
  215. if (!tmp->enabled) {
  216. tmp->enabled = true;
  217. err = count;
  218. }
  219. mutex_unlock(&xattr_list_mutex);
  220. goto out;
  221. }
  222. }
  223. list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
  224. mutex_unlock(&xattr_list_mutex);
  225. audit_log_format(ab, " res=0");
  226. audit_log_end(ab);
  227. return count;
  228. out:
  229. audit_log_format(ab, " res=%d", (err < 0) ? err : 0);
  230. audit_log_end(ab);
  231. if (xattr) {
  232. kfree(xattr->name);
  233. kfree(xattr);
  234. }
  235. return err;
  236. }
  237. static const struct file_operations evm_xattr_ops = {
  238. .read = evm_read_xattrs,
  239. .write = evm_write_xattrs,
  240. };
  241. static int evm_init_xattrs(void)
  242. {
  243. evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
  244. &evm_xattr_ops);
  245. if (!evm_xattrs || IS_ERR(evm_xattrs))
  246. return -EFAULT;
  247. return 0;
  248. }
  249. #else
  250. static int evm_init_xattrs(void)
  251. {
  252. return 0;
  253. }
  254. #endif
  255. int __init evm_init_secfs(void)
  256. {
  257. int error = 0;
  258. evm_dir = securityfs_create_dir("evm", integrity_dir);
  259. if (!evm_dir || IS_ERR(evm_dir))
  260. return -EFAULT;
  261. evm_init_tpm = securityfs_create_file("evm", 0660,
  262. evm_dir, NULL, &evm_key_ops);
  263. if (!evm_init_tpm || IS_ERR(evm_init_tpm)) {
  264. error = -EFAULT;
  265. goto out;
  266. }
  267. evm_symlink = securityfs_create_symlink("evm", NULL,
  268. "integrity/evm/evm", NULL);
  269. if (!evm_symlink || IS_ERR(evm_symlink)) {
  270. error = -EFAULT;
  271. goto out;
  272. }
  273. if (evm_init_xattrs() != 0) {
  274. error = -EFAULT;
  275. goto out;
  276. }
  277. return 0;
  278. out:
  279. securityfs_remove(evm_symlink);
  280. securityfs_remove(evm_init_tpm);
  281. securityfs_remove(evm_dir);
  282. return error;
  283. }