mirror of
				https://github.com/GrapheneOS/hardened_malloc.git
				synced 2025-11-04 01:36:33 +01:00 
			
		
		
		
	Compare commits
	
		
			3 commits
		
	
	
		
			1b0d38c268
			...
			f7956954e5
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
							 | 
						f7956954e5 | ||
| 
							 | 
						ff99511eb4 | ||
| 
							 | 
						96836f463b | 
					 4 changed files with 30 additions and 6 deletions
				
			
		
							
								
								
									
										12
									
								
								README.md
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								README.md
									
										
									
									
									
								
							| 
						 | 
					@ -65,14 +65,14 @@ used instead as this allocator fundamentally doesn't support that environment.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Dependencies
 | 
					## Dependencies
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Debian stable (currently Debian 12) determines the most ancient set of
 | 
					Debian stable (currently Debian 13) determines the most ancient set of
 | 
				
			||||||
supported dependencies:
 | 
					supported dependencies:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
* glibc 2.36
 | 
					* glibc 2.41
 | 
				
			||||||
* Linux 6.1
 | 
					* Linux 6.12
 | 
				
			||||||
* Clang 14.0.6 or GCC 12.2.0
 | 
					* Clang 19.1.7 or GCC 14.2.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
For Android, the Linux GKI 5.10, 5.15 and 6.1 branches are supported.
 | 
					For Android, the Linux GKI 6.1, 6.6 and 6.12 branches are supported.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
However, using more recent releases is highly recommended. Older versions of
 | 
					However, using more recent releases is highly recommended. Older versions of
 | 
				
			||||||
the dependencies may be compatible at the moment but are not tested and will
 | 
					the dependencies may be compatible at the moment but are not tested and will
 | 
				
			||||||
| 
						 | 
					@ -83,7 +83,7 @@ there will be custom integration offering better performance in the future
 | 
				
			||||||
along with other hardening for the C standard library implementation.
 | 
					along with other hardening for the C standard library implementation.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
For Android, only the current generation, actively developed maintenance branch of the Android
 | 
					For Android, only the current generation, actively developed maintenance branch of the Android
 | 
				
			||||||
Open Source Project will be supported, which currently means `android15-release`.
 | 
					Open Source Project will be supported, which currently means `android16-release`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Testing
 | 
					## Testing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1295,7 +1295,12 @@ COLD static void init_slow_path(void) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    atomic_store_explicit(&ro.slab_region_end, slab_region_end, memory_order_release);
 | 
					    atomic_store_explicit(&ro.slab_region_end, slab_region_end, memory_order_release);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if defined(__ANDROID__) && defined(HAS_ARM_MTE)
 | 
				
			||||||
 | 
					    /* Do not seal to support disabling memory tagging */
 | 
				
			||||||
    if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
 | 
					    if (unlikely(memory_protect_ro(&ro, sizeof(ro)))) {
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					    if (unlikely(memory_protect_seal(&ro, sizeof(ro)))) {
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
        fatal_error("failed to protect allocator data");
 | 
					        fatal_error("failed to protect allocator data");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    memory_set_name(&ro, sizeof(ro), "malloc read-only after init");
 | 
					    memory_set_name(&ro, sizeof(ro), "malloc read-only after init");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										18
									
								
								memory.c
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								memory.c
									
										
									
									
									
								
							| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/mman.h>
 | 
					#include <sys/mman.h>
 | 
				
			||||||
 | 
					#include <sys/syscall.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef LABEL_MEMORY
 | 
					#ifdef LABEL_MEMORY
 | 
				
			||||||
#include <sys/prctl.h>
 | 
					#include <sys/prctl.h>
 | 
				
			||||||
| 
						 | 
					@ -91,6 +93,22 @@ bool memory_protect_rw_metadata(void *ptr, size_t size) {
 | 
				
			||||||
    return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
 | 
					    return memory_protect_prot(ptr, size, PROT_READ|PROT_WRITE, get_metadata_key());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					COLD bool memory_protect_seal(void *ptr, size_t size) {
 | 
				
			||||||
 | 
					#if defined(__linux__) && defined(__NR_mseal)
 | 
				
			||||||
 | 
					    /* supported since Linux 6.10 */
 | 
				
			||||||
 | 
					    int ret = syscall(__NR_mseal, ptr, size, 0);
 | 
				
			||||||
 | 
					    if (ret == 0)
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    if (unlikely(errno == ENOMEM))
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    if (errno == ENOSYS)
 | 
				
			||||||
 | 
					        return memory_protect_ro(ptr, size);
 | 
				
			||||||
 | 
					    fatal_error("non-ENOMEM and non-ENOSYS mseal failure");
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					    return memory_protect_ro(ptr, size);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef HAVE_COMPATIBLE_MREMAP
 | 
					#ifdef HAVE_COMPATIBLE_MREMAP
 | 
				
			||||||
bool memory_remap(void *old, size_t old_size, size_t new_size) {
 | 
					bool memory_remap(void *old, size_t old_size, size_t new_size) {
 | 
				
			||||||
    void *ptr = mremap(old, old_size, new_size, 0);
 | 
					    void *ptr = mremap(old, old_size, new_size, 0);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										1
									
								
								memory.h
									
										
									
									
									
								
							
							
						
						
									
										1
									
								
								memory.h
									
										
									
									
									
								
							| 
						 | 
					@ -22,6 +22,7 @@ bool memory_unmap(void *ptr, size_t size);
 | 
				
			||||||
bool memory_protect_ro(void *ptr, size_t size);
 | 
					bool memory_protect_ro(void *ptr, size_t size);
 | 
				
			||||||
bool memory_protect_rw(void *ptr, size_t size);
 | 
					bool memory_protect_rw(void *ptr, size_t size);
 | 
				
			||||||
bool memory_protect_rw_metadata(void *ptr, size_t size);
 | 
					bool memory_protect_rw_metadata(void *ptr, size_t size);
 | 
				
			||||||
 | 
					bool memory_protect_seal(void *ptr, size_t size);
 | 
				
			||||||
#ifdef HAVE_COMPATIBLE_MREMAP
 | 
					#ifdef HAVE_COMPATIBLE_MREMAP
 | 
				
			||||||
bool memory_remap(void *old, size_t old_size, size_t new_size);
 | 
					bool memory_remap(void *old, size_t old_size, size_t new_size);
 | 
				
			||||||
bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);
 | 
					bool memory_remap_fixed(void *old, size_t old_size, void *new, size_t new_size);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue