Skip to content

Commit a2e8365

Browse files
krutonAndroid Git Automerger
authored andcommitted
am 9b0da58: Merge "Introduce a restorecon JNI binding."
* commit '9b0da58e3a30b760de37138cdd51d20f269c383e': Introduce a restorecon JNI binding.
2 parents 617ccc0 + 9b0da58 commit a2e8365

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

core/java/android/os/SELinux.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
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+
117
package android.os;
218

19+
import android.util.Slog;
20+
21+
import java.io.IOException;
22+
import java.io.File;
323
import java.io.FileDescriptor;
424

525
/**
@@ -9,6 +29,8 @@
929
*/
1030
public class SELinux {
1131

32+
private static final String TAG = "SELinux";
33+
1234
/**
1335
* Determine whether SELinux is disabled or enabled.
1436
* @return a boolean indicating whether SELinux is enabled.
@@ -102,4 +124,53 @@ public class SELinux {
102124
* @return a boolean indicating whether permission was granted.
103125
*/
104126
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+
}
105176
}

core/jni/android_os_SELinux.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
#define LOG_TAG "SELinuxJNI"
218
#include <utils/Log.h>
319

@@ -6,6 +22,7 @@
622
#include "android_runtime/AndroidRuntime.h"
723
#ifdef HAVE_SELINUX
824
#include "selinux/selinux.h"
25+
#include "selinux/android.h"
926
#endif
1027
#include <errno.h>
1128

@@ -457,6 +474,27 @@ namespace android {
457474
#endif
458475
}
459476

477+
/*
478+
* Function: native_restorecon
479+
* Purpose: restore default SELinux security context
480+
* Parameters: pathname: the pathname for the file to be relabeled
481+
* Returns: boolean: (true) file label successfully restored, (false) otherwise
482+
* Exceptions: none
483+
*/
484+
static jboolean native_restorecon(JNIEnv *env, jobject clazz, jstring pathname) {
485+
#ifdef HAVE_SELINUX
486+
if (isSELinuxDisabled)
487+
return true;
488+
489+
const char *file = const_cast<char *>(env->GetStringUTFChars(pathname, NULL));
490+
int ret = selinux_android_restorecon(file);
491+
env->ReleaseStringUTFChars(pathname, file);
492+
return (ret == 0);
493+
#else
494+
return true;
495+
#endif
496+
}
497+
460498
/*
461499
* JNI registration.
462500
*/
@@ -472,6 +510,7 @@ namespace android {
472510
{ "getPidContext" , "(I)Ljava/lang/String;" , (void*)getPidCon },
473511
{ "isSELinuxEnforced" , "()Z" , (void*)isSELinuxEnforced},
474512
{ "isSELinuxEnabled" , "()Z" , (void*)isSELinuxEnabled },
513+
{ "native_restorecon" , "(Ljava/lang/String;)Z" , (void*)native_restorecon},
475514
{ "setBooleanValue" , "(Ljava/lang/String;Z)Z" , (void*)setBooleanValue },
476515
{ "setFileContext" , "(Ljava/lang/String;Ljava/lang/String;)Z" , (void*)setFileCon },
477516
{ "setFSCreateContext" , "(Ljava/lang/String;)Z" , (void*)setFSCreateCon },

services/java/com/android/server/WallpaperManagerService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import android.os.FileObserver;
4646
import android.os.ParcelFileDescriptor;
4747
import android.os.RemoteCallbackList;
48+
import android.os.SELinux;
4849
import android.os.ServiceManager;
4950
import android.os.SystemClock;
5051
import android.os.UserId;
@@ -639,8 +640,12 @@ ParcelFileDescriptor updateWallpaperBitmapLocked(String name, WallpaperData wall
639640
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
640641
-1, -1);
641642
}
642-
ParcelFileDescriptor fd = ParcelFileDescriptor.open(new File(dir, WALLPAPER),
643+
File file = new File(dir, WALLPAPER);
644+
ParcelFileDescriptor fd = ParcelFileDescriptor.open(file,
643645
MODE_CREATE|MODE_READ_WRITE);
646+
if (!SELinux.restorecon(file)) {
647+
return null;
648+
}
644649
wallpaper.name = name;
645650
return fd;
646651
} catch (FileNotFoundException e) {

services/java/com/android/server/pm/PackageManagerService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import android.os.ParcelFileDescriptor;
9797
import android.os.Process;
9898
import android.os.RemoteException;
99+
import android.os.SELinux;
99100
import android.os.ServiceManager;
100101
import android.os.SystemClock;
101102
import android.os.SystemProperties;
@@ -6418,6 +6419,10 @@ boolean doRename(int status, final String pkgName, String oldCodePath) {
64186419
return false;
64196420
}
64206421

6422+
if (!SELinux.restorecon(newCodeFile)) {
6423+
return false;
6424+
}
6425+
64216426
return true;
64226427
}
64236428
}
@@ -7399,6 +7404,9 @@ private File createTempPackageFile(File installDir) {
73997404
FileUtils.setPermissions(
74007405
tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR,
74017406
-1, -1);
7407+
if (!SELinux.restorecon(tmpPackageFile)) {
7408+
return null;
7409+
}
74027410
} catch (IOException e) {
74037411
Slog.e(TAG, "Trouble getting the canoncical path for a temp file.");
74047412
return null;

0 commit comments

Comments
 (0)