From: Alejandro Colomar Date: Fri, 17 May 2024 11:40:58 +0000 (+0200) Subject: src/useradd.c: Add fmkstemp() to fix file-descriptor leak X-Git-Tag: 4.15.2~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e7d1508e076bbf4053faacc0370c6fe43d9c8f04;p=thirdparty%2Fshadow.git src/useradd.c: Add fmkstemp() to fix file-descriptor leak This function creates a temporary file, and returns a FILE pointer to it. This avoids dealing with both a file descriptor and a FILE pointer, and correctly deallocating the resources on error. The code before this patch was leaking the file descriptor if fdopen(3) failed. Reviewed-by: Iker Pedrosa Signed-off-by: Alejandro Colomar --- diff --git a/src/useradd.c b/src/useradd.c index ad2676c1c..e0238457d 100644 --- a/src/useradd.c +++ b/src/useradd.c @@ -238,6 +238,9 @@ static void create_home (void); static void create_mail (void); static void check_uid_range(int rflg, uid_t user_id); +static FILE *fmkstemp(char *template); + + /* * fail_exit - undo as much as possible */ @@ -524,7 +527,6 @@ static void show_defaults (void) */ static int set_defaults (void) { - int ofd; int ret = -1; bool out_group = false; bool out_groups = false; @@ -582,15 +584,7 @@ static int set_defaults (void) /* * Create a temporary file to copy the new output to. */ - ofd = mkstemp (new_file); - if (-1 == ofd) { - fprintf (stderr, - _("%s: cannot create new defaults file\n"), - Prog); - goto err_free_def; - } - - ofp = fdopen (ofd, "w"); + ofp = fmkstemp(new_file); if (NULL == ofp) { fprintf (stderr, _("%s: cannot open new defaults file\n"), @@ -2752,3 +2746,23 @@ int main (int argc, char **argv) return E_SUCCESS; } + +static FILE * +fmkstemp(char *template) +{ + int fd; + FILE *fp; + + fd = mkstemp(template); + if (fd == -1) + return NULL; + + fp = fdopen(fd, "w"); + if (fp == NULL) { + close(fd); + unlink(template); + return NULL; + } + + return fp; +}