1 /*
2  * Copyright (C) 2015 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.permissioncontroller.permission.utils;
18 
19 import android.util.EventLog;
20 
21 import com.android.permissioncontroller.permission.model.livedatatypes.LightAppPermGroup;
22 import com.android.permissioncontroller.permission.model.livedatatypes.LightPermission;
23 
24 import java.util.List;
25 
26 public final class SafetyNetLogger {
27 
28     // The log tag used by SafetyNet to pick entries from the event log.
29     private static final int SNET_NET_EVENT_LOG_TAG = 0x534e4554;
30 
31     // Log tag for the result of permissions request.
32     private static final String PERMISSIONS_REQUESTED = "individual_permissions_requested";
33 
34     // Log tag for the result of permissions toggling.
35     private static final String PERMISSIONS_TOGGLED = "individual_permissions_toggled";
36 
SafetyNetLogger()37     private SafetyNetLogger() {
38         /* do nothing */
39     }
40 
41     /**
42      * Log that permission groups have been requested for the purpose of safety net.
43      *
44      * <p>The groups might refer to different permission groups and different apps.
45      *
46      * @param packageName The name of the package for which permissions were requested
47      * @param uid The uid of the package
48      * @param groups The permission groups which were requested
49      */
logPermissionsRequested(String packageName, int uid, List<LightAppPermGroup> groups)50     public static void logPermissionsRequested(String packageName, int uid,
51             List<LightAppPermGroup> groups) {
52         EventLog.writeEvent(SNET_NET_EVENT_LOG_TAG, PERMISSIONS_REQUESTED, uid,
53                 buildChangedPermissionForPackageMessageNew(packageName, groups));
54     }
55 
56     /**
57      * Log that a permission group has been toggled for the purpose of safety net.
58      *
59      * @param group The group which was toggled. This group must represent the current state, not
60      * the old state
61      * @param logOnlyBackground Whether to log only background permissions, or foreground and
62      * background
63      */
logPermissionToggled(LightAppPermGroup group, boolean logOnlyBackground)64     public static void logPermissionToggled(LightAppPermGroup group, boolean logOnlyBackground) {
65         StringBuilder builder = new StringBuilder();
66         buildChangedPermissionForGroup(group, logOnlyBackground, builder);
67         EventLog.writeEvent(SNET_NET_EVENT_LOG_TAG, PERMISSIONS_TOGGLED,
68                 android.os.Process.myUid(), builder.toString());
69     }
70 
71     /**
72      * Log that a permission group has been toggled for the purpose of safety net. Logs both
73      * background and foreground permissions.
74      *
75      * @param group The group which was toggled. This group must represent the current state, not
76      * the old state
77      */
logPermissionToggled(LightAppPermGroup group)78     public static void logPermissionToggled(LightAppPermGroup group) {
79         logPermissionToggled(group, false);
80     }
81 
buildChangedPermissionForGroup(LightAppPermGroup group, boolean logOnlyBackground, StringBuilder builder)82     private static void buildChangedPermissionForGroup(LightAppPermGroup group,
83             boolean logOnlyBackground, StringBuilder builder) {
84 
85         builder.append(group.getPackageInfo().getPackageName()).append(':');
86 
87         for (LightPermission permission: group.getPermissions().values()) {
88             if (logOnlyBackground
89                     && !group.getBackgroundPermNames().contains(permission.getName())) {
90                 continue;
91             }
92 
93             if (builder.length() > 0) {
94                 builder.append(';');
95             }
96 
97             builder.append(permission.getName()).append('|');
98             builder.append(permission.isGrantedIncludingAppOp()).append('|');
99             builder.append(permission.getFlags());
100         }
101     }
102 
buildChangedPermissionForPackageMessageNew(String packageName, List<LightAppPermGroup> groups)103     private static String buildChangedPermissionForPackageMessageNew(String packageName,
104             List<LightAppPermGroup> groups) {
105         StringBuilder builder = new StringBuilder();
106 
107         builder.append(packageName).append(':');
108         for (LightAppPermGroup group: groups) {
109             buildChangedPermissionForGroup(group, false, builder);
110         }
111         return builder.toString();
112     }
113 }
114