diff options
author | Kumar Kartikeya Dwivedi <memxor@gmail.com> | 2021-10-28 12:04:58 +0530 |
---|---|---|
committer | Alexei Starovoitov <ast@kernel.org> | 2021-10-28 16:30:07 -0700 |
commit | 92274e24b01b331ef7a4227135933e6163fe94aa (patch) | |
tree | e7fc60b8c4b80def6d9a9c74cd4b811589ad55ee /tools/lib/bpf/libbpf.c | |
parent | 549a63238603103fa33cecd49487cf6c0f52e503 (diff) | |
download | linux-stable-92274e24b01b331ef7a4227135933e6163fe94aa.tar.gz linux-stable-92274e24b01b331ef7a4227135933e6163fe94aa.tar.bz2 linux-stable-92274e24b01b331ef7a4227135933e6163fe94aa.zip |
libbpf: Use O_CLOEXEC uniformly when opening fds
There are some instances where we don't use O_CLOEXEC when opening an
fd, fix these up. Otherwise, it is possible that a parallel fork causes
these fds to leak into a child process on execve.
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211028063501.2239335-6-memxor@gmail.com
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r-- | tools/lib/bpf/libbpf.c | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 73e1e70a722f..742d1a388179 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -1232,7 +1232,7 @@ static int bpf_object__elf_init(struct bpf_object *obj) */ elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz); } else { - obj->efile.fd = open(obj->path, O_RDONLY); + obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC); if (obj->efile.fd < 0) { char errmsg[STRERR_BUFSIZE], *cp; @@ -9615,7 +9615,7 @@ static int append_to_file(const char *file, const char *fmt, ...) int fd, n, err = 0; va_list ap; - fd = open(file, O_WRONLY | O_APPEND, 0); + fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0); if (fd < 0) return -errno; @@ -11260,7 +11260,7 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) int fd, err = 0, len; char buf[128]; - fd = open(fcpu, O_RDONLY); + fd = open(fcpu, O_RDONLY | O_CLOEXEC); if (fd < 0) { err = -errno; pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err); |