Skip to content

Commit 38aa3f5

Browse files
GustavoARSilvamimizohar
authored andcommitted
integrity: Avoid -Wflex-array-member-not-at-end warnings
-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting ready to enable it globally. There is currently an object (`hdr)` in `struct ima_max_digest_data` that contains a flexible structure (`struct ima_digest_data`): struct ima_max_digest_data { struct ima_digest_data hdr; u8 digest[HASH_MAX_DIGESTSIZE]; } __packed; So, in order to avoid ending up with a flexible-array member in the middle of a struct, we use the `__struct_group()` helper to separate the flexible array from the rest of the members in the flexible structure: struct ima_digest_data { __struct_group(ima_digest_data_hdr, hdr, __packed, ... the rest of the members ); u8 digest[]; } __packed; And similarly for `struct evm_ima_xattr_data`. With the change described above, we can now declare an object of the type of the tagged `struct ima_digest_data_hdr`, without embedding the flexible array in the middle of another struct: struct ima_max_digest_data { struct ima_digest_data_hdr hdr; u8 digest[HASH_MAX_DIGESTSIZE]; } __packed; And similarly for `struct evm_digest` and `struct evm_xattr`. We also use `container_of()` whenever we need to retrieve a pointer to the flexible structure. So, with these changes, fix the following warnings: security/integrity/evm/evm.h:64:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/evm/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/evm/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/ima/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/ima/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/platform_certs/../integrity.h:40:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] security/integrity/platform_certs/../integrity.h:68:32: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] Link: KSPP#202 Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Mimi Zohar <[email protected]>
1 parent cc293c8 commit 38aa3f5

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed

security/integrity/evm/evm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extern int evm_hmac_attrs;
6161
extern struct list_head evm_config_xattrnames;
6262

6363
struct evm_digest {
64-
struct ima_digest_data hdr;
64+
struct ima_digest_data_hdr hdr;
6565
char digest[IMA_MAX_DIGEST_SIZE];
6666
} __packed;
6767

security/integrity/ima/ima_api.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
246246
struct inode *inode = file_inode(file);
247247
struct inode *real_inode = d_real_inode(file_dentry(file));
248248
struct ima_max_digest_data hash;
249+
struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
250+
struct ima_digest_data, hdr);
249251
struct name_snapshot filename;
250252
struct kstat stat;
251253
int result = 0;
@@ -286,9 +288,9 @@ int ima_collect_measurement(struct ima_iint_cache *iint, struct file *file,
286288
result = -ENODATA;
287289
}
288290
} else if (buf) {
289-
result = ima_calc_buffer_hash(buf, size, &hash.hdr);
291+
result = ima_calc_buffer_hash(buf, size, hash_hdr);
290292
} else {
291-
result = ima_calc_file_hash(file, &hash.hdr);
293+
result = ima_calc_file_hash(file, hash_hdr);
292294
}
293295

294296
if (result && result != -EBADF && result != -EINVAL)

security/integrity/ima/ima_appraise.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ static int xattr_verify(enum ima_hooks func, struct ima_iint_cache *iint,
378378
}
379379

380380
rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo,
381-
iint->ima_hash->digest, &hash.hdr);
381+
iint->ima_hash->digest,
382+
container_of(&hash.hdr,
383+
struct ima_digest_data, hdr));
382384
if (rc) {
383385
*cause = "sigv3-hashing-error";
384386
*status = INTEGRITY_FAIL;

security/integrity/ima/ima_init.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ static int __init ima_add_boot_aggregate(void)
4848
struct ima_event_data event_data = { .iint = iint,
4949
.filename = boot_aggregate_name };
5050
struct ima_max_digest_data hash;
51+
struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
52+
struct ima_digest_data, hdr);
5153
int result = -ENOMEM;
5254
int violation = 0;
5355

5456
memset(iint, 0, sizeof(*iint));
5557
memset(&hash, 0, sizeof(hash));
56-
iint->ima_hash = &hash.hdr;
58+
iint->ima_hash = hash_hdr;
5759
iint->ima_hash->algo = ima_hash_algo;
5860
iint->ima_hash->length = hash_digest_size[ima_hash_algo];
5961

@@ -70,7 +72,7 @@ static int __init ima_add_boot_aggregate(void)
7072
* is not found.
7173
*/
7274
if (ima_tpm_chip) {
73-
result = ima_calc_boot_aggregate(&hash.hdr);
75+
result = ima_calc_boot_aggregate(hash_hdr);
7476
if (result < 0) {
7577
audit_cause = "hashing_error";
7678
goto err_out;

security/integrity/ima/ima_main.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ int process_buffer_measurement(struct mnt_idmap *idmap,
948948
.buf_len = size};
949949
struct ima_template_desc *template;
950950
struct ima_max_digest_data hash;
951+
struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
952+
struct ima_digest_data, hdr);
951953
char digest_hash[IMA_MAX_DIGEST_SIZE];
952954
int digest_hash_len = hash_digest_size[ima_hash_algo];
953955
int violation = 0;
@@ -986,7 +988,7 @@ int process_buffer_measurement(struct mnt_idmap *idmap,
986988
if (!pcr)
987989
pcr = CONFIG_IMA_MEASURE_PCR_IDX;
988990

989-
iint.ima_hash = &hash.hdr;
991+
iint.ima_hash = hash_hdr;
990992
iint.ima_hash->algo = ima_hash_algo;
991993
iint.ima_hash->length = hash_digest_size[ima_hash_algo];
992994

@@ -997,7 +999,7 @@ int process_buffer_measurement(struct mnt_idmap *idmap,
997999
}
9981000

9991001
if (buf_hash) {
1000-
memcpy(digest_hash, hash.hdr.digest, digest_hash_len);
1002+
memcpy(digest_hash, hash_hdr->digest, digest_hash_len);
10011003

10021004
ret = ima_calc_buffer_hash(digest_hash, digest_hash_len,
10031005
iint.ima_hash);

security/integrity/ima/ima_template_lib.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ int ima_eventdigest_init(struct ima_event_data *event_data,
339339
struct ima_field_data *field_data)
340340
{
341341
struct ima_max_digest_data hash;
342+
struct ima_digest_data *hash_hdr = container_of(&hash.hdr,
343+
struct ima_digest_data, hdr);
342344
u8 *cur_digest = NULL;
343345
u32 cur_digestsize = 0;
344346
struct inode *inode;
@@ -358,7 +360,7 @@ int ima_eventdigest_init(struct ima_event_data *event_data,
358360
if ((const char *)event_data->filename == boot_aggregate_name) {
359361
if (ima_tpm_chip) {
360362
hash.hdr.algo = HASH_ALGO_SHA1;
361-
result = ima_calc_boot_aggregate(&hash.hdr);
363+
result = ima_calc_boot_aggregate(hash_hdr);
362364

363365
/* algo can change depending on available PCR banks */
364366
if (!result && hash.hdr.algo != HASH_ALGO_SHA1)
@@ -368,7 +370,7 @@ int ima_eventdigest_init(struct ima_event_data *event_data,
368370
memset(&hash, 0, sizeof(hash));
369371
}
370372

371-
cur_digest = hash.hdr.digest;
373+
cur_digest = hash_hdr->digest;
372374
cur_digestsize = hash_digest_size[HASH_ALGO_SHA1];
373375
goto out;
374376
}
@@ -379,14 +381,14 @@ int ima_eventdigest_init(struct ima_event_data *event_data,
379381
inode = file_inode(event_data->file);
380382
hash.hdr.algo = ima_template_hash_algo_allowed(ima_hash_algo) ?
381383
ima_hash_algo : HASH_ALGO_SHA1;
382-
result = ima_calc_file_hash(event_data->file, &hash.hdr);
384+
result = ima_calc_file_hash(event_data->file, hash_hdr);
383385
if (result) {
384386
integrity_audit_msg(AUDIT_INTEGRITY_DATA, inode,
385387
event_data->filename, "collect_data",
386388
"failed", result, 0);
387389
return result;
388390
}
389-
cur_digest = hash.hdr.digest;
391+
cur_digest = hash_hdr->digest;
390392
cur_digestsize = hash.hdr.length;
391393
out:
392394
return ima_eventdigest_init_common(cur_digest, cur_digestsize,

security/integrity/integrity.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,24 @@ enum evm_ima_xattr_type {
3131
};
3232

3333
struct evm_ima_xattr_data {
34-
u8 type;
34+
/* New members must be added within the __struct_group() macro below. */
35+
__struct_group(evm_ima_xattr_data_hdr, hdr, __packed,
36+
u8 type;
37+
);
3538
u8 data[];
3639
} __packed;
3740

3841
/* Only used in the EVM HMAC code. */
3942
struct evm_xattr {
40-
struct evm_ima_xattr_data data;
43+
struct evm_ima_xattr_data_hdr data;
4144
u8 digest[SHA1_DIGEST_SIZE];
4245
} __packed;
4346

4447
#define IMA_MAX_DIGEST_SIZE HASH_MAX_DIGESTSIZE
4548

4649
struct ima_digest_data {
50+
/* New members must be added within the __struct_group() macro below. */
51+
__struct_group(ima_digest_data_hdr, hdr, __packed,
4752
u8 algo;
4853
u8 length;
4954
union {
@@ -57,6 +62,7 @@ struct ima_digest_data {
5762
} ng;
5863
u8 data[2];
5964
} xattr;
65+
);
6066
u8 digest[];
6167
} __packed;
6268

@@ -65,7 +71,7 @@ struct ima_digest_data {
6571
* with the maximum hash size, define ima_max_digest_data struct.
6672
*/
6773
struct ima_max_digest_data {
68-
struct ima_digest_data hdr;
74+
struct ima_digest_data_hdr hdr;
6975
u8 digest[HASH_MAX_DIGESTSIZE];
7076
} __packed;
7177

0 commit comments

Comments
 (0)