From 2cbffd5642c83b02d206110c483d5d83aed51c54 Mon Sep 17 00:00:00 2001 From: Ali Clark Date: Thu, 21 Jan 2016 02:43:22 +0000 Subject: [PATCH 1/2] actively disable stack execution on linux and bsd --- src/librustc_back/target/dragonfly_base.rs | 3 +++ src/librustc_back/target/freebsd_base.rs | 13 +++++++++++++ src/librustc_back/target/linux_base.rs | 3 +++ src/librustc_back/target/netbsd_base.rs | 3 +++ src/librustc_back/target/openbsd_base.rs | 3 +++ 5 files changed, 25 insertions(+) diff --git a/src/librustc_back/target/dragonfly_base.rs b/src/librustc_back/target/dragonfly_base.rs index b78fdc9f59ba1..ead16f957079c 100644 --- a/src/librustc_back/target/dragonfly_base.rs +++ b/src/librustc_back/target/dragonfly_base.rs @@ -24,6 +24,9 @@ pub fn opts() -> TargetOptions { // libraries which follow this flag. Thus, use it before // specifying libraries to link to. "-Wl,--as-needed".to_string(), + + // Always enable NX protection when it is available + "-Wl,-z,noexecstack".to_string(), ), position_independent_executables: true, archive_format: "gnu".to_string(), diff --git a/src/librustc_back/target/freebsd_base.rs b/src/librustc_back/target/freebsd_base.rs index e955f8c302bd6..9b5d55fa0d588 100644 --- a/src/librustc_back/target/freebsd_base.rs +++ b/src/librustc_back/target/freebsd_base.rs @@ -17,6 +17,19 @@ pub fn opts() -> TargetOptions { dynamic_linking: true, executables: true, has_rpath: true, + pre_link_args: vec![ + // We want to be able to strip as much executable code as possible + // from the linker command line, and this flag indicates to the + // linker that it can avoid linking in dynamic libraries that don't + // actually satisfy any symbols up to that point (as with many other + // resolutions the linker does). This option only applies to all + // following libraries so we're sure to pass it as one of the first + // arguments. + "-Wl,--as-needed".to_string(), + + // Always enable NX protection when it is available + "-Wl,-z,noexecstack".to_string(), + ], archive_format: "gnu".to_string(), exe_allocation_crate: super::maybe_jemalloc(), diff --git a/src/librustc_back/target/linux_base.rs b/src/librustc_back/target/linux_base.rs index 0efcf73ee8680..20aae113f7bb3 100644 --- a/src/librustc_back/target/linux_base.rs +++ b/src/librustc_back/target/linux_base.rs @@ -26,6 +26,9 @@ pub fn opts() -> TargetOptions { // following libraries so we're sure to pass it as one of the first // arguments. "-Wl,--as-needed".to_string(), + + // Always enable NX protection when it is available + "-Wl,-z,noexecstack".to_string(), ], position_independent_executables: true, archive_format: "gnu".to_string(), diff --git a/src/librustc_back/target/netbsd_base.rs b/src/librustc_back/target/netbsd_base.rs index 361f71f699591..ead8a7259f03b 100644 --- a/src/librustc_back/target/netbsd_base.rs +++ b/src/librustc_back/target/netbsd_base.rs @@ -24,6 +24,9 @@ pub fn opts() -> TargetOptions { // libraries which follow this flag. Thus, use it before // specifying libraries to link to. "-Wl,--as-needed".to_string(), + + // Always enable NX protection when it is available + "-Wl,-z,noexecstack".to_string(), ), position_independent_executables: true, archive_format: "gnu".to_string(), diff --git a/src/librustc_back/target/openbsd_base.rs b/src/librustc_back/target/openbsd_base.rs index 2e4aa27cf889d..e0c2d062949d8 100644 --- a/src/librustc_back/target/openbsd_base.rs +++ b/src/librustc_back/target/openbsd_base.rs @@ -24,6 +24,9 @@ pub fn opts() -> TargetOptions { // libraries which follow this flag. Thus, use it before // specifying libraries to link to. "-Wl,--as-needed".to_string(), + + // Always enable NX protection when it is available + "-Wl,-z,noexecstack".to_string(), ), position_independent_executables: true, archive_format: "gnu".to_string(), From 8e36b3ac4d8cf773d84231ad0958eda781c64139 Mon Sep 17 00:00:00 2001 From: Ali Clark Date: Fri, 22 Jan 2016 23:41:59 +0000 Subject: [PATCH 2/2] revert an unrelated flag addition for freebsd --- src/librustc_back/target/freebsd_base.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/librustc_back/target/freebsd_base.rs b/src/librustc_back/target/freebsd_base.rs index 9b5d55fa0d588..946e529502e7d 100644 --- a/src/librustc_back/target/freebsd_base.rs +++ b/src/librustc_back/target/freebsd_base.rs @@ -18,15 +18,6 @@ pub fn opts() -> TargetOptions { executables: true, has_rpath: true, pre_link_args: vec![ - // We want to be able to strip as much executable code as possible - // from the linker command line, and this flag indicates to the - // linker that it can avoid linking in dynamic libraries that don't - // actually satisfy any symbols up to that point (as with many other - // resolutions the linker does). This option only applies to all - // following libraries so we're sure to pass it as one of the first - // arguments. - "-Wl,--as-needed".to_string(), - // Always enable NX protection when it is available "-Wl,-z,noexecstack".to_string(), ],