FREE-FALL: TESLA HACKING 2016
Hacking Tesla from Wireless to CAN Bus

Who we are && What we did• Keen Security Lab of Tencent. (aka KeenLab or KeenTeam)• Researchers in KeenLab who are focusing on the cutting-edge security
research of smart cars.
• In September 2016, successfully implemented a remote attack on the Tesla
Model S in both Parking and Driving mode. This remote attack utilized acomplex chain of vulnerabilities.
• More Information• https://www.youtube.com/watch?v=c1XyhReNcHY• http://keenlab.tencent.com/en/2016/09/19/Keen-Security-Lab-of-Tencent-CarHacking-Research-Remote-Attack-to-Tesla-Cars/• https://www.wired.com/2016/09/tesla-responds-chinese-hack-major-securityupgrade/Deliver the Exploit without Physical Access• OLD WebKit used in QtCarBrowser on Tesla• Wi-Fi mode
• Tesla Car automatically scan and connect known SSIDs• “Tesla Guest” with password “abcd123456” in Body shop andSupercharger[1]• QtCarBrowser will automatically reload its current webpage.
• Trigger our WebKit exploit• Cellular mode
• Think about phishing and user mistyping, it’s only restricted byimagination.
[1] http://www.teslamotors.wiki/wiki/Fremont_Supercharger

Attacking Browser• User-Agent• Mozilla/5.0 (X11; Linux)
AppleWebKit/534.34 (KHTML, like Gecko)
QtCarBrowser Safari/534.34• Old WebKit• All the widely used vulnerabilities are patched.
• All the vulnerabilities reported by KeenTeam in 2015 arepatched.

Vulnerability in JSArray::sort()• JSArray::sort• Copy elements into AVLTree• Call compareFunction• Copy elements back into storage.

Root Causestorage
JSC::JSArray::shiftCount()storageAbility: Leak AddressAbility: leak addressstorage• Overlap

m_allocm_length m_num m_spMap subclass Base

ArrayStorage(before unshift)0

ArrayStorage( after unshift)m_allocm_length m_num m_spMap subclass Base

• Type Confusion0

JSValue-A

addr_cell 0xfffffffb

JSValue-B

CellTag

JSArray::sort():addr_cell5

Double…

Ability: UAF

Ability: UAF• Arbitrary address fastFree
new array A155

map=00m_alloc
Base0

JSValue-A

JSValue-B…

0

JSValue-a…

JSValue-A

JSValue-B…

shift() A1 in myCompFunc
0400m_alloc
Base

Copy back in JSC::JSArray::sort()
4400unshift A1 twice to trigger increaseVectorPrefixLength()
fastFree arbitrary address (JSValue-A.payload)Powerful CVE-2011-3928 for leak
• POC

• Type Confusion
typesize

Element

0x34

HTMLInputElement

0x7cPowerful CVE-2011-3928 for leak
• Corrupted HTMLInputElement structure(WebCore::Node *)m_next(WTF::StringImpl *)m_data.m_value.m_impl.m_ptr

aHTMLinputElement=document.importNode(aElement,true);Powerful CVE-2011-3928 for leak
• Corrupted WTF::StringImpl structure

WTF::StringImpl.m_length = 0xb7f458f8.m_data = 1• Arbitrary address read
corruptedString=aHTMLinputElement.value;e.g. corruptedString[0x100000] reads data from MEMORY[0x200001]

Summary• Arbitrary Address READ/WRITE• Leak JSCell address of Uint32Array (sort() vulnerability:leak)• Get address of Uint32Array from JSCell (importNode AAR)
• fastFree the address (sort() vulnerability:fastFree )UAF• Define a new Uint32Array(6) to achieve AAR/AAW• Arbitrary code execute• Insert a javascript function into a array• Leak JSCell address of this function (sort() vulnerability:leak)• Get address of JIT memory from JSCell address (AAR)• Write shellcode to JIT and execute this function (AAW)Shelled, but…
• Low privilege account• browser(uid=2222)• AppArmor/** ix/proc/** r…

• iptables• Accept Internet access• Deny Internal access(except specified port and protocol)

Explore Kernel• OLD kernel• CVE-2013-6282 (put_user/get_user)

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=8404663f81d212918ff85f493649a7991209fa04• Dump kernel

Exploit Kernel• Replace a syscall entry in syscall table• Arbitrary code execute• Call reset_security_ops() in new syscall• Disable AppArmor• Patch sys_resuid()• Get root

Rock’n’Roll with ECUs
• Introduction to the gateway• Run customized firmware• Send messages to other ECUs
• Affect the real world

Introduction to the gateway• Gateway/gw/gtw

ICCID

Gateway

EthernetECUs
CAN BusIntroduction to the gateway• Gateway/gw/gtw• PowerPC chip running RTOS (Most likelyFreeRTOS)• SDCard• Memory mapping
0x0
Bootldr0x20000
Gateway Software

0x40000000
RAM & Updater

Introduction to the gateway• Gateway/gw/gtw• PowerPC chip running RTOS (Most likely FreeRTOS)• SDCard• Memory mapping• Valid IP Packets: Text shell, File xfer, Diagnosticcommands(udp:3500), ...
• See more: Gateway Internals of Tesla Motors, our previous
talk on ZeroNights’16

Rock’n’Roll with ECUs
• Introduction to the gateway• Run customized firmware• Send messages to other ECUs
• Affect the real world

Run Customized Firmwares• Preparation of a ECU Upgrade• Trigger ECU upgrading• ECU Software Upgrade• Modify to make acceptable software package

Preparation of a ECU Upgrade• Files from OTA: Software bundle• Large set of compiled files• Transfer files to SD Card via UDP• "release.tgz" - ECU Software Package• "noboot.img" - ECU Updater• Pull the " update trigger"

Preparation of a ECU Upgrade• In "release.tgz" - ECU Software Package• "*.hex" - Firmware & Calibration Files• "manifest" – Version Infos

Trigger ECU Upgrading• Command to gw: 0x08(update trigger)• UDPSendDiagCommand("\x08noboot.img")• Gateway: Check then rename to boot.img and reboot• Upload "boot.img" directly is forbidden

Trigger ECU Upgrading• Load "boot.img" to 0x4000_0000, then run.
• Most important task is taskUpdate.
• Boot.img header

ECU Software Upgrade• pektronUpdate• Verify the software package• Send each firmware file• Reboot

ECU Software Upgrade• pektronUpdate• Verify the software package• Send each firmware file• Reboot• Happy fact: the big brother shared its log

Modify to make acceptable software
package• Now can run new code by modifying the update
software• “Protected” by CRC32 - can find collision(s)• Now at least we can modify/dump the bootloader.

Modify to make acceptable software
package• An ECU software package("release.tgz") contains:• Manifest file.
• ECU Software(s)• Checksum value. At the end of package.

• To produce a customized package for Gateway:• Re-calculate checksum in gtw.hex• Write a manifest file in the same format• compress.sh gtw.hex manifest | append_crc.sh
release.tgz

Modify to make acceptable software
package• A software package for ECU contains:• Manifest file.
• ECU Software(s)• Checksum value. At the end of file.

• To produce a customized package for Gateway:• Re-calculate checksum in gtw.hex• Write a manifest file in the same format• `compress.sh gtw.hex manifest | append_crc.sh release.tgz`• Modify updater to bypass the verification of "release.tgz"

Rock’n’Roll with ECUs
• Introduction to the gateway• Run customized firmwares• Send messages to other ECUs
• Affect the real world

Send Messages to Other ECUs
• CAN bus is sexy !
• Only way to talk on CAN for CID is gateway• First step: send/sniff any CAN bus via gateway• gw:udp:{20100,20101}• Works by design• Limitations• Limited channels• Not always available

Send Messages to Other ECUs
• Diag 0x04 on GTW is provided to send CAN message• Insert a message in the buffer• Send with other normal instructions• Still limitations• Unable to send under driving mode(pain!)• More reverse engineering….
• Diag 0x01

Send Messages to Other ECUs
• Fixing the limitation might be easy.

UDP Packet
DiagID |DiagContent

DiagID

pDiagHandler

0x01

hfDiag0x01

...

...

0x04

hfDiag0x04

...

...

DiagMsgDispatcher
hfDiag0x01(DiagContent)
fDiag0x01(DiagContent.param1,DiagContent.param2, ...)

Send Messages to Other ECUs
• Fixing the limitation can be easy.
• Swap the handler of 0x04 and 0x01
DiagID

UDP Packet
DiagID |DiagContent

pDiagHandler

0x04

hfDiag0x01

...

...

0x01

hfDiag0x04

...

...

DiagMsgDispatcher
hfDiag0x04(DiagContent)
fDiag0x04(DiagContent.param1,DiagContent.param2, ...)

Send Messages to Other ECUs
• Fixing the limitation can be easy.
• Swap the handler of 0x04 and 0x01
• Then everything works fine, for example• Send command to turn on/off light• Even when driving• Sadly, still limitations

Send Messages to Other ECUs
• Some ECUs just not responding under driving mode
• Broadcasted messages on the bus• Certain ECUs will notice the speed and disable dangerfunctions if necessary• Possible idea: Stop the speed information fromspreading on the whole CAN network

Send Messages to Other ECUs
• Focus on the forwarding table• From 20100/CAN to other CAN bus or UDP• Simple modification to block the forwarding process

Rock’n’Roll with ECUs
• Introduction to the gateway• Run customized firmwares• Send messages to other ECUs
• Affect the real worldAffect the Real World• Protocol used on CAN bus (at least whileupgrading): UDS (ISO 14229)• UDS assigned different ID for each type ofrequest/response:• 27H/67H: Security Access Request/Response• 10H/50H: Session Control• 11H/51H: Reset ECU

Affect the Real World• UDS assigned different ID for each type of request/response• Security Access: Get it to unlock ECU• Something like Challenge-ResponseController

Request: Gain Security Access of Level 10x02 0x27 0x01
Response: Fragmented. Need key. Seed 0x00 0x01 … 0x0F
0x10 0x12 0x67 0x01 0x00 0x01 0x02 0x03 … 0x0F
Request: Fragmented. Provide key. Value 0x35 0x34 … 0x3A
0x10 0x12 0x27 0x02 0x34 0x34 0x37 0x36 … 0x3A
Response: Access Granted.
0x02 0x67 0x02

ECU

Affect the Real World• UDS assigned different ID for each type of request/response:• Security Access: Get it to unlock ECU• Something like Challenge-Response• Vulnerability: the key/seed is fixedAffect the Real World• Gain higher access level  Under flashing mode
• Nothing will work for safety• Make ESP under diag mode, partially worked• No speed info on IC or CAN-CH• Alert info of ABS displayed• Power-assisted steering/breaking disabled.

Conclusion
1. Get control of3G/Wi-Fi

2. Exploit theWebKit Browser

3. Root the invehicle systems

4. Patch andDisable AppArmor

8. Control ECUs toperform somedangerous actions

7. Send malicious
CAN messages onCAN Bus

6. Reprogrammodified Gateway
firmware

5. Bypass ECU's
firmware integrity
verification

CIDECUs

Tesla’s Response• “They did good work. They helped us find something that’s a problem weneeded to fix. And that’s what we did.”-- JB Straubel (Tesla CTO)
• “impressive.”-- Chris EvansTesla’s Response• In just 10 days, Tesla responded with an update to fix
all our vulnerabilities.
• And, there are three big steps:• Browser Security Enhancement• Kernel Security Improvements• Code Signing Protection

Browser Security Enhancement• more strict AppArmor rules• Yes, Tesla uses AppArmor instead of SELinuxowner @{PROC}/** r,@{PROC}/self r,• dmesg restriction
CONFIG_SECURITY_DMESG_RESTRICT=yKernel Security Improvement in Linux 2.6.36• Patched every known vulnerabilities.
• Awesome, auto industry should learn from Tesla.

• For example:• put_user (CVE-2013-6282)• of course they patched this• iovyroot (CVE-2015-1805)• firstly exploited by k33nlab

• dirtycow (CVE-2016-5195)

Step into Linux 4.4.35 era• FROM: Linux version 2.6.36.3-pdk25.023-Tesla-20140430(tomcat7@ci-slave9.fw.teslamotors.com) (gcc version 4.5.2(GCC) ) #see_/etc/commit SMP PREEMPT 120279846• TO: Linux version 4.4.35-release-03mar2017-84029g4ddb263-dirty (tomcat7@ci-slave9.fw.teslamotors.com) (gccversion 4.5.2 (GCC) ) #see_/etc/commit SMP PREEMPT
1202798460

PXN/PAN Emulation Enabled• CONFIG_CPU_SW_DOMAIN_PAN=y• Increase kernel security by ensuring that normal kernel
accesses are unable to access userspace addresses.

Code Signing Everywhere• OTA Packages• ECU Firmwares

Implementation of ECU Code Signing• gtw.hex• boot.img• release.tgz

GatewayESP

gtw.hex
BMS

CID

boot.img
DCDC
release.tgz
otherECUs

Tesla Hacking 2017，Again…
demo video here.