Skip to content

Commit fa516b6

Browse files
mjg59Mimi Zohar
authored andcommitted
EVM: Allow runtime modification of the set of verified xattrs
Sites may wish to provide additional metadata alongside files in order to make more fine-grained security decisions[1]. The security of this is enhanced if this metadata is protected, something that EVM makes possible. However, the kernel cannot know about the set of extended attributes that local admins may wish to protect, and hardcoding this policy in the kernel makes it difficult to change over time and less convenient for distributions to enable. This patch adds a new /sys/kernel/security/integrity/evm/evm_xattrs node, which can be read to obtain the current set of EVM-protected extended attributes or written to in order to add new entries. Extending this list will not change the validity of any existing signatures provided that the file in question does not have any of the additional extended attributes - missing xattrs are skipped when calculating the EVM hash. [1] For instance, a package manager could install information about the package uploader in an additional extended attribute. Local LSM policy could then be associated with that extended attribute in order to restrict the privileges available to packages from less trusted uploaders. Signed-off-by: Matthew Garrett <[email protected]> Reviewed-by: James Morris <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 21af766 commit fa516b6

File tree

6 files changed

+202
-4
lines changed

6 files changed

+202
-4
lines changed

Documentation/ABI/testing/evm

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,16 @@ Description:
5757
dracut (via 97masterkey and 98integrity) and systemd (via
5858
core/ima-setup) have support for loading keys at boot
5959
time.
60+
61+
What: security/integrity/evm/evm_xattrs
62+
Date: April 2018
63+
Contact: Matthew Garrett <[email protected]>
64+
Description:
65+
Shows the set of extended attributes used to calculate or
66+
validate the EVM signature, and allows additional attributes
67+
to be added at runtime. Any signatures generated after
68+
additional attributes are added (and on files posessing those
69+
additional attributes) will only be valid if the same
70+
additional attributes are configured on system boot. Writing
71+
a single period (.) will lock the xattr list from any further
72+
modification.

include/uapi/linux/audit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
#define AUDIT_INTEGRITY_HASH 1803 /* Integrity HASH type */
148148
#define AUDIT_INTEGRITY_PCR 1804 /* PCR invalidation msgs */
149149
#define AUDIT_INTEGRITY_RULE 1805 /* policy rule */
150+
#define AUDIT_INTEGRITY_EVM_XATTR 1806 /* New EVM-covered xattr */
150151

151152
#define AUDIT_KERNEL 2000 /* Asynchronous audit record. NOT A REQUEST. */
152153

security/integrity/evm/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ config EVM_EXTRA_SMACK_XATTRS
4242
additional info to the calculation, requires existing EVM
4343
labeled file systems to be relabeled.
4444

45+
config EVM_ADD_XATTRS
46+
bool "Add additional EVM extended attributes at runtime"
47+
depends on EVM
48+
default n
49+
help
50+
Allow userland to provide additional xattrs for HMAC calculation.
51+
52+
When this option is enabled, root can add additional xattrs to the
53+
list used by EVM by writing them into
54+
/sys/kernel/security/integrity/evm/evm_xattrs.
55+
4556
config EVM_LOAD_X509
4657
bool "Load an X509 certificate onto the '.evm' trusted keyring"
4758
depends on EVM && INTEGRITY_TRUSTED_KEYRING

security/integrity/evm/evm_crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static int evm_calc_hmac_or_hash(struct dentry *dentry,
208208
return PTR_ERR(desc);
209209

210210
error = -ENODATA;
211-
list_for_each_entry(xattr, &evm_config_xattrnames, list) {
211+
list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
212212
bool is_ima = false;
213213

214214
if (strcmp(xattr->name, XATTR_NAME_IMA) == 0)

security/integrity/evm/evm_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static const char * const integrity_status_msg[] = {
3535
};
3636
int evm_hmac_attrs;
3737

38-
static struct xattr_list evm_config_default_xattrnames[] __ro_after_init = {
38+
static struct xattr_list evm_config_default_xattrnames[] = {
3939
#ifdef CONFIG_SECURITY_SELINUX
4040
{.name = XATTR_NAME_SELINUX},
4141
#endif
@@ -101,7 +101,7 @@ static int evm_find_protected_xattrs(struct dentry *dentry)
101101
if (!(inode->i_opflags & IOP_XATTR))
102102
return -EOPNOTSUPP;
103103

104-
list_for_each_entry(xattr, &evm_config_xattrnames, list) {
104+
list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
105105
error = __vfs_getxattr(dentry, inode, xattr->name, NULL, 0);
106106
if (error < 0) {
107107
if (error == -ENODATA)
@@ -228,7 +228,7 @@ static int evm_protected_xattr(const char *req_xattr_name)
228228
struct xattr_list *xattr;
229229

230230
namelen = strlen(req_xattr_name);
231-
list_for_each_entry(xattr, &evm_config_xattrnames, list) {
231+
list_for_each_entry_rcu(xattr, &evm_config_xattrnames, list) {
232232
if ((strlen(xattr->name) == namelen)
233233
&& (strncmp(req_xattr_name, xattr->name, namelen) == 0)) {
234234
found = 1;

security/integrity/evm/evm_secfs.c

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,22 @@
1515

1616
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1717

18+
#include <linux/audit.h>
1819
#include <linux/uaccess.h>
1920
#include <linux/module.h>
21+
#include <linux/mutex.h>
2022
#include "evm.h"
2123

2224
static struct dentry *evm_dir;
2325
static struct dentry *evm_init_tpm;
2426
static struct dentry *evm_symlink;
2527

28+
#ifdef CONFIG_EVM_ADD_XATTRS
29+
static struct dentry *evm_xattrs;
30+
static DEFINE_MUTEX(xattr_list_mutex);
31+
static int evm_xattrs_locked;
32+
#endif
33+
2634
/**
2735
* evm_read_key - read() for <securityfs>/evm
2836
*
@@ -109,6 +117,166 @@ static const struct file_operations evm_key_ops = {
109117
.write = evm_write_key,
110118
};
111119

120+
#ifdef CONFIG_EVM_ADD_XATTRS
121+
/**
122+
* evm_read_xattrs - read() for <securityfs>/evm_xattrs
123+
*
124+
* @filp: file pointer, not actually used
125+
* @buf: where to put the result
126+
* @count: maximum to send along
127+
* @ppos: where to start
128+
*
129+
* Returns number of bytes read or error code, as appropriate
130+
*/
131+
static ssize_t evm_read_xattrs(struct file *filp, char __user *buf,
132+
size_t count, loff_t *ppos)
133+
{
134+
char *temp;
135+
int offset = 0;
136+
ssize_t rc, size = 0;
137+
struct xattr_list *xattr;
138+
139+
if (*ppos != 0)
140+
return 0;
141+
142+
rc = mutex_lock_interruptible(&xattr_list_mutex);
143+
if (rc)
144+
return -ERESTARTSYS;
145+
146+
list_for_each_entry(xattr, &evm_config_xattrnames, list)
147+
size += strlen(xattr->name) + 1;
148+
149+
temp = kmalloc(size + 1, GFP_KERNEL);
150+
if (!temp)
151+
return -ENOMEM;
152+
153+
list_for_each_entry(xattr, &evm_config_xattrnames, list) {
154+
sprintf(temp + offset, "%s\n", xattr->name);
155+
offset += strlen(xattr->name) + 1;
156+
}
157+
158+
mutex_unlock(&xattr_list_mutex);
159+
rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
160+
161+
return rc;
162+
}
163+
164+
/**
165+
* evm_write_xattrs - write() for <securityfs>/evm_xattrs
166+
* @file: file pointer, not actually used
167+
* @buf: where to get the data from
168+
* @count: bytes sent
169+
* @ppos: where to start
170+
*
171+
* Returns number of bytes written or error code, as appropriate
172+
*/
173+
static ssize_t evm_write_xattrs(struct file *file, const char __user *buf,
174+
size_t count, loff_t *ppos)
175+
{
176+
int len, err;
177+
struct xattr_list *xattr, *tmp;
178+
struct audit_buffer *ab;
179+
struct iattr newattrs;
180+
struct inode *inode;
181+
182+
if (!capable(CAP_SYS_ADMIN) || evm_xattrs_locked)
183+
return -EPERM;
184+
185+
if (*ppos != 0)
186+
return -EINVAL;
187+
188+
if (count > XATTR_NAME_MAX)
189+
return -E2BIG;
190+
191+
ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_INTEGRITY_EVM_XATTR);
192+
if (IS_ERR(ab))
193+
return PTR_ERR(ab);
194+
195+
xattr = kmalloc(sizeof(struct xattr_list), GFP_KERNEL);
196+
if (!xattr) {
197+
err = -ENOMEM;
198+
goto out;
199+
}
200+
201+
xattr->name = memdup_user_nul(buf, count);
202+
if (IS_ERR(xattr->name)) {
203+
err = PTR_ERR(xattr->name);
204+
xattr->name = NULL;
205+
goto out;
206+
}
207+
208+
/* Remove any trailing newline */
209+
len = strlen(xattr->name);
210+
if (xattr->name[len-1] == '\n')
211+
xattr->name[len-1] = '\0';
212+
213+
if (strcmp(xattr->name, ".") == 0) {
214+
evm_xattrs_locked = 1;
215+
newattrs.ia_mode = S_IFREG | 0440;
216+
newattrs.ia_valid = ATTR_MODE;
217+
inode = evm_xattrs->d_inode;
218+
inode_lock(inode);
219+
err = simple_setattr(evm_xattrs, &newattrs);
220+
inode_unlock(inode);
221+
audit_log_format(ab, "locked");
222+
if (!err)
223+
err = count;
224+
goto out;
225+
}
226+
227+
audit_log_format(ab, "xattr=");
228+
audit_log_untrustedstring(ab, xattr->name);
229+
230+
if (strncmp(xattr->name, XATTR_SECURITY_PREFIX,
231+
XATTR_SECURITY_PREFIX_LEN) != 0) {
232+
err = -EINVAL;
233+
goto out;
234+
}
235+
236+
/* Guard against races in evm_read_xattrs */
237+
mutex_lock(&xattr_list_mutex);
238+
list_for_each_entry(tmp, &evm_config_xattrnames, list) {
239+
if (strcmp(xattr->name, tmp->name) == 0) {
240+
err = -EEXIST;
241+
mutex_unlock(&xattr_list_mutex);
242+
goto out;
243+
}
244+
}
245+
list_add_tail_rcu(&xattr->list, &evm_config_xattrnames);
246+
mutex_unlock(&xattr_list_mutex);
247+
248+
audit_log_format(ab, " res=0");
249+
audit_log_end(ab);
250+
return count;
251+
out:
252+
audit_log_format(ab, " res=%d", err);
253+
audit_log_end(ab);
254+
kfree(xattr->name);
255+
kfree(xattr);
256+
return err;
257+
}
258+
259+
static const struct file_operations evm_xattr_ops = {
260+
.read = evm_read_xattrs,
261+
.write = evm_write_xattrs,
262+
};
263+
264+
static int evm_init_xattrs(void)
265+
{
266+
evm_xattrs = securityfs_create_file("evm_xattrs", 0660, evm_dir, NULL,
267+
&evm_xattr_ops);
268+
if (!evm_xattrs || IS_ERR(evm_xattrs))
269+
return -EFAULT;
270+
271+
return 0;
272+
}
273+
#else
274+
static int evm_init_xattrs(void)
275+
{
276+
return 0;
277+
}
278+
#endif
279+
112280
int __init evm_init_secfs(void)
113281
{
114282
int error = 0;
@@ -131,6 +299,11 @@ int __init evm_init_secfs(void)
131299
goto out;
132300
}
133301

302+
if (evm_init_xattrs() != 0) {
303+
error = -EFAULT;
304+
goto out;
305+
}
306+
134307
return 0;
135308
out:
136309
securityfs_remove(evm_symlink);

0 commit comments

Comments
 (0)