Skip to content

Commit 5d3df5e

Browse files
author
Genkzsz11
committed
Remove some USAP commit
Revert "Fix error formatting issues" This reverts commit 3244d59. Revert "Allow app zygote preload to retain files across fork" This reverts commit 44cc0a2. Revert "Don't fork USAPs with open argument buffer" This reverts commit 97d6916.
1 parent 559449e commit 5d3df5e

7 files changed

Lines changed: 54 additions & 154 deletions

File tree

core/java/com/android/internal/os/AppZygoteInit.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ protected void handlePreloadApp(ApplicationInfo appInfo) {
9191
} else {
9292
Constructor<?> ctor = cl.getConstructor();
9393
ZygotePreload preloadObject = (ZygotePreload) ctor.newInstance();
94-
Zygote.markOpenedFilesBeforePreload();
9594
preloadObject.doPreload(appInfo);
96-
Zygote.allowFilesOpenedByPreload();
9795
}
9896
} catch (ReflectiveOperationException e) {
9997
Log.e(TAG, "AppZygote application preload failed for "

core/java/com/android/internal/os/Zygote.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -500,36 +500,6 @@ static void allowAppFilesAcrossFork(ApplicationInfo appInfo) {
500500
}
501501
}
502502

503-
/**
504-
* Scans file descriptors in /proc/self/fd/, stores their metadata from readlink(2)/stat(2) when
505-
* available. Saves this information in a global on native side, to be used by subsequent call
506-
* to allowFilesOpenedByPreload(). Fatally fails if the FDs are of unsupported type and are not
507-
* explicitly allowed. Ignores repeated invocations.
508-
*
509-
* Inspecting the FDs is more permissive than in forkAndSpecialize() because preload is invoked
510-
* earlier and hence needs to allow a few open sockets. The checks in forkAndSpecialize()
511-
* enforce that these sockets are closed when forking.
512-
*/
513-
static void markOpenedFilesBeforePreload() {
514-
nativeMarkOpenedFilesBeforePreload();
515-
}
516-
517-
private static native void nativeMarkOpenedFilesBeforePreload();
518-
519-
/**
520-
* By scanning /proc/self/fd/ determines file descriptor numbers in this process opened since
521-
* the first call to markOpenedFilesBeforePreload(). These FDs are treated as 'owned' by the
522-
* custom preload of the App Zygote - the app is responsible for not sharing data with its other
523-
* processes using these FDs, including by lseek(2). File descriptor types and file names are
524-
* not checked. Changes in FDs recorded by markOpenedFilesBeforePreload() are not expected and
525-
* kill the current process.
526-
*/
527-
static void allowFilesOpenedByPreload() {
528-
nativeAllowFilesOpenedByPreload();
529-
}
530-
531-
private static native void nativeAllowFilesOpenedByPreload();
532-
533503
/**
534504
* Installs a seccomp filter that limits setresuid()/setresgid() to the passed-in range
535505
* @param uidGidMin The smallest allowed uid/gid

core/java/com/android/internal/os/ZygoteConnection.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,8 @@ Runnable processCommand(ZygoteServer zygoteServer, boolean multipleOK) {
150150
return null;
151151
}
152152

153-
if (parsedArgs.mUsapPoolStatusSpecified
154-
|| parsedArgs.mApiBlacklistExemptions != null
155-
|| parsedArgs.mHiddenApiAccessLogSampleRate != -1
156-
|| parsedArgs.mHiddenApiAccessStatslogSampleRate != -1) {
157-
// Handle these once we've released argBuffer, to avoid opening a second one.
153+
if (parsedArgs.mUsapPoolStatusSpecified) {
154+
// Handle this once we've released the argBuffer, to avoid opening a second one.
158155
break;
159156
}
160157

@@ -187,7 +184,17 @@ Runnable processCommand(ZygoteServer zygoteServer, boolean multipleOK) {
187184
return null;
188185
}
189186

187+
if (parsedArgs.mApiBlacklistExemptions != null) {
188+
return handleApiBlacklistExemptions(zygoteServer,
189+
parsedArgs.mApiBlacklistExemptions);
190+
}
190191

192+
if (parsedArgs.mHiddenApiAccessLogSampleRate != -1
193+
|| parsedArgs.mHiddenApiAccessStatslogSampleRate != -1) {
194+
return handleHiddenApiAccessLogSampleRate(zygoteServer,
195+
parsedArgs.mHiddenApiAccessLogSampleRate,
196+
parsedArgs.mHiddenApiAccessStatslogSampleRate);
197+
}
191198

192199
if (parsedArgs.mPermittedCapabilities != 0
193200
|| parsedArgs.mEffectiveCapabilities != 0) {
@@ -309,20 +316,10 @@ Runnable processCommand(ZygoteServer zygoteServer, boolean multipleOK) {
309316
}
310317
}
311318
}
312-
// Handle anything that may need a ZygoteCommandBuffer after we've released ours.
313319
if (parsedArgs.mUsapPoolStatusSpecified) {
320+
// Now that we've released argBuffer:
314321
return handleUsapPoolStatusChange(zygoteServer, parsedArgs.mUsapPoolEnabled);
315322
}
316-
if (parsedArgs.mApiBlacklistExemptions != null) {
317-
return handleApiBlacklistExemptions(zygoteServer,
318-
parsedArgs.mApiBlacklistExemptions);
319-
}
320-
if (parsedArgs.mHiddenApiAccessLogSampleRate != -1
321-
|| parsedArgs.mHiddenApiAccessStatslogSampleRate != -1) {
322-
return handleHiddenApiAccessLogSampleRate(zygoteServer,
323-
parsedArgs.mHiddenApiAccessLogSampleRate,
324-
parsedArgs.mHiddenApiAccessStatslogSampleRate);
325-
}
326323
throw new AssertionError("Shouldn't get here");
327324
}
328325

core/jni/com_android_internal_os_Zygote.cpp

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@
3737
#include <sys/types.h>
3838
#include <dirent.h>
3939

40-
#include <algorithm>
4140
#include <array>
4241
#include <atomic>
4342
#include <functional>
44-
#include <iterator>
4543
#include <list>
4644
#include <optional>
4745
#include <sstream>
@@ -1944,9 +1942,6 @@ void zygote::ZygoteFailure(JNIEnv* env,
19441942
__builtin_unreachable();
19451943
}
19461944

1947-
static std::set<int>* gPreloadFds = nullptr;
1948-
static bool gPreloadFdsExtracted = false;
1949-
19501945
// Utility routine to fork a process from the zygote.
19511946
pid_t zygote::ForkCommon(JNIEnv* env, bool is_system_server,
19521947
const std::vector<int>& fds_to_close,
@@ -1972,12 +1967,9 @@ pid_t zygote::ForkCommon(JNIEnv* env, bool is_system_server,
19721967
__android_log_close();
19731968
AStatsSocket_close();
19741969

1975-
// If this is the first fork for this zygote, create the open FD table,
1976-
// verifying that files are of supported type and allowlisted. Otherwise (not
1977-
// the first fork), check that the open files have not changed. Newly open
1978-
// files are not expected, and will be disallowed in the future. Currently
1979-
// they are allowed if they pass the same checks as in the
1980-
// FileDescriptorTable::Create() above.
1970+
// If this is the first fork for this zygote, create the open FD table. If
1971+
// it isn't, we just need to check whether the list of open files has changed
1972+
// (and it shouldn't in the normal case).
19811973
if (gOpenFdTable == nullptr) {
19821974
gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore, fail_fn);
19831975
} else {
@@ -2074,12 +2066,7 @@ static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
20742066
fds_to_ignore.push_back(gSystemServerSocketFd);
20752067
}
20762068

2077-
if (gPreloadFds && gPreloadFdsExtracted) {
2078-
fds_to_ignore.insert(fds_to_ignore.end(), gPreloadFds->begin(), gPreloadFds->end());
2079-
}
2080-
2081-
pid_t pid = zygote::ForkCommon(env, /* is_system_server= */ false, fds_to_close, fds_to_ignore,
2082-
true);
2069+
pid_t pid = zygote::ForkCommon(env, false, fds_to_close, fds_to_ignore, true);
20832070

20842071
if (pid == 0) {
20852072
SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits,
@@ -2220,10 +2207,6 @@ int zygote::forkApp(JNIEnv* env,
22202207
}
22212208
fds_to_ignore.push_back(gSystemServerSocketFd);
22222209
}
2223-
if (gPreloadFds && gPreloadFdsExtracted) {
2224-
fds_to_ignore.insert(fds_to_ignore.end(), gPreloadFds->begin(), gPreloadFds->end());
2225-
}
2226-
22272210
return zygote::ForkCommon(env, /* is_system_server= */ false, fds_to_close,
22282211
fds_to_ignore, is_priority_fork == JNI_TRUE, purge);
22292212
}
@@ -2481,35 +2464,6 @@ static jboolean com_android_internal_os_Zygote_nativeSupportsTaggedPointers(JNIE
24812464
#endif
24822465
}
24832466

2484-
static void com_android_internal_os_Zygote_nativeMarkOpenedFilesBeforePreload(JNIEnv* env, jclass) {
2485-
// Ignore invocations when too early or too late.
2486-
if (gPreloadFds) {
2487-
return;
2488-
}
2489-
2490-
// App Zygote Preload starts soon. Save FDs remaining open. After the
2491-
// preload finishes newly open files will be determined.
2492-
auto fail_fn = std::bind(zygote::ZygoteFailure, env, "zygote", nullptr, _1);
2493-
gPreloadFds = GetOpenFds(fail_fn).release();
2494-
}
2495-
2496-
static void com_android_internal_os_Zygote_nativeAllowFilesOpenedByPreload(JNIEnv* env, jclass) {
2497-
// Ignore invocations when too early or too late.
2498-
if (!gPreloadFds || gPreloadFdsExtracted) {
2499-
return;
2500-
}
2501-
2502-
// Find the newly open FDs, if any.
2503-
auto fail_fn = std::bind(zygote::ZygoteFailure, env, "zygote", nullptr, _1);
2504-
std::unique_ptr<std::set<int>> current_fds = GetOpenFds(fail_fn);
2505-
auto difference = std::make_unique<std::set<int>>();
2506-
std::set_difference(current_fds->begin(), current_fds->end(), gPreloadFds->begin(),
2507-
gPreloadFds->end(), std::inserter(*difference, difference->end()));
2508-
delete gPreloadFds;
2509-
gPreloadFds = difference.release();
2510-
gPreloadFdsExtracted = true;
2511-
}
2512-
25132467
static const JNINativeMethod gMethods[] = {
25142468
{"nativeForkAndSpecialize",
25152469
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/"
@@ -2552,14 +2506,8 @@ static const JNINativeMethod gMethods[] = {
25522506
(void*)com_android_internal_os_Zygote_nativeBoostUsapPriority},
25532507
{"nativeParseSigChld", "([BI[I)I",
25542508
(void*)com_android_internal_os_Zygote_nativeParseSigChld},
2555-
25562509
{"nativeSupportsTaggedPointers", "()Z",
25572510
(void*)com_android_internal_os_Zygote_nativeSupportsTaggedPointers},
2558-
2559-
{"nativeMarkOpenedFilesBeforePreload", "()V",
2560-
(void*)com_android_internal_os_Zygote_nativeMarkOpenedFilesBeforePreload},
2561-
{"nativeAllowFilesOpenedByPreload", "()V",
2562-
(void*)com_android_internal_os_Zygote_nativeAllowFilesOpenedByPreload},
25632511
};
25642512

25652513
int register_com_android_internal_os_Zygote(JNIEnv* env) {
@@ -2572,4 +2520,4 @@ int register_com_android_internal_os_Zygote(JNIEnv* env) {
25722520

25732521
return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
25742522
}
2575-
} // namespace android
2523+
} // namespace android

core/jni/com_android_internal_os_ZygoteCommandBuffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ jboolean com_android_internal_os_ZygoteCommandBuffer_nativeForkRepeatedly(
402402
socklen_t cred_size = sizeof credentials;
403403
if (getsockopt(n_buffer->getFd(), SOL_SOCKET, SO_PEERCRED, &credentials, &cred_size) == -1
404404
|| cred_size != sizeof credentials) {
405-
fail_fn_1(CREATE_ERROR("ForkMany failed to get initial credentials, %s", strerror(errno)));
405+
fail_fn_1("ForkMany failed to get initial credentials, %s", strerror(errno));
406406
}
407407

408408
bool first_time = true;
@@ -453,7 +453,7 @@ jboolean com_android_internal_os_ZygoteCommandBuffer_nativeForkRepeatedly(
453453
close(session_socket);
454454
int new_fd = accept(zygote_socket_fd, nullptr, nullptr);
455455
if (new_fd == -1) {
456-
fail_fn_z(CREATE_ERROR("Accept(%d) failed: %s", zygote_socket_fd, strerror(errno)));
456+
fail_fn_z("Accept(%d) failed: %s", zygote_socket_fd, strerror(errno));
457457
}
458458
if (new_fd != session_socket) {
459459
// Move new_fd back to the old value, so that we don't have to change Java-level data

core/jni/fd_utils.cpp

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ static const char* kPathWhitelist[] = {
5959

6060
static const char kFdPath[] = "/proc/self/fd";
6161

62+
// static
6263
FileDescriptorWhitelist* FileDescriptorWhitelist::Get() {
6364
if (instance_ == nullptr) {
6465
instance_ = new FileDescriptorWhitelist();
@@ -171,8 +172,8 @@ class FileDescriptorInfo {
171172
// Create a FileDescriptorInfo for a given file descriptor.
172173
static FileDescriptorInfo* CreateFromFd(int fd, fail_fn_t fail_fn);
173174

174-
// Checks whether the file descriptor associated with this object refers to
175-
// the same description.
175+
// Checks whether the file descriptor associated with this object
176+
// refers to the same description.
176177
bool RefersToSameFile() const;
177178

178179
void ReopenOrDetach(fail_fn_t fail_fn) const;
@@ -187,10 +188,8 @@ class FileDescriptorInfo {
187188
const bool is_sock;
188189

189190
private:
190-
// Constructs for sockets.
191191
explicit FileDescriptorInfo(int fd);
192192

193-
// Constructs for non-socket file descriptors.
194193
FileDescriptorInfo(struct stat stat, const std::string& file_path, int fd, int open_flags,
195194
int fd_flags, int fs_flags, off_t offset);
196195

@@ -208,6 +207,7 @@ class FileDescriptorInfo {
208207
DISALLOW_COPY_AND_ASSIGN(FileDescriptorInfo);
209208
};
210209

210+
// static
211211
FileDescriptorInfo* FileDescriptorInfo::CreateFromFd(int fd, fail_fn_t fail_fn) {
212212
struct stat f_stat;
213213
// This should never happen; the zygote should always have the right set
@@ -469,36 +469,49 @@ void FileDescriptorInfo::DetachSocket(fail_fn_t fail_fn) const {
469469
}
470470
}
471471

472-
// TODO: Move the definitions here and eliminate the forward declarations. They
473-
// temporarily help making code reviews easier.
474-
static int ParseFd(dirent* dir_entry, int dir_fd);
475-
static std::unique_ptr<std::set<int>> GetOpenFdsIgnoring(const std::vector<int>& fds_to_ignore,
476-
fail_fn_t fail_fn);
477-
472+
// static
478473
FileDescriptorTable* FileDescriptorTable::Create(const std::vector<int>& fds_to_ignore,
479474
fail_fn_t fail_fn) {
480-
std::unique_ptr<std::set<int>> open_fds = GetOpenFdsIgnoring(fds_to_ignore, fail_fn);
475+
DIR* proc_fd_dir = opendir(kFdPath);
476+
if (proc_fd_dir == nullptr) {
477+
fail_fn(std::string("Unable to open directory ").append(kFdPath));
478+
}
479+
480+
int dir_fd = dirfd(proc_fd_dir);
481+
dirent* dir_entry;
481482

482483
std::unordered_map<int, FileDescriptorInfo*> open_fd_map;
483-
for (auto fd : *open_fds) {
484+
while ((dir_entry = readdir(proc_fd_dir)) != nullptr) {
485+
const int fd = ParseFd(dir_entry, dir_fd);
486+
if (fd == -1) {
487+
continue;
488+
}
489+
490+
if (std::find(fds_to_ignore.begin(), fds_to_ignore.end(), fd) != fds_to_ignore.end()) {
491+
continue;
492+
}
484493

485494
open_fd_map[fd] = FileDescriptorInfo::CreateFromFd(fd, fail_fn);
486495
}
487496

497+
if (closedir(proc_fd_dir) == -1) {
498+
fail_fn("Unable to close directory");
499+
}
500+
488501
return new FileDescriptorTable(open_fd_map);
489502
}
490503

491-
static std::unique_ptr<std::set<int>> GetOpenFdsIgnoring(const std::vector<int>& fds_to_ignore,
492-
fail_fn_t fail_fn) {
504+
void FileDescriptorTable::Restat(const std::vector<int>& fds_to_ignore, fail_fn_t fail_fn) {
505+
std::set<int> open_fds;
493506

507+
// First get the list of open descriptors.
494508
DIR* proc_fd_dir = opendir(kFdPath);
495509
if (proc_fd_dir == nullptr) {
496510
fail_fn(android::base::StringPrintf("Unable to open directory %s: %s",
497511
kFdPath,
498512
strerror(errno)));
499513
}
500514

501-
auto result = std::make_unique<std::set<int>>();
502515
int dir_fd = dirfd(proc_fd_dir);
503516
dirent* dir_entry;
504517
while ((dir_entry = readdir(proc_fd_dir)) != nullptr) {
@@ -511,26 +524,14 @@ static std::unique_ptr<std::set<int>> GetOpenFdsIgnoring(const std::vector<int>&
511524
continue;
512525
}
513526

514-
result->insert(fd);
527+
open_fds.insert(fd);
515528
}
516529

517530
if (closedir(proc_fd_dir) == -1) {
518531
fail_fn(android::base::StringPrintf("Unable to close directory: %s", strerror(errno)));
519532
}
520-
return result;
521-
}
522533

523-
std::unique_ptr<std::set<int>> GetOpenFds(fail_fn_t fail_fn) {
524-
const std::vector<int> nothing_to_ignore;
525-
return GetOpenFdsIgnoring(nothing_to_ignore, fail_fn);
526-
}
527-
528-
void FileDescriptorTable::Restat(const std::vector<int>& fds_to_ignore, fail_fn_t fail_fn) {
529-
std::unique_ptr<std::set<int>> open_fds = GetOpenFdsIgnoring(fds_to_ignore, fail_fn);
530-
531-
// Check that the files did not change, and leave only newly opened FDs in
532-
// |open_fds|.
533-
RestatInternal(*open_fds, fail_fn);
534+
RestatInternal(open_fds, fail_fn);
534535
}
535536

536537
// Reopens all file descriptors that are contained in the table.
@@ -551,12 +552,6 @@ FileDescriptorTable::FileDescriptorTable(
551552
: open_fd_map_(map) {
552553
}
553554

554-
FileDescriptorTable::~FileDescriptorTable() {
555-
for (auto& it : open_fd_map_) {
556-
delete it.second;
557-
}
558-
}
559-
560555
void FileDescriptorTable::RestatInternal(std::set<int>& open_fds, fail_fn_t fail_fn) {
561556
// ART creates a file through memfd for optimization purposes. We make sure
562557
// there is at most one being created.
@@ -627,7 +622,8 @@ void FileDescriptorTable::RestatInternal(std::set<int>& open_fds, fail_fn_t fail
627622
}
628623
}
629624

630-
static int ParseFd(dirent* dir_entry, int dir_fd) {
625+
// static
626+
int FileDescriptorTable::ParseFd(dirent* dir_entry, int dir_fd) {
631627
char* end;
632628
const int fd = strtol(dir_entry->d_name, &end, 10);
633629
if ((*end) != '\0') {

0 commit comments

Comments
 (0)