Project

General

Profile

RestoreApplicationInternalData » History » Version 8

Denis 'GNUtoo' Carikli, 10/29/2020 12:17 AM
Add repwifi example

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