linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Support the nonstring variable attribute (gcc >= 8)
@ 2018-02-17 19:10 Miguel Ojeda
  2018-02-18 23:20 ` David Rientjes
  0 siblings, 1 reply; 9+ messages in thread
From: Miguel Ojeda @ 2018-02-17 19:10 UTC (permalink / raw)
  To: mingo, jpoimboe, keescook, akpm, tglx, geert, gregkh,
	thomas.lendacky, rientjes, will.deacon, linux-kernel

>From the GCC manual:

The nonstring variable attribute specifies that an object or member
declaration with type array of char or pointer to char is intended to
store character arrays that do not necessarily contain a terminating NUL
character. This is useful in detecting uses of such arrays or pointers
with functions that expect NUL-terminated strings, and to avoid warnings
when such an array or pointer is used as an argument to a bounded string
manipulation function such as strncpy.

  https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html

Some reports are already coming to the LKML regarding these
warnings. When they are false positives, we can use __nonstring to let
gcc know a NUL character is not required; like in this case:

  https://lkml.org/lkml/2018/1/16/135

Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: David Rientjes <rientjes@google.com>
---
Another option is using -Wno-stringop-truncation, but it remains to be
seen how useful the new warning will be. We can try to keep it for the
moment until the real bugs and false positives are dealt with and see if
it is worth it.

At least in the reported case at drivers/auxdisplay, using __nonstring
is enough and it can actually replace a comment that was there about the
non-stringness of the char arrays that gcc complained about.
See https://godbolt.org/g/dydPah to play with the warning in this case.

 include/linux/compiler-gcc.h   | 14 ++++++++++++++
 include/linux/compiler_types.h |  4 ++++
 2 files changed, 18 insertions(+)

diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h
index 73bc63e0a1c4..6a9784c0c7f3 100644
--- a/include/linux/compiler-gcc.h
+++ b/include/linux/compiler-gcc.h
@@ -317,6 +317,20 @@
 #define __designated_init __attribute__((designated_init))
 #endif
 
+#if GCC_VERSION >= 80000
+/*
+ * The nonstring variable attribute specifies that an object or member
+ * declaration with type array of char or pointer to char is intended
+ * to store character arrays that do not necessarily contain a terminating
+ * NUL character. This is useful in detecting uses of such arrays or pointers
+ * with functions that expect NUL-terminated strings, and to avoid warnings
+ * when such an array or pointer is used as an argument to a bounded string
+ * manipulation function such as strncpy.
+ * https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
+ */
+#define __nonstring __attribute__((nonstring))
+#endif
+
 #endif	/* gcc version >= 40000 specific checks */
 
 #if !defined(__noclone)
diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 6b79a9bba9a7..654dd3114052 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -271,4 +271,8 @@ struct ftrace_likely_data {
 # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long))
 #endif
 
+#ifndef __nonstring
+# define __nonstring
+#endif
+
 #endif /* __LINUX_COMPILER_TYPES_H */
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-02-17 19:10 [PATCH] Support the nonstring variable attribute (gcc >= 8) Miguel Ojeda
@ 2018-02-18 23:20 ` David Rientjes
  2018-02-19  0:01   ` Miguel Ojeda
  0 siblings, 1 reply; 9+ messages in thread
From: David Rientjes @ 2018-02-18 23:20 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: mingo, jpoimboe, keescook, akpm, tglx, geert, gregkh,
	thomas.lendacky, will.deacon, linux-kernel

On Sat, 17 Feb 2018, Miguel Ojeda wrote:

> From the GCC manual:
> 
> The nonstring variable attribute specifies that an object or member
> declaration with type array of char or pointer to char is intended to
> store character arrays that do not necessarily contain a terminating NUL
> character. This is useful in detecting uses of such arrays or pointers
> with functions that expect NUL-terminated strings, and to avoid warnings
> when such an array or pointer is used as an argument to a bounded string
> manipulation function such as strncpy.
> 
>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
> 
> Some reports are already coming to the LKML regarding these
> warnings. When they are false positives, we can use __nonstring to let
> gcc know a NUL character is not required; like in this case:
> 
>   https://lkml.org/lkml/2018/1/16/135
> 
> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
> Cc: Ingo Molnar <mingo@kernel.org>
> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
> Cc: Kees Cook <keescook@chromium.org>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
> Cc: Will Deacon <will.deacon@arm.com>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: David Rientjes <rientjes@google.com>

I would have expected to have seen __nonstring used somewhere as part of 
this patch.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-02-18 23:20 ` David Rientjes
@ 2018-02-19  0:01   ` Miguel Ojeda
  2018-03-01  9:57     ` Arnd Bergmann
  0 siblings, 1 reply; 9+ messages in thread
From: Miguel Ojeda @ 2018-02-19  0:01 UTC (permalink / raw)
  To: David Rientjes
  Cc: mingo, jpoimboe, Kees Cook, Andrew Morton, Thomas Gleixner,
	Geert Uytterhoeven, Greg KH, thomas.lendacky, will.deacon,
	linux-kernel, Willy Tarreau, Xiongfeng Wang, Arnd Bergmann

On Mon, Feb 19, 2018 at 12:20 AM, David Rientjes <rientjes@google.com> wrote:
> On Sat, 17 Feb 2018, Miguel Ojeda wrote:
>
>> From the GCC manual:
>>
>> The nonstring variable attribute specifies that an object or member
>> declaration with type array of char or pointer to char is intended to
>> store character arrays that do not necessarily contain a terminating NUL
>> character. This is useful in detecting uses of such arrays or pointers
>> with functions that expect NUL-terminated strings, and to avoid warnings
>> when such an array or pointer is used as an argument to a bounded string
>> manipulation function such as strncpy.
>>
>>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
>>
>> Some reports are already coming to the LKML regarding these
>> warnings. When they are false positives, we can use __nonstring to let
>> gcc know a NUL character is not required; like in this case:
>>
>>   https://lkml.org/lkml/2018/1/16/135
>>
>> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
>> Cc: Ingo Molnar <mingo@kernel.org>
>> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
>> Cc: Kees Cook <keescook@chromium.org>
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
>> Cc: Will Deacon <will.deacon@arm.com>
>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>> Cc: David Rientjes <rientjes@google.com>
>
> I would have expected to have seen __nonstring used somewhere as part of
> this patch.

Do you mean to expand the commit message with an actual code example
instead of the links to the docs and the discussion about the report?
Otherwise, if you mean in the actual commit, I think in that case it
should be a patch series, not a single commit.

In any case, the key point here is to agree on the short-term policy:
i.e. whether we want to disable the upcoming warning or try to take
advantage of it (which not *necessarily* implies using __nonstring,
there are other workarounds; though where applicable, __nonstring is
probably the right thing to use).

[By the way, CC'ing Xiongfeng, Willy and Arnd, since they were
involved in the example report; sorry guys!].

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-02-19  0:01   ` Miguel Ojeda
@ 2018-03-01  9:57     ` Arnd Bergmann
  2018-03-02 17:36       ` Miguel Ojeda
  0 siblings, 1 reply; 9+ messages in thread
From: Arnd Bergmann @ 2018-03-01  9:57 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: David Rientjes, Ingo Molnar, Josh Poimboeuf, Kees Cook,
	Andrew Morton, Thomas Gleixner, Geert Uytterhoeven, Greg KH,
	Lendacky, Thomas, Will Deacon, linux-kernel, Willy Tarreau,
	Xiongfeng Wang, Martin Sebor

On Mon, Feb 19, 2018 at 1:01 AM, Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
> On Mon, Feb 19, 2018 at 12:20 AM, David Rientjes <rientjes@google.com> wrote:
>> On Sat, 17 Feb 2018, Miguel Ojeda wrote:
>>
>>> From the GCC manual:
>>>
>>> The nonstring variable attribute specifies that an object or member
>>> declaration with type array of char or pointer to char is intended to
>>> store character arrays that do not necessarily contain a terminating NUL
>>> character. This is useful in detecting uses of such arrays or pointers
>>> with functions that expect NUL-terminated strings, and to avoid warnings
>>> when such an array or pointer is used as an argument to a bounded string
>>> manipulation function such as strncpy.
>>>
>>>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
>>>
>>> Some reports are already coming to the LKML regarding these
>>> warnings. When they are false positives, we can use __nonstring to let
>>> gcc know a NUL character is not required; like in this case:
>>>
>>>   https://lkml.org/lkml/2018/1/16/135
>>>
>>> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
>>> Cc: Ingo Molnar <mingo@kernel.org>
>>> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
>>> Cc: Kees Cook <keescook@chromium.org>
>>> Cc: Andrew Morton <akpm@linux-foundation.org>
>>> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
>>> Cc: Will Deacon <will.deacon@arm.com>
>>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>> Cc: David Rientjes <rientjes@google.com>
>>
>> I would have expected to have seen __nonstring used somewhere as part of
>> this patch.
>
> Do you mean to expand the commit message with an actual code example
> instead of the links to the docs and the discussion about the report?
> Otherwise, if you mean in the actual commit, I think in that case it
> should be a patch series, not a single commit.
>
> In any case, the key point here is to agree on the short-term policy:
> i.e. whether we want to disable the upcoming warning or try to take
> advantage of it (which not *necessarily* implies using __nonstring,
> there are other workarounds; though where applicable, __nonstring is
> probably the right thing to use).

What David was asking for is to have a couple of users of the
__nonstring attribute in places for which it is the right solution.

I would suggest making it a patch series, with patch 1/x introducing
the attribute (i.e. your patch), and followed by additional patches
that add the attribute to individual header files or drivers for which
it is the right solution.

When I looked at the warning, I found that we have around 120 file
for which we warn. The majority of them are actually questionable
uses of strncpy() that probably should have been strscpy(), but
most of those do not actually cause undefined behavior.

A smaller number like the example from ext4 are nonstrings
(i.e. character arrays without nul-termination) that would benefit
from the nonstring attribute. About half of those are actually
arrays of u8/__u8/uint8_t/__uint8_t for which the currently
implemented nonstring attribute is invalid, and it seems odd
to convert those to 'char', e.g.

struct ext4_super_block {
        __le32  s_first_error_time;     /* first time an error happened */
        __le32  s_first_error_ino;      /* inode involved in first error */
        __le64  s_first_error_block;    /* block involved of first error */
-       __u8    s_first_error_func[32]; /* function where the error happened */
+       char    s_first_error_func[32] __nonstring;     /* function
where the error happened */
        __le32  s_first_error_line;     /* line number where error happened */
        __le32  s_last_error_time;      /* most recent time of an error */
        __le32  s_last_error_ino;       /* inode involved in last error */
        __le32  s_last_error_line;      /* line number where error happened */
        __le64  s_last_error_block;     /* block involved of last error */
-       __u8    s_last_error_func[32];  /* function where the error happened */
+       char    s_last_error_func[32] __nonstring;      /* function
where the error happened */

doesn't feel right. Maybe we can extend gcc to also accept
the attribute on arrays of other 8-bit types.

> [By the way, CC'ing Xiongfeng, Willy and Arnd, since they were
> involved in the example report; sorry guys!].

Martin Sebor also asked me about this, he's the one that worked on
the gcc code that introduced the warning. Sorry for not replying earlier.

For a complete list of affected files, see https://pastebin.com/eWFQf58i
this is what I come up with by doing randconfig builds, but I have not
tried to submit additional patches here, since I'm sure that a lot of
those are wrong -- they need a much closer inspection to decide which
ones are actual bugs vs harmless warnings, and which ones should
use strscpy()/strlcpy() vs a nonstring annotation or a rewrite of that
function.

       Arnd

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-03-01  9:57     ` Arnd Bergmann
@ 2018-03-02 17:36       ` Miguel Ojeda
  2018-03-05 19:05         ` Martin Sebor
  0 siblings, 1 reply; 9+ messages in thread
From: Miguel Ojeda @ 2018-03-02 17:36 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: David Rientjes, Ingo Molnar, Josh Poimboeuf, Kees Cook,
	Andrew Morton, Thomas Gleixner, Geert Uytterhoeven, Greg KH,
	Lendacky, Thomas, Will Deacon, linux-kernel, Willy Tarreau,
	Xiongfeng Wang, Martin Sebor

On Thu, Mar 1, 2018 at 10:57 AM, Arnd Bergmann <arnd@arndb.de> wrote:
> On Mon, Feb 19, 2018 at 1:01 AM, Miguel Ojeda
> <miguel.ojeda.sandonis@gmail.com> wrote:
>> On Mon, Feb 19, 2018 at 12:20 AM, David Rientjes <rientjes@google.com> wrote:
>>> On Sat, 17 Feb 2018, Miguel Ojeda wrote:
>>>
>>>> From the GCC manual:
>>>>
>>>> The nonstring variable attribute specifies that an object or member
>>>> declaration with type array of char or pointer to char is intended to
>>>> store character arrays that do not necessarily contain a terminating NUL
>>>> character. This is useful in detecting uses of such arrays or pointers
>>>> with functions that expect NUL-terminated strings, and to avoid warnings
>>>> when such an array or pointer is used as an argument to a bounded string
>>>> manipulation function such as strncpy.
>>>>
>>>>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
>>>>
>>>> Some reports are already coming to the LKML regarding these
>>>> warnings. When they are false positives, we can use __nonstring to let
>>>> gcc know a NUL character is not required; like in this case:
>>>>
>>>>   https://lkml.org/lkml/2018/1/16/135
>>>>
>>>> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
>>>> Cc: Ingo Molnar <mingo@kernel.org>
>>>> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
>>>> Cc: Kees Cook <keescook@chromium.org>
>>>> Cc: Andrew Morton <akpm@linux-foundation.org>
>>>> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
>>>> Cc: Will Deacon <will.deacon@arm.com>
>>>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>> Cc: David Rientjes <rientjes@google.com>
>>>
>>> I would have expected to have seen __nonstring used somewhere as part of
>>> this patch.
>>
>> Do you mean to expand the commit message with an actual code example
>> instead of the links to the docs and the discussion about the report?
>> Otherwise, if you mean in the actual commit, I think in that case it
>> should be a patch series, not a single commit.
>>
>> In any case, the key point here is to agree on the short-term policy:
>> i.e. whether we want to disable the upcoming warning or try to take
>> advantage of it (which not *necessarily* implies using __nonstring,
>> there are other workarounds; though where applicable, __nonstring is
>> probably the right thing to use).
>
> What David was asking for is to have a couple of users of the
> __nonstring attribute in places for which it is the right solution.
>

I understood :-) My question was regarding where he was asking to see it.

> I would suggest making it a patch series, with patch 1/x introducing
> the attribute (i.e. your patch), and followed by additional patches
> that add the attribute to individual header files or drivers for which
> it is the right solution.

Yep, that is what I suggested too.

>
> When I looked at the warning, I found that we have around 120 file
> for which we warn. The majority of them are actually questionable
> uses of strncpy() that probably should have been strscpy(), but
> most of those do not actually cause undefined behavior.

Then it looks like enabling the warning by default is useful and not
too noisy (at least for just char).

>
> A smaller number like the example from ext4 are nonstrings
> (i.e. character arrays without nul-termination) that would benefit
> from the nonstring attribute. About half of those are actually
> arrays of u8/__u8/uint8_t/__uint8_t for which the currently
> implemented nonstring attribute is invalid, and it seems odd
> to convert those to 'char', e.g.
>
> struct ext4_super_block {
>         __le32  s_first_error_time;     /* first time an error happened */
>         __le32  s_first_error_ino;      /* inode involved in first error */
>         __le64  s_first_error_block;    /* block involved of first error */
> -       __u8    s_first_error_func[32]; /* function where the error happened */
> +       char    s_first_error_func[32] __nonstring;     /* function
> where the error happened */
>         __le32  s_first_error_line;     /* line number where error happened */
>         __le32  s_last_error_time;      /* most recent time of an error */
>         __le32  s_last_error_ino;       /* inode involved in last error */
>         __le32  s_last_error_line;      /* line number where error happened */
>         __le64  s_last_error_block;     /* block involved of last error */
> -       __u8    s_last_error_func[32];  /* function where the error happened */
> +       char    s_last_error_func[32] __nonstring;      /* function
> where the error happened */
>
> doesn't feel right. Maybe we can extend gcc to also accept
> the attribute on arrays of other 8-bit types.

Hum... On one hand, the warning is meant to protect against misuses of
the typical string handling functions, and those take pointers to
char. Therefore, one could argue that using signed or unsigned char
already marks an array/pointer as "not a string" (for the purposes of
the attribute).

On the other hand, people *will* call string handling functions with
signed and unsigned char, and for those cases, it is useful to have
the warning nevertheless and being able to annotate those arrays with
nonstring, which is also good documentation-wise. On top of that, C
specifies char as equivalent to either signed or unsigned char (even
if it is a distinct type), so one could argue it should work for the
three types anyway.

Given that 1) this is a warning that can disabled just fine and that
2) we already have real life cases using nonstring, non-char arrays
calling typical string handling functions, I would favor supporting
the warning and the attribute for all the three types.

>
>> [By the way, CC'ing Xiongfeng, Willy and Arnd, since they were
>> involved in the example report; sorry guys!].
>
> Martin Sebor also asked me about this, he's the one that worked on
> the gcc code that introduced the warning. Sorry for not replying earlier.
>

Maybe you can pass this to him? (maybe open a bug in gcc's bugzilla?)

> For a complete list of affected files, see https://pastebin.com/eWFQf58i
> this is what I come up with by doing randconfig builds, but I have not
> tried to submit additional patches here, since I'm sure that a lot of
> those are wrong -- they need a much closer inspection to decide which
> ones are actual bugs vs harmless warnings, and which ones should
> use strscpy()/strlcpy() vs a nonstring annotation or a rewrite of that
> function.

Indeed -- nice work anyway finding those. If we agree on getting the
nonstring attribute, maybe you can send that patch as an RFC to ping
the respective maintainers?

Thanks,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-03-02 17:36       ` Miguel Ojeda
@ 2018-03-05 19:05         ` Martin Sebor
  2018-03-07 12:20           ` Miguel Ojeda
  0 siblings, 1 reply; 9+ messages in thread
From: Martin Sebor @ 2018-03-05 19:05 UTC (permalink / raw)
  To: Miguel Ojeda, Arnd Bergmann
  Cc: David Rientjes, Ingo Molnar, Josh Poimboeuf, Kees Cook,
	Andrew Morton, Thomas Gleixner, Geert Uytterhoeven, Greg KH,
	Lendacky, Thomas, Will Deacon, linux-kernel, Willy Tarreau,
	Xiongfeng Wang

On 03/02/2018 10:36 AM, Miguel Ojeda wrote:
> On Thu, Mar 1, 2018 at 10:57 AM, Arnd Bergmann <arnd@arndb.de> wrote:
>> On Mon, Feb 19, 2018 at 1:01 AM, Miguel Ojeda
>> <miguel.ojeda.sandonis@gmail.com> wrote:
>>> On Mon, Feb 19, 2018 at 12:20 AM, David Rientjes <rientjes@google.com> wrote:
>>>> On Sat, 17 Feb 2018, Miguel Ojeda wrote:
>>>>
>>>>> From the GCC manual:
>>>>>
>>>>> The nonstring variable attribute specifies that an object or member
>>>>> declaration with type array of char or pointer to char is intended to
>>>>> store character arrays that do not necessarily contain a terminating NUL
>>>>> character. This is useful in detecting uses of such arrays or pointers
>>>>> with functions that expect NUL-terminated strings, and to avoid warnings
>>>>> when such an array or pointer is used as an argument to a bounded string
>>>>> manipulation function such as strncpy.
>>>>>
>>>>>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
>>>>>
>>>>> Some reports are already coming to the LKML regarding these
>>>>> warnings. When they are false positives, we can use __nonstring to let
>>>>> gcc know a NUL character is not required; like in this case:
>>>>>
>>>>>   https://lkml.org/lkml/2018/1/16/135
>>>>>
>>>>> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
>>>>> Cc: Ingo Molnar <mingo@kernel.org>
>>>>> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
>>>>> Cc: Kees Cook <keescook@chromium.org>
>>>>> Cc: Andrew Morton <akpm@linux-foundation.org>
>>>>> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
>>>>> Cc: Will Deacon <will.deacon@arm.com>
>>>>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>>> Cc: David Rientjes <rientjes@google.com>
>>>>
>>>> I would have expected to have seen __nonstring used somewhere as part of
>>>> this patch.
>>>
>>> Do you mean to expand the commit message with an actual code example
>>> instead of the links to the docs and the discussion about the report?
>>> Otherwise, if you mean in the actual commit, I think in that case it
>>> should be a patch series, not a single commit.
>>>
>>> In any case, the key point here is to agree on the short-term policy:
>>> i.e. whether we want to disable the upcoming warning or try to take
>>> advantage of it (which not *necessarily* implies using __nonstring,
>>> there are other workarounds; though where applicable, __nonstring is
>>> probably the right thing to use).
>>
>> What David was asking for is to have a couple of users of the
>> __nonstring attribute in places for which it is the right solution.
>>
>
> I understood :-) My question was regarding where he was asking to see it.
>
>> I would suggest making it a patch series, with patch 1/x introducing
>> the attribute (i.e. your patch), and followed by additional patches
>> that add the attribute to individual header files or drivers for which
>> it is the right solution.
>
> Yep, that is what I suggested too.
>
>>
>> When I looked at the warning, I found that we have around 120 file
>> for which we warn. The majority of them are actually questionable
>> uses of strncpy() that probably should have been strscpy(), but
>> most of those do not actually cause undefined behavior.
>
> Then it looks like enabling the warning by default is useful and not
> too noisy (at least for just char).
>
>>
>> A smaller number like the example from ext4 are nonstrings
>> (i.e. character arrays without nul-termination) that would benefit
>> from the nonstring attribute. About half of those are actually
>> arrays of u8/__u8/uint8_t/__uint8_t for which the currently
>> implemented nonstring attribute is invalid, and it seems odd
>> to convert those to 'char', e.g.
>>
>> struct ext4_super_block {
>>         __le32  s_first_error_time;     /* first time an error happened */
>>         __le32  s_first_error_ino;      /* inode involved in first error */
>>         __le64  s_first_error_block;    /* block involved of first error */
>> -       __u8    s_first_error_func[32]; /* function where the error happened */
>> +       char    s_first_error_func[32] __nonstring;     /* function
>> where the error happened */
>>         __le32  s_first_error_line;     /* line number where error happened */
>>         __le32  s_last_error_time;      /* most recent time of an error */
>>         __le32  s_last_error_ino;       /* inode involved in last error */
>>         __le32  s_last_error_line;      /* line number where error happened */
>>         __le64  s_last_error_block;     /* block involved of last error */
>> -       __u8    s_last_error_func[32];  /* function where the error happened */
>> +       char    s_last_error_func[32] __nonstring;      /* function
>> where the error happened */
>>
>> doesn't feel right. Maybe we can extend gcc to also accept
>> the attribute on arrays of other 8-bit types.
>
> Hum... On one hand, the warning is meant to protect against misuses of
> the typical string handling functions, and those take pointers to
> char. Therefore, one could argue that using signed or unsigned char
> already marks an array/pointer as "not a string" (for the purposes of
> the attribute).
>
> On the other hand, people *will* call string handling functions with
> signed and unsigned char, and for those cases, it is useful to have
> the warning nevertheless and being able to annotate those arrays with
> nonstring, which is also good documentation-wise. On top of that, C
> specifies char as equivalent to either signed or unsigned char (even
> if it is a distinct type), so one could argue it should work for the
> three types anyway.
>
> Given that 1) this is a warning that can disabled just fine and that
> 2) we already have real life cases using nonstring, non-char arrays
> calling typical string handling functions, I would favor supporting
> the warning and the attribute for all the three types.
>
>>
>>> [By the way, CC'ing Xiongfeng, Willy and Arnd, since they were
>>> involved in the example report; sorry guys!].
>>
>> Martin Sebor also asked me about this, he's the one that worked on
>> the gcc code that introduced the warning. Sorry for not replying earlier.
>>
>
> Maybe you can pass this to him? (maybe open a bug in gcc's bugzilla?)

I've opened bug 84725 to extend attribute nonstring to the other
two character types:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84725

Thanks
Martin

>
>> For a complete list of affected files, see https://pastebin.com/eWFQf58i
>> this is what I come up with by doing randconfig builds, but I have not
>> tried to submit additional patches here, since I'm sure that a lot of
>> those are wrong -- they need a much closer inspection to decide which
>> ones are actual bugs vs harmless warnings, and which ones should
>> use strscpy()/strlcpy() vs a nonstring annotation or a rewrite of that
>> function.
>
> Indeed -- nice work anyway finding those. If we agree on getting the
> nonstring attribute, maybe you can send that patch as an RFC to ping
> the respective maintainers?
>
> Thanks,
> Miguel
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-03-05 19:05         ` Martin Sebor
@ 2018-03-07 12:20           ` Miguel Ojeda
  2018-03-13 15:41             ` Martin Sebor
  0 siblings, 1 reply; 9+ messages in thread
From: Miguel Ojeda @ 2018-03-07 12:20 UTC (permalink / raw)
  To: Martin Sebor
  Cc: Arnd Bergmann, David Rientjes, Ingo Molnar, Josh Poimboeuf,
	Kees Cook, Andrew Morton, Thomas Gleixner, Geert Uytterhoeven,
	Greg KH, Lendacky, Thomas, Will Deacon, linux-kernel,
	Willy Tarreau, Xiongfeng Wang

On Mon, Mar 5, 2018 at 8:05 PM, Martin Sebor <msebor@gmail.com> wrote:
> On 03/02/2018 10:36 AM, Miguel Ojeda wrote:
>>
>> On Thu, Mar 1, 2018 at 10:57 AM, Arnd Bergmann <arnd@arndb.de> wrote:
>>>
>>> On Mon, Feb 19, 2018 at 1:01 AM, Miguel Ojeda
>>> <miguel.ojeda.sandonis@gmail.com> wrote:
>>>>
>>>> On Mon, Feb 19, 2018 at 12:20 AM, David Rientjes <rientjes@google.com>
>>>> wrote:
>>>>>
>>>>> On Sat, 17 Feb 2018, Miguel Ojeda wrote:
>>>>>
>>>>>> From the GCC manual:
>>>>>>
>>>>>> The nonstring variable attribute specifies that an object or member
>>>>>> declaration with type array of char or pointer to char is intended to
>>>>>> store character arrays that do not necessarily contain a terminating
>>>>>> NUL
>>>>>> character. This is useful in detecting uses of such arrays or pointers
>>>>>> with functions that expect NUL-terminated strings, and to avoid
>>>>>> warnings
>>>>>> when such an array or pointer is used as an argument to a bounded
>>>>>> string
>>>>>> manipulation function such as strncpy.
>>>>>>
>>>>>>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
>>>>>>
>>>>>> Some reports are already coming to the LKML regarding these
>>>>>> warnings. When they are false positives, we can use __nonstring to let
>>>>>> gcc know a NUL character is not required; like in this case:
>>>>>>
>>>>>>   https://lkml.org/lkml/2018/1/16/135
>>>>>>
>>>>>> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
>>>>>> Cc: Ingo Molnar <mingo@kernel.org>
>>>>>> Cc: Josh Poimboeuf <jpoimboe@redhat.com>
>>>>>> Cc: Kees Cook <keescook@chromium.org>
>>>>>> Cc: Andrew Morton <akpm@linux-foundation.org>
>>>>>> Cc: Geert Uytterhoeven <geert@linux-m68k.org>
>>>>>> Cc: Will Deacon <will.deacon@arm.com>
>>>>>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>>>>>> Cc: David Rientjes <rientjes@google.com>
>>>>>
>>>>>
>>>>> I would have expected to have seen __nonstring used somewhere as part
>>>>> of
>>>>> this patch.
>>>>
>>>>
>>>> Do you mean to expand the commit message with an actual code example
>>>> instead of the links to the docs and the discussion about the report?
>>>> Otherwise, if you mean in the actual commit, I think in that case it
>>>> should be a patch series, not a single commit.
>>>>
>>>> In any case, the key point here is to agree on the short-term policy:
>>>> i.e. whether we want to disable the upcoming warning or try to take
>>>> advantage of it (which not *necessarily* implies using __nonstring,
>>>> there are other workarounds; though where applicable, __nonstring is
>>>> probably the right thing to use).
>>>
>>>
>>> What David was asking for is to have a couple of users of the
>>> __nonstring attribute in places for which it is the right solution.
>>>
>>
>> I understood :-) My question was regarding where he was asking to see it.
>>
>>> I would suggest making it a patch series, with patch 1/x introducing
>>> the attribute (i.e. your patch), and followed by additional patches
>>> that add the attribute to individual header files or drivers for which
>>> it is the right solution.
>>
>>
>> Yep, that is what I suggested too.
>>
>>>
>>> When I looked at the warning, I found that we have around 120 file
>>> for which we warn. The majority of them are actually questionable
>>> uses of strncpy() that probably should have been strscpy(), but
>>> most of those do not actually cause undefined behavior.
>>
>>
>> Then it looks like enabling the warning by default is useful and not
>> too noisy (at least for just char).
>>
>>>
>>> A smaller number like the example from ext4 are nonstrings
>>> (i.e. character arrays without nul-termination) that would benefit
>>> from the nonstring attribute. About half of those are actually
>>> arrays of u8/__u8/uint8_t/__uint8_t for which the currently
>>> implemented nonstring attribute is invalid, and it seems odd
>>> to convert those to 'char', e.g.
>>>
>>> struct ext4_super_block {
>>>         __le32  s_first_error_time;     /* first time an error happened
>>> */
>>>         __le32  s_first_error_ino;      /* inode involved in first error
>>> */
>>>         __le64  s_first_error_block;    /* block involved of first error
>>> */
>>> -       __u8    s_first_error_func[32]; /* function where the error
>>> happened */
>>> +       char    s_first_error_func[32] __nonstring;     /* function
>>> where the error happened */
>>>         __le32  s_first_error_line;     /* line number where error
>>> happened */
>>>         __le32  s_last_error_time;      /* most recent time of an error
>>> */
>>>         __le32  s_last_error_ino;       /* inode involved in last error
>>> */
>>>         __le32  s_last_error_line;      /* line number where error
>>> happened */
>>>         __le64  s_last_error_block;     /* block involved of last error
>>> */
>>> -       __u8    s_last_error_func[32];  /* function where the error
>>> happened */
>>> +       char    s_last_error_func[32] __nonstring;      /* function
>>> where the error happened */
>>>
>>> doesn't feel right. Maybe we can extend gcc to also accept
>>> the attribute on arrays of other 8-bit types.
>>
>>
>> Hum... On one hand, the warning is meant to protect against misuses of
>> the typical string handling functions, and those take pointers to
>> char. Therefore, one could argue that using signed or unsigned char
>> already marks an array/pointer as "not a string" (for the purposes of
>> the attribute).
>>
>> On the other hand, people *will* call string handling functions with
>> signed and unsigned char, and for those cases, it is useful to have
>> the warning nevertheless and being able to annotate those arrays with
>> nonstring, which is also good documentation-wise. On top of that, C
>> specifies char as equivalent to either signed or unsigned char (even
>> if it is a distinct type), so one could argue it should work for the
>> three types anyway.
>>
>> Given that 1) this is a warning that can disabled just fine and that
>> 2) we already have real life cases using nonstring, non-char arrays
>> calling typical string handling functions, I would favor supporting
>> the warning and the attribute for all the three types.
>>
>>>
>>>> [By the way, CC'ing Xiongfeng, Willy and Arnd, since they were
>>>> involved in the example report; sorry guys!].
>>>
>>>
>>> Martin Sebor also asked me about this, he's the one that worked on
>>> the gcc code that introduced the warning. Sorry for not replying earlier.
>>>
>>
>> Maybe you can pass this to him? (maybe open a bug in gcc's bugzilla?)
>
>
> I've opened bug 84725 to extend attribute nonstring to the other
> two character types:
>
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84725
>

Thanks Martin! Please let us know whenever it is in the gcc trunk.

Meanwhile I will send a v2 with the auxdisplay original use as an
example for __nonstring in a second patch.

Cheers,
Miguel

> Thanks
> Martin
>
>
>>
>>> For a complete list of affected files, see https://pastebin.com/eWFQf58i
>>> this is what I come up with by doing randconfig builds, but I have not
>>> tried to submit additional patches here, since I'm sure that a lot of
>>> those are wrong -- they need a much closer inspection to decide which
>>> ones are actual bugs vs harmless warnings, and which ones should
>>> use strscpy()/strlcpy() vs a nonstring annotation or a rewrite of that
>>> function.
>>
>>
>> Indeed -- nice work anyway finding those. If we agree on getting the
>> nonstring attribute, maybe you can send that patch as an RFC to ping
>> the respective maintainers?
>>
>> Thanks,
>> Miguel
>>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-03-07 12:20           ` Miguel Ojeda
@ 2018-03-13 15:41             ` Martin Sebor
  2018-03-13 15:57               ` Miguel Ojeda
  0 siblings, 1 reply; 9+ messages in thread
From: Martin Sebor @ 2018-03-13 15:41 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Arnd Bergmann, David Rientjes, Ingo Molnar, Josh Poimboeuf,
	Kees Cook, Andrew Morton, Thomas Gleixner, Geert Uytterhoeven,
	Greg KH, Lendacky, Thomas, Will Deacon, linux-kernel,
	Willy Tarreau, Xiongfeng Wang

>>>> Martin Sebor also asked me about this, he's the one that worked on
>>>> the gcc code that introduced the warning. Sorry for not replying earlier.
>>>>
>>>
>>> Maybe you can pass this to him? (maybe open a bug in gcc's bugzilla?)
>>
>>
>> I've opened bug 84725 to extend attribute nonstring to the other
>> two character types:
>>
>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84725
>>
>
> Thanks Martin! Please let us know whenever it is in the gcc trunk.

No problem.  The patch has been committed into the trunk of GCC 8.
Let me know if there's something else.  It may be too late to make
any more changes like this before GCC 8.1 is released but further
refinements to suppress false positives can be made in GCC 8.2.

Martin

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Support the nonstring variable attribute (gcc >= 8)
  2018-03-13 15:41             ` Martin Sebor
@ 2018-03-13 15:57               ` Miguel Ojeda
  0 siblings, 0 replies; 9+ messages in thread
From: Miguel Ojeda @ 2018-03-13 15:57 UTC (permalink / raw)
  To: Martin Sebor
  Cc: Arnd Bergmann, David Rientjes, Ingo Molnar, Josh Poimboeuf,
	Kees Cook, Andrew Morton, Thomas Gleixner, Geert Uytterhoeven,
	Greg KH, Lendacky, Thomas, Will Deacon, linux-kernel,
	Willy Tarreau, Xiongfeng Wang

On Tue, Mar 13, 2018 at 4:41 PM, Martin Sebor <msebor@gmail.com> wrote:
>>>>> Martin Sebor also asked me about this, he's the one that worked on
>>>>> the gcc code that introduced the warning. Sorry for not replying
>>>>> earlier.
>>>>>
>>>>
>>>> Maybe you can pass this to him? (maybe open a bug in gcc's bugzilla?)
>>>
>>>
>>>
>>> I've opened bug 84725 to extend attribute nonstring to the other
>>> two character types:
>>>
>>> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84725
>>>
>>
>> Thanks Martin! Please let us know whenever it is in the gcc trunk.
>
>
> No problem.  The patch has been committed into the trunk of GCC 8.
> Let me know if there's something else.  It may be too late to make
> any more changes like this before GCC 8.1 is released but further
> refinements to suppress false positives can be made in GCC 8.2.
>

Thanks a lot, I see it in gcc's
2bc9729cac37dee999f4fb48a166cffc198293aa ("PR tree-optimization/84725
- enable attribute nonstring for all narrow character types"). I will
compile it and see how it goes.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-03-13 15:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-17 19:10 [PATCH] Support the nonstring variable attribute (gcc >= 8) Miguel Ojeda
2018-02-18 23:20 ` David Rientjes
2018-02-19  0:01   ` Miguel Ojeda
2018-03-01  9:57     ` Arnd Bergmann
2018-03-02 17:36       ` Miguel Ojeda
2018-03-05 19:05         ` Martin Sebor
2018-03-07 12:20           ` Miguel Ojeda
2018-03-13 15:41             ` Martin Sebor
2018-03-13 15:57               ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).