blob: 3607e5786ee20dc4cf0907998055d9eac59c8e95 [file] [log] [blame]
dcashman9b615752015-01-07 14:23:11 -08001#!/usr/bin/env python
dcashmanb34ae0b2014-10-24 16:16:30 -07002
3src_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
19package android.cts.security;
20
Changfei Chen178b43b2016-12-05 18:13:06 -080021import android.platform.test.annotations.RestrictedBuildTest;
Aaron Holdend16ae8f2016-11-22 18:44:36 -080022import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
dcashmanb34ae0b2014-10-24 16:16:30 -070023import com.android.tradefed.build.IBuildInfo;
24import com.android.tradefed.device.ITestDevice;
25import com.android.tradefed.testtype.DeviceTestCase;
26import com.android.tradefed.testtype.IBuildReceiver;
dcashman4371f002016-03-29 10:42:03 -070027import com.android.tradefed.testtype.IDeviceTest;
dcashmanb34ae0b2014-10-24 16:16:30 -070028
29import java.io.BufferedReader;
30import java.io.File;
dcashmanb34ae0b2014-10-24 16:16:30 -070031import java.io.InputStream;
32import java.io.InputStreamReader;
dcashmanb34ae0b2014-10-24 16:16:30 -070033
34/**
35 * Neverallow Rules SELinux tests.
36 */
dcashman4371f002016-03-29 10:42:03 -070037public class SELinuxNeverallowRulesTest extends DeviceTestCase implements IBuildReceiver, IDeviceTest {
Tri Voa2631da2018-04-12 14:54:19 -070038 private static final int P_SEPOLICY_VERSION = 28;
dcashmanb34ae0b2014-10-24 16:16:30 -070039 private File sepolicyAnalyze;
40 private File devicePolicyFile;
Tri Voa2631da2018-04-12 14:54:19 -070041 private File deviceSystemPolicyFile;
dcashmanb34ae0b2014-10-24 16:16:30 -070042
dcashman9cf20df2016-04-01 11:32:35 -070043 private IBuildInfo mBuild;
Tri Voa2631da2018-04-12 14:54:19 -070044 private int mVendorSepolicyVersion = -1;
dcashman9cf20df2016-04-01 11:32:35 -070045
dcashmanb34ae0b2014-10-24 16:16:30 -070046 /**
47 * A reference to the device under test.
48 */
49 private ITestDevice mDevice;
50
dcashman4371f002016-03-29 10:42:03 -070051 /**
52 * {@inheritDoc}
53 */
54 @Override
55 public void setBuild(IBuildInfo build) {
dcashman9cf20df2016-04-01 11:32:35 -070056 mBuild = build;
dcashmanb34ae0b2014-10-24 16:16:30 -070057 }
58
dcashman4371f002016-03-29 10:42:03 -070059 /**
60 * {@inheritDoc}
61 */
62 @Override
63 public void setDevice(ITestDevice device) {
64 super.setDevice(device);
65 mDevice = device;
66 }
dcashmanb34ae0b2014-10-24 16:16:30 -070067 @Override
68 protected void setUp() throws Exception {
69 super.setUp();
Aaron Holdend16ae8f2016-11-22 18:44:36 -080070 CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(mBuild);
71 sepolicyAnalyze = buildHelper.getTestFile("sepolicy-analyze");
dcashmanb34ae0b2014-10-24 16:16:30 -070072 sepolicyAnalyze.setExecutable(true);
73
Alex Klyubine91509c2017-04-14 11:17:19 -070074 devicePolicyFile = android.security.cts.SELinuxHostTest.getDevicePolicyFile(mDevice);
Tri Voa2631da2018-04-12 14:54:19 -070075 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 }
dcashmanb34ae0b2014-10-24 16:16:30 -070083 }
Alex Klyubin9dd67db2017-04-06 20:14:43 -070084
85 private boolean isFullTrebleDevice() throws Exception {
86 return android.security.cts.SELinuxHostTest.isFullTrebleDevice(mDevice);
87 }
Jaekyun Seok64495e12018-01-30 17:08:54 +090088
89 private boolean isCompatiblePropertyEnforcedDevice() throws Exception {
90 return android.security.cts.SELinuxHostTest.isCompatiblePropertyEnforcedDevice(mDevice);
91 }
dcashmanb34ae0b2014-10-24 16:16:30 -070092"""
93src_body = ""
94src_footer = """}
95"""
96
97src_method = """
Changfei Chen178b43b2016-12-05 18:13:06 -080098 @RestrictedBuildTest
dcashmanb34ae0b2014-10-24 16:16:30 -070099 public void testNeverallowRules() throws Exception {
100 String neverallowRule = "$NEVERALLOW_RULE_HERE$";
Alex Klyubin9dd67db2017-04-06 20:14:43 -0700101 boolean fullTrebleOnly = $FULL_TREBLE_ONLY_BOOL_HERE$;
Jaekyun Seok64495e12018-01-30 17:08:54 +0900102 boolean compatiblePropertyOnly = $COMPATIBLE_PROPERTY_ONLY_BOOL_HERE$;
Alex Klyubin9dd67db2017-04-06 20:14:43 -0700103
104 if ((fullTrebleOnly) && (!isFullTrebleDevice())) {
105 // This test applies only to Treble devices but this device isn't one
106 return;
107 }
Jaekyun Seok64495e12018-01-30 17:08:54 +0900108 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 }
dcashmanb34ae0b2014-10-24 16:16:30 -0700113
Tri Voa2631da2018-04-12 14:54:19 -0700114 // 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
dcashmanb34ae0b2014-10-24 16:16:30 -0700120 /* run sepolicy-analyze neverallow check on policy file using given neverallow rules */
121 ProcessBuilder pb = new ProcessBuilder(sepolicyAnalyze.getAbsolutePath(),
Tri Voa2631da2018-04-12 14:54:19 -0700122 policyFile.getAbsolutePath(), "neverallow", "-w", "-n",
dcashmanb34ae0b2014-10-24 16:16:30 -0700123 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"""