Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linker: fix fd leak in fixupKfuncs #1145

Merged
merged 2 commits into from
Oct 3, 2023
Merged

Conversation

lmb
Copy link
Collaborator

@lmb lmb commented Oct 2, 2023

linker: fix fd leak in fixupKfuncs

The kfunc fixup code doesn't properly close the btf.Handle if the 
compatibility check fails:

    leaked fd created at:
       github.com/cilium/ebpf/internal/sys.newFD+0x26
           /home/lorenz/dev/ebpf/internal/sys/fd.go:23
       github.com/cilium/ebpf/internal/sys.NewFD+0x24
           /home/lorenz/dev/ebpf/internal/sys/fd.go:63
       github.com/cilium/ebpf/internal/sys.BtfGetFdById+0x36
           /home/lorenz/dev/ebpf/internal/sys/types.go:583
       github.com/cilium/ebpf/btf.NewHandleFromID+0x2e
           /home/lorenz/dev/ebpf/btf/handle.go:83
       github.com/cilium/ebpf/btf.(*HandleIterator).Next+0xa4
           /home/lorenz/dev/ebpf/btf/handle.go:228
       github.com/cilium/ebpf.findTargetInModule+0xc9
           /home/lorenz/dev/ebpf/prog.go:965
       github.com/cilium/ebpf.findTargetInKernel+0xe4
           /home/lorenz/dev/ebpf/prog.go:943
       github.com/cilium/ebpf.fixupKfuncs+0x3ef
           /home/lorenz/dev/ebpf/linker.go:284
       github.com/cilium/ebpf.newProgramWithOptions+0x80d
           /home/lorenz/dev/ebpf/prog.go:277
       github.com/cilium/ebpf.(*collectionLoader).loadProgram+0x415
           /home/lorenz/dev/ebpf/collection.go:522
       github.com/cilium/ebpf.NewCollectionWithOptions+0x239
           /home/lorenz/dev/ebpf/collection.go:385
       github.com/cilium/ebpf.NewCollection+0x44
           /home/lorenz/dev/ebpf/collection.go:358
       github.com/cilium/ebpf.LoadCollection+0x45
           /home/lorenz/dev/ebpf/collection.go:680
       github.com/cilium/ebpf.TestInvalidKfunc+0xcb
           /home/lorenz/dev/ebpf/elf_reader_test.go:740
       testing.tRunner+0xfe
           /usr/local/go/src/testing/testing.go:1595
       runtime.goexit+0x0
           /usr/local/go/src/runtime/asm_amd64.s:1650

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>

program: fix potential fd leak in findTargetInKernel

Close the module if looking up the target type in the spec fails. This
prevents leaking the module fd.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>

@lmb lmb requested a review from rgo3 October 2, 2023 15:25
@lmb lmb marked this pull request as ready for review October 2, 2023 15:25
The kfunc fixup code doesn't properly close the btf.Handle if the
compatibility check fails:

    leaked fd created at:
        github.com/cilium/ebpf/internal/sys.newFD+0x26
            /home/lorenz/dev/ebpf/internal/sys/fd.go:23
        github.com/cilium/ebpf/internal/sys.NewFD+0x24
            /home/lorenz/dev/ebpf/internal/sys/fd.go:63
        github.com/cilium/ebpf/internal/sys.BtfGetFdById+0x36
            /home/lorenz/dev/ebpf/internal/sys/types.go:583
        github.com/cilium/ebpf/btf.NewHandleFromID+0x2e
            /home/lorenz/dev/ebpf/btf/handle.go:83
        github.com/cilium/ebpf/btf.(*HandleIterator).Next+0xa4
            /home/lorenz/dev/ebpf/btf/handle.go:228
        github.com/cilium/ebpf.findTargetInModule+0xc9
            /home/lorenz/dev/ebpf/prog.go:965
        github.com/cilium/ebpf.findTargetInKernel+0xe4
            /home/lorenz/dev/ebpf/prog.go:943
        github.com/cilium/ebpf.fixupKfuncs+0x3ef
            /home/lorenz/dev/ebpf/linker.go:284
        github.com/cilium/ebpf.newProgramWithOptions+0x80d
            /home/lorenz/dev/ebpf/prog.go:277
        github.com/cilium/ebpf.(*collectionLoader).loadProgram+0x415
            /home/lorenz/dev/ebpf/collection.go:522
        github.com/cilium/ebpf.NewCollectionWithOptions+0x239
            /home/lorenz/dev/ebpf/collection.go:385
        github.com/cilium/ebpf.NewCollection+0x44
            /home/lorenz/dev/ebpf/collection.go:358
        github.com/cilium/ebpf.LoadCollection+0x45
            /home/lorenz/dev/ebpf/collection.go:680
        github.com/cilium/ebpf.TestInvalidKfunc+0xcb
            /home/lorenz/dev/ebpf/elf_reader_test.go:740
        testing.tRunner+0xfe
            /usr/local/go/src/testing/testing.go:1595
        runtime.goexit+0x0
            /usr/local/go/src/runtime/asm_amd64.s:1650

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
Close the module if looking up the target type in the spec fails.
This prevents leaking the module fd.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
@lmb lmb force-pushed the linker-kfunc-fixup-fd-leak branch from 16ab364 to 21a7b56 Compare October 2, 2023 15:43
Copy link
Contributor

@rgo3 rgo3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Thanks :)

@lmb lmb merged commit ddb4445 into cilium:main Oct 3, 2023
10 of 13 checks passed
@lmb lmb deleted the linker-kfunc-fixup-fd-leak branch October 3, 2023 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants