Project

General

Profile

RestoreApplicationInternalData » History » Version 13

Denis 'GNUtoo' Carikli, 10/30/2020 01:53 PM
add explanation about encryption

1 1 Denis 'GNUtoo' Carikli
h1. RestoreApplicationInternalData
2
3
{{toc}}
4
5
h2. /!\ Warning: Draft
6
7
This article is in draft form and is being written:
8
* Everybody is welcome to contribute
9
* Some things might not be accurate yet, so beware before using the information contained in it.
10 2 Denis 'GNUtoo' Carikli
11
h2. Introduction
12
13
In some case, it is useful to be able to restore internal applications data:
14 9 Denis 'GNUtoo' Carikli
15
For instance you might need to move the data of an application from a device to another if you want to switch device.
16
17
Another use case is if /data/system/packages.xml or /data/system/appops.xml get corrupted, applications can loose access to their data. This can make the launcher and other applications crash. 
18
19
So while it is possible to recover from that by wiping the data partition in the recovery, sometimes it's very impractical to do that because you might have important data like silence encryption keys and established sessions that you don't want to loose.
20 3 Denis 'GNUtoo' Carikli
21
h2. Howto
22
23 13 Denis 'GNUtoo' Carikli
Silence has been chosen as an example for this tutorial because:
24
* It's an application commonly used
25
* Loosing its data (key, sessions) can be painful
26
27 10 Denis 'GNUtoo' Carikli
This howto will explain how to move silence data from a device to another.
28 1 Denis 'GNUtoo' Carikli
29 10 Denis 'GNUtoo' Carikli
For instance you could want to move from a Galaxy SIII (GT-I9300) to a Galaxy SII (GT-I9100) or vice versa, and you might not want to recreate keys, sessions, etc when moving device.
30
31 13 Denis 'GNUtoo' Carikli
This howto assumes that the data partition is unencrypted. If you know how to open encrypted data partition in the recovery, or in a GNU/Linux distribution, it would be great to either modify this tutorial to add information on how to do it, or contact us on the mailing list or through the bugreporting system about it.
32 10 Denis 'GNUtoo' Carikli
33
Silence stores its data in the internal application storage. As far as I know it's not supposed to store any data on the microSD or user storage beside potential backups.
34 5 Denis 'GNUtoo' Carikli
35 11 Denis 'GNUtoo' Carikli
It might be interesting to make additional tutorial for other cases. For instance for:
36
* Applications that also require data to be on the microSD or user storage.
37
* System applications that have their data in a database.
38 3 Denis 'GNUtoo' Carikli
39 5 Denis 'GNUtoo' Carikli
TODO:
40
* Explain why we need the uid/gid
41
* Explain why the ls -ld gives the application uid/gid
42
* Point to how to get root
43
* Explain how to handle a corrupted /data/system/packages.xml and /data/system/appops.xml
44 3 Denis 'GNUtoo' Carikli
* Explain how to mount a full backup, and why not to restore full backup completely
45
* Explain how and why create a tarball of the application data
46
47 5 Denis 'GNUtoo' Carikli
Once we have a tarball backup of the application data we need to reboot in the recovery to avoid any writes to the data filesystem.
48
49
Once this is done you need to mount the data partition. 
50
51
For the Galaxy SIII (GT-I9300), this can be done from your computer with this command:
52 3 Denis 'GNUtoo' Carikli
<pre>
53
$ adb shell "mount /dev/block/platform/dw_mmc/by-name/USERDATA /data"
54
</pre>
55
56 5 Denis 'GNUtoo' Carikli
Then we need to get a root shell inside the recovery. This can be done with the @adb shell@ command:
57 3 Denis 'GNUtoo' Carikli
<pre>
58
$ adb shell
59
</pre>
60
61 5 Denis 'GNUtoo' Carikli
Then we assume that you are in /data/data to simplify this tutorial.
62
You will need to remember adjust all other commands if you are not in this directory.
63
To go in /data/data, you can use the following command:
64 3 Denis 'GNUtoo' Carikli
<pre>
65
root@m0:/ # cd /data/data/                                                     
66
root@m0:/data/data # 
67
</pre>
68
69 5 Denis 'GNUtoo' Carikli
As applications are sandboxed, and that as part of that sandboxing, they have their own usersname, we need to retrieve this username.
70
To do that we can just use @ls -ld@ on the directory holding the application internal data.
71
72
The directory has the internal name of the application.
73
74
Here are some well known name correspondances:
75
76 7 Denis 'GNUtoo' Carikli
| Internal name           | Application                                 |
77
| org.smssecure.smssecure | Silence                                     |
78
| com.android.dialer      | Dialer (Android's stock dialer application) |
79 8 Denis 'GNUtoo' Carikli
| fil.libre.repwifiapp    | RepWiFi                                     |
80 5 Denis 'GNUtoo' Carikli
81
For pakcages comming from f-droid, the f-droid website can find the correspondance.
82
83
For instance the "Silence page":https://f-droid.org/en/packages/org.smssecure.smssecure/ has @org.smssecure.smssecure@ in its URL and inside the page.
84
85
So with @ls -ld@ we can find the application username in this way:
86 1 Denis 'GNUtoo' Carikli
<pre>
87 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # ls -ld org.smssecure.smssecure
88 3 Denis 'GNUtoo' Carikli
__bionic_open_tzdata: couldn't find any tzdata when looking for localtime!
89
__bionic_open_tzdata: couldn't find any tzdata when looking for GMT!
90
__bionic_open_tzdata: couldn't find any tzdata when looking for posixrules!
91
drwxr-x--x 2 u0_a61 u0_a61 4096 2012-01-01 00:01 org.smssecure.smssecure
92
</pre>
93
94
Here the users and groups are @u0_a61@.
95 1 Denis 'GNUtoo' Carikli
96 5 Denis 'GNUtoo' Carikli
We will then need this information later on to restore the silence data from the other device: If we restore that data as-is it will most likely have wrong permissions: when the the silence application was installed on the older device, it was assigned an username. As this username depends on the number of applications that were installed before it, we cannot expect it to always be the same between the two devices.
97
98
It's also best to move or delete the data we don't want:
99 3 Denis 'GNUtoo' Carikli
<pre>
100
root@m0:/data/data # mv org.smssecure.smssecure org.smssecure.smssecure.delme
101
</pre>
102 1 Denis 'GNUtoo' Carikli
103 5 Denis 'GNUtoo' Carikli
Moving it has several advantages:
104
* We can still verify the username later on to see if it matches with the backup we restored.
105
* We can interrupt this tutorial more easily if something goes wrong.
106
107
Here we need to verify that the archive will extract its files in the @org.smssecure.smssecure@ directory and not in the current directory which is @/data/data@:
108 3 Denis 'GNUtoo' Carikli
<pre>
109
root@m0:/data/data # tar tf /org.smssecure.smssecure.tar
110
./org.smssecure.smssecure/
111
./org.smssecure.smssecure/lib -> /data/app/org.smssecure.smssecure-1/lib/arm
112
[...]
113 1 Denis 'GNUtoo' Carikli
</pre>
114 5 Denis 'GNUtoo' Carikli
Here we see that everything starts with @./org.smssecure.smssecure/@ (or @org.smssecure.smssecure/@) so it's good.
115 1 Denis 'GNUtoo' Carikli
116 5 Denis 'GNUtoo' Carikli
TODO: move this part earlier
117
118
If we had something like that instead:
119 1 Denis 'GNUtoo' Carikli
<pre>
120 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar tf /org.smssecure.smssecure.tar
121
./lib -> /data/app/org.smssecure.smssecure-1/lib/arm
122
[...]
123
</pre>
124
125
Then it's best to recreate the archive.
126
127
If you need more time you could also move back org.smssecure.smsecure.delme to org.smssecure.smssecure if needed.
128
129
We can then proceed to extract the application data (with the username from the old device):
130
<pre>
131 3 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar xpf /org.smssecure.smssecure.tar --numeric-owner
132
</pre>
133 1 Denis 'GNUtoo' Carikli
134 5 Denis 'GNUtoo' Carikli
Here we can see that the username differs from the one we need:
135 3 Denis 'GNUtoo' Carikli
<pre>
136
root@m0:/data/data # ls -ld org.smssecure.smssecure 
137
__bionic_open_tzdata: couldn't find any tzdata when looking for localtime!
138
__bionic_open_tzdata: couldn't find any tzdata when looking for GMT!
139
__bionic_open_tzdata: couldn't find any tzdata when looking for posixrules!
140
drwxr-x--x 9 u0_a63 u0_a63 4096 2012-01-01 00:21 org.smssecure.smssecure
141 1 Denis 'GNUtoo' Carikli
</pre>
142 5 Denis 'GNUtoo' Carikli
We have @u0_a63@ instead of @u0_a61@.
143 1 Denis 'GNUtoo' Carikli
144 5 Denis 'GNUtoo' Carikli
So we need to fix it. This can be done with the @chown@ command, like that:
145 3 Denis 'GNUtoo' Carikli
<pre>
146
root@m0:/data/data # chown u0_a61:u0_a61 -R org.smssecure.smssecure            
147
root@m0:/data/data # 
148
</pre>
149 1 Denis 'GNUtoo' Carikli
150 5 Denis 'GNUtoo' Carikli
At this point, we don't need the @org.smssecure.smssecure.delme@ directory anymore, and it's best to remove it not to create any issues later on.
151
This can be done with the following command:
152 3 Denis 'GNUtoo' Carikli
<pre>
153
root@m0:/data/data # rm -rf org.smssecure.smssecure.delme
154
root@m0:/data/data # 
155
</pre>
156 1 Denis 'GNUtoo' Carikli
157 5 Denis 'GNUtoo' Carikli
In addition to the standard unix permissions, Android also uses selinux, so we also need to fixup the selinux permissions.
158
159
The restorecon command can be used for that. Here's its help: 
160 3 Denis 'GNUtoo' Carikli
<pre>
161
root@m0:/data/data # restorecon                                                
162
usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...
163
164
Restores the default security contexts for the given files.
165
166
-D	apply to /data/data too
167
-F	force reset
168
-R	recurse into directories
169
-n	don't make any changes; useful with -v to see what would change
170
-v	verbose: show any changes
171
172
restorecon: Needs 1 argument
173
</pre>
174 1 Denis 'GNUtoo' Carikli
175 5 Denis 'GNUtoo' Carikli
So to use it to fixup the selinux permissions, we can use the following command:
176 1 Denis 'GNUtoo' Carikli
<pre>
177 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # restorecon -D -F -R -v /data/
178
</pre>
179
180 6 Denis 'GNUtoo' Carikli
The order of the arguments (-D, -F, etc) seem to be important here as the wrong order might result in nothing being done.
181
Without the @-v@ argument and with the wrong order of argument, it might make you think that it did its job while it did nothing.
182
183 5 Denis 'GNUtoo' Carikli
It will then print something that looks like that:
184
<pre>
185 3 Denis 'GNUtoo' Carikli
SELinux: Loaded file_contexts contexts from /file_contexts.
186
[...]
187
SELinux:  Relabeling /data/data/org.smssecure.smssecure from u:object_r:system_data_file:s0 to u:object_r:app_data_file:s0:c512,c768.
188
SELinux:  Relabeling /data/data/org.smssecure.smssecure/lib from u:object_r:system_data_file:s0 to u:object_r:app_data_file:s0:c512,c768.
189
[...]
190 1 Denis 'GNUtoo' Carikli
</pre>
191
192 5 Denis 'GNUtoo' Carikli
The premissions fixing is now done.
193
194
So we can then umount the data partition and reboot.
195
196
To do that first we need to go outside of data, else the mount will fail:
197 1 Denis 'GNUtoo' Carikli
<pre>
198 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # cd /
199
root@m0:/ # 
200
</pre>
201
202
Then we can simply unmount it with this command:
203
<pre>
204 3 Denis 'GNUtoo' Carikli
root@m0:/ # umount  /data/                                                     
205 1 Denis 'GNUtoo' Carikli
root@m0:/ # 
206
</pre>
207 3 Denis 'GNUtoo' Carikli
208 5 Denis 'GNUtoo' Carikli
Then it's a good practice to make sure that everything is written to the data partition before rebooting.
209
We can do that with the @sync@ command:
210 1 Denis 'GNUtoo' Carikli
<pre>
211
root@m0:/ # sync
212 5 Denis 'GNUtoo' Carikli
</pre>
213
214
And we can finally reboot:
215
<pre>
216 1 Denis 'GNUtoo' Carikli
root@m0:/ # reboot
217
</pre>
218
219 5 Denis 'GNUtoo' Carikli
After rebooting, silence still refused to start.
220
221
So I looked at the logs from my laptop with this command:
222 1 Denis 'GNUtoo' Carikli
<pre>
223 5 Denis 'GNUtoo' Carikli
$ adb logcat -b main
224
</pre>
225
226
I waited until no more new logs were printed.
227
228
I then press enter multiple times to create a separation with many new lines to be able to get back to the begining of the new logs easily.
229
Then I launched silence and started pressing enter multiple times again to mark the end of the silence related logs.
230
231
I then had that in these new logs:
232
<pre>
233 4 Denis 'GNUtoo' Carikli
01-01 01:27:48.260  4126  4126 D AndroidRuntime: Shutting down VM
234
01-01 01:27:48.265  4126  4126 E AndroidRuntime: FATAL EXCEPTION: main
235
01-01 01:27:48.265  4126  4126 E AndroidRuntime: Process: org.smssecure.smssecure, PID: 4126
236
01-01 01:27:48.265  4126  4126 E AndroidRuntime: Theme: themes:{}
237
01-01 01:27:48.265  4126  4126 E AndroidRuntime: java.lang.RuntimeException: Unable to create application org.smssecure.smssecure.ApplicationContext: java.lang.SecurityException: getActiveSubscriptionInfoList: Neither user 10061 nor current process has android.permission.READ_PHONE_STATE.
238 1 Denis 'GNUtoo' Carikli
01-01 01:27:48.265  4126  4126 E AndroidRuntime: 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4754)
239
[...]
240
</pre>
241
242 5 Denis 'GNUtoo' Carikli
So to fix it I went in @Settings->Apps->Silence->Permissions@ and gave it all the permissions it needed.
243
244
I had this issue because I didn't even launch silence after installing it, so it cound't ask me for the permissions it needed.
245
And the silence of the former device probably wrote in its data that it already asked the permissions.