-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/net: Add sk_bind_sendto_listen and sk_connect_zero_addr
This patch adds 2 new tests: sk_bind_sendto_listen and sk_connect_zero_addr. The sk_bind_sendto_listen test exercises the path where a socket's rcv saddr changes after it has been added to the binding tables, and then a listen() on the socket is invoked. The listen() should succeed. The sk_bind_sendto_listen test is copied over from one of syzbot's tests: https://syzkaller.appspot.com/x/repro.c?x=1673a38df00000 The sk_connect_zero_addr test exercises the path where the socket was never previously added to the binding tables and it gets assigned a saddr upon a connect() to address 0. Signed-off-by: Joanne Koong <joannelkoong@gmail.com> Signed-off-by: Jakub Kicinski <kuba@kernel.org>
- Loading branch information
Joanne Koong
authored and
Jakub Kicinski
committed
Aug 25, 2022
1 parent
c35ecb9
commit 1be9ac8
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ cmsg_sender | |
unix_connect | ||
tap | ||
bind_bhash | ||
sk_bind_sendto_listen | ||
sk_connect_zero_addr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include <arpa/inet.h> | ||
#include <error.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
|
||
int main(void) | ||
{ | ||
int fd1, fd2, one = 1; | ||
struct sockaddr_in6 bind_addr = { | ||
.sin6_family = AF_INET6, | ||
.sin6_port = htons(20000), | ||
.sin6_flowinfo = htonl(0), | ||
.sin6_addr = {}, | ||
.sin6_scope_id = 0, | ||
}; | ||
|
||
inet_pton(AF_INET6, "::", &bind_addr.sin6_addr); | ||
|
||
fd1 = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP); | ||
if (fd1 < 0) { | ||
error(1, errno, "socket fd1"); | ||
return -1; | ||
} | ||
|
||
if (setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) { | ||
error(1, errno, "setsockopt(SO_REUSEADDR) fd1"); | ||
goto out_err1; | ||
} | ||
|
||
if (bind(fd1, (struct sockaddr *)&bind_addr, sizeof(bind_addr))) { | ||
error(1, errno, "bind fd1"); | ||
goto out_err1; | ||
} | ||
|
||
if (sendto(fd1, NULL, 0, MSG_FASTOPEN, (struct sockaddr *)&bind_addr, | ||
sizeof(bind_addr))) { | ||
error(1, errno, "sendto fd1"); | ||
goto out_err1; | ||
} | ||
|
||
fd2 = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP); | ||
if (fd2 < 0) { | ||
error(1, errno, "socket fd2"); | ||
goto out_err1; | ||
} | ||
|
||
if (setsockopt(fd2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) { | ||
error(1, errno, "setsockopt(SO_REUSEADDR) fd2"); | ||
goto out_err2; | ||
} | ||
|
||
if (bind(fd2, (struct sockaddr *)&bind_addr, sizeof(bind_addr))) { | ||
error(1, errno, "bind fd2"); | ||
goto out_err2; | ||
} | ||
|
||
if (sendto(fd2, NULL, 0, MSG_FASTOPEN, (struct sockaddr *)&bind_addr, | ||
sizeof(bind_addr)) != -1) { | ||
error(1, errno, "sendto fd2"); | ||
goto out_err2; | ||
} | ||
|
||
if (listen(fd2, 0)) { | ||
error(1, errno, "listen"); | ||
goto out_err2; | ||
} | ||
|
||
close(fd2); | ||
close(fd1); | ||
return 0; | ||
|
||
out_err2: | ||
close(fd2); | ||
|
||
out_err1: | ||
close(fd1); | ||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#include <arpa/inet.h> | ||
#include <error.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
|
||
int main(void) | ||
{ | ||
int fd1, fd2, one = 1; | ||
struct sockaddr_in6 bind_addr = { | ||
.sin6_family = AF_INET6, | ||
.sin6_port = htons(20000), | ||
.sin6_flowinfo = htonl(0), | ||
.sin6_addr = {}, | ||
.sin6_scope_id = 0, | ||
}; | ||
|
||
inet_pton(AF_INET6, "::", &bind_addr.sin6_addr); | ||
|
||
fd1 = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP); | ||
if (fd1 < 0) { | ||
error(1, errno, "socket fd1"); | ||
return -1; | ||
} | ||
|
||
if (setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) { | ||
error(1, errno, "setsockopt(SO_REUSEADDR) fd1"); | ||
goto out_err1; | ||
} | ||
|
||
if (bind(fd1, (struct sockaddr *)&bind_addr, sizeof(bind_addr))) { | ||
error(1, errno, "bind fd1"); | ||
goto out_err1; | ||
} | ||
|
||
if (listen(fd1, 0)) { | ||
error(1, errno, "listen"); | ||
goto out_err1; | ||
} | ||
|
||
fd2 = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP); | ||
if (fd2 < 0) { | ||
error(1, errno, "socket fd2"); | ||
goto out_err1; | ||
} | ||
|
||
if (connect(fd2, (struct sockaddr *)&bind_addr, sizeof(bind_addr))) { | ||
error(1, errno, "bind fd2"); | ||
goto out_err2; | ||
} | ||
|
||
close(fd2); | ||
close(fd1); | ||
return 0; | ||
|
||
out_err2: | ||
close(fd2); | ||
out_err1: | ||
close(fd1); | ||
return -1; | ||
} |