From a23f12a1a9db0d75c39cbfbe9e794dc84418c962 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Mon, 10 Jun 2019 18:19:31 -0700 Subject: [PATCH] Catch SecurityException thrown from job binding In some circumstances, a SecurityException can be thrown by bindService() even when called by the OS itself. If this happens when starting up a scheduled job, make sure to catch and handle the exception rather than allowing the system server to crash. Fixes: 134893684 Test: atest CtsJobSchedulerTestCases Change-Id: Ib0dd2df5d48e13e7b7c75d7117d0ffa361b000ce --- .../android/server/job/JobServiceContext.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/job/JobServiceContext.java b/services/core/java/com/android/server/job/JobServiceContext.java index 7689bd26a1939..65dac8bf7cdd1 100644 --- a/services/core/java/com/android/server/job/JobServiceContext.java +++ b/services/core/java/com/android/server/job/JobServiceContext.java @@ -247,10 +247,20 @@ public final class JobServiceContext implements ServiceConnection { mVerb = VERB_BINDING; scheduleOpTimeOutLocked(); final Intent intent = new Intent().setComponent(job.getServiceComponent()); - boolean binding = mContext.bindServiceAsUser(intent, this, - Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND - | Context.BIND_NOT_VISIBLE | Context.BIND_ADJUST_BELOW_PERCEPTIBLE, - new UserHandle(job.getUserId())); + boolean binding = false; + try { + binding = mContext.bindServiceAsUser(intent, this, + Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND + | Context.BIND_NOT_VISIBLE | Context.BIND_ADJUST_BELOW_PERCEPTIBLE, + new UserHandle(job.getUserId())); + } catch (SecurityException e) { + // Some permission policy, for example INTERACT_ACROSS_USERS and + // android:singleUser, can result in a SecurityException being thrown from + // bindServiceAsUser(). If this happens, catch it and fail gracefully. + Slog.w(TAG, "Job service " + job.getServiceComponent().getShortClassName() + + " cannot be executed: " + e.getMessage()); + binding = false; + } if (!binding) { if (DEBUG) { Slog.d(TAG, job.getServiceComponent().getShortClassName() + " unavailable.");