History

bpf: Support O_PATH FDs in BPF_OBJ_PIN and BPF_OBJ_GET commands

Current UAPI of BPF_OBJ_PIN and BPF_OBJ_GET commands of bpf() syscall forces users to specify pinning location as a string-based absolute or relative (to current working directory) path. This has various implications related to security (e.g., symlink-based attacks), forces BPF FS to be exposed in the file system, which can cause races with other applications. One of the feedbacks we got from folks working with containers heavily was that inability to use purely FD-based location specification was an unfortunate limitation and hindrance for BPF_OBJ_PIN and BPF_OBJ_GET commands. This patch closes this oversight, adding path_fd field to BPF_OBJ_PIN and BPF_OBJ_GET UAPI, following conventions established by *at() syscalls for dirfd + pathname combinations. This now allows interesting possibilities like working with detached BPF FS mount (e.g., to perform multiple pinnings without running a risk of someone interfering with them), and generally making pinning/getting more secure and not prone to any races and/or security attacks. This is demonstrated by a selftest added in subsequent patch that takes advantage of new mount APIs (fsopen, fsconfig, fsmount) to demonstrate creating detached BPF FS mount, pinning, and then getting BPF map out of it, all while never exposing this private instance of BPF FS to outside worlds.

Andrii Nakryiko committed 3 years ago

Commit cb8edce1 parent 2b001b95 files · +50 −15Browse files

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

Unified · Split

include/uapi/linux/bpf.h+10−0
@@ -1272,6 +1272,9 @@ enum { · 1271 unmodified lines
12721272
12731273 /* Create a map that will be registered/unregesitered by the backed bpf_link */
12741274 BPF_F_LINK = (1U << 13),
1275+
1276+/* Get path from provided FD in BPF_OBJ_PIN/BPF_OBJ_GET commands */
1277+ BPF_F_PATH_FD = (1U << 14),
12751278 };
12761279
12771280 /* Flags for BPF_PROG_QUERY. */
@@ -1420,6 +1423,13 @@ union bpf_attr { · 142 unmodified lines
14201423 __aligned_u64 pathname;
14211424 __u32 bpf_fd;
14221425 __u32 file_flags;
1426+ /* Same as dirfd in openat() syscall; see openat(2)
1427+ * manpage for details of path FD and pathname semantics;
1428+ * path_fd should accompanied by BPF_F_PATH_FD flag set in
1429+ * file_flags field, otherwise it should be set to zero;
1430+ * if BPF_F_PATH_FD flag is not set, AT_FDCWD is assumed.
1431+ */
1432+ __s32 path_fd;
14231433 };
14241434
14251435 struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t