landlock: Work around randstruct unnamed static initializer support
Subsystems (2)
PATCH 1/1 landlock: Work around randstruct unnamed static initializer support
Unnamed static initializers aren't supported by the randstruct GCC plugin. Quoting the plugin, "set up a bogus anonymous struct field designed to error out on unnamed struct initializers as gcc provides no other way to detect such code". That is exactly what happens with the landlock code, so adjust the static initializers for structs lsm_ioctlop_audit and landlock_request that contain a randomized structure (struct path) to use named variables, which avoids the intentional GCC crashes:
security/landlock/fs.c: In function 'hook_file_ioctl_common':security/landlock/fs.c:1745:61: internal compiler error: in count_type_elements, at expr.cc:7092
1745 | .u.op = &(struct lsm_ioctlop_audit) {
| ^
security/landlock/fs.c: In function 'log_fs_change_topology_path':security/landlock/fs.c:1379:65: internal compiler error: in count_type_elements, at expr.cc:7092
1379 | landlock_log_denial(subject, &(struct landlock_request) {
| ^
We went 8 years before tripping over this! With this patch landed, we can enable COMPILE_TEST builds with the randstruct GCC plugin again.
---
Cc: "Mickaël Salaün" <[email protected]>
Cc: "Günther Noack" <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Paul Moore <[email protected]>
Cc: James Morris <[email protected]>
Cc: "Serge E. Hallyn" <[email protected]>
Cc: <[email protected]>
--- security/landlock/fs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
security/landlock/fs.c +10 -10
@@ -1376,14 +1376,14 @@ static void log_fs_change_topology_path(const struct landlock_cred_security *const subject, size_t handle_layer, const struct path *const path) { - landlock_log_denial(subject, &(struct landlock_request) { + struct landlock_request request = { .type =LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY , - .audit = { - .type =LSM_AUDIT_DATA_PATH , - .u.path = *path, - }, + .audit.type =LSM_AUDIT_DATA_PATH , .layer_plus_one = handle_layer +1 , - }); + }; + request.audit.u.path = *path; + +landlock_log_denial (subject, &request); } static void log_fs_change_topology_dentry(
@@ -1720,6 +1720,7 @@ static int hook_file_truncate(struct file *const file)static int hook_file_ioctl_common (const struct file *const file,const unsigned int cmd,const bool is_compat) { +struct lsm_ioctlop_audit audit_log;access_mask_t allowed_access =landlock_file (file)->allowed_access;/*
@@ -1738,14 +1739,13 @@ static int hook_file_ioctl_common(const struct file *const file, is_masked_device_ioctl(cmd))return 0 ; + audit_log.path = file->f_path; + audit_log.cmd = cmd; landlock_log_denial(landlock_cred(file->f_cred), &(struct landlock_request) { .type = LANDLOCK_REQUEST_FS_ACCESS, .audit = { .type = LSM_AUDIT_DATA_IOCTL_OP, - .u.op = &(struct lsm_ioctlop_audit) { - .path = file->f_path, - .cmd = cmd, - }, + .u.op = &audit_log, }, .all_existing_optional_access = _LANDLOCK_ACCESS_FS_OPTIONAL, .access = LANDLOCK_ACCESS_FS_IOCTL_DEV,
Signature · 1 line
2.34.1
Mickaël Salaün
maintainerQuoted 8 lines · Kees Cook
Unnamed static initializers aren't supported by the randstruct GCC plugin. Quoting the plugin, "set up a bogus anonymous struct field designed to error out on unnamed struct initializers as gcc provides no other way to detect such code". That is exactly what happens with the landlock code, so adjust the static initializers for structs
lsm_ioctlop_auditandlandlock_requestthat contain a randomized structure (struct path) to use named variables, which avoids the intentional GCC crashes:
This is not a sustainable solution. Could we fix the plugin instead? This new Landlock change may be the first to trigger this plugin bug but it will probably not be the last to use unnamed static initializers. Forbidding specific C constructs should be documented.
As a temporary solution (not sure if it's worth it, nor a good idea), could handling of types used in unnamed static initializers be ignored by this plugin? I guess [1] is already a temporary solution though.
[1] https://lore.kernel.org/all/[email protected]/
Quoted 89 lines
security/landlock/fs.c: In function 'hook_file_ioctl_common':security/landlock/fs.c:1745:61: internal compiler error: incount_type_elements, at expr.cc:7092
1745 | .u.op = &(structlsm_ioctlop_audit) {
| ^
security/landlock/fs.c: In function 'log_fs_change_topology_path':security/landlock/fs.c:1379:65: internal compiler error: incount_type_elements, at expr.cc:7092
1379 | landlock_log_denial(subject, &(structlandlock_request) {
| ^We went 8 years before tripping over this! With this patch landed, we can enable
COMPILE_TESTbuilds with the randstruct GCC plugin again.Reported-by "Dr. David Alan Gilbert" <[email protected]>Reported-by Mark Brown <[email protected]>Reported-by WangYuli <[email protected]>Closes https://lore.kernel.org/lkml/337D5D4887277B27+3c677db3-a8b9-47f0-93a4-7809355f1381@uniontech.com/ open hereSigned-off-by Kees Cook <[email protected]>---
Cc: "Mickaël Salaün" <[email protected]>
Cc: "Günther Noack" <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Paul Moore <[email protected]>
Cc: James Morris <[email protected]>
Cc: "Serge E. Hallyn" <[email protected]>
Cc: <[email protected]>--- security/landlock/fs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)security/landlock/fs.c +10
-10@@ -1376,14 +1376,14 @@ static void log_fs_change_topology_path(const struct landlock_cred_security *const subject, size_t handle_layer, const struct path *const path) { - landlock_log_denial(subject, &(struct landlock_request) { + struct landlock_request request = { .type =LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY , - .audit = { - .type =LSM_AUDIT_DATA_PATH , - .u.path = *path, - }, + .audit.type =LSM_AUDIT_DATA_PATH , .layer_plus_one = handle_layer +1 , - }); + }; + request.audit.u.path = *path; + +landlock_log_denial (subject, &request); } static void log_fs_change_topology_dentry(@@ -1720,6 +1720,7 @@ static int hook_file_truncate(struct file *const file)static int hook_file_ioctl_common (const struct file *const file,const unsigned int cmd,const bool is_compat) { +struct lsm_ioctlop_audit audit_log;access_mask_t allowed_access =landlock_file (file)->allowed_access;/* @@ -1738,14 +1739,13 @@ static int hook_file_ioctl_common(const struct file *const file, is_masked_device_ioctl(cmd))return 0 ; + audit_log.path = file->f_path; + audit_log.cmd = cmd; landlock_log_denial(landlock_cred(file->f_cred), &(struct landlock_request) { .type = LANDLOCK_REQUEST_FS_ACCESS, .audit = { .type = LSM_AUDIT_DATA_IOCTL_OP, - .u.op = &(struct lsm_ioctlop_audit) { - .path = file->f_path, - .cmd = cmd, - }, + .u.op = &audit_log, }, .all_existing_optional_access = _LANDLOCK_ACCESS_FS_OPTIONAL, .access = LANDLOCK_ACCESS_FS_IOCTL_DEV,Signature · 1 line
2.34.1
Arnd Bergmann
Acked-byQuoted 10 lines · Mickaël Salaün
Quoted 8 lines · Kees Cook
Unnamed static initializers aren't supported by the randstruct GCC plugin. Quoting the plugin, "set up a bogus anonymous struct field designed to error out on unnamed struct initializers as gcc provides no other way to detect such code". That is exactly what happens with the landlock code, so adjust the static initializers for structs
lsm_ioctlop_auditandlandlock_requestthat contain a randomized structure (struct path) to use named variables, which avoids the intentional GCC crashes:
Quoted 4 lines
This is not a sustainable solution. Could we fix the plugin instead? This new Landlock change may be the first to trigger this plugin bug but it will probably not be the last to use unnamed static initializers. Forbidding specific C constructs should be documented.
I think the version from Kees' patch looks more readable than the version with the compound literal, so it certainly seems appropriate as an immediate regression fix, even if it's possible to fix the plugin later.
Quoted 1 line
Quoted 1 line
We went 8 years before tripping over this!
Right, it's probably enough to revisit the plugin code after it happens again.
Quoted 2 lines
Quoted 2 lines
Quoted 21 lines · Arnd Bergmann
Quoted 10 lines · Mickaël Salaün
Quoted 8 lines · Kees Cook
Unnamed static initializers aren't supported by the randstruct GCC plugin. Quoting the plugin, "set up a bogus anonymous struct field designed to error out on unnamed struct initializers as gcc provides no other way to detect such code". That is exactly what happens with the landlock code, so adjust the static initializers for structs
lsm_ioctlop_auditandlandlock_requestthat contain a randomized structure (struct path) to use named variables, which avoids the intentional GCC crashes:Quoted 4 lines
This is not a sustainable solution. Could we fix the plugin instead? This new Landlock change may be the first to trigger this plugin bug but it will probably not be the last to use unnamed static initializers. Forbidding specific C constructs should be documented.
I think the version from Kees' patch looks more readable than the version with the compound literal, so it certainly seems appropriate as an immediate regression fix, even if it's possible to fix the plugin later.
Quoted 4 lines
Quoted 1 line
Quoted 1 line
We went 8 years before tripping over this!
Right, it's probably enough to revisit the plugin code after it happens again.
Yeah, that's my thinking as well.
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Acked-by Arnd Bergmann <[email protected]>
Thanks! Mickaël, are you good with this for now, and if so, do you want to carry it or shall I?
-Kees
Signature · 1 line
Kees Cook
Mickaël Salaün
maintainerQuoted 29 lines · Kees Cook
Quoted 21 lines · Arnd Bergmann
Quoted 10 lines · Mickaël Salaün
Quoted 8 lines · Kees Cook
Unnamed static initializers aren't supported by the randstruct GCC plugin. Quoting the plugin, "set up a bogus anonymous struct field designed to error out on unnamed struct initializers as gcc provides no other way to detect such code". That is exactly what happens with the landlock code, so adjust the static initializers for structs
lsm_ioctlop_auditandlandlock_requestthat contain a randomized structure (struct path) to use named variables, which avoids the intentional GCC crashes:Quoted 4 lines
This is not a sustainable solution. Could we fix the plugin instead? This new Landlock change may be the first to trigger this plugin bug but it will probably not be the last to use unnamed static initializers. Forbidding specific C constructs should be documented.
I think the version from Kees' patch looks more readable than the version with the compound literal, so it certainly seems appropriate as an immediate regression fix, even if it's possible to fix the plugin later.
Quoted 4 lines
Quoted 1 line
Quoted 1 line
We went 8 years before tripping over this!
Right, it's probably enough to revisit the plugin code after it happens again.
Yeah, that's my thinking as well.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Acked-by Arnd Bergmann <[email protected]>Thanks! Mickaël, are you good with this for now, and if so, do you want to carry it or shall I?
I don't like this kind of change but I'm OK with it if it comes with documentation explaining what construct should not be used in the kernel (and even better, updating checkpatch.pl accordingly), or if it's a temporary workaround and someone is working on fixing the plugin. Are you or someone else working on it? In the meantime, feel free to carry this patch.
Hello Kees!
Quoted 14 lines · Kees Cook
Unnamed static initializers aren't supported by the randstruct GCC plugin. Quoting the plugin, "set up a bogus anonymous struct field designed to error out on unnamed struct initializers as gcc provides no other way to detect such code". That is exactly what happens with the landlock code, so adjust the static initializers for structs
lsm_ioctlop_auditandlandlock_requestthat contain a randomized structure (struct path) to use named variables, which avoids the intentional GCC crashes:
security/landlock/fs.c: In function 'hook_file_ioctl_common':security/landlock/fs.c:1745:61: internal compiler error: incount_type_elements, at expr.cc:7092
1745 | .u.op = &(structlsm_ioctlop_audit) {
| ^
Quoted 7 lines
security/landlock/fs.c: In function 'log_fs_change_topology_path':security/landlock/fs.c:1379:65: internal compiler error: incount_type_elements, at expr.cc:7092
1379 | landlock_log_denial(subject, &(structlandlock_request) {
| ^We went 8 years before tripping over this! With this patch landed, we can enable
COMPILE_TESTbuilds with the randstruct GCC plugin again.
I am still struggling to understand the boundaries of what the randstruct plugin supports and what it fails on. Could you please clarify? (Specific questions below.)
Quoted 33 lines
Reported-by "Dr. David Alan Gilbert" <[email protected]>Reported-by Mark Brown <[email protected]>Reported-by WangYuli <[email protected]>Closes https://lore.kernel.org/lkml/337D5D4887277B27+3c677db3-a8b9-47f0-93a4-7809355f1381@uniontech.com/ open hereSigned-off-by Kees Cook <[email protected]>---
Cc: "Mickaël Salaün" <[email protected]>
Cc: "Günther Noack" <[email protected]>
Cc: Mark Brown <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Paul Moore <[email protected]>
Cc: James Morris <[email protected]>
Cc: "Serge E. Hallyn" <[email protected]>
Cc: <[email protected]>--- security/landlock/fs.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)security/landlock/fs.c +1
-3@@ -1376,14 +1376,14 @@ static void log_fs_change_topology_path(const struct landlock_cred_security *const subject, size_t handle_layer, const struct path *const path) { - landlock_log_denial(subject, &(struct landlock_request) { + struct landlock_request request = { .type = LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY, - .audit = { - .type = LSM_AUDIT_DATA_PATH,
security/landlock/fs.c static void · line 1387
Quoted 10 lines
security/landlock/fs.c +5
-3- .u.path = *path, - }, + .audit.type = LSM_AUDIT_DATA_PATH, .layer_plus_one = handle_layer + 1, - }); + }; + request.audit.u.path = *path; + +landlock_log_denial (subject, &request); }
If I understood the commit message correctly, we are giving a name ("request") to the struct landlock_request here, because the randstruct plugin needs that if the constructed struct (recursively?) contains a randstruct-enabled member, like .audit.u.path in this case?
I am surprised that you pulled the assignment to "request.audit.u.path" out of the struct initialization expression though. Was that also necessary for randstruct to work?
Would it have worked to initialize it inline instead, like this?
struct landlock_request request = {
.type = LANDLOCK_REQUEST_FS_CHANGE_TOPOLOGY,
.audit.type = LSM_AUDIT_DATA_PATH,
.audit.u.path = *path,
.layer_plus_one = handle_layer + 1,
};
Quoted 11 lines
security/landlock/fs.c +1
-0static void log_fs_change_topology_dentry(@@ -1720,6 +1720,7 @@ static int hook_file_truncate(struct file *const file)static int hook_file_ioctl_common (const struct file *const file,const unsigned int cmd,const bool is_compat) { +struct lsm_ioctlop_audit audit_log;access_mask_t allowed_access =landlock_file (file)->allowed_access;/*
security/landlock/fs.c static int hook_file_ioctl_common(const struct file *const file, · line 1743
Quoted 5 lines
security/landlock/fs.c +2
-0@@ -1738,14 +1739,13 @@ static int hook_file_ioctl_common(const struct file *const file, is_masked_device_ioctl(cmd))return 0 ; + audit_log.path = file->f_path; + audit_log.cmd = cmd;
Same question here, I guess. This could not have been written like this?
struct lsm_ioctlop_audit audit_log = {
.path = file->f_path,
.cmd = cmd,
};Quoted 15 lines
security/landlock/fs.c +1
-4landlock_log_denial(landlock_cred(file->f_cred), &(struct landlock_request) { .type = LANDLOCK_REQUEST_FS_ACCESS, .audit = { .type = LSM_AUDIT_DATA_IOCTL_OP, - .u.op = &(struct lsm_ioctlop_audit) { - .path = file->f_path, - .cmd = cmd, - }, + .u.op = &audit_log, }, .all_existing_optional_access = _LANDLOCK_ACCESS_FS_OPTIONAL, .access = LANDLOCK_ACCESS_FS_IOCTL_DEV,Signature · 1 line
2.34.1
That being said, the code transformation in this patch is obviously correct, and it's good to fix the build, so looks good:
But as Mickaël also said, it seems worrying that otherwise correct C code does not compile with that plugin, especially when the conditions under which that happens are not clear.
I would also like to understand this better, so that we can avoid tripping over it in the future.
Thanks!
-Günther