-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorize.diff
497 lines (477 loc) · 21.6 KB
/
authorize.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
diff -rupN '--exclude=.git' '--exclude=.svn' '--exclude=*.orig' '--exclude=*.rej' 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/IPrivacySettingsManager.aidl nexus/frameworks/base/privacy/java/android/privacy/IPrivacySettingsManager.aidl
--- 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/IPrivacySettingsManager.aidl 2012-12-05 00:44:01.000000000 +1100
+++ nexus/frameworks/base/privacy/java/android/privacy/IPrivacySettingsManager.aidl 2012-12-06 14:30:42.000000000 +1100
@@ -15,4 +15,7 @@ interface IPrivacySettingsManager
boolean setEnabled(boolean enable);
boolean setNotificationsEnabled(boolean enable);
void setBootCompleted();
+ boolean getIsAuthorizedManagerApp(String packageName);
+ void authorizeManagerApp(String packageName);
+ void deauthorizeManagerApp(String packageName);
}
diff -rupN '--exclude=.git' '--exclude=.svn' '--exclude=*.orig' '--exclude=*.rej' 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/PrivacyPersistenceAdapter.java nexus/frameworks/base/privacy/java/android/privacy/PrivacyPersistenceAdapter.java
--- 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/PrivacyPersistenceAdapter.java 2012-12-05 00:38:26.000000000 +1100
+++ nexus/frameworks/base/privacy/java/android/privacy/PrivacyPersistenceAdapter.java 2012-12-06 14:30:44.000000000 +1100
@@ -16,7 +16,9 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Set;
/**
* Responsible for persisting privacy settings to built-in memory
@@ -31,7 +33,7 @@ public class PrivacyPersistenceAdapter {
private static final String DATABASE_FILE = "/data/system/privacy.db";
- private static final int DATABASE_VERSION = 4;
+ private static final int DATABASE_VERSION = 5;
public static final int DUMMY_UID = -1;
@@ -47,6 +49,8 @@ public class PrivacyPersistenceAdapter {
private static final String TABLE_SETTINGS = "settings";
+ private static final String TABLE_MANAGER_APPS = "manager_apps";
+
private static final String TABLE_MAP = "map";
private static final String TABLE_ALLOWED_CONTACTS = "allowed_contacts";
@@ -104,6 +108,8 @@ public class PrivacyPersistenceAdapter {
" switchWifiStateSetting INTEGER" +
");";
+ private static final String CREATE_TABLE_MANAGER_APPS =
+ "CREATE TABLE IF NOT EXISTS " + TABLE_MANAGER_APPS + " ( packageName TEXT, signature TEXT, PRIMARY KEY (packageName, signature) )";
private static final String CREATE_TABLE_MAP =
"CREATE TABLE IF NOT EXISTS " + TABLE_MAP + " ( name TEXT PRIMARY KEY, value TEXT );";
@@ -212,7 +218,9 @@ public class PrivacyPersistenceAdapter {
}
}
- db.setTransactionSuccessful();
+ // need to add the management_apps table
+ db.execSQL(CREATE_TABLE_MANAGER_APPS);
+ db.setTransactionSuccessful();
}
} catch (Exception e) {
if (db != null && db.isOpen()) {
@@ -228,6 +236,25 @@ public class PrivacyPersistenceAdapter {
break;
case 4:
+ // need to add the management_apps table
+ try {
+ if (db != null && db.isOpen()) {
+ db.execSQL(CREATE_TABLE_MANAGER_APPS);
+ db.setTransactionSuccessful();
+ }
+ } catch (Exception e) {
+ if (db != null && db.isOpen()) {
+ db.endTransaction();
+ db.close();
+ }
+ Log.w(TAG, "upgradeDatabase - could not upgrade DB; will restore backup", e);
+ FileUtils.copyFile(dbBackupFile, dbFile);
+ dbBackupFile.delete();
+ }
+
+ break;
+
+ case 5:
// most current version, do nothing
Log.w(TAG, "upgradeDatabase - trying to upgrade most current DB version");
break;
@@ -606,6 +633,190 @@ public class PrivacyPersistenceAdapter {
return result;
}
+
+ public synchronized boolean getIsAuthorizedManagerApp(String [] packageNames, Set<String> signatures, boolean forceCloseDB) {
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - Starting with " + Integer.toString(signatures.size()) + " items");
+ Boolean isAuthorizedManagerApp = false;
+ if (packageNames == null || packageNames.length < 1) {
+ Log.e(TAG, "getIsAuthorizedManagerApp - insufficient application identifier - package name is required");
+ return isAuthorizedManagerApp;
+ }
+
+ // indicate that the DB is being read to prevent closing by other threads
+ readingThreads++;
+
+ SQLiteDatabase db;
+ try {
+ db = getReadableDatabase();
+ } catch (SQLiteException e) {
+ Log.e(TAG, "getIsAuthorizedManagerApp - database could not be opened", e);
+ readingThreads--;
+ return isAuthorizedManagerApp;
+ }
+
+ Cursor c = null;
+
+ //While it would be better from an isolation perspective to have the comparisons outside the database part,
+ //by doing the comparisons here we can short circuit the checks as soon as we get a match
+ try {
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - Running query");
+
+ for (String packageName : packageNames) {
+ c = query(db, TABLE_MANAGER_APPS, new String [] {"signature"}, "packageName=?", new String[] { packageName }, null, null, "signature", null);
+
+ if (c != null) {
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - cursor not null");
+ if (c.getCount() == 0) {
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - cursor getCount = 0");
+ //if there are no entries for the app, then it doesn't have permission to update settings
+ return false;
+ } else if (c.moveToFirst()) {
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - Result count " + Integer.toString(c.getCount()));
+ //we know the app is listed, so now we convert the signatures to something useful
+ int signatureColumn = c.getColumnIndex("signature"); //could probably use a constant for this, to (minimally) increase performance
+ do {
+ //as soon as we get a match, we can exit (one signature match is enough)
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - Check if signature present " + c.getString(signatureColumn));
+
+ if (signatures.contains(c.getString(signatureColumn))) {
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - Signature matched: app is permitted");
+ isAuthorizedManagerApp = true;
+ break;
+ }
+ Log.d(TAG, "getIsAuthorizedManagerApp in PrivacyPersistenceAdapter - Moving to next row");
+ } while (c.moveToNext());
+ }
+ }
+ }
+ } catch (Exception e) {
+ Log.e(TAG, "getIsAuthorizedManagerApp - Error occurred while reading database for signatures", e);
+ e.printStackTrace();
+ if (c != null) c.close();
+ } finally {
+ if (c != null) c.close();
+ synchronized (readingThreads) {
+ readingThreads--;
+ // only close DB if no other threads are reading
+ if (readingThreads == 0 && db != null && db.isOpen()) {
+ db.close();
+ }
+ }
+ }
+ return isAuthorizedManagerApp;
+ }
+
+ public synchronized void authorizeManagerApp(String packageName, Set<String> signatures, boolean forceCloseDB) {
+ if (packageName == null) {
+ Log.e(TAG, "authorizeManagerApp - insufficient application identifier - package name is required");
+ return;
+ }
+
+ // indicate that the DB is being read to prevent closing by other threads
+ readingThreads++;
+
+ SQLiteDatabase db;
+ try {
+ db = getWritableDatabase();
+ } catch (SQLiteException e) {
+ Log.e(TAG, "authorizeManagerApp - database could not be opened", e);
+ readingThreads--;
+ return;
+ }
+
+ Cursor c = null;
+ db.beginTransaction(); // make sure this ends up in a consistent state (DB and plain text files)
+
+ try {
+ c = query(db, TABLE_MANAGER_APPS, new String [] {"signature"}, "packageName=?", new String[] { packageName }, null, null, "signature", null);
+
+ if (c != null && c.getCount() != 0 && c.moveToFirst()) {
+ int signatureColumn = c.getColumnIndex("signature"); //could probably use a constant for this, to (minimally) increase performance
+ //there are one or more entries for this already - we need to remove ones with non-matching signatures before adding any new entries
+ List<String> sigsToDelete = new LinkedList<String>();
+ do {
+ if (signatures.contains(c.getString(signatureColumn))) {
+ signatures.remove(c.getString(signatureColumn));
+ } else {
+ sigsToDelete.add(c.getString(signatureColumn));
+ }
+ } while (c.moveToNext());
+
+ //delete non-matching signature entries
+ if (sigsToDelete.size() > 0) {
+ String [] whereArgs = new String [2];
+ whereArgs[0] = packageName;
+ for (String sigToDelete : sigsToDelete) {
+ whereArgs[1] = sigToDelete;
+ db.delete(TABLE_MANAGER_APPS, "packageName=? AND signature=?", whereArgs);
+ }
+ }
+ }
+
+ ContentValues values = new ContentValues();
+ values.put("packageName", packageName);
+ for (String signature : signatures) {
+ values.put("signature", signature);
+ db.insert(TABLE_MANAGER_APPS, null, values);
+ }
+
+ db.setTransactionSuccessful();
+
+ } catch (Exception e) {
+ Log.e(TAG, "authorizeManagerApp - Error occurred while reading database for signatures from : " + packageName, e);
+ e.printStackTrace();
+ if (c != null) c.close();
+ } finally {
+ if (c != null) c.close();
+ db.endTransaction();
+ synchronized (readingThreads) {
+ readingThreads--;
+ // only close DB if no other threads are reading
+ if (readingThreads == 0 && db != null && db.isOpen()) {
+ db.close();
+ }
+ }
+ }
+ }
+
+ public synchronized void deauthorizeManagerApp(String packageName, boolean forceCloseDB) {
+ if (packageName == null) {
+ Log.e(TAG, "deauthorizeManagerApp - insufficient application identifier - package name is required");
+ return;
+ }
+
+ // indicate that the DB is being read to prevent closing by other threads
+ readingThreads++;
+
+ SQLiteDatabase db;
+ try {
+ db = getWritableDatabase();
+ } catch (SQLiteException e) {
+ Log.e(TAG, "deauthorizeManagerApp - database could not be opened", e);
+ readingThreads--;
+ return;
+ }
+
+ db.beginTransaction(); // make sure this ends up in a consistent state (DB and plain text files)
+
+ try {
+ db.delete(TABLE_MANAGER_APPS, "packageName=?", new String [] {packageName});
+ db.setTransactionSuccessful();
+ } catch (Exception e) {
+ Log.e(TAG, "deauthorizeManagerApp - Error occurred while deleting rights for : " + packageName, e);
+ e.printStackTrace();
+ } finally {
+ db.endTransaction();
+ synchronized (readingThreads) {
+ readingThreads--;
+ // only close DB if no other threads are reading
+ if (readingThreads == 0 && db != null && db.isOpen()) {
+ db.close();
+ }
+ }
+ }
+ return;
+ }
+
/**
* This method creates external settings files for access from core librarys
* @param settingsName field name from database
@@ -822,6 +1033,7 @@ public class PrivacyPersistenceAdapter {
db.execSQL(CREATE_TABLE_SETTINGS);
db.execSQL(CREATE_TABLE_ALLOWED_CONTACTS);
db.execSQL(CREATE_TABLE_MAP);
+ db.execSQL(CREATE_TABLE_MANAGER_APPS);
db.execSQL(INSERT_VERSION);
db.execSQL(INSERT_ENABLED);
db.execSQL(INSERT_NOTIFICATIONS_ENABLED);
diff -rupN '--exclude=.git' '--exclude=.svn' '--exclude=*.orig' '--exclude=*.rej' 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/PrivacySettingsManager.java nexus/frameworks/base/privacy/java/android/privacy/PrivacySettingsManager.java
--- 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/PrivacySettingsManager.java 2012-12-05 00:44:18.000000000 +1100
+++ nexus/frameworks/base/privacy/java/android/privacy/PrivacySettingsManager.java 2012-12-06 14:30:44.000000000 +1100
@@ -52,7 +52,7 @@ public class PrivacySettingsManager {
public boolean saveSettings(PrivacySettings settings) {
try {
// Log.d(TAG, "saveSettings - " + settings);
- if (service != null) {
+ if (service != null) {
return service.saveSettings(settings);
} else {
Log.e(TAG, "saveSettings - PrivacySettingsManagerService is null");
@@ -197,4 +197,42 @@ public class PrivacySettingsManager {
Log.e(TAG, "RemoteException in setBootCompleted: ", e);
}
}
+
+ public boolean getIsAuthorizedManagerApp(String packageName) {
+ try {
+ if (service != null) {
+ return service.getIsAuthorizedManagerApp(packageName);
+ } else {
+ Log.e(TAG, "getIsAuthorizedManagerApp - PrivacySettingsManagerService is null");
+ return false;
+ }
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ public void authorizeManagerApp(String packageName) {
+ try {
+ if (service != null) {
+ service.authorizeManagerApp(packageName);
+ } else {
+ Log.e(TAG, "authorizeManagerApp - PrivacySettingsManagerService is null");
+ }
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public void deauthorizeManagerApp(String packageName) {
+ try {
+ if (service != null) {
+ service.deauthorizeManagerApp(packageName);
+ } else {
+ Log.e(TAG, "deauthorizeManagerApp - PrivacySettingsManagerService is null");
+ }
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ }
+ }
}
diff -rupN '--exclude=.git' '--exclude=.svn' '--exclude=*.orig' '--exclude=*.rej' 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/PrivacySettingsManagerService.java nexus/frameworks/base/privacy/java/android/privacy/PrivacySettingsManagerService.java
--- 4.1.2r1_pdroid/frameworks/base/privacy/java/android/privacy/PrivacySettingsManagerService.java 2012-12-05 00:38:26.000000000 +1100
+++ nexus/frameworks/base/privacy/java/android/privacy/PrivacySettingsManagerService.java 2012-12-06 14:37:46.000000000 +1100
@@ -1,11 +1,19 @@
package android.privacy;
+import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.Signature;
+import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Binder;
import android.util.Log;
import java.io.File;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
/**
* PrivacySettingsManager's counterpart running in the system process, which
@@ -21,6 +29,8 @@ public class PrivacySettingsManagerServi
private static final String WRITE_PRIVACY_SETTINGS = "android.privacy.WRITE_PRIVACY_SETTINGS";
private static final String READ_PRIVACY_SETTINGS = "android.privacy.READ_PRIVACY_SETTINGS";
+
+ private static final String MANAGE_PRIVACY_APPLICATIONS = "android.privacy.MANAGE_PRIVACY_APPLICATIONS";
private PrivacyPersistenceAdapter persistenceAdapter;
@@ -65,8 +75,12 @@ public class PrivacySettingsManagerServi
Log.d(TAG, "saveSettings - checking if caller (UID: " + Binder.getCallingUid() + ") has sufficient permissions");
// check permission if not being called by the system process
//if(!context.getPackageName().equals("com.privacy.pdroid.Addon")){ //enforce permission, because declaring in manifest doesn't work well -> let my addon package save settings
- if (Binder.getCallingUid() != 1000)
+ if (Binder.getCallingUid() != 1000) {
context.enforceCallingPermission(WRITE_PRIVACY_SETTINGS, "Requires WRITE_PRIVACY_SETTINGS");
+ if (!getIsAuthorizedManagerApp(Binder.getCallingPid())) {
+ throw new SecurityException("Application must be authorised to save changes");
+ }
+ }
//}
Log.d(TAG, "saveSettings - " + settings);
boolean result = persistenceAdapter.saveSettings(settings);
@@ -79,8 +93,12 @@ public class PrivacySettingsManagerServi
// "checking if caller (UID: " + Binder.getCallingUid() + ") has sufficient permissions");
// check permission if not being called by the system process
//if(!context.getPackageName().equals("com.privacy.pdroid.Addon")){//enforce permission, because declaring in manifest doesn't work well -> let my addon package delete settings
- if (Binder.getCallingUid() != 1000)
+ if (Binder.getCallingUid() != 1000) {
context.enforceCallingPermission(WRITE_PRIVACY_SETTINGS, "Requires WRITE_PRIVACY_SETTINGS");
+ if (!getIsAuthorizedManagerApp(Binder.getCallingPid())) {
+ throw new SecurityException("Application must be authorised to save changes");
+ }
+ }
//}
boolean result = persistenceAdapter.deleteSettings(packageName);
// update observer if directory exists
@@ -93,6 +111,94 @@ public class PrivacySettingsManagerServi
return result;
}
+ public boolean getIsAuthorizedManagerApp(int pid) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Getting packages for PID " + Integer.toString(pid));
+
+ String packageName = null;
+ ActivityManager actMgr = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
+ for(ActivityManager.RunningAppProcessInfo processInfo : actMgr.getRunningAppProcesses()){
+ if(processInfo.pid == pid){
+ packageName = processInfo.processName;
+ }
+ }
+ if (packageName == null) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Package name could not be obtained");
+ return false;
+ }
+
+ return getIsAuthorizedManagerApp(packageName);
+ }
+
+ public boolean getIsAuthorizedManagerApp(String packageName) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Running for package " + packageName);
+ PackageManager pkgMgr = context.getPackageManager();
+ if (pkgMgr == null) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Package manager could not be obtained");
+ return false;
+ }
+
+ PackageInfo pkgInfo;
+ try {
+ //get the package info so we can get the signatures
+ pkgInfo = pkgMgr.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
+ } catch (NameNotFoundException e) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Could not get package with name " + packageName);
+ return false;
+ }
+ if (pkgInfo == null) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Got null back when retrieving packageInfo for " + packageName);
+ return false;
+ }
+
+ Log.d(TAG, "getIsAuthorizedManagerApp - Retrieving asciiSigs for " + packageName);
+ Set<String> asciiSigs = new HashSet<String>();
+ for (Signature signature : pkgInfo.signatures) {
+ Log.d(TAG, "getIsAuthorizedManagerApp - Found signature " + signature.toCharsString());
+ asciiSigs.add(signature.toCharsString());
+ }
+
+ return persistenceAdapter.getIsAuthorizedManagerApp(new String[] {packageName}, asciiSigs, false);
+ }
+
+
+ public void authorizeManagerApp(String packageName) {
+ if (Binder.getCallingUid() != 1000)
+ context.enforceCallingPermission(MANAGE_PRIVACY_APPLICATIONS, "Requires MANAGE_PRIVACY_APPLICATIONS");
+
+ PackageManager pkgMgr = context.getPackageManager();
+ if (pkgMgr == null) {
+ Log.d(TAG, "authorizeManagerApp - Package manager could not be obtained");
+ return;
+ }
+
+ PackageInfo pkgInfo;
+ try {
+ //get the package info so we can get the signatures
+ pkgInfo = pkgMgr.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
+ } catch (NameNotFoundException e) {
+ Log.d(TAG, "authorizeManagerApp - Could not get package with name " + packageName);
+ return;
+ }
+ if (pkgInfo == null) {
+ Log.d(TAG, "authorizeManagerApp - Got null back when retrieving packageInfo for " + packageName);
+ return;
+ }
+
+ Set<String> asciiSigs = new HashSet<String>();
+ for (Signature signature : pkgInfo.signatures) {
+ asciiSigs.add(signature.toCharsString());
+ }
+
+ persistenceAdapter.authorizeManagerApp(packageName, asciiSigs, false);
+ }
+
+ public void deauthorizeManagerApp(String packageName) {
+ if (Binder.getCallingUid() != 1000)
+ context.enforceCallingPermission(MANAGE_PRIVACY_APPLICATIONS, "Requires MANAGE_PRIVACY_APPLICATIONS");
+
+ persistenceAdapter.deauthorizeManagerApp(packageName, false);
+ }
+
public double getVersion() {
return VERSION;
}