Skip to content

Commit

Permalink
fix: Incorrect owner and group
Browse files Browse the repository at this point in the history
Change-Id: I761f159a4d317d778c39e7e737054e10832d914d
  • Loading branch information
XayahSuSuSu committed Sep 28, 2024
1 parent bfa2c0c commit f1bcc67
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface IRemoteRootService {
long calculateSize(String path);
void clearEmptyDirectoriesRecursively(String path);
void setAllPermissions(String src);
int[] getUidGid(String path);

ParcelFileDescriptor getInstalledPackagesAsUser(int flags, int userId);
PackageInfo getPackageInfoAsUser(String packageName, int flags, int userId);
Expand Down
21 changes: 20 additions & 1 deletion source/core/rootservice/src/main/cpp/nativelib.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <jni.h>
#include <string>
#include <ftw.h>
#include <sys/stat.h>
#include <climits>

namespace NativeNS {
thread_local size_t total_size{0};
Expand All @@ -17,4 +19,21 @@ Java_com_xayah_core_rootservice_util_NativeLib_calculateSize(JNIEnv *env, jobjec
const char *p_path = env->GetStringUTFChars(path, JNI_FALSE);
ftw(p_path, &NativeNS::on_walking, 1024);
return NativeNS::total_size;
}
}

extern "C" JNIEXPORT jintArray JNICALL
Java_com_xayah_core_rootservice_util_NativeLib_getUidGid(JNIEnv *env, jobject, jstring path) {
struct stat file_stat{};
jintArray result = env->NewIntArray(2); // result[0] - uid, result[1] - gid
jint *p_result = env->GetIntArrayElements(result, nullptr);
const char *p_path = env->GetStringUTFChars(path, JNI_FALSE);
if (stat(p_path, &file_stat) == -1) {
p_result[0] = UINT_MAX;
p_result[1] = UINT_MAX;
} else {
p_result[0] = (int) file_stat.st_uid;
p_result[1] = (int) file_stat.st_gid;
}
env->ReleaseIntArrayElements(result, p_result, 0);
return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ internal class RemoteRootServiceImpl : IRemoteRootService.Stub() {

override fun setAllPermissions(src: String): Unit = synchronized(lock) { File(src).setAllPermissions() }

override fun getUidGid(path: String): IntArray = synchronized(lock) {
NativeLib.getUidGid(path)
}

/**
* AIDL limits transaction to 1M which means it may throw [android.os.TransactionTooLargeException]
* when the package list is too large. So we just make it parcelable and write into tmp file to avoid that.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ class RemoteRootService(private val context: Context) {

suspend fun setAllPermissions(src: String) = runCatching { getService().setAllPermissions(src) }.onFailure(onFailure)

/**
* Get the uid and gid of the file/directory
*
* @param path
* @return Uid to Gid
*/
suspend fun getUidGid(path: String): Pair<UInt, UInt> = runCatching { getService().getUidGid(path).let { it[0].toUInt() to it[1].toUInt() } }.onFailure(onFailure).getOrElse { UInt.MAX_VALUE to UInt.MAX_VALUE }

suspend fun getInstalledPackagesAsUser(flags: Int, userId: Int): List<PackageInfo> = runCatching {
val pfd = getService().getInstalledPackagesAsUser(flags, userId)
val packages = mutableListOf<PackageInfo>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.xayah.core.rootservice.util

object NativeLib {
external fun calculateSize(path: String): Long
external fun getUidGid(path: String): IntArray
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import com.xayah.core.common.util.toLineString
import com.xayah.core.data.repository.CloudRepository
import com.xayah.core.data.repository.PackageRepository
import com.xayah.core.data.util.srcDir
import com.xayah.core.database.dao.TaskDao
import com.xayah.core.datastore.readCleanRestoring
import com.xayah.core.datastore.readSelectionType
Expand Down Expand Up @@ -306,7 +307,12 @@ class PackagesRestoreUtil @Inject constructor(

// Restore SELinux context.
if (uid != -1) {
SELinux.chown(uid = uid, path = dst).also { result ->
var gid: UInt = uid.toUInt()
if (dataType == DataType.PACKAGE_DATA || dataType == DataType.PACKAGE_OBB || dataType == DataType.PACKAGE_MEDIA) {
val (_, pathGid) = rootService.getUidGid(dataType.srcDir(userId))
gid = pathGid
}
SELinux.chown(uid = uid.toUInt(), gid = gid, path = dst).also { result ->
isSuccess = result.isSuccess
out.addAll(result.out)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class PathUtil @Inject constructor(
suspend fun setFilesDirSELinux(context: Context) = SELinux.getContext(path = context.filesDir()).also { result ->
val pathContext = if (result.isSuccess) result.outString else ""
SELinux.chcon(context = pathContext, path = context.filesDir())
SELinux.chown(uid = context.applicationInfo.uid, path = context.filesDir())
val uidGid = context.applicationInfo.uid.toUInt()
SELinux.chown(uid = uidGid, gid = uidGid, path = context.filesDir())
}

fun getSsaidPath(userId: Int) = "/data/system/users/$userId/settings_ssaid.xml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ object SELinux {
)
}

suspend fun chown(uid: Int, path: String): ShellResult = run {
suspend fun chown(uid: UInt, gid: UInt, path: String): ShellResult = run {
// chown -hR "$uid:$uid" "$path/"
execute(
"chown",
"-hR",
"$QUOTE$uid:$uid$QUOTE",
"$QUOTE$uid:$gid$QUOTE",
"$QUOTE$path/$QUOTE",
)
}
Expand Down

0 comments on commit f1bcc67

Please sign in to comment.