mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-25 19:21:35 +01:00
bak
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
Programs in this directory rely on Linux kernel specific functionality such as `/proc`.
|
||||
Programs in this directory rely on Linux kernel specific functionality such as `/proc` or raw system calls.
|
||||
|
||||
These programs may also conform to ANSI C or POSIX, but we put them here because you can only observe them at work under Linux.
|
||||
|
||||
Many of these programs use glibc or GCC extensions.
|
||||
|
||||
25
userland/linux/init_env_poweroff.c
Normal file
25
userland/linux/init_env_poweroff.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <stdio.h>
|
||||
#include <sys/reboot.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int i;
|
||||
|
||||
puts("args:");
|
||||
for (i = 0; i < argc; ++i)
|
||||
puts(argv[i]);
|
||||
puts("");
|
||||
|
||||
puts("env:");
|
||||
extern char **environ;
|
||||
char **env = environ;
|
||||
while (*env) {
|
||||
printf("%s\n", *env);
|
||||
env++;
|
||||
}
|
||||
puts("");
|
||||
|
||||
/* Poweroff. */
|
||||
reboot(RB_POWER_OFF);
|
||||
}
|
||||
61
userland/linux/myinsmod.c
Normal file
61
userland/linux/myinsmod.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#myinsmod */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define init_module(module_image, len, param_values) syscall(__NR_init_module, module_image, len, param_values)
|
||||
#define finit_module(fd, param_values, flags) syscall(__NR_finit_module, fd, param_values, flags)
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *params;
|
||||
int fd, use_finit;
|
||||
size_t image_size;
|
||||
struct stat st;
|
||||
void *image;
|
||||
|
||||
/* CLI handling. */
|
||||
if (argc < 2) {
|
||||
puts("Usage ./prog mymodule.ko [args="" [use_finit=0]");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (argc < 3) {
|
||||
params = "";
|
||||
} else {
|
||||
params = argv[2];
|
||||
}
|
||||
if (argc < 4) {
|
||||
use_finit = 0;
|
||||
} else {
|
||||
use_finit = (argv[3][0] != '0');
|
||||
}
|
||||
|
||||
/* Action. */
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
if (use_finit) {
|
||||
puts("finit");
|
||||
if (finit_module(fd, params, 0) != 0) {
|
||||
perror("finit_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
close(fd);
|
||||
} else {
|
||||
puts("init");
|
||||
fstat(fd, &st);
|
||||
image_size = st.st_size;
|
||||
image = malloc(image_size);
|
||||
read(fd, image, image_size);
|
||||
close(fd);
|
||||
if (init_module(image, image_size, params) != 0) {
|
||||
perror("init_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
free(image);
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
24
userland/linux/myrmmod.c
Normal file
24
userland/linux/myrmmod.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#myinsmod */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define delete_module(name, flags) syscall(__NR_delete_module, name, flags)
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 2) {
|
||||
puts("Usage ./prog mymodule");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (delete_module(argv[1], O_NONBLOCK) != 0) {
|
||||
perror("delete_module");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -5,5 +5,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
reboot(RB_POWER_OFF);
|
||||
reboot(RB_POWER_OFF);
|
||||
}
|
||||
|
||||
171
userland/linux/proc_events.c
Normal file
171
userland/linux/proc_events.c
Normal file
@@ -0,0 +1,171 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#config_proc_events
|
||||
*
|
||||
* Adapted from: https://stackoverflow.com/questions/6075013/detect-launching-of-programs-on-linux-platform/8255487#8255487
|
||||
*/
|
||||
|
||||
#if defined(__aarch64__)
|
||||
|
||||
int main(void) {}
|
||||
|
||||
#else
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <errno.h>
|
||||
#include <linux/cn_proc.h>
|
||||
#include <linux/connector.h>
|
||||
#include <linux/netlink.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static volatile bool need_exit = false;
|
||||
|
||||
static int nl_connect() {
|
||||
int rc;
|
||||
int nl_sock;
|
||||
struct sockaddr_nl sa_nl;
|
||||
|
||||
nl_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
|
||||
if (nl_sock == -1) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
sa_nl.nl_family = AF_NETLINK;
|
||||
sa_nl.nl_groups = CN_IDX_PROC;
|
||||
sa_nl.nl_pid = getpid();
|
||||
rc = bind(nl_sock, (struct sockaddr *)&sa_nl, sizeof(sa_nl));
|
||||
if (rc == -1) {
|
||||
perror("bind");
|
||||
close(nl_sock);
|
||||
return -1;
|
||||
}
|
||||
return nl_sock;
|
||||
}
|
||||
|
||||
static int set_proc_ev_listen(int nl_sock, bool enable) {
|
||||
int rc;
|
||||
struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
|
||||
struct nlmsghdr nl_hdr;
|
||||
struct __attribute__ ((__packed__)) {
|
||||
struct cn_msg cn_msg;
|
||||
enum proc_cn_mcast_op cn_mcast;
|
||||
};
|
||||
} nlcn_msg;
|
||||
|
||||
memset(&nlcn_msg, 0, sizeof(nlcn_msg));
|
||||
nlcn_msg.nl_hdr.nlmsg_len = sizeof(nlcn_msg);
|
||||
nlcn_msg.nl_hdr.nlmsg_pid = getpid();
|
||||
nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE;
|
||||
|
||||
nlcn_msg.cn_msg.id.idx = CN_IDX_PROC;
|
||||
nlcn_msg.cn_msg.id.val = CN_VAL_PROC;
|
||||
nlcn_msg.cn_msg.len = sizeof(enum proc_cn_mcast_op);
|
||||
|
||||
nlcn_msg.cn_mcast = enable ? PROC_CN_MCAST_LISTEN : PROC_CN_MCAST_IGNORE;
|
||||
|
||||
rc = send(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
|
||||
if (rc == -1) {
|
||||
perror("netlink send");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_proc_ev(int nl_sock) {
|
||||
int rc;
|
||||
struct __attribute__ ((aligned(NLMSG_ALIGNTO))) {
|
||||
struct nlmsghdr nl_hdr;
|
||||
struct __attribute__ ((__packed__)) {
|
||||
struct cn_msg cn_msg;
|
||||
struct proc_event proc_ev;
|
||||
};
|
||||
} nlcn_msg;
|
||||
while (!need_exit) {
|
||||
rc = recv(nl_sock, &nlcn_msg, sizeof(nlcn_msg), 0);
|
||||
if (rc == 0) {
|
||||
return 0;
|
||||
} else if (rc == -1) {
|
||||
if (errno == EINTR) continue;
|
||||
perror("netlink recv");
|
||||
return -1;
|
||||
}
|
||||
switch (nlcn_msg.proc_ev.what) {
|
||||
case PROC_EVENT_NONE:
|
||||
printf("set mcast listen ok\n");
|
||||
break;
|
||||
case PROC_EVENT_FORK:
|
||||
printf("fork: parent tid=%d pid=%d -> child tid=%d pid=%d\n",
|
||||
nlcn_msg.proc_ev.event_data.fork.parent_pid,
|
||||
nlcn_msg.proc_ev.event_data.fork.parent_tgid,
|
||||
nlcn_msg.proc_ev.event_data.fork.child_pid,
|
||||
nlcn_msg.proc_ev.event_data.fork.child_tgid);
|
||||
break;
|
||||
case PROC_EVENT_EXEC:
|
||||
printf("exec: tid=%d pid=%d\n",
|
||||
nlcn_msg.proc_ev.event_data.exec.process_pid,
|
||||
nlcn_msg.proc_ev.event_data.exec.process_tgid);
|
||||
break;
|
||||
case PROC_EVENT_UID:
|
||||
printf("uid change: tid=%d pid=%d from %d to %d\n",
|
||||
nlcn_msg.proc_ev.event_data.id.process_pid,
|
||||
nlcn_msg.proc_ev.event_data.id.process_tgid,
|
||||
nlcn_msg.proc_ev.event_data.id.r.ruid,
|
||||
nlcn_msg.proc_ev.event_data.id.e.euid);
|
||||
break;
|
||||
case PROC_EVENT_GID:
|
||||
printf("gid change: tid=%d pid=%d from %d to %d\n",
|
||||
nlcn_msg.proc_ev.event_data.id.process_pid,
|
||||
nlcn_msg.proc_ev.event_data.id.process_tgid,
|
||||
nlcn_msg.proc_ev.event_data.id.r.rgid,
|
||||
nlcn_msg.proc_ev.event_data.id.e.egid);
|
||||
break;
|
||||
case PROC_EVENT_EXIT:
|
||||
printf("exit: tid=%d pid=%d exit_code=%d\n",
|
||||
nlcn_msg.proc_ev.event_data.exit.process_pid,
|
||||
nlcn_msg.proc_ev.event_data.exit.process_tgid,
|
||||
nlcn_msg.proc_ev.event_data.exit.exit_code);
|
||||
break;
|
||||
default:
|
||||
printf("unhandled proc event\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void on_sigint(__attribute__ ((unused)) int unused) {
|
||||
need_exit = true;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int nl_sock;
|
||||
int rc = EXIT_SUCCESS;
|
||||
|
||||
signal(SIGINT, &on_sigint);
|
||||
siginterrupt(SIGINT, true);
|
||||
nl_sock = nl_connect();
|
||||
if (nl_sock == -1)
|
||||
exit(EXIT_FAILURE);
|
||||
rc = set_proc_ev_listen(nl_sock, true);
|
||||
if (rc == -1) {
|
||||
rc = EXIT_FAILURE;
|
||||
goto out;
|
||||
}
|
||||
rc = handle_proc_ev(nl_sock);
|
||||
if (rc == -1) {
|
||||
rc = EXIT_FAILURE;
|
||||
goto out;
|
||||
}
|
||||
set_proc_ev_listen(nl_sock, false);
|
||||
out:
|
||||
close(nl_sock);
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,29 +13,29 @@ int bss = 0;
|
||||
int data = 1;
|
||||
|
||||
int main(__attribute__((unused)) int argc, char **argv) {
|
||||
int i, *ip;
|
||||
uint64_t uint64;
|
||||
FILE *fp;
|
||||
int i, *ip;
|
||||
uint64_t uint64;
|
||||
FILE *fp;
|
||||
|
||||
/* Loaded addresses. */
|
||||
printf("&i = %p\n", (void *)&i);
|
||||
printf("&argv[0] = %p\n", (void *)&argv[0]);
|
||||
printf("&main = %p\n", (void *)(intptr_t)main);
|
||||
printf("&bss = %p\n", (void *)&bss);
|
||||
printf("&data = %p\n", (void *)&data);
|
||||
/* Loaded addresses. */
|
||||
printf("&i = %p\n", (void *)&i);
|
||||
printf("&argv[0] = %p\n", (void *)&argv[0]);
|
||||
printf("&main = %p\n", (void *)(intptr_t)main);
|
||||
printf("&bss = %p\n", (void *)&bss);
|
||||
printf("&data = %p\n", (void *)&data);
|
||||
|
||||
/* Misc syscalls. */
|
||||
printf("time(NULL) = %ju\n", (uintmax_t)time(NULL));
|
||||
printf("pid = %ju\n", (uintmax_t)getpid());
|
||||
/* Misc syscalls. */
|
||||
printf("time(NULL) = %ju\n", (uintmax_t)time(NULL));
|
||||
printf("pid = %ju\n", (uintmax_t)getpid());
|
||||
|
||||
/* malloc */
|
||||
ip = malloc(sizeof(*ip));
|
||||
printf("&malloc = %p\n", (void *)ip);
|
||||
free(ip);
|
||||
/* malloc */
|
||||
ip = malloc(sizeof(*ip));
|
||||
printf("&malloc = %p\n", (void *)ip);
|
||||
free(ip);
|
||||
|
||||
/* /dev/urandom */
|
||||
fp = fopen("/dev/urandom", "rb");
|
||||
fread(&uint64, sizeof(uint64), 1, fp);
|
||||
printf("/dev/urandom = %" PRIx64 "\n", uint64);
|
||||
fclose(fp);
|
||||
/* /dev/urandom */
|
||||
fp = fopen("/dev/urandom", "rb");
|
||||
fread(&uint64, sizeof(uint64), 1, fp);
|
||||
printf("/dev/urandom = %" PRIx64 "\n", uint64);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
42
userland/linux/sched_getaffinity.c
Normal file
42
userland/linux/sched_getaffinity.c
Normal file
@@ -0,0 +1,42 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#gdb-step-debug-multicore */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <sched.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void print_affinity() {
|
||||
cpu_set_t mask;
|
||||
long nproc, i;
|
||||
|
||||
if (sched_getaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
|
||||
perror("sched_getaffinity");
|
||||
assert(false);
|
||||
} else {
|
||||
nproc = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
printf("sched_getaffinity = ");
|
||||
for (i = 0; i < nproc; i++) {
|
||||
printf("%d ", CPU_ISSET(i, &mask));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
cpu_set_t mask;
|
||||
|
||||
print_affinity();
|
||||
printf("sched_getcpu = %d\n", sched_getcpu());
|
||||
CPU_ZERO(&mask);
|
||||
CPU_SET(0, &mask);
|
||||
if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
|
||||
perror("sched_setaffinity");
|
||||
assert(false);
|
||||
}
|
||||
print_affinity();
|
||||
printf("sched_getcpu = %d\n", sched_getcpu());
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
51
userland/linux/sched_getaffinity_threads.c
Normal file
51
userland/linux/sched_getaffinity_threads.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/* https://github.com/cirosantilli/linux-kernel-module-cheat#gdb-step-debug-multicore */
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void* main_thread_0(void *arg) {
|
||||
int i;
|
||||
cpu_set_t mask;
|
||||
CPU_ZERO(&mask);
|
||||
CPU_SET(*((int*)arg), &mask);
|
||||
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
|
||||
i = 0;
|
||||
while (true) {
|
||||
printf("0 %d\n", i);
|
||||
sleep(1);
|
||||
i++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* main_thread_1(void *arg) {
|
||||
int i;
|
||||
cpu_set_t mask;
|
||||
CPU_ZERO(&mask);
|
||||
CPU_SET(*((int*)arg), &mask);
|
||||
sched_setaffinity(1, sizeof(cpu_set_t), &mask);
|
||||
i = 0;
|
||||
while (true) {
|
||||
printf("1 %d\n", i);
|
||||
sleep(1);
|
||||
i++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
enum NUM_THREADS {NUM_THREADS = 2};
|
||||
pthread_t threads[NUM_THREADS];
|
||||
int thread_args[NUM_THREADS];
|
||||
pthread_create(&threads[0], NULL, main_thread_0, (void*)&thread_args[0]);
|
||||
pthread_create(&threads[1], NULL, main_thread_1, (void*)&thread_args[1]);
|
||||
pthread_join(threads[0], NULL);
|
||||
pthread_join(threads[1], NULL);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user