Skip to content

Commit 4fbe374

Browse files
Pingasmastersquellbjorn3
authored
fix: passwd_tries=0 can result in unlimited password tries because of… (#1313)
Fix potential integer underflow when `passwd_tries` = 0 --------- Co-authored-by: Marc Schoolderman <marc@tweedegolf.com> Co-authored-by: Marc R. Schoolderman <info@squell.net> Co-authored-by: bjorn3 <17426603+bjorn3@users.noreply.github.com>
1 parent 0e332b0 commit 4fbe374

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/common/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum Error {
2828
Options(String),
2929
Pam(PamError),
3030
Io(Option<PathBuf>, std::io::Error),
31-
MaxAuthAttempts(usize),
31+
MaxAuthAttempts(u16),
3232
PathValidation(PathBuf),
3333
StringValidation(String),
3434
#[cfg(feature = "apparmor")]

src/sudo/pam.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ pub(super) fn attempt_authenticate(
9494
pam: &mut PamContext,
9595
auth_user: &str,
9696
non_interactive: bool,
97-
mut max_tries: u16,
97+
max_tries: u16,
9898
) -> Result<(), Error> {
99+
// Reject zero upfront so we don't ask for a password once when max_tries is 0.
100+
if max_tries == 0 {
101+
return Err(Error::MaxAuthAttempts(0));
102+
}
103+
99104
let mut current_try = 0;
100105
loop {
101106
current_try += 1;
@@ -110,8 +115,7 @@ pub(super) fn attempt_authenticate(
110115

111116
// there was an authentication error, we can retry
112117
Err(PamError::Pam(PamErrorType::AuthError | PamErrorType::ConversationError)) => {
113-
max_tries -= 1;
114-
if max_tries == 0 {
118+
if current_try >= max_tries {
115119
return Err(Error::MaxAuthAttempts(current_try));
116120
} else if non_interactive {
117121
return Err(Error::InteractionRequired);

0 commit comments

Comments
 (0)