Skip to content

Commit bfdbda2

Browse files
authored
Merge pull request from GHSA-2r3c-m6v7-9354
Use uid instead of username for storing session files
2 parents 9098406 + 6ce50df commit bfdbda2

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/sudo/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ fn sudo_process() -> Result<(), Error> {
9292
SudoAction::RemoveTimestamp => {
9393
let user = resolve_current_user()?;
9494
let mut record_file =
95-
SessionRecordFile::open_for_user(&user.name, Duration::seconds(0))?;
95+
SessionRecordFile::open_for_user(user.uid, Duration::seconds(0))?;
9696
record_file.reset()?;
9797
Ok(())
9898
}
9999
SudoAction::ResetTimestamp => {
100100
if let Some(scope) = RecordScope::for_process(&Process::new()) {
101101
let user = resolve_current_user()?;
102102
let mut record_file =
103-
SessionRecordFile::open_for_user(&user.name, Duration::seconds(0))?;
103+
SessionRecordFile::open_for_user(user.uid, Duration::seconds(0))?;
104104
record_file.disable(scope, None)?;
105105
}
106106
Ok(())

src/sudo/pipeline.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<Policy: PolicyPlugin, Auth: AuthPlugin> Pipeline<Policy, Auth> {
133133
context.use_session_records,
134134
scope,
135135
context.current_user.uid,
136-
&context.current_user.name,
136+
context.current_user.uid,
137137
prior_validity,
138138
);
139139
self.authenticator.init(context)?;
@@ -201,7 +201,7 @@ fn determine_auth_status(
201201
use_session_records: bool,
202202
record_for: Option<RecordScope>,
203203
auth_uid: UserId,
204-
current_user: &str,
204+
current_user: UserId,
205205
prior_validity: Duration,
206206
) -> AuthStatus {
207207
if !must_policy_authenticate {
@@ -232,13 +232,13 @@ fn determine_auth_status(
232232
}
233233
}
234234

235-
struct AuthStatus<'a> {
235+
struct AuthStatus {
236236
must_authenticate: bool,
237-
record_file: Option<SessionRecordFile<'a>>,
237+
record_file: Option<SessionRecordFile>,
238238
}
239239

240-
impl<'a> AuthStatus<'a> {
241-
fn new(must_authenticate: bool, record_file: Option<SessionRecordFile<'a>>) -> AuthStatus<'a> {
240+
impl AuthStatus {
241+
fn new(must_authenticate: bool, record_file: Option<SessionRecordFile>) -> AuthStatus {
242242
AuthStatus {
243243
must_authenticate,
244244
record_file,

src/system/timestamp.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ const SIZE_OF_BOOL: i64 = std::mem::size_of::<BoolStorage>() as i64;
3535
const MOD_OFFSET: i64 = SIZE_OF_TS + SIZE_OF_BOOL;
3636

3737
#[derive(Debug)]
38-
pub struct SessionRecordFile<'u> {
38+
pub struct SessionRecordFile {
3939
file: File,
4040
timeout: Duration,
41-
for_user: &'u str,
41+
for_user: UserId,
4242
}
4343

44-
impl<'u> SessionRecordFile<'u> {
44+
impl SessionRecordFile {
4545
const BASE_PATH: &'static str = "/var/run/sudo-rs/ts";
4646

47-
pub fn open_for_user(user: &'u str, timeout: Duration) -> io::Result<Self> {
47+
pub fn open_for_user(user: UserId, timeout: Duration) -> io::Result<Self> {
4848
let mut path = PathBuf::from(Self::BASE_PATH);
49-
path.push(user);
49+
path.push(user.to_string());
5050
SessionRecordFile::new(user, secure_open_cookie_file(&path)?, timeout)
5151
}
5252

@@ -59,7 +59,7 @@ impl<'u> SessionRecordFile<'u> {
5959
/// Create a new SessionRecordFile from the given i/o stream.
6060
/// Timestamps in this file are considered valid if they were created or
6161
/// updated at most `timeout` time ago.
62-
pub fn new(for_user: &'u str, io: File, timeout: Duration) -> io::Result<Self> {
62+
pub fn new(for_user: UserId, io: File, timeout: Duration) -> io::Result<Self> {
6363
let mut session_records = SessionRecordFile {
6464
file: io,
6565
timeout,
@@ -578,6 +578,8 @@ mod tests {
578578

579579
use crate::system::tests::tempfile;
580580

581+
const TEST_USER_ID: UserId = 1000;
582+
581583
impl SetLength for Cursor<Vec<u8>> {
582584
fn set_len(&mut self, new_len: usize) -> io::Result<()> {
583585
self.get_mut().truncate(new_len);
@@ -714,25 +716,25 @@ mod tests {
714716
// valid header should remain valid
715717
let c = tempfile_with_data(&[0xD0, 0x50, 0x01, 0x00]).unwrap();
716718
let timeout = Duration::seconds(30);
717-
assert!(SessionRecordFile::new("test", c.try_clone().unwrap(), timeout).is_ok());
719+
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
718720
let v = data_from_tempfile(c).unwrap();
719721
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
720722

721723
// invalid headers should be corrected
722724
let c = tempfile_with_data(&[0xAB, 0xBA]).unwrap();
723-
assert!(SessionRecordFile::new("test", c.try_clone().unwrap(), timeout).is_ok());
725+
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
724726
let v = data_from_tempfile(c).unwrap();
725727
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
726728

727729
// empty header should be filled in
728730
let c = tempfile_with_data(&[]).unwrap();
729-
assert!(SessionRecordFile::new("test", c.try_clone().unwrap(), timeout).is_ok());
731+
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
730732
let v = data_from_tempfile(c).unwrap();
731733
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
732734

733735
// invalid version should reset file
734736
let c = tempfile_with_data(&[0xD0, 0x50, 0xAB, 0xBA, 0x0, 0x0]).unwrap();
735-
assert!(SessionRecordFile::new("test", c.try_clone().unwrap(), timeout).is_ok());
737+
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
736738
let v = data_from_tempfile(c).unwrap();
737739
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
738740
}
@@ -741,7 +743,8 @@ mod tests {
741743
fn can_create_and_update_valid_file() {
742744
let timeout = Duration::seconds(30);
743745
let c = tempfile_with_data(&[]).unwrap();
744-
let mut srf = SessionRecordFile::new("test", c.try_clone().unwrap(), timeout).unwrap();
746+
let mut srf =
747+
SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).unwrap();
745748
let tty_scope = RecordScope::Tty {
746749
tty_device: 0,
747750
session_pid: 0,

0 commit comments

Comments
 (0)