From 324358180ddeaae482c5f66bb5647a4918710296 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Tue, 28 Mar 2023 12:08:41 -0700 Subject: Set close-on-exec when opening files Signed-off-by: Alan Coopersmith --- src/AsciiSrc.c | 14 +++++++++----- src/MultiSrc.c | 12 ++++++++---- src/TextPop.c | 9 ++++++++- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/AsciiSrc.c b/src/AsciiSrc.c index 7833514..f493b06 100644 --- a/src/AsciiSrc.c +++ b/src/AsciiSrc.c @@ -55,6 +55,10 @@ in this Software without prior written authorization from The Open Group. #include #include +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + #if (defined(ASCII_STRING) || defined(ASCII_DISK)) #include /* for Widget Classes */ #endif @@ -1280,7 +1284,7 @@ WriteToFile(String string, String name, unsigned length) { int fd; - if ((fd = creat(name, 0666)) == -1) + if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) == -1) return (False); if (write(fd, string, length) == -1) { @@ -1337,7 +1341,7 @@ WritePiecesToFile(AsciiSrcObject src, String name) } } - if ((fd = creat(name, 0666)) == -1) + if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) == -1) return (False); for (piece = src->ascii_src.first_piece; piece; piece = piece->next) @@ -1443,7 +1447,7 @@ InitStringOrFile(AsciiSrcObject src, Bool newString) XtErrorMsg("NoFile", "asciiSourceCreate", "XawError", "Creating a read only disk widget and no file specified.", NULL, NULL); - open_mode = O_RDONLY; + open_mode = O_RDONLY | O_CLOEXEC; fdopen_mode = "r"; break; case XawtextAppend: @@ -1453,9 +1457,9 @@ InitStringOrFile(AsciiSrcObject src, Bool newString) src->ascii_src.is_tempfile = True; } else { -/* O_NOFOLLOW is a FreeBSD & Linux extension */ +/* O_NOFOLLOW was a FreeBSD & Linux extension, now adopted by POSIX */ #ifdef O_NOFOLLOW - open_mode = O_RDWR | O_NOFOLLOW; + open_mode = O_RDWR | O_NOFOLLOW | O_CLOEXEC; #else open_mode = O_RDWR; /* unsafe; subject to race conditions */ #endif /* O_NOFOLLOW */ diff --git a/src/MultiSrc.c b/src/MultiSrc.c index a9e84ba..5544b8b 100644 --- a/src/MultiSrc.c +++ b/src/MultiSrc.c @@ -75,6 +75,10 @@ in this Software without prior written authorization from The Open Group. #include #include +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif + #define MAGIC_VALUE ((XawTextPosition)-1) #define streq(a, b) (strcmp((a), (b)) == 0) @@ -1089,7 +1093,7 @@ WriteToFile(String string, String name) int fd; Bool result = True; - if ((fd = creat(name, 0666)) == -1) + if ((fd = open(name, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0666)) == -1) return (False); if (write(fd, string, strlen(string)) == -1) @@ -1215,7 +1219,7 @@ InitStringOrFile(MultiSrcObject src, Bool newString) XtErrorMsg("NoFile", "multiSourceCreate", "XawError", "Creating a read only disk widget and no file specified.", NULL, 0); - open_mode = O_RDONLY; + open_mode = O_RDONLY | O_CLOEXEC; fdopen_mode = "r"; break; case XawtextAppend: @@ -1225,9 +1229,9 @@ InitStringOrFile(MultiSrcObject src, Bool newString) src->multi_src.is_tempfile = True; } else { -/* O_NOFOLLOW is a BSD & Linux extension */ +/* O_NOFOLLOW was a FreeBSD & Linux extension, now adopted by POSIX */ #ifdef O_NOFOLLOW - open_mode = O_RDWR | O_NOFOLLOW; + open_mode = O_RDWR | O_NOFOLLOW | O_CLOEXEC; #else open_mode = O_RDWR; /* unsafe; subject to race conditions */ #endif diff --git a/src/TextPop.c b/src/TextPop.c index 9bf35ee..499c340 100644 --- a/src/TextPop.c +++ b/src/TextPop.c @@ -61,6 +61,12 @@ in this Software without prior written authorization from The Open Group. #include #include "XawI18n.h" +#ifdef O_CLOEXEC +#define FOPEN_CLOEXEC "e" +#else +#define FOPEN_CLOEXEC "" +#endif + static _Xconst char* INSERT_FILE = "Enter Filename:"; static _Xconst char* SEARCH_LABEL_1 = "Use to change fields."; static _Xconst char* SEARCH_LABEL_2 = "Use ^q for ."; @@ -305,7 +311,8 @@ InsertFileNamed(Widget tw, String str) XawTextBlock text; XawTextPosition pos; - if (str == NULL || strlen(str) == 0 || (file = fopen(str, "r")) == NULL) + if (str == NULL || strlen(str) == 0 || + (file = fopen(str, "r" FOPEN_CLOEXEC)) == NULL) return (False); pos = XawTextGetInsertionPoint(tw); -- cgit v1.2.3