History

bpf: introduce BPF token object

Add new kind of BPF kernel object, BPF token. BPF token is meant to allow delegating privileged BPF functionality, like loading a BPF program or creating a BPF map, from privileged process to a *trusted* unprivileged process, all while having a good amount of control over which privileged operations could be performed using provided BPF token. This is achieved through mounting BPF FS instance with extra delegation mount options, which determine what operations are delegatable, and also constraining it to the owning user namespace (as mentioned in the previous patch). BPF token itself is just a derivative from BPF FS and can be created through a new bpf() syscall command, BPF_TOKEN_CREATE, which accepts BPF FS FD, which can be attained through open() API by opening BPF FS mount point. Currently, BPF token "inherits" delegated command, map types, prog type, and attach type bit sets from BPF FS as is. In the future, having an BPF token as a separate object with its own FD, we can allow to further restrict BPF token's allowable set of things either at the creation time or after the fact, allowing the process to guard itself further from unintentionally trying to load undesired kind of BPF programs. But for now we keep things simple and just copy bit sets as is. When BPF token is created from BPF FS mount, we take reference to the BPF super block's owning user namespace, and then use that namespace for checking all the {CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, CAP_SYS_ADMIN} capabilities that are normally only checked against init userns (using capable()), but now we check them using ns_capable() instead (if BPF token is provided). See bpf_token_capable() for details. Such setup means that BPF token in itself is not sufficient to grant BPF functionality. User namespaced process has to *also* have necessary combination of capabilities inside that user namespace. So while previously CAP_BPF was useless when granted within user namespace, now it gains a meaning and allows container managers and sys admins to have a flexible control over which processes can and need to use BPF functionality within the user namespace (i.e., container in practice). And BPF FS delegation mount options and derived BPF tokens serve as a per-container "flag" to grant overall ability to use bpf() (plus further restrict on which parts of bpf() syscalls are treated as namespaced). Note also, BPF_TOKEN_CREATE command itself requires ns_capable(CAP_BPF) within the BPF FS owning user namespace, rounding up the ns_capable() story of BPF token.

Andrii Nakryiko committed 2 years ago

Commit 45273581 parent 40bba147 files · +354 −6Browse files

Showing the changes to tools/include/uapi (1 of 7 files). Show all

Unified · Split

tools/include/uapi/linux/bpf.h+37−0
@@ -847,6 +847,36 @@ union bpf_iter_link_info { · 846 unmodified lines
847 * Returns zero on success. On error, -1 is returned and *errno*847 * Returns zero on success. On error, -1 is returned and *errno*
848 * is set appropriately.848 * is set appropriately.
849 *849 *
850 * BPF_TOKEN_CREATE
851 * Description
852 * Create BPF token with embedded information about what
853 * BPF-related functionality it allows:
854 * - a set of allowed bpf() syscall commands;
855 * - a set of allowed BPF map types to be created with
856 * BPF_MAP_CREATE command, if BPF_MAP_CREATE itself is allowed;
857 * - a set of allowed BPF program types and BPF program attach
858 * types to be loaded with BPF_PROG_LOAD command, if
859 * BPF_PROG_LOAD itself is allowed.
860 *
861 * BPF token is created (derived) from an instance of BPF FS,
862 * assuming it has necessary delegation mount options specified.
863 * This BPF token can be passed as an extra parameter to various
864 * bpf() syscall commands to grant BPF subsystem functionality to
865 * unprivileged processes.
866 *
867 * When created, BPF token is "associated" with the owning
868 * user namespace of BPF FS instance (super block) that it was
869 * derived from, and subsequent BPF operations performed with
870 * BPF token would be performing capabilities checks (i.e.,
871 * CAP_BPF, CAP_PERFMON, CAP_NET_ADMIN, CAP_SYS_ADMIN) within
872 * that user namespace. Without BPF token, such capabilities
873 * have to be granted in init user namespace, making bpf()
874 * syscall incompatible with user namespace, for the most part.
875 *
876 * Return
877 * A new file descriptor (a nonnegative integer), or -1 if an
878 * error occurred (in which case, *errno* is set appropriately).
879 *
850 * NOTES880 * NOTES
851 * eBPF objects (maps and programs) can be shared between processes.881 * eBPF objects (maps and programs) can be shared between processes.
852 *882 *
@@ -901,6 +931,8 @@ enum bpf_cmd { · 48 unmodified lines
901 BPF_ITER_CREATE,931 BPF_ITER_CREATE,
902 BPF_LINK_DETACH,932 BPF_LINK_DETACH,
903 BPF_PROG_BIND_MAP,933 BPF_PROG_BIND_MAP,
934 BPF_TOKEN_CREATE,
935 __MAX_BPF_CMD,
904};936};
905 937
906enum bpf_map_type {938enum bpf_map_type {
@@ -1712,6 +1744,11 @@ union bpf_attr { · 805 unmodified lines
1712 __u32 flags; /* extra flags */1744 __u32 flags; /* extra flags */
1713 } prog_bind_map;1745 } prog_bind_map;
1714 1746
1747 struct { /* struct used by BPF_TOKEN_CREATE command */
1748 __u32 flags;
1749 __u32 bpffs_fd;
1750 } token_create;
1751
1715} __attribute__((aligned(8)));1752} __attribute__((aligned(8)));
1716 1753
1717/* The description below is an attempt at providing documentation to eBPF1754/* The description below is an attempt at providing documentation to eBPF