Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2021 Microsoft Corporation
4 *
5 * Author: Lakshmi Ramasubramanian (nramas@linux.microsoft.com)
6 *
7 * Measure critical data structures maintainted by SELinux
8 * using IMA subsystem.
9 */
10#include <linux/vmalloc.h>
11#include <linux/ima.h>
12#include "security.h"
13#include "ima.h"
14
15/*
16 * selinux_ima_collect_state - Read selinux configuration settings
17 *
18 * @state: selinux_state
19 *
20 * On success returns the configuration settings string.
21 * On error, returns NULL.
22 */
23static char *selinux_ima_collect_state(struct selinux_state *state)
24{
25 const char *on = "=1;", *off = "=0;";
26 char *buf;
27 int buf_len, len, i, rc;
28
29 buf_len = strlen("initialized=0;enforcing=0;checkreqprot=0;") + 1;
30
31 len = strlen(on);
32 for (i = 0; i < __POLICYDB_CAP_MAX; i++)
33 buf_len += strlen(selinux_policycap_names[i]) + len;
34
35 buf = kzalloc(buf_len, GFP_KERNEL);
36 if (!buf)
37 return NULL;
38
39 rc = strscpy(buf, "initialized", buf_len);
40 WARN_ON(rc < 0);
41
42 rc = strlcat(buf, selinux_initialized(state) ? on : off, buf_len);
43 WARN_ON(rc >= buf_len);
44
45 rc = strlcat(buf, "enforcing", buf_len);
46 WARN_ON(rc >= buf_len);
47
48 rc = strlcat(buf, enforcing_enabled(state) ? on : off, buf_len);
49 WARN_ON(rc >= buf_len);
50
51 rc = strlcat(buf, "checkreqprot", buf_len);
52 WARN_ON(rc >= buf_len);
53
54 rc = strlcat(buf, checkreqprot_get(state) ? on : off, buf_len);
55 WARN_ON(rc >= buf_len);
56
57 for (i = 0; i < __POLICYDB_CAP_MAX; i++) {
58 rc = strlcat(buf, selinux_policycap_names[i], buf_len);
59 WARN_ON(rc >= buf_len);
60
61 rc = strlcat(buf, state->policycap[i] ? on : off, buf_len);
62 WARN_ON(rc >= buf_len);
63 }
64
65 return buf;
66}
67
68/*
69 * selinux_ima_measure_state_locked - Measure SELinux state and hash of policy
70 *
71 * @state: selinux state struct
72 */
73void selinux_ima_measure_state_locked(struct selinux_state *state)
74{
75 char *state_str = NULL;
76 void *policy = NULL;
77 size_t policy_len;
78 int rc = 0;
79
80 lockdep_assert_held(&state->policy_mutex);
81
82 state_str = selinux_ima_collect_state(state);
83 if (!state_str) {
84 pr_err("SELinux: %s: failed to read state.\n", __func__);
85 return;
86 }
87
88 ima_measure_critical_data("selinux", "selinux-state",
89 state_str, strlen(state_str), false,
90 NULL, 0);
91
92 kfree(state_str);
93
94 /*
95 * Measure SELinux policy only after initialization is completed.
96 */
97 if (!selinux_initialized(state))
98 return;
99
100 rc = security_read_state_kernel(state, &policy, &policy_len);
101 if (rc) {
102 pr_err("SELinux: %s: failed to read policy %d.\n", __func__, rc);
103 return;
104 }
105
106 ima_measure_critical_data("selinux", "selinux-policy-hash",
107 policy, policy_len, true,
108 NULL, 0);
109
110 vfree(policy);
111}
112
113/*
114 * selinux_ima_measure_state - Measure SELinux state and hash of policy
115 *
116 * @state: selinux state struct
117 */
118void selinux_ima_measure_state(struct selinux_state *state)
119{
120 lockdep_assert_not_held(&state->policy_mutex);
121
122 mutex_lock(&state->policy_mutex);
123 selinux_ima_measure_state_locked(state);
124 mutex_unlock(&state->policy_mutex);
125}
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2021 Microsoft Corporation
4 *
5 * Author: Lakshmi Ramasubramanian (nramas@linux.microsoft.com)
6 *
7 * Measure critical data structures maintained by SELinux
8 * using IMA subsystem.
9 */
10#include <linux/vmalloc.h>
11#include <linux/ima.h>
12#include "security.h"
13#include "ima.h"
14
15/*
16 * selinux_ima_collect_state - Read selinux configuration settings
17 *
18 * On success returns the configuration settings string.
19 * On error, returns NULL.
20 */
21static char *selinux_ima_collect_state(void)
22{
23 const char *on = "=1;", *off = "=0;";
24 char *buf;
25 int buf_len, len, i, rc;
26
27 buf_len = strlen("initialized=0;enforcing=0;checkreqprot=0;") + 1;
28
29 len = strlen(on);
30 for (i = 0; i < __POLICYDB_CAP_MAX; i++)
31 buf_len += strlen(selinux_policycap_names[i]) + len;
32
33 buf = kzalloc(buf_len, GFP_KERNEL);
34 if (!buf)
35 return NULL;
36
37 rc = strscpy(buf, "initialized", buf_len);
38 WARN_ON(rc < 0);
39
40 rc = strlcat(buf, selinux_initialized() ? on : off, buf_len);
41 WARN_ON(rc >= buf_len);
42
43 rc = strlcat(buf, "enforcing", buf_len);
44 WARN_ON(rc >= buf_len);
45
46 rc = strlcat(buf, enforcing_enabled() ? on : off, buf_len);
47 WARN_ON(rc >= buf_len);
48
49 rc = strlcat(buf, "checkreqprot", buf_len);
50 WARN_ON(rc >= buf_len);
51
52 rc = strlcat(buf, checkreqprot_get() ? on : off, buf_len);
53 WARN_ON(rc >= buf_len);
54
55 for (i = 0; i < __POLICYDB_CAP_MAX; i++) {
56 rc = strlcat(buf, selinux_policycap_names[i], buf_len);
57 WARN_ON(rc >= buf_len);
58
59 rc = strlcat(buf, selinux_state.policycap[i] ? on : off,
60 buf_len);
61 WARN_ON(rc >= buf_len);
62 }
63
64 return buf;
65}
66
67/*
68 * selinux_ima_measure_state_locked - Measure SELinux state and hash of policy
69 */
70void selinux_ima_measure_state_locked(void)
71{
72 char *state_str = NULL;
73 void *policy = NULL;
74 size_t policy_len;
75 int rc = 0;
76
77 lockdep_assert_held(&selinux_state.policy_mutex);
78
79 state_str = selinux_ima_collect_state();
80 if (!state_str) {
81 pr_err("SELinux: %s: failed to read state.\n", __func__);
82 return;
83 }
84
85 ima_measure_critical_data("selinux", "selinux-state",
86 state_str, strlen(state_str), false,
87 NULL, 0);
88
89 kfree(state_str);
90
91 /*
92 * Measure SELinux policy only after initialization is completed.
93 */
94 if (!selinux_initialized())
95 return;
96
97 rc = security_read_state_kernel(&policy, &policy_len);
98 if (rc) {
99 pr_err("SELinux: %s: failed to read policy %d.\n", __func__, rc);
100 return;
101 }
102
103 ima_measure_critical_data("selinux", "selinux-policy-hash",
104 policy, policy_len, true,
105 NULL, 0);
106
107 vfree(policy);
108}
109
110/*
111 * selinux_ima_measure_state - Measure SELinux state and hash of policy
112 */
113void selinux_ima_measure_state(void)
114{
115 lockdep_assert_not_held(&selinux_state.policy_mutex);
116
117 mutex_lock(&selinux_state.policy_mutex);
118 selinux_ima_measure_state_locked();
119 mutex_unlock(&selinux_state.policy_mutex);
120}