Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.integrity.engine; |
| 18 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 19 | import android.content.integrity.AppInstallMetadata; |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 20 | import android.content.integrity.AtomicFormula; |
| 21 | import android.content.integrity.CompoundFormula; |
| 22 | import android.content.integrity.Formula; |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 23 | import android.content.integrity.Rule; |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 24 | import android.util.Slog; |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 25 | |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 27 | import com.android.server.integrity.IntegrityFileManager; |
Khaled Abdelmohsen | aba57d6 | 2019-10-08 13:40:38 +0100 | [diff] [blame] | 28 | import com.android.server.integrity.model.IntegrityCheckResult; |
Khaled Abdelmohsen | 3101269 | 2019-10-02 17:54:47 +0100 | [diff] [blame] | 29 | |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 30 | import java.util.ArrayList; |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 31 | import java.util.Arrays; |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 32 | import java.util.List; |
Song Pan | f17fd09 | 2019-11-29 13:00:50 +0000 | [diff] [blame] | 33 | import java.util.Map; |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 34 | import java.util.Optional; |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * The engine used to evaluate rules against app installs. |
| 38 | * |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 39 | * <p>Every app install is evaluated against rules (pushed by the verifier) by the evaluation engine |
| 40 | * to allow/block that install. |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 41 | */ |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 42 | public class RuleEvaluationEngine { |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 43 | private static final String TAG = "RuleEvaluation"; |
| 44 | |
| 45 | // The engine for loading rules, retrieving metadata for app installs, and evaluating app |
| 46 | // installs against rules. |
| 47 | private static RuleEvaluationEngine sRuleEvaluationEngine; |
| 48 | |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 49 | private final IntegrityFileManager mIntegrityFileManager; |
| 50 | |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 51 | @VisibleForTesting |
| 52 | RuleEvaluationEngine(IntegrityFileManager integrityFileManager) { |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 53 | mIntegrityFileManager = integrityFileManager; |
| 54 | } |
| 55 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 56 | /** Provide a singleton instance of the rule evaluation engine. */ |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 57 | public static synchronized RuleEvaluationEngine getRuleEvaluationEngine() { |
| 58 | if (sRuleEvaluationEngine == null) { |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 59 | return new RuleEvaluationEngine(IntegrityFileManager.getInstance()); |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 60 | } |
| 61 | return sRuleEvaluationEngine; |
| 62 | } |
Khaled Abdelmohsen | aba57d6 | 2019-10-08 13:40:38 +0100 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Load, and match the list of rules against an app install metadata. |
| 66 | * |
| 67 | * @param appInstallMetadata Metadata of the app to be installed, and to evaluate the rules |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 68 | * against. |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 69 | * @return result of the integrity check |
Khaled Abdelmohsen | aba57d6 | 2019-10-08 13:40:38 +0100 | [diff] [blame] | 70 | */ |
Song Pan | f17fd09 | 2019-11-29 13:00:50 +0000 | [diff] [blame] | 71 | public IntegrityCheckResult evaluate( |
| 72 | AppInstallMetadata appInstallMetadata, Map<String, String> allowedInstallers) { |
Khaled Abdelmohsen | aba57d6 | 2019-10-08 13:40:38 +0100 | [diff] [blame] | 73 | List<Rule> rules = loadRules(appInstallMetadata); |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 74 | allowedInstallersRule(allowedInstallers).ifPresent(rules::add); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 75 | return RuleEvaluator.evaluateRules(rules, appInstallMetadata); |
Khaled Abdelmohsen | aba57d6 | 2019-10-08 13:40:38 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | private List<Rule> loadRules(AppInstallMetadata appInstallMetadata) { |
Song Pan | 6e3677c | 2019-10-29 14:19:26 +0000 | [diff] [blame] | 79 | try { |
| 80 | return mIntegrityFileManager.readRules(appInstallMetadata); |
| 81 | } catch (Exception e) { |
| 82 | Slog.e(TAG, "Error loading rules.", e); |
| 83 | return new ArrayList<>(); |
| 84 | } |
Khaled Abdelmohsen | aba57d6 | 2019-10-08 13:40:38 +0100 | [diff] [blame] | 85 | } |
Song Pan | e24b80a | 2019-12-30 15:35:10 +0000 | [diff] [blame] | 86 | |
| 87 | private static Optional<Rule> allowedInstallersRule(Map<String, String> allowedInstallers) { |
| 88 | if (allowedInstallers.isEmpty()) { |
| 89 | return Optional.empty(); |
| 90 | } |
| 91 | |
| 92 | List<Formula> formulas = new ArrayList<>(allowedInstallers.size()); |
| 93 | allowedInstallers.forEach( |
| 94 | (installer, cert) -> { |
| 95 | formulas.add(allowedInstallerFormula(installer, cert)); |
| 96 | }); |
| 97 | |
| 98 | // We need this special case since OR-formulas require at least two operands. |
| 99 | Formula allInstallersFormula = |
| 100 | formulas.size() == 1 |
| 101 | ? formulas.get(0) |
| 102 | : new CompoundFormula(CompoundFormula.OR, formulas); |
| 103 | |
| 104 | return Optional.of( |
| 105 | new Rule( |
| 106 | new CompoundFormula( |
| 107 | CompoundFormula.NOT, Arrays.asList(allInstallersFormula)), |
| 108 | Rule.DENY)); |
| 109 | } |
| 110 | |
| 111 | private static Formula allowedInstallerFormula(String installer, String cert) { |
| 112 | return new CompoundFormula( |
| 113 | CompoundFormula.AND, |
| 114 | Arrays.asList( |
| 115 | new AtomicFormula.StringAtomicFormula( |
| 116 | AtomicFormula.INSTALLER_NAME, |
| 117 | installer, |
| 118 | /* isHashedValue= */ false), |
| 119 | new AtomicFormula.StringAtomicFormula( |
| 120 | AtomicFormula.INSTALLER_CERTIFICATE, |
| 121 | cert, |
| 122 | /* isHashedValue= */ false))); |
| 123 | } |
Khaled Abdelmohsen | 3a7a43f | 2019-10-02 11:38:22 +0100 | [diff] [blame] | 124 | } |