Skip to content

Commit 6b26285

Browse files
robertosassumimizohar
authored andcommitted
ima/evm: Fix type mismatch
The endianness of a variable written to the measurement list cannot be determined at compile time, as it depends on the value of the ima_canonical_fmt global variable (set through a kernel option with the same name if the machine is big endian). If ima_canonical_fmt is false, the endianness of a variable is the same as the machine; if ima_canonical_fmt is true, the endianness is little endian. The warning arises due to this type of instruction: var = cpu_to_leXX(var) which tries to assign a value in little endian to a variable with native endianness (little or big endian). Given that the variables set with this instruction are not used in any operation but just written to a buffer, it is safe to force the type of the value being set to be the same of the type of the variable with: var = (__force <var type>)cpu_to_leXX(var) Reported-by: kernel test robot <[email protected]> Signed-off-by: Roberto Sassu <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent 24c9ae2 commit 6b26285

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

security/integrity/evm/evm_main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
360360
size = sizeof(u32);
361361
if (buffer) {
362362
if (canonical_fmt)
363-
rc = cpu_to_le32(rc);
363+
rc = (__force int)cpu_to_le32(rc);
364364

365365
*(u32 *)(buffer + total_size) = rc;
366366
}

security/integrity/ima/ima_crypto.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ static int ima_calc_field_array_hash_tfm(struct ima_field_data *field_data,
598598
u8 buffer[IMA_EVENT_NAME_LEN_MAX + 1] = { 0 };
599599
u8 *data_to_hash = field_data[i].data;
600600
u32 datalen = field_data[i].len;
601-
u32 datalen_to_hash =
602-
!ima_canonical_fmt ? datalen : cpu_to_le32(datalen);
601+
u32 datalen_to_hash = !ima_canonical_fmt ?
602+
datalen : (__force u32)cpu_to_le32(datalen);
603603

604604
if (strcmp(td->name, IMA_TEMPLATE_IMA_NAME) != 0) {
605605
rc = crypto_shash_update(shash,

security/integrity/ima/ima_fs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ int ima_measurements_show(struct seq_file *m, void *v)
147147
* PCR used defaults to the same (config option) in
148148
* little-endian format, unless set in policy
149149
*/
150-
pcr = !ima_canonical_fmt ? e->pcr : cpu_to_le32(e->pcr);
150+
pcr = !ima_canonical_fmt ? e->pcr : (__force u32)cpu_to_le32(e->pcr);
151151
ima_putc(m, &pcr, sizeof(e->pcr));
152152

153153
/* 2nd: template digest */
154154
ima_putc(m, e->digests[ima_sha1_idx].digest, TPM_DIGEST_SIZE);
155155

156156
/* 3rd: template name size */
157157
namelen = !ima_canonical_fmt ? strlen(template_name) :
158-
cpu_to_le32(strlen(template_name));
158+
(__force u32)cpu_to_le32(strlen(template_name));
159159
ima_putc(m, &namelen, sizeof(namelen));
160160

161161
/* 4th: template name */
@@ -167,7 +167,7 @@ int ima_measurements_show(struct seq_file *m, void *v)
167167

168168
if (!is_ima_template) {
169169
template_data_len = !ima_canonical_fmt ? e->template_data_len :
170-
cpu_to_le32(e->template_data_len);
170+
(__force u32)cpu_to_le32(e->template_data_len);
171171
ima_putc(m, &template_data_len, sizeof(e->template_data_len));
172172
}
173173

security/integrity/ima/ima_template_lib.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ static void ima_show_template_data_binary(struct seq_file *m,
133133
strlen(field_data->data) : field_data->len;
134134

135135
if (show != IMA_SHOW_BINARY_NO_FIELD_LEN) {
136-
u32 field_len = !ima_canonical_fmt ? len : cpu_to_le32(len);
136+
u32 field_len = !ima_canonical_fmt ?
137+
len : (__force u32)cpu_to_le32(len);
137138

138139
ima_putc(m, &field_len, sizeof(field_len));
139140
}
@@ -570,9 +571,9 @@ static int ima_eventinodedac_init_common(struct ima_event_data *event_data,
570571

571572
if (ima_canonical_fmt) {
572573
if (sizeof(id) == sizeof(u16))
573-
id = cpu_to_le16(id);
574+
id = (__force u16)cpu_to_le16(id);
574575
else
575-
id = cpu_to_le32(id);
576+
id = (__force u32)cpu_to_le32(id);
576577
}
577578

578579
return ima_write_template_field_data((void *)&id, sizeof(id),
@@ -607,15 +608,15 @@ int ima_eventinodemode_init(struct ima_event_data *event_data,
607608
struct ima_field_data *field_data)
608609
{
609610
struct inode *inode;
610-
umode_t mode;
611+
u16 mode;
611612

612613
if (!event_data->file)
613614
return 0;
614615

615616
inode = file_inode(event_data->file);
616617
mode = inode->i_mode;
617618
if (ima_canonical_fmt)
618-
mode = cpu_to_le16(mode);
619+
mode = (__force u16)cpu_to_le16(mode);
619620

620621
return ima_write_template_field_data((char *)&mode, sizeof(mode),
621622
DATA_FMT_UINT, field_data);

0 commit comments

Comments
 (0)