evm_secfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <linux/uaccess.h>
  16. #include <linux/module.h>
  17. #include "evm.h"
  18. static struct dentry *evm_init_tpm;
  19. /**
  20. * evm_read_key - read() for <securityfs>/evm
  21. *
  22. * @filp: file pointer, not actually used
  23. * @buf: where to put the result
  24. * @count: maximum to send along
  25. * @ppos: where to start
  26. *
  27. * Returns number of bytes read or error code, as appropriate
  28. */
  29. static ssize_t evm_read_key(struct file *filp, char __user *buf,
  30. size_t count, loff_t *ppos)
  31. {
  32. char temp[80];
  33. ssize_t rc;
  34. if (*ppos != 0)
  35. return 0;
  36. sprintf(temp, "%d", evm_initialized);
  37. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  38. return rc;
  39. }
  40. /**
  41. * evm_write_key - write() for <securityfs>/evm
  42. * @file: file pointer, not actually used
  43. * @buf: where to get the data from
  44. * @count: bytes sent
  45. * @ppos: where to start
  46. *
  47. * Used to signal that key is on the kernel key ring.
  48. * - get the integrity hmac key from the kernel key ring
  49. * - create list of hmac protected extended attributes
  50. * Returns number of bytes written or error code, as appropriate
  51. */
  52. static ssize_t evm_write_key(struct file *file, const char __user *buf,
  53. size_t count, loff_t *ppos)
  54. {
  55. char temp[80];
  56. int i, error;
  57. if (!capable(CAP_SYS_ADMIN) || evm_initialized)
  58. return -EPERM;
  59. if (count >= sizeof(temp) || count == 0)
  60. return -EINVAL;
  61. if (copy_from_user(temp, buf, count) != 0)
  62. return -EFAULT;
  63. temp[count] = '\0';
  64. if ((sscanf(temp, "%d", &i) != 1) || (i != 1))
  65. return -EINVAL;
  66. error = evm_init_key();
  67. if (!error) {
  68. evm_initialized = 1;
  69. pr_info("EVM: initialized\n");
  70. } else
  71. pr_err("EVM: initialization failed\n");
  72. return count;
  73. }
  74. static const struct file_operations evm_key_ops = {
  75. .read = evm_read_key,
  76. .write = evm_write_key,
  77. };
  78. int __init evm_init_secfs(void)
  79. {
  80. int error = 0;
  81. evm_init_tpm = securityfs_create_file("evm", S_IRUSR | S_IRGRP,
  82. NULL, NULL, &evm_key_ops);
  83. if (!evm_init_tpm || IS_ERR(evm_init_tpm))
  84. error = -EFAULT;
  85. return error;
  86. }
  87. void __exit evm_cleanup_secfs(void)
  88. {
  89. if (evm_init_tpm)
  90. securityfs_remove(evm_init_tpm);
  91. }