-
Notifications
You must be signed in to change notification settings - Fork 2
/
libcore.diff
204 lines (202 loc) · 8.36 KB
/
libcore.diff
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
diff -Npru --exclude='.git*' cyanogen/libcore/luni/src/main/java/java/lang/PrivacyInputStream.java pang/libcore/luni/src/main/java/java/lang/PrivacyInputStream.java
--- cyanogen/libcore/luni/src/main/java/java/lang/PrivacyInputStream.java 1969-12-31 16:00:00.000000000 -0800
+++ pang/libcore/luni/src/main/java/java/lang/PrivacyInputStream.java 2012-07-13 10:18:29.364673191 -0700
@@ -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 -Npru --exclude='.git*' cyanogen/libcore/luni/src/main/java/java/lang/PrivacyProcessManager.java pang/libcore/luni/src/main/java/java/lang/PrivacyProcessManager.java
--- cyanogen/libcore/luni/src/main/java/java/lang/PrivacyProcessManager.java 1969-12-31 16:00:00.000000000 -0800
+++ pang/libcore/luni/src/main/java/java/lang/PrivacyProcessManager.java 2012-07-13 10:18:29.365673461 -0700
@@ -0,0 +1,143 @@
+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;
+ }
+
+ /**
+ * 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 -Npru --exclude='.git*' cyanogen/libcore/luni/src/main/java/java/lang/ProcessManager.java pang/libcore/luni/src/main/java/java/lang/ProcessManager.java
--- cyanogen/libcore/luni/src/main/java/java/lang/ProcessManager.java 2012-07-13 09:01:53.925255478 -0700
+++ pang/libcore/luni/src/main/java/java/lang/ProcessManager.java 2012-07-13 10:18:29.372675319 -0700
@@ -248,7 +248,12 @@ 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);
}