Commit 92274e24 authored by Kumar Kartikeya Dwivedi's avatar Kumar Kartikeya Dwivedi Committed by Alexei Starovoitov

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: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211028063501.2239335-6-memxor@gmail.com
parent 549a6323
...@@ -897,7 +897,7 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, ...@@ -897,7 +897,7 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
return ERR_PTR(-LIBBPF_ERRNO__LIBELF); return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
} }
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
err = -errno; err = -errno;
pr_warn("failed to open %s: %s\n", path, strerror(errno)); pr_warn("failed to open %s: %s\n", path, strerror(errno));
......
...@@ -1232,7 +1232,7 @@ static int bpf_object__elf_init(struct bpf_object *obj) ...@@ -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); elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
} else { } else {
obj->efile.fd = open(obj->path, O_RDONLY); obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC);
if (obj->efile.fd < 0) { if (obj->efile.fd < 0) {
char errmsg[STRERR_BUFSIZE], *cp; char errmsg[STRERR_BUFSIZE], *cp;
...@@ -9615,7 +9615,7 @@ static int append_to_file(const char *file, const char *fmt, ...) ...@@ -9615,7 +9615,7 @@ static int append_to_file(const char *file, const char *fmt, ...)
int fd, n, err = 0; int fd, n, err = 0;
va_list ap; va_list ap;
fd = open(file, O_WRONLY | O_APPEND, 0); fd = open(file, O_WRONLY | O_APPEND | O_CLOEXEC, 0);
if (fd < 0) if (fd < 0)
return -errno; return -errno;
...@@ -11260,7 +11260,7 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) ...@@ -11260,7 +11260,7 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
int fd, err = 0, len; int fd, err = 0, len;
char buf[128]; char buf[128];
fd = open(fcpu, O_RDONLY); fd = open(fcpu, O_RDONLY | O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
err = -errno; err = -errno;
pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err); pr_warn("Failed to open cpu mask file %s: %d\n", fcpu, err);
......
...@@ -33,7 +33,7 @@ static int get_vendor_id(int ifindex) ...@@ -33,7 +33,7 @@ static int get_vendor_id(int ifindex)
snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname); snprintf(path, sizeof(path), "/sys/class/net/%s/device/vendor", ifname);
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) if (fd < 0)
return -1; return -1;
......
...@@ -301,7 +301,7 @@ static int init_output_elf(struct bpf_linker *linker, const char *file) ...@@ -301,7 +301,7 @@ static int init_output_elf(struct bpf_linker *linker, const char *file)
if (!linker->filename) if (!linker->filename)
return -ENOMEM; return -ENOMEM;
linker->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644); linker->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (linker->fd < 0) { if (linker->fd < 0) {
err = -errno; err = -errno;
pr_warn("failed to create '%s': %d\n", file, err); pr_warn("failed to create '%s': %d\n", file, err);
...@@ -556,7 +556,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, ...@@ -556,7 +556,7 @@ static int linker_load_obj_file(struct bpf_linker *linker, const char *filename,
obj->filename = filename; obj->filename = filename;
obj->fd = open(filename, O_RDONLY); obj->fd = open(filename, O_RDONLY | O_CLOEXEC);
if (obj->fd < 0) { if (obj->fd < 0) {
err = -errno; err = -errno;
pr_warn("failed to open file '%s': %d\n", filename, err); pr_warn("failed to open file '%s': %d\n", filename, err);
......
...@@ -300,7 +300,7 @@ int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area, ...@@ -300,7 +300,7 @@ int xsk_umem__create_v0_0_4(struct xsk_umem **umem_ptr, void *umem_area,
if (!umem) if (!umem)
return -ENOMEM; return -ENOMEM;
umem->fd = socket(AF_XDP, SOCK_RAW, 0); umem->fd = socket(AF_XDP, SOCK_RAW | SOCK_CLOEXEC, 0);
if (umem->fd < 0) { if (umem->fd < 0) {
err = -errno; err = -errno;
goto out_umem_alloc; goto out_umem_alloc;
...@@ -549,7 +549,7 @@ static int xsk_get_max_queues(struct xsk_socket *xsk) ...@@ -549,7 +549,7 @@ static int xsk_get_max_queues(struct xsk_socket *xsk)
struct ifreq ifr = {}; struct ifreq ifr = {};
int fd, err, ret; int fd, err, ret;
fd = socket(AF_LOCAL, SOCK_DGRAM, 0); fd = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (fd < 0) if (fd < 0)
return -errno; return -errno;
...@@ -1046,7 +1046,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, ...@@ -1046,7 +1046,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
} }
if (umem->refcount++ > 0) { if (umem->refcount++ > 0) {
xsk->fd = socket(AF_XDP, SOCK_RAW, 0); xsk->fd = socket(AF_XDP, SOCK_RAW | SOCK_CLOEXEC, 0);
if (xsk->fd < 0) { if (xsk->fd < 0) {
err = -errno; err = -errno;
goto out_xsk_alloc; goto out_xsk_alloc;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment