Yuyang Huang | 4155755 | 2023-11-28 12:47:22 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | import com.android.modules.utils.build.SdkLevel; |
| 24 | |
| 25 | import java.io.BufferedReader; |
| 26 | import java.io.File; |
| 27 | import java.io.FileInputStream; |
| 28 | import java.io.FileNotFoundException; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.InputStreamReader; |
| 32 | import java.nio.charset.StandardCharsets; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
| 37 | * BpfRcUtils is responsible for comparing the bpf loader rc file. |
| 38 | * |
| 39 | * {@hide} |
| 40 | */ |
| 41 | public class BpfLoaderRcUtils { |
| 42 | public static final String TAG = BpfLoaderRcUtils.class.getSimpleName(); |
| 43 | |
| 44 | private static final List<String> BPF_LOADER_RC_S_T = List.of( |
| 45 | "service bpfloader /system/bin/bpfloader", |
| 46 | "capabilities CHOWN SYS_ADMIN NET_ADMIN", |
| 47 | "rlimit memlock 1073741824 1073741824", |
| 48 | "oneshot", |
| 49 | "reboot_on_failure reboot,bpfloader-failed", |
| 50 | "updatable" |
| 51 | ); |
| 52 | |
| 53 | private static final List<String> BPF_LOADER_RC_U = List.of( |
| 54 | "service bpfloader /system/bin/bpfloader", |
| 55 | "capabilities CHOWN SYS_ADMIN NET_ADMIN", |
| 56 | "group root graphics network_stack net_admin net_bw_acct net_bw_stats net_raw system", |
| 57 | "user root", |
| 58 | "rlimit memlock 1073741824 1073741824", |
| 59 | "oneshot", |
| 60 | "reboot_on_failure reboot,bpfloader-failed", |
| 61 | "updatable" |
| 62 | ); |
| 63 | |
| 64 | private static final List<String> BPF_LOADER_RC_UQPR2 = List.of( |
| 65 | "service bpfloader /system/bin/netbpfload", |
| 66 | "capabilities CHOWN SYS_ADMIN NET_ADMIN", |
| 67 | "group root graphics network_stack net_admin net_bw_acct net_bw_stats net_raw system", |
| 68 | "user root", |
| 69 | "rlimit memlock 1073741824 1073741824", |
| 70 | "oneshot", |
| 71 | "reboot_on_failure reboot,bpfloader-failed", |
| 72 | "updatable" |
| 73 | ); |
| 74 | |
| 75 | |
| 76 | private static final String BPF_LOADER_RC_FILE_PATH = "/etc/init/bpfloader.rc"; |
| 77 | private static final String NET_BPF_LOAD_RC_FILE_PATH = "/etc/init/netbpfload.rc"; |
| 78 | |
| 79 | private BpfLoaderRcUtils() { |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Load the bpf rc file content from the input stream. |
| 84 | */ |
| 85 | @VisibleForTesting |
| 86 | public static List<String> loadExistingBpfRcFile(@NonNull InputStream inputStream) { |
| 87 | List<String> contents = new ArrayList<>(); |
| 88 | boolean bpfSectionFound = false; |
| 89 | try (BufferedReader br = new BufferedReader( |
Yuyang Huang | 71a65dc | 2023-12-08 15:03:01 +0900 | [diff] [blame] | 90 | new InputStreamReader(inputStream, StandardCharsets.ISO_8859_1))) { |
Yuyang Huang | 4155755 | 2023-11-28 12:47:22 +0900 | [diff] [blame] | 91 | String line; |
| 92 | while ((line = br.readLine()) != null) { |
| 93 | line = line.trim(); |
| 94 | if (line.isEmpty()) { |
| 95 | continue; |
| 96 | } |
| 97 | if (line.startsWith("#")) { |
| 98 | continue; |
| 99 | } |
| 100 | // If bpf service section was found and new service or action section start. The |
| 101 | // read should stop. |
| 102 | if (bpfSectionFound && (line.startsWith("service ") || (line.startsWith("on ")))) { |
| 103 | break; |
| 104 | } |
| 105 | if (line.startsWith("service bpfloader ")) { |
| 106 | bpfSectionFound = true; |
| 107 | } |
| 108 | if (bpfSectionFound) { |
| 109 | contents.add(line); |
| 110 | } |
| 111 | } |
| 112 | } catch (IOException e) { |
| 113 | Log.wtf("read input stream failed.", e); |
| 114 | contents.clear(); |
| 115 | return contents; |
| 116 | } |
| 117 | return contents; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Check the bpfLoader rc file on the system image matches any of the template files. |
| 122 | */ |
| 123 | public static boolean checkBpfLoaderRc() { |
| 124 | File bpfRcFile = new File(BPF_LOADER_RC_FILE_PATH); |
| 125 | if (!bpfRcFile.exists()) { |
| 126 | if (SdkLevel.isAtLeastU()) { |
| 127 | bpfRcFile = new File(NET_BPF_LOAD_RC_FILE_PATH); |
| 128 | } |
| 129 | if (!bpfRcFile.exists()) { |
| 130 | Log.wtf(TAG, |
| 131 | "neither " + BPF_LOADER_RC_FILE_PATH + " nor " + NET_BPF_LOAD_RC_FILE_PATH |
| 132 | + " exist."); |
| 133 | return false; |
| 134 | } |
| 135 | // Check bpf rc file in U QPR2 |
| 136 | return compareBpfLoaderRc(bpfRcFile, BPF_LOADER_RC_UQPR2); |
| 137 | } |
| 138 | |
| 139 | if (SdkLevel.isAtLeastU()) { |
| 140 | // Check bpf rc file in U |
| 141 | return compareBpfLoaderRc(bpfRcFile, BPF_LOADER_RC_U); |
| 142 | } |
| 143 | // Check bpf rc file in S/T |
| 144 | return compareBpfLoaderRc(bpfRcFile, BPF_LOADER_RC_S_T); |
| 145 | } |
| 146 | |
| 147 | private static boolean compareBpfLoaderRc(@NonNull File bpfRcFile, |
| 148 | @NonNull List<String> template) { |
| 149 | try { |
| 150 | List<String> actualContent = loadExistingBpfRcFile(new FileInputStream(bpfRcFile)); |
| 151 | if (!actualContent.equals(template)) { |
| 152 | Log.wtf(TAG, "BPF rc file is not same as the template files " + actualContent); |
| 153 | return false; |
| 154 | } |
| 155 | } catch (FileNotFoundException e) { |
| 156 | Log.wtf(bpfRcFile.getPath() + " doesn't exist.", e); |
| 157 | return false; |
| 158 | } |
| 159 | return true; |
| 160 | } |
| 161 | } |