7c0317
From 4b6de608c25a6ffbdb507be958e12f814b43077c Mon Sep 17 00:00:00 2001
7c0317
From: "Todd C. Miller" <Todd.Miller@sudo.ws>
7c0317
Date: Wed, 4 Dec 2019 12:38:22 -0700
7c0317
Subject: [PATCH] Only update the time stamp entry after the approval function
7c0317
 has succeeded. Bug #910
7c0317
7c0317
---
7c0317
 plugins/sudoers/check.c | 59 +++++++++++++++++++----------------------
7c0317
 1 file changed, 27 insertions(+), 32 deletions(-)
7c0317
7c0317
diff --git a/plugins/sudoers/check.c b/plugins/sudoers/check.c
7c0317
index db8e05161..ea1d89085 100644
7c0317
--- a/plugins/sudoers/check.c
7c0317
+++ b/plugins/sudoers/check.c
7c0317
@@ -51,6 +51,7 @@ static bool display_lecture(int);
7c0317
 static struct passwd *get_authpw(int);
7c0317
 
7c0317
 struct getpass_closure {
7c0317
+    int tstat;
7c0317
     void *cookie;
7c0317
     struct passwd *auth_pw;
7c0317
 };
7c0317
@@ -89,27 +90,20 @@ getpass_resume(int signo, void *vclosure)
7c0317
  * or -1 on fatal error.
7c0317
  */
7c0317
 static int
7c0317
-check_user_interactive(int validated, int mode, struct passwd *auth_pw)
7c0317
+check_user_interactive(int validated, int mode, struct getpass_closure *closure)
7c0317
 {
7c0317
     struct sudo_conv_callback cb, *callback = NULL;
7c0317
-    struct getpass_closure closure;
7c0317
-    int status = TS_ERROR;
7c0317
     int ret = -1;
7c0317
     char *prompt;
7c0317
     bool lectured;
7c0317
     debug_decl(check_user_interactive, SUDOERS_DEBUG_AUTH)
7c0317
 
7c0317
-    /* Setup closure for getpass_{suspend,resume} */
7c0317
-    closure.auth_pw = auth_pw;
7c0317
-    closure.cookie = NULL;
7c0317
-    sudo_pw_addref(closure.auth_pw);
7c0317
-
7c0317
     /* Open, lock and read time stamp file if we are using it. */
7c0317
     if (!ISSET(mode, MODE_IGNORE_TICKET)) {
7c0317
 	/* Open time stamp file and check its status. */
7c0317
-	closure.cookie = timestamp_open(user_name, user_sid);
7c0317
-	if (timestamp_lock(closure.cookie, closure.auth_pw))
7c0317
-	    status = timestamp_status(closure.cookie, closure.auth_pw);
7c0317
+	closure->cookie = timestamp_open(user_name, user_sid);
7c0317
+	if (timestamp_lock(closure->cookie, closure->auth_pw))
7c0317
+	    closure->tstat = timestamp_status(closure->cookie, closure->auth_pw);
7c0317
 
7c0317
 	/* Construct callback for getpass function. */
7c0317
 	memset(&cb, 0, sizeof(cb));
7c0317
@@ -120,7 +114,7 @@ check_user_interactive(int validated, int mode, struct passwd *auth_pw)
7c0317
 	callback = &cb;
7c0317
     }
7c0317
 
7c0317
-    switch (status) {
7c0317
+    switch (closure->tstat) {
7c0317
     case TS_FATAL:
7c0317
 	/* Fatal error (usually setuid failure), unsafe to proceed. */
7c0317
 	goto done;
7c0317
@@ -144,32 +138,22 @@ check_user_interactive(int validated, int mode, struct passwd *auth_pw)
7c0317
 	}
7c0317
 
7c0317
 	/* XXX - should not lecture if askpass helper is being used. */
7c0317
-	lectured = display_lecture(status);
7c0317
+	lectured = display_lecture(closure->tstat);
7c0317
 
7c0317
 	/* Expand any escapes in the prompt. */
7c0317
 	prompt = expand_prompt(user_prompt ? user_prompt : def_passprompt,
7c0317
-	    closure.auth_pw->pw_name);
7c0317
+	    closure->auth_pw->pw_name);
7c0317
 	if (prompt == NULL)
7c0317
 	    goto done;
7c0317
 
7c0317
-	ret = verify_user(closure.auth_pw, prompt, validated, callback);
7c0317
+	ret = verify_user(closure->auth_pw, prompt, validated, callback);
7c0317
 	if (ret == true && lectured)
7c0317
 	    (void)set_lectured();	/* lecture error not fatal */
7c0317
 	free(prompt);
7c0317
 	break;
7c0317
     }
7c0317
 
7c0317
-    /*
7c0317
-     * Only update time stamp if user was validated.
7c0317
-     * Failure to update the time stamp is not a fatal error.
7c0317
-     */
7c0317
-    if (ret == true && ISSET(validated, VALIDATE_SUCCESS) && status != TS_ERROR)
7c0317
-	(void)timestamp_update(closure.cookie, closure.auth_pw);
7c0317
 done:
7c0317
-    if (closure.cookie != NULL)
7c0317
-	timestamp_close(closure.cookie);
7c0317
-    sudo_pw_delref(closure.auth_pw);
7c0317
-
7c0317
     debug_return_int(ret);
7c0317
 }
7c0317
 
7c0317
@@ -180,7 +164,7 @@ done:
7c0317
 int
7c0317
 check_user(int validated, int mode)
7c0317
 {
7c0317
-    struct passwd *auth_pw;
7c0317
+    struct getpass_closure closure = { TS_ERROR };
7c0317
     int ret = -1;
7c0317
     bool exempt = false;
7c0317
     debug_decl(check_user, SUDOERS_DEBUG_AUTH)
7c0317
@@ -189,9 +173,9 @@ check_user(int validated, int mode)
7c0317
      * Init authentication system regardless of whether we need a password.
7c0317
      * Required for proper PAM session support.
7c0317
      */
7c0317
-    if ((auth_pw = get_authpw(mode)) == NULL)
7c0317
+    if ((closure.auth_pw = get_authpw(mode)) == NULL)
7c0317
 	goto done;
7c0317
-    if (sudo_auth_init(auth_pw) == -1)
7c0317
+    if (sudo_auth_init(closure.auth_pw) == -1)
7c0317
 	goto done;
7c0317
 
7c0317
     /*
7c0317
@@ -222,15 +206,26 @@ check_user(int validated, int mode)
7c0317
 	}
7c0317
     }
7c0317
 
7c0317
-    ret = check_user_interactive(validated, mode, auth_pw);
7c0317
+    ret = check_user_interactive(validated, mode, &closure);
7c0317
 
7c0317
 done:
7c0317
     if (ret == true) {
7c0317
 	/* The approval function may disallow a user post-authentication. */
7c0317
-	ret = sudo_auth_approval(auth_pw, validated, exempt);
7c0317
+	ret = sudo_auth_approval(closure.auth_pw, validated, exempt);
7c0317
+
7c0317
+	/*
7c0317
+	 * Only update time stamp if user validated and was approved.
7c0317
+	 * Failure to update the time stamp is not a fatal error.
7c0317
+	 */
7c0317
+	if (ret == true && closure.tstat != TS_ERROR) {
7c0317
+	    if (ISSET(validated, VALIDATE_SUCCESS))
7c0317
+		(void)timestamp_update(closure.cookie, closure.auth_pw);
7c0317
+	}
7c0317
     }
7c0317
-    sudo_auth_cleanup(auth_pw);
7c0317
-    sudo_pw_delref(auth_pw);
7c0317
+    timestamp_close(closure.cookie);
7c0317
+    sudo_auth_cleanup(closure.auth_pw);
7c0317
+    if (closure.auth_pw != NULL)
7c0317
+	sudo_pw_delref(closure.auth_pw);
7c0317
 
7c0317
     debug_return_int(ret);
7c0317
 }
7c0317
-- 
7c0317
2.25.1
7c0317