]> git.lizzy.rs Git - rust.git/commitdiff
GetProcAddress: basic validation for hModule argument
authorRalf Jung <post@ralfj.de>
Thu, 21 May 2020 21:06:31 +0000 (23:06 +0200)
committerRalf Jung <post@ralfj.de>
Thu, 21 May 2020 21:06:31 +0000 (23:06 +0200)
src/shims/foreign_items/windows.rs

index a11e3b8aa6a019c72a468792b6e5110f2559c396..60448406a67de829817b640e9b146bca504e258e 100644 (file)
@@ -206,6 +206,20 @@ fn emulate_foreign_item_by_name(
                 this.write_scalar(Scalar::from_i32(result), dest)?;
             }
 
+            // Dynamic symbol loading
+            "GetProcAddress" => {
+                #[allow(non_snake_case)]
+                let &[hModule, lpProcName] = check_arg_count(args)?;
+                this.read_scalar(hModule)?.not_undef()?;
+                let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.not_undef()?)?;
+                if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.target.target_os)? {
+                    let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
+                    this.write_scalar(Scalar::from(ptr), dest)?;
+                } else {
+                    this.write_null(dest)?;
+                }
+            }
+
             // Miscellaneous
             "SystemFunction036" => {
                 // The actual name of 'RtlGenRandom'
@@ -258,17 +272,6 @@ fn emulate_foreign_item_by_name(
                 // Pretend this does not exist / nothing happened, by returning zero.
                 this.write_null(dest)?;
             }
-            "GetProcAddress" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
-                #[allow(non_snake_case)]
-                let &[_hModule, lpProcName] = check_arg_count(args)?;
-                let name = this.memory.read_c_str(this.read_scalar(lpProcName)?.not_undef()?)?;
-                if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.target.target_os)? {
-                    let ptr = this.memory.create_fn_alloc(FnVal::Other(dlsym));
-                    this.write_scalar(Scalar::from(ptr), dest)?;
-                } else {
-                    this.write_null(dest)?;
-                }
-            }
             "SetConsoleTextAttribute" if this.frame().instance.to_string().starts_with("std::sys::windows::") => {
                 #[allow(non_snake_case)]
                 let &[_hConsoleOutput, _wAttribute] = check_arg_count(args)?;