From c531e9cbb71ea5f481335d4a0300bfa2f249e2b2 Mon Sep 17 00:00:00 2001 From: Shuyang Xin Date: Thu, 12 Sep 2024 11:51:07 +0800 Subject: [PATCH] Replace unsafe.Slice with memory copying to avoid potential fault memory issue * 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. Signed-off-by: Shuyang Xin --- pkg/agent/util/syscall/syscall_windows.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/agent/util/syscall/syscall_windows.go b/pkg/agent/util/syscall/syscall_windows.go index c0b5d29e9ca..03ddf75cea0 100644 --- a/pkg/agent/util/syscall/syscall_windows.go +++ b/pkg/agent/util/syscall/syscall_windows.go @@ -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 {