Skip to contentSkip to conversation

fix is_pure_ops_struct()

PATCH In review Joonwon Kang 2 participants 6 messages Jul 31, 2019 – Aug 1, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover fix is_pure_ops_struct()

Hello,

This series fixes unreachable code bug and removes dead code in is_pure_ops_struct().

Thanks.

Joonwon Kang (2):
randstruct: fix a bug in is_pure_ops_struct()
randstruct: remove dead code in is_pure_ops_struct()

 scripts/gcc-plugins/randomize_layout_plugin.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/2 randstruct: fix a bug in is_pure_ops_struct()

Before this, there were false negatives in the case where a struct contains other structs which contain only function pointers because of unreachable code in is_pure_ops_struct().

---
 scripts/gcc-plugins/randomize_layout_plugin.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
scripts/gcc-plugins/randomize_layout_plugin.c +5 -5
index 6d5bbd31db7f..bd29e4e7a524 100644
@@ -443,13 +443,13 @@ static int is_pure_ops_struct(const_tree node)
 		if (node = fieldtype)
 			continue;
 
-		if (!is_fptr(fieldtype))
-			return 0;
-
-		if (code != RECORD_TYPE && code != UNION_TYPE)
+		if (code = RECORD_TYPE || code = UNION_TYPE) {
+			if (!is_pure_ops_struct(fieldtype))
+				return 0;
 			continue;
+		}
 
-		if (!is_pure_ops_struct(fieldtype))
+		if (!is_fptr(fieldtype))
 			return 0;
 	}
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH 2/2 randstruct: remove dead code in is_pure_ops_struct()

In reply to Joonwon Kang

Recursive declaration for struct which has member of the same struct type, for example,

struct foo {
    struct foo f;
    ...
};

is not allowed. So, it is unnecessary to check if a struct has this kind of member.

---
 scripts/gcc-plugins/randomize_layout_plugin.c | 3 ---
 1 file changed, 3 deletions(-)
scripts/gcc-plugins/randomize_layout_plugin.c +0 -3
index bd29e4e7a524..e14efe23e645 100644
@@ -440,9 +440,6 @@ static int is_pure_ops_struct(const_tree node)
 		const_tree fieldtype = get_field_type(field);
 		enum tree_code code = TREE_CODE(fieldtype);
 
-		if (node = fieldtype)
-			continue;
-
 		if (code = RECORD_TYPE || code = UNION_TYPE) {
 			if (!is_pure_ops_struct(fieldtype))
 				return 0;
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH 1/2] randstruct: fix a bug in is_pure_ops_struct()

In reply to Joonwon Kang
Quoted 5 lines · Joonwon Kang

Before this, there were false negatives in the case where a struct contains other structs which contain only function pointers because of unreachable code in is_pure_ops_struct().

I've applied this (with some commit log tweaks) and it should be visible in linux-next soon. I'll send this on to Linus before -rc3.

Signature · 1 line
Kees Cook
#4 lore raw

Re: [PATCH 2/2] randstruct: remove dead code in is_pure_ops_struct()

In reply to Joonwon Kang
Quoted 10 lines · Joonwon Kang

Recursive declaration for struct which has member of the same struct type, for example,

struct foo {
    struct foo f;
    ...
};

is not allowed. So, it is unnecessary to check if a struct has this kind of member.

Is that the only case where this loop could happen? Seems also safe to just leave it as-is...

-Kees

Quoted 23 lines
---
 scripts/gcc-plugins/randomize_layout_plugin.c | 3 ---
 1 file changed, 3 deletions(-)
scripts/gcc-plugins/randomize_layout_plugin.c +0 -3
index bd29e4e7a524..e14efe23e645 100644
@@ -440,9 +440,6 @@ static int is_pure_ops_struct(const_tree node)
 		const_tree fieldtype = get_field_type(field);
 		enum tree_code code = TREE_CODE(fieldtype);
 
-		if (node = fieldtype)
-			continue;
-
 		if (code = RECORD_TYPE || code = UNION_TYPE) {
 			if (!is_pure_ops_struct(fieldtype))
 				return 0;
Signature · 1 line
2.17.1
Signature · 1 line
Kees Cook
#5 lore raw

Re: [PATCH 2/2] randstruct: remove dead code in is_pure_ops_struct()

Quoted 9 lines · Kees Cook
Quoted 8 lines · Joonwon Kang

Recursive declaration for struct which has member of the same struct type, for example,

struct foo {
    struct foo f;
    ...
};
Quoted 7 lines
Quoted 2 lines

is not allowed. So, it is unnecessary to check if a struct has this kind of member.

Is that the only case where this loop could happen? Seems also safe to just leave it as-is...

-Kees

I think it is pretty obvious that it is the only case. I compiled kernel with allyesconfig and the condition never hit even once. However, it will also be no problem to just leave it as-is as you mentioned.

Quoted 27 lines
Quoted 24 lines
---
 scripts/gcc-plugins/randomize_layout_plugin.c | 3 ---
 1 file changed, 3 deletions(-)
scripts/gcc-plugins/randomize_layout_plugin.c +0 -3
index bd29e4e7a524..e14efe23e645 100644
@@ -440,9 +440,6 @@ static int is_pure_ops_struct(const_tree node)
 		const_tree fieldtype = get_field_type(field);
 		enum tree_code code = TREE_CODE(fieldtype);
 
-		if (node = fieldtype)
-			continue;
-
 		if (code = RECORD_TYPE || code = UNION_TYPE) {
 			if (!is_pure_ops_struct(fieldtype))
 				return 0;
Signature · 1 line
2.17.1
Signature · 1 line
Kees Cook