Skip to content

eBPF-based security platform

treewide: open every descriptor close-on-exec

Daniel Borkmann28c6f0d4dfeb
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 44bafb63..7cebdf0c 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -301,7 +301,7 @@ static FILE *source_open(const char *path)
int fd;
if (!image_config_active())
- return fopen(path, "r");
+ return fopen(path, "re");
fd = image_open_relative(path + 1, O_RDONLY);
if (fd < 0) {
errno = -fd;
@@ -330,7 +330,7 @@ static FILE *tree_conf_open(void)
int fd;
if (!image_config_active())
- return fopen(BPM_TREE_ETC_PATH, "r");
+ return fopen(BPM_TREE_ETC_PATH, "re");
fd = image_open(IMAGE_TREE_CONF, O_RDONLY);
if (fd < 0) {
errno = -fd;
diff --git a/builtin-genkey.c b/builtin-genkey.c
index 8d55d358..d10c9a59 100644
--- a/builtin-genkey.c
+++ b/builtin-genkey.c
@@ -240,7 +240,7 @@ static int write_private_pem(EVP_PKEY *pkey, const char *path)
int fd, ret = -1;
BIO *b = NULL;
- fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
if (fd < 0) {
pr_err("open %s: %s", path, strerror(errno));
return -1;
@@ -277,7 +277,7 @@ static int write_cert_der(X509 *c, const char *path)
int fd, ret = -1;
BIO *bio = NULL;
- fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
+ fd = open(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
if (fd < 0) {
pr_err("open %s: %s", path, strerror(errno));
return -1;
diff --git a/builtin-init.c b/builtin-init.c
index 9b1f37e7..079643c7 100644
--- a/builtin-init.c
+++ b/builtin-init.c
@@ -218,7 +218,7 @@ static int init_keyring(void)
static int is_bpffs_mounted(const char *path)
{
- FILE *f = fopen("/proc/self/mounts", "r");
+ FILE *f = fopen("/proc/self/mounts", "re");
char line[4096];
int found = 0;
diff --git a/builtin-status.c b/builtin-status.c
index afbe442a..1bac8e9a 100644
--- a/builtin-status.c
+++ b/builtin-status.c
@@ -131,7 +131,7 @@ static bool bpffs_is_mounted(void)
bool found = false;
FILE *f;
- f = fopen("/proc/self/mounts", "r");
+ f = fopen("/proc/self/mounts", "re");
if (!f)
return false;
while (fgets(line, sizeof(line), f)) {
diff --git a/tools/shirudo-build/package.c b/tools/shirudo-build/package.c
index 914d5457..7388ae55 100644
--- a/tools/shirudo-build/package.c
+++ b/tools/shirudo-build/package.c
@@ -1686,7 +1686,7 @@ static int finalize_object(struct bpf_object *obj, const char *out_path,
1u * 1024u * 1024u,
rodata_data_off, nr_maps, nr_progs);
- fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
if (fd < 0) {
err = -errno;
pr_err("open %s: %s", out_path, strerror(errno));
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index df2ecbe6..9fc1b3a3 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -130,7 +130,7 @@ static int read_config_file(const char *path, struct tree_kv *out,
return ret;
}
{
- FILE *f = fopen(path, "r");
+ FILE *f = fopen(path, "re");
if (!f)
return -errno;
@@ -331,7 +331,7 @@ static int read_selector_config_file(const char *path,
}
{
- FILE *f = fopen(path, "r");
+ FILE *f = fopen(path, "re");
if (!f)
return -errno;
diff --git a/util/config.c b/util/config.c
index 73f98b9c..c22991aa 100644
--- a/util/config.c
+++ b/util/config.c
@@ -320,7 +320,7 @@ int config_load_path(struct config *cfg, const char *path)
if (!cfg || !path)
return -EINVAL;
config_defaults(cfg);
- f = fopen(path, "r");
+ f = fopen(path, "re");
if (!f) {
if (errno != ENOENT)
ret = -errno; /* still apply env override below */
diff --git a/util/preflight.c b/util/preflight.c
index c6613655..a91e8d43 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -121,7 +121,7 @@ static void check_bpf_lsm(struct preflight_result *r)
static bool bpffs_in_proc_filesystems(void)
{
- FILE *f = fopen("/proc/filesystems", "r");
+ FILE *f = fopen("/proc/filesystems", "re");
char line[128];
bool found = false;
diff --git a/util/upatch_maps_otel.c b/util/upatch_maps_otel.c
index 8fda9625..2dcc3b95 100644
--- a/util/upatch_maps_otel.c
+++ b/util/upatch_maps_otel.c
@@ -819,6 +819,7 @@ static int build_log_payload(const char *path,
upatch_decode_map_otel(v, &decoded, &decoded_len);
if (!decode_rc && record_max && decoded_len > record_max) {
xfree(decoded, decoded_len + 1);
+ decoded = NULL;
decode_rc = -E2BIG;
}
if (decode_rc == 0) {