Fix panic caused by invalid address for Bind in packet sockets.

PiperOrigin-RevId: 298476533
This commit is contained in:
Nayana Bidari 2020-03-02 16:30:51 -08:00 committed by gVisor bot
parent d80b6a6e49
commit 43abb24657
2 changed files with 17 additions and 0 deletions

View File

@ -712,6 +712,10 @@ func (s *SocketOperations) Connect(t *kernel.Task, sockaddr []byte, blocking boo
// Bind implements the linux syscall bind(2) for sockets backed by
// tcpip.Endpoint.
func (s *SocketOperations) Bind(t *kernel.Task, sockaddr []byte) *syserr.Error {
if len(sockaddr) < 2 {
return syserr.ErrInvalidArgument
}
family := usermem.ByteOrder.Uint16(sockaddr)
var addr tcpip.FullAddress

View File

@ -417,6 +417,19 @@ TEST_P(CookedPacketTest, BindDrop) {
EXPECT_THAT(RetryEINTR(poll)(&pfd, 1, 1000), SyscallSucceedsWithValue(0));
}
// Bind with invalid address.
TEST_P(CookedPacketTest, BindFail) {
// Null address.
ASSERT_THAT(bind(socket_, nullptr, sizeof(struct sockaddr)),
SyscallFailsWithErrno(EFAULT));
// Address of size 1.
uint8_t addr = 0;
ASSERT_THAT(
bind(socket_, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)),
SyscallFailsWithErrno(EINVAL));
}
INSTANTIATE_TEST_SUITE_P(AllInetTests, CookedPacketTest,
::testing::Values(ETH_P_IP, ETH_P_ALL));