Skip to content

Commit

Permalink
link: fix nil pointer dereference in AttachXDP
Browse files Browse the repository at this point in the history
Signed-off-by: Gavin Bunney <[email protected]>
  • Loading branch information
gavinbunney authored and lmb committed Jul 15, 2024
1 parent 5976561 commit 88e8f88
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion link/xdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func AttachXDP(opts XDPOptions) (Link, error) {
Flags: uint32(opts.Flags),
})

return &xdpLink{*rawLink}, err
if err != nil {
return nil, fmt.Errorf("failed to attach link: %w", err)
}

return &xdpLink{*rawLink}, nil
}

type xdpLink struct {
Expand Down
12 changes: 9 additions & 3 deletions link/xdp_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package link

import (
"math"
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal/testutils"
"github.com/go-quicktest/qt"
)

const IfIndexLO = 1
Expand All @@ -14,13 +16,17 @@ func TestAttachXDP(t *testing.T) {

prog := mustLoadProgram(t, ebpf.XDP, 0, "")

_, err := AttachXDP(XDPOptions{
Program: prog,
Interface: math.MaxInt,
})
qt.Assert(t, qt.IsNotNil(err))

l, err := AttachXDP(XDPOptions{
Program: prog,
Interface: IfIndexLO,
})
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.IsNil(err))

testLink(t, l, prog)
}

0 comments on commit 88e8f88

Please sign in to comment.