Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 1119211

Browse files
committed
lucetc: add command line option to disable wat translation
wat translation is a potential attack vector, since it happens in the C++ `wabt` library, so we want a way to turn it off if required.
1 parent f109245 commit 1119211

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

lucetc/lucetc/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub fn run(opts: &Options) -> Result<(), Error> {
143143
c.count_instructions(true);
144144
}
145145

146+
c.translate_wat(opts.translate_wat);
147+
146148
match opts.codegen {
147149
CodegenOutput::Obj => c.object_file(&opts.output)?,
148150
CodegenOutput::SharedObj => c.shared_object_file(&opts.output)?,

lucetc/lucetc/options.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub struct Options {
122122
pub count_instructions: bool,
123123
pub error_style: ErrorStyle,
124124
pub target: Triple,
125+
pub translate_wat: bool,
125126
}
126127

127128
impl Options {
@@ -211,6 +212,7 @@ impl Options {
211212
let sk_path = m.value_of("sk_path").map(PathBuf::from);
212213
let pk_path = m.value_of("pk_path").map(PathBuf::from);
213214
let count_instructions = m.is_present("count_instructions");
215+
let translate_wat = !m.is_present("no_translate_wat");
214216

215217
let error_style = match m.value_of("error_style") {
216218
None => ErrorStyle::default(),
@@ -241,6 +243,7 @@ impl Options {
241243
count_instructions,
242244
error_style,
243245
target,
246+
translate_wat,
244247
})
245248
}
246249
pub fn get() -> Result<Self, Error> {
@@ -459,6 +462,12 @@ SSE3 but not AVX:
459462
.possible_values(&["human", "json"])
460463
.help("Style of error reporting (default: human)"),
461464
)
465+
.arg(
466+
Arg::with_name("no_translate_wat")
467+
.long("--no-translate-wat")
468+
.takes_value(false)
469+
.help("Disable translating wat input files to wasm")
470+
)
462471
.get_matches();
463472

464473
Self::from_args(&m)

lucetc/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub enum Error {
2020
IOError(#[from] std::io::Error),
2121
#[error("Converting to Wasm signature: {0}")]
2222
SignatureConversion(#[from] SignatureError),
23+
#[error("Input does not have Wasm preamble")]
24+
MissingWasmPreamble,
2325
#[error("Wasm validation: {0}")]
2426
WasmValidation(#[from] wasmparser::BinaryReaderError),
2527
#[error("Wat input: {0}")]

lucetc/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub struct Lucetc {
4646
pk: Option<PublicKey>,
4747
sign: bool,
4848
verify: bool,
49+
translate_wat: bool,
4950
}
5051

5152
pub trait AsLucetc {
@@ -104,6 +105,8 @@ pub trait LucetcOpts {
104105
fn with_count_instructions(self, enable_count: bool) -> Self;
105106
fn canonicalize_nans(&mut self, enable_canonicalize_nans: bool);
106107
fn with_canonicalize_nans(self, enable_canonicalize_nans: bool) -> Self;
108+
fn translate_wat(&mut self, enable_translate_wat: bool);
109+
fn with_translate_wat(self, enable_translate_wat: bool) -> Self;
107110
}
108111

109112
impl<T: AsLucetc> LucetcOpts for T {
@@ -258,6 +261,14 @@ impl<T: AsLucetc> LucetcOpts for T {
258261
self.canonicalize_nans(enable_nans_canonicalization);
259262
self
260263
}
264+
fn translate_wat(&mut self, enable_translate_wat: bool) {
265+
self.as_lucetc().translate_wat = enable_translate_wat;
266+
}
267+
268+
fn with_translate_wat(mut self, enable_translate_wat: bool) -> Self {
269+
self.translate_wat(enable_translate_wat);
270+
self
271+
}
261272
}
262273

263274
impl Lucetc {
@@ -271,6 +282,7 @@ impl Lucetc {
271282
sk: None,
272283
sign: false,
273284
verify: false,
285+
translate_wat: true,
274286
}
275287
}
276288

@@ -284,13 +296,16 @@ impl Lucetc {
284296
sk: None,
285297
sign: false,
286298
verify: false,
299+
translate_wat: false,
287300
})
288301
}
289302

290303
fn build(&self) -> Result<(Vec<u8>, Bindings), Error> {
291304
let module_binary = match &self.input {
292305
LucetcInput::Bytes(bytes) => bytes.clone(),
293-
LucetcInput::Path(path) => read_module(&path, &self.pk, self.verify)?,
306+
LucetcInput::Path(path) => {
307+
read_module(&path, &self.pk, self.verify, self.translate_wat)?
308+
}
294309
};
295310

296311
// Collect set of Bindings into a single Bindings:

lucetc/src/load.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub fn read_module(
77
path: impl AsRef<Path>,
88
pk: &Option<PublicKey>,
99
verify: bool,
10+
translate_wat: bool,
1011
) -> Result<Vec<u8>, Error> {
1112
let contents = std::fs::read(&path)?;
1213
if verify {
@@ -18,7 +19,15 @@ pub fn read_module(
1819
.ok_or(Error::Signature("public key is missing".to_string()))?,
1920
)?;
2021
}
21-
read_bytes(contents)
22+
if translate_wat {
23+
read_bytes(contents)
24+
} else {
25+
if wasm_preamble(&contents) {
26+
Ok(contents)
27+
} else {
28+
Err(Error::MissingWasmPreamble)
29+
}
30+
}
2231
}
2332

2433
pub fn read_bytes(bytes: Vec<u8>) -> Result<Vec<u8>, Error> {
@@ -39,7 +48,7 @@ pub fn read_bytes(bytes: Vec<u8>) -> Result<Vec<u8>, Error> {
3948
},
4049
_ => { }
4150
};
42-
crate::error::Error::Input(result)
51+
Error::Input(result)
4352
})
4453
}
4554
}

0 commit comments

Comments
 (0)