Skip to content

Commit 57ce66d

Browse files
committed
Merge branch 'next-integrity' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security
Pull integrity updates from James Morris: "From Mimi: This contains a couple of bug fixes, including one for a recent problem with calculating file hashes on overlayfs, and some code cleanup" * 'next-integrity' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security: MAINTAINERS: add Jarkko as maintainer for trusted keys ima: open a new file instance if no read permissions ima: fix showing large 'violations' or 'runtime_measurements_count' security/integrity: remove unnecessary 'init_keyring' variable security/integrity: constify some read-only data vfs: require i_size <= SIZE_MAX in kernel_read_file()
2 parents 4ba9628 + 34bccd6 commit 57ce66d

File tree

11 files changed

+59
-47
lines changed

11 files changed

+59
-47
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8165,6 +8165,7 @@ F: security/keys/encrypted-keys/
81658165

81668166
KEYS-TRUSTED
81678167
M: James Bottomley <jejb@linux.vnet.ibm.com>
8168+
M: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
81688169
M: Mimi Zohar <zohar@linux.vnet.ibm.com>
81698170
L: linux-integrity@vger.kernel.org
81708171
L: keyrings@vger.kernel.org

fs/exec.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -908,14 +908,14 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
908908
goto out;
909909

910910
i_size = i_size_read(file_inode(file));
911-
if (max_size > 0 && i_size > max_size) {
912-
ret = -EFBIG;
913-
goto out;
914-
}
915911
if (i_size <= 0) {
916912
ret = -EINVAL;
917913
goto out;
918914
}
915+
if (i_size > SIZE_MAX || (max_size > 0 && i_size > max_size)) {
916+
ret = -EFBIG;
917+
goto out;
918+
}
919919

920920
if (id != READING_FIRMWARE_PREALLOC_BUFFER)
921921
*buf = vmalloc(i_size);

security/integrity/digsig.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
static struct key *keyring[INTEGRITY_KEYRING_MAX];
2828

29-
static const char *keyring_name[INTEGRITY_KEYRING_MAX] = {
29+
static const char * const keyring_name[INTEGRITY_KEYRING_MAX] = {
3030
#ifndef CONFIG_INTEGRITY_TRUSTED_KEYRING
3131
"_evm",
3232
"_ima",
@@ -37,12 +37,6 @@ static const char *keyring_name[INTEGRITY_KEYRING_MAX] = {
3737
"_module",
3838
};
3939

40-
#ifdef CONFIG_INTEGRITY_TRUSTED_KEYRING
41-
static bool init_keyring __initdata = true;
42-
#else
43-
static bool init_keyring __initdata;
44-
#endif
45-
4640
#ifdef CONFIG_IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY
4741
#define restrict_link_to_ima restrict_link_by_builtin_and_secondary_trusted
4842
#else
@@ -85,7 +79,7 @@ int __init integrity_init_keyring(const unsigned int id)
8579
struct key_restriction *restriction;
8680
int err = 0;
8781

88-
if (!init_keyring)
82+
if (!IS_ENABLED(CONFIG_INTEGRITY_TRUSTED_KEYRING))
8983
return 0;
9084

9185
restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);

security/integrity/evm/evm_crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#define EVMKEY "evm-key"
2828
#define MAX_KEY_SIZE 128
2929
static unsigned char evmkey[MAX_KEY_SIZE];
30-
static int evmkey_len = MAX_KEY_SIZE;
30+
static const int evmkey_len = MAX_KEY_SIZE;
3131

3232
struct crypto_shash *hmac_tfm;
3333
static struct crypto_shash *evm_tfm[HASH_ALGO__LAST];
@@ -38,7 +38,7 @@ static DEFINE_MUTEX(mutex);
3838

3939
static unsigned long evm_set_key_flags;
4040

41-
static char * const evm_hmac = "hmac(sha1)";
41+
static const char evm_hmac[] = "hmac(sha1)";
4242

4343
/**
4444
* evm_set_key() - set EVM HMAC key from the kernel

security/integrity/ima/ima.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct ima_template_desc {
8888
char *name;
8989
char *fmt;
9090
int num_fields;
91-
struct ima_template_field **fields;
91+
const struct ima_template_field **fields;
9292
};
9393

9494
struct ima_template_entry {

security/integrity/ima/ima_api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ int ima_alloc_init_template(struct ima_event_data *event_data,
5151

5252
(*entry)->template_desc = template_desc;
5353
for (i = 0; i < template_desc->num_fields; i++) {
54-
struct ima_template_field *field = template_desc->fields[i];
54+
const struct ima_template_field *field =
55+
template_desc->fields[i];
5556
u32 len;
5657

5758
result = field->field_init(event_data,

security/integrity/ima/ima_crypto.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int ima_calc_file_hash_atfm(struct file *file,
210210
{
211211
loff_t i_size, offset;
212212
char *rbuf[2] = { NULL, };
213-
int rc, read = 0, rbuf_len, active = 0, ahash_rc = 0;
213+
int rc, rbuf_len, active = 0, ahash_rc = 0;
214214
struct ahash_request *req;
215215
struct scatterlist sg[1];
216216
struct crypto_wait wait;
@@ -257,11 +257,6 @@ static int ima_calc_file_hash_atfm(struct file *file,
257257
&rbuf_size[1], 0);
258258
}
259259

260-
if (!(file->f_mode & FMODE_READ)) {
261-
file->f_mode |= FMODE_READ;
262-
read = 1;
263-
}
264-
265260
for (offset = 0; offset < i_size; offset += rbuf_len) {
266261
if (!rbuf[1] && offset) {
267262
/* Not using two buffers, and it is not the first
@@ -300,8 +295,6 @@ static int ima_calc_file_hash_atfm(struct file *file,
300295
/* wait for the last update request to complete */
301296
rc = ahash_wait(ahash_rc, &wait);
302297
out3:
303-
if (read)
304-
file->f_mode &= ~FMODE_READ;
305298
ima_free_pages(rbuf[0], rbuf_size[0]);
306299
ima_free_pages(rbuf[1], rbuf_size[1]);
307300
out2:
@@ -336,7 +329,7 @@ static int ima_calc_file_hash_tfm(struct file *file,
336329
{
337330
loff_t i_size, offset = 0;
338331
char *rbuf;
339-
int rc, read = 0;
332+
int rc;
340333
SHASH_DESC_ON_STACK(shash, tfm);
341334

342335
shash->tfm = tfm;
@@ -357,11 +350,6 @@ static int ima_calc_file_hash_tfm(struct file *file,
357350
if (!rbuf)
358351
return -ENOMEM;
359352

360-
if (!(file->f_mode & FMODE_READ)) {
361-
file->f_mode |= FMODE_READ;
362-
read = 1;
363-
}
364-
365353
while (offset < i_size) {
366354
int rbuf_len;
367355

@@ -378,8 +366,6 @@ static int ima_calc_file_hash_tfm(struct file *file,
378366
if (rc)
379367
break;
380368
}
381-
if (read)
382-
file->f_mode &= ~FMODE_READ;
383369
kfree(rbuf);
384370
out:
385371
if (!rc)
@@ -420,6 +406,8 @@ int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
420406
{
421407
loff_t i_size;
422408
int rc;
409+
struct file *f = file;
410+
bool new_file_instance = false, modified_flags = false;
423411

424412
/*
425413
* For consistency, fail file's opened with the O_DIRECT flag on
@@ -431,15 +419,41 @@ int ima_calc_file_hash(struct file *file, struct ima_digest_data *hash)
431419
return -EINVAL;
432420
}
433421

434-
i_size = i_size_read(file_inode(file));
422+
/* Open a new file instance in O_RDONLY if we cannot read */
423+
if (!(file->f_mode & FMODE_READ)) {
424+
int flags = file->f_flags & ~(O_WRONLY | O_APPEND |
425+
O_TRUNC | O_CREAT | O_NOCTTY | O_EXCL);
426+
flags |= O_RDONLY;
427+
f = dentry_open(&file->f_path, flags, file->f_cred);
428+
if (IS_ERR(f)) {
429+
/*
430+
* Cannot open the file again, lets modify f_flags
431+
* of original and continue
432+
*/
433+
pr_info_ratelimited("Unable to reopen file for reading.\n");
434+
f = file;
435+
f->f_flags |= FMODE_READ;
436+
modified_flags = true;
437+
} else {
438+
new_file_instance = true;
439+
}
440+
}
441+
442+
i_size = i_size_read(file_inode(f));
435443

436444
if (ima_ahash_minsize && i_size >= ima_ahash_minsize) {
437-
rc = ima_calc_file_ahash(file, hash);
445+
rc = ima_calc_file_ahash(f, hash);
438446
if (!rc)
439-
return 0;
447+
goto out;
440448
}
441449

442-
return ima_calc_file_shash(file, hash);
450+
rc = ima_calc_file_shash(f, hash);
451+
out:
452+
if (new_file_instance)
453+
fput(f);
454+
else if (modified_flags)
455+
f->f_flags &= ~FMODE_READ;
456+
return rc;
443457
}
444458

445459
/*

security/integrity/ima/ima_fs.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ static int __init default_canonical_fmt_setup(char *str)
4242
__setup("ima_canonical_fmt", default_canonical_fmt_setup);
4343

4444
static int valid_policy = 1;
45-
#define TMPBUFLEN 12
45+
4646
static ssize_t ima_show_htable_value(char __user *buf, size_t count,
4747
loff_t *ppos, atomic_long_t *val)
4848
{
49-
char tmpbuf[TMPBUFLEN];
49+
char tmpbuf[32]; /* greater than largest 'long' string value */
5050
ssize_t len;
5151

52-
len = scnprintf(tmpbuf, TMPBUFLEN, "%li\n", atomic_long_read(val));
52+
len = scnprintf(tmpbuf, sizeof(tmpbuf), "%li\n", atomic_long_read(val));
5353
return simple_read_from_buffer(buf, count, ppos, tmpbuf, len);
5454
}
5555

@@ -179,7 +179,8 @@ int ima_measurements_show(struct seq_file *m, void *v)
179179
/* 6th: template specific data */
180180
for (i = 0; i < e->template_desc->num_fields; i++) {
181181
enum ima_show_type show = IMA_SHOW_BINARY;
182-
struct ima_template_field *field = e->template_desc->fields[i];
182+
const struct ima_template_field *field =
183+
e->template_desc->fields[i];
183184

184185
if (is_ima_template && strcmp(field->field_id, "d") == 0)
185186
show = IMA_SHOW_BINARY_NO_FIELD_LEN;

security/integrity/ima/ima_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "ima.h"
2626

2727
/* name for boot aggregate entry */
28-
static const char *boot_aggregate_name = "boot_aggregate";
28+
static const char boot_aggregate_name[] = "boot_aggregate";
2929
struct tpm_chip *ima_tpm_chip;
3030

3131
/* Add the boot aggregate to the IMA measurement list and extend

security/integrity/ima/ima_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ int ima_read_file(struct file *file, enum kernel_read_file_id read_id)
440440
return 0;
441441
}
442442

443-
static int read_idmap[READING_MAX_ID] = {
443+
static const int read_idmap[READING_MAX_ID] = {
444444
[READING_FIRMWARE] = FIRMWARE_CHECK,
445445
[READING_FIRMWARE_PREALLOC_BUFFER] = FIRMWARE_CHECK,
446446
[READING_MODULE] = MODULE_CHECK,

security/integrity/ima/ima_template.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static struct ima_template_desc builtin_templates[] = {
3232
static LIST_HEAD(defined_templates);
3333
static DEFINE_SPINLOCK(template_list);
3434

35-
static struct ima_template_field supported_fields[] = {
35+
static const struct ima_template_field supported_fields[] = {
3636
{.field_id = "d", .field_init = ima_eventdigest_init,
3737
.field_show = ima_show_template_digest},
3838
{.field_id = "n", .field_init = ima_eventname_init,
@@ -49,7 +49,7 @@ static struct ima_template_field supported_fields[] = {
4949
static struct ima_template_desc *ima_template;
5050
static struct ima_template_desc *lookup_template_desc(const char *name);
5151
static int template_desc_init_fields(const char *template_fmt,
52-
struct ima_template_field ***fields,
52+
const struct ima_template_field ***fields,
5353
int *num_fields);
5454

5555
static int __init ima_template_setup(char *str)
@@ -125,7 +125,8 @@ static struct ima_template_desc *lookup_template_desc(const char *name)
125125
return found ? template_desc : NULL;
126126
}
127127

128-
static struct ima_template_field *lookup_template_field(const char *field_id)
128+
static const struct ima_template_field *
129+
lookup_template_field(const char *field_id)
129130
{
130131
int i;
131132

@@ -153,11 +154,11 @@ static int template_fmt_size(const char *template_fmt)
153154
}
154155

155156
static int template_desc_init_fields(const char *template_fmt,
156-
struct ima_template_field ***fields,
157+
const struct ima_template_field ***fields,
157158
int *num_fields)
158159
{
159160
const char *template_fmt_ptr;
160-
struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
161+
const struct ima_template_field *found_fields[IMA_TEMPLATE_NUM_FIELDS_MAX];
161162
int template_num_fields;
162163
int i, len;
163164

0 commit comments

Comments
 (0)