Project

General

Profile

RestoreApplicationInternalData » History » Version 18

Denis 'GNUtoo' Carikli, 10/30/2020 02:12 PM

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 15 Denis 'GNUtoo' Carikli
h2. Rationale
12 2 Denis 'GNUtoo' Carikli
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 16 Denis 'GNUtoo' Carikli
h2. Goals
22 1 Denis 'GNUtoo' Carikli
23 13 Denis 'GNUtoo' Carikli
This howto will explain how to move silence data from a device to another.
24 1 Denis 'GNUtoo' Carikli
25
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.
26
27 16 Denis 'GNUtoo' Carikli
This could also be adapted to restore application data from a block device level backup of the internal eMMC or the internal data partition.
28
29 14 Denis 'GNUtoo' Carikli
h2. Silence
30 1 Denis 'GNUtoo' Carikli
31 14 Denis 'GNUtoo' Carikli
Silence has been chosen as an example for this tutorial because:
32
* It's an application commonly used
33
* Loosing its data (key, sessions) can be painful
34
35 1 Denis 'GNUtoo' Carikli
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.
36
37
It might be interesting to make additional tutorial for other cases. For instance for:
38
* Applications that also require data to be on the microSD or user storage.
39
* System applications that have their data in a database.
40 10 Denis 'GNUtoo' Carikli
41 14 Denis 'GNUtoo' Carikli
h2. Requirements 
42
43
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.
44
45
h2. TODO
46
47 5 Denis 'GNUtoo' Carikli
* Explain why we need the uid/gid
48 1 Denis 'GNUtoo' Carikli
* Explain why the ls -ld gives the application uid/gid
49
* Point to how to get root
50
* Explain how to handle a corrupted /data/system/packages.xml and /data/system/appops.xml
51
* Explain how to mount a full backup, and why not to restore full backup completely
52
* Explain how and why create a tarball of the application data
53
54
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.
55 14 Denis 'GNUtoo' Carikli
56 1 Denis 'GNUtoo' Carikli
h2. Backuping Silence's data from the old device
57
58 18 Denis 'GNUtoo' Carikli
For the Galaxy SIII (GT-I9300), this can be done from your computer with this command:
59 16 Denis 'GNUtoo' Carikli
<pre>
60
$ adb shell "mount /dev/block/platform/dw_mmc/by-name/USERDATA /data"
61
</pre>
62 1 Denis 'GNUtoo' Carikli
63 16 Denis 'GNUtoo' Carikli
<pre>
64
$ adb shell
65
</pre>
66
67
<pre>
68 17 Denis 'GNUtoo' Carikli
root@m0:/ # cd /data/data
69 16 Denis 'GNUtoo' Carikli
</pre>
70
71 1 Denis 'GNUtoo' Carikli
<pre>
72 17 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar cvpf /org.smssecure.smssecure.tar org.smssecure.smssecure/
73
</pre>
74
75
<pre>
76 16 Denis 'GNUtoo' Carikli
root@m0:/ # exit
77
</pre>
78
79
<pre>
80
$ adb pull /org.smssecure.smssecure.tar ./
81
</pre>
82
83
<pre>
84
$ tar tvf org.smssecure.smssecure.tar
85 1 Denis 'GNUtoo' Carikli
</pre>
86 17 Denis 'GNUtoo' Carikli
87
<pre>
88
drwxr-x--x u0_a61/u0_a61     0 2020-10-28 18:58 org.smssecure.smssecure/
89
lrwxrwxrwx root/root         0 2020-10-28 18:58 org.smssecure.smssecure/lib -> /data/app/org.smssecure.smssecure-1/lib/arm
90
[...]
91
</pre>
92 16 Denis 'GNUtoo' Carikli
93 14 Denis 'GNUtoo' Carikli
h2. Restoring Silence's data to the new device
94
95
Once the backup is done you need to mount the data partition. 
96 5 Denis 'GNUtoo' Carikli
97
For the Galaxy SIII (GT-I9300), this can be done from your computer with this command:
98 3 Denis 'GNUtoo' Carikli
<pre>
99
$ adb shell "mount /dev/block/platform/dw_mmc/by-name/USERDATA /data"
100
</pre>
101
102 5 Denis 'GNUtoo' Carikli
Then we need to get a root shell inside the recovery. This can be done with the @adb shell@ command:
103 3 Denis 'GNUtoo' Carikli
<pre>
104
$ adb shell
105
</pre>
106
107 5 Denis 'GNUtoo' Carikli
Then we assume that you are in /data/data to simplify this tutorial.
108
You will need to remember adjust all other commands if you are not in this directory.
109
To go in /data/data, you can use the following command:
110 3 Denis 'GNUtoo' Carikli
<pre>
111
root@m0:/ # cd /data/data/                                                     
112
root@m0:/data/data # 
113
</pre>
114
115 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.
116
To do that we can just use @ls -ld@ on the directory holding the application internal data.
117
118
The directory has the internal name of the application.
119
120
Here are some well known name correspondances:
121
122 7 Denis 'GNUtoo' Carikli
| Internal name           | Application                                 |
123
| org.smssecure.smssecure | Silence                                     |
124
| com.android.dialer      | Dialer (Android's stock dialer application) |
125 8 Denis 'GNUtoo' Carikli
| fil.libre.repwifiapp    | RepWiFi                                     |
126 5 Denis 'GNUtoo' Carikli
127
For pakcages comming from f-droid, the f-droid website can find the correspondance.
128
129
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.
130
131
So with @ls -ld@ we can find the application username in this way:
132 1 Denis 'GNUtoo' Carikli
<pre>
133 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # ls -ld org.smssecure.smssecure
134 3 Denis 'GNUtoo' Carikli
__bionic_open_tzdata: couldn't find any tzdata when looking for localtime!
135
__bionic_open_tzdata: couldn't find any tzdata when looking for GMT!
136
__bionic_open_tzdata: couldn't find any tzdata when looking for posixrules!
137
drwxr-x--x 2 u0_a61 u0_a61 4096 2012-01-01 00:01 org.smssecure.smssecure
138
</pre>
139
140
Here the users and groups are @u0_a61@.
141 1 Denis 'GNUtoo' Carikli
142 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.
143
144
It's also best to move or delete the data we don't want:
145 3 Denis 'GNUtoo' Carikli
<pre>
146
root@m0:/data/data # mv org.smssecure.smssecure org.smssecure.smssecure.delme
147
</pre>
148 1 Denis 'GNUtoo' Carikli
149 5 Denis 'GNUtoo' Carikli
Moving it has several advantages:
150
* We can still verify the username later on to see if it matches with the backup we restored.
151
* We can interrupt this tutorial more easily if something goes wrong.
152
153
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@:
154 3 Denis 'GNUtoo' Carikli
<pre>
155
root@m0:/data/data # tar tf /org.smssecure.smssecure.tar
156
./org.smssecure.smssecure/
157
./org.smssecure.smssecure/lib -> /data/app/org.smssecure.smssecure-1/lib/arm
158
[...]
159 1 Denis 'GNUtoo' Carikli
</pre>
160 5 Denis 'GNUtoo' Carikli
Here we see that everything starts with @./org.smssecure.smssecure/@ (or @org.smssecure.smssecure/@) so it's good.
161 1 Denis 'GNUtoo' Carikli
162 5 Denis 'GNUtoo' Carikli
TODO: move this part earlier
163
164
If we had something like that instead:
165 1 Denis 'GNUtoo' Carikli
<pre>
166 5 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar tf /org.smssecure.smssecure.tar
167
./lib -> /data/app/org.smssecure.smssecure-1/lib/arm
168
[...]
169
</pre>
170
171
Then it's best to recreate the archive.
172
173
If you need more time you could also move back org.smssecure.smsecure.delme to org.smssecure.smssecure if needed.
174
175
We can then proceed to extract the application data (with the username from the old device):
176
<pre>
177 3 Denis 'GNUtoo' Carikli
root@m0:/data/data # tar xpf /org.smssecure.smssecure.tar --numeric-owner
178
</pre>
179 1 Denis 'GNUtoo' Carikli
180 5 Denis 'GNUtoo' Carikli
Here we can see that the username differs from the one we need:
181 3 Denis 'GNUtoo' Carikli
<pre>
182
root@m0:/data/data # ls -ld org.smssecure.smssecure 
183
__bionic_open_tzdata: couldn't find any tzdata when looking for localtime!
184
__bionic_open_tzdata: couldn't find any tzdata when looking for GMT!
185
__bionic_open_tzdata: couldn't find any tzdata when looking for posixrules!
186
drwxr-x--x 9 u0_a63 u0_a63 4096 2012-01-01 00:21 org.smssecure.smssecure
187 1 Denis 'GNUtoo' Carikli
</pre>
188 5 Denis 'GNUtoo' Carikli
We have @u0_a63@ instead of @u0_a61@.
189 1 Denis 'GNUtoo' Carikli
190 5 Denis 'GNUtoo' Carikli
So we need to fix it. This can be done with the @chown@ command, like that:
191 3 Denis 'GNUtoo' Carikli
<pre>
192
root@m0:/data/data # chown u0_a61:u0_a61 -R org.smssecure.smssecure            
193
root@m0:/data/data # 
194
</pre>
195 1 Denis 'GNUtoo' Carikli
196 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.
197
This can be done with the following command:
198 3 Denis 'GNUtoo' Carikli
<pre>
199
root@m0:/data/data # rm -rf org.smssecure.smssecure.delme
200
root@m0:/data/data # 
201
</pre>
202 1 Denis 'GNUtoo' Carikli
203 5 Denis 'GNUtoo' Carikli
In addition to the standard unix permissions, Android also uses selinux, so we also need to fixup the selinux permissions.
204
205
The restorecon command can be used for that. Here's its help: 
206 3 Denis 'GNUtoo' Carikli
<pre>
207
root@m0:/data/data # restorecon                                                
208
usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...
209
210
Restores the default security contexts for the given files.
211
212
-D	apply to /data/data too
213
-F	force reset
214
-R	recurse into directories
215
-n	don't make any changes; useful with -v to see what would change
216
-v	verbose: show any changes
217
218
restorecon: Needs 1 argument
219 1 Denis 'GNUtoo' Carikli
</pre>
220 5 Denis 'GNUtoo' Carikli
221 1 Denis 'GNUtoo' Carikli
So to use it to fixup the selinux permissions, we can use the following command:
222 5 Denis 'GNUtoo' Carikli
<pre>
223
root@m0:/data/data # restorecon -D -F -R -v /data/
224
</pre>
225 6 Denis 'GNUtoo' Carikli
226
The order of the arguments (-D, -F, etc) seem to be important here as the wrong order might result in nothing being done.
227
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.
228 5 Denis 'GNUtoo' Carikli
229
It will then print something that looks like that:
230 3 Denis 'GNUtoo' Carikli
<pre>
231
SELinux: Loaded file_contexts contexts from /file_contexts.
232
[...]
233
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.
234
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.
235 1 Denis 'GNUtoo' Carikli
[...]
236
</pre>
237 5 Denis 'GNUtoo' Carikli
238
The premissions fixing is now done.
239
240
So we can then umount the data partition and reboot.
241 1 Denis 'GNUtoo' Carikli
242 5 Denis 'GNUtoo' Carikli
To do that first we need to go outside of data, else the mount will fail:
243 1 Denis 'GNUtoo' Carikli
<pre>
244 16 Denis 'GNUtoo' Carikli
root@m0:/data/data # cd /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.
245 5 Denis 'GNUtoo' Carikli
root@m0:/ # 
246
</pre>
247
248
Then we can simply unmount it with this command:
249
<pre>
250 3 Denis 'GNUtoo' Carikli
root@m0:/ # umount  /data/                                                     
251 1 Denis 'GNUtoo' Carikli
root@m0:/ # 
252 3 Denis 'GNUtoo' Carikli
</pre>
253 5 Denis 'GNUtoo' Carikli
254
Then it's a good practice to make sure that everything is written to the data partition before rebooting.
255 1 Denis 'GNUtoo' Carikli
We can do that with the @sync@ command:
256
<pre>
257 5 Denis 'GNUtoo' Carikli
root@m0:/ # sync
258
</pre>
259
260
And we can finally reboot:
261 1 Denis 'GNUtoo' Carikli
<pre>
262
root@m0:/ # reboot
263
</pre>
264 5 Denis 'GNUtoo' Carikli
265
After rebooting, silence still refused to start.
266
267 1 Denis 'GNUtoo' Carikli
So I looked at the logs from my laptop with this command:
268 5 Denis 'GNUtoo' Carikli
<pre>
269
$ adb logcat -b main
270
</pre>
271
272
I waited until no more new logs were printed.
273
274 1 Denis 'GNUtoo' Carikli
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.
275 5 Denis 'GNUtoo' Carikli
Then I launched silence and started pressing enter multiple times again to mark the end of the silence related logs.
276
277
I then had that in these new logs:
278 16 Denis 'GNUtoo' Carikli
<pre>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.
279 4 Denis 'GNUtoo' Carikli
01-01 01:27:48.260  4126  4126 D AndroidRuntime: Shutting down VM
280
01-01 01:27:48.265  4126  4126 E AndroidRuntime: FATAL EXCEPTION: main
281
01-01 01:27:48.265  4126  4126 E AndroidRuntime: Process: org.smssecure.smssecure, PID: 4126
282
01-01 01:27:48.265  4126  4126 E AndroidRuntime: Theme: themes:{}
283
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.
284 1 Denis 'GNUtoo' Carikli
01-01 01:27:48.265  4126  4126 E AndroidRuntime: 	at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4754)
285
[...]
286
</pre>
287
288 5 Denis 'GNUtoo' Carikli
So to fix it I went in @Settings->Apps->Silence->Permissions@ and gave it all the permissions it needed.
289
290
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.
291
And the silence of the former device probably wrote in its data that it already asked the permissions.