• Andrii Nakryiko's avatar
    selftests/bpf: Add BPF token-enabled tests · fcb9597f
    Andrii Nakryiko authored
    Add a selftest that attempts to conceptually replicate intended BPF
    token use cases inside user namespaced container.
    
    Child process is forked. It is then put into its own userns and mountns.
    Child creates BPF FS context object. This ensures child userns is
    captured as the owning userns for this instance of BPF FS. Given setting
    delegation mount options is privileged operation, we ensure that child
    cannot set them.
    
    This context is passed back to privileged parent process through Unix
    socket, where parent sets up delegation options, creates, and mounts it
    as a detached mount. This mount FD is passed back to the child to be
    used for BPF token creation, which allows otherwise privileged BPF
    operations to succeed inside userns.
    
    We validate that all of token-enabled privileged commands (BPF_BTF_LOAD,
    BPF_MAP_CREATE, and BPF_PROG_LOAD) work as intended. They should only
    succeed inside the userns if a) BPF token is provided with proper
    allowed sets of commands and types; and b) namespaces CAP_BPF and other
    privileges are set. Lacking a) or b) should lead to -EPERM failures.
    
    Based on suggested workflow by Christian Brauner ([0]).
    
      [0] https://lore.kernel.org/bpf/20230704-hochverdient-lehne-eeb9eeef785e@brauner/Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20240124022127.2379740-17-andrii@kernel.org
    fcb9597f
token.c 16.5 KB