Skip to content

Commit ac566d2

Browse files
committed
memory_encryption: prevent setting the encryption bit in PhysAddr
1 parent ba9ec73 commit ac566d2

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/addr.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
//! Physical and virtual addresses manipulation
22
3+
#[cfg(feature = "memory_encryption")]
4+
use crate::structures::mem_encrypt::ENC_BIT_MASK;
5+
use crate::structures::paging::page_table::PageTableLevel;
6+
use crate::structures::paging::{PageOffset, PageTableIndex};
7+
use bit_field::BitField;
38
use core::convert::TryFrom;
49
use core::fmt;
510
#[cfg(feature = "step_trait")]
611
use core::iter::Step;
712
use core::ops::{Add, AddAssign, Sub, SubAssign};
8-
9-
use crate::structures::paging::page_table::PageTableLevel;
10-
use crate::structures::paging::{PageOffset, PageTableIndex};
11-
use bit_field::BitField;
13+
#[cfg(feature = "memory_encryption")]
14+
use core::sync::atomic::Ordering;
1215

1316
const ADDRESS_SPACE_SIZE: u64 = 0x1_0000_0000_0000;
1417

@@ -439,6 +442,8 @@ impl PhysAddr {
439442
/// ## Panics
440443
///
441444
/// This function panics if a bit in the range 52 to 64 is set.
445+
/// If the `memory_encryption` feature is available and has been enabled, this function also
446+
/// panics fails if the encryption bit is manually set in the address.
442447
#[inline]
443448
pub const fn new(addr: u64) -> Self {
444449
// TODO: Replace with .ok().expect(msg) when that works on stable.
@@ -448,12 +453,6 @@ impl PhysAddr {
448453
}
449454
}
450455

451-
/// Creates a new physical address, throwing bits 52..64 away.
452-
#[inline]
453-
pub const fn new_truncate(addr: u64) -> PhysAddr {
454-
PhysAddr(addr % (1 << 52))
455-
}
456-
457456
/// Creates a new physical address, without any checks.
458457
///
459458
/// ## Safety
@@ -467,6 +466,8 @@ impl PhysAddr {
467466
/// Tries to create a new physical address.
468467
///
469468
/// Fails if any bits in the range 52 to 64 are set.
469+
/// If the `memory_encryption` feature is available and has been enabled, this also fails if the
470+
/// encryption bit is manually set in the address.
470471
#[inline]
471472
pub const fn try_new(addr: u64) -> Result<Self, PhysAddrNotValid> {
472473
let p = Self::new_truncate(addr);
@@ -546,6 +547,24 @@ impl PhysAddr {
546547
}
547548
}
548549

550+
#[cfg(feature = "memory_encryption")]
551+
impl PhysAddr {
552+
/// Creates a new physical address, throwing bits 52..64 and the encryption bit away.
553+
#[inline]
554+
pub fn new_truncate(addr: u64) -> PhysAddr {
555+
PhysAddr((addr % (1 << 52)) & !ENC_BIT_MASK.load(Ordering::Relaxed))
556+
}
557+
}
558+
559+
#[cfg(not(feature = "memory_encryption"))]
560+
impl PhysAddr {
561+
/// Creates a new physical address, throwing bits 52..64 away.
562+
#[inline]
563+
pub const fn new_truncate(addr: u64) -> PhysAddr {
564+
PhysAddr(addr % (1 << 52))
565+
}
566+
}
567+
549568
impl fmt::Debug for PhysAddr {
550569
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
551570
f.debug_tuple("PhysAddr")

src/structures/mem_encrypt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::structures::paging::page_table::PHYSICAL_ADDRESS_MASK;
1111
use crate::structures::paging::PageTableFlags;
1212

1313
/// Position of the encryption (C/S) bit in the physical address
14-
static ENC_BIT_MASK: AtomicU64 = AtomicU64::new(0);
14+
pub(crate) static ENC_BIT_MASK: AtomicU64 = AtomicU64::new(0);
1515

1616
/// Is the encryption bit reversed (i.e. its presence denote that the page is _decrypted_ rather
1717
/// than encrypted)

0 commit comments

Comments
 (0)