Skip to content

Commit

Permalink
Use open instead of fopen to avoid calls to malloc
Browse files Browse the repository at this point in the history
Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Oct 16, 2024
1 parent 1a31aa2 commit 51898d2
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions lib/ulp.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,30 +88,31 @@ begin(void)
msgq_push("libpulp loaded...\n");
}

/** @brief Attempt to get FILE pointer to /proc/self/mem
/** @brief Attempt to get file descriptor to /proc/self/mem
*
* Chroots may change the path to the /proc folder. This function attempts to
* discover it.
*
* @return file pointer.
*/
FILE *
int
get_proc_mem(void)
{
FILE *file = fopen("/proc/self/mem", "r+");
int fd = open("/proc/self/mem", O_WRONLY);

/* SLE have some processes which chroots into /proc. If the above fopen
fails then try this to check if this is the case. */
if (file == NULL && errno == ENOENT) {
if (fd < 0 && errno == ENOENT) {
/* Process has chroot'ed. Check if it was chroot'ed to /proc itself. */
file = fopen("/self/mem", "r+");
fd = open("/self/mem", O_WRONLY);
}

if (file == NULL) {
if (fd < 0) {
WARN("Attempting to open /proc/self/mem failed: %s", libpulp_strerror(errno));
set_libpulp_error_state(EPROCMEM);
}

return file;
return fd;
}

/** @brief Write into memory bypassing memory protections
Expand All @@ -128,14 +129,18 @@ get_proc_mem(void)
void *
memwrite(void *dest, const void *src, size_t n)
{
FILE *file = get_proc_mem();
libpulp_assert(file);
int fd = get_proc_mem();
off_t dst = (off_t) dest;

libpulp_assert(fseek(file, (size_t)dest, SEEK_SET) == 0);
libpulp_assert(fwrite(src, 1, n, file) == n);
if (fd < 0) {
return NULL;
}

fflush(file);
fclose(file);
libpulp_assert(lseek(fd, dst, SEEK_SET) == dst);
libpulp_assert(write(fd, src, n) == (ssize_t) n);

fsync(fd);
close(fd);

return dest;
}
Expand Down

0 comments on commit 51898d2

Please sign in to comment.