My server keeps crashing from a segmentation error. I know they a re happening, i just wish my server wouldn’t crash.
use rocket::serde::json::Json;
use rocket::serde::Deserialize;
use rocket::serde::Serialize;
use rocket::post;
use rocket::http::Header;
use rocket::response::status;
use rocket::response::status::Custom;
use rocket::http::Status;
use rocket::http::Method;
use rocket_cors::{CorsOptions, AllowedHeaders, AllowedOrigins};
use std::process::Command;
#[repr(C)]
struct Hackvist {
buffer: [u8; 16],
point: *const fn(),
}
#[post("/execute", data = "<data>")]
fn execute_command(data: Json<ExecuteRequest>) -> Json<ExecuteResponse> {
let input_command = data.command.clone();
let mut hackvist = Hackvist {
buffer: [0; 16],
point: 0 as *const fn() -> (),
};
let input_bytes: &[u8] = input_command.as_bytes();
unsafe {
std::ptr::copy(
input_bytes.as_ptr(),
hackvist.buffer.as_mut_ptr(),
input_bytes.len(),
)
}
let result = if hackvist.point as usize == 0 {
"Try again".to_string()
} else {
let code: fn() = unsafe { std::mem::transmute(hackvist.point) };
code();
"Success".to_string()
};
Json(ExecuteResponse { result })
}
#[derive(Deserialize)]
struct ExecuteRequest {
command: String,
}
#[derive(Serialize)]
struct ExecuteResponse {
result: String,
}
//#[post("/execute", data = "<data>")]
//fn execute_command(data: Json<ExecuteRequest>) -> Json<ExecuteResponse> {
// let input_command = data.command.clone();
// Execute the command and capture its output
// let output = Command::new("sh")
// .arg("-c")
// .arg(&input_command)
// .output()
//.expect("failed to execute command");
// Convert output bytes to string
//let result = String::from_utf8_lossy(&output.stdout).to_string();
// Create and return the response
//let response = ExecuteResponse { result };
//Json(response)
//}
#[options("/execute")]
fn options_execute() -> status::Custom<String> {
let header_value = "Access-Control-Allow-Origin: *";
let header = Header::new("Access-Control-Allow-Origin", header_value);
status::Custom(Status::Ok, header.value().to_string())
}
#[rocket::main]
async fn main() {
let cors = CorsOptions {
allowed_origins: AllowedOrigins::all(),
allowed_methods: vec![rocket::http::Method::Get, rocket::http::Method::Post, rocket::http::Method::Put]
.into_iter()
.map(From::from)
.collect(),
allowed_headers: AllowedHeaders::all(),
allow_credentials: true,
..Default::default()
}
.to_cors()
.expect("Cors configuration failed");
rocket::build()
.attach(cors)
.mount("/", routes![execute_command, options_execute])
.launch()
.await
.expect("Rocket did not launch successfully");
}
I tried encapsulating the line were i use std::ptr::copy in an unsafe capsule, but the segmentation error keep happening:(exit code: 0xc0000005, STATUS_ACCESS_VIOLATION).
I don’t really have more details, ask if you need any. Thanks
New contributor
help is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.