| From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| From: Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com> |
| Date: Tue, 5 Oct 2021 17:07:57 +0800 |
| Subject: ANDROID: security: selinux: Add restricted vendor hook in avc |
| |
| Add restricted vendor hook for avc, so we can get avc_node |
| information to monitor avc lifetime. |
| |
| Squash: |
| ANDROID: selinux: add restricted vendor hook in selinux |
| |
| Bug: 181639260 |
| Bug: 186363840 |
| |
| Signed-off-by: Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com> |
| Change-Id: Idbebeca926c2cb407264f2872b032e1f18462697 |
| Signed-off-by: Lee Jones <joneslee@google.com> |
| --- |
| drivers/android/vendor_hooks.c | 7 +++++++ |
| include/trace/hooks/avc.h | 33 +++++++++++++++++++++++++++++++++ |
| include/trace/hooks/selinux.h | 21 +++++++++++++++++++++ |
| security/selinux/avc.c | 10 +++++++++- |
| security/selinux/ss/services.c | 3 +++ |
| 5 files changed, 73 insertions(+), 1 deletion(-) |
| create mode 100644 include/trace/hooks/avc.h |
| create mode 100644 include/trace/hooks/selinux.h |
| |
| diff --git a/drivers/android/vendor_hooks.c b/drivers/android/vendor_hooks.c |
| --- a/drivers/android/vendor_hooks.c |
| +++ b/drivers/android/vendor_hooks.c |
| @@ -29,8 +29,10 @@ |
| #include <trace/hooks/pm_domain.h> |
| #include <trace/hooks/cpuidle_psci.h> |
| #include <trace/hooks/vmscan.h> |
| +#include <trace/hooks/avc.h> |
| #include <trace/hooks/creds.h> |
| #include <trace/hooks/module.h> |
| +#include <trace/hooks/selinux.h> |
| #include <trace/hooks/syscall_check.h> |
| |
| /* |
| @@ -85,6 +87,10 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_sync_txn_recvd); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_cpufreq_transition); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_balance_anon_file_reclaim); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_show_max_freq); |
| +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_avc_insert); |
| +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_avc_node_delete); |
| +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_avc_node_replace); |
| +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_avc_lookup); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_commit_creds); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_exit_creds); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_override_creds); |
| @@ -93,5 +99,6 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_core_rw_nx); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_init_rw_nx); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_permit_before_init); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_set_module_permit_after_init); |
| +EXPORT_TRACEPOINT_SYMBOL_GPL(android_rvh_selinux_is_initialized); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_check_mmap_file); |
| EXPORT_TRACEPOINT_SYMBOL_GPL(android_vh_check_bpf_syscall); |
| diff --git a/include/trace/hooks/avc.h b/include/trace/hooks/avc.h |
| new file mode 100644 |
| --- /dev/null |
| +++ b/include/trace/hooks/avc.h |
| @@ -0,0 +1,33 @@ |
| +/* SPDX-License-Identifier: GPL-2.0 */ |
| +#undef TRACE_SYSTEM |
| +#define TRACE_SYSTEM avc |
| + |
| +#define TRACE_INCLUDE_PATH trace/hooks |
| +#if !defined(_TRACE_HOOK_AVC_H) || defined(TRACE_HEADER_MULTI_READ) |
| +#define _TRACE_HOOK_AVC_H |
| +#include <linux/tracepoint.h> |
| +#include <trace/hooks/vendor_hooks.h> |
| +/* |
| + * Following tracepoints are not exported in tracefs and provide a |
| + * mechanism for vendor modules to hook and extend functionality |
| + */ |
| +struct avc_node; |
| +DECLARE_RESTRICTED_HOOK(android_rvh_selinux_avc_insert, |
| + TP_PROTO(const struct avc_node *node), |
| + TP_ARGS(node), 1); |
| + |
| +DECLARE_RESTRICTED_HOOK(android_rvh_selinux_avc_node_delete, |
| + TP_PROTO(const struct avc_node *node), |
| + TP_ARGS(node), 1); |
| + |
| +DECLARE_RESTRICTED_HOOK(android_rvh_selinux_avc_node_replace, |
| + TP_PROTO(const struct avc_node *old, const struct avc_node *new), |
| + TP_ARGS(old, new), 1); |
| + |
| +DECLARE_RESTRICTED_HOOK(android_rvh_selinux_avc_lookup, |
| + TP_PROTO(const struct avc_node *node, u32 ssid, u32 tsid, u16 tclass), |
| + TP_ARGS(node, ssid, tsid, tclass), 1); |
| + |
| +#endif /* _TRACE_HOOK_AVC_H */ |
| +/* This part must be outside protection */ |
| +#include <trace/define_trace.h> |
| diff --git a/include/trace/hooks/selinux.h b/include/trace/hooks/selinux.h |
| new file mode 100644 |
| --- /dev/null |
| +++ b/include/trace/hooks/selinux.h |
| @@ -0,0 +1,21 @@ |
| +/* SPDX-License-Identifier: GPL-2.0 */ |
| +#undef TRACE_SYSTEM |
| +#define TRACE_SYSTEM selinux |
| + |
| +#define TRACE_INCLUDE_PATH trace/hooks |
| +#if !defined(_TRACE_HOOK_SELINUX_H) || defined(TRACE_HEADER_MULTI_READ) |
| +#define _TRACE_HOOK_SELINUX_H |
| +#include <linux/tracepoint.h> |
| +#include <trace/hooks/vendor_hooks.h> |
| +/* |
| + * Following tracepoints are not exported in tracefs and provide a |
| + * mechanism for vendor modules to hook and extend functionality |
| + */ |
| +struct selinux_state; |
| +DECLARE_RESTRICTED_HOOK(android_rvh_selinux_is_initialized, |
| + TP_PROTO(const struct selinux_state *state), |
| + TP_ARGS(state), 1); |
| + |
| +#endif /* _TRACE_HOOK_SELINUX_H */ |
| +/* This part must be outside protection */ |
| +#include <trace/define_trace.h> |
| diff --git a/security/selinux/avc.c b/security/selinux/avc.c |
| --- a/security/selinux/avc.c |
| +++ b/security/selinux/avc.c |
| @@ -44,6 +44,9 @@ |
| #define avc_cache_stats_incr(field) do {} while (0) |
| #endif |
| |
| +#undef CREATE_TRACE_POINTS |
| +#include <trace/hooks/avc.h> |
| + |
| struct avc_entry { |
| u32 ssid; |
| u32 tsid; |
| @@ -441,6 +444,7 @@ static void avc_node_free(struct rcu_head *rhead) |
| |
| static void avc_node_delete(struct selinux_avc *avc, struct avc_node *node) |
| { |
| + trace_android_rvh_selinux_avc_node_delete(node); |
| hlist_del_rcu(&node->list); |
| call_rcu(&node->rhead, avc_node_free); |
| atomic_dec(&avc->avc_cache.active_nodes); |
| @@ -457,6 +461,7 @@ static void avc_node_kill(struct selinux_avc *avc, struct avc_node *node) |
| static void avc_node_replace(struct selinux_avc *avc, |
| struct avc_node *new, struct avc_node *old) |
| { |
| + trace_android_rvh_selinux_avc_node_replace(old, new); |
| hlist_replace_rcu(&old->list, &new->list); |
| call_rcu(&old->rhead, avc_node_free); |
| atomic_dec(&avc->avc_cache.active_nodes); |
| @@ -566,8 +571,10 @@ static struct avc_node *avc_lookup(struct selinux_avc *avc, |
| avc_cache_stats_incr(lookups); |
| node = avc_search_node(avc, ssid, tsid, tclass); |
| |
| - if (node) |
| + if (node) { |
| + trace_android_rvh_selinux_avc_lookup(node, ssid, tsid, tclass); |
| return node; |
| + } |
| |
| avc_cache_stats_incr(misses); |
| return NULL; |
| @@ -652,6 +659,7 @@ static struct avc_node *avc_insert(struct selinux_avc *avc, |
| } |
| } |
| hlist_add_head_rcu(&node->list, head); |
| + trace_android_rvh_selinux_avc_insert(node); |
| found: |
| spin_unlock_irqrestore(lock, flag); |
| return node; |
| diff --git a/security/selinux/ss/services.c b/security/selinux/ss/services.c |
| --- a/security/selinux/ss/services.c |
| +++ b/security/selinux/ss/services.c |
| @@ -68,6 +68,8 @@ |
| #include "policycap_names.h" |
| #include "ima.h" |
| |
| +#include <trace/hooks/selinux.h> |
| + |
| struct selinux_policy_convert_data { |
| struct convert_context_args args; |
| struct sidtab_convert_params sidtab_params; |
| @@ -2255,6 +2257,7 @@ void selinux_policy_commit(struct selinux_state *state, |
| */ |
| selinux_mark_initialized(state); |
| selinux_complete_init(); |
| + trace_android_rvh_selinux_is_initialized(state); |
| } |
| |
| /* Free the old policy */ |