Skip to content

Commit

Permalink
Added FD reopener
Browse files Browse the repository at this point in the history
  • Loading branch information
snake-4 committed May 5, 2024
1 parent a7dd49f commit 6530a5a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "module/jni/system_properties"]
path = module/jni/system_properties
url = https://github.com/topjohnwu/system_properties
[submodule "module/jni/aosp_fd_utils"]
path = module/jni/aosp_fd_utils
url = https://github.com/snake-4/aosp_fd_utils.git
7 changes: 4 additions & 3 deletions module/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/elfio
LOCAL_MODULE := zygisk
LOCAL_SRC_FILES := utils.cpp map_parser.cpp mountinfo_parser.cpp modules.cpp main.cpp
LOCAL_STATIC_LIBRARIES := libcxx libsystemproperties
LOCAL_SRC_FILES := fd_reopener.cpp utils.cpp map_parser.cpp mountinfo_parser.cpp modules.cpp main.cpp
LOCAL_STATIC_LIBRARIES := libcxx libsystemproperties libfdutils
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

include jni/libcxx/Android.mk
include jni/system_properties/Android.mk
include jni/system_properties/Android.mk
include jni/aosp_fd_utils/Android.mk
1 change: 1 addition & 0 deletions module/jni/aosp_fd_utils
Submodule aosp_fd_utils added at 303bcb
33 changes: 33 additions & 0 deletions module/jni/fd_reopener.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "fd_reopener.hpp"
#include <fd_utils.h>
#include <string>
#include <utility>
#include "logging.hpp"

FDReopener::ScopedRegularReopener::ScopedRegularReopener()
{
auto pFdSet = GetOpenFds([](const std::string &error)
{ LOGE("GetOpenFds: %s", error.c_str()); });
if (pFdSet)
{
for (const auto &fd : *pFdSet)
{
auto pFDI = FileDescriptorInfo::CreateFromFd(fd, [fd](const std::string &error)
{ LOGE("CreateFromFd(%d): %s", fd, error.c_str()); });

// Only process regular files that are not memfds
if (pFDI && !pFDI->is_sock && !pFDI->file_path.starts_with("/memfd:"))
fdi_vector.emplace_back(std::move(pFDI));
}
}
}

FDReopener::ScopedRegularReopener::~ScopedRegularReopener()
{
for (const auto &pFDI : fdi_vector)
{
LOGD("Reopening FD %d with %s", pFDI->fd, pFDI->file_path.c_str());
pFDI->ReopenOrDetach([fd = pFDI->fd](const std::string &error)
{ LOGE("ReopenOrDetach(%d): %s", fd, error.c_str()); });
}
}
19 changes: 19 additions & 0 deletions module/jni/include/fd_reopener.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "fd_utils.h"
#include <vector>
#include <memory>

namespace FDReopener
{
class ScopedRegularReopener
{
public:
ScopedRegularReopener();
~ScopedRegularReopener();

private:
ScopedRegularReopener(const ScopedRegularReopener &);
void operator=(const ScopedRegularReopener &);
std::vector<std::unique_ptr<FileDescriptorInfo>> fdi_vector;
};
}
5 changes: 5 additions & 0 deletions module/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "logging.hpp"
#include "utils.hpp"
#include "modules.hpp"
#include "fd_reopener.hpp"

using zygisk::Api;
using zygisk::AppSpecializeArgs;
Expand Down Expand Up @@ -46,6 +47,8 @@ DCL_HOOK_FUNC(static int, unshare, int flags)
/*
* The reason why we hook setresuid is because so far it has been unconditionally called
* and we still have CAP_SYS_ADMIN during this call.
* Also, KSU hooks setuid and unmounts some overlays
* so we have to run our code before the syscall.
*/
DCL_HOOK_FUNC(static int, setresuid, uid_t ruid, uid_t euid, uid_t suid)
{
Expand Down Expand Up @@ -97,6 +100,8 @@ class ZygiskModule : public zygisk::ModuleBase

callbackFunction = [fd = companionFd]()
{
FDReopener::ScopedRegularReopener srr;

bool result = false;
if (fd != -1)
{
Expand Down

0 comments on commit 6530a5a

Please sign in to comment.