x86/mm/pgtable: Fix -Wstringop-overflow warnings
PATCH v2 1/1 x86/mm/pgtable: Fix -Wstringop-overflow warnings
Fix the following -Wstringop-overflow warnings when building with GCC-12.1:
arch/x86/mm/pgtable.c:437:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=] arch/x86/mm/pgtable.c:440:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=] arch/x86/mm/pgtable.c:462:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=] arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=] arch/x86/mm/pgtable.c:455:9: warning: 'pgd_prepopulate_user_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=] arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]
There is a case in which PREALLOCATED_PMDS, MAX_PREALLOCATED_PMDS, PREALLOCATED_USER_PMDS and MAX_PREALLOCATED_USER_PMDS are defined as zero:
204 #else /* !CONFIG_X86_PAE */
205
206 /* No need to prepopulate any pagetable entries in non-PAE modes. */
207 #define PREALLOCATED_PMDS 0
208 #define MAX_PREALLOCATED_PMDS 0
209 #define PREALLOCATED_USER_PMDS 0
210 #define MAX_PREALLOCATED_USER_PMDS 0
211 #endif /* CONFIG_X86_PAE */
It seems that GCC is legitimately complaining about the fact that, under certain circumstances, u_pmds and pmds are declared as zero-length arrays in the stack and, of course, they are not flexible arrays.
424 pgd_t *pgd_alloc(struct mm_struct *mm)
425 {
426 pgd_t *pgd; 427 pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS]; 428 pmd_t *pmds[MAX_PREALLOCATED_PMDS];
429
Notice that "Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed."[1]
We can fix this by checking that MAX_PREALLOCATED_PMDS and MAX_PREALLOCATED_USER_PMDS are different than zero, prior to passing u_pmds amd pmds as arguments to any function, in this case to functions preallocate_pmds(), pgd_prepopulate_pmd() and free_pmds().
This helps with the ongoing efforts to globally enable -Wstringop-overflow.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
---
Changes in v2:
- Check MAX_PREALLOCATED_PMDS and MAX_PREALLOCATED_USER_PMDS instead of using pointer notation.
- Update changelog text.
arch/x86/mm/pgtable.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
arch/x86/mm/pgtable.c +10 -6
@@ -434,14 +434,18 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds; -if (paravirt_pgd_alloc (mm) !=0 ) - goto out_free_user_pmds; +if (paravirt_pgd_alloc (mm) !=0 ) + goto out_free_user_pmds; + }else { + goto out_free_pgd; + }/* * Make sure that pre-populating the pmds is atomic with
Signature · 1 line
2.27.0
Quoted 22 lines · Gustavo A. R. Silva
Fix the following -Wstringop-overflow warnings when building with GCC-12.1:
arch/x86/mm/pgtable.c:437:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:440:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:462:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:455:9: warning: 'pgd_prepopulate_user_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]There is a case in which
PREALLOCATED_PMDS,MAX_PREALLOCATED_PMDS,PREALLOCATED_USER_PMDSandMAX_PREALLOCATED_USER_PMDSare defined as zero:204 #else /* !CONFIG_X86_PAE */
205
206 /* No need to prepopulate any pagetable entries in non-PAE modes. */
207 #definePREALLOCATED_PMDS0
208 #defineMAX_PREALLOCATED_PMDS0
209 #definePREALLOCATED_USER_PMDS0
210 #defineMAX_PREALLOCATED_USER_PMDS0
211 #endif /*CONFIG_X86_PAE*/
Quoted 3 lines
It seems that GCC is legitimately complaining about the fact that, under certain circumstances,
u_pmdsand pmds are declared as zero-length arrays in the stack and, of course, they are not flexible arrays.
Ah yeah, I've run into this a few times. Since the relationship between the macro pairs can't be seen by GCC, it gets upset (i.e. sizeof(u_pmds) has no relationship wtih PREALLOCATED_USER_PMDS and the calls weren't inlined, so it can't see that it'll always be 0 and 0).
Quoted 47 lines
424
pgd_t*pgd_alloc(structmm_struct*mm)
425 {426 pgd_t *pgd; 427 pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS]; 428 pmd_t *pmds[MAX_PREALLOCATED_PMDS];429
Notice that "Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed."[1]
We can fix this by checking that
MAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSare different than zero, prior to passingu_pmdsamd pmds as arguments to any function, in this case to functionspreallocate_pmds(),pgd_prepopulate_pmd()andfree_pmds().This helps with the ongoing efforts to globally enable -Wstringop-overflow.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
---
Changes in v2:
- CheckMAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSinstead of using pointer notation.- Update changelog text.
arch/x86/mm/pgtable.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/x86/mm/pgtable.c +3
-3@@ -434,14 +434,18 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 )
arch/x86/mm/pgtable.c pgd_t *pgd_alloc(struct mm_struct *mm) · line 447
Quoted 10 lines
arch/x86/mm/pgtable.c +6
-3- goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds; -if (paravirt_pgd_alloc (mm) !=0 ) - goto out_free_user_pmds; +if (paravirt_pgd_alloc (mm) !=0 ) + goto out_free_user_pmds; + }else { + goto out_free_pgd;
The "all 0" case shouldn't be a failure mode; it should just skip the preallocate_pmds() calls.
Quoted 7 lines
arch/x86/mm/pgtable.c +1
-0+ }/* * Make sure that pre-populating the pmds is atomic with Signature · 1 line
2.27.0
Signature · 1 line
Kees Cook
Quoted 83 lines · Kees Cook
Quoted 22 lines · Gustavo A. R. Silva
Fix the following -Wstringop-overflow warnings when building with GCC-12.1:
arch/x86/mm/pgtable.c:437:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:440:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:462:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:455:9: warning: 'pgd_prepopulate_user_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]There is a case in which
PREALLOCATED_PMDS,MAX_PREALLOCATED_PMDS,PREALLOCATED_USER_PMDSandMAX_PREALLOCATED_USER_PMDSare defined as zero:204 #else /* !CONFIG_X86_PAE */
205
206 /* No need to prepopulate any pagetable entries in non-PAE modes. */
207 #definePREALLOCATED_PMDS0
208 #defineMAX_PREALLOCATED_PMDS0
209 #definePREALLOCATED_USER_PMDS0
210 #defineMAX_PREALLOCATED_USER_PMDS0
211 #endif /*CONFIG_X86_PAE*/Quoted 3 lines
It seems that GCC is legitimately complaining about the fact that, under certain circumstances,
u_pmdsand pmds are declared as zero-length arrays in the stack and, of course, they are not flexible arrays.Ah yeah, I've run into this a few times. Since the relationship between the macro pairs can't be seen by GCC, it gets upset (i.e.
sizeof(u_pmds)has no relationship wtihPREALLOCATED_USER_PMDSand the calls weren't inlined, so it can't see that it'll always be 0 and 0).Quoted 51 lines
424
pgd_t*pgd_alloc(structmm_struct*mm)
425 {426 pgd_t *pgd; 427 pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS]; 428 pmd_t *pmds[MAX_PREALLOCATED_PMDS];429
Notice that "Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed."[1]
We can fix this by checking that
MAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSare different than zero, prior to passingu_pmdsamd pmds as arguments to any function, in this case to functionspreallocate_pmds(),pgd_prepopulate_pmd()andfree_pmds().This helps with the ongoing efforts to globally enable -Wstringop-overflow.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
---
Changes in v2:
- CheckMAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSinstead of using pointer notation.- Update changelog text.
arch/x86/mm/pgtable.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/x86/mm/pgtable.c +5
-4@@ -434,14 +434,18 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds;
Quoted 9 lines
arch/x86/mm/pgtable.c pgd_t *pgd_alloc(struct mm_struct *mm) · line 447
Quoted 6 lines
arch/x86/mm/pgtable.c +4
-2-if (paravirt_pgd_alloc (mm) !=0 ) - goto out_free_user_pmds; +if (paravirt_pgd_alloc (mm) !=0 ) + goto out_free_user_pmds; + }else { + goto out_free_pgd;The "all 0" case shouldn't be a failure mode; it should just skip the
preallocate_pmds()calls.
Do you mean something like this:
arch/x86/mm/pgtable.c +6 -4
@@ -434,11 +434,13 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds; + }if (paravirt_pgd_alloc (mm) !=0 ) goto out_free_user_pmds;
It seems that the above is not enough, because we have the same issue when calling pgd_prepopulate_pmd(), pgd_prepopulate_user_pmd() and free_pmds():
CC arch/x86/mm/pgtable.oarch/x86/mm/pgtable.c: In function 'pgd_alloc':arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]
464 | free_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/pgtable.c:464:9: note: referencing argument 2 of type 'pmd_t *[0]'
arch/x86/mm/pgtable.c:213:13: note: in a call to function 'free_pmds'
213 | static void free_pmds(struct mm_struct *mm, pmd_t *pmds[], int count)
| ^~~~~~~~~
arch/x86/mm/pgtable.c:466:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]
466 | free_pmds(mm, pmds, PREALLOCATED_PMDS);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/pgtable.c:466:9: note: referencing argument 2 of type 'pmd_t *[0]'
arch/x86/mm/pgtable.c:213:13: note: in a call to function 'free_pmds'
213 | static void free_pmds(struct mm_struct *mm, pmd_t *pmds[], int count)
| ^~~~~~~~~
arch/x86/mm/pgtable.c:456:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]
456 | pgd_prepopulate_pmd(mm, pgd, pmds);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/pgtable.c:456:9: note: referencing argument 3 of type 'pmd_t *[0]'
arch/x86/mm/pgtable.c:296:13: note: in a call to function 'pgd_prepopulate_pmd'
296 | static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
| ^~~~~~~~~~~~~~~~~~~
arch/x86/mm/pgtable.c:457:9: warning: 'pgd_prepopulate_user_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]
457 | pgd_prepopulate_user_pmd(mm, pgd, u_pmds);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/pgtable.c:457:9: note: referencing argument 3 of type 'pmd_t *[0]'
arch/x86/mm/pgtable.c:320:13: note: in a call to function 'pgd_prepopulate_user_pmd'
320 | static void pgd_prepopulate_user_pmd(struct mm_struct *mm,
| ^~~~~~~~~~~~~~~~~~~~~~~~--
Gustavo
Quoted 11 lines
Quoted 8 lines
arch/x86/mm/pgtable.c +1
-0+ }/* * Make sure that pre-populating the pmds is atomic with Signature · 1 line
2.27.0Signature · 1 line
Kees Cook
Quoted 123 lines · Gustavo A. R. Silva
Quoted 83 lines · Kees Cook
Quoted 22 lines · Gustavo A. R. Silva
Fix the following -Wstringop-overflow warnings when building with GCC-12.1:
arch/x86/mm/pgtable.c:437:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:440:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:462:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:455:9: warning: 'pgd_prepopulate_user_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]There is a case in which
PREALLOCATED_PMDS,MAX_PREALLOCATED_PMDS,PREALLOCATED_USER_PMDSandMAX_PREALLOCATED_USER_PMDSare defined as zero:204 #else /* !CONFIG_X86_PAE */
205
206 /* No need to prepopulate any pagetable entries in non-PAE modes. */
207 #definePREALLOCATED_PMDS0
208 #defineMAX_PREALLOCATED_PMDS0
209 #definePREALLOCATED_USER_PMDS0
210 #defineMAX_PREALLOCATED_USER_PMDS0
211 #endif /*CONFIG_X86_PAE*/Quoted 3 lines
It seems that GCC is legitimately complaining about the fact that, under certain circumstances,
u_pmdsand pmds are declared as zero-length arrays in the stack and, of course, they are not flexible arrays.Ah yeah, I've run into this a few times. Since the relationship between the macro pairs can't be seen by GCC, it gets upset (i.e.
sizeof(u_pmds)has no relationship wtihPREALLOCATED_USER_PMDSand the calls weren't inlined, so it can't see that it'll always be 0 and 0).Quoted 51 lines
424
pgd_t*pgd_alloc(structmm_struct*mm)
425 {426 pgd_t *pgd; 427 pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS]; 428 pmd_t *pmds[MAX_PREALLOCATED_PMDS];429
Notice that "Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed."[1]
We can fix this by checking that
MAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSare different than zero, prior to passingu_pmdsamd pmds as arguments to any function, in this case to functionspreallocate_pmds(),pgd_prepopulate_pmd()andfree_pmds().This helps with the ongoing efforts to globally enable -Wstringop-overflow.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
---
Changes in v2:
- CheckMAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSinstead of using pointer notation.- Update changelog text.
arch/x86/mm/pgtable.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/x86/mm/pgtable.c +5
-4@@ -434,14 +434,18 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds;Quoted 9 lines
arch/x86/mm/pgtable.c pgd_t *pgd_alloc(struct mm_struct *mm) · line 447
Quoted 6 lines
arch/x86/mm/pgtable.c +4
-2-if (paravirt_pgd_alloc (mm) !=0 ) - goto out_free_user_pmds; +if (paravirt_pgd_alloc (mm) !=0 ) + goto out_free_user_pmds; + }else { + goto out_free_pgd;The "all 0" case shouldn't be a failure mode; it should just skip the
preallocate_pmds()calls.Do you mean something like this:
arch/x86/mm/pgtable.c +6
-4@@ -434,11 +434,13 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds; + }if (paravirt_pgd_alloc (mm) !=0 ) goto out_free_user_pmds;It seems that the above is not enough, because we have the same issue when calling
pgd_prepopulate_pmd(),pgd_prepopulate_user_pmd()andfree_pmds():
Quoted 5 lines
CC
arch/x86/mm/pgtable.oarch/x86/mm/pgtable.c: In function 'pgd_alloc':arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]464 | free_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ugh. Perhaps just marking both preallocate_pmds() and free_pmds() as inline is enough to let the compiler "see" everything correctly?
Otherwise, they'll likely each need the same check that was added to pgd_prepopulate_pmd() ages ago for a similar situation...
Signature · 1 line
Kees Cook
Quoted 124 lines · Kees Cook
Quoted 123 lines · Gustavo A. R. Silva
Quoted 83 lines · Kees Cook
Quoted 22 lines · Gustavo A. R. Silva
Fix the following -Wstringop-overflow warnings when building with GCC-12.1:
arch/x86/mm/pgtable.c:437:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:440:13: warning: 'preallocate_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:462:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:455:9: warning: 'pgd_prepopulate_user_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]There is a case in which
PREALLOCATED_PMDS,MAX_PREALLOCATED_PMDS,PREALLOCATED_USER_PMDSandMAX_PREALLOCATED_USER_PMDSare defined as zero:204 #else /* !CONFIG_X86_PAE */
205
206 /* No need to prepopulate any pagetable entries in non-PAE modes. */
207 #definePREALLOCATED_PMDS0
208 #defineMAX_PREALLOCATED_PMDS0
209 #definePREALLOCATED_USER_PMDS0
210 #defineMAX_PREALLOCATED_USER_PMDS0
211 #endif /*CONFIG_X86_PAE*/Quoted 3 lines
It seems that GCC is legitimately complaining about the fact that, under certain circumstances,
u_pmdsand pmds are declared as zero-length arrays in the stack and, of course, they are not flexible arrays.Ah yeah, I've run into this a few times. Since the relationship between the macro pairs can't be seen by GCC, it gets upset (i.e.
sizeof(u_pmds)has no relationship wtihPREALLOCATED_USER_PMDSand the calls weren't inlined, so it can't see that it'll always be 0 and 0).Quoted 51 lines
424
pgd_t*pgd_alloc(structmm_struct*mm)
425 {426 pgd_t *pgd; 427 pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS]; 428 pmd_t *pmds[MAX_PREALLOCATED_PMDS];429
Notice that "Accessing elements of zero-length arrays declared in such contexts is undefined and may be diagnosed."[1]
We can fix this by checking that
MAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSare different than zero, prior to passingu_pmdsamd pmds as arguments to any function, in this case to functionspreallocate_pmds(),pgd_prepopulate_pmd()andfree_pmds().This helps with the ongoing efforts to globally enable -Wstringop-overflow.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
---
Changes in v2:
- CheckMAX_PREALLOCATED_PMDSandMAX_PREALLOCATED_USER_PMDSinstead of using pointer notation.- Update changelog text.
arch/x86/mm/pgtable.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)arch/x86/mm/pgtable.c +5
-4@@ -434,14 +434,18 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds;Quoted 9 lines
arch/x86/mm/pgtable.c pgd_t *pgd_alloc(struct mm_struct *mm) · line 447
Quoted 6 lines
arch/x86/mm/pgtable.c +4
-2-if (paravirt_pgd_alloc (mm) !=0 ) - goto out_free_user_pmds; +if (paravirt_pgd_alloc (mm) !=0 ) + goto out_free_user_pmds; + }else { + goto out_free_pgd;The "all 0" case shouldn't be a failure mode; it should just skip the
preallocate_pmds()calls.Do you mean something like this:
arch/x86/mm/pgtable.c +6
-4@@ -434,11 +434,13 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds; + }if (paravirt_pgd_alloc (mm) !=0 ) goto out_free_user_pmds;It seems that the above is not enough, because we have the same issue when calling
pgd_prepopulate_pmd(),pgd_prepopulate_user_pmd()andfree_pmds():
Quoted 8 lines
Quoted 5 lines
CC
arch/x86/mm/pgtable.oarch/x86/mm/pgtable.c: In function 'pgd_alloc':arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]464 | free_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Ugh. Perhaps just marking both
preallocate_pmds()andfree_pmds()as inline is enough to let the compiler "see" everything correctly?
It doesn't seem to work... however, the following piece of code implies that pmds and u_pmds should be first preallocated through preallocate_pmds(), which cannot happen if (MAX_PREALLOCATED_PMDS != 0 && MAX_PREALLOCATED_USER_PMDS != 0)
448 /*
449 * Make sure that pre-populating the pmds is atomic with 450 * respect to anything walking the pgd_list, so that they 451 * never see a partially populated pgd. 452 */ 453 spin_lock(&pgd_lock); 454 455 pgd_ctor(mm, pgd); 456 pgd_prepopulate_pmd(mm, pgd, pmds); 457 pgd_prepopulate_user_pmd(mm, pgd, u_pmds);
458
459 spin_unlock(&pgd_lock);
460
461 return pgd;
So, my question here is why do you think the "all 0" case should only skip the preallocate_pmds() calls and not the pgd_prepopulate_pmd() calls too?
Quoted 3 lines
Otherwise, they'll likely each need the same check that was added to
pgd_prepopulate_pmd()ages ago for a similar situation...
uhm... that doesn't seem to have an impact nowadays, or at least now Wstringop-overflow sees the problem first, because now the issue is detected at the moment of passing the arguments to the the function and not when actually executing the function?
otherwise, I think we wouldn't see this error:
arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]
454 | pgd_prepopulate_pmd(mm, pgd, pmds);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arch/x86/mm/pgtable.c:454:9: note: referencing argument 3 of type 'pmd_t *[0]'
arch/x86/mm/pgtable.c:296:13: note: in a call to function 'pgd_prepopulate_pmd'
296 | static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[])
| ^~~~~~~~~~~~~~~~~~~Thanks
--
Gustavo
Quoted 65 lines · Gustavo A. R. Silva
Quoted 56 lines
Quoted 56 lines
Quoted 17 lines
Quoted 17 lines
arch/x86/mm/pgtable.c +5
-4@@ -434,14 +434,18 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds;Quoted 9 lines
arch/x86/mm/pgtable.c pgd_t *pgd_alloc(struct mm_struct *mm) · line 447
Quoted 6 lines
arch/x86/mm/pgtable.c +4
-2-if (paravirt_pgd_alloc (mm) !=0 ) - goto out_free_user_pmds; +if (paravirt_pgd_alloc (mm) !=0 ) + goto out_free_user_pmds; + }else { + goto out_free_pgd;The "all 0" case shouldn't be a failure mode; it should just skip the
preallocate_pmds()calls.Do you mean something like this:
arch/x86/mm/pgtable.c +6
-4@@ -434,11 +434,13 @@ pgd_t *pgd_alloc(struct mm_struct *mm) mm->pgd = pgd; -if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) - goto out_free_pgd; +if (MAX_PREALLOCATED_PMDS !=0 &&MAX_PREALLOCATED_USER_PMDS !=0 ) { +if (preallocate_pmds (mm, pmds,PREALLOCATED_PMDS ) !=0 ) + goto out_free_pgd; -if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) - goto out_free_pmds; +if (preallocate_pmds (mm, u_pmds,PREALLOCATED_USER_PMDS ) !=0 ) + goto out_free_pmds; + }if (paravirt_pgd_alloc (mm) !=0 ) goto out_free_user_pmds;It seems that the above is not enough, because we have the same issue when calling
pgd_prepopulate_pmd(),pgd_prepopulate_user_pmd()andfree_pmds():Quoted 8 lines
Quoted 5 lines
CC
arch/x86/mm/pgtable.oarch/x86/mm/pgtable.c: In function 'pgd_alloc':arch/x86/mm/pgtable.c:464:9: warning: 'free_pmds' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]464 | free_pmds(mm, u_pmds, PREALLOCATED_USER_PMDS); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Ugh. Perhaps just marking both
preallocate_pmds()andfree_pmds()as inline is enough to let the compiler "see" everything correctly?
Quoted 3 lines
It doesn't seem to work... however, the following piece of code implies that pmds and
u_pmdsshould be first preallocated throughpreallocate_pmds(), which cannot happen if (MAX_PREALLOCATED_PMDS!= 0 &&MAX_PREALLOCATED_USER_PMDS!= 0)
I wanted to say: which cannot happen if MAX_PREALLOCATED_PMDS == 0 && MAX_PREALLOCATED_USER_PMDS == 0
Quoted 30 lines
448 /*
449 * Make sure that pre-populating the pmds is atomic with 450 * respect to anything walking the pgd_list, so that they 451 * never see a partially populated pgd. 452 */ 453 spin_lock(&pgd_lock); 454 455 pgd_ctor(mm, pgd); 456 pgd_prepopulate_pmd(mm, pgd, pmds); 457 pgd_prepopulate_user_pmd(mm, pgd, u_pmds);458
459spin_unlock(&pgd_lock);
460
461 return pgd;So, my question here is why do you think the "all 0" case should only skip the
preallocate_pmds()calls and not thepgd_prepopulate_pmd()calls too?Quoted 3 lines
Otherwise, they'll likely each need the same check that was added to
pgd_prepopulate_pmd()ages ago for a similar situation...uhm... that doesn't seem to have an impact nowadays, or at least now Wstringop-overflow sees the problem first, because now the issue is detected at the moment of passing the arguments to the the function and not when actually executing the function?
otherwise, I think we wouldn't see this error:
Quoted 8 lines
arch/x86/mm/pgtable.c:454:9: warning: 'pgd_prepopulate_pmd' accessing 8 bytes in a region of size 0 [-Wstringop-overflow=]454 | pgd_prepopulate_pmd(mm, pgd, pmds); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arch/x86/mm/pgtable.c:454:9: note: referencing argument 3 of type 'pmd_t *[0]' arch/x86/mm/pgtable.c:296:13: note: in a call to function 'pgd_prepopulate_pmd' 296 | static void pgd_prepopulate_pmd(struct mm_struct *mm, pgd_t *pgd, pmd_t *pmds[]) | ^~~~~~~~~~~~~~~~~~~
Thanks
--
Gustavo
Quoted 3 lines · Gustavo A. R. Silva
It doesn't seem to work... however, the following piece of code implies that pmds and
u_pmdsshould be first preallocated throughpreallocate_pmds(), which cannot happen if (MAX_PREALLOCATED_PMDS!= 0 &&MAX_PREALLOCATED_USER_PMDS!= 0)
This works, weirdly:
arch/x86/mm/pgtable.c +2 -2
@@ -424,8 +424,8 @@ static inline void _pgd_free(pgd_t *pgd)pgd_t *pgd_alloc (struct mm_struct *mm) {pgd_t *pgd; -pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS ]; -pmd_t *pmds[MAX_PREALLOCATED_PMDS ]; +pmd_t *u_pmds[MAX_PREALLOCATED_USER_PMDS +1 ]; +pmd_t *pmds[MAX_PREALLOCATED_PMDS +1 ]; pgd =_pgd_alloc ();
Signature · 1 line
Kees Cook