Skip to content

Commit 526fae7

Browse files
committed
GetProcAddress: basic validation for hModule argument
1 parent f09decb commit 526fae7

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/shims/foreign_items/windows.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
206206
this.write_scalar(Scalar::from_i32(result), dest)?;
207207
}
208208

209+
// Dynamic symbol loading
210+
"GetProcAddress" => {
211+
#[allow(non_snake_case)]
212+
let &[hModule, lpProcName] = check_arg_count(args)?;
213+
this.read_scalar(hModule)?.not_undef()?;
214+
let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.not_undef()?)?;
215+
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.target.target_os)? {
216+
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
217+
this.write_scalar(Scalar::from(ptr), dest)?;
218+
} else {
219+
this.write_null(dest)?;
220+
}
221+
}
222+
209223
// Miscellaneous
210224
"SystemFunction036" => {
211225
// The actual name of 'RtlGenRandom'
@@ -258,17 +272,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
258272
// Pretend this does not exist / nothing happened, by returning zero.
259273
this.write_null(dest)?;
260274
}
261-
"GetProcAddress" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
262-
#[allow(non_snake_case)]
263-
let &[_hModule, lpProcName] = check_arg_count(args)?;
264-
let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.not_undef()?)?;
265-
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.target.target_os)? {
266-
let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
267-
this.write_scalar(Scalar::from(ptr), dest)?;
268-
} else {
269-
this.write_null(dest)?;
270-
}
271-
}
272275
"SetConsoleTextAttribute" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
273276
#[allow(non_snake_case)]
274277
let &[_hConsoleOutput, _wAttribute] = check_arg_count(args)?;

0 commit comments

Comments
 (0)