Skip to content

Commit c2eae70

Browse files
author
Vasili Novikov
committed
Make PositionMonitor safe by using checked overflowing operations
1 parent 4cfb3ce commit c2eae70

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

Diff for: intel-sgx/enclave-runner/src/usercalls/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use tokio::io::{AsyncRead, AsyncWrite};
3131
use tokio::stream::Stream as TokioStream;
3232
use tokio::sync::{broadcast, mpsc as async_mpsc, oneshot, Semaphore};
3333
use fortanix_sgx_abi::*;
34-
use ipc_queue::{self, DescriptorGuard, Identified, QueueEvent, WritePosition};
34+
use ipc_queue::{self, DescriptorGuard, Identified, QueueEvent};
35+
use ipc_queue::position::WritePosition;
3536
use sgxs::loader::Tcs as SgxsTcs;
3637

3738
use crate::loader::{EnclavePanic, ErasedTcs};

Diff for: ipc-queue/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use {
2929
mod fifo;
3030
mod interface_sync;
3131
mod interface_async;
32-
mod position;
32+
pub mod position;
3333
#[cfg(test)]
3434
mod test_support;
3535

Diff for: ipc-queue/src/position.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::sync::atomic::Ordering;
1313
/// read to/from the queue. This is useful in case we want to know whether or
1414
/// not a particular value written to the queue has been read.
1515
pub struct PositionMonitor<T: 'static> {
16-
read_epoch: Arc<AtomicU64>,
17-
fifo: Fifo<T>,
16+
pub(crate) read_epoch: Arc<AtomicU64>,
17+
pub(crate) fifo: Fifo<T>,
1818
}
1919

2020
/// A read position in a queue.
@@ -27,7 +27,10 @@ impl<T> PositionMonitor<T> {
2727
pub fn read_position(&self) -> ReadPosition {
2828
let current = self.fifo.current_offsets(Ordering::Relaxed);
2929
let read_epoch = self.read_epoch.load(Ordering::Relaxed);
30-
ReadPosition(((read_epoch as u64) << 32) | (current.read_offset() as u64))
30+
let read_epoch_shifted = read_epoch
31+
.checked_shl(32)
32+
.expect("Reading from position of over 2^32 (2 to the power of 32). This is unsupported.");
33+
ReadPosition(read_epoch_shifted | (current.read_offset() as u64))
3134
}
3235

3336
pub fn write_position(&self) -> WritePosition {
@@ -36,7 +39,10 @@ impl<T> PositionMonitor<T> {
3639
if current.read_high_bit() != current.write_high_bit() {
3740
write_epoch += 1;
3841
}
39-
WritePosition(((write_epoch as u64) << 32) | (current.write_offset() as u64))
42+
let write_epoch_shifted = write_epoch
43+
.checked_shl(32)
44+
.expect("Writing to position of over 2^32 (2 to the power of 32). This is unsupported.");
45+
WritePosition(write_epoch_shifted | (current.write_offset() as u64))
4046
}
4147
}
4248

@@ -62,4 +68,4 @@ impl ReadPosition {
6268
}
6369
true
6470
}
65-
}
71+
}

0 commit comments

Comments
 (0)