In setup3, the following line introduces an undefined behavior in purecap: ifr = *(struct ifreq *)ifc.ifc_buf;
Indeed, at this point it can be assumed that ifc.ifc_buf is suitably aligned for struct ifreq, which is 16 in purecap as it stores a capability. 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 will produce a segmentation fault.
Force the alignment of buf to 16 bytes in purecap.
Signed-off-by: Teo Couprie Diaz teo.coupriediaz@arm.com --- testcases/kernel/syscalls/sockioctl/sockioctl01.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/testcases/kernel/syscalls/sockioctl/sockioctl01.c b/testcases/kernel/syscalls/sockioctl/sockioctl01.c index 486236af9d6b..22e96b42ca0c 100644 --- a/testcases/kernel/syscalls/sockioctl/sockioctl01.c +++ b/testcases/kernel/syscalls/sockioctl/sockioctl01.c @@ -42,6 +42,18 @@ #include "test.h" #include "safe_macros.h"
+/* + * Aligning to 16 bytes is required in purecap as the test loads a struct ifreq + * from buf, which is 16 bytes aligned and might be loaded via capability + * registers. Unsuitable alignment would be an undefined behavior and could lead + * to a segmentation fault. + */ +#ifdef __CHERI_PURE_CAPABILITY__ +#define BUF_ALIGN __attribute__((aligned(16))) +#else +#define BUF_ALIGN +#endif + char *TCID = "sockioctl01"; int testno;
@@ -52,7 +64,7 @@ static struct ifreq ifr; static int sinlen; static int optval;
-static char buf[8192]; +static BUF_ALIGN char buf[8192];
static void setup(void); static void setup0(void);