diff mbox series

[v2] syscalls/sockioctl: Make buf a struct ifreq array

Message ID 20230327110234.266785-1-teo.coupriediaz@arm.com
State Accepted
Headers show
Series [v2] syscalls/sockioctl: Make buf a struct ifreq array | expand

Commit Message

Teo Couprie Diaz March 27, 2023, 11:02 a.m. UTC
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, a char array, 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.

Change buf from a char to a struct ifreq array, as it isn't used for
anything else in this test.
This guarantees that buff will be properly aligned.

Signed-off-by: Teo Couprie Diaz <teo.coupriediaz@arm.com>
I changed the cast from Cyril suggestion from (void*) to (char*) just
to be consistent with the type of (struct ifconf).ifc_buf.
From my understanding this should be equivalent.

  - As per Cyril comments, make buf a struct ifreq array rather than
    align it with __attribute__
  - Update commit message accordingly

CI Build: https://github.com/Teo-CD/ltp/actions/runs/4531482995

 testcases/kernel/syscalls/sockioctl/sockioctl01.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)


Li Wang March 28, 2023, 2:50 a.m. UTC | #1

Reviewed-by: Li Wang <liwang@redhat.com>
Petr Vorel May 5, 2023, 4:57 p.m. UTC | #2
Hi all,

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Cyril Hrubis May 12, 2023, 11:48 a.m. UTC | #3
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Petr Vorel May 12, 2023, 12:25 p.m. UTC | #4
Hi all,

thanks, merged!

Kind regards,
diff mbox series


diff --git a/testcases/kernel/syscalls/sockioctl/sockioctl01.c b/testcases/kernel/syscalls/sockioctl/sockioctl01.c
index 51dac9c16309..ff3738f327ef 100644
--- a/testcases/kernel/syscalls/sockioctl/sockioctl01.c
+++ b/testcases/kernel/syscalls/sockioctl/sockioctl01.c
@@ -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,
 	ifc.ifc_len = sizeof(buf);
-	ifc.ifc_buf = buf;
+	ifc.ifc_buf = (char *)buf;
 static void setup3(void)