@@ -106,16 +106,16 @@ public synchronized boolean prepare(String oldPackage, String newPackage) {
106106 return true ;
107107 }
108108
109- // Only system user can revoke a package.
110- if (Binder .getCallingUid () != Process .SYSTEM_UID ) {
111- throw new SecurityException ("Unauthorized Caller" );
112- }
109+ // Check if the caller is authorized.
110+ enforceControlPermission ();
113111
114112 // Reset the interface and hide the notification.
115113 if (mInterface != null ) {
116114 jniReset (mInterface );
115+ long identity = Binder .clearCallingIdentity ();
117116 mCallback .restore ();
118117 hideNotification ();
118+ Binder .restoreCallingIdentity (identity );
119119 mInterface = null ;
120120 }
121121
@@ -291,6 +291,26 @@ public synchronized void interfaceRemoved(String interfaze) {
291291 public void limitReached (String limit , String interfaze ) {
292292 }
293293
294+ private void enforceControlPermission () {
295+ // System user is allowed to control VPN.
296+ if (Binder .getCallingUid () == Process .SYSTEM_UID ) {
297+ return ;
298+ }
299+
300+ try {
301+ // System dialogs are also allowed to control VPN.
302+ PackageManager pm = mContext .getPackageManager ();
303+ ApplicationInfo app = pm .getApplicationInfo (VpnConfig .DIALOGS_PACKAGE , 0 );
304+ if (Binder .getCallingUid () == app .uid ) {
305+ return ;
306+ }
307+ } catch (Exception e ) {
308+ // ignore
309+ }
310+
311+ throw new SecurityException ("Unauthorized Caller" );
312+ }
313+
294314 private class Connection implements ServiceConnection {
295315 private IBinder mService ;
296316
@@ -368,10 +388,8 @@ public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, Strin
368388 * Return the information of the current ongoing legacy VPN.
369389 */
370390 public synchronized LegacyVpnInfo getLegacyVpnInfo () {
371- // Only system user can call this method.
372- if (Binder .getCallingUid () != Process .SYSTEM_UID ) {
373- throw new SecurityException ("Unauthorized Caller" );
374- }
391+ // Check if the caller is authorized.
392+ enforceControlPermission ();
375393 return (mLegacyVpnRunner == null ) ? null : mLegacyVpnRunner .getInfo ();
376394 }
377395
0 commit comments