-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rust module to prevent run-time memory dumping of main (#9393)
- Loading branch information
Showing
11 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
apps/desktop/desktop_native/core/src/process_isolation/linux.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use anyhow::Result; | ||
use libc::{c_int, self}; | ||
#[cfg(target_env = "gnu")] | ||
use libc::c_uint; | ||
|
||
// RLIMIT_CORE is the maximum size of a core dump file. Setting both to 0 disables core dumps, on crashes | ||
// https://github.com/torvalds/linux/blob/1613e604df0cd359cf2a7fbd9be7a0bcfacfabd0/include/uapi/asm-generic/resource.h#L20 | ||
#[cfg(target_env = "musl")] | ||
const RLIMIT_CORE: c_int = 4; | ||
#[cfg(target_env = "gnu")] | ||
const RLIMIT_CORE: c_uint = 4; | ||
|
||
// PR_SET_DUMPABLE makes it so no other running process (root or same user) can dump the memory of this process | ||
// or attach a debugger to it. | ||
// https://github.com/torvalds/linux/blob/a38297e3fb012ddfa7ce0321a7e5a8daeb1872b6/include/uapi/linux/prctl.h#L14 | ||
const PR_SET_DUMPABLE: c_int = 4; | ||
|
||
pub fn disable_coredumps() -> Result<()> { | ||
let rlimit = libc::rlimit { | ||
rlim_cur: 0, | ||
rlim_max: 0, | ||
}; | ||
if unsafe { libc::setrlimit(RLIMIT_CORE, &rlimit) } != 0 { | ||
let e = std::io::Error::last_os_error(); | ||
return Err(anyhow::anyhow!("failed to disable core dumping, memory might be persisted to disk on crashes {}", e)) | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn is_core_dumping_disabled() -> Result<bool> { | ||
let mut rlimit = libc::rlimit { | ||
rlim_cur: 0, | ||
rlim_max: 0, | ||
}; | ||
if unsafe { libc::getrlimit(RLIMIT_CORE, &mut rlimit) } != 0 { | ||
let e = std::io::Error::last_os_error(); | ||
return Err(anyhow::anyhow!("failed to get core dump limit {}", e)) | ||
} | ||
|
||
Ok(rlimit.rlim_cur == 0 && rlimit.rlim_max == 0) | ||
} | ||
|
||
pub fn disable_memory_access() -> Result<()> { | ||
if unsafe { libc::prctl(PR_SET_DUMPABLE, 0) } != 0 { | ||
let e = std::io::Error::last_os_error(); | ||
return Err(anyhow::anyhow!("failed to disable memory dumping, memory is dumpable by other processes {}", e)) | ||
} | ||
|
||
Ok(()) | ||
} |
13 changes: 13 additions & 0 deletions
13
apps/desktop/desktop_native/core/src/process_isolation/macos.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use anyhow::{bail, Result}; | ||
|
||
pub fn disable_coredumps() -> Result<()> { | ||
bail!("Not implemented on Mac") | ||
} | ||
|
||
pub fn is_core_dumping_disabled() -> Result<bool> { | ||
bail!("Not implemented on Mac") | ||
} | ||
|
||
pub fn disable_memory_access() -> Result<()> { | ||
bail!("Not implemented on Mac") | ||
} |
5 changes: 5 additions & 0 deletions
5
apps/desktop/desktop_native/core/src/process_isolation/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[cfg_attr(target_os = "linux", path = "linux.rs")] | ||
#[cfg_attr(target_os = "windows", path = "windows.rs")] | ||
#[cfg_attr(target_os = "macos", path = "macos.rs")] | ||
mod process_isolation; | ||
pub use process_isolation::*; |
13 changes: 13 additions & 0 deletions
13
apps/desktop/desktop_native/core/src/process_isolation/windows.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use anyhow::{bail, Result}; | ||
|
||
pub fn disable_coredumps() -> Result<()> { | ||
bail!("Not implemented on Windows") | ||
} | ||
|
||
pub fn is_core_dumping_disabled() -> Result<bool> { | ||
bail!("Not implemented on Windows") | ||
} | ||
|
||
pub fn disable_memory_access() -> Result<()> { | ||
bail!("Not implemented on Windows") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters