hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/security/selinux/avc.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Implementation of the kernel access vector cache (AVC).
34 *
....@@ -8,10 +9,6 @@
89 * Replaced the avc_lock spinlock by RCU.
910 *
1011 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
11
- *
12
- * This program is free software; you can redistribute it and/or modify
13
- * it under the terms of the GNU General Public License version 2,
14
- * as published by the Free Software Foundation.
1512 */
1613 #include <linux/types.h>
1714 #include <linux/stddef.h>
....@@ -34,6 +31,9 @@
3431 #include "avc_ss.h"
3532 #include "classmap.h"
3633
34
+#define CREATE_TRACE_POINTS
35
+#include <trace/events/avc.h>
36
+
3737 #define AVC_CACHE_SLOTS 512
3838 #define AVC_DEF_CACHE_THRESHOLD 512
3939 #define AVC_CACHE_RECLAIM 16
....@@ -43,6 +43,9 @@
4343 #else
4444 #define avc_cache_stats_incr(field) do {} while (0)
4545 #endif
46
+
47
+#undef CREATE_TRACE_POINTS
48
+#include <trace/hooks/avc.h>
4649
4750 struct avc_entry {
4851 u32 ssid;
....@@ -127,75 +130,6 @@
127130 static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
128131 {
129132 return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
130
-}
131
-
132
-/**
133
- * avc_dump_av - Display an access vector in human-readable form.
134
- * @tclass: target security class
135
- * @av: access vector
136
- */
137
-static void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av)
138
-{
139
- const char **perms;
140
- int i, perm;
141
-
142
- if (av == 0) {
143
- audit_log_format(ab, " null");
144
- return;
145
- }
146
-
147
- BUG_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map));
148
- perms = secclass_map[tclass-1].perms;
149
-
150
- audit_log_format(ab, " {");
151
- i = 0;
152
- perm = 1;
153
- while (i < (sizeof(av) * 8)) {
154
- if ((perm & av) && perms[i]) {
155
- audit_log_format(ab, " %s", perms[i]);
156
- av &= ~perm;
157
- }
158
- i++;
159
- perm <<= 1;
160
- }
161
-
162
- if (av)
163
- audit_log_format(ab, " 0x%x", av);
164
-
165
- audit_log_format(ab, " }");
166
-}
167
-
168
-/**
169
- * avc_dump_query - Display a SID pair and a class in human-readable form.
170
- * @ssid: source security identifier
171
- * @tsid: target security identifier
172
- * @tclass: target security class
173
- */
174
-static void avc_dump_query(struct audit_buffer *ab, struct selinux_state *state,
175
- u32 ssid, u32 tsid, u16 tclass)
176
-{
177
- int rc;
178
- char *scontext;
179
- u32 scontext_len;
180
-
181
- rc = security_sid_to_context(state, ssid, &scontext, &scontext_len);
182
- if (rc)
183
- audit_log_format(ab, "ssid=%d", ssid);
184
- else {
185
- audit_log_format(ab, "scontext=%s", scontext);
186
- kfree(scontext);
187
- }
188
-
189
- rc = security_sid_to_context(state, tsid, &scontext, &scontext_len);
190
- if (rc)
191
- audit_log_format(ab, " tsid=%d", tsid);
192
- else {
193
- audit_log_format(ab, " tcontext=%s", scontext);
194
- kfree(scontext);
195
- }
196
-
197
- BUG_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map));
198
- audit_log_format(ab, " tclass=%s", secclass_map[tclass-1].name);
199133 }
200134
201135 /**
....@@ -510,6 +444,7 @@
510444
511445 static void avc_node_delete(struct selinux_avc *avc, struct avc_node *node)
512446 {
447
+ trace_android_vh_selinux_avc_node_delete(node);
513448 hlist_del_rcu(&node->list);
514449 call_rcu(&node->rhead, avc_node_free);
515450 atomic_dec(&avc->avc_cache.active_nodes);
....@@ -526,6 +461,7 @@
526461 static void avc_node_replace(struct selinux_avc *avc,
527462 struct avc_node *new, struct avc_node *old)
528463 {
464
+ trace_android_vh_selinux_avc_node_replace(old, new);
529465 hlist_replace_rcu(&old->list, &new->list);
530466 call_rcu(&old->rhead, avc_node_free);
531467 atomic_dec(&avc->avc_cache.active_nodes);
....@@ -634,8 +570,10 @@
634570 avc_cache_stats_incr(lookups);
635571 node = avc_search_node(avc, ssid, tsid, tclass);
636572
637
- if (node)
573
+ if (node) {
574
+ trace_android_vh_selinux_avc_lookup(node, ssid, tsid, tclass);
638575 return node;
576
+ }
639577
640578 avc_cache_stats_incr(misses);
641579 return NULL;
....@@ -719,6 +657,7 @@
719657 }
720658 }
721659 hlist_add_head_rcu(&node->list, head);
660
+ trace_android_vh_selinux_avc_insert(node);
722661 found:
723662 spin_unlock_irqrestore(lock, flag);
724663 return node;
....@@ -733,11 +672,36 @@
733672 static void avc_audit_pre_callback(struct audit_buffer *ab, void *a)
734673 {
735674 struct common_audit_data *ad = a;
736
- audit_log_format(ab, "avc: %s ",
737
- ad->selinux_audit_data->denied ? "denied" : "granted");
738
- avc_dump_av(ab, ad->selinux_audit_data->tclass,
739
- ad->selinux_audit_data->audited);
740
- audit_log_format(ab, " for ");
675
+ struct selinux_audit_data *sad = ad->selinux_audit_data;
676
+ u32 av = sad->audited;
677
+ const char **perms;
678
+ int i, perm;
679
+
680
+ audit_log_format(ab, "avc: %s ", sad->denied ? "denied" : "granted");
681
+
682
+ if (av == 0) {
683
+ audit_log_format(ab, " null");
684
+ return;
685
+ }
686
+
687
+ perms = secclass_map[sad->tclass-1].perms;
688
+
689
+ audit_log_format(ab, " {");
690
+ i = 0;
691
+ perm = 1;
692
+ while (i < (sizeof(av) * 8)) {
693
+ if ((perm & av) && perms[i]) {
694
+ audit_log_format(ab, " %s", perms[i]);
695
+ av &= ~perm;
696
+ }
697
+ i++;
698
+ perm <<= 1;
699
+ }
700
+
701
+ if (av)
702
+ audit_log_format(ab, " 0x%x", av);
703
+
704
+ audit_log_format(ab, " } for ");
741705 }
742706
743707 /**
....@@ -749,14 +713,57 @@
749713 static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
750714 {
751715 struct common_audit_data *ad = a;
752
- audit_log_format(ab, " ");
753
- avc_dump_query(ab, ad->selinux_audit_data->state,
754
- ad->selinux_audit_data->ssid,
755
- ad->selinux_audit_data->tsid,
756
- ad->selinux_audit_data->tclass);
757
- if (ad->selinux_audit_data->denied) {
758
- audit_log_format(ab, " permissive=%u",
759
- ad->selinux_audit_data->result ? 0 : 1);
716
+ struct selinux_audit_data *sad = ad->selinux_audit_data;
717
+ char *scontext = NULL;
718
+ char *tcontext = NULL;
719
+ const char *tclass = NULL;
720
+ u32 scontext_len;
721
+ u32 tcontext_len;
722
+ int rc;
723
+
724
+ rc = security_sid_to_context(sad->state, sad->ssid, &scontext,
725
+ &scontext_len);
726
+ if (rc)
727
+ audit_log_format(ab, " ssid=%d", sad->ssid);
728
+ else
729
+ audit_log_format(ab, " scontext=%s", scontext);
730
+
731
+ rc = security_sid_to_context(sad->state, sad->tsid, &tcontext,
732
+ &tcontext_len);
733
+ if (rc)
734
+ audit_log_format(ab, " tsid=%d", sad->tsid);
735
+ else
736
+ audit_log_format(ab, " tcontext=%s", tcontext);
737
+
738
+ tclass = secclass_map[sad->tclass-1].name;
739
+ audit_log_format(ab, " tclass=%s", tclass);
740
+
741
+ if (sad->denied)
742
+ audit_log_format(ab, " permissive=%u", sad->result ? 0 : 1);
743
+
744
+ trace_selinux_audited(sad, scontext, tcontext, tclass);
745
+ kfree(tcontext);
746
+ kfree(scontext);
747
+
748
+ /* in case of invalid context report also the actual context string */
749
+ rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
750
+ &scontext_len);
751
+ if (!rc && scontext) {
752
+ if (scontext_len && scontext[scontext_len - 1] == '\0')
753
+ scontext_len--;
754
+ audit_log_format(ab, " srawcon=");
755
+ audit_log_n_untrustedstring(ab, scontext, scontext_len);
756
+ kfree(scontext);
757
+ }
758
+
759
+ rc = security_sid_to_context_inval(sad->state, sad->tsid, &scontext,
760
+ &scontext_len);
761
+ if (!rc && scontext) {
762
+ if (scontext_len && scontext[scontext_len - 1] == '\0')
763
+ scontext_len--;
764
+ audit_log_format(ab, " trawcon=");
765
+ audit_log_n_untrustedstring(ab, scontext, scontext_len);
766
+ kfree(scontext);
760767 }
761768 }
762769
....@@ -768,6 +775,9 @@
768775 {
769776 struct common_audit_data stack_data;
770777 struct selinux_audit_data sad;
778
+
779
+ if (WARN_ON(!tclass || tclass >= ARRAY_SIZE(secclass_map)))
780
+ return -EINVAL;
771781
772782 if (!a) {
773783 a = &stack_data;
....@@ -1052,7 +1062,8 @@
10521062 int rc = 0, rc2;
10531063
10541064 xp_node = &local_xp_node;
1055
- BUG_ON(!requested);
1065
+ if (WARN_ON(!requested))
1066
+ return -EACCES;
10561067
10571068 rcu_read_lock();
10581069
....@@ -1142,7 +1153,8 @@
11421153 int rc = 0;
11431154 u32 denied;
11441155
1145
- BUG_ON(!requested);
1156
+ if (WARN_ON(!requested))
1157
+ return -EACCES;
11461158
11471159 rcu_read_lock();
11481160