Commit Graph

337 Commits

Author SHA1 Message Date
Solar Designer 5959cf4af3 LKRG 0.9.0 2021-04-12 21:16:09 +02:00
Solar Designer 1465213ffb Add .mailmap, prevent its export in .gitattributes 2021-04-12 21:15:56 +02:00
Solar Designer 4ca3f42c4a scripts/copy-builtin.sh, test.sh: Add execute permissions 2021-04-12 14:17:34 +02:00
Solar Designer 5cc2e93103 CHANGES: Detail on 32-bit x86 breakage, document SMAP logging bug 2021-04-12 14:12:04 +02:00
Solar Designer 8e3abdb7a8 test.sh: Add comment explaining the script is for out-of-tree 2021-04-12 14:09:33 +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
Solar Designer 45cce0e876 CHANGES: Document major changes since 0.8.1
Co-authored-by: Adam Zabrocki <pi3@pi3.com.pl>
2021-04-10 19:49:40 +02:00
Vitaly Chikunov ed37ed3f3f CI: Boot test using Ubuntu mainline kernel build
Ubuntu have daily builds of mainline kernels at
  https://wiki.ubuntu.com/Kernel/MainlineBuilds
They are built on groovy.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
2021-04-08 12:58:42 +02:00
Vitaly Chikunov 55d538dd15 CI: Boot test on GA using mkosi
Use mkosi to test full system boot with LKRG module loaded early in
initrd. mkosi creates system disk image (quite slow, 5 minutes for
ubuntu focal in my tests, and size is 1.3G), builds lkrg there (using
systemd-nspawn), and finally boots it in qemu. Then we grep boot.log
for possible problems.

Ubuntu is chosen, because it's native to GA. Only successful (for the
test) releases are 'focal' and 'groovy'. It seems mkosi does not support
'hirsute' (yet, failure installing packages into image). Also, 'bionic'
does not build lkrg properly, but mkosi works good. Older Ubuntu seems
to not have systemd, which is a hard mkosi requirement.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
2021-04-03 13:39:19 +02:00
Vitaly Chikunov d1ded32cfa Add mkosi support
mkosi is systemd's boot test tool. This support is mainly for GA CI
to implement full boot tests (on Ubuntu). But, this would be useful
on it's own for experiments with lkrg on all mkosi supported distros.

I support only bios (grub) BootProtocol without unified kernel.

- .gitignore updated to exclude mkosi artifacts (otherwise they could
  recursively go into created image causing disk full error).
- mkosi.default is mkosi config pre-configured for ubuntu focal, you can
  overwrite this with command line options.
- mkosi.build is script to build lkrg and install it into DESTDIR.
- mkosi.postinst hook updates initrd to include and insmod lkrg and
  grub to remove 'quiet' cmdline option.

Signed-off-by: Vitaly Chikunov <vt@altlinux.org>
2021-04-03 13:39:19 +02:00
F0x1fy a42b4cf800 README: Fixed punctuation, word choice, and word flow
Made a few minor changes, particularly to punctuation, but also to word
choice and word flow. For example, "might or might not" is more
uncomfortable to say and read than "may or may not" as the ending "t"
requires a stop. I believe these changes **may** (heh) increase readability
marginally. I also did not go through and check the commands and their
descriptions.
2021-03-30 01:20:51 +02:00
F0x1fy bdfc5fd657 README: Restructured the sentence 2021-03-30 01:20:51 +02: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 b42ef9c1d5 scripts: double-quote variables and expansions, copy file with archive mode 2021-03-04 18:40:55 +01:00
Vladimir D. Seleznev ffdc3c62c2 lkrg-bootup.sh: exec P_LKRG_SYSTEMD 2021-03-04 18:40:55 +01:00
Vladimir D. Seleznev 9a42e57911 scripts: do not use P_PWD 2021-03-04 18:40:55 +01:00
Vladimir D. Seleznev 70b30cb579 lkrg-bootup.sh: canonicalize readlink 2021-03-04 10:34:12 +01:00
Vladimir D. Seleznev d114423af4 lkrg-bootup.sh: wrap variables with double quotes to prevent word splitting 2021-03-04 10:34:12 +01:00
0xC0ncord 18c927ef53 copy-builtin: fix syntax error
This fixes the following error when using bash:
bash: [: missing `]'
2021-03-03 11:31:42 +01: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
RageLtMan abaca2fc72 Copy-builtin: optionally use git via LKRG_USE_GIT
As pointed out by Solar Designer, git is somewhat non-germaine to
the script as it's really part of an external workflow replicated
across out-of-tree repos used by Semper Victus for the trees which
we maintain in-house. The meat of the function in ths script puts
data into the right places within a kernel tree, with the commit
semantics being an ancillary function.

Make all of the git operations optional by enclosing them in tests
for the existence of the LKRG_USE_GIT variable.
2021-02-25 16:54:28 +00:00
0xC0ncord 64491151a9 copy-builtin: avoid errors when committing changes
If the source tree is not a git repository or git is not installed,
avoid throwing errors.
2021-02-22 20:09:15 +01: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
Solar Designer 993be4b624 Travis CI: Disable the only remaining arm64 build
Unfortunately, "sudo m-a prepare" keeps failing on Travis' arm64 hosts
most of the time lately.
2021-02-07 14:09:56 +01: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
RageLtMan 226ab5ed2e Copy-builtin: use bash and correct depends 2021-01-18 13:28:37 +01: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