Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injected library lifecycle management? #150

Open
ajwerner opened this issue Sep 6, 2024 · 6 comments
Open

Injected library lifecycle management? #150

ajwerner opened this issue Sep 6, 2024 · 6 comments

Comments

@ajwerner
Copy link
Contributor

ajwerner commented Sep 6, 2024

How does one detect when an injected library has exited? Looking at the implementation of the injector, it seems like upon exit, at least in theory, the code will deallocate the memory in the target that it allocated.

In practice I never see the code run even though the library does exit. Also, there's not a way as far as I can tell to wait for or detect the state of the injected library. All you're handed is an JD that seems like it can only be used with demonitor (which is not a clear concept to me).

I think I'd want some way to wait for the library to exit and be deallocated.

It's possible that this is a request for new APIs in frida-core. It seems to me that the Vala API has an uninjwcted signal, but I don't know how to use that.

@s1341
Copy link
Contributor

s1341 commented Sep 8, 2024

what do you mean by "library exited"? Do you mean when the library is unloaded?

@ajwerner
Copy link
Contributor Author

ajwerner commented Sep 9, 2024

what do you mean by "library exited"? Do you mean when the library is unloaded?

I suppose yes, though what I really mean is when the injected loader "agent" exits. I'd love to have better terminology. Imagine I use inject_library_file_sync and that library does not touch stay_resident. frida-core's loader will call dlclose and then send bye back on the control socket. At that point, if frida stays running, the loader will get cleaned up (see https://github.com/frida/frida-core/blob/31188db39a7c9ae24f640a34b3fdf701f4a93bb3/src/linux/frida-helper-backend.vala#L367-L385)

I want some way to synchronize shutdown of frida in my code with these agents being unloaded. I can't figure out what APIs to use to do that.

@hsorbo
Copy link
Member

hsorbo commented Sep 9, 2024

Generally you can use frida_script_load / frida_script_unload. And while it's loaded you can call rpc-methods (WIP in rust).

@ajwerner
Copy link
Contributor Author

ajwerner commented Sep 9, 2024

Generally you can use frida_script_load / frida_script_unload. And while it's loaded you can call rpc-methods (WIP in rust).

I think you're talking about something quite different from what I'm talking about. You're talking about the script APIs in Frida where the javascript runtime stays loaded. I'm talking about the lower-level library injection APIs.

@oleavr
Copy link
Member

oleavr commented Sep 10, 2024

what do you mean by "library exited"? Do you mean when the library is unloaded?

I suppose yes, though what I really mean is when the injected loader "agent" exits. I'd love to have better terminology. Imagine I use inject_library_file_sync and that library does not touch stay_resident. frida-core's loader will call dlclose and then send bye back on the control socket. At that point, if frida stays running, the loader will get cleaned up (see https://github.com/frida/frida-core/blob/31188db39a7c9ae24f640a34b3fdf701f4a93bb3/src/linux/frida-helper-backend.vala#L367-L385)

I want some way to synchronize shutdown of frida in my code with these agents being unloaded. I can't figure out what APIs to use to do that.

What you're looking for is frida-core/lib/pipe -- we should expose this in the Rust bindings.

@ajwerner
Copy link
Contributor Author

What you're looking for is frida-core/lib/pipe -- we should expose this in the Rust bindings.

Can you say more about how I'd use that if it were exposed? These pipe APIs seem to be about connecting to the injected agent. Let me know if I'm misunderstanding.

As it stands, I'm not using lib anywhere in my "agent" library. I have IPC between the injected library and the injector/control plane set up independently of frida. I can detect when my agent thinks it has exited. What I can't figure out is how to wait until Frida has actually unloaded the loader it injected into the target. If I just add a sleep for a second after my library exits, that works well enough, but is not a very robust solution.


I have a follow-up goal to figure out how to recover from a case where the injector process crashes, and still to be able to clean-up the injected state. I've avoided talking about this goal up to this point because it feels certain that it'll need more code/APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants