Jacob Bramley
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    References, and existing work: - [Original Zulip thread in t-compiler](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/.60-Zbranch-protection.60.20stability) - [... continued in project-exploit-mitigations](https://rust-lang.zulipchat.com/#narrow/stream/343119-project-exploit-mitigations/topic/CFI.20option.20stabilisation) - [`branch-protection` tracking issue](https://github.com/rust-lang/rust/issues/93754) - [`cf-protection` (CET) tracking issue](https://github.com/rust-lang/rust/issues/93754) - [Tracking issue for sanitizer support](https://github.com/rust-lang/rust/issues/39699) - [Tracking Issue for LLVM Control Flow Integrity (CFI) Support for Rust](https://github.com/rust-lang/rust/issues/89653) # Summary |Options like this... |Become... | |-----------------------------------------|------------------------------------------------------| |_Various target-specific CFI choices..._ |`-Cabi-variant=cfi` | |`-Zbranch-protection=pac-ret,bti` |`-Zabi-variant=aarch64:branch-protection:pac-ret,bti` | |`-Ccontrol-flow-guard=yes` |`-Cabi-variant=windows:control-flow-guard:on` | # Objectives Our long-term objectives are **to make CFI mitigations easy to enable in Rust/Cargo, project-wide.** However, the scope of _this_ proposal is simply **to define a command-line interface for controlling CFI options**, as a step towards the overall objective. In particular, whilst the command-line interface for stable and unstable options is considered, no additional stabilisation is considered here today. ## Background and scope Typically, CFI (control-flow integrity) mitigations are implemented as target-specific ABI variants. Rust already has some support for enabling these variants, for example as target-specific CodeGen options: - [`-Zbranch-protection`][] (for AArch64), - [`-Zcf-protection`][] (for x86), - [`-Ccontrol-flow-guard`][] (for Windows). _In addition, this proposal overlaps somewhat with [`-Zsanitizer`][]. See the [open questions (at the end)](#-Zsanitizer), for a discussion._ However, they aren't straightforward to use: 1. All but `-Ccontrol-flow-guard` are unstable. 2. The unstable (Cargo) `build-std` option is required to get a `std` built with the same flags. 3. `cc-rs` does not forward these settings to the C compiler, so included C code (including some examples in `std`) will not include mitigations. A similar, but technically distinct problem occurs with the C library, which is usually dynamically linked with Rust binaries. Since this is already an external dependency, we might consider this out of Rust's scope, but that at least requires documentation. Note that, to be effective, most of these CFI techniques require that _all_ code in a process is protected. Even unreachable binary code can be targeted by CFI attacks if it is loaded into an executable memory region. Usually, this is a security concern, but we also consider that correct deployment may be required for the _correctness_ of some techniques. We hope to find solutions to these problems, perhaps by stabilising `build-std`, but such solutions are out of scope of this proposal. One minor inconsistency with the existing options is the handling of errors: `control-flow-guard` is ignored for targets other than Windows, whilst `branch-protection` fails if the target is not AArch64. This proposal aims to unify the behaviours here. [`-Zbranch-protection`]: https://doc.rust-lang.org/unstable-book/compiler-flags/branch-protection.html [`-Zcf-protection`]: https://doc.rust-lang.org/unstable-book/compiler-flags/cf-protection.html [`-Ccontrol-flow-guard`]: https://doc.rust-lang.org/rustc/codegen-options/index.html#control-flow-guard [`-Zsanitizer`]: https://doc.rust-lang.org/unstable-book/compiler-flags/sanitizer.html ## Use-cases and motivation ### Rust-C interoperability C (or C++) projects might already deploy CFI mitigations, and this might even be mandated by a system's security analysis team. If they introduce Rust code, they will need a way to ensure that it implements the same standard of mitigation. This may also be required for some mixed-language LTO use-cases. **Rust should provide options that map, in a simple and easy-to-audit fashion, onto common C compiler options.** In addition, if new options are added to future C compilers, we should be able to support them in Rust too. ### Pure-Rust applications A notable disadvantage of matching C compiler options is that they are target-specific, which makes them difficult to use in a project that is otherwise target-agnostic Rust. **Rust should provide simple, target-agnostic Rust options that map, in a target-specific manner, onto some ABI variant.** The specific mitigations used will vary from one target to another, and for some targets may not be possible at all. The behaviour should be "best-effort": targets that don't implement the option can simply ignore it, perhaps with a warning. ### Fine control where required Fine control is required for Rust-C interoperability, but may also apply to pure-Rust programs, providing very fine control over the mitigations used, at expense of portability. For example, the `pac-ret` scheme for AArch64 can be deployed with one of two keys ("A" or "B"). **Rust should allow this flexibility**, even if it is not exposed by default. This is quite unlike the general, best-effort approach. If fine control is requested, it would be misleading for Rust to ignore it, so Rust should instead refuse to build the project. ### Room for future growth C compilers typically have to be concerned about compatibility, so that compiled functions can be linked together without recompilation. Rust's approach of statically linking (almost) everything somewhat frees it from this restriction, and potentially allows it to deploy mitigations very quickly, at least when using the Rust ABI. In addition, we observe that whilst all mitigations listed here concern CFI, that need not always be the case. For example, AArch64 Pointer Authentication can also be used for data protection. Ideally, the option name should be chosen with this in mind. We propose no new ABI variants today, but the command-line interface proposal should allow for such additions. ### Turning things off Currently, none of these features are enabled by default. However, if that were to change (or if a default was set earlier on the command line), it can be useful to have a means to turn them off. This is possible today for `cf-protection` and `control-flow-guard`, but not for `branch-protection`, which is purely additive. It would be useful to make these consistent. # The proposal ## `-Cabi-variant=...` / `-Zabi-variant=...` 1. This is principally a [CodeGen option][] so it makes sense under `-C` rather than at the top level. 2. Keeping both `-C` and `-Z` forms provides a deployment path for new options that avoids them being instantly stable after `abi-variant` itself is stable. 3. Grouping everything under one option makes similar options for different targets easier to find, and makes it easier to keep the interface consistent. 4. The existing, stable `-Ccontrol-flow-guard` should remain, for backwards-compatibility, but becomes an alias for `abi-variant` options. 5. The existing, unstable `-Zbranch-protection` and `-Zcf-protection` should also remain for some transition period, but may be removed in the future. 6. The name `abi-variant` is not currently in use, so should not come with any particular behaviour expectations. 7. The name is not specific to CFI. [CodeGen option]: https://doc.rust-lang.org/rustc/codegen-options/index.html The option's value is a comma-separated list of zero or more `<variant>` names, preceded by zero or more `<group>:` selectors. However, behaviour differs slightly if no `<group>` is provided. ### `<variant>` Top-level variants (without any `<group>`) are high-level, best-effort protections. Suggested flags include: - `backward-edge-cfi`, which enables best-effort backward-edge CFI. - This might enable `aarch64:branch-protection:pac-ret`, `x86:cf-protection:return`, something else, or nothing at all. - `forward-edge-cfi`, which enables best-effort forward-edge CFI. - This might enable `aarch64:branch-protection:bti`, `x86:cf-protection:branch`, something else, or nothing at all. - `cfi` enables both of the above. - `none` (the default) disables all top-level protections. All of these top-level variants work on a best-effort basis. It is an error to name a variant that doesn't exist, but if a known variant cannot be implemented for the compiler target, it is simply ignored. In addition, Rust may change the exact meanings of these flags, for example if new techniques are introduced in future versions. Top-level variants are treated as simple shortcuts for target-specific variants _that the target platform supports_, and are intended to avoid target-specific configuration in build infrastructure. ### `<group>:[<group>:]...[<variant>,]...` `<group>`-specific variants are treated as precise, target-specific controls. Typically, we expect the first `<group>` to specify the target (or group of targets), and the next `<group>` to match the name of the de-facto C compiler option (if there is one). For example: - `-Cabi-variant=aarch64:branch-protection:pac-ret,bti` - `-Cabi-variant=x86:cf-protection:branch` - `-Cabi-variant=x86:cf-protection:branch,return` (or equivalently, `x86:cf-protection:full`) - `-Cabi-variant=windows:control-flow-guard:on` Groups may be named loosely, but are chosen as a compromise between verbosity and specificity. Notably, we don't expect to see full target triples here. For example, "aarch64" would group features that only make sense on `aarch64*` targets, but we don't require that _all_ `aarch64*` targets can actually implement the included `<variants>`. It is an _error_ to specify a variant under a named group unless that variant is actually supported by the target. ### Combinations It is valid to combine target-specific variants with a more general top-level baseline, but we provide no forward-compatibility guarantees for this use-case. For example, the following will work according to this proposal, but if `cfi` is later updated to use something other than `pac-ret`, then `b-key` will be invalid: `-Cabi-variant=cfi -Cabi-variant=aarch64:branch-protection:b-key` However, it may be useful to permit these combinations for use-cases where we want a "good" baseline, but want fine control over some aspects: `-Cabi-variant=forward-edge-cfi -Cabi-variant=aarch64:branch-protection:pac-ret,b-key` **Is this behaviour reasonable? Should we instead forbid mixing these at all?** ### Evaluation Evaluation should be intuitive, but the exact rules are described here to avoid misunderstandings. The existing options have slightly different behaviours, so different readers may have different intuitions. Variants are evaluated in command-line order, then first to last in the comma-separated list. Variants are typically additive, but that isn't required. We expect `none` and `off` to have the effect of disabling the whole `<group>` (but nothing outside it). We don't check validity until the last `abi-variant` has been parsed. Note that this differs from today's `branch-protection`, for example, which checks validity as it goes (and therefore permits `pac-ret,leaf` but not `leaf,pac-ret`). `<group>` selections can only be specified once per `-Cabi-variant`, but in all other respects, multiple `abi-variant` options are equivalent to a single option with multiple `<variant>`s. For example, the following are all equivalent (assuming that `none` is the default): - `-Cabi-variant=aarch64:branch-protection:pac-ret,bti` - `-Cabi-variant=aarch64:branch-protection:pac-ret -Cabi-variant=aarch64:branch-protection:bti` - `-Cabi-variant=aarch64:branch-protection:b-key -Cabi-variant=aarch64:branch-protection:none,bti -Cabi-variant=aarch64:branch-protection:pac-ret` ## `-Zabi-variant` The unstable form of the option should remain permanently, and behave identically to the stable form. The only difference is that the unstable `abi-variant` can accept unstable `<variant>`s. This allows us to extend the interface gracefully, without breaking stability rules. Notably, it is possible to combine both forms. The following are all equivalent (with a nightly compiler): - `-Cabi-variant=forward-edge-cfi -Zabi-variant=backward-edge-cfi` - `-Cabi-variant=cfi` - `-Zabi-variant=cfi` ## Initially-supported variants |`-Zabi-variant=...` |Meaning | |---------------------------------|---------------------------------------------------| |`aarch64:branch-protection:...` |`-Zbranch-protection=...` | |`x86:cf-protection:...` |`-Zcf-protection=...` | |`windows:control-flow-guard:...` |`-Ccontrol-flow-guard=...` | |`forward-edge-cfi` |_Varies with target._ | |`backward-edge-cfi` |_Varies with target._ | |`cfi` |`-Zabi-variant=backward-edge-cfi,forward-edge-cfi` | Once `abi-variant` itself is stabilised: |`-Cabi-variant=...` |Meaning | |---------------------------------|---------------------------------------------------| |`aarch64:branch-protection:...` |_Rejected until `branch-protection` is stable._ | |`x86:cf-protection:...` |_Rejected until `cf-protection` is stable._ | |`windows:control-flow-guard:...` |`-Ccontrol-flow-guard=...` | |`forward-edge-cfi` |_Varies with target._ | |`backward-edge-cfi` |_Varies with target._ | |`cfi` |`-Cabi-variant=backward-edge-cfi,forward-edge-cfi` | # Open questions ## `-Zsanitizer` There is considerable overlap between this proposed `{-Z,-C}abi-variant` and the existing (unstable) `-Zsanitizer`, because many of the available sanitizers could be viewed as ABI variants. **Question: should this proposal be merged with `-Zsanitizer`?** `{-Z,-C}abi-variant` has three notable properties that `-Zsanitizer` lacks: 1. Target-specific variants are distinguished, at least partially. 2. Best-effort options can be described. 3. Individual variants can be stabilised without affecting other unstable variants (though the same technique could be applied to `{-Z,-C}sanitizer`). It is clearly undesirable to maintain `abi-variant` mappings for every `sanitizer` option. Would it instead be acceptable to _replace_ `sanitizer` with `abi-variant`? Alternatively, if the `abi-variant` behaviours described here are deemed useful, should we attempt to fit them under the existing `sanitizer` option? Also note that the naming might require further attention. `abi-variant` currently covers technologies that are designed to be used in release builds, in production. They will have a performance and code-size overhead, but should be "light". Conversely, the term `sanitizer` suggests more comprehensive debug and test tools, though many of its options are clearly designed for deployment too. # Answers to common or expected questions ## Can't this be a target feature? The ABI variant describes what the compiler should _do_, whilst the target features describe the set of tools it has available to do it. For example: - Variants might be backwards-compatible, operating in a reduced form (or as no-ops) if some target features are unavailable. For example, `pac-ret` generates instructions that were no-ops before Armv8.3 Pointer Authentication was introduced. - Conversely, better code generation might be possible if the compiler _knows_ that a given target feature is available. For example, if `paca` is enabled at compile-time, Rust might implement `pac-ret` using instructions that weren't interpreted as no-ops prior to Armv8.3 Pointer Authentication. We also consider the possibility that an ABI variant might _require_ a target, or target feature. Specifying such a variant should cause a compile-time error, as noted earlier. For example, [`-Zsanitizer=shadow-call-stack`][] is only available on `aarch64-linux-android`. [`-Zsanitizer=shadow-call-stack`]: https://doc.rust-lang.org/unstable-book/compiler-flags/sanitizer.html#shadowcallstack

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully