60
Job(s) Bless Us! Privileged Operations on macOS

Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

  • Upload
    others

  • View
    7

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Job(s) Bless Us!Privileged Operations on macOS

Page 2: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

@aronskaya 🇺🇦

WWCKyivmacOSChapterLead

SoftwareEngineer,Anti-malwareteam,Triageteam

iaronskaya

Page 3: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

AgendaIntrotoprivilegedoperationsAPIonmacOS

FirstCleanMyMac'ssecurityissue,reportedby

CleanMyMacon

Comparisonofprivilegedoperationsimplementationonand

Summary&Takeaways

Page 4: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Intro to privileged operations API on macOS

Intr

o

Page 5: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

High-level APIs

SMJobBless() AuthorizationExecuteWithPrivileges()

Intr

o

Page 6: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

High-level APIs

SMJobBless() AuthorizationExecuteWithPrivileges()

Intr

o

Page 7: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

High-level APIs

SMJobBless() AuthorizationExecuteWithPrivileges()

Intr

o

Page 8: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Intr

o

There is no ‘UnBless’ 😔

Page 9: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Signing requirements

👑🧢Client Privileged Helper

Client has requirements for Helper(s)

Helper has requirements for Client(s)

OS performs validation of the requirements ONLY on install & update of the Helper

NO validation is performed on establishing XPC connection

Intr

o

Page 10: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

SMJobBless()1. Client has the Privileged Helper

executable in the bundle2. Signing requirements are met

• Both client and Helper are signed • Privileged Helper has a plist file for launchd

embedded into __TEXT section • Privileged Helper has Info.plist embedded • Client has signing requirements listed in its Info.plist

Intr

o

Page 11: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

SMJobBless()3. Obtain Authorization object:

call AuthorizationCreate()

4. Call SMJobBless() with acquired Authorization object

Intr

o

Page 12: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

SMJobBless()5. OS validates code signing requirements in client and helper’s

Info.plist and copies the executable from the bundle to /Library/PrivilegedHelperTools

Intr

o

Page 13: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

SMJobBless()5. Client can establish XPC connection to the Privileged Helper

Intr

o

Page 14: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Apple’s Sample CodeIntr

o

Page 15: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Apple’s Sample CodeIntr

o

Page 16: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Apple’s Sample Code

🤔

Intr

o

Page 17: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Issue #1

🤨

Intr

o

Page 18: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

First security issue,reported by

Talo

s

Page 19: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Zero-Day Reports• November 2018

Talo

s

Page 20: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Talo

s

Page 21: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Stumbled upon Talos’es Zero-Day reports

Contacted Talos for details, they answer the same day

We release a patched update

v. 4.2.0Talos reports insufficient fix

We release a patch v. 4.3.0

Tyler Bohan (Talos) delivers a talk

at OffenciveCon19

TimelineTalo

s

0 1

Page 22: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Tyler Bohan: ‘OSX XPC Revisited - 3rd Party Application Flaws’ at OffensiveCon19

https://www.youtube.com/watch?v=KPzhTqwf0bA

Talo

s

Page 23: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Tyler Bohan: ‘OSX XPC Revisited - 3rd Party Application Flaws’ at OffensiveCon19

https://www.youtube.com/watch?v=KPzhTqwf0bA

Talo

s

Page 24: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

FixTalo

s

Page 25: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Fix #1Talo

s

Page 26: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

on

h1

Page 27: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Timeline

March 2018

MacPaw launched a private h1 program for our other product Setapp

May 2019

CleanMyMac desktop client is added to the scope

h1

Page 28: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Client’s requirements

{

Bundle identifier Signing identity (team id)

{

h1

Page 29: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Client’s requirements

Privileged helper’s executable can be replaced with old version

{

Bundle identifier Signing identity (team id)

{

h1

Page 30: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

What’s the fuss about old versions?h1

Page 31: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

What’s the fuss about old versions?

El Capitan 10.11 Sierra 10.12 High Sierra 10.13 Mojave 10.14 Catalina 10.15

Hardened Runtime introduced in Mojave: • libraries signing validation == protect from dylib injection • remove get-task-allow from entitlements == protect from attaching with debugger

(and other things)

h1

Page 32: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Issue #2: stepsPreconditions: Privileged Helper is not authorized yet. A malicious executable is present on the user’s computer.

1. Download an app version, vulnerable to dylib injection

2. Replace the Privileged Helper executable in the installed app with the vulnerable one

3. User authorizes the Helper

4. Perform a dylib injection into the Helper—it is run as root!

h1

Page 33: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

What about code signing?Replacing the Privileged Helper in the signed bundle doesn’t change anything, because

OS validates the signature only when app is quarantined

After the first launch no signature validation is performed on Mojave.

Time-to-time signature checks were announced in Catalina.

h1

Page 34: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Fix #2h1

{

Version check

Page 35: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Privileged Helper’s requirements

{

Signing identity (team id)

h1

Page 36: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

old 🐞 client versions can connect

Privileged Helper’s requirements

{

Signing identity (team id)

h1

Page 37: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Issue #3: stepsPreconditions: Privileged Helper is authorized. A malicious executable is present on the user’s computer.

• Download an old app version, vulnerable to dylib injection

• Launch client executable with a dylib injection

• Call privileged helper’s methods from the injected code

• In our case it leads to LPE to root

h1

Page 38: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Issue #3: stepsPreconditions: Privileged Helper is authorized. A malicious executable is present on the user’s computer.

• Download an old app version, vulnerable to dylib injection

• Launch client executable with a dylib injection

• Call privileged helper’s methods from the injected code

• In our case it leads to LPE to root

Takeaway: Dylib injection does NOT break the code signature

h1

Page 39: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Fix #3h1

Page 40: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

old 🐞 client versions can connect

other apps of the same vendor can connect

Privileged Helper’s requirements

{

Signing identity (team id)

h1

Page 41: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Fix #4h1

Page 42: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Privileged Helper’s codeh1

Page 43: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

anyone can impersonate the client due to pid checks logic performed by OS

Privileged Helper’s codeh1

Page 44: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Issue #5: anyone can impersonate the client due to 🐞 racy 🐞 pid checks performed by OS

h1

Page 45: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

h1

Page 46: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Fix #5h1

Page 47: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

The APIs are private 😞h1

Page 48: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Privileged operations implementation on

and

Seta

pp

Page 49: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

SMJobBless() AuthorizationExecuteWithPrivileges()

Application

API

# bugs reported* 5

Seta

pp

* as for March 2020

Page 50: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

SMJobBless() AuthorizationExecuteWithPrivileges()

Application

API

# bugs reported* 5 0

* as for March 2020

Seta

pp

Page 51: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Summary & Takeaways

Summ

ary

Page 52: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Takeaways for developers1. Think about security in your project/company. A good start is creating a [email protected] email handle.

2. Have one source of truth for Client’s signing requirements and one for Privileged Helper’s, e.g. put them in Preprocessor Macros and use it in:

ℹ Info.plist file

👑 listener:shouldAcceptNewConnection:

3. In signing requirements check at least for:

🧑💻 signing identity

🆔 bundle identifier

#⃣ minimum version

4. In SecCodeCopyGuestWithAttributes use 🔖 audit token to obtain code reference for signature validation, not the pid

5. In order to be a good citizen remember to unregister the Privileged Helper via launchctl or SMJobRemove API, remove the executable from /Library/PrivilegedHelperTools and the auto generated .plist from /Library/LaunchDaemons

Summ

ary

Page 53: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Example set up requirementsfor Privileged Helper

1. Add User-Defined Build Settings:

CLIENT_REQUIREMENTS="@\"anchor trusted and certificate leaf [subject.CN] = \\\"$(CLIENT_SIGNING_IDENTITY)\\\" and info[CFBundleShortVersionString] >= \\\"$CLIENT_MIN_VERSION\\\" and identifier \\\"$CLIENT_IDENTIFIER\\\"\""

2. Use them to create a macro definition

Summ

ary

Page 54: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

3. Use your Build Settings in Info.plist client requirements:

4. Use the Macro Definition from 2. in code to validate incoming connection:

Example set up requirementsfor Privileged HelperSu

mmar

y

Page 55: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

📝 Summary/WishlistSumm

ary

Page 56: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

📝 Summary/Wishlist1. We need the documentation

There is no easily available Apple’s documentation about securing XPC connection with Privileged Helpers

2. We need Code Samples

Apple’s code samples are not secure

3. Using pid to check the signature of a process is not secure. It should be clearly stated in docs

Checks by pid are racy by nature

4. Audit token should not be private

It is the most secure way, but it is not available to 3rd party developers

5. There should be some Uninstallation API

When the app is being removed, the Helpers are usually forgotten in /Library/PrivilegedHelperTools

Summ

ary

Page 57: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

📖 Further Reading1. project-zero ‘Issue 1223: MacOS/iOS userspace entitlement checking is racy’ by

Ian Beer

2. OffensiveCon19 'OSX XPC Revisited - 3rd Party Application Flaws' by Tyler Bohan

3. Apple Developer Forums 'XPC restricted to processes with the same code signing?'

4. Objective Development ‘The Story Behind CVE-2019-13013’ by Christian from Little Snitch

5. ‘No Privileged Helper Tool Left Behind’ by Erik Berglund

Summ

ary

Page 58: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Call to Action 🧞If I could ask you to do 1 thing, let it be:

Summ

ary

Page 59: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Call to Action 🧞Summ

ary

reporting to Apple, that audit tokens should be made available for 3rd party developers:

If I could ask you to do 1 thing, let it be:

Page 60: Job(s) Bless Us! Privileged Operations on macOS · Anti-malware team, Triage team iaronskaya. Agenda Intro to privileged operations API on macOS First CleanMyMac's security issue,

Thank you! 🤓

iaronskaya