Skip to content

Commit c7ff727

Browse files
kolyshkingopherbot
authored andcommitted
unix: fix double copy in (*SockaddrALG).sockaddr
The code uses ByteSliceFromString first, which - checks that the string does not contain \0; - copies the string to a byte slice which ends with \0. Next, it does one more copy, to sa.raw fields. Double copying is not needed because: - the code already checks that there's an extra byte for \0 in the sa.raw field; - there is no need to check for \0 byte in the middle of the fields (those are hash and cipher names, and an unknown name will result in EINVAL from the kernel). While at it, remove the use of magic numbers. Change-Id: I0cf096fad6f974507d4aa8d429f077000b3bb639 Reviewed-on: https://go-review.googlesource.com/c/sys/+/527836 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Run-TryBot: Kirill Kolyshkin <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent c7cbcbb commit c7ff727

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

unix/syscall_linux.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -693,28 +693,19 @@ type SockaddrALG struct {
693693

694694
func (sa *SockaddrALG) sockaddr() (unsafe.Pointer, _Socklen, error) {
695695
// Leave room for NUL byte terminator.
696-
if len(sa.Type) > 13 {
696+
if len(sa.Type) > len(sa.raw.Type)-1 {
697697
return nil, 0, EINVAL
698698
}
699-
if len(sa.Name) > 63 {
699+
if len(sa.Name) > len(sa.raw.Name)-1 {
700700
return nil, 0, EINVAL
701701
}
702702

703703
sa.raw.Family = AF_ALG
704704
sa.raw.Feat = sa.Feature
705705
sa.raw.Mask = sa.Mask
706706

707-
typ, err := ByteSliceFromString(sa.Type)
708-
if err != nil {
709-
return nil, 0, err
710-
}
711-
name, err := ByteSliceFromString(sa.Name)
712-
if err != nil {
713-
return nil, 0, err
714-
}
715-
716-
copy(sa.raw.Type[:], typ)
717-
copy(sa.raw.Name[:], name)
707+
copy(sa.raw.Type[:], sa.Type)
708+
copy(sa.raw.Name[:], sa.Name)
718709

719710
return unsafe.Pointer(&sa.raw), SizeofSockaddrALG, nil
720711
}

0 commit comments

Comments
 (0)