1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.internal.telephony.ims; 18 19 import android.Manifest; 20 import android.app.AppOpsManager; 21 import android.content.Context; 22 import android.os.Binder; 23 24 class RcsPermissions { checkReadPermissions(Context context, String callingPackage)25 static void checkReadPermissions(Context context, String callingPackage) { 26 int pid = Binder.getCallingPid(); 27 int uid = Binder.getCallingUid(); 28 29 context.enforcePermission(Manifest.permission.READ_SMS, pid, uid, null); 30 31 checkOp(context, uid, callingPackage, AppOpsManager.OP_READ_SMS); 32 } 33 checkWritePermissions(Context context, String callingPackage)34 static void checkWritePermissions(Context context, String callingPackage) { 35 int uid = Binder.getCallingUid(); 36 37 checkOp(context, uid, callingPackage, AppOpsManager.OP_WRITE_SMS); 38 } 39 40 /** 41 * Notes the provided op, but throws even if the op mode is {@link AppOpsManager.MODE_IGNORED}. 42 * <p> 43 * {@link AppOpsManager.OP_WRITE_SMS} defaults to {@link AppOpsManager.MODE_IGNORED} to avoid 44 * crashing applications written before the app op was introduced. Since this is a new API, 45 * consumers should be aware of the permission requirements, and we should be safe to throw a 46 * {@link SecurityException} instead of providing a dummy value (which could cause unexpected 47 * application behavior and possible loss of user data). {@link AppOpsManager.OP_READ_SMS} is 48 * not normally in {@link AppOpsManager.MODE_IGNORED}, but we maintain the same behavior for 49 * consistency with handling of write permissions. 50 */ checkOp(Context context, int uid, String callingPackage, int op)51 private static void checkOp(Context context, int uid, String callingPackage, int op) { 52 AppOpsManager appOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 53 54 int mode = appOps.noteOp(op, uid, callingPackage); 55 56 if (mode != AppOpsManager.MODE_ALLOWED) { 57 throw new SecurityException( 58 AppOpsManager.opToName(op) + " not allowed for " + callingPackage); 59 } 60 } 61 } 62