syscalls/sockioctl: Align buffer to struct ifreq
PATCH 1/1 syscalls/sockioctl: Align buffer to struct ifreq
In setup3, the following line can lead to an undefined behavior:
ifr = *(struct ifreq *)ifc.ifc_buf;
Indeed, at this point it can be assumed that ifc.ifc_buf is suitably aligned for struct ifreq.
However, ifc.ifc_buf is assigned to buf which has no alignment constraints. This means there exists cases where buf is not suitably aligned to load a struct ifreq, which can generate a SIGBUS.
Force the alignment of buf to that of struct ifreq.
---
CI Build: https://github.com/Teo-CD/ltp/actions/runs/4502204132
testcases/kernel/syscalls/sockioctl/sockioctl01.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/sockioctl/sockioctl01.c +7 -1
@@ -52,7 +52,13 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +/* +* buf has no alignment constraints by default. However, it is used to load +* a struct ifreq in setup3, which requires it to have an appropriate alignment +* to prevent a possible undefined behavior. +*/ +static char buf[8192] + __attribute__((aligned(__alignof__(struct ifreq))));static void setup (void );static void setup0 (void );
Signature · 5 lines
2.34.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 27 lines
In setup3, the following line can lead to an undefined behavior:
ifr = *(struct ifreq *)ifc.ifc_buf;Indeed, at this point it can be assumed that ifc.ifc_buf is suitably aligned for struct ifreq.
However, ifc.ifc_buf is assigned to buf which has no alignment constraints. This means there exists cases where buf is not suitably aligned to load a struct ifreq, which can generate a SIGBUS.Force the alignment of buf to that of struct ifreq.
Signed-off-by Teo Couprie Diaz <[email protected]>---
CI Build: https://github.com/Teo-CD/ltp/actions/runs/4502204132testcases/kernel/syscalls/sockioctl/sockioctl01.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)testcases/kernel/syscalls/sockioctl/sockioctl01.c +0
-1@@ -52,7 +52,13 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ];
testcases/kernel/syscalls/sockioctl/sockioctl01.c static struct ifreq ifr; · line 64
Quoted 10 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +7
-0+/* +* buf has no alignment constraints by default. However, it is used to load +* a struct ifreq in setup3, which requires it to have an appropriate alignment +* to prevent a possible undefined behavior. +*/ +static char buf[8192] + __attribute__((aligned(__alignof__(struct ifreq))));static void setup (void );static void setup0 (void );
Looking at the code, shouldn't we rather than that declare the buffer as an struct ifreq array, that would naturally align the buffer without any need for tricky __attribute__.
testcases/kernel/syscalls/sockioctl/sockioctl01.c +2 -2
@@ -52,7 +52,7 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +static struct ifreq buf[200 ];static void setup (void );static void setup0 (void );
@@ -218,7 +218,7 @@ static void setup2(void) s =SAFE_SOCKET (cleanup, tdat[testno].domain, tdat[testno].type, tdat[testno].proto); ifc.ifc_len =sizeof (buf); - ifc.ifc_buf = buf; + ifc.ifc_buf = (void *)buf; }
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril,
Quoted 36 lines · Cyril Hrubis
Hi!
Quoted 35 lines
In setup3, the following line can lead to an undefined behavior:
ifr = *(struct ifreq *)ifc.ifc_buf;Indeed, at this point it can be assumed that ifc.ifc_buf is suitably aligned for struct ifreq.
However, ifc.ifc_buf is assigned to buf which has no alignment constraints. This means there exists cases where buf is not suitably aligned to load a struct ifreq, which can generate a SIGBUS.Force the alignment of buf to that of struct ifreq.
Signed-off-by Teo Couprie Diaz <[email protected]>---
CI Build: https://github.com/Teo-CD/ltp/actions/runs/4502204132testcases/kernel/syscalls/sockioctl/sockioctl01.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)testcases/kernel/syscalls/sockioctl/sockioctl01.c +7
-1@@ -52,7 +52,13 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +/* +* buf has no alignment constraints by default. However, it is used to load +* a struct ifreq in setup3, which requires it to have an appropriate alignment +* to prevent a possible undefined behavior. +*/ +static char buf[8192] + __attribute__((aligned(__alignof__(struct ifreq))));
Quoted 5 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c static struct ifreq ifr; · line 58
Quoted 2 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +0
-0static void setup (void );static void setup0 (void );Looking at the code, shouldn't we rather than that declare the buffer as an struct ifreq array, that would naturally align the buffer without any need for tricky
__attribute__.
__attribute__+__alignof__ is quite unwieldy indeed. I kept the char* to match the struct definition, but it's really just to represent a pointer to something. It's not used as anything else in the test anyway.
If you feel there's no good reason to keep the char* buff and cast to void* for the syscall, I agree that it would be better. I tested on our system which generated the fault initially and it works fine as expected.
What would be the procedure in this case ? Shall I resend the patch with your changes ? Would you just apply it or send it yourself ? Happy to follow up however is best.
Thanks for taking the time to look into it,
Best regards
Téo
Quoted 23 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +2
-2@@ -52,7 +52,7 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +static struct ifreq buf[200 ];static void setup (void );static void setup0 (void );@@ -218,7 +218,7 @@ static void setup2(void) s =SAFE_SOCKET (cleanup, tdat[testno].domain, tdat[testno].type, tdat[testno].proto); ifc.ifc_len =sizeof (buf); - ifc.ifc_buf = buf; + ifc.ifc_buf = (void *)buf; }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Teo,
Quoted 50 lines · Teo Couprie Diaz
Hi Cyril,
Quoted 36 lines · Cyril Hrubis
Hi!
Quoted 35 lines
In setup3, the following line can lead to an undefined behavior:
ifr = *(struct ifreq *)ifc.ifc_buf;Indeed, at this point it can be assumed that ifc.ifc_buf is suitably aligned for struct ifreq.
However, ifc.ifc_buf is assigned to buf which has no alignment constraints. This means there exists cases where buf is not suitably aligned to load a struct ifreq, which can generate a SIGBUS.Force the alignment of buf to that of struct ifreq.
Signed-off-by Teo Couprie Diaz <[email protected]>---
CI Build: https://github.com/Teo-CD/ltp/actions/runs/4502204132testcases/kernel/syscalls/sockioctl/sockioctl01.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)testcases/kernel/syscalls/sockioctl/sockioctl01.c +7
-1@@ -52,7 +52,13 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +/* +* buf has no alignment constraints by default. However, it is used to load +* a struct ifreq in setup3, which requires it to have an appropriate alignment +* to prevent a possible undefined behavior. +*/ +static char buf[8192] + __attribute__((aligned(__alignof__(struct ifreq))));Quoted 5 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c static struct ifreq ifr; · line 58
Quoted 2 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +0
-0static void setup (void );static void setup0 (void );Looking at the code, shouldn't we rather than that declare the buffer as an struct ifreq array, that would naturally align the buffer without any need for tricky
__attribute__.__attribute__+__alignof__ is quite unwieldy indeed. I kept the char* to match the struct definition,
but it's really just to represent a pointer to something. It's not used as anything else in the test anyway.
Quoted 8 lines
If you feel there's no good reason to keep the char* buff and cast to void* for the syscall,
I agree that it would be better. I tested on our system which generated the fault initially
and it works fine as expected.What would be the procedure in this case ? Shall I resend the patch with your changes ?
Yes, you need to send a patch v2 with Cyril's suggestion.
Quoted 32 lines
Would you just apply it or send it yourself ? Happy to follow up however is best.
Thanks for taking the time to look into it,
Best regards
TéoQuoted 15 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +1
-1@@ -52,7 +52,7 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +static struct ifreq buf[200 ];static void setup (void );static void setup0 (void );testcases/kernel/syscalls/sockioctl/sockioctl01.c static void setup2(void) · line 224
Quoted 8 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +1
-1@@ -218,7 +218,7 @@ static void setup2(void) s =SAFE_SOCKET (cleanup, tdat[testno].domain, tdat[testno].type, tdat[testno].proto); ifc.ifc_len =sizeof (buf); - ifc.ifc_buf = buf; + ifc.ifc_buf = (void *)buf; }--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Li,
Quoted 59 lines · Li Wang
Hi Teo,
Quoted 55 lines · Teo Couprie Diaz
Hi Cyril,
Quoted 36 lines · Cyril Hrubis
Hi!
Quoted 35 lines
In setup3, the following line can lead to an undefined behavior:
ifr = *(struct ifreq *)ifc.ifc_buf;Indeed, at this point it can be assumed that ifc.ifc_buf is suitably aligned for struct ifreq.
However, ifc.ifc_buf is assigned to buf which has no alignment constraints. This means there exists cases where buf is not suitably aligned to load a struct ifreq, which can generate a SIGBUS.Force the alignment of buf to that of struct ifreq.
Signed-off-by Teo Couprie Diaz <[email protected]>---
CI Build: https://github.com/Teo-CD/ltp/actions/runs/4502204132testcases/kernel/syscalls/sockioctl/sockioctl01.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)testcases/kernel/syscalls/sockioctl/sockioctl01.c +7
-1@@ -52,7 +52,13 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +/* +* buf has no alignment constraints by default. However, it is used to load +* a struct ifreq in setup3, which requires it to have an appropriate alignment +* to prevent a possible undefined behavior. +*/ +static char buf[8192] + __attribute__((aligned(__alignof__(struct ifreq))));Quoted 5 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c static struct ifreq ifr; · line 58
Quoted 2 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +0
-0static void setup (void );static void setup0 (void );Looking at the code, shouldn't we rather than that declare the buffer as an struct ifreq array, that would naturally align the buffer without any need for tricky
__attribute__.__attribute__+__alignof__ is quite unwieldy indeed. I kept the char* to match the struct definition,
but it's really just to represent a pointer to something. It's not used as anything else in the test anyway.If you feel there's no good reason to keep the char* buff and cast to void* for the syscall,
I agree that it would be better. I tested on our system which generated the fault initially
and it works fine as expected.
Quoted 3 lines
Quoted 2 lines
What would be the procedure in this case ? Shall I resend the patch with your changes ?
Yes, you need to send a patch v2 with Cyril's suggestion.
Thanks for clarifying Li, then I will simply send out the v2 with Cyril's changes and updated commit message.
Best regards,
Téo
Quoted 33 lines
Quoted 31 lines
Would you just apply it or send it yourself ? Happy to follow up however is best.
Thanks for taking the time to look into it,
Best regards
TéoQuoted 14 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +1
-1@@ -52,7 +52,7 @@ static struct ifreq ifr;static int sinlen;static int optval; -static char buf[8192 ]; +static struct ifreq buf[200 ];static void setup (void );static void setup0 (void );testcases/kernel/syscalls/sockioctl/sockioctl01.c static void setup2(void) · line 224
Quoted 8 lines
testcases/kernel/syscalls/sockioctl/sockioctl01.c +1
-1@@ -218,7 +218,7 @@ static void setup2(void) s =SAFE_SOCKET (cleanup, tdat[testno].domain, tdat[testno].type, tdat[testno].proto); ifc.ifc_len =sizeof (buf); - ifc.ifc_buf = buf; + ifc.ifc_buf = (void *)buf; }--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp