Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.server.util; |
| 18 | |
| 19 | import static android.os.Binder.getCallingUid; |
| 20 | |
| 21 | import android.os.Process; |
Remi NGUYEN VAN | 3b90687 | 2019-01-31 08:46:36 +0900 | [diff] [blame] | 22 | import android.os.UserHandle; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Utility class to check calling permissions on the network stack. |
| 26 | */ |
| 27 | public final class PermissionUtil { |
| 28 | |
| 29 | /** |
| 30 | * Check that the caller is allowed to communicate with the network stack. |
| 31 | * @throws SecurityException The caller is not allowed to communicate with the network stack. |
| 32 | */ |
| 33 | public static void checkNetworkStackCallingPermission() { |
| 34 | // TODO: check that the calling PID is the system server. |
Remi NGUYEN VAN | 69b967f | 2019-01-13 03:10:49 +0900 | [diff] [blame] | 35 | final int caller = getCallingUid(); |
Chalard Jean | 12aaf7e | 2019-03-08 19:46:00 +0900 | [diff] [blame] | 36 | if (caller != Process.SYSTEM_UID |
| 37 | && UserHandle.getAppId(caller) != Process.BLUETOOTH_UID |
| 38 | && UserHandle.getAppId(caller) != Process.PHONE_UID) { |
Remi NGUYEN VAN | 69b967f | 2019-01-13 03:10:49 +0900 | [diff] [blame] | 39 | throw new SecurityException("Invalid caller: " + caller); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Check that the caller is allowed to dump the network stack, e.g. dumpsys. |
| 45 | * @throws SecurityException The caller is not allowed to dump the network stack. |
| 46 | */ |
| 47 | public static void checkDumpPermission() { |
| 48 | final int caller = getCallingUid(); |
| 49 | if (caller != Process.SYSTEM_UID && caller != Process.ROOT_UID |
| 50 | && caller != Process.SHELL_UID) { |
| 51 | throw new SecurityException("No dump permissions for caller: " + caller); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | private PermissionUtil() { |
| 56 | throw new UnsupportedOperationException("This class is not to be instantiated"); |
| 57 | } |
| 58 | } |