Skip to content

Commit e1b81b7

Browse files
committed
Check the session id when using ppid based timestamping
We already did when using tty based timestamping like is the default.
1 parent eb2934e commit e1b81b7

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/system/timestamp.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl SessionRecordFile {
4242
SessionRecordFile::new(uid, secure_open_cookie_file(&path)?, timeout)
4343
}
4444

45-
const FILE_VERSION: u16 = 1;
45+
const FILE_VERSION: u16 = 2;
4646
const MAGIC_NUM: u16 = 0x50D0;
4747
const VERSION_OFFSET: u64 = Self::MAGIC_NUM.to_le_bytes().len() as u64;
4848
const FIRST_RECORD_OFFSET: u64 =
@@ -332,6 +332,7 @@ pub enum RecordScope {
332332
},
333333
Ppid {
334334
group_pid: ProcessId,
335+
session_pid: ProcessId,
335336
init_time: ProcessCreateTime,
336337
},
337338
}
@@ -353,11 +354,14 @@ impl RecordScope {
353354
}
354355
RecordScope::Ppid {
355356
group_pid,
357+
session_pid,
356358
init_time,
357359
} => {
358360
target.write_all(&[2u8])?;
359361
let b = group_pid.inner().to_le_bytes();
360362
target.write_all(&b)?;
363+
let b = session_pid.inner().to_le_bytes();
364+
target.write_all(&b)?;
361365
init_time.encode(target)?;
362366
}
363367
}
@@ -387,9 +391,13 @@ impl RecordScope {
387391
let mut buf = [0; std::mem::size_of::<libc::pid_t>()];
388392
from.read_exact(&mut buf)?;
389393
let group_pid = libc::pid_t::from_le_bytes(buf);
394+
let mut buf = [0; std::mem::size_of::<libc::pid_t>()];
395+
from.read_exact(&mut buf)?;
396+
let session_pid = libc::pid_t::from_le_bytes(buf);
390397
let init_time = ProcessCreateTime::decode(from)?;
391398
Ok(RecordScope::Ppid {
392399
group_pid: ProcessId::new(group_pid),
400+
session_pid: ProcessId::new(session_pid),
393401
init_time,
394402
})
395403
}
@@ -420,6 +428,7 @@ impl RecordScope {
420428
if let Ok(init_time) = Process::starting_time(WithProcess::Other(parent_pid)) {
421429
Some(RecordScope::Ppid {
422430
group_pid: parent_pid,
431+
session_pid: process.session_id,
423432
init_time,
424433
})
425434
} else {
@@ -610,6 +619,7 @@ mod tests {
610619
let ppid_sample = SessionRecord::new(
611620
RecordScope::Ppid {
612621
group_pid: ProcessId::new(42),
622+
session_pid: ProcessId::new(43),
613623
init_time: ProcessCreateTime::new(151, 0),
614624
},
615625
UserId::new(123),
@@ -644,6 +654,7 @@ mod tests {
644654
assert!(!tty_sample.matches(
645655
&RecordScope::Ppid {
646656
group_pid: ProcessId::new(42),
657+
session_pid: ProcessId::new(43),
647658
init_time
648659
},
649660
&auth_user_from_uid(675),
@@ -698,29 +709,29 @@ mod tests {
698709
#[test]
699710
fn session_record_file_header_checks() {
700711
// valid header should remain valid
701-
let c = tempfile_with_data(&[0xD0, 0x50, 0x01, 0x00]).unwrap();
712+
let c = tempfile_with_data(&[0xD0, 0x50, 0x02, 0x00]).unwrap();
702713
let timeout = Duration::from_secs(30);
703714
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
704715
let v = data_from_tempfile(c).unwrap();
705-
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
716+
assert_eq!(&v[..], &[0xD0, 0x50, 0x02, 0x00]);
706717

707718
// invalid headers should be corrected
708719
let c = tempfile_with_data(&[0xAB, 0xBA]).unwrap();
709720
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
710721
let v = data_from_tempfile(c).unwrap();
711-
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
722+
assert_eq!(&v[..], &[0xD0, 0x50, 0x02, 0x00]);
712723

713724
// empty header should be filled in
714725
let c = tempfile_with_data(&[]).unwrap();
715726
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
716727
let v = data_from_tempfile(c).unwrap();
717-
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
728+
assert_eq!(&v[..], &[0xD0, 0x50, 0x02, 0x00]);
718729

719730
// invalid version should reset file
720731
let c = tempfile_with_data(&[0xD0, 0x50, 0xAB, 0xBA, 0x0, 0x0]).unwrap();
721732
assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok());
722733
let v = data_from_tempfile(c).unwrap();
723-
assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]);
734+
assert_eq!(&v[..], &[0xD0, 0x50, 0x02, 0x00]);
724735
}
725736

726737
#[test]
@@ -760,6 +771,6 @@ mod tests {
760771

761772
// after all this the data should be just an empty header
762773
let data = data_from_tempfile(c).unwrap();
763-
assert_eq!(&data, &[0xD0, 0x50, 0x01, 0x00]);
774+
assert_eq!(&data, &[0xD0, 0x50, 0x02, 0x00]);
764775
}
765776
}

0 commit comments

Comments
 (0)