libbpf: ensure libbpf always opens files with O_CLOEXEC
Andrii Nakryiko
maintainerPATCH 1/2 libbpf: ensure libbpf always opens files with O_CLOEXEC
Make sure that libbpf code always gets FD with O_CLOEXEC flag set, regardless if file is open through open() or fopen(). For the latter this means to add "e" to mode string, which is supported since pretty ancient glibc v2.7.
I also dropped outdated TODO comment in usdt.c, which was already completed.
--- tools/lib/bpf/btf.c | 2 +- tools/lib/bpf/libbpf.c | 6 +++--- tools/lib/bpf/libbpf_probes.c | 2 +- tools/lib/bpf/usdt.c | 5 ++--- 4 files changed, 7 insertions(+), 8 deletions(-)
tools/lib/bpf/btf.c +1 -1
@@ -1064,7 +1064,7 @@ static struct btf *btf_parse_raw(const char *path, struct btf *base_btf)int err =0 ;long sz; - f =fopen (path,"rb" ); + f =fopen (path,"rbe" );if (!f) { err = -errno; goto err_out;
tools/lib/bpf/libbpf.c +3 -3
@@ -4351,7 +4351,7 @@ static int bpf_get_map_info_from_fdinfo(int fd, struct bpf_map_info *info)snprintf (file,sizeof (file),"/proc/%d/fdinfo/%d" ,getpid (), fd);memset (info,0 ,sizeof (*info)); - fp =fopen (file,"r" ); + fp =fopen (file,"re" );if (!fp) { err = -errno; pr_warn("failed to open %s: %d. No procfs support?\n", file,
@@ -7455,7 +7455,7 @@ int libbpf_kallsyms_parse(kallsyms_cb_t cb, void *ctx)int ret, err =0 ;FILE *f; - f =fopen ("/proc/kallsyms" ,"r" ); + f =fopen ("/proc/kallsyms" ,"re" );if (!f) { err = -errno;pr_warn ("failed to open /proc/kallsyms: %d\n" , err);
@@ -10075,7 +10075,7 @@ static int parse_uint_from_file(const char *file, const char *fmt)int err, ret;FILE *f; - f =fopen (file,"r" ); + f =fopen (file,"re" );if (!f) { err = -errno; pr_debug("failed to open '%s': %s\n", file,
tools/lib/bpf/libbpf_probes.c +1 -1
@@ -38,7 +38,7 @@ static __u32 get_ubuntu_kernel_version(void)if (faccessat (AT_FDCWD , ubuntu_kver_file,R_OK ,AT_EACCESS ) !=0 )return 0 ; - f =fopen (ubuntu_kver_file,"r" ); + f =fopen (ubuntu_kver_file,"re" );if (!f)return 0 ;
tools/lib/bpf/usdt.c +2 -3
@@ -466,7 +466,7 @@ static int parse_vma_segs(int pid, const char *lib_path, struct elf_seg **segs, proceed:sprintf (line,"/proc/%d/maps" , pid); - f =fopen (line,"r" ); + f =fopen (line,"re" );if (!f) { err = -errno; pr_warn("usdt: failed to open '%s' to get base addr of '%s': %d\n",
@@ -954,8 +954,7 @@ struct bpf_link *usdt_manager_attach_usdt(struct usdt_manager *man, const struct spec_map_fd =bpf_map__fd (man->specs_map); ip_map_fd =bpf_map__fd (man->ip_to_spec_id_map); -/* TODO: perform path resolution similar to uprobe's */ - fd =open (path,O_RDONLY ); + fd =open (path,O_RDONLY |O_CLOEXEC );if (fd <0 ) { err = -errno;pr_warn ("usdt: failed to open ELF binary '%s': %d\n" , path, err);
Signature · 1 line
2.34.1
Andrii Nakryiko
maintainerPATCH 2/2 libbpf: ensure FD >= 3 during bpf_map__reuse_fd()
Improve bpf_map__reuse_fd() logic and ensure that dup'ed map FD is "good" (>= 3) and has O_CLOEXEC flags. Use fcntl(F_DUPFD_CLOEXEC) for that, similarly to ensure_good_fd() helper we already use in low-level APIs that work with bpf() syscall.
--- tools/lib/bpf/libbpf.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
tools/lib/bpf/libbpf.c +6 -7
@@ -4414,18 +4414,17 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)if (!new_name)return libbpf_err (-errno); - new_fd =open ("/" ,O_RDONLY |O_CLOEXEC ); +/* +* Like dup(), but make sure new FD is >= 3 and has O_CLOEXEC set. +* This is similar to what we do in ensure_good_fd(), but without +* closing original FD. +*/ + new_fd =fcntl (fd,F_DUPFD_CLOEXEC ,3 );if (new_fd <0 ) { err = -errno; goto err_free_new_name; } - new_fd =dup3 (fd, new_fd,O_CLOEXEC ); -if (new_fd <0 ) { - err = -errno; - goto err_close_new_fd; - } - err =zclose (map->fd);if (err) { err = -errno;
Signature · 1 line
2.34.1
Quoted 21 lines · Andrii Nakryiko
Make sure that libbpf code always gets FD with
O_CLOEXECflag set, regardless if file is open throughopen()orfopen(). For the latter this means to add "e" to mode string, which is supported since pretty ancient glibc v2.7.I also dropped outdated TODO comment in
usdt.c, which was already completed.Suggested-by Lennart Poettering <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- tools/lib/bpf/btf.c | 2 +- tools/lib/bpf/libbpf.c | 6 +++--- tools/lib/bpf/libbpf_probes.c | 2 +- tools/lib/bpf/usdt.c | 5 ++--- 4 files changed, 7 insertions(+), 8 deletions(-)tools/lib/bpf/btf.c +0
-0
tools/lib/bpf/btf.c static struct btf *btf_parse_raw(const char *path, struct btf *base_btf) · line 1067
Quoted 5 lines
tools/lib/bpf/btf.c +1
-1@@ -1064,7 +1064,7 @@ static struct btf *btf_parse_raw(const char *path, struct btf *base_btf)int err =0 ;long sz; - f =fopen (path,"rb" ); + f =fopen (path,"rbe" );
You might as well drop the "b". That's a thing only on non-POSIX systems. So unless you want to support windows with this, you can drop it with zero effect.
Thanks for doing this, much appreciated!
Lennart
--
Lennart Poettering, Berlin
Daniel Borkmann
maintainerQuoted 26 lines · Lennart Poettering
Quoted 24 lines · Andrii Nakryiko
Make sure that libbpf code always gets FD with
O_CLOEXECflag set, regardless if file is open throughopen()orfopen(). For the latter this means to add "e" to mode string, which is supported since pretty ancient glibc v2.7.I also dropped outdated TODO comment in
usdt.c, which was already completed.Suggested-by Lennart Poettering <[email protected]>Signed-off-by Andrii Nakryiko <[email protected]>--- tools/lib/bpf/btf.c | 2 +- tools/lib/bpf/libbpf.c | 6 +++--- tools/lib/bpf/libbpf_probes.c | 2 +- tools/lib/bpf/usdt.c | 5 ++--- 4 files changed, 7 insertions(+), 8 deletions(-)tools/lib/bpf/btf.c +0
-0@@ -1064,7 +1064,7 @@ static struct btf *btf_parse_raw(const char *path, struct btf *base_btf)int err =0 ;long sz;
Quoted 6 lines
tools/lib/bpf/btf.c static struct btf *btf_parse_raw(const char *path, struct btf *base_btf) · line 1067
Quoted 2 lines
tools/lib/bpf/btf.c +1
-1- f =fopen (path,"rb" ); + f =fopen (path,"rbe" );You might as well drop the "b". That's a thing only on non-POSIX systems. So unless you want to support windows with this, you can drop it with zero effect.
Iiuc, the library is also imported by the 'ebpf for windows' project [0], so we might need to keep the 'b' intact in that case.
Thanks,
Daniel
[0] https://github.com/microsoft/ebpf-for-windows/#architectural-overview
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:
Quoted 8 lines · On Thu, 25 May 2023 15:13:10 -0700 you wrote:
Make sure that libbpf code always gets FD with
O_CLOEXECflag set, regardless if file is open throughopen()orfopen(). For the latter this means to add "e" to mode string, which is supported since pretty ancient glibc v2.7.I also dropped outdated TODO comment in
usdt.c, which was already completed.[...]
Here is the summary with links:
- [bpf-next,1/2] libbpf: ensure libbpf always opens files with O_CLOEXEC
https://git.kernel.org/bpf/bpf-next/c/59842c5451fe - [bpf-next,2/2] libbpf: ensure FD >= 3 during bpf_map__reuse_fd() https://git.kernel.org/bpf/bpf-next/c/4aadd2920b81
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html