Commit graph

74 commits

Author SHA1 Message Date
Adam_pi3
ee41d81599 kINT: Fix synchronization problem
The reported problem with integrity verification on ARM64 (#269)
is a result of a very tight race condition with tracepoints.
Changes which simplify synchronization with JUMP_LABEL engine:
f98da1b17c
affected differently ARM64 platform which made such race possible.
However, potentially the same race problem may exist on x86 and
this commit fixes it and should address #269
2023-10-25 14:56:11 +02:00
fluidog
001768de0c Simplify the use of P_SYM_INIT() 2023-05-19 14:09:29 +02:00
Adam_pi3
3d8e52e90b
Add support for Linux kernel post-6.3
Linux kernel post-6.3 modified the 'struct module' and introduced a new
substructure describing module's memory layout. Additionally, the logic for
dynamic debug (ddebug) has been modified and some of the functions which LKRG
uses are no longer exported. This commit adopts to these post-6.3 changed and
addresses #267
2023-05-03 00:47:51 +00:00
Solar Designer
f32f627950 Simplify our get_random_long() wrapper (only used with old kernels)
Fixes #234
2022-10-17 22:20:20 +02:00
Solar Designer
49a3117542 Rename the module from p_lkrg to lkrg 2022-07-20 17:57:37 +02:00
Solar Designer
e3badfaf73 Logging: Revise, unify, and reduce duplication of logging and responses
Use macros, move logging and enforcement responses from callers into
called functions, remove where it was duplicate.

Unify our log and kernel panic messages.
2022-07-17 20:39:31 +00:00
Solar Designer
f4dc1e31a9 Unify kernel symbol lookups through introduction of a macro 2022-07-17 20:39:31 +00:00
Adam_pi3
bc0037c9f7 Reduce memory overhead generated by kmem_cache_create
This commit brings a few important changes:
 - LKRG has used to leverage SLAB_HWCACHE_ALIGN but memory overhead
   may be too significant for LKRG's use cases
 - Since the kernel 4.5+ we can use SLAB_ACCOUNT to make sure that
   LKRG's caches are standalone
 - Modify the size of pCFI stack buffer cache to be smaller and
   decoupled from the PAGE_SIZE (there is no reason for that)

Additionally, this commit should help addressing #131
2022-06-12 00:00:07 +02:00
lc85446
c9af2ccfaf p_ed_enforce_pcfi: validate the p_task stack but not irq_stack
In the irq context, we should not get the p_task stack from p_regs.
2022-05-06 14:33:36 +02:00
Solar Designer
17752c8cef Fix build on CentOS Stream 9
Fixes #145
2022-01-21 16:46:48 +01:00
Adam_pi3
819b01bdea
Support the "nolkrg" option in LKRG itself
This commit adds support the "nolkrg" boot option and addresses issue #125
2021-11-23 16:27:21 -05:00
Adam_pi3
1eb7e820ec
Replace deprecated CPU-hotplug functions for kernels 5.15+
On the latest kernels (5.15+) get/put_onlince_cpus() API is deprecated
and new synchronization functions must be used. This commit addressed
that issue and #118
2021-10-08 11:16:47 -04:00
Mariusz Zaborski
b7c83fc910 Add more stable kernels with unexported __module_address. 2021-07-21 15:01:38 +02:00
Mariusz Zaborski
5d31ab5d05 Introduce LKRG_P_MODULE_ADDRESS and LKRG_P_MODULE_TEXT_ADDRESS.
No functional change, just simplify the code.
2021-07-21 15:01:38 +02:00
Adam_pi3
a8e2857626
Support various CONFIG_SECCOMP configurations
Some distro kernels for ARM do not enable CONFIG_SECCOMP. Up until this
commit, LKRG presumed that SECCOMP was always enabled/compiled in.
2021-06-06 00:49:19 +00:00
Solar Designer
a516ef4e40 Support unexporting of __module_address* by Linux 5.4.118+
Fixes #93
2021-05-21 09:29:21 +02:00
Adam_pi3
053493bba5
Correctly handle CONFIG_TRIM_UNUSED_KSYMS option
LKRG requires CONFIG_TRIM_UNUSED_KSYMS to be disabled if it should be built
as an out-of-tree kernel module. Otherwise, it can be enabled.
2021-04-18 23:45:47 +00:00
Adam_pi3
523dd2c5b0
Add support for kernels 5.12+
Since this patch:
https://www.mail-archive.com/linuxppc-dev@lists.ozlabs.org/msg182925.html

'module_mutex' and 'find_module' is not exported. We need to manually find it.
This commit addresses described issue.
2021-03-28 19:40:13 +00:00
Mariusz Zaborski
c1a87c3529 SELinux: Disable state monitoring for kernels with randomized structs
New Linux kernels may be built with the CONFIG_GCC_PLUGIN_RANDSTRUCT
option.  This randomly changes the order of fields in certain structures,
including selinux_state.  Currently, LKRG isn't capable to recreate the
structure layout.  Thus, we have to disable LKRG's SELinux monitoring on
kernels built with this option.

CONFIG_GCC_PLUGIN_RANDSTRUCT was introduced to make it harder for attackers
to overwrite particular fields of structures.  LKRG's goal was the same.
So even disabling LKRG's monitoring, we still have some mitigations for
SELinux state overwrites.

We might make LKRG capable to recreate randomized structures in the future.
2021-03-19 15:17:11 -07:00
Adam_pi3
4a3b011804
Allow CONFIG_RT_MUTEXES if CONFIG_PREEMPT_RT is not defined
Commit ec16f777f7 was too restrictive
2021-02-16 00:17:46 -05:00
Adam_pi3
ec16f777f7
Do not support RT kernels
We do not want to support RT kernels (at least not for now). RT kernels are
commonly used in medical and similar devices, where reliability is crucial.
It is safer to to not support RT kernels in LKRG for now.
For more information please read entire discussion at #40.
2021-02-16 00:07:53 -05:00
Adam_pi3
62de614bab
Do not resolve 'native_write_cr4' on AARCH64
Since kernel 5.8+ 'native_write_cr4' must be manually resolved. However, this is X86 specific code which should nbot be executed on other platforms. This commit fixes that and addresses #48
2021-02-11 14:43:49 -05:00
Adam_pi3
7f9742103e
Add dependency on CONFIG_OPTPROBES
If CONFIG_OPTPROBES is not enabled, don't try to sync with kprobe optimizer
2021-02-06 20:33:03 -05:00
Adam_pi3
7d45ab04d3
Introduce a new compilation macro - P_KERNEL_AGGRESSIVE_INLINING
Some custom compilation of the kernel might aggresively inline critical
functions (from LKRG perspective). That's problematic for the project.
However, some of the problems *might* be solved by uncommenting this new
definition (P_KERNEL_AGGRESSIVE_INLINING). Unfortunately, not all of the
problems can be solved by it (at least no for now). You need to experiment.
This can be useful to address issues like #40
2021-02-06 20:15:55 -05:00
Adam_pi3
76a9382bab
Add synchronization with kprobe optimizer
On the aggressively optimized kernels it is possible that kprobe optimizer
won't be fast enough to do the job before LKRG creates own database. This
is problematic because LKRG might snapshot hash of the kernel's .text
section with non-optimized own hooks. As soon as the kprobe optimizer
finishes the job, previously snapshoted hash won't be correct and LKRG will
detect this inconsistency.
To be able to correctly solve this unusual corner case problem, LKRG can
wait for kprobe optimizer before creating database.
2021-01-18 20:00:45 -05:00
Adam_pi3
42d648a592
Enforce verification during internal RO-page modification
When we 'open' our internal RO-page for modification we should verify if integrity of the system is fine. Add verification during this operation.
2020-12-21 15:34:05 -05:00
Vladimir D. Seleznev
cc1aa17a29 define p_kzfree() wrapper
p_kzfree() wraps kzfree() call for kernel < 5.10 and kfree_sensitive()
in the other case. This reflects the changes made in kernel since
23224e45004ed84c8466fd1e8e5860f541187029 and fix the build against
kernel 5.10.
2020-12-16 09:45:36 -08:00
Adam_pi3
f98da1b17c
Simplify synchronization with JUMP_LABEL engine
We don't need to introduce custom LKRG-counter lock to synchronize with JUMP_LABEL engine and avoid potential deadlock with FTRACE. We can check if jump_label lock is taken after acquiring ftrace lock and before taking text_mutex.
This simplification changes p_text_section_(un)lock API.

This also fixes problem reported by Jacek
2020-11-02 16:09:07 -05:00
Adam_pi3
83cb64640a
Fix p_lkrg_counter_lock_trylock() API
spin_trylock() disables preemption under the hood.
2020-11-02 01:43:02 -05:00
Adam_pi3
6857d18aea
Add FTRACE support
1) We are hooking into FTRACE's internal functions to be able to monitor when new modifications are executed and react accordingly.
2) Linux kernel has bugs in FTRACE code. The LKRG may highlight them.
3) We are introducing 'p_state_init' variable to track when full LKRG's initialization is complete.
2020-11-01 21:40:46 -05:00
Adam_pi3
477be5d84c
Add LKRG's counter lock around *JUMP_LABEL engine
1) This is necessary for future FTRACE support. FTRACE is not fully synchronized with JUMP_LABEL (which I think is a buggy logic in the kernel). However, we can manually add such logic. The way how text_mutex is used by both subsystems makes it prone to deadlock if 3rd system wants to sync with both of them.
2) New lock efnorces changes in p_text_section_(un)lock API which we do in the same commit
3) Introduce new LKRG's counter lock API - trylock
4) Add a few minor changes:
  - notrace attribute (probably, we need to add such attributes to majority of our functions)
  - add information about module name in case of KMOD notifier activity
2020-10-30 14:34:53 -04:00
Adam_pi3
cb882e83a7
Remove dependency on 'kernfs' mutex
Current LKRG's design does not need to be dependent on kernfs mutex. Remove it.
2020-10-26 01:56:05 -04:00
Adam_pi3
339f340b61
Change SELinux-type lock to be global LKRG-counter lock
This type of locking idea is useful in other situations as well
2020-10-25 19:54:19 -04:00
0xC0ncord
c7d427de47 Fix resolution and usage of put_seccomp_filter on Linux >= 5.9 2020-10-19 10:19:23 +02:00
0xC0ncord
24d7117647 Dynamically resolve __module_address and __module_text_address on Linux
>= 5.9
2020-10-19 10:19:23 +02:00
Adam_pi3
330bd59fb7
Remove dependency on 'jump_label_mutex'
With the current design of JUMP_LABEL support we do not need to manually take this mutex. Our hooks are deep enough to be protected and integrity routine depends on text mutext
2020-10-11 14:22:29 -04:00
Vitaly Chikunov
072a1b8367 Fix implicit declaration of function 'task_stack_page' on arm
Due to kernel commit f3ac60671954c ("sched/headers: Move task-stack
related APIs from <linux/sched.h> to <linux/sched/task_stack.h>") (Linux
v4.11) `linux/sched/task_stack.h' should be included to access
`task_stack_page'.

Compilation failure is appearing on armv8l arch:

  In file included from ./include/linux/prefetch.h:15,
		   from ./arch/arm/include/asm/atomic.h:12,
		   from ./include/linux/atomic.h:7,
		   from ./include/asm-generic/bitops/lock.h:5,
		   from ./arch/arm/include/asm/bitops.h:243,
		   from ./include/linux/bitops.h:26,
		   from ./include/linux/kernel.h:12,
		   from /usr/src/RPM/BUILD/lkrg-0.8.1/src/modules/exploit_detection/../../p_lkrg_main.h:23,
		   from /usr/src/RPM/BUILD/lkrg-0.8.1/src/modules/exploit_detection/p_exploit_detection.c:18:
  /usr/src/RPM/BUILD/lkrg-0.8.1/src/modules/exploit_detection/p_exploit_detection.c: In function 'p_iterate_processes':
  ./arch/arm/include/asm/processor.h:99:40: error: implicit declaration of function 'task_stack_page'; did you mean 'walk_stackframe'? [-Werror=implicit-function-declaration]
     99 |  ((struct pt_regs *)(THREAD_START_SP + task_stack_page(p)) - 1)
	|                                        ^~~~~~~~~~~~~~~
  /usr/src/RPM/BUILD/lkrg-0.8.1/src/modules/exploit_detection/p_exploit_detection.c:779:30: note: in expansion of macro 'task_pt_regs'
    779 |                p_regs_set_ip(task_pt_regs(p_tmp), -1);
	|                              ^~~~~~~~~~~~
  cc1: some warnings being treated as errors
  make[1]: *** [scripts/Makefile.build:265: /usr/src/RPM/BUILD/lkrg-0.8.1/src/modules/exploit_detection/p_exploit_detection.o] Error 1

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
2020-08-19 17:02:27 +02:00
Adam_pi3
671b079eb9
Since kernel 5.8 function native_write_cr4 is not exported anymore. We could write own function which modifies CR4, however, we don't want to introduce new potential gadgets. Instead we dynamically resolve it to fix this problem. 2020-08-16 13:38:24 -04:00
Solar Designer
8d926b99fe Fix build with non-RHEL kernels broken with the previous commit 2020-08-05 18:19:32 +02:00
Solar Designer
b459b334e6 Don't include linux/cryptohash.h on recent kernels
This fixes LKRG build on Linux 5.8+, which renamed that header file.  Thanks to
Andy Lavr for reporting this problem and suggesting a (different) fix, which
made us revisit our use of that header file.

We only need that header file on older kernels (< 4.4.72 or < RHEL 7.4) for the
one use of md5_transform() in get_random_long().  On newer kernels, we simply
use the kernel-provided get_random_long().  Further, 5.8's crypto/sha.h doesn't
declare md5_transform() anyway (linux/cryptohash.h on much older kernels did).
2020-08-04 21:49:32 +02:00
Solar Designer
cfe3bf406e No longer ask people to contact us for no-CONFIG_JUMP_LABEL support 2020-07-08 16:36:28 +02:00
Adam_pi3
97cb12a801
Cleanup commit 2020-06-19 13:20:39 -04:00
Adam_pi3
841bb4dc47
unwind.h is not available on RHEL7 with old kernel(s). Fix it. 2020-06-14 19:21:09 -04:00
Adam_pi3
ee1263aa66
[1] Change initialization logic for exploit detection module:
- Not all hooks are fatal. If for any reason non-fatal hook can't be placed, continue initialization and print appropriate message
  - If hook is fatal, stop intialization
[2] Add support for ISRA optimized functions:
  - Some of the functions might be optimized by ISRA. However, some of the hooks can still be functional even under ISRA optimized functions.
2020-06-09 17:38:01 -04:00
Adam_pi3
c46108cec6
Add verification whether specific CONFIG_* options are enabled 2020-06-04 12:32:43 -04:00
Adam_pi3
d8869b0e85
Minor fixes - typos, spaces, formating, etc. 2020-06-03 00:22:04 -04:00
Mariusz Zaborski
7a99c0c599 Merged in oshogbo/lkrg-osho/umh (pull request #6)
Rework UMH.

* Introduce nitems for nice array counting scheme.

* Rework the umh whitelist.

    No functional changes intended.

* Sort the UMH and remove dups.
2020-06-03 03:34:30 +00:00
Adam_pi3
11da921d41
Introduce 'profiles' configurable from the sysctl interfact:
1) profile_validate:
     a) 0 (Disabled):
        -> kint_validate = 0 (Disabled)
        -> pint_validate = 0 (Disabled)
        -> pcfi_validate = 0 (Disabled)
        -> umh_validate  = 0 (Disabled)
        -> msr_validate  = 0 (Disabled)
        -> smep_validate = 0 (Disabled)
        -> smap_validate = 0 (Disabled)
     b) 1 (Light):
        -> kint_validate = 1 (Manual trigger only)
        -> pint_validate = 1 (Current task only)
        -> pcfi_validate = 1 (Weak pCFI)
        -> umh_validate  = 1 (Whitelist)
        -> msr_validate  = 0 (Disabled)
        -> smep_validate = 1 (Enabled)
        -> smap_validate = 1 (Enabled)
     c) 2 (Balanced):
        -> kint_validate = 2 (Triggered by timer)
        -> pint_validate = 2 (Current + weaking up task)
        -> pcfi_validate = 1 (Weak pCFI)
        -> umh_validate  = 1 (Whitelist)
        -> msr_validate  = 0 (Disabled)
        -> smep_validate = 1 (Enabled)
        -> smap_validate = 1 (Enabled)
     d) 3 (Moderate):
        -> kint_validate = 3 (Triggered by timer + random events)
        -> pint_validate = 2 (Current + weaking up task)
        -> pcfi_validate = 2 (Full pCFI)
        -> umh_validate  = 1 (Whitelist)
        -> msr_validate  = 1 (Enabled)
        -> smep_validate = 1 (Enabled)
        -> smap_validate = 1 (Enabled)
     e) 4 (Heavy):
        -> kint_validate = 3 (Triggered by timer + random events)
        -> pint_validate = 3 (Paranoid mode - verify all tasks in the system by every hook)
        -> pcfi_validate = 2 (Full pCFI)
        -> umh_validate  = 2 (Full UMH lock-down)
        -> msr_validate  = 1 (Enabled)
        -> smep_validate = 1 (Enabled)
        -> smap_validate = 1 (Enabled)

 2) profile_enforce:
     a) 0 (Log & Accept):
        -> kint_enforce = 0 (Log & accept)
        -> pint_enforce = 0 (Log & accept)
        -> pcfi_enforce = 0 (Log only)
        -> umh_enforce  = 0 (Log only)
        -> smep_enforce = 0 (Log & accept)
        -> smap_enforce = 0 (Log & accept)
     b) 1 (Balanced - selective panic):
        -> kint_enforce = 1 (Log only)
        -> pint_enforce = 1 (Kill task)
        -> pcfi_enforce = 1 (Kill task)
        -> umh_enforce  = 1 (Prevent execution)
        -> smep_enforce = 2 (Panic)
        -> smap_enforce = 2 (Panic)
     c) 2 (Moderate - more panic):
        -> kint_enforce = 2 (Panic)
        -> pint_enforce = 1 (Kill task)
        -> pcfi_enforce = 1 (Kill task)
        -> umh_enforce  = 1 (Prevent execution)
        -> smep_enforce = 2 (Panic)
        -> smap_enforce = 2 (Panic)
     d) 3 (Panic):
        -> kint_enforce = 2 (Panic)
        -> pint_enforce = 2 (Panic)
        -> pcfi_enforce = 2 (Panic)
        -> umh_enforce  = 2 (Panic)
        -> smep_enforce = 2 (Panic)
        -> smap_enforce = 2 (Panic)
2020-05-27 12:24:00 -04:00
Adam_pi3
b1b11e99cf
Group hot RO page settings into a cache line 2020-05-21 15:16:53 -04:00
Mariusz Zaborski
cf7ac2f321
Simplify install/uninstall functions.
This deduplicate a lot of code.

Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-05-11 09:42:06 +02:00