summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/socket/epsocket/epsocket.go
diff options
context:
space:
mode:
authorIan Gudger <igudger@google.com>2019-03-08 20:26:55 -0800
committerShentubot <shentubot@google.com>2019-03-08 20:27:58 -0800
commit281092e842445cfb9ff474aae81c169954b469cb (patch)
tree674d9225402b69cc3323494b16d393e88e756482 /pkg/sentry/socket/epsocket/epsocket.go
parent86036f979b34855f0c945056f908961ccb804c1e (diff)
Make IP_MULTICAST_LOOP and IP_MULTICAST_TTL allow setting int or char.
This is the correct Linux behavior, and at least PHP depends on it. PiperOrigin-RevId: 237565639 Change-Id: I931af09c8ed99a842cf70d22bfe0b65e330c4137
Diffstat (limited to 'pkg/sentry/socket/epsocket/epsocket.go')
-rw-r--r--pkg/sentry/socket/epsocket/epsocket.go34
1 files changed, 19 insertions, 15 deletions
diff --git a/pkg/sentry/socket/epsocket/epsocket.go b/pkg/sentry/socket/epsocket/epsocket.go
index f7636e056..6e95fd448 100644
--- a/pkg/sentry/socket/epsocket/epsocket.go
+++ b/pkg/sentry/socket/epsocket/epsocket.go
@@ -1193,24 +1193,30 @@ func copyInMulticastRequest(optVal []byte) (linux.InetMulticastRequestWithNIC, *
return req, nil
}
-// reduceToByte ORs all of the bytes in the input.
-func reduceToByte(buf []byte) byte {
- var out byte
- for _, b := range buf {
- out |= b
+// parseIntOrChar copies either a 32-bit int or an 8-bit uint out of buf.
+//
+// net/ipv4/ip_sockglue.c:do_ip_setsockopt does this for its socket options.
+func parseIntOrChar(buf []byte) (int32, *syserr.Error) {
+ if len(buf) == 0 {
+ return 0, syserr.ErrInvalidArgument
+ }
+
+ if len(buf) >= sizeOfInt32 {
+ return int32(usermem.ByteOrder.Uint32(buf)), nil
}
- return out
+
+ return int32(buf[0]), nil
}
// setSockOptIP implements SetSockOpt when level is SOL_IP.
func setSockOptIP(t *kernel.Task, ep commonEndpoint, name int, optVal []byte) *syserr.Error {
switch name {
case linux.IP_MULTICAST_TTL:
- if len(optVal) < sizeOfInt32 {
- return syserr.ErrInvalidArgument
+ v, err := parseIntOrChar(optVal)
+ if err != nil {
+ return err
}
- v := int32(usermem.ByteOrder.Uint32(optVal))
if v == -1 {
// Linux translates -1 to 1.
v = 1
@@ -1260,15 +1266,13 @@ func setSockOptIP(t *kernel.Task, ep commonEndpoint, name int, optVal []byte) *s
}))
case linux.IP_MULTICAST_LOOP:
- if len(optVal) < 1 {
- return syserr.ErrInvalidArgument
- }
- if len(optVal) > sizeOfInt32 {
- optVal = optVal[:sizeOfInt32]
+ v, err := parseIntOrChar(optVal)
+ if err != nil {
+ return err
}
return syserr.TranslateNetstackError(ep.SetSockOpt(
- tcpip.MulticastLoopOption(reduceToByte(optVal) != 0),
+ tcpip.MulticastLoopOption(v != 0),
))
case linux.MCAST_JOIN_GROUP: