rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 1 | /* |
| 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 Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 17 | package android.os; |
| 18 | |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 20 | import android.util.Slog; |
| 21 | |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 22 | import java.io.File; |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 23 | import java.io.FileDescriptor; |
Makoto Onuki | 9700015b | 2018-07-27 17:06:30 -0700 | [diff] [blame] | 24 | import java.io.IOException; |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * This class provides access to the centralized jni bindings for |
| 28 | * SELinux interaction. |
| 29 | * {@hide} |
| 30 | */ |
| 31 | public class SELinux { |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 32 | private static final String TAG = "SELinux"; |
| 33 | |
Nick Kralevich | d88acc9 | 2019-04-04 15:54:58 -0700 | [diff] [blame] | 34 | /** Keep in sync with ./external/selinux/libselinux/include/selinux/android.h */ |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 35 | 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 Kralevich | d88acc9 | 2019-04-04 15:54:58 -0700 | [diff] [blame] | 40 | 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 Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 43 | |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 44 | /** |
Florian Mayer | d4db997 | 2019-03-21 13:57:42 +0000 | [diff] [blame] | 45 | * 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 Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 52 | * Determine whether SELinux is disabled or enabled. |
| 53 | * @return a boolean indicating whether SELinux is enabled. |
| 54 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 55 | @UnsupportedAppUsage |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 56 | 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 62 | @UnsupportedAppUsage |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 63 | public static final native boolean isSELinuxEnforced(); |
| 64 | |
| 65 | /** |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 66 | * 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 Haines | 66d5369 | 2013-05-22 13:25:15 +0100 | [diff] [blame] | 75 | * @param context new security context given as a String. |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 76 | * @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 Brazdil | 576da05 | 2019-01-28 12:43:28 +0000 | [diff] [blame] | 85 | @UnsupportedAppUsage |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 86 | 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 Onuki | 9700015b | 2018-07-27 17:06:30 -0700 | [diff] [blame] | 96 | * 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 Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 103 | * Gets the security context of the current process. |
| 104 | * @return a String representing the security context of the current process. |
| 105 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 106 | @UnsupportedAppUsage |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 107 | public static final native String getContext(); |
| 108 | |
| 109 | /** |
| 110 | * Gets the security context of a given process id. |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 111 | * @param pid an int representing the process id to check. |
| 112 | * @return a String representing the security context of the given pid. |
| 113 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 114 | @UnsupportedAppUsage |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 115 | public static final native String getPidContext(int pid); |
| 116 | |
| 117 | /** |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 118 | * 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 125 | @UnsupportedAppUsage |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 126 | public static final native boolean checkSELinuxAccess(String scon, String tcon, String tclass, String perm); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 127 | |
| 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 Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 141 | return native_restorecon(pathname, 0); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 142 | } |
| 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 Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 154 | private static native boolean native_restorecon(String pathname, int flags); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 155 | |
| 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 Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 169 | return native_restorecon(file.getCanonicalPath(), 0); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 170 | } catch (IOException e) { |
| 171 | Slog.e(TAG, "Error getting canonical path. Restorecon failed for " + |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 172 | file.getPath(), e); |
rpcraig | 554cb0c | 2012-07-05 06:41:43 -0400 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | } |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 176 | |
| 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 Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 185 | @UnsupportedAppUsage |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 186 | public static boolean restoreconRecursive(File file) { |
| 187 | try { |
Nick Kralevich | d88acc9 | 2019-04-04 15:54:58 -0700 | [diff] [blame] | 188 | return native_restorecon(file.getCanonicalPath(), |
| 189 | SELINUX_ANDROID_RESTORECON_RECURSE | SELINUX_ANDROID_RESTORECON_SKIP_SEHASH); |
Jeff Sharkey | d746057 | 2014-07-06 20:44:55 -0700 | [diff] [blame] | 190 | } catch (IOException e) { |
| 191 | Slog.e(TAG, "Error getting canonical path. Restorecon failed for " + |
| 192 | file.getPath(), e); |
| 193 | return false; |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 194 | } |
Jeff Sharkey | 57dcf5b | 2014-06-18 17:46:05 -0700 | [diff] [blame] | 195 | } |
Stephen Smalley | c07fca3 | 2012-01-13 08:31:39 -0500 | [diff] [blame] | 196 | } |