Skip to content

Commit f2f3409

Browse files
3v1n0millert
authored andcommitted
plugins/auth/pam: expose sudo prompt to PAM via SUDO_PROMPT env
Some PAM implementations do not use PAM conversations and instead present their own native client UI. In those cases, modules cannot infer sudo's custom prompt text from the conversation callback path. Set SUDO_PROMPT in the PAM environment before pam_authenticate(), so modules that render their own UI can combine: - PAM service identity (from the PAM transaction) - sudo's resolved prompt string (SUDO_PROMPT) to display a consistent, context-aware prompt. To avoid stale state, clear SUDO_PROMPT before authentication starts and clear it again after pam_authenticate() returns. Allowing PAM modules in the stack to read (and potentially change) SUDO_PROMPT does not introduce a new trust boundary concern: PAM modules already control the conversation flow and can provide their own prompt text/messages through the standard PAM conversation mechanism.
1 parent cb6ea72 commit f2f3409

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

  • plugins/sudoers/auth

‎plugins/sudoers/auth/pam.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#endif
5757

5858
#include <sudoers.h>
59+
#include <sudo_compat.h>
5960
#include "sudo_auth.h"
6061

6162
/* Only OpenPAM and Linux PAM use const qualifiers. */
@@ -313,12 +314,52 @@ sudo_pam_verify(const struct sudoers_context *ctx, struct passwd *pw,
313314
}
314315
}
315316

317+
/*
318+
* Clear SUDO_PROMPT before authentication to avoid stale values
319+
* from previous PAM transactions.
320+
*/
321+
rc = pam_putenv(pamh, "SUDO_PROMPT");
322+
if (rc != PAM_SUCCESS && rc != PAM_BAD_ITEM) {
323+
const char *errstr = sudo_pam_strerror(pamh, rc);
324+
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
325+
"pam_putenv(pamh, SUDO_PROMPT): %s", errstr);
326+
debug_return_int(AUTH_ERROR);
327+
}
328+
329+
if (ctx->user.prompt || def_passprompt_override) {
330+
char *sudo_prompt = NULL;
331+
332+
if (asprintf(&sudo_prompt, "SUDO_PROMPT=%s", def_prompt) != -1) {
333+
rc = pam_putenv(pamh, sudo_prompt);
334+
free(sudo_prompt);
335+
336+
if (rc != PAM_SUCCESS) {
337+
const char *errstr = sudo_pam_strerror(pamh, rc);
338+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
339+
"pam_putenv(pamh, SUDO_PROMPT, %s): %s",
340+
def_prompt, errstr);
341+
}
342+
} else {
343+
const char *errstr = strerror(errno);
344+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
345+
"unable to set SUDO_PROMPT: %s", errstr);
346+
}
347+
}
348+
316349
/* PAM_SILENT prevents the authentication service from generating output. */
317350
*pam_status = pam_authenticate(pamh, def_pam_silent ? PAM_SILENT : 0);
318351

319352
/* Restore def_prompt, the passed-in prompt may be freed later. */
320353
def_prompt = PASSPROMPT;
321354

355+
/* Clear the SUDO_PROMPT PAM environment variable. */
356+
rc = pam_putenv(pamh, "SUDO_PROMPT");
357+
if (rc != PAM_SUCCESS) {
358+
const char *errstr = sudo_pam_strerror(pamh, rc);
359+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
360+
"pam_putenv(pamh, SUDO_PROMPT): %s", errstr);
361+
}
362+
322363
/* Restore KRB5CCNAME to its original value. */
323364
if (envccname == NULL && sudo_unsetenv("KRB5CCNAME") != 0) {
324365
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,

0 commit comments

Comments
 (0)