Skip to contentSkip to conversation

memremap: Replace 0-length array with flexible array

PATCH In review Kees Cook 4 participants 8 messages Jan 5, 2023 – Jan 6, 2023

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 memremap: Replace 0-length array with flexible array

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

In file included from include/asm-generic/memory_model.h:5,

                 from arch/x86/include/asm/page.h:86,
                 from arch/x86/include/asm/thread_info.h:12,
                 from include/linux/thread_info.h:60,
                 from arch/x86/include/asm/preempt.h:9,
                 from include/linux/preempt.h:78,
                 from include/linux/spinlock.h:56,
                 from include/linux/mmzone.h:8,
                 from include/linux/gfp.h:7,
                 from include/linux/mm.h:7,
                 from mm/sparse-vmemmap.c:21:

In function 'reuse_compound_section',
inlined from 'vmemmap_populate_compound_pages' at mm/sparse-vmemmap.c:407:6,
inlined from '__populate_section_memmap' at mm/sparse-vmemmap.c:463:7:
mm/sparse-vmemmap.c:376:39: warning: array subscript <unknown> is outside array bounds of 'struct range[0]' [-Warray-bounds=]

  376 |                 PHYS_PFN(pgmap->ranges[pgmap->nr_range].start);
      |                          ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

include/linux/pfn.h:22:43: note: in definition of macro 'PHYS_PFN'
22 | #define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))
| ^
In file included from include/linux/mm.h:31:
include/linux/memremap.h: In function '__populate_section_memmap':
include/linux/memremap.h:138:30: note: while referencing 'ranges'
138 | struct range ranges[0];
| ^~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc Andrew Morton <[email protected]>
Cc Dan Williams <[email protected]>
Cc Alex Sierra <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Shiyang Ruan <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
---
 include/linux/memremap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/memremap.h +1 -1
index 7fcaf3180a5b..1314d9c5f05b 100644
@@ -135,7 +135,7 @@ struct dev_pagemap {
 	int nr_range;
 	union {
 		struct range range;
-		struct range ranges[0];
+		DECLARE_FLEX_ARRAY(struct range, ranges);
 	};
 };
 
Signature · 1 line
2.34.1
#1 lore raw

I think this is the same patch:

https://lore.kernel.org/linux-hardening/YxKO%2FjY1x0xTpl4r@work/

It's actually in linux-next.

--
Gustavo

Quoted 61 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

In file included from include/asm-generic/memory_model.h:5,

                 from arch/x86/include/asm/page.h:86,
                 from arch/x86/include/asm/thread_info.h:12,
                 from include/linux/thread_info.h:60,
                 from arch/x86/include/asm/preempt.h:9,
                 from include/linux/preempt.h:78,
                 from include/linux/spinlock.h:56,
                 from include/linux/mmzone.h:8,
                 from include/linux/gfp.h:7,
                 from include/linux/mm.h:7,
                 from mm/sparse-vmemmap.c:21:

In function 'reuse_compound_section',
inlined from 'vmemmap_populate_compound_pages' at mm/sparse-vmemmap.c:407:6,
inlined from '__populate_section_memmap' at mm/sparse-vmemmap.c:463:7:
mm/sparse-vmemmap.c:376:39: warning: array subscript <unknown> is outside array bounds of 'struct range[0]' [-Warray-bounds=]

  376 |                 PHYS_PFN(pgmap->ranges[pgmap->nr_range].start);
      |                          ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

include/linux/pfn.h:22:43: note: in definition of macro 'PHYS_PFN'
22 | #define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))
| ^
In file included from include/linux/mm.h:31:
include/linux/memremap.h: In function '__populate_section_memmap':
include/linux/memremap.h:138:30: note: while referencing 'ranges'
138 | struct range ranges[0];
| ^~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Cc Andrew Morton <[email protected]>
Cc Dan Williams <[email protected]>
Cc Alex Sierra <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Shiyang Ruan <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
---
 include/linux/memremap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/memremap.h +1 -1
index 7fcaf3180a5b..1314d9c5f05b 100644
@@ -135,7 +135,7 @@ struct dev_pagemap {
 	int nr_range;
 	union {
 		struct range range;
-		struct range ranges[0];
+		DECLARE_FLEX_ARRAY(struct range, ranges);
 	};
 };
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#7 lore raw
In reply to Kees Cook
Quoted 32 lines · Kees Cook

Zero-length arrays are deprecated[1]. Replace struct ethtool_rxnfc's "rule_locs" 0-length array with a flexible array. Detected with GCC 13, using -fstrict-flex-arrays=3:

In file included from include/asm-generic/memory_model.h:5,

                 from arch/x86/include/asm/page.h:86,
                 from arch/x86/include/asm/thread_info.h:12,
                 from include/linux/thread_info.h:60,
                 from arch/x86/include/asm/preempt.h:9,
                 from include/linux/preempt.h:78,
                 from include/linux/spinlock.h:56,
                 from include/linux/mmzone.h:8,
                 from include/linux/gfp.h:7,
                 from include/linux/mm.h:7,
                 from mm/sparse-vmemmap.c:21:

In function 'reuse_compound_section',
inlined from 'vmemmap_populate_compound_pages' at mm/sparse-vmemmap.c:407:6,
inlined from '__populate_section_memmap' at mm/sparse-vmemmap.c:463:7:
mm/sparse-vmemmap.c:376:39: warning: array subscript <unknown> is outside array bounds of 'struct range[0]' [-Warray-bounds=]

  376 |                 PHYS_PFN(pgmap->ranges[pgmap->nr_range].start);
      |                          ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

include/linux/pfn.h:22:43: note: in definition of macro 'PHYS_PFN'
22 | #define PHYS_PFN(x) ((unsigned long)((x) >> PAGE_SHIFT))
| ^
In file included from include/linux/mm.h:31:
include/linux/memremap.h: In function '__populate_section_memmap':
include/linux/memremap.h:138:30: note: while referencing 'ranges'
138 | struct range ranges[0];
| ^~~~~~

[1] https://www.kernel.org/doc/html/latest/process/deprecated.html#zero-length-and-one-element-arrays

Quoted 9 lines
Cc Andrew Morton <[email protected]>
Cc Dan Williams <[email protected]>
Cc Alex Sierra <[email protected]>
Cc Felix Kuehling <[email protected]>
Cc "Matthew Wilcox (Oracle)" <[email protected]>
Cc Shiyang Ruan <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>

Looks good to me:

Quoted 20 lines
---
 include/linux/memremap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/memremap.h +1 -1
index 7fcaf3180a5b..1314d9c5f05b 100644
@@ -135,7 +135,7 @@ struct dev_pagemap {
 	int nr_range;
 	union {
 		struct range range;
-		struct range ranges[0];
+		DECLARE_FLEX_ARRAY(struct range, ranges);
 	};
 };
 
Signature · 1 line
2.34.1
#4 lore raw
Quoted 9 lines · Kees Cook
Quoted 5 lines · Gustavo A. R. Silva

I think this is the same patch:

https://lore.kernel.org/linux-hardening/YxKO%2FjY1x0xTpl4r@work/

It's actually in linux-next.

Hm, it's been in -next since September? Is this in a tree of yours that didn't get pulled for v6.2?

No. I didn't send it. It's just one of the two patches I have in my -next tree. They didn't look like high priority at the time.

--
Gustavo

author
#5 lore raw
Quoted 8 lines · Gustavo A. R. Silva
Quoted 5 lines · Kees Cook
Quoted 4 lines · Gustavo A. R. Silva

I think this is the same patch:

https://lore.kernel.org/linux-hardening/YxKO%2FjY1x0xTpl4r@work/

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

It's actually in linux-next.

Hm, it's been in -next since September? Is this in a tree of yours that didn't get pulled for v6.2?

No. I didn't send it. It's just one of the two patches I have in my -next tree. They didn't look like high priority at the time.

Cool. I'd like to get -fstrict-flex-arrays=3 finished this devel cycle, if possible. Can you add this one too?

https://lore.kernel.org/lkml/YzIc8z+QaHvqPjLX@work

I don't see it in patchwork which I find confusing too. :P

Signature · 1 line
Kees Cook
#6 lore raw
Quoted 14 lines · Kees Cook
Quoted 13 lines · Gustavo A. R. Silva
Quoted 9 lines · Kees Cook
Quoted 5 lines · Gustavo A. R. Silva

I think this is the same patch:

https://lore.kernel.org/linux-hardening/YxKO%2FjY1x0xTpl4r@work/

It's actually in linux-next.

Hm, it's been in -next since September? Is this in a tree of yours that didn't get pulled for v6.2?

Quoted 7 lines
Quoted 2 lines

No. I didn't send it. It's just one of the two patches I have in my -next tree. They didn't look like high priority at the time.

Cool. I'd like to get -fstrict-flex-arrays=3 finished this devel cycle, if possible. Can you add this one too?

https://lore.kernel.org/lkml/YzIc8z+QaHvqPjLX@work

Done!

https://git.kernel.org/pub/scm/linux/kernel/git/gustavoars/linux.git/log/?h=for-next/kspp

Quoted 2 lines

I don't see it in patchwork which I find confusing too. :P

mmh... and I don't remember updating it in patchwork before. (thinkingface)

--
Gustavo