Skip to content

Commit

Permalink
Replace unsafe.Slice with memory copying to avoid potential fault mem…
Browse files Browse the repository at this point in the history
…ory issue

* Refactored ListIPForwardRows to deep copy IP forwarding table rows.
* Removed unsafe.Slice and replaced with manual pointer dereferencing and copying.

This change addresses a potential fault memory issue when iterating through the IP forwarding table,
caused by the use of slices after corresponding memory has been freed, leading to access failure.

Signed-off-by: Shuyang Xin <gavinx@vmware.com>
Signed-off-by: Wenying Dong <wenyingd@vmware.com>
  • Loading branch information
XinShuYang committed Sep 13, 2024
1 parent afdd261 commit f003d82
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion pkg/agent/util/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ func (a *RawSockAddrInet) String() string {
return a.IP().String()
}

func (a *RawSockAddrInet) deepCopy() RawSockAddrInet {
sockData := [26]byte{}
copy(a.data[:], sockData[:])
return RawSockAddrInet{Family: a.Family, data: sockData}
}

func NewRawSockAddrInetFromIP(ip net.IP) *RawSockAddrInet {
sockAddrInet := new(RawSockAddrInet)
if ip.To4() != nil {
Expand Down Expand Up @@ -202,6 +208,10 @@ func (p *AddressPrefix) String() string {
return p.IPNet().String()
}

func (p *AddressPrefix) deepCopy() AddressPrefix {
return AddressPrefix{Prefix: p.Prefix, prefixLength: p.prefixLength}
}

func NewAddressPrefixFromIPNet(ipnet *net.IPNet) *AddressPrefix {
if ipnet == nil {
return nil
Expand Down Expand Up @@ -359,6 +369,27 @@ func (n *netIO) getIPForwardTable(family uint16, ipForwardTable **MibIPForwardTa
return
}

func (r *MibIPForwardRow) deepCopy() MibIPForwardRow {
return MibIPForwardRow{
Luid: r.Luid,
Index: r.Index,
SitePrefixLength: r.SitePrefixLength,
ValidLifetime: r.ValidLifetime,
PreferredLifetime: r.PreferredLifetime,
Metric: r.Metric,
Protocol: r.Protocol,
Loopback: r.Loopback,
AutoconfigureAddress: r.AutoconfigureAddress,
Publish: r.Publish,
Immortal: r.Immortal,
Age: r.Age,
Origin: r.Origin,

DestinationPrefix: r.DestinationPrefix.deepCopy(),
NextHop: r.NextHop.deepCopy(),
}
}

func (n *netIO) ListIPForwardRows(family uint16) ([]MibIPForwardRow, error) {
var table *MibIPForwardTable
err := n.getIPForwardTable(family, &table)
Expand All @@ -368,7 +399,16 @@ func (n *netIO) ListIPForwardRows(family uint16) ([]MibIPForwardRow, error) {
if err != nil {
return nil, os.NewSyscallError("iphlpapi.GetIpForwardTable", err)
}
return unsafe.Slice(&table.Table[0], table.NumEntries), nil
rows := make([]MibIPForwardRow, table.NumEntries, table.NumEntries)

pFirstRow := uintptr(unsafe.Pointer(&table.Table[0]))
rowSize := unsafe.Sizeof(table.Table[0])

for i := uint32(0); i < table.NumEntries; i++ {
row := *(*MibIPForwardRow)(unsafe.Pointer(pFirstRow + rowSize*uintptr(i)))
rows[i] = row.deepCopy()
}
return rows, nil
}

func NewIPForwardRow() *MibIPForwardRow {
Expand Down

0 comments on commit f003d82

Please sign in to comment.