xref: /linux/security/landlock/setup.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Landlock LSM - Security framework setup
4  *
5  * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6  * Copyright © 2018-2020 ANSSI
7  */
8 
9 #include <linux/bits.h>
10 #include <linux/init.h>
11 #include <linux/lsm_hooks.h>
12 #include <uapi/linux/lsm.h>
13 
14 #include "cap.h"
15 #include "common.h"
16 #include "cred.h"
17 #include "errata.h"
18 #include "fs.h"
19 #include "id.h"
20 #include "net.h"
21 #include "ns.h"
22 #include "setup.h"
23 #include "task.h"
24 
25 bool landlock_initialized __ro_after_init = false;
26 
27 const struct lsm_id landlock_lsmid = {
28 	.name = LANDLOCK_NAME,
29 	.id = LSM_ID_LANDLOCK,
30 };
31 
32 struct lsm_blob_sizes landlock_blob_sizes __ro_after_init = {
33 	.lbs_cred = sizeof(struct landlock_cred_security),
34 	.lbs_file = sizeof(struct landlock_file_security),
35 	.lbs_inode = sizeof(struct landlock_inode_security),
36 	.lbs_superblock = sizeof(struct landlock_superblock_security),
37 };
38 
39 int landlock_errata __ro_after_init;
40 
41 static void __init compute_errata(void)
42 {
43 	size_t i;
44 
45 #ifndef __has_include
46 	/*
47 	 * This is a safeguard to make sure the compiler implements
48 	 * __has_include (see errata.h).
49 	 */
50 	WARN_ON_ONCE(1);
51 	return;
52 #endif
53 
54 	for (i = 0; landlock_errata_init[i].number; i++) {
55 		const int prev_errata = landlock_errata;
56 
57 		if (WARN_ON_ONCE(landlock_errata_init[i].abi >
58 				 landlock_abi_version))
59 			continue;
60 
61 		landlock_errata |= BIT(landlock_errata_init[i].number - 1);
62 		WARN_ON_ONCE(prev_errata == landlock_errata);
63 	}
64 }
65 
66 static int __init landlock_init(void)
67 {
68 	compute_errata();
69 	landlock_add_cred_hooks();
70 	landlock_add_task_hooks();
71 	landlock_add_fs_hooks();
72 	landlock_add_net_hooks();
73 	landlock_add_ns_hooks();
74 	landlock_add_cap_hooks();
75 	landlock_init_id();
76 	landlock_initialized = true;
77 	pr_info("Up and running.\n");
78 	return 0;
79 }
80 
81 DEFINE_LSM(LANDLOCK_NAME) = {
82 	.id = &landlock_lsmid,
83 	.init = landlock_init,
84 	.blobs = &landlock_blob_sizes,
85 };
86