evm_secfs.c 2.4 KB

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