Open
fix 6.x kernel compile error #124
AtomGit-BotAdd on Feb 19, 2024
fix 6.x kernel compile error #124
Open
Merged fromrefs/pull/124/headintomaster
Total: 6 files changed+63-60
| @@ -26,8 +26,10 @@ ifndef KDIR | |||
| 26 | KDIR=$(KERNEL_SRC) | 26 | KDIR=$(KERNEL_SRC) |
| 27 | endif | 27 | endif |
| 28 | 28 | ||
| 29 | +ifneq ($(VERSION), 6) | ||
| 29 | KBUILD_EXTRA_SYMBOLS += $(PWD)/../core/Module.symvers | 30 | KBUILD_EXTRA_SYMBOLS += $(PWD)/../core/Module.symvers |
| 30 | export KBUILD_EXTRA_SYMBOLS | 31 | export KBUILD_EXTRA_SYMBOLS |
| 32 | +endif | ||
| 31 | 33 | ||
| 32 | all: | 34 | all: |
| 33 | $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules KCPPFLAGS="${cflags-y}" | 35 | $(MAKE) -C $(KERNEL_SRC) M=$(PWD) modules KCPPFLAGS="${cflags-y}" |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <linux/list.h> | 10 | #include <linux/list.h> |
| 11 | #include <linux/spinlock.h> | 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/slab.h> | 12 | #include <linux/slab.h> |
| 13 | +#include <linux/version.h> | ||
| 13 | #include <time.h> | 14 | #include <time.h> |
| 14 | #include "secDetector_mc_kmodule_baseline.h" | 15 | #include "secDetector_mc_kmodule_baseline.h" |
| 15 | #include "secDetector_response.h" | 16 | #include "secDetector_response.h" |
| @@ -123,7 +124,9 @@ void check_kmodule_baseline(void) | |||
| 123 | if (module_kset == NULL) | 124 | if (module_kset == NULL) |
| 124 | return; | 125 | return; |
| 125 | 126 | ||
| 127 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 0, 0) | ||
| 126 | mutex_lock(&module_mutex); | 128 | mutex_lock(&module_mutex); |
| 129 | +#endif | ||
| 127 | spin_lock(&module_kset->list_lock); | 130 | spin_lock(&module_kset->list_lock); |
| 128 | list_for_each_entry(k, &module_kset->list, entry) { | 131 | list_for_each_entry(k, &module_kset->list, entry) { |
| 129 | if (k->name == NULL) | 132 | if (k->name == NULL) |
| @@ -137,7 +140,9 @@ void check_kmodule_baseline(void) | |||
| 137 | break; | 140 | break; |
| 138 | } | 141 | } |
| 139 | spin_unlock(&module_kset->list_lock); | 142 | spin_unlock(&module_kset->list_lock); |
| 143 | +#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 0, 0) | ||
| 140 | mutex_unlock(&module_mutex); | 144 | mutex_unlock(&module_mutex); |
| 145 | +#endif | ||
| 141 | 146 | ||
| 142 | report_kmodule_baseline(); | 147 | report_kmodule_baseline(); |
| 143 | free_kmodule_baseline(); | 148 | free_kmodule_baseline(); |
| @@ -38,6 +38,7 @@ | |||
| 38 | #include <linux/ctype.h> | 38 | #include <linux/ctype.h> |
| 39 | #include <linux/cred.h> | 39 | #include <linux/cred.h> |
| 40 | #include <linux/kthread.h> | 40 | #include <linux/kthread.h> |
| 41 | +#include <linux/version.h> | ||
| 41 | #include <string.h> | 42 | #include <string.h> |
| 42 | 43 | ||
| 43 | #include "secDetector_manager.h" | 44 | #include "secDetector_manager.h" |
| @@ -84,6 +85,46 @@ struct process_info { | |||
| 84 | int umask; | 85 | int umask; |
| 85 | }; | 86 | }; |
| 86 | 87 | ||
| 88 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) | ||
| 89 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 7, 0) | ||
| 90 | +struct file *get_mm_exe_file(struct mm_struct *mm) | ||
| 91 | +{ | ||
| 92 | + struct file *exe_file; | ||
| 93 | + | ||
| 94 | + rcu_read_lock(); | ||
| 95 | + exe_file = get_file_rcu(&mm->exe_file); | ||
| 96 | + rcu_read_unlock(); | ||
| 97 | + return exe_file; | ||
| 98 | +} | ||
| 99 | +#else | ||
| 100 | +struct file *get_mm_exe_file(struct mm_struct *mm) | ||
| 101 | +{ | ||
| 102 | + struct file *exe_file; | ||
| 103 | + | ||
| 104 | + rcu_read_lock(); | ||
| 105 | + exe_file = rcu_dereference(mm->exe_file); | ||
| 106 | + if (exe_file && !get_file_rcu(exe_file)) | ||
| 107 | + exe_file = NULL; | ||
| 108 | + rcu_read_unlock(); | ||
| 109 | + return exe_file; | ||
| 110 | +} | ||
| 111 | +#endif | ||
| 112 | +struct file *get_task_exe_file(struct task_struct *task) | ||
| 113 | +{ | ||
| 114 | + struct file *exe_file = NULL; | ||
| 115 | + struct mm_struct *mm; | ||
| 116 | + | ||
| 117 | + spin_lock(&task->alloc_lock); | ||
| 118 | + mm = task->mm; | ||
| 119 | + if (mm) { | ||
| 120 | + if (!(task->flags & PF_KTHREAD)) | ||
| 121 | + exe_file = get_mm_exe_file(mm); | ||
| 122 | + } | ||
| 123 | + spin_unlock(&task->alloc_lock); | ||
| 124 | + return exe_file; | ||
| 125 | +} | ||
| 126 | +#endif | ||
| 127 | + | ||
| 87 | char *get_process_path(struct task_struct *p, char *pathname, int len) | 128 | char *get_process_path(struct task_struct *p, char *pathname, int len) |
| 88 | { | 129 | { |
| 89 | char *process_path = NULL; | 130 | char *process_path = NULL; |
| @@ -17,6 +17,7 @@ | |||
| 17 | #include <linux/module.h> | 17 | #include <linux/module.h> |
| 18 | #include <linux/kmemleak.h> | 18 | #include <linux/kmemleak.h> |
| 19 | #include <linux/fs.h> | 19 | #include <linux/fs.h> |
| 20 | +#include <linux/version.h> | ||
| 20 | 21 | ||
| 21 | static unsigned long rb_datasz; | 22 | static unsigned long rb_datasz; |
| 22 | static unsigned long rb_mask; | 23 | static unsigned long rb_mask; |
| @@ -287,7 +288,11 @@ static int ringbuffer_mmap(struct file *flip, struct vm_area_struct *vma) | |||
| 287 | vma->vm_end - vma->vm_start != PAGE_SIZE) | 288 | vma->vm_end - vma->vm_start != PAGE_SIZE) |
| 288 | return -EPERM; | 289 | return -EPERM; |
| 289 | } else { | 290 | } else { |
| 291 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) | ||
| 292 | + vm_flags_clear(vma, VM_MAYWRITE); | ||
| 293 | +#else | ||
| 290 | vma->vm_flags &= ~VM_MAYWRITE; | 294 | vma->vm_flags &= ~VM_MAYWRITE; |
| 295 | +#endif | ||
| 291 | } | 296 | } |
| 292 | /* remap_vmalloc_range() checks size and offset */ | 297 | /* remap_vmalloc_range() checks size and offset */ |
| 293 | return remap_vmalloc_range(vma, g_rb, vma->vm_pgoff + RINGBUF_PGOFF); | 298 | return remap_vmalloc_range(vma, g_rb, vma->vm_pgoff + RINGBUF_PGOFF); |
| @@ -366,8 +371,11 @@ int __init secDetector_ringbuf_dev_init(unsigned int rb_sz) | |||
| 366 | ret = major; | 371 | ret = major; |
| 367 | goto error_free; | 372 | goto error_free; |
| 368 | } | 373 | } |
| 369 | - | 374 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0) |
| 375 | + class = class_create(MODULE_DEVICE); | ||
| 376 | +#else | ||
| 370 | class = class_create(THIS_MODULE, MODULE_DEVICE); | 377 | class = class_create(THIS_MODULE, MODULE_DEVICE); |
| 378 | +#endif | ||
| 371 | if (IS_ERR(class)) { | 379 | if (IS_ERR(class)) { |
| 372 | ret = PTR_ERR(class); | 380 | ret = PTR_ERR(class); |
| 373 | goto error_class_create; | 381 | goto error_class_create; |
| @@ -398,4 +406,4 @@ void __exit secDetector_ringbuf_dev_exit(void) | |||
| 398 | class_destroy(class); | 406 | class_destroy(class); |
| 399 | unregister_chrdev(major, MODULE_DEVICE); | 407 | unregister_chrdev(major, MODULE_DEVICE); |
| 400 | ringbuf_free(g_rb); | 408 | ringbuf_free(g_rb); |
| 401 | -} | 409 | +} |
| @@ -1,7 +1,7 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.22) | 1 | cmake_minimum_required(VERSION 3.22) |
| 2 | 2 | ||
| 3 | add_subdirectory(ebpf) | 3 | add_subdirectory(ebpf) |
| 4 | -set(CMAKE_CXX_STANDARD 11) | 4 | +set(CMAKE_CXX_STANDARD 17) |
| 5 | project(observer_agent VERSION 1.0 LANGUAGES CXX) | 5 | project(observer_agent VERSION 1.0 LANGUAGES CXX) |
| 6 | set(GRPC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/grpc_comm) | 6 | set(GRPC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/grpc_comm) |
| 7 | add_custom_target(grpc_demo ALL | 7 | add_custom_target(grpc_demo ALL |
| @@ -16,5 +16,5 @@ target_include_directories(secDetectord PUBLIC service grpc_comm ${CMAKE_SOURCE_ | |||
| 16 | target_link_libraries(secDetectord ${CMAKE_CURRENT_BINARY_DIR}/ebpf/.output/fentry.o) | 16 | target_link_libraries(secDetectord ${CMAKE_CURRENT_BINARY_DIR}/ebpf/.output/fentry.o) |
| 17 | target_link_libraries(secDetectord ${CMAKE_CURRENT_BINARY_DIR}/ebpf/file_ebpf/.output/file_fentry.o) | 17 | target_link_libraries(secDetectord ${CMAKE_CURRENT_BINARY_DIR}/ebpf/file_ebpf/.output/file_fentry.o) |
| 18 | target_link_libraries(secDetectord ${GRPC_PATH}/comm_api.pb.o ${GRPC_PATH}/comm_api.grpc.pb.o) | 18 | target_link_libraries(secDetectord ${GRPC_PATH}/comm_api.pb.o ${GRPC_PATH}/comm_api.grpc.pb.o) |
| 19 | -target_link_libraries(secDetectord protobuf grpc++ grpc absl_synchronization uuid) | 19 | +target_link_libraries(secDetectord protobuf grpc++ grpc absl_synchronization absl_log_internal_message absl_log_internal_check_op absl_cord absl_cordz_info absl_cordz_functions absl_cordz_handle gpr uuid) |
| 20 | target_link_libraries(secDetectord z elf bpf) | 20 | target_link_libraries(secDetectord z elf bpf) |
| @@ -17,8 +17,8 @@ | |||
| 17 | HOST_SYSTEM = $(shell uname | cut -f 1 -d_) | 17 | HOST_SYSTEM = $(shell uname | cut -f 1 -d_) |
| 18 | SYSTEM ?= $(HOST_SYSTEM) | 18 | SYSTEM ?= $(HOST_SYSTEM) |
| 19 | CXX = g++ | 19 | CXX = g++ |
| 20 | -CPPFLAGS += `pkg-config --cflags protobuf grpc` | 20 | +CPPFLAGS += `pkg-config --cflags protobuf grpc` -std=c++17 |
| 21 | -CXXFLAGS += -std=c++11 -fPIC | 21 | +CXXFLAGS += -fPIC |
| 22 | ifeq ($(SYSTEM),Darwin) | 22 | ifeq ($(SYSTEM),Darwin) |
| 23 | LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\ | 23 | LDFLAGS += -L/usr/local/lib `pkg-config --libs protobuf grpc++`\ |
| 24 | -pthread\ | 24 | -pthread\ |
| @@ -38,7 +38,7 @@ PROTOS_PATH = ./protos | |||
| 38 | 38 | ||
| 39 | vpath %.proto $(PROTOS_PATH) | 39 | vpath %.proto $(PROTOS_PATH) |
| 40 | 40 | ||
| 41 | -all: system-check client_pub_demo client_sub_demo server_demo | 41 | +all: client_pub_demo client_sub_demo server_demo |
| 42 | 42 | ||
| 43 | client_pub_demo: comm_api.pb.o comm_api.grpc.pb.o client.o client_pub_demo.o | 43 | client_pub_demo: comm_api.pb.o comm_api.grpc.pb.o client.o client_pub_demo.o |
| 44 | $(CXX) $^ $(LDFLAGS) -o $@ | 44 | $(CXX) $^ $(LDFLAGS) -o $@ |
| @@ -58,56 +58,3 @@ server_demo: comm_api.pb.o comm_api.grpc.pb.o server.o server_demo.o | |||
| 58 | clean: | 58 | clean: |
| 59 | rm -f *.o *.pb.cc *.pb.h server_demo client_sub_demo client_pub_demo | 59 | rm -f *.o *.pb.cc *.pb.h server_demo client_sub_demo client_pub_demo |
| 60 | 60 | ||
| 61 | - | ||
| 62 | -# The following is to test your system and ensure a smoother experience. | ||
| 63 | -# They are by no means necessary to actually compile a grpc-enabled software. | ||
| 64 | - | ||
| 65 | -PROTOC_CMD = which $(PROTOC) | ||
| 66 | -PROTOC_CHECK_CMD = $(PROTOC) --version | grep -q libprotoc.3 | ||
| 67 | -PLUGIN_CHECK_CMD = which $(GRPC_CPP_PLUGIN) | ||
| 68 | -HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false) | ||
| 69 | -ifeq ($(HAS_PROTOC),true) | ||
| 70 | -HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false) | ||
| 71 | -endif | ||
| 72 | -HAS_PLUGIN = $(shell $(PLUGIN_CHECK_CMD) > /dev/null && echo true || echo false) | ||
| 73 | - | ||
| 74 | -SYSTEM_OK = false | ||
| 75 | -ifeq ($(HAS_VALID_PROTOC),true) | ||
| 76 | -ifeq ($(HAS_PLUGIN),true) | ||
| 77 | -SYSTEM_OK = true | ||
| 78 | -endif | ||
| 79 | -endif | ||
| 80 | - | ||
| 81 | -system-check: | ||
| 82 | -ifneq ($(HAS_VALID_PROTOC),true) | ||
| 83 | - @echo " DEPENDENCY ERROR" | ||
| 84 | - @echo | ||
| 85 | - @echo "You don't have protoc 3.0.0 installed in your path." | ||
| 86 | - @echo "Please install Google protocol buffers 3.0.0 and its compiler." | ||
| 87 | - @echo "You can find it here:" | ||
| 88 | - @echo | ||
| 89 | - @echo " https://github.com/protocolbuffers/protobuf/releases/tag/v3.0.0" | ||
| 90 | - @echo | ||
| 91 | - @echo "Here is what I get when trying to evaluate your version of protoc:" | ||
| 92 | - @echo | ||
| 93 | - -$(PROTOC) --version | ||
| 94 | - @echo | ||
| 95 | - @echo | ||
| 96 | -endif | ||
| 97 | -ifneq ($(HAS_PLUGIN),true) | ||
| 98 | - @echo " DEPENDENCY ERROR" | ||
| 99 | - @echo | ||
| 100 | - @echo "You don't have the grpc c++ protobuf plugin installed in your path." | ||
| 101 | - @echo "Please install grpc. You can find it here:" | ||
| 102 | - @echo | ||
| 103 | - @echo " https://github.com/grpc/grpc" | ||
| 104 | - @echo | ||
| 105 | - @echo "Here is what I get when trying to detect if you have the plugin:" | ||
| 106 | - @echo | ||
| 107 | - -which $(GRPC_CPP_PLUGIN) | ||
| 108 | - @echo | ||
| 109 | - @echo | ||
| 110 | -endif | ||
| 111 | -ifneq ($(SYSTEM_OK),true) | ||
| 112 | - @false | ||
| 113 | -endif | ||