forked from mateor/OpenPDroidPatches
-
Notifications
You must be signed in to change notification settings - Fork 24
/
openpdroid_4.4_libcore.patch
434 lines (426 loc) · 18.1 KB
/
openpdroid_4.4_libcore.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
diff --git a/luni/src/main/java/java/lang/PrivacyInputStream.java b/luni/src/main/java/java/lang/PrivacyInputStream.java
new file mode 100644
index 0000000..4986566
--- /dev/null
+++ b/luni/src/main/java/java/lang/PrivacyInputStream.java
@@ -0,0 +1,36 @@
+package java.lang;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Simulates an empty InputStream
+ * @author Svyatoslav Hresyk
+ * {@hide}
+ */
+public class PrivacyInputStream extends InputStream {
+
+ public PrivacyInputStream() {
+ }
+
+ @Override
+ public int read() throws IOException {
+ return -1;
+ }
+
+ @Override
+ public void close() throws IOException {
+ super.close();
+ }
+
+ @Override
+ public int read(byte[] b, int offset, int length) throws IOException {
+ return -1;
+ }
+
+ @Override
+ public int read(byte[] b) throws IOException {
+ return -1;
+ }
+
+}
diff --git a/luni/src/main/java/java/lang/PrivacyProcessManager.java b/luni/src/main/java/java/lang/PrivacyProcessManager.java
new file mode 100644
index 0000000..3cb0a02
--- /dev/null
+++ b/luni/src/main/java/java/lang/PrivacyProcessManager.java
@@ -0,0 +1,182 @@
+package java.lang;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+/**
+ * Provides privacy handling for {@link java.lang.ProcessManager}
+ * @author Svyatoslav Hresyk
+ * TODO: test if this works, also test it with root apps
+ * {@hide}
+ */
+public class PrivacyProcessManager {
+
+ private static final int GET_COMMAND_WAIT_MS = 10;
+
+ private static final int GET_COMMAND_WAIT_STEP = 5;
+
+ /**
+ * Verifies if the current process has privacy access permission
+ * to the specified setting
+ * @param setting name of the setting file (e.g., systemLogsSetting or
+ * externalStorageSetting)
+ * @return boolean true if permission is granted or false otherwise
+ */
+ public static boolean hasPrivacyPermission(String setting, int pid) {
+ String packageName = null;
+// String uid = null;
+ boolean output = true;
+ try {
+ packageName = getPackageName();
+// uid = getUid();
+ } catch (Exception e) {
+ System.err.println("PrivacyProcessManager: could not find package name or UID");
+ e.printStackTrace();
+ }
+ try {
+ PrivacyFileReader freader = null;
+
+ // get the command line of starting process
+ String commandLineFile = "/proc/" + pid + "/cmdline";
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: creating command line file FileReader for " + commandLineFile);
+ freader = new PrivacyFileReader(commandLineFile);
+ String proc = "";
+ for (int i = GET_COMMAND_WAIT_MS; (proc = freader.readLine()) == null && i >= 0; i-= GET_COMMAND_WAIT_STEP) {
+ try {
+ Thread.sleep(GET_COMMAND_WAIT_STEP);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: read \"" + proc + "\" from " + commandLineFile);
+ }
+ freader.close();
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: process: " + proc);
+
+ // check if logs are being read
+ if (proc != null && proc.trim().length() > 5 && proc.contains("logcat")) {
+ // get setting value
+ String settingsFilePath = "/data/system/privacy/" + packageName + /*"/" + uid +*/ "/" + setting;
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: creating settings file FileReader for " + settingsFilePath);
+ freader = new PrivacyFileReader(settingsFilePath);
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: reading first line from " + settingsFilePath);
+ String line = freader.readLine();
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: reading systemLogsSetting from the line");
+ int systemLogsSetting = line != null ? Integer.parseInt(line.trim()) : -1;
+ freader.close();
+ // check permission
+ if (systemLogsSetting == 1) output = false;
+ }
+ } catch (FileNotFoundException e) {
+ // no setting for this application; do nothing
+ } catch (Exception e) {
+ System.err.println("PrivacyProcessManager: could not read privacy settings: " + setting);
+ e.printStackTrace();
+ }
+// System.err.println("PrivacyProcessManager - hasPrivacyPermission: returning: " + output);
+
+ return output;
+ }
+
+
+
+ /**
+ * Verifies if the current process has privacy access permission
+ * to the specified setting. Use this method if you check before which commands will be executed. If you don't know
+ * use hasPrivacyPermission(String setting, int pid) it asked e.g. for 'logcat' command only at this time
+ * @param setting name of the setting file (e.g., ipTableSetting)
+ * @return boolean true if permission is granted or false otherwise
+ */
+ public static boolean hasPrivacyPermission(String setting) {
+ String packageName = null;
+ boolean output = true;
+ try {
+ packageName = getPackageName();
+ System.out.println("PrivacyProcessManager: got package name: " + packageName); //remove it after testing
+ } catch (Exception e) {
+ System.err.println("PrivacyProcessManager: could not find package name");
+ e.printStackTrace();
+ }
+ try {
+ PrivacyFileReader freader = null;
+ // get setting value
+ String settingsFilePath = "/data/system/privacy/" + packageName + "/" + setting;
+ freader = new PrivacyFileReader(settingsFilePath);
+ String line = freader.readLine();
+ int currentSetting = line != null ? Integer.parseInt(line.trim()) : -1;
+ freader.close();
+ // check permission
+ if (currentSetting == 1) output = false;
+
+ } catch (FileNotFoundException e) {
+ // no setting for this application; do nothing
+ } catch (Exception e) {
+ System.err.println("PrivacyProcessManager: could not read privacy settings: " + setting);
+ e.printStackTrace();
+ }
+ return output;
+ }
+
+ /**
+ * Finds the package name corresponding to the current process
+ * @return Current process' package name
+ * @throws IOException, FileNotFoundException
+ */
+ private static String getPackageName() throws IOException, FileNotFoundException {
+ PrivacyFileReader freader = new PrivacyFileReader("/proc/self/cmdline");
+ String packageName = freader.readLine().trim();
+ freader.close();
+ return packageName;
+ }
+
+ /**
+ * Finds the UID corresponding to the current process
+ * @return Current process' UID
+ * @throws IOException, FileNotFoundException, NumberFormatException, Exception
+ */
+// private static String getUid() throws IOException, FileNotFoundException,
+// NumberFormatException, Exception {
+// PrivacyFileReader freader;
+// try {
+// freader = new PrivacyFileReader("/proc/self/cgroup");
+// } catch (FileNotFoundException e) {
+// // this is most likely root (UID 0)
+// return "0";
+// }
+// String uid = null;
+// String line = "";
+// while (!line.contains("/uid/")) line = freader.readLine();
+// freader.close();
+// if (line != null) {
+// int index = line.indexOf("/uid/");
+// index += "/uid/".length();
+// // make sure the found UID is an int and convert it back to string
+// uid = Integer.parseInt(line.substring(index).trim()) + "";
+// }
+// if (uid != null) return uid;
+// else throw new Exception();
+// }
+
+ public static class PrivacyFileReader {
+
+ private FileInputStream inputStream;
+
+ private BufferedReader buffReader;
+
+ public PrivacyFileReader(String path) throws FileNotFoundException {
+ inputStream = new FileInputStream(new File(path));
+ buffReader = new BufferedReader(new InputStreamReader(inputStream));
+ }
+
+ public String readLine() throws IOException {
+ return buffReader.readLine();
+ }
+
+ public void close() throws IOException {
+ inputStream.close();
+ }
+ }
+}
diff --git a/luni/src/main/java/java/lang/ProcessManager.java b/luni/src/main/java/java/lang/ProcessManager.java
index 28314b7..400668b 100644
--- a/luni/src/main/java/java/lang/ProcessManager.java
+++ b/luni/src/main/java/java/lang/ProcessManager.java
@@ -16,18 +16,23 @@
package java.lang;
+import java.io.BufferedReader;
+import java.io.DataInputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
+
+
import libcore.io.ErrnoException;
import libcore.io.IoUtils;
import libcore.io.Libcore;
@@ -38,6 +43,51 @@ import static libcore.io.OsConstants.*;
* Manages child processes.
*/
final class ProcessManager {
+
+ //+++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ /**
+ * This method checks if some script files contains iptables command
+ * @param path path to script file
+ * @return true if script contains command, false otherwise
+ */
+ private boolean containsIpTableCommand(String path){
+ try{
+ System.out.println("now we're in containsIpTableCommand");
+ FileInputStream fstream = new FileInputStream(path);
+ // Get the object of DataInputStream
+ DataInputStream dis = new DataInputStream(fstream);
+ BufferedReader bR = new BufferedReader(new InputStreamReader(dis));
+ String line;
+ //Read File Line By Line
+ while ((line = bR.readLine()) != null) {
+ if(line.contains("iptables") || line.contains("ip6tables")){
+ try{
+ dis.close();
+ bR.close();
+ fstream.close();
+ } catch(IOException e){
+ System.out.println("got exception while closing streams");
+ // do nothing, we have to inform that iptables command exist
+ } finally{
+ fstream = null;
+ dis = null;
+ bR = null;
+ System.gc();
+ }
+ System.out.println("returning true, file contains iptable command");
+ return true;
+ }
+ }
+ System.out.println("returning false, file doesn't contains iptable command");
+ return false;
+ } catch(Exception e){
+ System.out.println("returning false,because we got exception while parsing");
+ return false;
+ }
+ }
+
+ //+++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
/**
* Map from pid to Process. We keep weak references to the Process objects
* and clean up the entries when no more external references are left. The
@@ -166,7 +216,35 @@ final class ProcessManager {
*/
public Process exec(String[] taintedCommand, String[] taintedEnvironment, File workingDirectory,
boolean redirectErrorStream) throws IOException {
- // Make sure we throw the same exceptions as the RI.
+ //------------------------------------------------------------------------------------------------------------------------------------------------------
+ //first check for script running
+ boolean isAllowed = true;
+ if(taintedCommand != null){
+ for(int i=0;i<taintedCommand.length;i++) System.out.println("tainted command part " +i+ ": " + taintedCommand[i]);
+ }
+ if(taintedCommand != null && taintedCommand.length > 0 && (taintedCommand[0].equals("su") || taintedCommand[0].equals("sh") || taintedCommand[0].equals("bash") || taintedCommand[0].equals("rbash"))){
+ //now we have to find the part of command which included the path of the script
+ for(int i=0;i<taintedCommand.length;i++){
+ System.out.println("Now test tainted command: " + taintedCommand[i]);
+ if(taintedCommand[i].contains(".sh") || taintedCommand[i].contains("/")){
+ if(containsIpTableCommand(taintedCommand[i]) && !PrivacyProcessManager.hasPrivacyPermission("ipTableProtectSetting")){
+ isAllowed = false;
+ break;
+ }
+ }
+ }
+ }
+ if(taintedCommand != null && taintedCommand.length > 0 && isAllowed){
+ for(int i=0;i<taintedCommand.length;i++){
+ if(taintedCommand[i].contains("iptables") || taintedCommand[i].contains("ip6tables")){
+ if(PrivacyProcessManager.hasPrivacyPermission("ipTableProtectSetting")) break;
+ else isAllowed = false;
+ }
+ }
+ }
+ if(!isAllowed) taintedCommand = new String[] {"su"};
+ //------------------------------------------------------------------------------------------------------------------------------------------------------
+ // Make sure we throw the same exceptions as the RI.
if (taintedCommand == null) {
throw new NullPointerException("taintedCommand == null");
}
@@ -215,7 +293,14 @@ final class ProcessManager {
wrapper.initCause(e);
throw wrapper;
}
- ProcessImpl process = new ProcessImpl(pid, in, out, err);
+ //-------------------------------------------------------------------------------------------------------------------------------------------------------------
+ //TODO we have to control if it is better to throw exception or leave inputstream empty. Test it!
+ ProcessImpl process;
+ if(isAllowed)
+ process = new ProcessImpl(pid, in, out, err);
+ else
+ process = new ProcessImpl(pid, in, out, err, false);
+ //-------------------------------------------------------------------------------------------------------------------------------------------------------------
ProcessReference processReference = new ProcessReference(process, referenceQueue);
processReferences.put(pid, processReference);
@@ -239,7 +324,13 @@ final class ProcessManager {
/** Sends output to process. */
private final OutputStream outputStream;
+
+ //--------------------------------------------------------------------------------------
+ /**Indicates if the process should be a fake or not. */
+ private boolean fakeProcess = false;
+ //--------------------------------------------------------------------------------------
+
/** The process's exit value. */
private Integer exitValue = null;
private final Object exitValueMutex = new Object();
@@ -248,14 +339,42 @@ final class ProcessManager {
this.pid = pid;
this.errorStream = new ProcessInputStream(err);
- this.inputStream = new ProcessInputStream(in);
+ // BEGIN privacy-modified
+ if (PrivacyProcessManager.hasPrivacyPermission("systemLogsSetting", pid)) {
+ this.inputStream = new ProcessInputStream(in);
+ } else {
+ this.inputStream = new PrivacyInputStream();
+ }
+ // END privacy-modified
this.outputStream = new ProcessOutputStream(out);
}
+
+ //--------------------------------------------------------------------------------------
+ /**
+ * Use this constructor if you checked before that process is not allowed to send this command.
+ * @param isAllowed true if process is allowed or false if process is not allowed to execute the following commands
+ * @author CollegeDev
+ */
+ ProcessImpl(int pid, FileDescriptor in, FileDescriptor out, FileDescriptor err, boolean isAllowed) {
+ this.pid = pid;
+ this.errorStream = new ProcessInputStream(err);
+ // BEGIN privacy-modified
+ if (isAllowed)
+ this.inputStream = new ProcessInputStream(in);
+ else{
+ this.inputStream = new PrivacyInputStream();
+ fakeProcess = true;
+ }
+ // END privacy-modified
+ this.outputStream = new ProcessOutputStream(out);
+ }
+ //--------------------------------------------------------------------------------------
+
public void destroy() {
// If the process hasn't already exited, send it SIGKILL.
synchronized (exitValueMutex) {
- if (exitValue == null) {
+ if (exitValue == null || fakeProcess) {
try {
Libcore.os.kill(pid, SIGKILL);
} catch (ErrnoException e) {
@@ -270,6 +389,11 @@ final class ProcessManager {
}
public int exitValue() {
+ //--------------------------------------------------------------------------------------
+ if(fakeProcess){
+ setExitValue(0);
+ }
+ //--------------------------------------------------------------------------------------
synchronized (exitValueMutex) {
if (exitValue == null) {
throw new IllegalThreadStateException("Process has not yet terminated: " + pid);