Use after free
UAF is the use of after-free abbreviations, is a more common memory error-based utilization. Many iOS jailbreak are used in this way.
Here's a simple example of what happens to UAF.
Code explains everything
class Car {public:virtual void setValue (int value) = 0; virtual int getValue () = 0; Protected:int Mvalue; }; Class Electric_car:public Car {public:void setValue (int value) {mvalue = value; } int GetValue () {mvalue + = 1; cout<< "This is Electric_car ' s GetValue" <<endl; return mvalue; } }; Class Fuel_car:public Car {public:void setValue (int value) {mvalue = value; } int GetValue () {cout<< "This is Fuel_car ' s GetValue" <<endl; Mvalue + = 100; return mvalue; } }; void Handleobject (car* Car) {car->setvalue (0); Cout<<car->getvalue () <<endl; }
This program has three classes, of which Fuel_car and Electric_car are inherited from car. and the virtual function of car class is realized respectively. So when the program calls Handleobject this function, regardless of whether the argument is Electric_car or Fuel_car,handleobject function can be called normally.
Then let's take a look at the main function of the case
int main(void) { Electric_car *myElectric_car = new Electric_car(); printf("Electric_car=%p\n",myElectric_car); handleObject(myElectric_car); free(myElectric_car); Fuel_car *myFuel_car = new Fuel_car(); printf("Fuel_car=%p\n",myFuel_car); handleObject(c); }
- We'll start with a new Electric_car and then call Handleobject to print its value. Finally, release the Electric_car.
- Next, we new a fuel_car and then call Handleobject to print the Electric_car (note that electric_car, not fuel_car)
What happens when the related function is called by the above steps, and there is a memory leak error under normal circumstances. But the main function above is only free myelectric_car, and the pointer is not also null. At this point, if there is another object (such as the fuel_car above), it is just assigned to the MYELECTRIC_CAR's pointer address. Handleobject will handle this object and will not have an error. This is a typical UAF error.
Simply look at the results of the operation.
myElectric_car=0x15b23a76 This is Electric_car‘s getValue 1 myFuel_car=0x15b23a76 This is Fuel_car‘s getValue 100
You can see that the Electric_car object has an address of 0x15b23a76 in memory, and then the Electric_car is lost. The program then creates another object, Myfuel_car. Because of the nature of the heap, the system will allocate the memory just lost to Myfuel_car. So myfuel_car in-memory addresses are also 0x15b23a76. So when the program calls Handleobject (Myelectric_car), it should be expected to call Electric_car's GetValue () function to invoke the Fuel_car ' GetValue () function, which creates a UAF error.
Summarize
The above is the introduction of the cause of the UAF error, the corresponding method is generally to release memory after also remember to release the pointer. An interesting topic, in the iOS9.0, someone used this UAF error to achieve the jailbreak. The main vulnerability occurs when the function is iohidresourceuserclient. Can you find out the problem (students this is a sub-problem AH)
//---------------------------------------------------------------------------- // IOHIDResourceDeviceUserClient::terminateDevice //---------------------------------------------------------------------------- IOReturn IOHIDResourceDeviceUserClient::terminateDevice() { if (_device) { _device->terminate(); } OSSafeRelease(_device); return kIOReturnSuccess; }
(最近在尝试在gitpage上写文章,所以这篇文章也放到了自己的github上)
What is wrong with iOS UAF errors