From 864371cc10544df23701ec9471a09a28768af494 Mon Sep 17 00:00:00 2001 From: wanggang26 Date: Mon, 30 Oct 2023 18:10:52 +0800 Subject: [PATCH] nshlib:enable O_CLOEXEC expliciti to avoid potential fd leak leaking here means fork/vfork will duplicate fd without O_CLOEXEC flag to the child process. Signed-off-by: wanggang26 --- nshlib/nsh_fsutils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nshlib/nsh_fsutils.c b/nshlib/nsh_fsutils.c index 4dd8f4141..309f24dc2 100644 --- a/nshlib/nsh_fsutils.c +++ b/nshlib/nsh_fsutils.c @@ -82,7 +82,7 @@ static int getpid_callback(FAR struct nsh_vtbl_s *vtbl, snprintf(buffer, sizeof(buffer), "%s/%s/cmdline", dirpath, entryp->d_name); - fd = open(buffer, O_RDONLY); + fd = open(buffer, O_RDONLY | O_CLOEXEC); if (fd < 0) { return 0; @@ -137,7 +137,7 @@ int nsh_catfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, /* Open the file for reading */ - fd = open(filepath, O_RDONLY); + fd = open(filepath, O_RDONLY | O_CLOEXEC); if (fd < 0) { #if defined(CONFIG_NSH_PROC_MOUNTPOINT) @@ -282,7 +282,7 @@ int nsh_readfile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, /* Open the file */ - fd = open(filepath, O_RDONLY); + fd = open(filepath, O_RDONLY | O_CLOEXEC); if (fd < 0) { nsh_error(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO); @@ -381,7 +381,7 @@ int nsh_writefile(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, /* Open the file for reading */ - fd = open(filepath, O_WRONLY); + fd = open(filepath, O_WRONLY | O_CLOEXEC); if (fd < 0) { #if defined(CONFIG_NSH_PROC_MOUNTPOINT)