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

mcount-dynamic: Use MAP_FIXED_NOREPLACE for trampoline #1798

Merged
merged 1 commit into from Sep 4, 2023

Conversation

oldiob
Copy link

@oldiob oldiob commented Aug 5, 2023

By trying to allocate the extra page for a trampoline with MAP_FIXED, Uftrace could end up overlapping already mapped pages, which are discarded. Typically, if data was there with rw- protection, now the data is undefined and has rwx protection.

Since Linux 4.17, the flag MAP_FIXED_NOREPLACE can be used to ensure that the fixed location is mapped atomically, preventing any clobbering.

For earlier kernel, the call to mmap(2) will fall back to a random address, by not honoring the MAP_FIXED request at all. So callers should ensure that the returned address matches the requested one.

By trying to allocate the extra page for a trampoline with MAP_FIXED,
Uftrace could end up overlapping already mapped pages, which are
discarded.  Typically, if data was there with rw- protection, now the
data is undefined and has rwx protection.

Since Linux 4.17, the flag MAP_FIXED_NOREPLACE can be used to ensure
that the fixed location is mapped atomically, preventing any clobbering.

For earlier kernel, the call to mmap(2) will fall back to a random
address, by not honoring the MAP_FIXED request at all.  So callers
should ensure that the returned address matches the requested one.

Signed-off-by: Olivier Dion <odion@efficios.com>
@honggyukim
Copy link
Collaborator

Hi @oldiob, thanks for the PR. We need to think a bit more about the new MAP_FIXED_NOREPLACE so we will get back to you later. Especially, we need to think if this is backward compatible on the old kernels before v4.17.

@namhyung
Copy link
Owner

namhyung commented Aug 9, 2023

It seems the header defines the flag as a preprocessor macro. Then we can simply check it like:

#ifndef MAP_FIXED_NOREPLACE
#define MAP_FIXED_NOREPLACE  MAP_FIXED
#endif

Copy link
Owner

@namhyung namhyung left a comment

Choose a reason for hiding this comment

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

I'll merge this for now and add a follow up separately.

@namhyung namhyung merged commit 955b9e9 into namhyung:master Sep 4, 2023
3 checks passed
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.

None yet

3 participants