blob: 6fbeeadb7e7270c0ffb1de7e288fd4a7dda08ad1 [file] [log] [blame]
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +09001/*
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
17package com.android.server.util;
18
19import static android.os.Binder.getCallingUid;
20
21import android.os.Process;
Remi NGUYEN VAN3b906872019-01-31 08:46:36 +090022import android.os.UserHandle;
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090023
24/**
25 * Utility class to check calling permissions on the network stack.
26 */
27public 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 VAN69b967f2019-01-13 03:10:49 +090035 final int caller = getCallingUid();
Chalard Jean12aaf7e2019-03-08 19:46:00 +090036 if (caller != Process.SYSTEM_UID
37 && UserHandle.getAppId(caller) != Process.BLUETOOTH_UID
38 && UserHandle.getAppId(caller) != Process.PHONE_UID) {
Remi NGUYEN VAN69b967f2019-01-13 03:10:49 +090039 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 VAN0e3d09232018-12-04 12:13:09 +090052 }
53 }
54
55 private PermissionUtil() {
56 throw new UnsupportedOperationException("This class is not to be instantiated");
57 }
58}