Skip to content

Introduced an API to collect the core from an unrelated process#11

Open
ramkums wants to merge 1 commit intoPercona-Lab:masterfrom
ramkums:coredumper_tool
Open

Introduced an API to collect the core from an unrelated process#11
ramkums wants to merge 1 commit intoPercona-Lab:masterfrom
ramkums:coredumper_tool

Conversation

@ramkums
Copy link

@ramkums ramkums commented Dec 2, 2024

The API WritePidCoreDumpWith() will take argument pid, and try to collect the core from the process with pid, this uses the existing logic to identify the threads under the given process and to identify the maps and the properties of each memory maps. Then read the memory content from /proc/[pid]/mem, we assume that the caller has the permission to trace and read the mem from process [pid].

A tool also introduced, which will use the API WritePidCoreDumpWith and provide a convenient CLI for the user, which will help in creating core dump of any process that the user has permission to trace.

/* If pid belong to the one called the library no need to proceed */
if (expid == 0 && pid == clone_pid) continue;

strcat(strcpy(fname, "/proc/"), entry->d_name);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fine

Copy link

@jlevon jlevon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I think this looks good to me now

The API WriteCoreDumpOfPID() will take argument pid, and try to
collect the core from the process with pid, this uses the
existing logic to identify the threads under the given process
and to identify the maps and the properties of each memory maps.
Then read the memory content from /proc/[pid]/mem, we assume that
the caller has the permission to trace and read the mem from
process [pid].

A tool also introduced, which will use the API WriteCoreDumpOfPID
and provide a convenient CLI for the user, which will help in
creating core dump of any process that the user has permission to trace.

Following is the help from the tool:
Usage: ./coredumper_tool <-c corefile> <-p pid> [-s core_size] [-z]
<-h>               Show this message and exit
<-c core_file>     Collect the core dump into a file core_file
<-p pid>           Collect the core dump of process with pid
<-s core_size>     Limit the size of core_file to core_size
<-z>               Compress the core_file on the fly with gzip
@dutow
Copy link
Collaborator

dutow commented Mar 19, 2025

Hello! Thanks for the pull request.

@ramkums can you explain the reasoning behind this functionality, what's your usecase? This seems to be already doable with gcore from gdb.

@ramkums
Copy link
Author

ramkums commented Mar 19, 2025

Hello! Thanks for the pull request.

@ramkums can you explain the reasoning behind this functionality, what's your usecase? This seems to be already doable with gcore from gdb.

Yes, gcore can do it. But it requires the gdb package which is a heavy hammer. And we consider having gdb on a production setup is risky. Moreover we wanted to collect the coredump on specific conditions without user intervention.

@jlevon
Copy link

jlevon commented Mar 19, 2025

You could also make the same point about self-coredump, right? You can also use gcore for that...

@dutow
Copy link
Collaborator

dutow commented Mar 19, 2025

Thanks for the quick replies @jlevon, @ramkums!

And we consider having gdb on a production setup is risky.

Security/risk is my main concern too, and that's why I'm asking if there are no workarounds without this feature.

On the positive side, I can see how this could make implementing some things in a multi process environment easier, even for us.

But the point there is easier, and not possible. I couldn't come up with a single situation, where we can modify the sources of the programs we are using, to actually need this feature. And that's not just with gcore, I only mentioned that because of the example tool you included in the PR - gcore does exactly the same.

Unless you want to implement a lightweight gcore, or a helper process for pods, without modifying the main app running on the pod itself, which of course can be a use case.

On the other hand, this ability can be a potential security risk, a vulnerability in a program using this library could provide an easy way to get a coredump of something else running on the same system, as long as the permissions allow it.

@jlevon
Copy link

jlevon commented Mar 20, 2025

@dutow can you expand on your security concerns a little more, as I'm not quite getting it. You said:

vulnerability in a program using this library could provide an easy way to get a coredump of something else running on the same system

But that would only be a problem if it were possible to elevate privileges as part of this (for example, making that program setuid or it being a privileged daemon). I don't see how that's any different to any other facility: both suid and privileged processes must be written in a secure manner regardless.

In particular, a program such as "coredumper" that uses this facility but is not otherwise given additional privileges would not have any security impact, since everything the program can do with this code, the invoking user could do anyway.

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

Successfully merging this pull request may close these issues.

3 participants