~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/security/selinux/ima.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  */
 21 static 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  */
 70 void 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  */
113 void 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 }
121 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php