Skip to content

libbpf: prog 'lsm_bpf': failed to attach: Device or resource busy #352

@chunbuwan

Description

@chunbuwan

I have compiled android 15 aosp, car sdk x86 64 userdebug, and I want bpf lsm to take effect on it.
I used the lsm in the libbpf-bootstrap example and compiled lsm.bpf.c into lsm.bpf.o using android.bp:

bpf {
    name: "lsm.bpf.o",
    srcs: ["lsm.bpf.c"],
    include_dirs: ["external/libbpf/src",
    "system/bpf/include/vmlinux/android15_6.6/x86_64",],
    cflags: [
        "-Wno-all",
        "-Wno-error",
	"-Wno-unused-parameter",
    ],
}

Then, use bpftool and lsm.bpf.o to generate lsm.skel.h, and then compile Lsm.c into the executable file lsm using Android.bp and lsm.skel.h:

cc_binary {
    name: "lsm",

    cflags: [
        "-Wno-all",
        "-Wno-error",
	"-Wno-unused-parameter",
    ],
    include_dirs: ["external/libbpf/src",],
     shared_libs: [
        "liblog",
        "libcutils",
        "libbpf_android",
        "libbase",
        "libnetdutils",
        "libbpf_bcc",
        "libbpf"
    ],
    srcs: [
        "lsm.c",
    ],
}

Finally, the executable file lsm was pushed into the emulator and run./lsm, but an error was reported:

emulator_car64_x86_64:/data/local/tmp # ./lsm
libbpf: loading object 'lsm_bpf' from buffer
libbpf: elf: section(3) lsm/bpf, size 96, link 0, flags 6, type=1
libbpf: sec 'lsm/bpf': found program 'lsm_bpf' at insn offset 0 (0 bytes), code size 12 insns (96 bytes)
libbpf: elf: section(4) .rellsm/bpf, size 16, link 9, flags 40, type=9
libbpf: elf: section(5) license, size 4, link 0, flags 3, type=1
libbpf: license of lsm_bpf is GPL
libbpf: elf: section(6) .rodata, size 24, link 0, flags 2, type=1
libbpf: elf: section(7) .BTF, size 553, link 0, flags 0, type=1
libbpf: elf: section(9) .symtab, size 120, link 1, flags 0, type=2
libbpf: looking for externs among 5 symbols...
libbpf: collected 0 externs total
libbpf: map 'lsm_bpf.rodata' (global data): at sec_idx 6, offset 0, flags 80.
libbpf: map 0 is "lsm_bpf.rodata"
libbpf: sec '.rellsm/bpf': collecting relocation for section(3) 'lsm/bpf'
libbpf: sec '.rellsm/bpf': relo #0: insn #5 against '.rodata'
libbpf: prog 'lsm_bpf': found data map 0 (lsm_bpf.rodata, sec 6, off 0) for insn 5
libbpf: object 'lsm_bpf': failed (-22) to create BPF token from '/sys/fs/bpf', skipping optional step...
libbpf: loaded kernel BTF from '/sys/kernel/btf/vmlinux'
libbpf: map 'lsm_bpf.rodata': created successfully, fd=3
libbpf: prog 'lsm_bpf': failed to attach: Device or resource busy
libbpf: prog 'lsm_bpf': failed to auto-attach: -16
Failed to attach BPF skeleton

strace ./lsm, part of the content:

bpf(BPF_BTF_LOAD, 0x7ffed3783cf0, 40)   = 4
close(4)                                = 0
bpf(BPF_BTF_LOAD, 0x7ffed3783cc0, 40)   = -1 EINVAL (Invalid argument)
bpf(BPF_BTF_LOAD, 0x7ffed3783d20, 40)   = 4
bpf(BPF_MAP_CREATE, {map_type=BPF_MAP_TYPE_ARRAY, key_size=4, value_size=32, max_entries=1, map_flags=0, inner_map_fd=0, map_name="libbpf_global", map_ifindex=0}, 80) = 5
bpf(BPF_PROG_LOAD, {prog_type=BPF_PROG_TYPE_SOCKET_FILTER, insn_cnt=5, insns=0x7ffed3783d90, license="GPL", log_level=0, log_size=0, log_buf=NULL, kern_version=KERNEL_VERSION(0, 0, 0), prog_flags=0, prog_name="", prog_ifindex=0, expected_attach_type=BPF_CGROUP_INET_INGRESS}, 148) = 6
close(5)                                = 0
close(6)                                = 0
bpf(BPF_MAP_CREATE, {map_type=BPF_MAP_TYPE_ARRAY, key_size=4, value_size=24, max_entries=1, map_flags=0x80 /* BPF_F_??? */, inner_map_fd=0, map_name="lsm_bpf.rodata", map_ifindex=0, ...}, 80) = 5
dup3(5, 3, O_CLOEXEC)                   = 3
close(5)                                = 0
write(2, "libbpf: map 'lsm_bpf.rodata': cr"..., 57libbpf: map 'lsm_bpf.rodata': created successfully, fd=3
) = 57
bpf(BPF_MAP_UPDATE_ELEM, {map_fd=3, key=0x7ffed3785160, value=0x73a7ad70e000, flags=BPF_ANY}, 32) = 0
bpf(0x16 /* BPF_??? */, 0x7ffed3783de0, 4) = 0
bpf(BPF_PROG_LOAD, {prog_type=0x1d /* BPF_PROG_TYPE_??? */, insn_cnt=12, insns=0x73a5ec6d2a20, license="GPL", log_level=0, log_size=0, log_buf=NULL, kern_version=KERNEL_VERSION(6, 6, 57), prog_flags=0, prog_name="lsm_bpf", prog_ifindex=0, expected_attach_type=0x1b /* BPF_??? */, ...}, 148) = 5
bpf(BPF_MAP_CREATE, {map_type=BPF_MAP_TYPE_ARRAY, key_size=4, value_size=32, max_entries=1, map_flags=0, inner_map_fd=0, map_name="libbpf_det_bind", map_ifindex=0}, 80) = 6
bpf(BPF_PROG_LOAD, {prog_type=BPF_PROG_TYPE_SOCKET_FILTER, insn_cnt=2, insns=0x7ffed3783db0, license="GPL", log_level=0, log_size=0, log_buf=NULL, kern_version=KERNEL_VERSION(0, 0, 0), prog_flags=0, prog_name="", prog_ifindex=0, expected_attach_type=BPF_CGROUP_INET_INGRESS}, 148) = 8
bpf(0x23 /* BPF_??? */, 0x7ffed3783c40, 12) = 0
close(6)                                = 0
close(8)                                = 0
bpf(0x23 /* BPF_??? */, 0x7ffed3783de0, 12) = 0
munmap(0x73a51b6d6000, 6217728)         = 0
madvise(0x73a51b4d4000, 704512, MADV_DONTNEED) = 0
bpf(0x1c /* BPF_??? */, 0x7ffed37851b0, 64) = -1 EBUSY (Device or resource busy)
write(2, "libbpf: prog 'lsm_bpf': failed t"..., 66libbpf: prog 'lsm_bpf': failed to attach: Device or resource busy
) = 66
write(2, "libbpf: prog 'lsm_bpf': failed t"..., 51libbpf: prog 'lsm_bpf': failed to auto-attach: -16
) = 51
write(2, "Failed to attach BPF skeleton\n", 30Failed to attach BPF skeleton
) = 30

Configuration as follows:

emulator_car64_x86_64:/data/local/tmp # zcat /proc/config.gz | grep "BPF"
CONFIG_BPF=y
CONFIG_HAVE_EBPF_JIT=y
CONFIG_ARCH_WANT_DEFAULT_BPF_JIT=y
# BPF subsystem
CONFIG_BPF_SYSCALL=y
CONFIG_BPF_JIT=y
CONFIG_BPF_JIT_ALWAYS_ON=y
CONFIG_BPF_JIT_DEFAULT_ON=y
# CONFIG_BPF_UNPRIV_DEFAULT_OFF is not set
# CONFIG_BPF_PRELOAD is not set
CONFIG_BPF_LSM=y
# end of BPF subsystem
CONFIG_CGROUP_BPF=y
CONFIG_NETFILTER_BPF_LINK=y
CONFIG_NETFILTER_XT_MATCH_BPF=y
# CONFIG_BPFILTER is not set
CONFIG_NET_CLS_BPF=y
CONFIG_NET_ACT_BPF=y
# CONFIG_BPF_STREAM_PARSER is not set
CONFIG_BPF_LIRC_MODE2=y
# HID-BPF support
# end of HID-BPF support
CONFIG_FUSE_BPF=y
CONFIG_BPF_EVENTS=y
# CONFIG_TEST_BPF is not set

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions