Skip to content
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

Lock memory by default throughout #13

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ build-my-react-js = "0.1.7"
inline_colorization = "0.1.6"

[dependencies]
libc = "0.2.169"
rocket = { version = "0.5.1", features = ["json", "tls"] }
serde = { version = "1.0.214", features = ["derive"] }
serde_json = "1.0.133"
Expand Down
34 changes: 28 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ extern crate rocket;
use aes::Aes256;
use fpe::ff1::{BinaryNumeralString, FF1};
use rocket::{
form::Form, fs::{relative, FileServer}, http::{Cookie, CookieJar, SameSite, Status}, request::{self, FromRequest}, response::status, time::Duration, tokio::sync::Mutex, tokio::sync::oneshot, tokio::sync::oneshot::Sender, tokio::sync::oneshot::Receiver, Request};
form::Form, fs::{relative, FileServer}, http::{Cookie, CookieJar, SameSite, Status}, request::{self, FromRequest}, response::status, time::Duration, tokio::sync::Mutex, tokio::sync::oneshot, tokio::sync::oneshot::Sender, Request};

use rocket::serde::{json::Json, Serialize};
use rocket::{Rocket, Build};

use lazy_static::lazy_static;
use serde::Deserialize;
Expand All @@ -24,6 +25,8 @@ use pwhash::bcrypt;

use redb::{Database, ReadableTable, TableDefinition};

use libc::{mlockall, MCL_CURRENT, MCL_FUTURE, MCL_ONFAULT};

const THE_DATABASE: &str = "ratchet_db.redb";
// XXX: this has to be less than i64::MAX.
const AUTH_TIMEOUT_MINUTES: u64 = 30;
Expand Down Expand Up @@ -107,7 +110,10 @@ lazy_static! {
static ref PERM_DB_KEY: Arc<&'static [u8; 32]> = {
// SAFETY: DB_KEY must not be mutated after init, see rtp_take_key
// if anyone else touches DB_KEY and not PERM_DB_KEY, slap them.
unsafe { Arc::new(&DB_KEY) }
unsafe {
#[allow(static_mut_refs)]
Arc::new(&DB_KEY)
}
};
}
/// SAFETY: This must only be called once prior to operation of any
Expand All @@ -118,6 +124,7 @@ lazy_static! {
pub unsafe fn rtp_take_key (key: &String) {
// SAFETY: Nothing else writes the DB_KEY.
unsafe {
#[allow(static_mut_refs)]
DB_KEY.iter_mut()
.enumerate()
.for_each(|(i,k)| *k = *key.as_bytes()
Expand Down Expand Up @@ -331,7 +338,7 @@ async fn api_long_poll(_valid: RatchetApiKey) -> String {
pins.push(tx);
}
match rx.await {
Ok(v) => String::from("Update"),
Ok(_v) => String::from("Update"),
Err(_) => String::from(""),
}
}
Expand All @@ -353,8 +360,23 @@ fn conflict(_req: &Request) -> String {
format!("Conflict")
}

#[launch]
async fn rocket() -> _ {
fn main() {
// lock all allocations
let result = unsafe { mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) };
if result != 0 {
eprintln!("mlockall failed with error code: {}", result);
} else {
println!("mlockall succeeded");
}
// https://github.com/rwf2/Rocket/issues/1881 👍👍👍
rocket::execute(async move {
let _ = rocket().await
.launch()
.await;
});
}

async fn rocket() -> Rocket<Build> {
rtp_force_db_init().await.expect("Unable to init database");
rtp_import_database().await.expect("Error importing database");

Expand Down Expand Up @@ -608,7 +630,7 @@ impl<'r> FromRequest<'r> for RatchetApiKey {
type Error = RatchetAuthError;

async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
let mut api_key_store = RATCHET_APIKEYS.lock().await;
let api_key_store = RATCHET_APIKEYS.lock().await;
if let Some(api_key) = req.headers().get_one("X-Ratchet-Api-Key") {
match api_key_store.get_key_value(api_key) {
Some((_, _)) => {
Expand Down