From 88e8f88881dd1423ffafa8c4db42d24b87ae87b3 Mon Sep 17 00:00:00 2001 From: Gavin Bunney Date: Mon, 15 Jul 2024 11:12:02 +0100 Subject: [PATCH] link: fix nil pointer dereference in AttachXDP Signed-off-by: Gavin Bunney --- link/xdp.go | 6 +++++- link/xdp_test.go | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/link/xdp.go b/link/xdp.go index dbfa52996..2ec441229 100644 --- a/link/xdp.go +++ b/link/xdp.go @@ -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 { diff --git a/link/xdp_test.go b/link/xdp_test.go index d08721c27..c25deb140 100644 --- a/link/xdp_test.go +++ b/link/xdp_test.go @@ -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 @@ -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) }