//! Parts of the code below are Copyright (c) 2023, Conrad Kleinespel et al
//!
//! This module contains code that was originally written by Conrad Kleinespel for the rpassword
//! crate. No copyright notices were found in the original code.
//!
//! See:
//!
//! Most code was replaced and so is no longer a derived work; work that we kept:
//!
//! - the "HiddenInput" struct and implementation, with changes:
//! * replaced occurrences of explicit 'i32' and 'c_int' with RawFd
//! * open the TTY ourselves to mitigate Linux CVE-2023-2002
//! - the general idea of a "SafeString" type that clears its memory
//! (although much more robust than in the original code)
use std::ffi::c_void;
use std::io::{self, ErrorKind};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd};
use std::path::PathBuf;
use std::time::{Duration, Instant};
use std::{fs, mem};
use libc::{
ECHO, ECHONL, ICANON, SIGALRM, SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGTSTP, SIGTTIN, SIGTTOU,
TCSANOW, VEOF, VERASE, VKILL, tcsetattr, termios,
};
use crate::cutils::{cerr, safe_isatty};
use crate::pam::{PamError, PamResult, askpass};
use crate::system::signal::{
self, SignalHandler, SignalHandlerBehavior, SignalsState, exit_with_signal,
};
use crate::system::wait::{Wait, WaitError, WaitOptions};
use super::securemem::PamBuffer;
struct HiddenInput<'a> {
tty: BorrowedFd<'a>,
term_orig: termios,
}
impl HiddenInput<'_> {
fn new(tty: BorrowedFd) -> io::Result {
// Make two copies of the terminal settings. The first one will be modified
// and the second one will act as a backup for when we want to set the
// terminal back to its original state.
let mut term = safe_tcgetattr(tty)?;
let term_orig = safe_tcgetattr(tty)?;
// Hide the password. This is what makes this function useful.
term.c_lflag &= !ECHO;
// But don't hide the NL character when the user hits ENTER.
term.c_lflag |= ECHONL;
// Disable canonical mode to read character by character when pwfeedback is enabled.
term.c_lflag &= !ICANON;
// Save the settings for now.
// SAFETY: we are passing tcsetattr a valid file descriptor and pointer-to-struct
cerr(unsafe { tcsetattr(tty.as_raw_fd(), TCSANOW, &term) })?;
Ok(HiddenInput { tty, term_orig })
}
}
impl Drop for HiddenInput<'_> {
fn drop(&mut self) {
// Set the the mode back to normal
// SAFETY: we are passing tcsetattr a valid file descriptor and pointer-to-struct
unsafe {
tcsetattr(self.tty.as_raw_fd(), TCSANOW, &self.term_orig);
}
}
}
fn safe_tcgetattr(tty: impl AsFd) -> io::Result {
let mut term = mem::MaybeUninit::::uninit();
// SAFETY: we are passing tcgetattr a pointer to valid memory
cerr(unsafe { ::libc::tcgetattr(tty.as_fd().as_raw_fd(), term.as_mut_ptr()) })?;
// SAFETY: if the previous call was a success, `tcgetattr` has initialized `term`
Ok(unsafe { term.assume_init() })
}
fn erase_feedback(sink: &mut dyn io::Write, i: usize) {
const BACKSPACE: u8 = 0x08;
for _ in 0..i {
if sink.write(&[BACKSPACE, b' ', BACKSPACE]).is_err() {
return;
}
}
// Erase everyting after the cursor to make sure we don't leak the password
// size through the amount of spaces that are shown if you select the line
// containing the password in your terminal emulator.
// CSI n K: Erase in Line, 0 = erase from cursor to end of line
let _ = sink.write(b"\x1b[0K");
}
#[derive(Clone)]
pub(super) enum Hidden {
No,
Yes(T),
WithFeedback(T),
}
/// Prompt for a password while optionally showing feedback to the user.
fn prompt_password(
source: BorrowedFd<'_>,
sink: &mut dyn io::Write,
prompt: &str,
timeout: Option,
hidden: Hidden<()>,
) -> PamResult {
'getpass: loop {
let hide_input = match hidden.clone() {
// If input is not a tty, we can't hide feedback.
_ if !safe_isatty(source) => Hidden::No,
Hidden::No => Hidden::No,
Hidden::Yes(()) => Hidden::Yes(HiddenInput::new(source)?),
Hidden::WithFeedback(()) => Hidden::WithFeedback(HiddenInput::new(source)?),
};
let mut reader = TimeoutRead::new(source, timeout);
// Write prompt after hiding input to ensure the prompt shows after resuming sudo-rs if it
// was started in the background using & rather than -b.
write_unbuffered(sink, prompt.as_bytes())?;
let handlers = {
// Creating a new SignalsState is fine here as we are guaranteed to restore the signals
// again before returning.
let original_signals: &mut SignalsState = &mut SignalsState::save().unwrap();
[
SIGALRM, SIGINT, SIGHUP, SIGQUIT, SIGTERM, SIGTSTP, SIGTTIN, SIGTTOU,
]
.map(|signal| {
SignalHandler::register(
signal,
SignalHandlerBehavior::StorePending,
original_signals,
)
.unwrap()
})
};
let res = read_unbuffered(&mut reader, sink, &hide_input);
drop(hide_input);
drop(handlers);
while let Some(signal) = signal::take_first_pending() {
match signal {
SIGTSTP | SIGTTIN | SIGTTOU => {
crate::system::kill(crate::system::Process::process_id(), signal)?;
continue 'getpass;
}
_ => {
exit_with_signal(signal).unwrap();
}
}
}
return res;
}
}
/// Heuristically determine the length of the final (potentially incomplete) UTF8 sequence
fn last_char_size(slice: &[u8]) -> usize {
let start = |byte| byte & 0b1100_0000 == 0b1100_0000;
let trail = |byte| byte & 0b1100_0000 == 0b1000_0000;
match slice {
[] => 0,
[.., a, b] if start(a) && trail(b) => 2,
[.., a, b, c] if start(a) && [b, c].into_iter().all(trail) => 3,
[.., a, b, c, d] if start(a) && [b, c, d].into_iter().all(trail) => 4,
_ => 1,
}
}
/// Reads a password from the given file descriptor while optionally showing feedback to the user.
fn read_unbuffered(
source: &mut TimeoutRead<'_>,
sink: &mut dyn io::Write,
hide_input: &Hidden,
) -> PamResult {
struct Bullets<'a> {
visible_len: Option,
sink: &'a mut dyn io::Write,
}
const BULLET: &[u8] = b"*";
impl Bullets<'_> {
fn push(&mut self) {
if let Some(ref mut len) = self.visible_len {
let _ = self.sink.write(BULLET);
*len += 1;
}
}
fn pop(&mut self) {
match self.visible_len {
Some(ref mut len) if *len > 0 => {
erase_feedback(self.sink, 1);
*len -= 1;
}
_ => {}
}
}
fn clear(&mut self) {
if let Some(ref mut len) = self.visible_len {
erase_feedback(self.sink, *len);
*len = 0;
}
}
}
// Ensure we erase the password feedback no matter how we exit read_unbuffered
impl Drop for Bullets<'_> {
fn drop(&mut self) {
self.clear();
let _ = self.sink.write(b"\n");
}
}
let mut feedback = Bullets {
visible_len: matches!(hide_input, Hidden::WithFeedback(_)).then_some(0),
sink,
};
let mut password = PamBuffer::default();
let mut pw_len = 0;
loop {
let read_byte = match source.read_byte() {
Ok(Some(byte)) => byte,
Ok(None) => break,
Err(err) if err.kind() == io::ErrorKind::TimedOut => return Err(PamError::TimedOut),
Err(err) => return Err(PamError::IoError(err)),
};
if read_byte == b'\n' || read_byte == b'\r' {
return Ok(password);
}
if let Hidden::Yes(input) | Hidden::WithFeedback(input) = hide_input {
if read_byte == input.term_orig.c_cc[VEOF] {
break;
}
if read_byte == input.term_orig.c_cc[VERASE] {
feedback.pop();
let chunk = last_char_size(&password[..pw_len]);
password[pw_len - chunk..pw_len].fill(0);
pw_len -= chunk;
continue;
}
if read_byte == input.term_orig.c_cc[VKILL] {
feedback.clear();
password.fill(0);
pw_len = 0;
continue;
}
if read_byte == b'\t' && feedback.visible_len.is_some() {
feedback.clear();
feedback.visible_len = None;
let _ = feedback.sink.write(b"(no echo)");
continue;
}
}
if let Some(dest) = password.get_mut(pw_len) {
*dest = read_byte;
pw_len += 1;
if last_char_size(&password[..pw_len]) == 1 {
feedback.push();
}
} else {
return Err(PamError::IncorrectPasswordAttempt);
}
}
if pw_len == 0 {
// In case of EOF or Ctrl-D we don't want to ask for a password a second
// time, so return an error.
Err(PamError::NoPasswordProvided)
} else {
Ok(password)
}
}
/// Write something and immediately flush
fn write_unbuffered(sink: &mut dyn io::Write, text: &[u8]) -> io::Result<()> {
sink.write_all(text)?;
sink.flush()
}
struct TimeoutRead<'a> {
timeout_at: Option,
fd: BorrowedFd<'a>,
}
impl<'a> TimeoutRead<'a> {
fn new(fd: BorrowedFd<'a>, timeout: Option) -> TimeoutRead<'a> {
TimeoutRead {
timeout_at: timeout.map(|timeout| Instant::now() + timeout),
fd,
}
}
}
impl TimeoutRead<'_> {
fn read_byte(&mut self) -> io::Result