Skip to contentSkip to conversation

fortify: Update compile-time tests for Clang 14

PATCH In review Kees Cook 3 participants 5 messages Jan 30, 2022 – Jan 31, 2022

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 fortify: Update compile-time tests for Clang 14

Clang 14 introduces support for compiletime_assert(). Update the compile-time warning regex to catch Clang's variant of the warning text in preparation for Clang supporting CONFIG_FORTIFY_SOURCE.

---
I'm splitting this patch out of the main Clang FORTIFY enabling patch.

---
 scripts/test_fortify.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/test_fortify.sh +6 -2
index a4da365508f0..c2688ab8281d 100644
@@ -46,8 +46,12 @@ if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then
 		status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN"
 	fi
 else
-	# If the build failed, check for the warning in the stderr (gcc).
-	if ! grep -q -m1 "error: call to .\b${WANT}\b." "$TMP" ; then
+	# If the build failed, check for the warning in the stderr.
+	# GCC:
+	# ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
+	# Clang 14:
+	# ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
+	if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then
 		status="warning: unsafe ${FUNC}() usage lacked '$WANT' warning in $IN"
 	fi
 fi
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Kees Cook

Clang 14 introduces support for compiletime_assert(). Update the compile-time warning regex to catch Clang's variant of the warning text in preparation for Clang supporting CONFIG_FORTIFY_SOURCE.

Quoted 28 lines

---
I'm splitting this patch out of the main Clang FORTIFY enabling patch.

---
 scripts/test_fortify.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/test_fortify.sh +6 -2
index a4da365508f0..c2688ab8281d 100644
@@ -46,8 +46,12 @@ if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then
 		status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN"
 	fi
 else
-	# If the build failed, check for the warning in the stderr (gcc).
-	if ! grep -q -m1 "error: call to .\b${WANT}\b." "$TMP" ; then
+	# If the build failed, check for the warning in the stderr.
+	# GCC:
+	# ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
+	# Clang 14:
+	# ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
+	if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then
 		status="warning: unsafe ${FUNC}() usage lacked '$WANT' warning in $IN"
 	fi
 fi
Signature · 1 line
2.30.2
#2 lore raw
In reply to Kees Cook
Quoted 4 lines · Kees Cook

Clang 14 introduces support for compiletime_assert(). Update the compile-time warning regex to catch Clang's variant of the warning text in preparation for Clang supporting CONFIG_FORTIFY_SOURCE.

https://twitter.com/ifosteve/status/1190348262500421634?lang=en
error messages can change over time. More thoughts below.

Quoted 18 lines

---
I'm splitting this patch out of the main Clang FORTIFY enabling patch.

---
 scripts/test_fortify.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/test_fortify.sh +0 -0
index a4da365508f0..c2688ab8281d 100644
@@ -46,8 +46,12 @@ if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then
                status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN"

scripts/test_fortify.sh if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then · line 54

Quoted 10 lines
scripts/test_fortify.sh +6 -2
        fi
 else
-       # If the build failed, check for the warning in the stderr (gcc).
-       if ! grep -q -m1 "error: call to .\b${WANT}\b." "$TMP" ; then
+       # If the build failed, check for the warning in the stderr.
+       # GCC:
+       # ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
+       # Clang 14:
+       # ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
+       if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then

Doesn't this depend on -Werror being set? I guess it did so before hand, too, but couldn't I unset CONFIG_WERROR then this check would still fail (since instead of error: we'd have warning:)? If we used __attribute__((error(""))) then this would always be an error. Right now, it is only because -Werror is set promoting the warning diagnostic to an error.

Quoted 6 lines
scripts/test_fortify.sh +0 -1
                status="warning: unsafe ${FUNC}() usage lacked '$WANT' warning in $IN"
        fi
 fi
--

2.30.2

Signature · 2 lines
Thanks,
~Nick Desaulniers
author
#3 lore raw
Quoted 8 lines · Nick Desaulniers
Quoted 4 lines · Kees Cook

Clang 14 introduces support for compiletime_assert(). Update the compile-time warning regex to catch Clang's variant of the warning text in preparation for Clang supporting CONFIG_FORTIFY_SOURCE.

https://twitter.com/ifosteve/status/1190348262500421634?lang=en
error messages can change over time. More thoughts below.

Sure, but I don't want the compile-time checks to silently regress, which requires looking specifically for the error.

Quoted 26 lines
Quoted 26 lines

---
I'm splitting this patch out of the main Clang FORTIFY enabling patch.

---
 scripts/test_fortify.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/test_fortify.sh +5 -2
index a4da365508f0..c2688ab8281d 100644
@@ -46,8 +46,12 @@ if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then
                status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN"
        fi
 else
-       # If the build failed, check for the warning in the stderr (gcc).
-       if ! grep -q -m1 "error: call to .\b${WANT}\b." "$TMP" ; then
+       # If the build failed, check for the warning in the stderr.
+       # GCC:
+       # ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
+       # Clang 14:
+       # ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
Quoted 8 lines

scripts/test_fortify.sh if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then · line 54

Quoted 1 line
scripts/test_fortify.sh +1 -0
+       if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then

Doesn't this depend on -Werror being set? I guess it did so before hand, too, but couldn't I unset CONFIG_WERROR then this check would still fail (since instead of error: we'd have warning:)? If we used __attribute__((error(""))) then this would always be an error. Right now, it is only because -Werror is set promoting the warning diagnostic to an error.

Right, see earlier up in the script. "-Werror" is explicitly set:

line 40: if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then

Signature · 1 line
Kees Cook
Reviewed-by
#4 lore raw
Quoted 49 lines · Kees Cook
Quoted 8 lines · Nick Desaulniers
Quoted 4 lines · Kees Cook

Clang 14 introduces support for compiletime_assert(). Update the compile-time warning regex to catch Clang's variant of the warning text in preparation for Clang supporting CONFIG_FORTIFY_SOURCE.

https://twitter.com/ifosteve/status/1190348262500421634?lang=en
error messages can change over time. More thoughts below.

Sure, but I don't want the compile-time checks to silently regress, which requires looking specifically for the error.

Quoted 26 lines
Quoted 26 lines

---
I'm splitting this patch out of the main Clang FORTIFY enabling patch.

---
 scripts/test_fortify.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/test_fortify.sh +5 -2
index a4da365508f0..c2688ab8281d 100644
@@ -46,8 +46,12 @@ if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then
                status="warning: unsafe ${FUNC}() usage lacked '$WANT' symbol in $IN"
        fi
 else
-       # If the build failed, check for the warning in the stderr (gcc).
-       if ! grep -q -m1 "error: call to .\b${WANT}\b." "$TMP" ; then
+       # If the build failed, check for the warning in the stderr.
+       # GCC:
+       # ./include/linux/fortify-string.h:316:25: error: call to '__write_overflow_field' declared with attribute warning: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror=attribute-warning]
+       # Clang 14:
+       # ./include/linux/fortify-string.h:316:4: error: call to __write_overflow_field declared with 'warning' attribute: detected write beyond size of field (1st parameter); maybe use struct_group()? [-Werror,-Wattribute-warning]
Quoted 8 lines

scripts/test_fortify.sh if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then · line 54

Quoted 1 line
scripts/test_fortify.sh +1 -0
+       if ! grep -Eq -m1 "error: call to .?\b${WANT}\b.?" "$TMP" ; then

Doesn't this depend on -Werror being set? I guess it did so before hand, too, but couldn't I unset CONFIG_WERROR then this check would still fail (since instead of error: we'd have warning:)? If we used __attribute__((error(""))) then this would always be an error. Right now, it is only because -Werror is set promoting the warning diagnostic to an error.

Quoted 3 lines

Right, see earlier up in the script. "-Werror" is explicitly set:

line 40: if "$@" -Werror -c "$IN" -o "$OUT".o 2> "$TMP" ; then

Yep, I missed that.

Quoted 3 lines

--
Kees Cook

Signature · 2 lines
Thanks,
~Nick Desaulniers