Skip to content

[PAC][InstCombine] Replace auth+sign with resign #130807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,34 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
Intrinsic::getOrInsertDeclaration(II->getModule(), NewIntrin);
return CallInst::Create(NewFn, CallArgs);
}
case Intrinsic::ptrauth_sign: {
// Replace auth+sign with a single resign intrinsic.
// When auth and sign operations are performed separately, later compiler
// passes may spill intermediate result to memory as a raw, unprotected
// pointer, which makes it possible for an attacker to replace it under
// PAuth threat model. On the other hand, resign intrinsic is not expanded
// until AsmPrinter, when it is emitted as a contiguous, non-attackable
// sequence of instructions.
Value *Ptr = II->getArgOperand(0);
Value *SignKey = II->getArgOperand(1);
Value *SignDisc = II->getArgOperand(2);

const auto *CI = dyn_cast<CallBase>(Ptr);
if (!CI || CI->getIntrinsicID() != Intrinsic::ptrauth_auth)
break;

Value *BasePtr = CI->getOperand(0);
Value *AuthKey = CI->getArgOperand(1);
Value *AuthDisc = CI->getArgOperand(2);

// Not replacing auth+sign using the same schema with nop, as auth+sign
// pair traps on authentication failure.

Function *NewFn = Intrinsic::getOrInsertDeclaration(
II->getModule(), Intrinsic::ptrauth_resign);
return CallInst::Create(NewFn,
{BasePtr, AuthKey, AuthDisc, SignKey, SignDisc});
}
case Intrinsic::arm_neon_vtbl1:
case Intrinsic::aarch64_neon_tbl1:
if (Value *V = simplifyNeonTbl1(*II, Builder))
Expand Down
58 changes: 58 additions & 0 deletions llvm/test/Transforms/InstCombine/ptrauth-intrinsics.ll
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,64 @@ define i64 @test_ptrauth_resign_ptrauth_constant(ptr %p) {
ret i64 %authed
}

define i64 @test_ptrauth_auth_sign_same_schema(ptr %p) {
; CHECK-LABEL: @test_ptrauth_auth_sign_same_schema(
; CHECK-NEXT: [[P_INT:%.*]] = ptrtoint ptr [[P:%.*]] to i64
; CHECK-NEXT: [[RESIGNED:%.*]] = call i64 @llvm.ptrauth.resign(i64 [[P_INT]], i32 1, i64 1234, i32 1, i64 1234)
; CHECK-NEXT: ret i64 [[RESIGNED]]
;
%p.int = ptrtoint ptr %p to i64
%authed = call i64 @llvm.ptrauth.auth(i64 %p.int, i32 1, i64 1234)
%resigned = call i64 @llvm.ptrauth.sign(i64 %authed, i32 1, i64 1234)
ret i64 %resigned
}

define i64 @test_ptrauth_auth_sign_opaque_disc_same_schema(ptr %p, i64 %disc) {
; CHECK-LABEL: @test_ptrauth_auth_sign_opaque_disc_same_schema(
; CHECK-NEXT: [[P_INT:%.*]] = ptrtoint ptr [[P:%.*]] to i64
; CHECK-NEXT: [[RESIGNED:%.*]] = call i64 @llvm.ptrauth.resign(i64 [[P_INT]], i32 1, i64 [[DISC:%.*]], i32 1, i64 [[DISC]])
; CHECK-NEXT: ret i64 [[RESIGNED]]
;
%p.int = ptrtoint ptr %p to i64
%authed = call i64 @llvm.ptrauth.auth(i64 %p.int, i32 1, i64 %disc)
%resigned = call i64 @llvm.ptrauth.sign(i64 %authed, i32 1, i64 %disc)
ret i64 %resigned
}

define i64 @test_ptrauth_auth_sign_different_disc(ptr %p, i64 %disc) {
; CHECK-LABEL: @test_ptrauth_auth_sign_different_disc(
; CHECK-NEXT: [[P_INT:%.*]] = ptrtoint ptr [[P:%.*]] to i64
; CHECK-NEXT: [[RESIGNED:%.*]] = call i64 @llvm.ptrauth.resign(i64 [[P_INT]], i32 1, i64 [[DISC:%.*]], i32 1, i64 1234)
; CHECK-NEXT: ret i64 [[RESIGNED]]
;
%p.int = ptrtoint ptr %p to i64
%authed = call i64 @llvm.ptrauth.auth(i64 %p.int, i32 1, i64 %disc)
%resigned = call i64 @llvm.ptrauth.sign(i64 %authed, i32 1, i64 1234)
ret i64 %resigned
}

define i64 @test_ptrauth_auth_sign_different_key(ptr %p) {
; CHECK-LABEL: @test_ptrauth_auth_sign_different_key(
; CHECK-NEXT: [[P_INT:%.*]] = ptrtoint ptr [[P:%.*]] to i64
; CHECK-NEXT: [[RESIGNED:%.*]] = call i64 @llvm.ptrauth.resign(i64 [[P_INT]], i32 0, i64 1234, i32 1, i64 1234)
; CHECK-NEXT: ret i64 [[RESIGNED]]
;
%p.int = ptrtoint ptr %p to i64
%authed = call i64 @llvm.ptrauth.auth(i64 %p.int, i32 0, i64 1234)
%resigned = call i64 @llvm.ptrauth.sign(i64 %authed, i32 1, i64 1234)
ret i64 %resigned
}

define i64 @test_ptrauth_sign_nonauth_nonconst_disc(i64 %disc) {
; CHECK-LABEL: @test_ptrauth_sign_nonauth_nonconst_disc(
; CHECK-NEXT: [[SIGNED:%.*]] = call i64 @llvm.ptrauth.sign(i64 ptrtoint (ptr @foo to i64), i32 1, i64 [[DISC:%.*]])
; CHECK-NEXT: ret i64 [[SIGNED]]
;
%foo.int = ptrtoint ptr @foo to i64
%signed = call i64 @llvm.ptrauth.sign(i64 %foo.int, i32 1, i64 %disc)
ret i64 %signed
}

declare i64 @llvm.ptrauth.auth(i64, i32, i64)
declare i64 @llvm.ptrauth.sign(i64, i32, i64)
declare i64 @llvm.ptrauth.resign(i64, i32, i64, i32, i64)
Expand Down