Commit graph

380 commits

Author SHA1 Message Date
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
35b47c2cef
Minor fixes for TRACEPOINT
This commit addresses comments for cccc01dd05
2021-04-26 12:47:49 -04:00
Adam_pi3
cccc01dd05
Add support for x86/static_call used by TRACEPOINTs since kernel 5.10+
Since kernel 5.10 tracepoints don't use JUMP_LABEL engine for .text kernel
modification.
Linux kernel introduced 'static_call' as a replacement for global function
pointers. It uses code patching to allow direct calls to be used instead of
indirect calls. Related Linux kernel commits:

e6d6c071f2
1e7e478838 (diff-d7873f00dcd8c46df3e1e57b3225ff91036c83d5d7339d410b468418fc9a32a4)

Currently, only x86(-64) architecture has implementation for static calls.
This commit should address #69
2021-04-23 01:36:29 -04:00
Adam_pi3
5113a5b53c
Fix SELinux initialization logic
As a result of the SELinux refactoring work:
59bed0a813 (diff-54fcdb751a789548b7a7498e3e341d4a3561b3b3ab7dee1a4dcbef0040fe6bfe)

we have introduced a gentle bug for kernel >= 4.17. The original SELinux state
was not snapshotted during the initialization phase. This commit fixes the
described issue and #72
2021-04-20 02:01:29 +00: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
Solar Designer
e56e67fe95 Avoid producing too many build errors/warnings without CONFIG_JUMP_LABEL
We currently don't support such configuration anyway, but we prefer the build
to fail cleaner on the explicit #error only.

Fixes #70
2021-04-18 21:29:39 +02:00
Adam_pi3
b913995bbc
Fix the P_PCFI_X86_SMAP bit definition
P_PCFI_X86_SMAP is defined to 3 but should be 4, so that it's a separate bit
not clashing with others. This commit fixes the problem and addresses #64
2021-04-11 16:18:18 +00:00
Adam_pi3
7553c35219
Fix SELinux validation on kernels < 4.17
Latest SELinux changes:
59bed0a813

introduced two SELinux problems on kernels < 4.17. First, LKRG won't compile
on such kernels due to function name mismatch. However, even if this would be
fixed there is another issue in the same function. Instead of overwriting the
value of SELinux state itself, the code was overwriting pointer. The second bug
could never be triggered because of the first bug failing LKRG compilation for
such kernels.
This commit fixes both problems and addresses #60
2021-04-11 16:07:57 +00:00
Adam_pi3
3c2611f9ef
Add support for RHEL 8.4
RHEL 8.4+ kernel backport 5.9+ vanilla kernel changes for
'struct subprocess_info'. This commit addresses this issue.
It also fixes #59
2021-03-28 20:57:00 +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
Adam_pi3
b9ff71131d
Add WARN message when LKRG can't enforce SELinux validation
It can happen when CONFIG_GCC_PLUGIN_RANDSTRUCT is used. Additionally,
this commit reorganize the code to keep SELinux initialization together.
2021-03-21 00:50:22 +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
Mariusz Zaborski
ca485a03fc SELinux: Recognize that the "disabled" field is optional
Starting with Linux 5.6 a new kernel configuration option was
introduced: CONFIG_SECURITY_SELINUX_DISABLE.
Reflect in LKRG selinux_struct that the SELinux "disabled"
field is optional for these newer kernels.
2021-03-19 15:17:11 -07:00
Mariusz Zaborski
59bed0a813 SELinux: Refactor state monitoring
Introduce a new API for SELinux state monitoring.
Move the API to a separate file.
2021-03-19 15:17:11 -07:00
Adam_pi3
7092d7aa0c
Add support for RHEL 8.3
RHEL 8.3 backported modern *_JUMP_LABEL logic to own kernels. This commit
add supports for it.
2021-03-05 15:19:07 -05:00
Vladimir D. Seleznev
2a65bdf58e Fix compilation fo kernel 5.11 on arm64
Since kernel commit 3d2403fd10a1dbb359b154af41ffed9f2a7520e8 there is no
more addr_limit on arm64.
2021-03-02 19:41:13 +00:00
Adam_pi3
007093a5c7
Minor cleanup 2021-02-19 13:52:24 -05:00
Adam_pi3
8a3aaa65c0
Fix LKRG on x86 32 bits arch (IA-32)
Since Linux kernel 5.7 SYSCALL_WRAPPER's magic was backported to x86 (32
bits) as well. This commit bring the support for it.
However, regardless of the SYSCALL_WRAPPER's magic LKRG was broken on IA-32
and this commit "resurrects" such support. It also addresses #49 and #46
2021-02-19 02:28:20 -05: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
da571d3e8a
Suppress warning messages on non-debug builds
Regardless of the fix for #47 (156d2bab39),
LOCKDEP might still report warning messages. This commit silnce them on
non-debug build. If P_LKRG_DEBUG_BUILD is enabled, such information will
be still available.
2021-02-11 21:25:42 -05:00
Adam_pi3
156d2bab39
Add missing synchronization with kprobe optimizer
If kernel is compiled with CONFIG_OPTPROBES we must synchronize with kprobe
optimizer during the process of creating database. LKRG places many
kretprobes which modifies .text section. In the standard scenario after
placing the kprobes, LKRG can safely calculate the hash of all .text
sections. However, if CONFIG_OPTPROBES is enabled, placed kprobes could be
optimized. Optimization modifies .text seciton by converting kprobes into
FTRACE which using different hooking mechanism. If LKRG is in the process
of building the hash database and optimizer is running parallel, at least
we could have FP and at most deadlock.
This fix addresses described issue and reported bug #47
2021-02-11 15:12:33 -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
e43d2dd525
Fix security_bprm_committed_creds hook
security_bprm_committed_creds does not return any value (void). LKRG's old
logic for handling exec* family verified return code. This is an incorrect
behavior for the current design. Fix it.
2021-01-19 20:39:56 -05:00
Adam_pi3
1a72c11cf5
Disable ADDR_LIMIT verification during capable() 2021-01-19 01:03:10 -05:00
Adam_pi3
d3276d45e7
Modify the logic how LKRG tracks the exec syscalls
Since kernel 5.8 function search_binary_handler is not exported anymore.
On the aggressively optimized kernels it is possible that
`search_binary_handler` can be inlined. However, GCC can splits the
function to put the big part in its own function, which receives as a name
the original function name plus .part + .<some number>, and inlines the
rest in other functions.

This is a very problematic behavior from the LKRG point of view and was
reported as #41 and #45. This commit fixes the problem by replacing the
'search_binary_handler' (or 'do_execveat_common') hook with
security_bprm_committing_creds and security_bprm_committed_creds.
Additionally, this change is desired from the security point of view.
2021-01-19 00:42:25 -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
c5222df2db
Optimize initialization state variable 2021-01-18 19:50:03 -05:00
Solar Designer
50285b0516 Use module_init() and not late_initcall_sync() when building as module
We switched to using late_initcall_sync() in order to have LKRG initialize
sufficiently late when it's linked into the kernel.  That change was a
no-op when building/loading LKRG as a module on recent kernels, because
their module.h defines late_initcall_sync() as an alias for module_init().
However, it broke LKRG on some older kernels, where late_initcall_sync()
wasn't defined for modules at all.

This commit fixes that by explicitly using module_init() when building LKRG
as a module.  This change is a no-op on recent kernels.

Fixes #37, updates ddc14c6544
2021-01-17 18:28:01 +01:00
RageLtMan
139645fe0d CONFIG_PAX_RANDKSTACK ^ P_VERIFY_ADDR_LIMIT
There are unofficial versions of RANDKSTACK patches floating about
the web, including in VMWare's PhotonOS.
The randomized stack addresses conflict with LKRG's ADDR_LIMIT
checks a la:
```
[  195.272462] [p_lkrg] <Exploit Detection> Detected ADDR_LIMIT
segment corruption! process[552 | sysctl] has different segment
address! [7ffffffff000 vs ffffffffffffffff]
```

Address this by ensuring that P_VERIFY_ADDR_LIMIT does not get
defined when CONFIG_PAX_RANDKSTACK is enabled.

This is a strange edge-case, and normally wouldn't be submitted as
a pull request to upstream projects, except that users seeking to
harden their kernels with public code are likely to run across
LKRG and some links to the PhotonOS patches or similar extracts
from Grsecurity's old patchsets. The commit is a no-op in 99% of
cases, but may result in one less bug report over the next decade.
2021-01-17 16:34:45 +00:00
Adam_pi3
b2d193b5ec
[FTRACE] Add dependency on CONFIG_DYNAMIC_FTRACE
Based on #40 it looks like some people compile the kernel with
CONFIG_FTRACE and CONFIG_FUNCTION_TRACER but don't enable
CONFIG_DYNAMIC_FTRACE. Let's try to check that in this commit.
2021-01-14 21:42:04 -05:00
Adam_pi3
8814ebe804
Fix issue #38 2021-01-13 17:46:11 -05:00
Adam_pi3
9dcee1e948
Add support for the kernels 5.11+
Since kernel 5.11, on x86(-64) architecture TIF_SECCOMP flag is not used
anymore to track SECCOMP state per thread. This commit updates the code
accordingly
2021-01-13 00:04:26 -05:00
Adam_pi3
f7335cf712
Remove validation of waking-up tasks
Current LKRG's architecture has small benefits from validation waking-up
tasks. However, it might have noticeable performance impact. After this
commit, 'pint_validate' option 2 has the same meaning as option 1.
2021-01-08 16:18:57 -05:00
Adam_pi3
6cf9e241bb
Fix an issue discussed at #35
Since kernel 5.8 'search_binary_handler' function does not have EXPORT
attribute and LKRG can't place correct hooks. In such case use
'do_execveat_common' function instead.
2021-01-04 21:52:06 -05:00
Adam_pi3
e0547c4d7c
Fix compilation for kernel 5.10+ 2020-12-30 17:36:13 -05:00
Adam_pi3
1299583b56
Fix commit: c049fa5695 2020-12-30 15:35:43 -05:00
Adam_pi3
c049fa5695
Replace exec* syscall hooks with 'search_binary_handler'
We can significantly simplify our logic and hook only one function
instead of every syscall's entrypoint for exec*.
2020-12-29 17:47:46 -05:00
Adam_pi3
d3118e45e6
Minor fixes 2020-12-24 17:17:02 -05:00
Adam_pi3
2de8450633
Snapshot ADDR_LIMIT as task's attribute
Many exploits use culnerability to corrupt 'addr_limit' and achieve full R/W primitive in the kernel. This is a 'known' technique. We can't verify 'addr_limit' as part of normal verification process because kernel might legitimately modify it via call set_fs(KERNEL..). However, there are places where we can enforce such policy, e.g. during generic_permission() or capable() hook as well as at the syscall hook. I'm adding such verification on execve() syscall as well. Since kernel 5.10 on x86 platform set_fs/get_fs API is removed (and addr_limit variable) but it's not the case for ARM architecture. Moreover, many Android exploit relies on 'addr_limit' corruption. This beta-version of 'addr_limit' verification can be effective and important feature.
2020-12-23 19:49:41 -05:00
Adam_pi3
89697f32bc
Minor simplification
Minor simplifaction for dynamic support of OverlayFS
2020-12-22 17:30:56 -05:00
Adam_pi3
9ccf69d06d
Add support for the delayed docker/container initialization
Docker/containers require 'overlay' or 'overlay2' module for supporting storage/scratchspaces. To be able to correctly support docker environment LKRG needs to hook 'ovl_create_or_link' function from these modules. However, it is possible that during LKRG initialization, 'overlay[2]' is not loaded yet because of the delayed docker/containers initialization. In such case we will produce FP. This commit changes the requirement of loading 'overlay[2]' module before loading LKRG. Now, we are dynamically add necessary hooks.
2020-12-21 15:43:18 -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
Adam_pi3
ddc14c6544
Replace module_init with late_initcall_sync
We want to be loaded after Linux kernel is done with the majority of the initialization work. Otherwise, some critical kernel attributes (from LKRG's perspective), like RO data, might be still dynamically modified without informing LKRG. This might produce FP.
2020-12-19 00:56:18 -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
47d6aca4d4
Simplify 'do_exit' hook
We don't need to do such verification here. Process is dying anyway so the impact of verification is low.
2020-12-10 01:38:12 -05:00
Adam_pi3
b175832dba
FTRACE: Simplify 'ftrace_enable_sysctl' hook
We don't need to use extra cycles for that logic
2020-12-10 01:35:07 -05:00
Adam_pi3
6be46f80dd
Fix the logic in security_ptrace_access hook
Function security_ptrace_access() can be called internally by the kernel. We must take this into account.
2020-12-05 12:05:29 -05:00
Adam_pi3
47804120c3
Fix a gentle bug when compiled with P_LKRG_TASK_OFF_DEBUG
P_LKRG_TASK_OFF_DEBUG introduces extra lines of code which was not taken into account for seccomp() and namespace API. This commit fixes it. Additionally, we are adding extra information in case of corruption (dump_stack()).
2020-12-03 15:07:40 -05:00
Adam_pi3
d051bc2802
Fix debug task logic for seccomp
Track child in case of SECCOMP_FILTER_FLAG_TSYNC flag
2020-12-01 16:47:19 -05:00
Adam_pi3
24f4156516
Add task debugging infrastructure
This is a relatively heavy feature. It introduces a possibility of having a 'ring-buffer' per each tracked task in the kernel. Such buffer keeps a history of important events (from LKRG perspective) related to this task
2020-11-29 20:47:47 -05:00
Mariusz Zaborski
7eae8d3101 UMH: Allow all the pathnames listed in p_umh_global
Previously, we'd only allow pathnames present in the filesystem at LKRG
startup. Now we don't require them to be present in the filesystem
2020-11-27 23:30:53 +01:00
Adam_pi3
3cd05c371f
Change locking logic
KPROBE optimizer might replace KPROBE with FTRACE. If we are in the middle of initialization current logic might be incompatible. This patch fixes it.
2020-11-25 02:17:35 -05:00
Solar Designer
6acc31f314 UMH: allow /sbin/request-key as discussed in #11 2020-11-19 12:47:07 +01:00
Adam_pi3
3f76f5148b
Fix forgotten code-cleanup
One of the commit changed the way how we debug the code. Fix forgotten path for compilation with P_LKRG_JUMP_LABEL_STEXT_DEBUG
2020-11-16 18:56:13 -05:00
Adam_pi3
65153f466c
[FTRACE] Add forgoten check for kernel version
'__module_text_address' is not exported since 5.9+
2020-11-16 18:46:13 -05:00
Mariusz Zaborski
645983fbf6 ptrace: replace ptrace kprobes with security_ptrace_access_check 2020-11-09 08:47:20 -08:00
Mariusz Zaborski
5db3f983cb umh: Kill process using the proper SIGKILL signal.
When we detect an invalid binary, we forbid it execution by changing the
subprocess execution path with slashes.
On some kernels, this may not work because we have to map the memory page as
writable. At this moment of execution, we can stop executing the process using
the froce_sig to send signals. The send_sig_info will not arrive at the right
time.

Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-11-06 23:04:56 +01:00
Mariusz Zaborski
ad681d3215 Drop 0x prefix in new code. 2020-11-03 18:42:39 +01: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
9241d2354a
Update JUMP_LABEL and FTRACE debug messages
FTRACE might generate dynamic trampoline which is not part of .text section. This is not abnormal situation anymore.
2020-11-01 21:58:28 -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
Mariusz Zaborski
1f07ebd7fc lkrg: drop the 0x prefix.
Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-10-28 12:28:04 +01:00
Mariusz Zaborski
83b31857f8 Require a zero return from register_kretprobe(), not just non-negative
Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-10-28 12:28:04 +01:00
Mariusz Zaborski
e79c76548f p_ed_pcfi_validate_sp: Validate all conditions before returning
We would want to have in the logs all violations, not just the first one.

Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-10-28 12:28:04 +01:00
Adam_pi3
f9f215d17b
Modify p_text_section_(un)lock API
Move 'module_mutex' under text section (un)lock API.
2020-10-26 23:02:12 -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
0cf7e11bf7
Modify p_lkrg_open* API
We don't need text section lock here
2020-10-25 20:39:38 -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
Mariusz Zaborski
069e10df4d Rework debug logging of entry and return from functions
Use instrumentation.  This reduces source code size by over 10%.

Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-10-19 20:56:38 +02: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
0xC0ncord
a46e7b0ae1 Fix checking of umh in-memory files 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
Adam_pi3
fa7857a7ea
Minor change in deregister function
Move arch-specific kprobe deregistration close to ED.
2020-10-10 21:30:34 -04:00
Solar Designer
1b6d98e123 Detect any changes of UIDs/GIDs, not just lowering of them
The limited detection was an inadvertent leftover from very early stages of
LKRG development.  Reported by Vadim Andreev.
2020-09-18 23:24:43 +02:00
William
372bb0cdbb Log file permissions in octal format 2020-09-18 09:07:54 +02:00
Adam_pi3
ef4d2c5f0b
Change SELinux synchronization logic
Introduce new type of SELinux lock type - p_lkrg_selinux_lock. Verification routine can take this lock only when atomic counter is zero. This means there are no other consumers of SELinux variables
2020-08-31 15:05:00 -04:00
Adam_pi3
6f700b5b08
Add support for RHEL 8.2
New kernels for RHEL 8.2 backported JUMP_LABEL batch-mode feature. Add support for it.
2020-08-27 21:59:16 -04:00
Adam_pi3
a311616633
Introduce P_LKRG_JUMP_LABEL_STEXT_DEBUG compilation flag.
If we want to track all changes in kernel core .text section and when modification happens we want to know which exactly bytes were modified, which offset and what's the symbol corelated to it, we can compile LKRG with this feature. Disabled by default. It's mostly useful for debugging.
2020-08-27 21:55:02 -04:00
Adam_pi3
2404d32a19
Don't disable preemption.
We don't need to disable preemption in this code path.
2020-08-27 21:18:58 -04:00
Adam_pi3
73e923b5b0
Fix failure path
If error appears during database creation, we might deadlock system since we do not unlock text section
2020-08-27 21:15:29 -04:00
Adam_pi3
4adc39fe68
Remove internal JL sync lock
It's OK to remove such lock, since LKRG .text section validation is syncing with the kernel through JL mutex and .text mutex. There is one corner case where these mutexes are not taken by the kernel when new kernel module might be compiled without long nops. In such case, Linux kernel might modify such .text and 'inject' long-nops where is needed, however, it is done when new module has UNFORMED state. UNFORMED modules can't be verified yet so we are fine.
2020-08-25 11:53:52 -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
Vitaly Chikunov
2648a1d607 Various spelling and grammar corrections (#7)
* Various spelling corrections by codespell 1.17.1
* Various grammar corrections

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
Co-authored-by: Solar Designer <solar@openwall.com>
2020-08-19 00:59:35 +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
Adam_pi3
32fc7d27ae
New RHEL kernels define 'struct stack_trace' by themselves. Take this into account 2020-07-15 20:26:23 -04: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
Solar Designer
ccd71872c5 Drop init_module() and delete_module() syscall hooks 2020-07-08 15:26:20 +02:00
Adam_pi3
b3a499e7f6
Fix user-triggerable Oops (dereference of a near-NULL pointer) on newer kernels with new syscall implementation. Found by Jason A. Donenfeld. 2020-07-04 16:11:17 -04:00
Adam_pi3
ec595f555b
It was reported that in a very slow devices, some of the busy loop of locking/unlocking text_mutext is too tight and optimize_kprobe() can't win the race of getting text_mutex. This simple patch helps to solve that problem. 2020-06-30 12:11:10 -04:00
Adam_pi3
7ee25605d5
Rename profile names 2020-06-23 18:19:32 -04:00
Adam_pi3
73ec77654f
Make msr_validate=1 only in paranoid profile 2020-06-23 14:47:48 -04:00
Adam_pi3
07eb00cee1
Before leaving process integrity verification routine in paranoid mode, verify current task 2020-06-22 19:00:28 -04:00
Adam_pi3
8e4d93380e
Set default knobs and profile values to: profile_validate=3 and profile_enforce=2 2020-06-21 12:46:23 -04:00
Adam_pi3
97cb12a801
Cleanup commit 2020-06-19 13:20:39 -04:00
Adam_pi3
bb3a3a9ec7
Change the logic of detecting a situation when already blocked path sotred in memory is being reexecuted 2020-06-18 14:33:22 -04:00
Adam_pi3
44dc1bb72d
Minor fix 2020-06-17 13:43:59 -04:00
Adam_pi3
75f866d5ca
Minor fix on on-x86 platforms 2020-06-17 13:10:34 -04:00
Adam_pi3
b67e642ee6
Remove misleading comments 2020-06-16 17:45:33 -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
a900c9fa41
[1] Change UMH messages to not be unnecessarily verbose and shouting.
[2] Take into account a special case situation when already blocked path sotred in memory is being reexecuted.
2020-06-14 15:02:20 -04:00
Adam_pi3
9b4dea13da
Change log_level from ERR->WARN for ISRA / CONSPROP. Refactor when the logs are printed 2020-06-13 18:40:13 -04:00
Adam_pi3
72084d989a
Add support for CONSTPROP optimized functions:
- Some of the functions might be optimized by CONSTPROP. However, some of the hooks can still be functional even under CONSTPROP optimized functions.
2020-06-12 17:58:05 -04:00
Adam_pi3
cd62484459
Fix RB-tree deletion logic 2020-06-11 11:28:16 -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
8e660501d8
[x86] Use Boot CPU to verify smXp flags 2020-06-04 16:42:32 -04:00
Adam_pi3
5ddb061719
Fix hiding feature. This code was not correctly adopted to support newly introduced RO page 2020-06-04 13:37:14 -04:00
Adam_pi3
c46108cec6
Add verification whether specific CONFIG_* options are enabled 2020-06-04 12:32:43 -04:00
Adam_pi3
d57b4c0f0e
Since kernel 5.7 "kallsyms_lookup_name" and "kallsyms_on_each_symbol" functions are not exported. This patch (hack) retrives necessary function pointer 2020-06-03 16:28:30 -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
0b0b469739
[1] Fix 'kint_validate = 1' logic
[2] Change timer delete logic
2020-05-28 02:22:04 -04: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
Adam_pi3
5f691f6324
Remove redundant checks for CPU bits 2020-05-20 21:07:40 -04:00
Adam_pi3
37fa35e0cc
Fix typo 2020-05-19 21:40:53 -04:00
Adam_pi3
9077cae8b9
Some more improvements regarding smXp validation 2020-05-18 16:03:29 -04:00
Adam_pi3
dda846107a
Fix logic from the previous PR 2020-05-17 20:23:10 -04:00
Adam_pi3
8639e0e5bf
Improve SMAP/SMEP/WP validation logic 2020-05-17 20:02:56 -04:00
Adam_pi3
228daef326
Remove redundant validation 2020-05-15 22:44:18 +00:00
Adam_pi3
c2c27f077f
Add spinlock synchronizing UMH 2020-05-14 20:53:09 -04:00
Adam_pi3
8c1a55f7e0
Improve performance for flag validation 2020-05-13 17:38:19 -04:00
Adam_pi3
01bfa8e953
[1] Change *_JUMP_LABEL log information from WARN -> INFO
[2] Fix compilation warning (const qualifier)
2020-05-12 14:41:20 -04:00
Adam_pi3
2ac6980b9c
Fix type. Add 'const' qualifier 2020-05-12 04:22:27 +00:00
Mariusz Zaborski
51de292a73
Remove rest of the duplicated code.
Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-05-11 09:42:14 +02:00
Mariusz Zaborski
6a670f48a6
Add macro to genereate install func.
Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-05-11 09:42:10 +02: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
Mariusz Zaborski
ea08cd0414
Simplify the initialization of the hooks.
Signed-off-by: Mariusz Zaborski <oshogbo@vexillium.org>
2020-05-11 09:42:00 +02:00
Adam_pi3
3923eec14e
Add SMAP bit verification logic (x86 arch). It is guarded in the same way as SMEP. Two new LKRG sysctl interface are introduced to control verification and enforcement logic:
1) Introduce 'smap_validate' to control if SMAP validation will be performed
       0 - disable SMAP validation
       1 - enable SMAP validation
   6) Introduce 'smap_enforce' to control how LKRG reacts when SMAP validation fails:
       0 - log & accept
       1 - log & restore
       2 - panic() - kill the kernel
2020-05-10 16:56:47 -04:00
Adam_pi3
517961fd43
Add "/sbin/bridge-stp" to whitelisted UMH 2020-05-09 20:02:46 -04:00
Adam_pi3
b3694bfc2a
When LKRG is configured to allow only whitelisted executables to be run by UMH, we might be in the situation of overwriting RO page. Add support for that 2020-05-09 19:56:58 -04:00
Adam_pi3
4ca6cacbba
Fix typo 2020-05-09 17:21:08 -04:00
Adam_pi3
4da623e51e
Change WARN info when 'ovl_create_or_link' can't be hooked. 2020-05-09 16:39:50 -04:00
Adam_pi3
404a3aef32
Mariusz Zaborski and Patrick Schleizer pointed out that LKRG generates kernel panic when SMEP is not supported on x86. Such problem occure after the 2aef433. This commit fixes that issue. 2020-05-09 15:43:43 -04:00
Adam_pi3
fe54765efb
Refactor LKRG process tracking DB. Instead of relying on 1 global spinlock, now we have multiple RW-locks. This commit introduces 512 buckets of RB-trees + RW-locks. This commit significantly increases performance. 2020-05-09 14:30:11 -04:00
Adam_pi3
6bef8f5ee2
Revert 186608db69. Some SL*B allocators do not allow to delete/shutdown cache under IRQs being disabled. In such case we can hit BUG_ON() 2020-05-03 20:12:10 -04:00
Adam_pi3
2aef4330d9
Change SMEP enforcement/validation logic 2020-05-03 14:47:44 -04:00
Adam_pi3
c8e18884a6
Introduce LKRG's tasks integrity database locking API 2020-05-01 21:59:28 -04:00
Adam_pi3
2daf922b6f
[1] Add support for pCFI stackwalk when CONFIG_UNWINDER_ORC is enabled
[2] Whitelist true/false binaries
[3] Modify the logic when stack-page is updated
[4] Add __scm_send hook to verify creds on SCM_CREDENTIALS
2020-04-30 11:25:53 -04:00
Adam_pi3
5278bebd45
Add _trylock() in the ttwu_do_wakeup and __queue_work hook 2020-04-28 14:20:26 -04:00
Adam_pi3
186608db69
Update p_delete_rb_ed_pids() to use IRQ-save locks 2020-04-27 21:10:11 -04:00
Adam_pi3
f2ab708a67
Change the default log-level from P_LKRG_CRIT to P_LKRG_INFO when LKRG detects lost/extra module which is related to the current module activity events (system is stable and not hacked). Maybe we should use P_LKRG_WARN? for now it will be P_LKRG_INFO. 2020-04-25 21:26:52 -04:00
Adam_pi3
2febcf467d
This is a huge change. We completely rewrote communication channel and added a lot of new configuration options. Summary:
1) Introduce 'kint_validate' to control kernel/system integrity logic:
    0 - disabled
    1 - validation is performed only when manually triggered
    3 - validation is performed periodically by timer interrupt and on random events
 2) Introduce 'kint_enforce' to control how LKRG reacts when kernel/system integrity fails:
    0 - log & accept corruption
    1 - log only (for SELinux and CR0.WP violation log & restore original values)
    2 - panic() - kill the kernel

 3) Introduce 'pint_validate' to control tasks validation logic:
    0 - disabled
    1 - validate only currently running tasks
    2 - validate only currently running tasks + task which changes state to RUNNING
    3 - validate all tasks in the system (paranoid mode)
 4) Introduce 'pint_enforce' to control how LKRG reacts when task validation fails:
    0 - log & accept corruption
    1 - kill corrupted task
    2 - panic() - kill the kernel

 5) Introduce 'smep_validate' to control if SMEP validation will be performed
    0 - disable SMEP validation
    1 - enable SMEP validation
 6) Introduce 'smep_enforce' to control how LKRG reacts when SMEP validation fails:
    0 - log & accept
    1 - log & restore
    2 - panic() - kill the kernel

 7) Introduce 'umh_validate' to control if UMH validation will be performed
    0 - disable UMH validation
    1 - allow only whitelited binaries to execute via UMH
    2 - completely block UMH
 8) Introduce 'smep_enforce' to control how LKRG reacts when UMH validation fails:
    0 - log only
    1 - prevent execution
    2 - panic() - kill the kernel

 9) Introduce 'pcfi_validate' to control if pCFI validation will be performed
    0 - disabled
    1 - no stackwalk (weak pCFI)
    2 - fully enabled
10) Introduce 'pcfi_enforce' to control how LKRG reacts when pCFI validation fails:
    0 - log only
    1 - kill corrupted task
    2 - panic() - kill the kernel

11) Rename 'timestamp' to 'interval'
12) Rename 'force_run' to 'trigger'
13) Rename 'clean_message' to 'heartbeat'
14) Rename 'msr_enforce' to 'msr_validate'

15) Option 'hide' stays the same
16) Option 'log_level' stays the same
17) Option 'block_modules' stays the same
2020-04-25 13:41:52 -04:00