Skip to content

Commit d660fba

Browse files
shailend-ggvisor-bot
authored andcommitted
Deny SOL_IP socket options for raw AF_INET6 sockets
PiperOrigin-RevId: 813907049
1 parent 031e72d commit d660fba

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

pkg/sentry/socket/netstack/netstack.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,11 @@ func getSockOptIP(t *kernel.Task, s socket.Socket, ep commonEndpoint, name int,
16451645
log.Warningf("SOL_IP options not supported on endpoints other than tcpip.Endpoint: option = %d, endpoint = %T", name, ep)
16461646
return nil, syserr.ErrUnknownProtocolOption
16471647
}
1648+
// Rejection of SOL_IP options for AF_INET6 RAW sockets matches Linux behavior:
1649+
// https://github.com/torvalds/linux/blob/cec1e6e5d1a/net/ipv6/ipv6_sockglue.c#L1453
1650+
if family, skType, _ := s.Type(); family == linux.AF_INET6 && skType == linux.SOCK_RAW {
1651+
return nil, syserr.ErrUnknownProtocolOption
1652+
}
16481653

16491654
switch name {
16501655
case linux.IP_TTL:
@@ -2717,6 +2722,11 @@ func setSockOptIP(t *kernel.Task, s socket.Socket, ep commonEndpoint, name int,
27172722
log.Warningf("SOL_IP options not supported on endpoints other than tcpip.Endpoint: option = %d, endpoint = %T", name, ep)
27182723
return syserr.ErrUnknownProtocolOption
27192724
}
2725+
// Rejection of SOL_IP options for AF_INET6 RAW sockets matches Linux behavior:
2726+
// https://github.com/torvalds/linux/blob/cec1e6e5d1a/net/ipv6/ipv6_sockglue.c#L963
2727+
if family, skType, _ := s.Type(); family == linux.AF_INET6 && skType == linux.SOCK_RAW {
2728+
return syserr.ErrUnknownProtocolOption
2729+
}
27202730

27212731
switch name {
27222732
case linux.IP_MULTICAST_TTL:

test/syscalls/linux/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,7 @@ cc_binary(
21402140
":unix_domain_socket_test_util",
21412141
"//test/util:capability_util",
21422142
"//test/util:file_descriptor",
2143+
"//test/util:posix_error",
21432144
"//test/util:socket_util",
21442145
"//test/util:test_main",
21452146
"//test/util:test_util",

test/syscalls/linux/raw_socket.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
#include <sys/types.h>
2424
#include <unistd.h>
2525

26+
#include <cerrno>
27+
2628
#include "gmock/gmock.h"
2729
#include "gtest/gtest.h"
2830
#include "test/syscalls/linux/ip_socket_test_util.h"
2931
#include "test/syscalls/linux/unix_domain_socket_test_util.h"
3032
#include "test/util/capability_util.h"
3133
#include "test/util/file_descriptor.h"
34+
#include "test/util/posix_error.h"
3235
#include "test/util/socket_util.h"
3336
#include "test/util/test_util.h"
3437

@@ -1586,6 +1589,39 @@ TEST(RawSocketTest, IPv6Checksum_ValidateAndCalculate) {
15861589
ASSERT_NO_FATAL_FAILURE(expect_receive(checksum_not_set, counter, true));
15871590
}
15881591

1592+
// SOL_IP options on a raw AF_INET6 socket are denied.
1593+
TEST(RawSocketTest, SolIPSetSockOptOnRawV6SocketFails) {
1594+
SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveRawIPSocketCapability()));
1595+
FileDescriptor raw =
1596+
ASSERT_NO_ERRNO_AND_VALUE(Socket(AF_INET6, SOCK_RAW, IPPROTO_UDP));
1597+
1598+
const ip_mreqn group = {
1599+
.imr_multiaddr =
1600+
{
1601+
.s_addr = inet_addr(kMulticastAddress),
1602+
},
1603+
.imr_ifindex = ASSERT_NO_ERRNO_AND_VALUE(InterfaceIndex("lo")),
1604+
};
1605+
EXPECT_THAT(
1606+
setsockopt(raw.get(), SOL_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group)),
1607+
SyscallFailsWithErrno(ENOPROTOOPT));
1608+
1609+
// Try with another SOL_IP option.
1610+
const ip_mreqn iface = {
1611+
.imr_multiaddr = {},
1612+
.imr_ifindex = ASSERT_NO_ERRNO_AND_VALUE(InterfaceIndex("lo")),
1613+
};
1614+
EXPECT_THAT(
1615+
setsockopt(raw.get(), SOL_IP, IP_MULTICAST_IF, &iface, sizeof(iface)),
1616+
SyscallFailsWithErrno(ENOPROTOOPT));
1617+
1618+
// getsockopt is also denied.
1619+
int on;
1620+
socklen_t slen = sizeof(on);
1621+
EXPECT_THAT(getsockopt(raw.get(), SOL_IP, IP_HDRINCL, &on, &slen),
1622+
SyscallFailsWithErrno(ENOPROTOOPT));
1623+
}
1624+
15891625
} // namespace
15901626

15911627
} // namespace testing

0 commit comments

Comments
 (0)