From 151e03fb5b51e3d4efd69c37d71bf62c91d3a7ba Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 6 May 2016 14:12:08 -0600 Subject: [PATCH] Instead of using stat(2) to see if the admin flag file exists and creating it if not, just try to create the file and treat EEXIST as a non-error. Coverity CID 104121. --- plugins/sudoers/sudoers.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/plugins/sudoers/sudoers.c b/plugins/sudoers/sudoers.c index 34631a260..d95ba8067 100644 --- a/plugins/sudoers/sudoers.c +++ b/plugins/sudoers/sudoers.c @@ -1262,9 +1262,8 @@ find_editor(int nfiles, char **files, int *argc_out, char ***argv_out) static int create_admin_success_flag(void) { - struct stat statbuf; char flagfile[PATH_MAX]; - int len, fd = -1; + int len, rval = -1; debug_decl(create_admin_success_flag, SUDOERS_DEBUG_PLUGIN) /* Check whether the user is in the admin group. */ @@ -1279,15 +1278,14 @@ create_admin_success_flag(void) /* Create admin flag file if it doesn't already exist. */ if (set_perms(PERM_USER)) { - if (stat(flagfile, &statbuf) != 0) { - fd = open(flagfile, O_CREAT|O_WRONLY|O_EXCL, 0644); - if (fd != -1) - close(fd); - } + int fd = open(flagfile, O_CREAT|O_WRONLY|O_NONBLOCK|O_EXCL, 0644); + rval = fd != -1 || errno == EEXIST; + if (fd != -1) + close(fd); if (!restore_perms()) - debug_return_int(-1); + rval = -1; } - debug_return_int(fd != -1); + debug_return_int(rval); } #else /* !USE_ADMIN_FLAG */ static int -- 2.50.1