Skip to contentSkip to conversation

security: mark LSM hooks as __ro_after_init

RFC PATCH In review James Morris 6 participants 13 messages Feb 13, 2017 – Feb 14, 2017

Subsystems (8)
lore
Share & tools
lore

James Morris

author maintainer
#0 lore raw

RFC PATCH 1/4 security: mark LSM hooks as __ro_after_init

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

---
 security/apparmor/lsm.c    |    2 +-
 security/commoncap.c       |    2 +-
 security/loadpin/loadpin.c |    2 +-
 security/security.c        |    2 +-
 security/selinux/hooks.c   |    2 +-
 security/smack/smack_lsm.c |    2 +-
 security/tomoyo/tomoyo.c   |    2 +-
 security/yama/yama_lsm.c   |    2 +-
 8 files changed, 8 insertions(+), 8 deletions(-)
security/apparmor/lsm.c +1 -1
index 709eacd..73aad35 100644
@@ -587,7 +587,7 @@ static int apparmor_task_setrlimit(struct task_struct *task,
 	return error;
 }
 
-static struct security_hook_list apparmor_hooks[] = {
+static struct security_hook_list apparmor_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
 	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
 	LSM_HOOK_INIT(capget, apparmor_capget),
security/commoncap.c +1 -1
index 6d4d586..2c0cf0a 100644
@@ -1070,7 +1070,7 @@ int cap_mmap_file(struct file *file, unsigned long reqprot,
 
 #ifdef CONFIG_SECURITY
 
-struct security_hook_list capability_hooks[] = {
+struct security_hook_list capability_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(capable, cap_capable),
 	LSM_HOOK_INIT(settime, cap_settime),
 	LSM_HOOK_INIT(ptrace_access_check, cap_ptrace_access_check),
security/loadpin/loadpin.c +1 -1
index 1d82eae..aaef8d9 100644
@@ -174,7 +174,7 @@ static int loadpin_read_file(struct file *file, enum kernel_read_file_id id)
 	return 0;
 }
 
-static struct security_hook_list loadpin_hooks[] = {
+static struct security_hook_list loadpin_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(sb_free_security, loadpin_sb_free_security),
 	LSM_HOOK_INIT(kernel_read_file, loadpin_read_file),
 };
security/security.c +1 -1
index d0e07f2..e913abb 100644
@@ -1622,7 +1622,7 @@ int security_audit_rule_match(u32 secid, u32 field, u32 op, void *lsmrule,
 }
 #endif /* CONFIG_AUDIT */
 
-struct security_hook_heads security_hook_heads = {
+struct security_hook_heads security_hook_heads __ro_after_init = {
 	.binder_set_context_mgr =
 		LIST_HEAD_INIT(security_hook_heads.binder_set_context_mgr),
 	.binder_transaction =
security/selinux/hooks.c +1 -1
index 9bc12bc..9599e97 100644
@@ -6104,7 +6104,7 @@ static int selinux_key_getsecurity(struct key *key, char **_buffer)
 
 #endif
 
-static struct security_hook_list selinux_hooks[] = {
+static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
 	LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
 	LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
security/smack/smack_lsm.c +1 -1
index 60b4217..9bad3cc 100644
@@ -4633,7 +4633,7 @@ static int smack_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
 	return 0;
 }
 
-static struct security_hook_list smack_hooks[] = {
+static struct security_hook_list smack_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(ptrace_access_check, smack_ptrace_access_check),
 	LSM_HOOK_INIT(ptrace_traceme, smack_ptrace_traceme),
 	LSM_HOOK_INIT(syslog, smack_syslog),
security/tomoyo/tomoyo.c +1 -1
index edc52d6..2677427 100644
@@ -496,7 +496,7 @@ static int tomoyo_socket_sendmsg(struct socket *sock, struct msghdr *msg,
  * tomoyo_security_ops is a "struct security_operations" which is used for
  * registering TOMOYO.
  */
-static struct security_hook_list tomoyo_hooks[] = {
+static struct security_hook_list tomoyo_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(cred_alloc_blank, tomoyo_cred_alloc_blank),
 	LSM_HOOK_INIT(cred_prepare, tomoyo_cred_prepare),
 	LSM_HOOK_INIT(cred_transfer, tomoyo_cred_transfer),
security/yama/yama_lsm.c +1 -1
index 88271a3..fae3cd9 100644
@@ -428,7 +428,7 @@ int yama_ptrace_traceme(struct task_struct *parent)
 	return rc;
 }
 
-static struct security_hook_list yama_hooks[] = {
+static struct security_hook_list yama_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(ptrace_access_check, yama_ptrace_access_check),
 	LSM_HOOK_INIT(ptrace_traceme, yama_ptrace_traceme),
 	LSM_HOOK_INIT(task_prctl, yama_task_prctl),
Signature · 1 line
1.7.1
maintainer
#1 lore raw
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

#2 lore raw
Quoted 8 lines · Tetsuo Handa
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

LKM based LSMs don't exist yet, and when they do, we may also have the "write rarely" infrastructure done, which LKM based LSMs can use to update the structures.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#3 lore raw
Quoted 8 lines · Kees Cook
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

Quoted 9 lines
Quoted 2 lines

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

LKM based LSMs don't exist yet, and when they do, we may also have the "write rarely" infrastructure done, which LKM based LSMs can use to update the structures.

-Kees

Is someone actually working on the write rarely patches? If a version has been sent out, I don't recall seeing it.

Thanks,
Laura

#4 lore raw
Quoted 12 lines · Laura Abbott
Quoted 11 lines · Kees Cook
Quoted 8 lines · Tetsuo Handa
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

Quoted 6 lines
Quoted 3 lines

LKM based LSMs don't exist yet, and when they do, we may also have the "write rarely" infrastructure done, which LKM based LSMs can use to update the structures.

Is someone actually working on the write rarely patches? If a version has been sent out, I don't recall seeing it.

Still mostly just discussion. I've been toying with the PaX-style of it on x86, and I think Mark Rutland had some ideas for arm64, but I don't know if he's actually written code.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#5 lore raw

Hi,

Quoted 17 lines · Kees Cook
Quoted 16 lines · Laura Abbott
Quoted 14 lines · Kees Cook
Quoted 8 lines · Tetsuo Handa
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

LKM based LSMs don't exist yet, and when they do, we may also have the "write rarely" infrastructure done, which LKM based LSMs can use to update the structures.

Quoted 6 lines
Quoted 2 lines

Is someone actually working on the write rarely patches? If a version has been sent out, I don't recall seeing it.

Still mostly just discussion. I've been toying with the PaX-style of it on x86, and I think Mark Rutland had some ideas for arm64, but I don't know if he's actually written code.

While I had a rough idea [1] of what that could look like, I haven't written any code.

Thanks,
Mark.

[1] http://www.openwall.com/lists/kernel-hardening/2016/11/18/3

author maintainer
#6 lore raw
In reply to Kees Cook
Quoted 6 lines · Kees Cook
Quoted 4 lines · Tetsuo Handa
Quoted 3 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

LKM based LSMs don't exist yet, and when they do, we may also have the "write rarely" infrastructure done, which LKM based LSMs can use to update the structures.

I think it would be a backwards step security-wise to allow dynamically loadable security modules. The security risks of security code in the kernel should be aggressively minimized.

Signature · 2 lines
James Morris
<[email protected]>
maintainer
#7 lore raw
Quoted 12 lines · James Morris
Quoted 11 lines · Kees Cook
Quoted 8 lines · Tetsuo Handa
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

This patch makes LKM based LSMs (e.g. AKARI) impossible. I'm not happy with this patch.

Quoted 7 lines
Quoted 3 lines

LKM based LSMs don't exist yet, and when they do, we may also have the "write rarely" infrastructure done, which LKM based LSMs can use to update the structures.

I think it would be a backwards step security-wise to allow dynamically loadable security modules. The security risks of security code in the kernel should be aggressively minimized.

If you do want secure kernels, build with CONFIG_MODULES=n for individual environment.

Loadable kernel modules used by antivirus software temporarily modify syscall tables
( http://stackoverflow.com/questions/13876369/system-call-interception-in-linux-kernel-module-kernel-3-5 ) in order to register hooks for execve()/open()/close(). It is very frustrating for
many users if you enforce CONFIG_MODULES=n or forbid post-__init registration of hooks.

Marking LSM hooks as __ro_after_init might help mitigating modification of LSM hooks by innocent errors. A malicious attack can try to modify variables that control whether access controls are in enforcing mode or not. You need to eliminate such variables if you don't want access controls being disabled by malicious attacks. You want to eliminate more things such as security= kernel command line option, function pointers like security_hook_list, ability to update policy configuration without rebooting.

Why SELinux is not the only security module? Why Smack, TOMOYO, AppArmor, Yama were proposed and became in-tree security modules? Why still other modules such as CaitSith, ptags, Timgad are proposed? Answer is very simple. In-tree modules (or modules enabled in distributor's kernels) cannot satisfy everybody's needs. But asking end users to rebuild their kernels is too painful.

Disallowing dynamically loadable security modules is as silly idea as getting rid of LSM framework ( https://lwn.net/Articles/138042/
http://lkml.kernel.org/r/[email protected] ) unless we accept whatever out-of-tree LSM modules and maintain them as in-tree modules and enable them in distributor's kernels. But such things won't happen. If we legally allow LKM based LSMs, we don't need to make security/ directory look like /dev/random .

There will be vulnerabilities like DirtyCOW which might defeat protection for LSM. Security of kernel depends on correctness of all kernel code. Marking LSM hooks as __ro_after_init buys little compared to its sacrifice. We don't need to disallow dynamically loadable security modules.

author maintainer
#8 lore raw
Quoted 5 lines · Tetsuo Handa
Quoted 3 lines

I think it would be a backwards step security-wise to allow dynamically loadable security modules. The security risks of security code in the kernel should be aggressively minimized.

If you do want secure kernels, build with CONFIG_MODULES=n for individual environment.

The rationale for disallowing dynamically loaded LSMs in this case is so that the LSM hooks can be made read-only, to harden the kernel against LSM being used as an attack vector. Overwriting an LSM hook can provide an attacker with a security-critical control point at any of hundreds locations within the kernel.

It's not about modules, per se.

Quoted 5 lines

Loadable kernel modules used by antivirus software temporarily modify syscall tables
( http://stackoverflow.com/questions/13876369/system-call-interception-in-linux-kernel-module-kernel-3-5 ) in order to register hooks for execve()/open()/close(). It is very frustrating for
many users if you enforce CONFIG_MODULES=n or forbid post-__init registration of hooks.

We don't cater to out of tree code.

Additionally, I'd also seriously question whether the security benefits of kernel AV outweigh its security risks.

Quoted 8 lines

Marking LSM hooks as __ro_after_init might help mitigating modification of LSM hooks by innocent errors. A malicious attack can try to modify variables that control whether access controls are in enforcing mode or not. You need to eliminate such variables if you don't want access controls being disabled by malicious attacks. You want to eliminate more things such as security= kernel command line option, function pointers like security_hook_list, ability to update policy configuration without rebooting.

We are not trying to prevent every possible attack here, just the class of attack where someone uses a function pointer overwrite to compromise the kernel, via an LSM hook.

We want to reduce the security risk introduced by LSM itself, i.e. providing hooks at security-critical locations all over the kernel.

This is an important and useful goal, IMHO.

Quoted 6 lines

Why SELinux is not the only security module? Why Smack, TOMOYO, AppArmor, Yama were proposed and became in-tree security modules? Why still other modules such as CaitSith, ptags, Timgad are proposed? Answer is very simple. In-tree modules (or modules enabled in distributor's kernels) cannot satisfy everybody's needs. But asking end users to rebuild their kernels is too painful.

Once again, and this is a very widely accepted and long standing norm, the mainline kernel does not cater to out of tree code.

Quoted 7 lines

Disallowing dynamically loadable security modules is as silly idea as getting rid of LSM framework ( https://lwn.net/Articles/138042/
http://lkml.kernel.org/r/[email protected] ) unless we accept whatever out-of-tree LSM modules and maintain them as in-tree modules and enable them in distributor's kernels. But such things won't happen. If we legally allow LKM based LSMs, we don't need to make security/ directory look like /dev/random .

Dynamically loadable LSMs are legally allowed, we just don't cater to them in mainline.

Quoted 2 lines

There will be vulnerabilities like DirtyCOW which might defeat protection for LSM. Security of kernel depends on correctness of all kernel code.

As mentioned above, we are trying to harden the LSM framework against being an attack vector. We are not trying to harden it against an already compromised kernel.

Quoted 1 line

Marking LSM hooks as __ro_after_init buys little compared to its sacrifice.

On the contrary, I believe it provides useful hardening and in fact helps to mitigate the risk of the LSM API itself.

Quoted 1 line

We don't need to disallow dynamically loadable security modules.

It's not supported by mainline LSMs, for very good reasons (such as needing to start from a known security state during boot), per many previous threads on the topic.

- James

Signature · 2 lines
James Morris
<[email protected]>
author maintainer
#9 lore raw
Quoted 3 lines · James Morris

As mentioned above, we are trying to harden the LSM framework against being an attack vector. We are not trying to harden it against an already compromised kernel.

I should clarify here -- by already compromised, I mean specifically in terms of the attacker being able to bypass/change RO kernel pages.

Signature · 2 lines
James Morris
<[email protected]>
maintainer
#10 lore raw
In reply to James Morris
Quoted 11 lines · James Morris
Quoted 7 lines

Disallowing dynamically loadable security modules is as silly idea as getting rid of LSM framework ( https://lwn.net/Articles/138042/
http://lkml.kernel.org/r/[email protected] ) unless we accept whatever out-of-tree LSM modules and maintain them as in-tree modules and enable them in distributor's kernels. But such things won't happen. If we legally allow LKM based LSMs, we don't need to make security/ directory look like /dev/random .

Dynamically loadable LSMs are legally allowed, we just don't cater to them in mainline.

I'm saying that this patch will make dynamically loadable LSMs illegal, for not allowing updating struct list_head prevents dynamically loadable LSMs from registering.

author maintainer
#11 lore raw
Quoted 9 lines · Tetsuo Handa
Quoted 8 lines · James Morris
Quoted 7 lines

Disallowing dynamically loadable security modules is as silly idea as getting rid of LSM framework ( https://lwn.net/Articles/138042/
http://lkml.kernel.org/r/[email protected] ) unless we accept whatever out-of-tree LSM modules and maintain them as in-tree modules and enable them in distributor's kernels. But such things won't happen. If we legally allow LKM based LSMs, we don't need to make security/ directory look like /dev/random .

Quoted 6 lines
Quoted 3 lines

Dynamically loadable LSMs are legally allowed, we just don't cater to them in mainline.

I'm saying that this patch will make dynamically loadable LSMs illegal, for not allowing updating struct list_head prevents dynamically loadable LSMs from registering.

The next patch set will include an option to allow writable hooks (for SELinux runtime disable).

Signature · 2 lines
James Morris
<[email protected]>
maintainer
#12 lore raw
In reply to Tetsuo Handa
Quoted 6 lines · Tetsuo Handa
Quoted 4 lines · James Morris

As the regsitration of LSMs is performed during init and then does not change, we can mark all of the regsitration hooks as __ro_after_init.

This patch makes LKM based LSMs (e.g. AKARI) impossible.

When a mechanism to do LKM based modules work is proposed it could include ifdef's around the __ro_after_init. I'm assuming that enabling LKM modules is something we'd want to make optional.

Quoted 6 lines

I'm not happy with this patch.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html