blob: f6eb900c4910387e36e44115d1bf0d82364314b3 [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();
Remi NGUYEN VAN3b906872019-01-31 08:46:36 +090036 if (caller != Process.SYSTEM_UID && UserHandle.getAppId(caller) != Process.BLUETOOTH_UID) {
Remi NGUYEN VAN69b967f2019-01-13 03:10:49 +090037 throw new SecurityException("Invalid caller: " + caller);
38 }
39 }
40
41 /**
42 * Check that the caller is allowed to dump the network stack, e.g. dumpsys.
43 * @throws SecurityException The caller is not allowed to dump the network stack.
44 */
45 public static void checkDumpPermission() {
46 final int caller = getCallingUid();
47 if (caller != Process.SYSTEM_UID && caller != Process.ROOT_UID
48 && caller != Process.SHELL_UID) {
49 throw new SecurityException("No dump permissions for caller: " + caller);
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090050 }
51 }
52
53 private PermissionUtil() {
54 throw new UnsupportedOperationException("This class is not to be instantiated");
55 }
56}