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

fix: avoid WRITE+EXEC for CPUID check #15912

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion py-polars/polars/_cpu_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from __future__ import annotations

import ctypes
import ctypes.util
import os
from ctypes import CFUNCTYPE, POINTER, c_long, c_size_t, c_uint32, c_ulong, c_void_p
from typing import ClassVar
Expand Down Expand Up @@ -159,15 +160,26 @@ def __init__(self) -> None:
else:
import mmap # Only import if necessary.

# On some platforms PROT_WRITE + PROT_EXEC is forbidden, so we first
# only write and then mprotect into PROT_EXEC.
libc = ctypes.CDLL(ctypes.util.find_library("c"), use_errno=True)
mprotect = libc.mprotect
mprotect.argtypes = (ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int)
mprotect.restype = ctypes.c_int

self.mmap = mmap.mmap(
-1,
size,
mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS,
mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC,
mmap.PROT_READ | mmap.PROT_WRITE,
)
self.addr = ctypes.addressof(ctypes.c_void_p.from_buffer(self.mmap))
self.mmap.write(code)

if mprotect(self.addr, size, mmap.PROT_READ | mmap.PROT_EXEC) != 0:
msg = "could not execute mprotect for CPUID check"
raise RuntimeError(msg)

func_type = CFUNCTYPE(None, POINTER(CPUID_struct), c_uint32, c_uint32)
self.func_ptr = func_type(self.addr)

Expand Down
Loading