WEBVTT

00:08.040 --> 00:09.860
Hello, everyone.

00:09.860 --> 00:10.760
Good afternoon.

00:10.760 --> 00:14.080
I hope you're enjoying Devcon as well as I am.

00:14.560 --> 00:22.820
So my talk is about Androzia, which is a tool for securing data in process for your Android applications.

00:23.140 --> 00:34.020
So if I had to explain what Androzia is doing in a couple of lines, it's basically providing you the features of a proactive garbage collector.

00:34.480 --> 00:39.580
And garbage collectors inherently are lazy.

00:39.580 --> 00:44.480
So they only kick in when your application is memory constrained.

00:44.480 --> 00:48.680
And they will reclaim memory from objects which are unreferenced.

00:48.680 --> 00:58.980
But with Androzia, you will be able to reclaim memory from objects immediately after their last use.

00:59.280 --> 01:17.620
So this is helpful because if you have a lot of sensitive information lying around in your heap memory, as soon as it is last used, Androzia is going to kick in and collect all that sensitive information from your application and replace the memory contents with the default values.

01:17.620 --> 01:21.100
So your sensitive data will not remain on the memory anymore.

01:21.100 --> 01:24.160
So before we begin, a little bit about myself.

01:24.160 --> 01:24.800
I'm Samit.

01:24.800 --> 01:27.740
I work for the product security team at Citrix.

01:27.840 --> 01:31.080
I'm a web and mobile application security enthusiast.

01:31.080 --> 01:40.600
Spoken at a bunch of conferences, including Black Hat Asia, AppSec USA, Cocon, CodeBlue, IEEE Services, MobileSoft, and CloudCom.

01:41.220 --> 01:47.580
So, yeah, a little bit of contact information in case you want to contact me.

01:49.380 --> 01:57.780
So starting off, the first question for you is, which one of these is the most difficult to protect?

01:58.280 --> 02:03.220
Number one data at rest, number two data in process, or number three data in motion?

02:03.900 --> 02:07.800
So arguably, I would say that it's number two.

02:07.800 --> 02:08.740
Why?

02:09.220 --> 02:14.540
Because the least number of solutions in the market are for number two.

02:14.540 --> 02:17.100
Because this is a very gray area.

02:18.160 --> 02:23.400
You know, EMM providers do not really understand how to protect data in process.

02:23.760 --> 02:25.680
Data at rest is very simple.

02:25.680 --> 02:28.860
You use any ciphers and then protect your data.

02:28.860 --> 02:31.440
Data in motion can be protected using TLS.

02:31.480 --> 02:36.840
However, data in process is still a mystery for most of us.

02:36.840 --> 02:39.780
So this tool targets number two.

02:40.640 --> 02:44.380
And we'll see how, yeah.

02:44.380 --> 02:47.020
So, going ahead.

02:48.780 --> 02:55.200
So Java creates a lot of objects, or Android itself creates a lot of objects on the heap, right?

02:55.200 --> 03:08.920
And the objects may contain a lot of sensitive information which may include your authentication credentials, authorisation tokens, and encryption decryption keys, bins, ODPs, and personally identifiable information.

03:09.300 --> 03:15.560
So, as in when the application is executing, all of these will be on your heap memory.

03:15.560 --> 03:23.260
So the developers want to ensure that they get rid of all the sensitive information as soon as it is last used in the programme.

03:24.400 --> 03:28.360
So the myth is that the garbage collector will eventually collect it.

03:28.380 --> 03:32.400
But, you know, the garbage collector is very lazy, as I said.

03:32.400 --> 03:39.160
It will not come into the picture, or it will not collect all your data until it is essential for it.

03:39.160 --> 03:44.060
Because unless your application is memory-constrained, it will not come into the picture.

03:44.060 --> 03:48.180
And even if it does, right, its scope is very limited.

03:48.540 --> 03:53.320
So this is something which is called as an object tree on the heap.

03:53.540 --> 04:00.200
And how garbage collectors actually does a collection is using mark-and-sweep algorithm.

04:00.380 --> 04:05.140
So it will start off from the root nodes which are called GC root.

04:05.560 --> 04:14.900
And from the GC root, it traverses the object tree using depth-first search and keeps on marking all the objects that are reachable.

04:15.120 --> 04:26.860
In the sweep phase of the direct garbage collector's collection process, it is going to find out all the objects on the heap which are unreachable, and it is going to collect only those objects.

04:27.500 --> 04:41.420
So when a developer is, you know, writing code, he may forget to unreference some objects which may be these ones which are in the circle.

04:42.220 --> 04:49.820
And garbage collector will not collect these set of objects just because they are reachable from the GC roots.

04:50.240 --> 05:00.880
And these objects may contain sensitive information and they're going to lie around in your application throughout the lifetime of your application until the application is terminated.

05:01.420 --> 05:05.160
So this is where Androsia kicks in.

05:05.160 --> 05:10.800
It will clean off all the unused but reachable objects in your memory.

05:10.820 --> 05:19.000
So to summarize what I've been talking about until now, reachable and unused string builder objects may contain sensitive information.

05:19.000 --> 05:24.480
So throughout the course of this presentation, I'm going to specifically talk about string builder objects as an example.

05:24.480 --> 05:29.460
But this framework is, you know, extendable to any other kind of objects as well.

05:29.980 --> 05:41.240
So reachable and unused string builder objects may contain sensitive information and a heap dump or an application compromise will help an attacker to reveal that sensitive information easily.

05:41.260 --> 05:50.080
So don't just rely on the garbage collector to collect all that information, but instead destroy by overwriting all the critical data that you have in the application.

05:51.760 --> 06:02.920
So with that in mind, we do have some solutions provided by Java libraries which allow you to destroy objects that are created on the heap.

06:03.200 --> 06:07.520
One of them is the keystore.password protection class.

06:07.760 --> 06:13.160
And this class provides a destroy method which can be used to destroy the object's content.

06:14.480 --> 06:20.940
But again, you know, it's the developers, you know, it's upon the developer to use this API.

06:20.940 --> 06:25.640
He may use it at a very late stage in the program, or he may not even use it at all.

06:25.720 --> 06:28.840
So, ideally, we want to automate this process.

06:28.840 --> 06:31.660
We don't want to rely on the developer to destroy our data.

06:31.660 --> 06:34.240
Our data should be in our hands, not the developer's hands.

06:34.240 --> 06:44.700
So, you know, this is where Androsia will again automate that process and instrument code to remove or destroy any object that has critical information.

06:47.930 --> 06:49.990
So how does Androsia help?

06:52.630 --> 07:00.330
So Androsia uses static code analysis, and it determines the last use of objects at a whole program level.

07:01.890 --> 07:07.170
If I had to be specific, it uses a summary-based interprocedural data flow analysis.

07:07.450 --> 07:09.730
So there are three terms here, three key terms.

07:09.730 --> 07:14.450
One is summary-based, second is interprocedural, and the third one is data flow analysis.

07:14.450 --> 07:24.470
So static code analysis power is just not grabbing data and giving you information about what the Android applications permissions are or whatever stuff.

07:24.470 --> 07:26.690
It comes from data flow analysis.

07:26.690 --> 07:31.410
So every statement in your program has some data to contribute.

07:31.730 --> 07:43.950
And that data needs to be propagated across all the statements in your application, and every statement could probably add or remove some data from the data flow sets that are going on.

07:44.010 --> 07:47.990
So that's how powerful data flow analysis is.

07:47.990 --> 07:55.970
It will help you perform any sort of data related analysis, which may include even taint analysis.

07:56.650 --> 08:05.610
So that's where the power of static code analysis is better used.

08:05.870 --> 08:11.250
What I mean by summary-based interprocedural analysis is that this is a whole program analysis.

08:11.250 --> 08:13.950
This is not just analyzing a specific method.

08:13.970 --> 08:20.790
And every method, when it is analyzed, the analysis results can be maintained or cached in a summary.

08:21.750 --> 08:27.790
The reason for that is that a particular method, let's say foo, can be called from multiple locations.

08:27.790 --> 08:37.590
And if you have already computed the summary for foo, you may want to reuse that computed summary instead of computing it again and again whenever the method is called.

08:38.010 --> 08:44.150
So that's why we use summary as a cache for storing our analysis results for a specific method.

08:44.270 --> 08:51.990
And after we have determined the last usage point of a particular object, we are going to instrument byte code to clear its memory content.

08:53.870 --> 09:04.050
So to give you a very simple example, I have statements numbered 1 to 10 here, and there is a definition of variable x at statement number 3.

09:04.470 --> 09:08.310
And then there is a use of variable x at statement number 5.

09:08.590 --> 09:16.490
So between statement number 3 and 5, since the variable is defined and used, I can say that the variable is live.

09:17.510 --> 09:23.010
But before statement number 3, the variable x wasn't even defined and neither was it used.

09:23.010 --> 09:26.430
So it is said to be dead between 1 and 3.

09:26.430 --> 09:34.190
And now at 7, the variable x gets redefined and then reused at 8.

09:34.190 --> 09:37.570
So between 7 and 8, the variable is again live.

09:37.610 --> 09:40.810
However, between 5 and 7, the variable won't be live.

09:40.810 --> 09:43.630
It has been defined, but it is not being used.

09:44.510 --> 09:50.250
So this is just to give you an idea about how liveness of a variable looks like.

09:50.250 --> 10:01.070
And we are going to use it further ahead when we are seeing how Androsia really works and how it computes the liveness sets and how it then infers the last usage point.

10:04.200 --> 10:11.040
So basically, if you have a span of definition and usage, between that span, a variable is going to be live.

10:13.540 --> 10:18.400
So this is a snapshot of the heap dump taken using Eclipse Memory Analyzer Toolkit.

10:18.400 --> 10:26.880
So I had this sample code which contained a static field which is called static secret and it contained a password.

10:27.000 --> 10:29.200
And it shows up in the heap dump.

10:29.200 --> 10:40.220
And after the optimization or after the instrumentation, the static secret does not contain any password at all because Androsia has successfully removed the password from the heap memory.

10:40.220 --> 10:53.180
So, in order to give you an overview where Androsia would fit in is that a user could provide an application or source code to Androsia server.

10:53.180 --> 10:58.340
And Androsia server can then unpack the application to Dalvik byte code.

11:00.100 --> 11:04.640
And the Dalvik byte code gets converted into Gimple code.

11:04.740 --> 11:07.800
Now, Gimple is an intermediate representation.

11:08.320 --> 11:14.300
Just like Smiley, it is an intermediate representation which takes good from both the worlds.

11:14.300 --> 11:20.700
It takes good from the Dalvik byte code as well as good from the Java high-level language as well.

11:20.720 --> 11:25.680
So Gimple is basically a mixture of Java and it is simple so they call it Gimple.

11:26.180 --> 11:29.300
So Gimple has only 15 different kind of statements.

11:29.300 --> 11:37.380
Unlike Java which has a lot of complicated structures and different kind of statements, even Dalvik byte code has like 200 different opcodes.

11:37.620 --> 11:47.860
So Gimple has only a precise number of statements which is 15 and, you know, it is easier to analyze when you have just 15 odd statements to play around with.

11:47.880 --> 12:08.540
And this Gimple code is then analyzed by the analysis that we have embedded into Androsia and then the code instrumentation happens and then we convert the Gimple code back to Dalvik byte code and then the Dalvik byte code is packaged again and signed or we could just provide the analysis results to the user.

12:09.700 --> 12:15.440
So this is the entire flow of how will you eventually use Androsia.

12:16.920 --> 12:28.840
So an important fact that I should also mention here is that the framework which is called suit which is the backbone of Androsia can be used to perform any sort of data flow analysis.

12:28.840 --> 12:31.740
It's not just the analysis that I have done.

12:31.780 --> 12:39.200
So I'm going to talk about suit next and it can be used, you know, you can build your own data flow analysis over suit.

12:39.980 --> 12:49.640
So suit is basically static, so it's a framework for Java byte code analysis and it can be used to implement your own analysis as well.

12:50.040 --> 12:55.760
It provides this three address code representation called Gimple and it looks something like this.

12:55.760 --> 13:05.520
So for example, I have an if statement here and in any line of Gimple code, you will not see more than three operands in one line.

13:05.520 --> 13:09.060
So you have R1, null and label zero here.

13:09.280 --> 13:12.500
So it is very simple to view, you know.

13:12.500 --> 13:18.220
It's not like Java that you can have a lot of complicated structures in one statement itself.

13:19.520 --> 13:31.660
And this diagram shows you that the suit framework itself takes in the Java source code or the class files and converts it into Gimple three address intermediate representation.

13:32.000 --> 13:37.860
And then you can perform your analysis optimization and then again convert it back to the class files.

13:38.040 --> 13:48.960
So until recently, actually, you know, as I talked about that we are going to do an inter-procedural analysis, so suit also provides you the ability to perform an intra-procedural analysis.

13:51.300 --> 13:57.180
So until now, until recently, suit was missing a Dalvik to Gimple transformation module.

13:57.620 --> 14:02.280
And now that void has been fulfilled.

14:02.940 --> 14:10.800
And that void has been fulfilled by a plugin called Dexplore which allows you to transform Dalvik to Gimple code.

14:11.820 --> 14:15.560
So it makes it easier for you to analyse any Android application.

14:16.040 --> 14:16.480
Okay.

14:16.480 --> 14:21.460
So there's one more tool that I want to, you know, talk about here which is called FlowDroid.

14:21.680 --> 14:31.300
Now, FlowDroid allows you to generate a dummy main method because unlike Java, Android does not have, Android applications do not have a main method.

14:31.520 --> 14:35.520
And to start your data flow analysis, you need a starting point.

14:35.600 --> 14:39.960
And this dummy main method acts like a starting point for your data flow analysis.

14:43.290 --> 14:48.330
The dummy main method actually connects all the Android life cycle callbacks.

14:48.330 --> 14:51.250
So it gives you, you

15:04.630 --> 15:13.670
know, a dummy main method.

15:13.850 --> 15:17.470
So that way, you can start your data flow analysis from a single point.

15:17.710 --> 15:18.770
Okay.

15:18.770 --> 15:24.030
So objects can, you know, exist in various scopes.

15:24.050 --> 15:27.890
They can either exist as a local variable inside a method.

15:27.890 --> 15:35.010
For example, I have XYZ variables here which contain eventually the string builder objects.

15:35.030 --> 15:38.710
And their scope is limited to this method foo only.

15:38.710 --> 15:41.450
They're not used outside this method anywhere.

15:41.630 --> 15:50.710
So this is one simple example which I'm going to take or, you know, it will be a walk-through example throughout the course of this presentation.

15:50.830 --> 15:53.110
So this example will be recurring.

15:53.950 --> 16:00.530
And I have a variable X here which I'm instantiating with a string builder object.

16:00.630 --> 16:05.090
So basically X will be referring to the string builder which contains the value secret.

16:05.270 --> 16:08.510
Y will be referring to the string builder which contains the value password.

16:08.510 --> 16:14.690
And there's some logic here which I'm going to, you know, I'll just add it for the sake of explaining things.

16:15.430 --> 16:24.710
So the scope could be either as a local variable or your objects could even exist as static fields of a class.

16:24.750 --> 16:30.330
And this is completely different because, you know, the scope of a static field is the entire program.

16:30.330 --> 16:32.590
It's not just a specific method.

16:33.790 --> 16:41.830
So X, the value X, can be invoked from anywhere else using the class name which is my class.

16:43.610 --> 16:50.970
So this is how you will invoke variable, sorry, this is how you will invoke variable X.

16:54.020 --> 16:58.980
And this invocation can happen in any other method which is bar in my case.

16:59.280 --> 17:02.820
So the scope, you know, becomes the entire program.

17:04.020 --> 17:07.360
Or you can have objects as instance field.

17:07.520 --> 17:17.220
Here you can see that I have a private access modifier specified for instance field X inside the my instance field string builder class.

17:17.300 --> 17:27.560
So the scope of this variable X will now be limited to the scope of the object of this class, my instance field SB.

17:30.710 --> 17:31.390
Okay.

17:31.610 --> 17:36.830
So coming to the demo, I have three classes here.

17:36.830 --> 17:37.410
So I have three classes here .

17:37.610 --> 17:41.770
And one of them is the main activity where my control will begin.

17:41.950 --> 17:45.190
And this is where the secret has been defined.

17:45.470 --> 17:48.290
And then I'm calling the useStaticField method.

17:49.150 --> 17:55.170
And the useStaticField method uses the static secret and then calls bar method.

17:55.290 --> 17:58.850
And inside the bar method, I'm again using the static secret.

17:59.470 --> 18:08.230
So if I had to, you know, just ask you guys to point out the last usage of the static secret, it would be like inside the bar method, right?

18:08.230 --> 18:08.850
Here.

18:08.850 --> 18:14.390
So the instrumentation should happen immediately after the system.out.println statement of static secret.

18:16.290 --> 18:17.290
That's correct.

18:17.290 --> 18:19.710
But, you know, there can be loops as well.

18:20.370 --> 18:23.030
Like this statement could be inside a for loop.

18:23.030 --> 18:26.590
And then the instrumentation point will not be immediately after static secret.

18:26.590 --> 18:29.770
It will be right before the return statement of bar method.

18:29.770 --> 18:36.230
If there's a loop outside the call to the bar method, then your instrumentation will happen here.

18:36.430 --> 18:37.270
Why is that?

18:37.270 --> 18:43.390
Because if you instrument it within the loop, then your logic of the application will break.

18:43.390 --> 18:46.370
And you don't want that to happen.

18:47.230 --> 18:53.090
So we'll see an example, actually a demo on it, which is on the next slide.

18:53.590 --> 18:56.790
So this is the same code which I showed you on the last slide.

18:57.190 --> 19:03.570
I have a password string builder object being created which contains the value password.

19:03.750 --> 19:06.390
And then I'm calling the useStaticField method here.

19:07.270 --> 19:10.870
Now we'll see the definition of the useStaticField.

19:10.870 --> 19:14.930
The useStaticField is using the static secret and calling bar method.

19:15.450 --> 19:19.070
And inside the bar method, we have again useStaticSecret.

19:19.070 --> 19:23.390
And then we have two print statements which are printing hello and bye.

19:24.530 --> 19:33.670
So if you see, the instrumentation point should be right now just after the last use of static secret, which is on line 20.

19:33.670 --> 19:36.830
So let's run the tool on this.

19:41.650 --> 19:44.710
So this is how the dummy main method looks like.

19:44.710 --> 19:47.970
You'll see it in the stack trace that gets printed out.

19:51.090 --> 19:56.750
And right now, the output format is set to jimple so that we can see where the instrumentation is happening.

19:56.750 --> 20:00.850
You can even set it to Dalvik bytecode so you'll get a dex format as an output.

20:02.870 --> 20:03.510
Alright.

20:03.510 --> 20:06.670
We're in the onCreate method, but we need to go to the bar method, right?

20:06.750 --> 20:14.830
So inside the bar method... so can we pause the video, please?

20:16.530 --> 20:25.410
So you'll see that right here, you have the print line statement for static secret.

20:25.410 --> 20:29.810
This is the static secret reference variable, which is an R2.

20:29.970 --> 20:32.310
And you're printing the value of R2.

20:33.830 --> 20:42.210
And from line number 30 to 34 is where the code from Androsia that has been instrumented.

20:42.210 --> 20:46.410
So what I'm doing here is that I'm getting a reference to the static secret field.

20:46.410 --> 20:50.130
I'm calculating the length of the static secret field.

20:50.570 --> 20:58.670
And I'm using the delete API from zero to the length of the static secret to remove or clear its memory content.

20:59.170 --> 21:06.770
So the instrumentation point right now is right after the print line statement for static secret, which is the correct instrumentation point.

21:06.870 --> 21:08.790
So can we resume the video, please?

21:11.150 --> 21:12.150
Alright.

21:14.130 --> 21:18.170
Now what we're going to do is that we're going to remove the for loop here.

21:18.170 --> 21:20.890
We're going to uncomment the for loop here.

21:21.190 --> 21:32.930
And now the instrumentation point will not be between, will not be actually right after the static secret statement, but it will be right before the by statement here.

21:32.930 --> 21:37.510
The system.out.print line for the by statement, by keyword.

21:37.510 --> 21:39.090
So yeah.

21:39.090 --> 21:47.730
That's because you don't want to instrument within the loop, because in the next iteration of the loop, your resetted value will be used, which will break the logic of the code.

21:48.350 --> 21:49.050
Yeah.

21:49.150 --> 22:01.650
So this time the instrumentation, which is from 31 to 34, 33, is happening right before the print statement for by, which is the right instrumentation point.

22:01.750 --> 22:09.650
And Androzia is smart enough to figure out if there is a loop, it can hoist the instrumentation point outside the loop.

22:09.670 --> 22:12.650
So that's the main idea that I want to convey here.

22:13.050 --> 22:16.870
And that goes on for any level of nested for loops.

22:16.870 --> 22:28.410
You know, you can have the bar method being called from a for loop like this, and then the instrumentation point will change to somewhere in between, somewhere just before returning the use static field method.

22:28.490 --> 22:36.010
So the return statement would be somewhere here in the Gimple code, and the instrumentation point will be right before the return statement.

22:37.050 --> 22:47.750
This is the use static field, and right before the return statement of the use static field, you'll find the instrumented code from line number 73 to 74, 75, actually.

22:48.010 --> 22:49.110
Even 77.

22:49.110 --> 22:53.630
So this is the part which gets instrumented, just before the return statement.

22:53.750 --> 23:08.890
So you see that the instrumentation points are changing according to the loops that you have inside the code, which is, you know, which may not be because of nested loops, it may be even because of recursion of a specific method.

23:08.890 --> 23:16.170
So Androsia will be smartly identifying if there is recursion or loops, and changing the instrumentation points accordingly.

23:16.670 --> 23:29.290
So we've seen how Androsia is doing or performing the instrumentation, but we really don't know what is the logic behind that, what is the algorithm that is running behind Androsia.

23:29.490 --> 23:36.690
So to come to that, first let's identify what information can a single statement provide us.

23:36.750 --> 23:41.630
So there is, every line of statement has some or the other data to add, right?

23:41.630 --> 23:46.090
And the data that we are concerned about here is called liveness data.

23:50.660 --> 24:04.600
So the next few slides are going to talk about the live variable analysis definition, and how we compute the summary for every method, which is going to look something like this.

24:04.600 --> 24:07.100
It will be a two-tuple combination.

24:07.480 --> 24:10.060
Basically, the first tuple will be the variable.

24:10.140 --> 24:19.740
So the summary of foo method will tell you that the variable x was last used in the statement if y.length is less than x.length.

24:19.740 --> 24:21.280
It will be something like this.

24:21.400 --> 24:33.880
So for every variable, you will have, you know, a two-tuple element, and the summary will tell you how many variables are there, and what is the last usage point of every variable inside it.

24:34.600 --> 24:38.160
So to compute the summary, we have a two-step process.

24:38.160 --> 24:44.380
One is to use of, one is to compute the definition and use sets for every statement.

24:44.820 --> 24:51.160
Then using the definition and use sets, we are going to compute live variable sets for every statement.

24:52.080 --> 24:53.960
LVENTRY and LVEXIT.

24:55.060 --> 25:05.220
And using the live variable entry and exit sets, we're going to infer the last usage point for a local or static field reference within that method.

25:05.280 --> 25:07.320
And we're going to store that as a summary.

25:10.190 --> 25:23.090
And now, once we have the summaries for every method that is analyzed by Androsia, we're going to compute the last usage point of a static field reference, considering the whole program, not just within a method.

25:23.090 --> 25:29.590
Because we know that the summary will tell us that within a method, this is the last usage point of a particular local variable.

25:29.590 --> 25:34.490
But it will not tell us across the entire program where is the last usage actually happening.

25:34.490 --> 25:41.030
So the summaries need to be combined and propagated across your program to infer that knowledge.

25:44.400 --> 25:59.860
So to start off with the definition of live variable analysis, live variable analysis determines for each statement, which variables must have a subsequent use prior to the next definition, which is what we learned from the diagram we saw earlier, right?

25:59.860 --> 26:02.560
We had a definition and use of variable X.

26:02.560 --> 26:04.760
And between that span, the variable was live.

26:04.760 --> 26:06.600
And after that, it was dead.

26:06.600 --> 26:09.440
So this is exactly what the statement is telling us.

26:09.500 --> 26:17.980
So in this particular code, the last usage point of variable X is this blue statement, which is number 4.

26:18.120 --> 26:23.260
And the last usage point of Y is statement number 5 and 6.

26:23.260 --> 26:29.600
Because they are two different statements within the if clause.

26:29.600 --> 26:35.360
So either this one could execute or this one could execute based on the result from the if clause.

26:35.660 --> 26:39.060
And the last use of Z is statement number 7.

26:39.060 --> 26:40.400
So this is very intuitive, right?

26:40.400 --> 26:41.680
We can see it.

26:41.680 --> 26:47.180
But how do you determine this using an automated way?

26:47.180 --> 26:48.460
That's the question.

26:48.720 --> 26:51.200
And it's not as easy as it seems like.

26:51.200 --> 26:59.980
So we're going to run through this same example in the next few slides and figure out how we determine the last usage points of X, Y, and Z.

27:00.120 --> 27:06.160
So the last usage point of a variable is also the last statement where that variable was live.

27:06.620 --> 27:09.360
So X is last live here.

27:09.360 --> 27:12.540
And it is also the last usage point of that variable X.

27:12.600 --> 27:17.580
So we're going to use this fact to determine the last usage point of every variable.

27:19.920 --> 27:30.340
So talking about the def use sets, the definition of a definition set is that it will contain all the variables defined in a particular statement.

27:30.460 --> 27:34.220
So in statement number 1, X is being defined.

27:34.360 --> 27:37.420
So for statement 1, you'll have an entry X.

27:37.980 --> 27:43.340
So the use set are the set of variables that are used in a statement.

27:43.580 --> 27:49.620
So if we run through this example and compute the def use set, they will look something like this.

27:49.620 --> 27:50.960
So we start from the bottom.

27:50.960 --> 27:52.660
Z is being used here.

27:52.660 --> 27:57.600
So it will go in the use column for statement number 7.

27:58.540 --> 28:00.120
And X is being defined here.

28:00.120 --> 28:03.380
So it will go in the definition column for statement number 7.

28:03.380 --> 28:09.400
And similarly, if we keep going, it will eventually populate the entire table.

28:10.420 --> 28:19.640
So the direction of propagating that data or propagating the data flow facts is from bottom to top.

28:19.700 --> 28:23.160
It is the reverse order of execution of the program.

28:23.540 --> 28:34.500
So if I compute the live variable of exit of 6th statement, which is the last statement in my code, then it is going to be 5.

28:34.600 --> 28:40.100
Because there is no other variable which is being used after statement number 6.

28:40.440 --> 28:41.720
So this is a must.

28:41.720 --> 28:47.960
Like this has to be 5 because there cannot be any other variable which is used after the last statement in the code.

28:48.080 --> 28:52.400
So this will be an initial point for the algorithm to run.

28:52.520 --> 29:00.440
And once you compute LVExit of 6, you are going to use the def use set of 6 to compute LVEntry of 6.

29:00.940 --> 29:10.040
And then you will compute LVExit of 5, then you will compute LVEntry of 5, then LVExit of 4, LVEntry of 4.

29:10.040 --> 29:22.540
Similarly, once you reach at a point where there is two branches, you are going to merge the results from LVEntry 3 and LVEntry 4 using a union operator.

29:25.260 --> 29:29.080
And that result will go into LVExit of 2.

29:29.380 --> 29:33.780
And using LVExit of 2, we are going to compute LVEntry of 2 and so on.

29:34.960 --> 29:36.500
So that's up next.

29:36.500 --> 29:42.960
We are going to use the same example, the one we discussed for the def use set and we are going to compute these things.

29:43.080 --> 29:53.820
So if I had to give you a mathematical representation, I mean, there's nothing possible without math here because data flow analysis needs to have math backing it up for proof of correctness.

29:53.820 --> 29:59.780
And without any algorithm or without some math, there's no way you can perform a data flow analysis.

30:00.180 --> 30:10.620
So the LVExit of a statement L will be 5 if L is the last statement of the body, which is what we discussed on the last slide.

30:10.880 --> 30:17.460
And it will be the union over the LVEntry set of the successors of L.

30:18.880 --> 30:30.480
Otherwise, so if you have a merge point, we'll have to take this LVEntry sets and do a union over all the entry sets to compute LVExit of L.

30:30.640 --> 30:40.920
Now once we have the LVExit of L, we are going to plug that value here and we already computed LVEfinition and use it for a statement L.

30:41.660 --> 30:44.780
And that's going to give us LVEntry of L.

30:46.060 --> 30:47.800
Let's see this in action.

30:47.860 --> 30:50.920
So we have the def use columns populated.

30:52.480 --> 30:58.000
We know that the LVExit of 7 statement will be 5 because this is the last statement.

30:58.420 --> 31:03.300
And now we are going to use this formula to compute LVEntry of 7.

31:03.300 --> 31:12.920
So LVExit of 7 will be 5 minus x union z, which will give you z.

31:14.060 --> 31:19.620
And then LVEntry of 7 will form the LVExit of 6.

31:19.700 --> 31:22.660
So LVExit of 6 will be z.

31:22.860 --> 31:27.620
And now z minus z union y will give you y.

31:27.620 --> 31:31.620
So you get the LVEntry of statement number 6 as well.

31:31.620 --> 31:39.780
And so on, you can populate this table iteratively and you get the entire table.

31:39.900 --> 31:58.040
Now, the interesting part to note here is that if a variable disappears from the entry set to the exit set in the live variable table, like in the fourth statement, we have x disappearing from the entry set to the exit set.

31:58.040 --> 32:03.560
In the fifth and sixth statement, y disappears from entry set to the exit set.

32:03.560 --> 32:10.120
And in the seventh statement, we see that z disappears from the entry set to the exit set.

32:10.120 --> 32:15.540
So those will be, the corresponding numbers will be the last usage point of those variables.

32:16.060 --> 32:17.820
And that's what we were after.

32:17.820 --> 32:19.100
We've got that now.

32:19.640 --> 32:28.120
So now we're going to store all these results, the last usage point results, as the summary of foo method.

32:29.400 --> 32:35.860
And we're going to use this summary in other parts of the program when the foo method gets invoked from there.

32:35.860 --> 32:38.620
We don't want to compute this again and again and again.

32:40.020 --> 32:45.440
So let's just say that foo was, there was a method foo which calls bar and bar called baz.

32:45.440 --> 32:46.200
Okay?

32:46.200 --> 32:54.640
So, and just, we've already computed, let's just assume that we've already computed summaries for each of these individual methods.

32:54.640 --> 32:56.440
Foo, bar, as well as baz.

32:56.520 --> 33:03.880
And baz will tell me that, the summary of baz is going to tell me that static field reference was last used at C4.

33:04.140 --> 33:09.920
The summary for bar is telling me that the static field reference was used at B5.

33:09.920 --> 33:12.740
And there was no use of SFR in foo.

33:13.020 --> 33:21.020
Now what I'm going to do is, I'm going to propagate the summaries across these methods in a reverse topological order.

33:21.080 --> 33:25.600
So, this is how the analysis will run.

33:25.600 --> 33:30.320
And when you read statement C4, you see that SFR is being used.

33:30.360 --> 33:45.020
What I'm going to do is create a data structure with the value baz, SFR, C4, which is going to tell me that the last use of SFR is happening at C4 statement in baz method.

33:45.500 --> 33:46.320
Okay?

33:46.820 --> 33:54.800
And once I reach, I've already analyzed, I've completed the analysis for baz method, I'm going to analyze the bar method.

33:55.100 --> 33:58.980
So, the analysis happens in a reverse topological order.

33:59.040 --> 34:02.860
So, if baz is the last method that is called, it will be analyzed first.

34:03.200 --> 34:11.260
If bar is the second last method that is analyzed, it will be, you know, if bar is the second last method that is called, it will be analyzed second last.

34:11.420 --> 34:20.720
And if the first, sorry, so basically foo is calling bar, bar is calling baz, baz will be analyzed first, bar will be analyzed second, and then you'll analyze foo.

34:22.800 --> 34:26.300
So, here, we see that SFR is being used at B5.

34:26.300 --> 34:29.360
So, the LV entry will contain B5 SFR.

34:29.360 --> 34:37.580
And once we reach the call statement for baz, we're going to pull in the summary of baz method from here.

34:37.760 --> 34:46.120
And now we're going to see whether the LV entries, LV exit set of B3 already contains an SFR's use, and it does in our case.

34:46.180 --> 34:59.760
So, we're going to replace the value of this little red data structure with the new entry, which will be bar SFR B5, which would mean that SFR is last used at B5 statement inside bar method.

35:00.060 --> 35:05.320
So, this is how you'll keep on going and updating the red data structure.

35:05.340 --> 35:17.860
And once the analysis is complete over all these methods, you'll realize that the red data structure will complete the last usage point of the SFR variable across the entire program.

35:18.160 --> 35:34.120
So, similarly, if you go ahead and see that there's a bar method invocation here, you're going to pull in the summary for bar method, but the exit set of A3 contains 5, because SFR is not being used ahead in the program inside the foo method.

35:34.540 --> 35:44.580
So, this summary will not be over, this particular value will not be overwritten by anything, because it's just 5 that is being coming from the exit set of A3.

35:44.580 --> 35:53.140
We just completed our analysis, and the results tell us that SFR was last being used at B5 statement inside the bar method.

35:53.320 --> 36:03.700
So, this is how, you know, algorithmically, Androsia will run on your code and figure out what is the last usage point of any object in that application.

36:04.780 --> 36:10.280
Just to summarize, we went through this definition of live variable analysis.

36:10.540 --> 36:15.700
We computed summaries for individual methods using a two-step process.

36:15.700 --> 36:32.780
One of the steps is to compute the defuse set for every statement, and using the defuse sets, we computed the LV entry and exit sets, and using the LV entry and exit sets, we figured out the last usage point of every static field reference or local method,

36:32.780 --> 36:40.100
and put it in a summary, and eventually, we used the summary to compute the last usage point for a static field reference at a whole program level.

36:40.260 --> 36:40.960
Phew!

36:40.960 --> 36:43.460
So, that was too much.

36:43.460 --> 36:50.520
I mean, just intuitively, you can see what is the last use point, but to automate that, you require a lot of algorithm and math.

36:51.640 --> 36:56.280
So, yeah, I mean, things are not easy always.

36:56.640 --> 37:03.020
So, for instance field approach, the approach, you know, for instance fields is a little different.

37:03.020 --> 37:07.480
What we do is we mark all the classes which are string builder instance fields.

37:07.500 --> 37:09.480
We find their object instances.

37:09.760 --> 37:16.100
We track the last usage of object instances and their aliases instead of the string builder fields themselves.

37:16.120 --> 37:23.100
We will actually see a demo of this again, and I think that would explain this even better.

37:23.100 --> 37:27.140
So, just let's see, we have three classes here, which one of them is main activity.

37:27.140 --> 37:29.440
This is where my control is going to start.

37:29.480 --> 37:45.740
I have a secret defined here, which is a variable, which is going into a variable my secret, and then I'm instantiating the my class, which is here, and the variable that is referring to the object of my class is mc.

37:45.740 --> 37:52.120
Then we have a wrapper class whom I'm instantiating, and the reference variable for that is w.

37:52.300 --> 38:01.240
And then I have a call to call w method, which is defined inside the wrapper method.

38:01.400 --> 38:02.680
Wrapper class, sorry.

38:02.760 --> 38:14.880
And I'm passing the mc object instance to this call w method, which is invoking set a to pass on the secret value here, and this is just a setter method, which is setting the value of a.

38:14.880 --> 38:27.000
So, basically, the secret is here, then it goes here, and then it gets defined in this, and then it gets set in the setter method to the instance field a, which is a private instance field.

38:27.920 --> 38:37.400
So, in order to reset, if we had to reset this private instance field, we cannot just do it outside from within the on create method.

38:37.400 --> 38:50.280
We need to invoke a method within the class to reset the value of a, because it has been privately, the access modifier is private, so we can't, you know, access it outside its class.

38:52.100 --> 38:55.960
So, let's just quickly jump on to the demo.

38:58.940 --> 39:07.880
So, the steps that I had explained on the previous slides were we mark all classes which are string builder instance fields, which will be my class.

39:08.780 --> 39:12.740
We find the object instances, which is MC.

39:13.500 --> 39:19.280
Then we track the last uses of object instances, which is here in the call W statement.

39:19.880 --> 39:24.820
And then we instrument the code right after the call W statement.

39:25.180 --> 39:30.020
And here, you can see that I'm invoking a method, which is reset SBA.

39:30.040 --> 39:34.320
And this reset SBA is being defined in the class right here.

39:34.320 --> 39:41.200
That's because I cannot access the private instance field small a within the on create method.

39:42.240 --> 39:44.980
So, I have to invoke this reset method.

39:45.900 --> 39:46.620
All right.

39:46.620 --> 39:48.440
So, let's have a look at the demo.

39:49.880 --> 39:51.180
Pretty same code.

39:51.180 --> 39:52.780
Pretty much the same code.

39:52.780 --> 39:56.460
I have the call W statement here where I'm passing MC and my secret.

39:57.000 --> 40:02.440
And I also have the wrapper class here instantiated.

40:02.440 --> 40:04.400
And the reference variable is W.

40:04.560 --> 40:07.280
And I'm printing the W.SB field.

40:07.280 --> 40:12.000
So, SBField is another string builder instance field of wrapper class.

40:12.240 --> 40:15.160
And A and B, we can see that.

40:15.520 --> 40:20.400
So, A and B will be instance fields of my class.

40:20.400 --> 40:23.460
And SB will be an instance field in the wrapper class.

40:23.520 --> 40:25.800
We'll see that just in a while.

40:32.000 --> 40:35.720
So, you can see that we have an instance field SB here in the wrapper class.

40:36.080 --> 40:41.960
And I'm calling set A and passing on the secrets value to the set A method.

40:41.960 --> 40:45.660
And my class has A and B as its instance fields.

40:45.660 --> 40:49.880
And inside the set A method, I'm setting the value of the secret to A.

40:51.180 --> 40:53.380
B is nowhere used right now.

41:00.820 --> 41:06.780
So, inside the wrapper class, you see that the method call W also has a secret defined called password.

41:07.160 --> 41:10.980
And its last use is inside the call W method.

41:14.610 --> 41:16.710
So, B is used here.

41:19.130 --> 41:20.830
MC is last used here.

41:20.830 --> 41:22.630
And W is last used here.

41:22.950 --> 41:29.290
So, the instrumentation point for A and B should be right after the call W statement here.

41:29.450 --> 41:35.010
And the instrumentation point for the string build SB should be right after the WSB statement here.

41:35.590 --> 41:43.450
So, you see here that there's a call W method being invoked.

41:43.690 --> 41:50.230
And I have the reset methods being injected or instrumented right after the call W method.

41:50.230 --> 41:52.010
Because we were tracking MC.

41:52.010 --> 41:55.890
And MC's last use was in the call W method call.

41:55.890 --> 41:59.030
And after that, we have reset both the instance fields.

41:59.030 --> 42:09.630
And similarly, for the wrapper class, we had an instance field SB which is being reset right here in statement number 58.

42:18.490 --> 42:25.330
So, these reset methods are defined in the respective classes which have those instance fields.

42:25.950 --> 42:34.230
So, if we go to the respective classes, this is the wrapper class and the reset method has been defined here.

42:34.510 --> 42:36.190
What it's doing is the same thing.

42:36.190 --> 42:39.490
It's calling the length function on the string builder.

42:39.490 --> 42:43.310
And then it's calling the delete API to remove the content of the string builder.

42:46.860 --> 42:54.940
And similarly, we have reset methods for variable A and B, which were the instance field in my class.

42:57.500 --> 43:02.360
Here we have reset SBB, which is the reset method for variable B.

43:04.440 --> 43:08.060
So, you know, we've tackled all the three scopes now.

43:08.060 --> 43:10.480
We know how to deal with instance fields.

43:10.480 --> 43:13.540
We know how to deal with static fields.

43:13.540 --> 43:15.560
We also know how to deal with local variables.

43:16.060 --> 43:20.880
So, I mean, we did not need to see all of this because this is too much in depth.

43:20.880 --> 43:25.760
But if you want to go ahead and implement your own static code analysis, this is important stuff for you.

43:25.860 --> 43:29.780
So, I mean, I thought it appropriate to be, you know, displayed here.

43:30.820 --> 43:41.080
So, the work in progress is basically we're working on a test development so that we can, you know, get rid of the remaining bugs that we have.

43:41.080 --> 43:47.020
And we're also planning to include this into the CICD pipeline, which should be straightforward.

43:47.280 --> 43:54.040
And that way, a lot of big companies could, you know, just plug in their APKs and just push it across to us.

43:54.040 --> 44:03.760
And then we can just analyze those APKs and instrument the code that Androsia, you know, instruments to clear the memory contents of the APK.

44:03.820 --> 44:13.140
So, that way, it will be helpful for big corporates and especially the enterprise mobility management companies.

44:14.040 --> 44:20.840
So, if you want to use Androsia or you want to contribute or get in touch, this is the URL you should take a note of.

44:20.860 --> 44:23.460
And the tool and documentation will be available here .

44:23.700 --> 44:29.100
And if you want to contribute to the tool, here's my Twitter account or email.

44:29.100 --> 44:31.140
You can just shoot out an email to me.

44:32.840 --> 44:39.880
All right.

44:39.880 --> 44:42.120
So, these are some good references.

44:42.160 --> 44:48.100
The first three ones will get you started when it comes to creating your own data flow analysis.

44:48.320 --> 44:52.160
And the last two ones are a little bit more in-depth.

44:52.160 --> 44:57.460
If you want to, you know, see how things are actually working in the background.

44:57.460 --> 45:01.100
I'll anyways post these slides on the website of DEF CON.

45:01.100 --> 45:04.320
So, you can have a look at these references.

45:06.880 --> 45:07.920
Cool.

45:08.840 --> 45:11.640
So, that brings me to the end of my talk.

45:11.680 --> 45:13.520
Thank you for your time.

45:13.900 --> 45:16.540
I'm really happy to speak over here on this podium.

45:16.540 --> 45:17.820
I hope you enjoyed it.

45:17.820 --> 45:19.160
Thank you very much.

