Skip to content

Commit

Permalink
Fix SIGSEGV when nokogiri is loaded before oci8
Browse files Browse the repository at this point in the history
This bug was introduced in 2.2.13.
  • Loading branch information
kubo committed Aug 5, 2024
1 parent 7226147 commit 4862f8a
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 111 deletions.
8 changes: 8 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @markup markdown

2.2.14 (2024-08-XX)
===================

Fixed issues
------------

- Fix SIGSEGV when nokogiri is loaded before oci8 (GH-263 - Linux only)

2.2.13 (2024-07-27)
===================

Expand Down
15 changes: 14 additions & 1 deletion ext/oci8/oci8lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endif
#if defined(HAVE_PLTHOOK) && !defined(WIN32)
#include <dlfcn.h>
#include <sys/mman.h>
#include "plthook.h"
#endif

Expand Down Expand Up @@ -135,6 +136,8 @@ static void rebind_internal_symbols(void)
void **addr;
const char *prefix;
size_t prefix_len;
int prot;
size_t page_size = sysconf(_SC_PAGESIZE);

#ifdef RTLD_FIRST
flags |= RTLD_FIRST; /* for macOS */
Expand All @@ -161,7 +164,7 @@ static void rebind_internal_symbols(void)
plthook_close(ph);
return;
}
while (plthook_enum(ph, &pos, &name, &addr) == 0) {
while (plthook_enum_with_prot(ph, &pos, &name, &addr, &prot) == 0) {
void *funcaddr;
if (prefix_len != 0) {
if (strncmp(name, prefix, prefix_len) != 0) {
Expand All @@ -179,7 +182,17 @@ static void rebind_internal_symbols(void)
* PLT entries are forcedly modified to point to itself not
* to use functions in other libraries.
*/
#define ALIGN_ADDR(addr) ((void*)((size_t)(addr) & ~(page_size - 1)))
if ((prot & PROT_WRITE) == 0) {
/* when the region containing addr isn't writable, make it writable temporarily */
if (mprotect(ALIGN_ADDR(addr), page_size, PROT_READ | PROT_WRITE) != 0) {
continue;
}
}
*addr = funcaddr;
if ((prot & PROT_WRITE) == 0) {
mprotect(ALIGN_ADDR(addr), page_size, prot);
}
}
}
plthook_close(ph);
Expand Down
8 changes: 7 additions & 1 deletion ext/oci8/plthook.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ int plthook_replace(plthook_t *plthook, const char *funcname, void *funcaddr, vo
void plthook_close(plthook_t *plthook);
const char *plthook_error(void);

/* enumerate entries with memory protection information (bitwise-OR of PROT_READ, PROT_WRITE and PROT_EXEC)
*
* source: plthook_elf.c and plthook_osx.c
*/
int plthook_enum_with_prot(plthook_t *plthook, unsigned int *pos, const char **name_out, void ***addr_out, int *prot);

#ifdef __cplusplus
}; /* extern "C" */
} /* extern "C" */
#endif

#endif
Loading

0 comments on commit 4862f8a

Please sign in to comment.