|
| 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 | + |
1 | 17 | package android.os; |
2 | 18 |
|
| 19 | +import android.util.Slog; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.File; |
3 | 23 | import java.io.FileDescriptor; |
4 | 24 |
|
5 | 25 | /** |
|
9 | 29 | */ |
10 | 30 | public class SELinux { |
11 | 31 |
|
| 32 | + private static final String TAG = "SELinux"; |
| 33 | + |
12 | 34 | /** |
13 | 35 | * Determine whether SELinux is disabled or enabled. |
14 | 36 | * @return a boolean indicating whether SELinux is enabled. |
@@ -102,4 +124,53 @@ public class SELinux { |
102 | 124 | * @return a boolean indicating whether permission was granted. |
103 | 125 | */ |
104 | 126 | public static final native boolean checkSELinuxAccess(String scon, String tcon, String tclass, String perm); |
| 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(); } |
| 141 | + return native_restorecon(pathname); |
| 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 | + */ |
| 154 | + private static native boolean native_restorecon(String pathname); |
| 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 { |
| 169 | + return native_restorecon(file.getCanonicalPath()); |
| 170 | + } catch (IOException e) { |
| 171 | + Slog.e(TAG, "Error getting canonical path. Restorecon failed for " + |
| 172 | + file.getPath(), e); |
| 173 | + return false; |
| 174 | + } |
| 175 | + } |
105 | 176 | } |
0 commit comments