1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include <linux/uaccess.h>
19#include <linux/module.h>
20#include "evm.h"
21
22static struct dentry *evm_init_tpm;
23
24
25
26
27
28
29
30
31
32
33
34static ssize_t evm_read_key(struct file *filp, char __user *buf,
35 size_t count, loff_t *ppos)
36{
37 char temp[80];
38 ssize_t rc;
39
40 if (*ppos != 0)
41 return 0;
42
43 sprintf(temp, "%d", evm_initialized);
44 rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
45
46 return rc;
47}
48
49
50
51
52
53
54
55
56
57
58
59
60
61static ssize_t evm_write_key(struct file *file, const char __user *buf,
62 size_t count, loff_t *ppos)
63{
64 char temp[80];
65 int i;
66
67 if (!capable(CAP_SYS_ADMIN) || (evm_initialized & EVM_INIT_HMAC))
68 return -EPERM;
69
70 if (count >= sizeof(temp) || count == 0)
71 return -EINVAL;
72
73 if (copy_from_user(temp, buf, count) != 0)
74 return -EFAULT;
75
76 temp[count] = '\0';
77
78 if ((sscanf(temp, "%d", &i) != 1) || (i != 1))
79 return -EINVAL;
80
81 evm_init_key();
82
83 return count;
84}
85
86static const struct file_operations evm_key_ops = {
87 .read = evm_read_key,
88 .write = evm_write_key,
89};
90
91int __init evm_init_secfs(void)
92{
93 int error = 0;
94
95 evm_init_tpm = securityfs_create_file("evm", S_IRUSR | S_IRGRP,
96 NULL, NULL, &evm_key_ops);
97 if (!evm_init_tpm || IS_ERR(evm_init_tpm))
98 error = -EFAULT;
99 return error;
100}
101