All of lore.kernel.org
 help / color / mirror / Atom feed
From: Teo Couprie Diaz <teo.coupriediaz@arm.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2] syscalls/sockioctl: Make buf a struct ifreq array
Date: Mon, 27 Mar 2023 12:02:34 +0100	[thread overview]
Message-ID: <20230327110234.266785-1-teo.coupriediaz@arm.com> (raw)

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.

v2:
  - 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(-)

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,
 			tdat[testno].proto);
 	ifc.ifc_len = sizeof(buf);
-	ifc.ifc_buf = buf;
+	ifc.ifc_buf = (char *)buf;
 }
 
 static void setup3(void)
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

             reply	other threads:[~2023-03-27 11:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-27 11:02 Teo Couprie Diaz [this message]
2023-03-28  2:50 ` [LTP] [PATCH v2] syscalls/sockioctl: Make buf a struct ifreq array Li Wang
2023-05-05 16:57   ` Petr Vorel
2023-05-12 11:48 ` Cyril Hrubis
2023-05-12 12:25   ` Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230327110234.266785-1-teo.coupriediaz@arm.com \
    --to=teo.coupriediaz@arm.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.