Skip to content

Commit

Permalink
Move ia2_get_pkey into libia2
Browse files Browse the repository at this point in the history
We need to use ia2_get_pkey from outside the allocator library, so this
change moves that function into libia2 where it can be shared for both.
  • Loading branch information
rinon committed Aug 1, 2023
1 parent f9572c6 commit a22b75f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#endif // BUILDFLAG(STARSCAN)

#include <sys/mman.h>
#include <ia2_get_pkey.h>
#include <ia2.h>

namespace partition_alloc::internal {

Expand Down
68 changes: 68 additions & 0 deletions libia2/ia2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,74 @@

#include "ia2.h"

#ifdef LIBIA2_INSECURE
size_t ia2_get_pkey() { return 0; }
#else
size_t ia2_get_pkey() {
uint32_t pkru;
__asm__("rdpkru" : "=a"(pkru) : "a"(0), "d"(0), "c"(0));
switch (pkru) {
case 0xFFFFFFFC: {
return 0;
}
case 0xFFFFFFF0: {
return 1;
}
case 0xFFFFFFCC: {
return 2;
}
case 0xFFFFFF3C: {
return 3;
}
case 0xFFFFFCFC: {
return 4;
}
case 0xFFFFF3FC: {
return 5;
}
case 0xFFFFCFFC: {
return 6;
}
case 0xFFFF3FFC: {
return 7;
}
case 0xFFFCFFFC: {
return 8;
}
case 0xFFF3FFFC: {
return 9;
}
case 0xFFCFFFFC: {
return 10;
}
case 0xFF3FFFFC: {
return 11;
}
case 0xFCFFFFFC: {
return 12;
}
case 0xF3FFFFFC: {
return 13;
}
case 0xCFFFFFFC: {
return 14;
}
case 0x3FFFFFFC: {
return 15;
}
// TODO: We currently treat any unexpected PKRU value as pkey 0 (the shared
// heap) for simplicity since glibc(?) initializes the PKRU to 0x55555554
// (usually). We don't set the PKRU until the first compartment transition, so
// let's default to using the shared heap before our first wrpkru. When we
// initialize the PKRU properly (see issue #95) we should probably abort when
// we see unexpected PKRU values.
default: {
return 0;
}
}
}
#endif // LIBIA2_INSECURE

static const char *shared_sections[][2] = {
{"__start_ia2_shared_data", "__stop_ia2_shared_data"},
};
Expand Down
10 changes: 10 additions & 0 deletions libia2/include/ia2.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ static uint32_t ia2_get_pkru() {
#define IA2_IGNORE_FIELD(decl) decl


#ifdef __cplusplus
extern "C" {
#endif

size_t ia2_get_pkey();

/// Protect pages in the given shared object
///
/// \param info dynamic linker information for the current object
Expand Down Expand Up @@ -310,3 +316,7 @@ static int insecure_pkey_mprotect(void *ptr, size_t len, int prot, int pkey) {
protect_tls(); \
init_stacks(); \
}

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions partition-alloc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ add_library(partition-alloc SHARED
src/allocator_shim.cc
${PA_SRCS})

target_link_libraries(partition-alloc libia2)

if(LIBIA2_INSECURE)
target_compile_definitions(partition-alloc PUBLIC LIBIA2_INSECURE=1)
endif()
Expand Down
74 changes: 1 addition & 73 deletions partition-alloc/src/allocator_shim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,7 @@
#include "allocator_shim.h"
#include "base/allocator/partition_allocator/partition_alloc.h"
#include "base/allocator/partition_allocator/partition_root.h"
#include <ia2_get_pkey.h>

extern "C" {

#ifdef LIBIA2_INSECURE
size_t ia2_get_pkey() { return 0; }
#else
size_t ia2_get_pkey() {
uint32_t pkru;
__asm__("rdpkru" : "=a"(pkru) : "a"(0), "d"(0), "c"(0));
switch (pkru) {
case 0xFFFFFFFC: {
return 0;
}
case 0xFFFFFFF0: {
return 1;
}
case 0xFFFFFFCC: {
return 2;
}
case 0xFFFFFF3C: {
return 3;
}
case 0xFFFFFCFC: {
return 4;
}
case 0xFFFFF3FC: {
return 5;
}
case 0xFFFFCFFC: {
return 6;
}
case 0xFFFF3FFC: {
return 7;
}
case 0xFFFCFFFC: {
return 8;
}
case 0xFFF3FFFC: {
return 9;
}
case 0xFFCFFFFC: {
return 10;
}
case 0xFF3FFFFC: {
return 11;
}
case 0xFCFFFFFC: {
return 12;
}
case 0xF3FFFFFC: {
return 13;
}
case 0xCFFFFFFC: {
return 14;
}
case 0x3FFFFFFC: {
return 15;
}
// TODO: We currently treat any unexpected PKRU value as pkey 0 (the shared
// heap) for simplicity since glibc(?) initializes the PKRU to 0x55555554
// (usually). We don't set the PKRU until the first compartment transition, so
// let's default to using the shared heap before our first wrpkru. When we
// initialize the PKRU properly (see issue #95) we should probably abort when
// we see unexpected PKRU values.
default: {
return 0;
}
}
}
#endif // LIBIA2_INSECURE
#include <ia2.h>

using namespace partition_alloc::internal;
using partition_alloc::PartitionOptions;
Expand Down Expand Up @@ -217,5 +147,3 @@ void *ShimCallocWithPkey(size_t num, size_t size, size_t pkey) {
return ret;
}
}

} // extern "C"
2 changes: 1 addition & 1 deletion scripts/partition_alloc/partition_alloc.diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ index 6434ee54..c27030dd 100644
#endif // BUILDFLAG(STARSCAN)

+#include <sys/mman.h>
+#include <ia2_get_pkey.h>
+#include <ia2.h>
+
namespace partition_alloc::internal {

Expand Down

0 comments on commit a22b75f

Please sign in to comment.