1 /*
2  * Copyright (C) 2011 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 android.os.cts;
18 
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.util.Scanner;
22 
23 import junit.framework.TestCase;
24 
25 /**
26  * {@link TestCase} that checks that the NX (No Execute) feature is enabled. This feature makes it
27  * harder to perform attacks against Android by marking certain data blocks as non-executable.
28  */
29 public class NoExecutePermissionTest extends TestCase {
30 
31     static {
32         System.loadLibrary("ctsos_jni");
33     }
34 
testNoExecuteStack()35     public void testNoExecuteStack() {
36         if (!cpuHasNxSupport()) {
37             return;
38         }
39         assertFalse(isStackExecutable());
40     }
41 
testNoExecuteHeap()42     public void testNoExecuteHeap() {
43         if (!cpuHasNxSupport()) {
44             return;
45         }
46         assertFalse(isHeapExecutable());
47     }
48 
testExecuteCode()49     public void testExecuteCode() {
50         assertTrue(isMyCodeExecutable());
51     }
52 
cpuHasNxSupport()53     private static boolean cpuHasNxSupport() {
54         if (CpuFeatures.isArmCpu() && !CpuFeatures.isArm7Compatible()) {
55             // ARM processors before v7 do not have NX support.
56             // http://code.google.com/p/android/issues/detail?id=17328
57             return false;
58         }
59         if (CpuFeatures.isMipsCpu()) {
60             // MIPS processors do not have NX support.
61             return false;
62         }
63 
64         // TODO: handle other processors.  For now, assume those processors
65         // have NX support.
66         return true;
67     }
68 
isStackExecutable()69     private static native boolean isStackExecutable();
isHeapExecutable()70     private static native boolean isHeapExecutable();
isMyCodeExecutable()71     private static native boolean isMyCodeExecutable();
72 }
73