From cd38f32666a2777757240d63f271e31f8f823db9 Mon Sep 17 00:00:00 2001 From: Gavin Xin Date: Sat, 14 Sep 2024 14:02:16 +0800 Subject: [PATCH] Replace unsafe.Slice with memory copying to avoid potential fault memory 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 Signed-off-by: Wenying Dong --- 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 {