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 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 <[email protected]>
  • Loading branch information
XinShuYang committed Sep 12, 2024
1 parent b3936b1 commit eab0b6c
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

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Build Antrea Windows binaries

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement) (typecheck)

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement) (typecheck)

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement) (typecheck)

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Golangci-lint (macos-latest)

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement (typecheck)

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Analyze on Windows (go)

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement

Check failure on line 380 in pkg/agent/util/syscall/syscall_windows.go

View workflow job for this annotation

GitHub Actions / Unit test (windows-2022)

cannot use rows (variable of type []*MibIPForwardRow) as []MibIPForwardRow value in return statement
}

func NewIPForwardRow() *MibIPForwardRow {
Expand Down

0 comments on commit eab0b6c

Please sign in to comment.