blob: f64a81177ce2b44b9a0ba24fbb7c58b05c197344 [file] [log] [blame]
rpcraig554cb0c2012-07-05 06:41:43 -04001/*
2 * Copyright (C) 2012 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
Stephen Smalleyc07fca32012-01-13 08:31:39 -050017package android.os;
18
Artur Satayevafdb23a2019-12-10 17:47:53 +000019import android.compat.annotation.UnsupportedAppUsage;
rpcraig554cb0c2012-07-05 06:41:43 -040020import android.util.Slog;
21
rpcraig554cb0c2012-07-05 06:41:43 -040022import java.io.File;
Stephen Smalleyc07fca32012-01-13 08:31:39 -050023import java.io.FileDescriptor;
Makoto Onuki9700015b2018-07-27 17:06:30 -070024import java.io.IOException;
Stephen Smalleyc07fca32012-01-13 08:31:39 -050025
26/**
27 * This class provides access to the centralized jni bindings for
28 * SELinux interaction.
29 * {@hide}
30 */
31public class SELinux {
rpcraig554cb0c2012-07-05 06:41:43 -040032 private static final String TAG = "SELinux";
33
Nick Kralevichd88acc92019-04-04 15:54:58 -070034 /** Keep in sync with ./external/selinux/libselinux/include/selinux/android.h */
Jeff Sharkeyd7460572014-07-06 20:44:55 -070035 private static final int SELINUX_ANDROID_RESTORECON_NOCHANGE = 1;
36 private static final int SELINUX_ANDROID_RESTORECON_VERBOSE = 2;
37 private static final int SELINUX_ANDROID_RESTORECON_RECURSE = 4;
38 private static final int SELINUX_ANDROID_RESTORECON_FORCE = 8;
39 private static final int SELINUX_ANDROID_RESTORECON_DATADATA = 16;
Nick Kralevichd88acc92019-04-04 15:54:58 -070040 private static final int SELINUX_ANDROID_RESTORECON_SKIPCE = 32;
41 private static final int SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS = 64;
42 private static final int SELINUX_ANDROID_RESTORECON_SKIP_SEHASH = 128;
Jeff Sharkeyd7460572014-07-06 20:44:55 -070043
Stephen Smalleyc07fca32012-01-13 08:31:39 -050044 /**
Florian Mayerd4db9972019-03-21 13:57:42 +000045 * Get context associated with path by file_contexts.
46 * @param path path to the regular file to get the security context for.
47 * @return a String representing the security context or null on failure.
48 */
49 public static final native String fileSelabelLookup(String path);
50
51 /**
Stephen Smalleyc07fca32012-01-13 08:31:39 -050052 * Determine whether SELinux is disabled or enabled.
53 * @return a boolean indicating whether SELinux is enabled.
54 */
Andrei Onea24ec3212019-03-15 17:35:05 +000055 @UnsupportedAppUsage
Stephen Smalleyc07fca32012-01-13 08:31:39 -050056 public static final native boolean isSELinuxEnabled();
57
58 /**
59 * Determine whether SELinux is permissive or enforcing.
60 * @return a boolean indicating whether SELinux is enforcing.
61 */
Andrei Onea24ec3212019-03-15 17:35:05 +000062 @UnsupportedAppUsage
Stephen Smalleyc07fca32012-01-13 08:31:39 -050063 public static final native boolean isSELinuxEnforced();
64
65 /**
Stephen Smalleyc07fca32012-01-13 08:31:39 -050066 * Sets the security context for newly created file objects.
67 * @param context a security context given as a String.
68 * @return a boolean indicating whether the operation succeeded.
69 */
70 public static final native boolean setFSCreateContext(String context);
71
72 /**
73 * Change the security context of an existing file object.
74 * @param path representing the path of file object to relabel.
Richard Haines66d53692013-05-22 13:25:15 +010075 * @param context new security context given as a String.
Stephen Smalleyc07fca32012-01-13 08:31:39 -050076 * @return a boolean indicating whether the operation succeeded.
77 */
78 public static final native boolean setFileContext(String path, String context);
79
80 /**
81 * Get the security context of a file object.
82 * @param path the pathname of the file object.
83 * @return a security context given as a String.
84 */
David Brazdil576da052019-01-28 12:43:28 +000085 @UnsupportedAppUsage
Stephen Smalleyc07fca32012-01-13 08:31:39 -050086 public static final native String getFileContext(String path);
87
88 /**
89 * Get the security context of a peer socket.
90 * @param fd FileDescriptor class of the peer socket.
91 * @return a String representing the peer socket security context.
92 */
93 public static final native String getPeerContext(FileDescriptor fd);
94
95 /**
Makoto Onuki9700015b2018-07-27 17:06:30 -070096 * Get the security context of a file descriptor of a file.
97 * @param fd FileDescriptor of a file.
98 * @return a String representing the file descriptor security context.
99 */
100 public static final native String getFileContext(FileDescriptor fd);
101
102 /**
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500103 * Gets the security context of the current process.
104 * @return a String representing the security context of the current process.
105 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000106 @UnsupportedAppUsage
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500107 public static final native String getContext();
108
109 /**
110 * Gets the security context of a given process id.
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500111 * @param pid an int representing the process id to check.
112 * @return a String representing the security context of the given pid.
113 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000114 @UnsupportedAppUsage
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500115 public static final native String getPidContext(int pid);
116
117 /**
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500118 * Check permissions between two security contexts.
119 * @param scon The source or subject security context.
120 * @param tcon The target or object security context.
121 * @param tclass The object security class name.
122 * @param perm The permission name.
123 * @return a boolean indicating whether permission was granted.
124 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000125 @UnsupportedAppUsage
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500126 public static final native boolean checkSELinuxAccess(String scon, String tcon, String tclass, String perm);
rpcraig554cb0c2012-07-05 06:41:43 -0400127
128 /**
129 * Restores a file to its default SELinux security context.
130 * If the system is not compiled with SELinux, then {@code true}
131 * is automatically returned.
132 * If SELinux is compiled in, but disabled, then {@code true} is
133 * returned.
134 *
135 * @param pathname The pathname of the file to be relabeled.
136 * @return a boolean indicating whether the relabeling succeeded.
137 * @exception NullPointerException if the pathname is a null object.
138 */
139 public static boolean restorecon(String pathname) throws NullPointerException {
140 if (pathname == null) { throw new NullPointerException(); }
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700141 return native_restorecon(pathname, 0);
rpcraig554cb0c2012-07-05 06:41:43 -0400142 }
143
144 /**
145 * Restores a file to its default SELinux security context.
146 * If the system is not compiled with SELinux, then {@code true}
147 * is automatically returned.
148 * If SELinux is compiled in, but disabled, then {@code true} is
149 * returned.
150 *
151 * @param pathname The pathname of the file to be relabeled.
152 * @return a boolean indicating whether the relabeling succeeded.
153 */
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700154 private static native boolean native_restorecon(String pathname, int flags);
rpcraig554cb0c2012-07-05 06:41:43 -0400155
156 /**
157 * Restores a file to its default SELinux security context.
158 * If the system is not compiled with SELinux, then {@code true}
159 * is automatically returned.
160 * If SELinux is compiled in, but disabled, then {@code true} is
161 * returned.
162 *
163 * @param file The File object representing the path to be relabeled.
164 * @return a boolean indicating whether the relabeling succeeded.
165 * @exception NullPointerException if the file is a null object.
166 */
167 public static boolean restorecon(File file) throws NullPointerException {
168 try {
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700169 return native_restorecon(file.getCanonicalPath(), 0);
rpcraig554cb0c2012-07-05 06:41:43 -0400170 } catch (IOException e) {
171 Slog.e(TAG, "Error getting canonical path. Restorecon failed for " +
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700172 file.getPath(), e);
rpcraig554cb0c2012-07-05 06:41:43 -0400173 return false;
174 }
175 }
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700176
177 /**
178 * Recursively restores all files under the given path to their default
179 * SELinux security context. If the system is not compiled with SELinux,
180 * then {@code true} is automatically returned. If SELinux is compiled in,
181 * but disabled, then {@code true} is returned.
182 *
183 * @return a boolean indicating whether the relabeling succeeded.
184 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000185 @UnsupportedAppUsage
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700186 public static boolean restoreconRecursive(File file) {
187 try {
Nick Kralevichd88acc92019-04-04 15:54:58 -0700188 return native_restorecon(file.getCanonicalPath(),
189 SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH);
Jeff Sharkeyd7460572014-07-06 20:44:55 -0700190 } catch (IOException e) {
191 Slog.e(TAG, "Error getting canonical path. Restorecon failed for " +
192 file.getPath(), e);
193 return false;
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700194 }
Jeff Sharkey57dcf5b2014-06-18 17:46:05 -0700195 }
Stephen Smalleyc07fca32012-01-13 08:31:39 -0500196}