Skip to content

Commit

Permalink
added segment APIs for freebsd
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbo committed Mar 7, 2024
1 parent 16b092f commit 9b06778
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 16 deletions.
36 changes: 36 additions & 0 deletions src/freebsd/osprot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* ----------------------------------
* | libmem - by rdbo |
* | Memory Hacking Library |
* ----------------------------------
*/

/*
* Copyright (C) 2023 Rdbo
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "osprot.h"
#include <sys/mman.h>

int
get_os_prot(lm_prot_t prot)
{
return (int)(prot & LM_PROT_XRW);
}

lm_prot_t
get_prot(int osprot)
{
return (lm_prot_t)(osprot & (PROT_EXEC | PROT_READ | PROT_WRITE));
}
36 changes: 36 additions & 0 deletions src/freebsd/osprot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* ----------------------------------
* | libmem - by rdbo |
* | Memory Hacking Library |
* ----------------------------------
*/

/*
* Copyright (C) 2023 Rdbo
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/* TODO: Merge 'osprot.h' and 'osprot.c' for Linux and FreeBSD */

#ifndef OSPROT_H
#define OSPROT_H

#include <libmem/libmem.h>

int
get_os_prot(lm_prot_t prot);

lm_prot_t
get_prot(int osprot);

#endif
76 changes: 76 additions & 0 deletions src/freebsd/segment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* ----------------------------------
* | libmem - by rdbo |
* | Memory Hacking Library |
* ----------------------------------
*/

/*
* Copyright (C) 2023 Rdbo
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include <libmem/libmem.h>
#include "osprot.h"
#include <unistd.h>
#include <sys/user.h>
#include <sys/sysctl.h>
#include <libprocstat.h>

LM_API lm_bool_t LM_CALL
LM_EnumSegmentsEx(const lm_process_t *process,
lm_bool_t (LM_CALL *callback)(lm_segment_t *segment,
lm_void_t *arg),
lm_void_t *arg)
{
lm_bool_t result = LM_FALSE;
struct procstat *ps;
struct kinfo_proc *proc;
struct kinfo_vmentry *vmmap;
unsigned int count;
unsigned int i;
lm_segment_t segment;

if (!process || !callback)
return result;

ps = procstat_open_sysctl();
if (!ps)
return result;

proc = procstat_getprocs(ps, KERN_PROC_PID, process->pid, &count);
if (!proc)
goto CLOSE_EXIT;

vmmap = procstat_getvmmap(ps, proc, &count);
procstat_freeprocs(ps, proc);
if (!vmmap)
goto CLOSE_EXIT;

for (i = 0; i < count; ++i) {
segment.base = vmmap[i].kve_start;
segment.end = vmmap[i].kve_end;
segment.size = segment.end - segment.base;
segment.prot = get_prot(vmmap[i].kve_protection);

if (callback(&segment, arg) == LM_FALSE)
break;
}

result = LM_TRUE;

procstat_freevmmap(ps, vmmap);
CLOSE_EXIT:
procstat_close(ps);
return result;
}
22 changes: 6 additions & 16 deletions src/linux/osprot.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,14 @@
#include "osprot.h"
#include <sys/mman.h>



#include <stdio.h>

int
get_os_prot(lm_prot_t prot)
{
int osprot = PROT_NONE;
return (int)(prot & LM_PROT_XRW);
}

switch (prot) {
case LM_PROT_X: osprot = PROT_EXEC;
case LM_PROT_R: osprot = PROT_READ;
case LM_PROT_W: osprot = PROT_WRITE;
case LM_PROT_XR: osprot = PROT_EXEC | PROT_READ;
case LM_PROT_XW: osprot = PROT_EXEC | PROT_WRITE;
case LM_PROT_RW: osprot = PROT_READ | PROT_WRITE;
case LM_PROT_XRW: osprot = PROT_EXEC | PROT_READ | PROT_WRITE;
}

return osprot;
lm_prot_t
get_prot(int osprot)
{
return (lm_prot_t)(osprot & (PROT_EXEC | PROT_READ | PROT_WRITE));
}
5 changes: 5 additions & 0 deletions src/linux/osprot.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

/* TODO: Merge 'osprot.h' and 'osprot.c' for Linux and FreeBSD */

#ifndef OSPROT_H
#define OSPROT_H

Expand All @@ -28,4 +30,7 @@
int
get_os_prot(lm_prot_t prot);

lm_prot_t
get_prot(int osprot);

#endif

0 comments on commit 9b06778

Please sign in to comment.