Huawei has seemingly stepped its foot into the kernel-self protection game with the release of HKSP. The patch itself is riddled with bugs and weaknesses and generally lacks any kind of threat model (making its mitigations similar to those present in LKRG where knowledge of the mitigation in place is enough to bypass it). It is not clear if the posted patchset is an official Huawei release or whether this code is already shipping on any Huawei devices, but the patchset uses Huawei in its name, and the Github account for the patchset lists Huawei as the organization for the account.
Our focus today in this short blog post will be how the HKSP patchset introduced a trivially exploitable vulnerability due to a complete lack of defensive programming.
In the patch, a /proc/ksguard/state entry is created. Giving a hint to the level of review of the code, every time this entry is opened or closed, the following lines referencing a nonexistent filename are output to dmesg:
open /proc/ksg_state_proc ok.
close /proc/ksg_state_proc ok.
As we can see in the below line of the patch, the state
entry is created with global RWX rights:
state_proc = proc_create("state", 0777, ksg_proc, &ksg_state_ops);
The ksg_state_write
handler for writes to this entry looks like this:
static ssize_t ksg_state_write(struct file *file, const char __user *buf, size_t len, loff_t *offset) { u64 value; char tmp[32]; size_t n = 0; if (copy_from_user(tmp, buf, len)) return -1; value = simple_strtoul(tmp, '\0', 10); switch (value) { case 1: ksg_check_keyboard(); break; case 2: ksg_check_nf(); break; case 3: ksg_check_pointer(); break; case 4: ksg_check_sct(); break; default: break; } *offset += len; n += len; return len; }
There are a number of issues with this function. First, there is the return -1
which should return a proper errno (generally -EFAULT
). There are no checks at all on the value of len
. The first issue this causes is acting as a limited oracle. By writing 0 bytes to the entry, the uninitialized tmp
array will attempt to have a number parsed out of it and then acted upon. Most importantly, due to the lack of checks on len
, and given that tmp
is a simple 32-byte stack array, this introduces a trivially exploitable kernel stack buffer overflow able to be performed by any unprivileged user.
Effective security defenses require defined, realistic threat models. Defenses in the kernel should be programmed defensively and with reducing maintenance burdens in mind. The kernel can effectively be thought of as the largest, most vulnerable setuid root binary on the system. New code added to this most-privileged component of the system is potential new attack surface and requires heavy scrutiny, lest worse problems be introduced than were attempted to be solved in the first place.
from Hacker News https://ift.tt/2yzpWxZ
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.