Skip to content

Commit c07fca3

Browse files
Add JNI bindings for some of the libselinux interfaces.
Change-Id: Ifcc68cb06f9f56a04f3bc64dd9906a9436fabc88
1 parent 8ea93aa commit c07fca3

File tree

5 files changed

+662
-1
lines changed

5 files changed

+662
-1
lines changed

core/java/android/os/SELinux.java

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package android.os;
2+
3+
import java.io.FileDescriptor;
4+
5+
/**
6+
* This class provides access to the centralized jni bindings for
7+
* SELinux interaction.
8+
* {@hide}
9+
*/
10+
public class SELinux {
11+
12+
/**
13+
* Determine whether SELinux is disabled or enabled.
14+
* @return a boolean indicating whether SELinux is enabled.
15+
*/
16+
public static final native boolean isSELinuxEnabled();
17+
18+
/**
19+
* Determine whether SELinux is permissive or enforcing.
20+
* @return a boolean indicating whether SELinux is enforcing.
21+
*/
22+
public static final native boolean isSELinuxEnforced();
23+
24+
/**
25+
* Set whether SELinux is permissive or enforcing.
26+
* @param boolean representing whether to set SELinux to enforcing
27+
* @return a boolean representing whether the desired mode was set
28+
*/
29+
public static final native boolean setSELinuxEnforce(boolean value);
30+
31+
/**
32+
* Sets the security context for newly created file objects.
33+
* @param context a security context given as a String.
34+
* @return a boolean indicating whether the operation succeeded.
35+
*/
36+
public static final native boolean setFSCreateContext(String context);
37+
38+
/**
39+
* Change the security context of an existing file object.
40+
* @param path representing the path of file object to relabel.
41+
* @param con new security context given as a String.
42+
* @return a boolean indicating whether the operation succeeded.
43+
*/
44+
public static final native boolean setFileContext(String path, String context);
45+
46+
/**
47+
* Get the security context of a file object.
48+
* @param path the pathname of the file object.
49+
* @return a security context given as a String.
50+
*/
51+
public static final native String getFileContext(String path);
52+
53+
/**
54+
* Get the security context of a peer socket.
55+
* @param fd FileDescriptor class of the peer socket.
56+
* @return a String representing the peer socket security context.
57+
*/
58+
public static final native String getPeerContext(FileDescriptor fd);
59+
60+
/**
61+
* Gets the security context of the current process.
62+
* @return a String representing the security context of the current process.
63+
*/
64+
public static final native String getContext();
65+
66+
/**
67+
* Gets the security context of a given process id.
68+
* Use of this function is discouraged for Binder transactions.
69+
* Use Binder.getCallingSecctx() instead.
70+
* @param pid an int representing the process id to check.
71+
* @return a String representing the security context of the given pid.
72+
*/
73+
public static final native String getPidContext(int pid);
74+
75+
/**
76+
* Gets a list of the SELinux boolean names.
77+
* @return an array of strings containing the SELinux boolean names.
78+
*/
79+
public static final native String[] getBooleanNames();
80+
81+
/**
82+
* Gets the value for the given SELinux boolean name.
83+
* @param String The name of the SELinux boolean.
84+
* @return a boolean indicating whether the SELinux boolean is set.
85+
*/
86+
public static final native boolean getBooleanValue(String name);
87+
88+
/**
89+
* Sets the value for the given SELinux boolean name.
90+
* @param String The name of the SELinux boolean.
91+
* @param Boolean The new value of the SELinux boolean.
92+
* @return a boolean indicating whether or not the operation succeeded.
93+
*/
94+
public static final native boolean setBooleanValue(String name, boolean value);
95+
96+
/**
97+
* Check permissions between two security contexts.
98+
* @param scon The source or subject security context.
99+
* @param tcon The target or object security context.
100+
* @param tclass The object security class name.
101+
* @param perm The permission name.
102+
* @return a boolean indicating whether permission was granted.
103+
*/
104+
public static final native boolean checkSELinuxAccess(String scon, String tcon, String tclass, String perm);
105+
}

core/jni/Android.mk

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ LOCAL_SRC_FILES:= \
6666
android_os_MessageQueue.cpp \
6767
android_os_ParcelFileDescriptor.cpp \
6868
android_os_Power.cpp \
69+
android_os_SELinux.cpp \
6970
android_os_StatFs.cpp \
7071
android_os_SystemClock.cpp \
7172
android_os_SystemProperties.cpp \
@@ -216,7 +217,13 @@ LOCAL_SHARED_LIBRARIES := \
216217
libnfc_ndef \
217218
libusbhost \
218219
libharfbuzz \
219-
libz \
220+
libz
221+
222+
ifeq ($(HAVE_SELINUX),true)
223+
LOCAL_C_INCLUDES += external/libselinux/include
224+
LOCAL_SHARED_LIBRARIES += libselinux
225+
LOCAL_CFLAGS += -DHAVE_SELINUX
226+
endif # HAVE_SELINUX
220227

221228
ifeq ($(USE_OPENGL_RENDERER),true)
222229
LOCAL_SHARED_LIBRARIES += libhwui

core/jni/AndroidRuntime.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ extern int register_android_os_Debug(JNIEnv* env);
135135
extern int register_android_os_MessageQueue(JNIEnv* env);
136136
extern int register_android_os_ParcelFileDescriptor(JNIEnv *env);
137137
extern int register_android_os_Power(JNIEnv *env);
138+
extern int register_android_os_SELinux(JNIEnv* env);
138139
extern int register_android_os_StatFs(JNIEnv *env);
139140
extern int register_android_os_SystemProperties(JNIEnv *env);
140141
extern int register_android_os_SystemClock(JNIEnv* env);
@@ -1153,6 +1154,7 @@ static const RegJNIRec gRegJNI[] = {
11531154
REG_JNI(register_android_os_MessageQueue),
11541155
REG_JNI(register_android_os_ParcelFileDescriptor),
11551156
REG_JNI(register_android_os_Power),
1157+
REG_JNI(register_android_os_SELinux),
11561158
REG_JNI(register_android_os_StatFs),
11571159
REG_JNI(register_android_os_UEventObserver),
11581160
REG_JNI(register_android_net_LocalSocketImpl),

0 commit comments

Comments
 (0)