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