Skip to content

add InvalidStarSegmentSelectors #317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/registers/model_specific.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ mod x86_64 {
use crate::PrivilegeLevel;
use bit_field::BitField;
use core::convert::TryInto;
use core::fmt;

impl Msr {
/// Read 64 bits msr register.
Expand Down Expand Up @@ -367,21 +368,21 @@ mod x86_64 {
ss_sysret: SegmentSelector,
cs_syscall: SegmentSelector,
ss_syscall: SegmentSelector,
) -> Result<(), &'static str> {
) -> Result<(), InvalidStarSegmentSelectors> {
if cs_sysret.0 - 16 != ss_sysret.0 - 8 {
return Err("Sysret CS and SS is not offset by 8.");
return Err(InvalidStarSegmentSelectors::SysretOffset);
}

if cs_syscall.0 != ss_syscall.0 - 8 {
return Err("Syscall CS and SS is not offset by 8.");
return Err(InvalidStarSegmentSelectors::SyscallOffset);
}

if ss_sysret.rpl() != PrivilegeLevel::Ring3 {
return Err("Sysret's segment must be a Ring3 segment.");
return Err(InvalidStarSegmentSelectors::SysretPrivilegeLevel);
}

if ss_syscall.rpl() != PrivilegeLevel::Ring0 {
return Err("Syscall's segment must be a Ring0 segment.");
return Err(InvalidStarSegmentSelectors::SyscallPrivilegeLevel);
}

unsafe { Self::write_raw(ss_sysret.0 - 8, cs_syscall.0) };
Expand All @@ -390,6 +391,29 @@ mod x86_64 {
}
}

#[derive(Debug)]
pub enum InvalidStarSegmentSelectors {
SysretOffset,
SyscallOffset,
SysretPrivilegeLevel,
SyscallPrivilegeLevel,
}

impl fmt::Display for InvalidStarSegmentSelectors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::SysretOffset => write!(f, "Sysret CS and SS are not offset by 8."),
Self::SyscallOffset => write!(f, "Syscall CS and SS are not offset by 8."),
Self::SysretPrivilegeLevel => {
write!(f, "Sysret's segment must be a Ring3 segment.")
}
Self::SyscallPrivilegeLevel => {
write!(f, "Syscall's segment must be a Ring0 segment.")
}
}
}
}

impl LStar {
/// Read the current LStar register.
/// This holds the target RIP of a syscall.
Expand Down