Skip to contentSkip to conversation

samples/seccomp: eliminate two compile warnings in user-trap.c

PATCH In review Zhen Lei 2 participants 5 messages Sep 1, 2020 – Sep 9, 2020

lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

v2 cover samples/seccomp: eliminate two compile warnings in user-trap.c

v1 --> v2:
1. a character 'g' is missed in "userccflags += -fno-strict-aliasing".
2. delete the word "boring" in the subject.

Zhen Lei (1):
samples/seccomp: eliminate two compile warnings in user-trap.c

 samples/seccomp/user-trap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
1.8.3
author
#1 lore raw

PATCH v2 1/1 samples/seccomp: eliminate two compile warnings in user-trap.c

samples/seccomp/user-trap.c is compiled with $(userccflags), and the latter does not contain -fno-strict-aliasing, so the warnings reported as below. Due to add "userccflags += -fno-strict-aliasing" will impact other files, so use __attribute__((__may_alias__)) to suppress it exactly.

My gcc version is 5.5.0 20171010.

----------
samples/seccomp/user-trap.c: In function ‘send_fd’:
samples/seccomp/user-trap.c:50:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((int *)CMSG_DATA(cmsg)) = fd;
^
samples/seccomp/user-trap.c: In function ‘recv_fd’:
samples/seccomp/user-trap.c:83:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
return *((int *)CMSG_DATA(cmsg));
^

---
 samples/seccomp/user-trap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
samples/seccomp/user-trap.c +4 -2
index 20291ec6489f31e..e36696b7f41517f 100644
@@ -23,6 +23,8 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
 
+typedef int __attribute__((__may_alias__)) __int_alias_t;
+
 static int seccomp(unsigned int op, unsigned int flags, void *args)
 {
 	errno = 0;
@@ -47,7 +49,7 @@ static int send_fd(int sock, int fd)
 	cmsg->cmsg_level = SOL_SOCKET;
 	cmsg->cmsg_type = SCM_RIGHTS;
 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
-	*((int *)CMSG_DATA(cmsg)) = fd;
+	*(__int_alias_t *)CMSG_DATA(cmsg) = fd;
 	msg.msg_controllen = cmsg->cmsg_len;
 
 	if (sendmsg(sock, &msg, 0) < 0) {
@@ -80,7 +82,7 @@ static int recv_fd(int sock)
 
 	cmsg = CMSG_FIRSTHDR(&msg);
 
-	return *((int *)CMSG_DATA(cmsg));
+	return *(__int_alias_t *)CMSG_DATA(cmsg);
 }
 
 static int user_trap_syscall(int nr, unsigned int flags)
Signature · 1 line
1.8.3
#2 lore raw

Doesn't anyone care about this? Or is it that everyone hasn't encountered this problem? Why do these two warnings occur every time I compiled?

Quoted 54 lines · Zhen Lei

samples/seccomp/user-trap.c is compiled with $(userccflags), and the latter does not contain -fno-strict-aliasing, so the warnings reported as below. Due to add "userccflags += -fno-strict-aliasing" will impact other files, so use __attribute__((__may_alias__)) to suppress it exactly.

My gcc version is 5.5.0 20171010.

----------
samples/seccomp/user-trap.c: In function ‘send_fd’:
samples/seccomp/user-trap.c:50:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((int *)CMSG_DATA(cmsg)) = fd;
^
samples/seccomp/user-trap.c: In function ‘recv_fd’:
samples/seccomp/user-trap.c:83:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
return *((int *)CMSG_DATA(cmsg));
^

---
 samples/seccomp/user-trap.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
samples/seccomp/user-trap.c +4 -2
index 20291ec6489f31e..e36696b7f41517f 100644
@@ -23,6 +23,8 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
 
+typedef int __attribute__((__may_alias__)) __int_alias_t;
+
 static int seccomp(unsigned int op, unsigned int flags, void *args)
 {
 	errno = 0;
@@ -47,7 +49,7 @@ static int send_fd(int sock, int fd)
 	cmsg->cmsg_level = SOL_SOCKET;
 	cmsg->cmsg_type = SCM_RIGHTS;
 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
-	*((int *)CMSG_DATA(cmsg)) = fd;
+	*(__int_alias_t *)CMSG_DATA(cmsg) = fd;
 	msg.msg_controllen = cmsg->cmsg_len;
 
 	if (sendmsg(sock, &msg, 0) < 0) {
@@ -80,7 +82,7 @@ static int recv_fd(int sock)
 
 	cmsg = CMSG_FIRSTHDR(&msg);
 
-	return *((int *)CMSG_DATA(cmsg));
+	return *(__int_alias_t *)CMSG_DATA(cmsg);
 }
 
 static int user_trap_syscall(int nr, unsigned int flags)
#3 lore raw
Quoted 18 lines · Leizhen (ThunderTown)
Quoted 7 lines · Zhen Lei

samples/seccomp/user-trap.c is compiled with $(userccflags), and the latter does not contain -fno-strict-aliasing, so the warnings reported as below. Due to add "userccflags += -fno-strict-aliasing" will impact other files, so use __attribute__((__may_alias__)) to suppress it exactly.

My gcc version is 5.5.0 20171010.

Quoted 9 lines

----------
samples/seccomp/user-trap.c: In function ‘send_fd’:
samples/seccomp/user-trap.c:50:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((int *)CMSG_DATA(cmsg)) = fd;
^
samples/seccomp/user-trap.c: In function ‘recv_fd’:
samples/seccomp/user-trap.c:83:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
return *((int *)CMSG_DATA(cmsg));
^

Quoted 2 lines

Doesn't anyone care about this? Or is it that everyone hasn't encountered this problem? Why do these two warnings occur every time I compiled?

Hi!

I think the samples have been a bit ignored lately because they have a lot of weird build issues with regard to native vs compat and needing the kernel headers to be built first, etc.

That said, yes, I'd like to fix warnings. However, I can't reproduce this. How are you building? I tried x86_64 and cross-compiled to i386.

Signature · 1 line
Kees Cook
#4 lore raw
Quoted 22 lines · Kees Cook
Quoted 18 lines · Leizhen (ThunderTown)
Quoted 7 lines · Zhen Lei

samples/seccomp/user-trap.c is compiled with $(userccflags), and the latter does not contain -fno-strict-aliasing, so the warnings reported as below. Due to add "userccflags += -fno-strict-aliasing" will impact other files, so use __attribute__((__may_alias__)) to suppress it exactly.

My gcc version is 5.5.0 20171010.

Quoted 9 lines

----------
samples/seccomp/user-trap.c: In function ‘send_fd’:
samples/seccomp/user-trap.c:50:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
*((int *)CMSG_DATA(cmsg)) = fd;
^
samples/seccomp/user-trap.c: In function ‘recv_fd’:
samples/seccomp/user-trap.c:83:2: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
return *((int *)CMSG_DATA(cmsg));
^

Quoted 2 lines

Doesn't anyone care about this? Or is it that everyone hasn't encountered this problem? Why do these two warnings occur every time I compiled?

Quoted 8 lines

Hi!

I think the samples have been a bit ignored lately because they have a lot of weird build issues with regard to native vs compat and needing the kernel headers to be built first, etc.

That said, yes, I'd like to fix warnings. However, I can't reproduce this. How are you building? I tried x86_64 and cross-compiled to i386.

I can reproduce it both on X86 and ARM64.

On X86:
make distclean allmodconfig
make -j64 2>err.txt
vi err.txt

$ arch
x86_64
$ ls -l samples/seccomp/user-trap
user-trap    user-trap.c
$ gcc -v
gcc version 5.5.0 20171010 (Ubuntu 5.5.0-12ubuntu5~16.04)

On ARM64:
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- distclean allmodconfig make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j64 2>err.tx vi err.txt

$ ls -l samples/seccomp/user-trap
user-trap    user-trap.c
$ aarch64-linux-gnu-gcc -v
gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.9)
Quoted 1 line