abi-aa icon indicating copy to clipboard operation
abi-aa copied to clipboard

[SYSVABI] Describe implementation details for memory tagging

Open yury-khrustalev opened this issue 8 months ago • 9 comments

Current issues

The MemTagABIELF64 extension provides recommendations for operating systems such as Linux. There are some implementation details that should also be specified but are out of scope of the MemTagABIELF64 document:

  1. Use of dynamic array tags DT_AARCH64_MEMTAG_* for ELF marking: a program property might be more practical for ELF marking related to memory tagging (similar to BTI and GCS).

    • The dynamic array tags are not applicable to static binaries. There should be support for marking statically linked binaries (executables and statically-linked DSOs).
    • Using dynamic array tags for some features and program properties for others unnecessarily complicates the specification and complying implementations.
  2. Some of the dynamic array tags DT_AARCH64_MEMTAG_* are described in MemTagABIELF64 as "only valid on the main executable". This leaves questions about compatibility of shared modules and lack of tools for diagnostics and fail-early implementation.

  3. The DT_AARCH64_MEMTAG_MODE tag implies that software is in control of the MTE mode that is enabled, however the platform may impose restrictions to that. Furthermore, ideally the actual MTE mode should be transparent for software. Therefore, it might be practical to describe that the DT_AARCH64_MEMTAG_MODE requirement established by MemTagABIELF64 is optional or doesn't have to be implemented.

Proposed program property for ELF marking related to memory tagging

The GNU_PROPERTY_AARCH64_MEMTAG_1 processor-specific program property describes memory tagging support in the binary. It is a bit mask of the following fields:

  • GNU_PROPERTY_AARCH64_MEMTAG_HEAP indicates that heap allocations may be protected with memory tagging.
  • GNU_PROPERTY_AARCH64_MEMTAG_STACK is a signal to the loader that the stack regions should be mapped using tag-capable memory.

This program property can be extended with more fields. Lack of this property or it being equal to 0 means that memory tagging is not supported by this binary.

If a loaded DSO module's value of GNU_PROPERTY_AARCH64_MEMTAG_1 is different from its value in the main executable, loading such modules is an error.

Static linkers may show warning when DSOs being used for linking have mismatching values of GNU_PROPERTY_AARCH64_MEMTAG_1.

yury-khrustalev avatar Mar 17 '26 12:03 yury-khrustalev

Related to first point, having the DT_AARCH64_MEMTAG_* being ELF generic instead of a GNU_PROPERTY allows other runtimes to define its own semantic for dynamic linking more suitable for their needs (like Android that allows loading DSO during process execution and implements a Memtag switch for running threads).

Adding another GNU specific mechanism with the same capabilites of DT_AARCH64_MEMTAG_* adds the extra complexity of how to handle binaries with two machanism and which one takes precedence over the other. It will require extending the Build Attributes with extra MTE flags, duplicating a subset of the Memtag ABI Extension to ELF, and also adding more static linker complexity to handle this extra bit flag (as done for BTI, PAC, GCS). My understanding from Build Attribute enablement on binutils is that the idea is to move away from this.

For the second point, I think it is out of scope for the SYSVABI to overspecify how the dynamic linker should act. I added my remarks on the required points for the DT_AARCH64_MEMTAG_STACK for the glibc implementation. The DT_AARCH64_MEMTAG_* already provide all the requires information for dynamic linking decision, the GNU_PROPERTY_AARCH64_MEMTAG_1 seems superflous.

For the third point, the current DT_AARCH64_MEMTAG_MODE is already optional (”If DT_AARCH64_MEMTAG_MODE is present” ) and only defines Synchronous and Asynchronous, which should cover ARMv8.5-A (MTE2). Synchronous, in some cases, might be necessary for better debugging or hardening, and this gives the user the option to enforce it (and on glibc side we might add an option to use system default as per Linux 'Per-CPU preferred tag checking mode'). It might become an issue if/when DT_AARCH64_MEMTAG_MODE is extended to support Asymmetric Mode (MTE3), but it can be lowered to synchronous with just performance implications.

This missing support that DT_AARCH64_MEMTAG_* does not address is ET_EXEC without PT_INTERP. Android solved it with .note.android.memtag, and for current GNU/glibc deployments (where GNU_PROPERTY is the main target), AArch64 already has static-pie widely available.

zatrazz avatar Mar 18 '26 12:03 zatrazz

@zatrazz

Adding another GNU specific mechanism with the same capabilites of DT_AARCH64_MEMTAG_*

Capabilities are not the same: static executables are not supported by dynamic array tags. How do you propose to handle those? Supporting GNU properties for PAC, BTI, and GCS while using dynamic array tags for MTE seems messy to me (but I already said all this above...)

It will require extending the Build Attributes with extra MTE flags

No, we don't need to extend build attributes because BAs only apply to object files and here we only talk about binaries (executables and DSOs).

yury-khrustalev avatar Mar 18 '26 13:03 yury-khrustalev

@zatrazz

Adding another GNU specific mechanism with the same capabilites of DT_AARCH64_MEMTAG_*

Capabilities are not the same: static executables are not supported by dynamic array tags. How do you propose to handle those? Supporting GNU properties for PAC, BTI, and GCS while using dynamic array tags for MTE seems messy to me (but I already said all this above...)

Is static binaries really worth all the trouble to define another Memtag ABI and reimplement it on all toolchains that already support Memtag ABI? Android is already deploying MTE binaries with relative success, I think it highly unlikely that they will move to GNU attributes.

And if this is really a deal breaker I will add a new OS specific flag (PF_MASKOS) for the PT_GNU_STACK to specify that the stack should tagged.

It will require extending the Build Attributes with extra MTE flags

No, we don't need to extend build attributes because BAs only apply to object files and here we only talk about binaries (executables and DSOs).

But how do you make the static linker decision to enable for SHT_AARCH64_MEMTAG_GLOBALS_STATIC/SHT_AARCH64_MEMTAG_GLOBALS_GLOBAL ? Or do you not intend to track this for the GNU property?

Also, I makes sense to propagate a possible Tag_Feature_MTE on ET_REL objects if the idea of enabling MTE on ET_EXEC/ET_DYN only makes sense if the object does use MTE instructions.

zatrazz avatar Mar 18 '26 13:03 zatrazz

Is static binaries really worth all the trouble to define another Memtag ABI

None alone, no, but there are other factors too mentioned above.

But how do you make the static linker decision to enable for SHT_AARCH64_MEMTAG_GLOBALS_STATIC/SHT_AARCH64_MEMTAG_GLOBALS_GLOBAL ? Or do you not intend to track this for the GNU property?

This is not in scope here. The proposed GNU properties would be set via exactly the same mechanism that is used for DT_AARCH64_MEMTAG_*: -Wl,-z,memtag-foo -Wl,-z,memtag-mode=bar.

yury-khrustalev avatar Mar 18 '26 13:03 yury-khrustalev

I think the GNU properties would only be worth defining if we had an implementation that was wanting to use them.

IIUC a statically linked ELF file on linux does not introspect its own ELF file so the only use of GNU properties would be by an external ELF loader, which I assume would be the kernel's ELF loader. For BTI a property makes sense as this needs to be enabled for each page.

Would it be possible for the static C-library have the equivalent of memtag heap and memtag stack defined in the library itself. There would then be no need for ELF marking at all.

As an example Arm's proprietary compiler had a --library-security option https://developer.arm.com/documentation/101754/0624/armlink-Reference/armlink-Command-line-Options/--library-security-protection that could select a heap and stack tagging library.

smithp35 avatar Mar 18 '26 15:03 smithp35

I think the GNU properties would only be worth defining if we had an implementation that was wanting to use them.

IIUC a statically linked ELF file on linux does not introspect its own ELF file so the only use of GNU properties would be by an external ELF loader, which I assume would be the kernel's ELF loader. For BTI a property makes sense as this needs to be enabled for each page.

For the Linux kernel it depends of ARCH_USE_GNU_PROPERTY config, which only enabled by default on aarch64 for BTI support (arch_parse_elf_property).

And on glibc side we had to add static GNU Property handling to enable GCS support (which is not enforced by default).

Would it be possible for the static C-library have the equivalent of memtag heap and memtag stack defined in the library itself. There would then be no need for ELF marking at all.

As an example Arm's proprietary compiler had a --library-security option https://developer.arm.com/documentation/101754/0624/armlink-Reference/armlink-Command-line-Options/--library-security-protection that could select a heap and stack tagging library.

This requires either the compiler or the C runtime to provide extra startup files, like an specialized crt1.o that always enable memtag stack/heap. I am not sure if this works seamlessly for GNU ecosystem, if this is provided by the compiler it will need to handle itself all the required machinery to keep compatibility and hardware discovery (not impossible, but it is a duplicate effort since for dynamic case the C runtime seems the most straightforward place).

zatrazz avatar Mar 18 '26 16:03 zatrazz

Regarding @zatrazz's

Adding another GNU specific mechanism with the same capabilites of DT_AARCH64_MEMTAG_* adds the extra complexity of how to handle binaries with two machanism and which one takes precedence over the other.

In Glibc we already have implementation for GNU properties that are used for BTI and GCS. Supporting dynamic tags as well would mean that we have to add more code that would indeed increase complexity of how to handle binaries. My proposal is partly based on trying to avoid this complexity.

Regarding @smithp35's

I think the GNU properties would only be worth defining if we had an implementation that was wanting to use them.

I think it would make sense to do exactly that in Glibc. We already have support for GNU properties.

Another reason why I think using GNU properties might be reasonable is:

Using dynamic array tags for some features and program properties for others unnecessarily complicates the specification and complying implementations.

I think we should try avoiding this fragmentation as this affects not just runtimes/ dynamic linker but also static linker. Using different ways to convey similar types of information (feature support and / or requirements) seems unnecessary to me with no clear benefit to outweigh the complexity.

yury-khrustalev avatar Mar 19 '26 09:03 yury-khrustalev

I generally agree with @zatrazz that these should remain as dynamic tags. Although I agree with @yury-khrustalev that they should have been GNU properties from the start, the migration costs in the static linker don't seem worth it. Either way, there is some fragmentation: either (1) the static linker needs to know about two ways of specifying MTE properties, or (2) the dynamic linker needs to know that MTE is specified in dynamic tags and BTI/GCS/etc are specified in properties.

2 seems preferable. I don't think there's really anything shared about the concept of "feature support" that would benefit from everything related to it using the same format -- it just makes things slightly easier to understand. 2 also resolves some practical problems: it avoids the need to support a migration between binary formats, with all the potential associated problems such as backwards compatibility, as well as the need to teach static linkers about both formats and when to use which one.

Android's use of .note.android.memtag is largely a workaround for a lack of static-pie support. I think that the generic ABI's position should be that MTE support in static binaries depends on static-pie.

pcc avatar Jun 10 '26 17:06 pcc

Hi! You guys may be interested in the discussion in #399; basically the current definition of R_AARCH64_RELATIVE is not idempotent because it stores the tag offset in *P. Solving that issue may require adding a new relocation type, breaking backwards compatibility.

EliaGeretto avatar Jun 29 '26 17:06 EliaGeretto

Closing this since not much support for making proposed changes.

yury-khrustalev avatar Jul 08 '26 15:07 yury-khrustalev