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 (#6664)

* Refactored ListIPForwardRows to 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 14, 2024
1 parent 89bb9ae commit 68b9628
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/agent/util/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,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
}
return rows, nil
}

func NewIPForwardRow() *MibIPForwardRow {
Expand Down

0 comments on commit 68b9628

Please sign in to comment.