dcashman | 9b61575 | 2015-01-07 14:23:11 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 2 | |
| 3 | src_header = """/* |
| 4 | * Copyright (C) 2014 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | package android.cts.security; |
| 20 | |
Changfei Chen | 178b43b | 2016-12-05 18:13:06 -0800 | [diff] [blame] | 21 | import android.platform.test.annotations.RestrictedBuildTest; |
Aaron Holden | d16ae8f | 2016-11-22 18:44:36 -0800 | [diff] [blame] | 22 | import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 23 | import com.android.tradefed.build.IBuildInfo; |
| 24 | import com.android.tradefed.device.ITestDevice; |
| 25 | import com.android.tradefed.testtype.DeviceTestCase; |
| 26 | import com.android.tradefed.testtype.IBuildReceiver; |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 27 | import com.android.tradefed.testtype.IDeviceTest; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 28 | |
| 29 | import java.io.BufferedReader; |
| 30 | import java.io.File; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 31 | import java.io.InputStream; |
| 32 | import java.io.InputStreamReader; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Neverallow Rules SELinux tests. |
| 36 | */ |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 37 | public class SELinuxNeverallowRulesTest extends DeviceTestCase implements IBuildReceiver, IDeviceTest { |
Tri Vo | a2631da | 2018-04-12 14:54:19 -0700 | [diff] [blame] | 38 | private static final int P_SEPOLICY_VERSION = 28; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 39 | private File sepolicyAnalyze; |
| 40 | private File devicePolicyFile; |
Tri Vo | a2631da | 2018-04-12 14:54:19 -0700 | [diff] [blame] | 41 | private File deviceSystemPolicyFile; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 42 | |
dcashman | 9cf20df | 2016-04-01 11:32:35 -0700 | [diff] [blame] | 43 | private IBuildInfo mBuild; |
Tri Vo | a2631da | 2018-04-12 14:54:19 -0700 | [diff] [blame] | 44 | private int mVendorSepolicyVersion = -1; |
dcashman | 9cf20df | 2016-04-01 11:32:35 -0700 | [diff] [blame] | 45 | |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 46 | /** |
| 47 | * A reference to the device under test. |
| 48 | */ |
| 49 | private ITestDevice mDevice; |
| 50 | |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 51 | /** |
| 52 | * {@inheritDoc} |
| 53 | */ |
| 54 | @Override |
| 55 | public void setBuild(IBuildInfo build) { |
dcashman | 9cf20df | 2016-04-01 11:32:35 -0700 | [diff] [blame] | 56 | mBuild = build; |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 57 | } |
| 58 | |
dcashman | 4371f00 | 2016-03-29 10:42:03 -0700 | [diff] [blame] | 59 | /** |
| 60 | * {@inheritDoc} |
| 61 | */ |
| 62 | @Override |
| 63 | public void setDevice(ITestDevice device) { |
| 64 | super.setDevice(device); |
| 65 | mDevice = device; |
| 66 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 67 | @Override |
| 68 | protected void setUp() throws Exception { |
| 69 | super.setUp(); |
Aaron Holden | d16ae8f | 2016-11-22 18:44:36 -0800 | [diff] [blame] | 70 | CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuild); |
| 71 | sepolicyAnalyze = buildHelper.getTestFile("sepolicy-analyze"); |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 72 | sepolicyAnalyze.setExecutable(true); |
| 73 | |
Alex Klyubin | e91509c | 2017-04-14 11:17:19 -0700 | [diff] [blame] | 74 | devicePolicyFile = android.security.cts.SELinuxHostTest.getDevicePolicyFile(mDevice); |
Tri Vo | a2631da | 2018-04-12 14:54:19 -0700 | [diff] [blame] | 75 | deviceSystemPolicyFile = |
| 76 | android.security.cts.SELinuxHostTest.getDeviceSystemPolicyFile(mDevice); |
| 77 | |
| 78 | // Caching this variable to save time. |
| 79 | if (mVendorSepolicyVersion == -1) { |
| 80 | mVendorSepolicyVersion = |
| 81 | android.security.cts.SELinuxHostTest.getVendorSepolicyVersion(mDevice); |
| 82 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 83 | } |
Alex Klyubin | 9dd67db | 2017-04-06 20:14:43 -0700 | [diff] [blame] | 84 | |
| 85 | private boolean isFullTrebleDevice() throws Exception { |
| 86 | return android.security.cts.SELinuxHostTest.isFullTrebleDevice(mDevice); |
| 87 | } |
Jaekyun Seok | 64495e1 | 2018-01-30 17:08:54 +0900 | [diff] [blame] | 88 | |
| 89 | private boolean isCompatiblePropertyEnforcedDevice() throws Exception { |
| 90 | return android.security.cts.SELinuxHostTest.isCompatiblePropertyEnforcedDevice(mDevice); |
| 91 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 92 | """ |
| 93 | src_body = "" |
| 94 | src_footer = """} |
| 95 | """ |
| 96 | |
| 97 | src_method = """ |
Changfei Chen | 178b43b | 2016-12-05 18:13:06 -0800 | [diff] [blame] | 98 | @RestrictedBuildTest |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 99 | public void testNeverallowRules() throws Exception { |
| 100 | String neverallowRule = "$NEVERALLOW_RULE_HERE$"; |
Alex Klyubin | 9dd67db | 2017-04-06 20:14:43 -0700 | [diff] [blame] | 101 | boolean fullTrebleOnly = $FULL_TREBLE_ONLY_BOOL_HERE$; |
Jaekyun Seok | 64495e1 | 2018-01-30 17:08:54 +0900 | [diff] [blame] | 102 | boolean compatiblePropertyOnly = $COMPATIBLE_PROPERTY_ONLY_BOOL_HERE$; |
Alex Klyubin | 9dd67db | 2017-04-06 20:14:43 -0700 | [diff] [blame] | 103 | |
| 104 | if ((fullTrebleOnly) && (!isFullTrebleDevice())) { |
| 105 | // This test applies only to Treble devices but this device isn't one |
| 106 | return; |
| 107 | } |
Jaekyun Seok | 64495e1 | 2018-01-30 17:08:54 +0900 | [diff] [blame] | 108 | if ((compatiblePropertyOnly) && (!isCompatiblePropertyEnforcedDevice())) { |
| 109 | // This test applies only to devices on which compatible property is enforced but this |
| 110 | // device isn't one |
| 111 | return; |
| 112 | } |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 113 | |
Tri Vo | a2631da | 2018-04-12 14:54:19 -0700 | [diff] [blame] | 114 | // If vendor sepolicy version is behind platform's, only test against platform policy. |
| 115 | File policyFile = |
| 116 | (mVendorSepolicyVersion < P_SEPOLICY_VERSION) ? |
| 117 | deviceSystemPolicyFile : |
| 118 | devicePolicyFile; |
| 119 | |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 120 | /* run sepolicy-analyze neverallow check on policy file using given neverallow rules */ |
| 121 | ProcessBuilder pb = new ProcessBuilder(sepolicyAnalyze.getAbsolutePath(), |
Tri Vo | a2631da | 2018-04-12 14:54:19 -0700 | [diff] [blame] | 122 | policyFile.getAbsolutePath(), "neverallow", "-w", "-n", |
dcashman | b34ae0b | 2014-10-24 16:16:30 -0700 | [diff] [blame] | 123 | neverallowRule); |
| 124 | pb.redirectOutput(ProcessBuilder.Redirect.PIPE); |
| 125 | pb.redirectErrorStream(true); |
| 126 | Process p = pb.start(); |
| 127 | p.waitFor(); |
| 128 | BufferedReader result = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 129 | String line; |
| 130 | StringBuilder errorString = new StringBuilder(); |
| 131 | while ((line = result.readLine()) != null) { |
| 132 | errorString.append(line); |
| 133 | errorString.append("\\n"); |
| 134 | } |
| 135 | assertTrue("The following errors were encountered when validating the SELinux" |
| 136 | + "neverallow rule:\\n" + neverallowRule + "\\n" + errorString, |
| 137 | errorString.length() == 0); |
| 138 | } |
| 139 | """ |