Skip to content

Commit

Permalink
syscalls/sockioctl: Make buf a struct ifreq array
Browse files Browse the repository at this point in the history
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.

Reviewed-by: Li Wang <[email protected]>
Reviewed-by: Petr Vorel <[email protected]>
Reviewed-by: Cyril Hrubis <[email protected]>
Signed-off-by: Teo Couprie Diaz <[email protected]>
  • Loading branch information
Teo-CD authored and pevik committed May 12, 2023
1 parent 301022b commit ce48029
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions testcases/kernel/syscalls/sockioctl/sockioctl01.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ce48029

Please sign in to comment.