| 1 | /* | 
|---|
| 2 | * Linux Security Module interfaces | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (C) 2001 WireX Communications, Inc <chris@wirex.com> | 
|---|
| 5 | * Copyright (C) 2001 Greg Kroah-Hartman <greg@kroah.com> | 
|---|
| 6 | * Copyright (C) 2001 Networks Associates Technology, Inc <ssmalley@nai.com> | 
|---|
| 7 | * Copyright (C) 2001 James Morris <jmorris@intercode.com.au> | 
|---|
| 8 | * Copyright (C) 2001 Silicon Graphics, Inc. (Trust Technology Group) | 
|---|
| 9 | * Copyright (C) 2015 Intel Corporation. | 
|---|
| 10 | * Copyright (C) 2015 Casey Schaufler <casey@schaufler-ca.com> | 
|---|
| 11 | * Copyright (C) 2016 Mellanox Techonologies | 
|---|
| 12 | * | 
|---|
| 13 | *	This program is free software; you can redistribute it and/or modify | 
|---|
| 14 | *	it under the terms of the GNU General Public License as published by | 
|---|
| 15 | *	the Free Software Foundation; either version 2 of the License, or | 
|---|
| 16 | *	(at your option) any later version. | 
|---|
| 17 | * | 
|---|
| 18 | *	Due to this file being licensed under the GPL there is controversy over | 
|---|
| 19 | *	whether this permits you to write a module that #includes this file | 
|---|
| 20 | *	without placing your module under the GPL.  Please consult a lawyer for | 
|---|
| 21 | *	advice before doing this. | 
|---|
| 22 | * | 
|---|
| 23 | */ | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef __LINUX_LSM_HOOKS_H | 
|---|
| 26 | #define __LINUX_LSM_HOOKS_H | 
|---|
| 27 |  | 
|---|
| 28 | #include <uapi/linux/lsm.h> | 
|---|
| 29 | #include <linux/security.h> | 
|---|
| 30 | #include <linux/init.h> | 
|---|
| 31 | #include <linux/rculist.h> | 
|---|
| 32 | #include <linux/xattr.h> | 
|---|
| 33 | #include <linux/static_call.h> | 
|---|
| 34 | #include <linux/unroll.h> | 
|---|
| 35 | #include <linux/jump_label.h> | 
|---|
| 36 | #include <linux/lsm_count.h> | 
|---|
| 37 |  | 
|---|
| 38 | union security_list_options { | 
|---|
| 39 | #define LSM_HOOK(RET, DEFAULT, NAME, ...) RET (*NAME)(__VA_ARGS__); | 
|---|
| 40 | #include "lsm_hook_defs.h" | 
|---|
| 41 | #undef LSM_HOOK | 
|---|
| 42 | void *lsm_func_addr; | 
|---|
| 43 | }; | 
|---|
| 44 |  | 
|---|
| 45 | /* | 
|---|
| 46 | * @key: static call key as defined by STATIC_CALL_KEY | 
|---|
| 47 | * @trampoline: static call trampoline as defined by STATIC_CALL_TRAMP | 
|---|
| 48 | * @hl: The security_hook_list as initialized by the owning LSM. | 
|---|
| 49 | * @active: Enabled when the static call has an LSM hook associated. | 
|---|
| 50 | */ | 
|---|
| 51 | struct lsm_static_call { | 
|---|
| 52 | struct static_call_key *key; | 
|---|
| 53 | void *trampoline; | 
|---|
| 54 | struct security_hook_list *hl; | 
|---|
| 55 | /* this needs to be true or false based on what the key defaults to */ | 
|---|
| 56 | struct static_key_false *active; | 
|---|
| 57 | } __randomize_layout; | 
|---|
| 58 |  | 
|---|
| 59 | /* | 
|---|
| 60 | * Table of the static calls for each LSM hook. | 
|---|
| 61 | * Once the LSMs are initialized, their callbacks will be copied to these | 
|---|
| 62 | * tables such that the calls are filled backwards (from last to first). | 
|---|
| 63 | * This way, we can jump directly to the first used static call, and execute | 
|---|
| 64 | * all of them after. This essentially makes the entry point | 
|---|
| 65 | * dynamic to adapt the number of static calls to the number of callbacks. | 
|---|
| 66 | */ | 
|---|
| 67 | struct lsm_static_calls_table { | 
|---|
| 68 | #define LSM_HOOK(RET, DEFAULT, NAME, ...) \ | 
|---|
| 69 | struct lsm_static_call NAME[MAX_LSM_COUNT]; | 
|---|
| 70 | #include <linux/lsm_hook_defs.h> | 
|---|
| 71 | #undef LSM_HOOK | 
|---|
| 72 | } __packed __randomize_layout; | 
|---|
| 73 |  | 
|---|
| 74 | /** | 
|---|
| 75 | * struct lsm_id - Identify a Linux Security Module. | 
|---|
| 76 | * @lsm: name of the LSM, must be approved by the LSM maintainers | 
|---|
| 77 | * @id: LSM ID number from uapi/linux/lsm.h | 
|---|
| 78 | * | 
|---|
| 79 | * Contains the information that identifies the LSM. | 
|---|
| 80 | */ | 
|---|
| 81 | struct lsm_id { | 
|---|
| 82 | const char *name; | 
|---|
| 83 | u64 id; | 
|---|
| 84 | }; | 
|---|
| 85 |  | 
|---|
| 86 | /* | 
|---|
| 87 | * Security module hook list structure. | 
|---|
| 88 | * For use with generic list macros for common operations. | 
|---|
| 89 | * | 
|---|
| 90 | * struct security_hook_list - Contents of a cacheable, mappable object. | 
|---|
| 91 | * @scalls: The beginning of the array of static calls assigned to this hook. | 
|---|
| 92 | * @hook: The callback for the hook. | 
|---|
| 93 | * @lsm: The name of the lsm that owns this hook. | 
|---|
| 94 | */ | 
|---|
| 95 | struct security_hook_list { | 
|---|
| 96 | struct lsm_static_call *scalls; | 
|---|
| 97 | union security_list_options hook; | 
|---|
| 98 | const struct lsm_id *lsmid; | 
|---|
| 99 | } __randomize_layout; | 
|---|
| 100 |  | 
|---|
| 101 | /* | 
|---|
| 102 | * Security blob size or offset data. | 
|---|
| 103 | */ | 
|---|
| 104 | struct lsm_blob_sizes { | 
|---|
| 105 | int lbs_cred; | 
|---|
| 106 | int lbs_file; | 
|---|
| 107 | int lbs_ib; | 
|---|
| 108 | int lbs_inode; | 
|---|
| 109 | int lbs_sock; | 
|---|
| 110 | int lbs_superblock; | 
|---|
| 111 | int lbs_ipc; | 
|---|
| 112 | int lbs_key; | 
|---|
| 113 | int lbs_msg_msg; | 
|---|
| 114 | int lbs_perf_event; | 
|---|
| 115 | int lbs_task; | 
|---|
| 116 | int lbs_xattr_count; /* number of xattr slots in new_xattrs array */ | 
|---|
| 117 | int lbs_tun_dev; | 
|---|
| 118 | int lbs_bdev; | 
|---|
| 119 | int lbs_bpf_map; | 
|---|
| 120 | int lbs_bpf_prog; | 
|---|
| 121 | int lbs_bpf_token; | 
|---|
| 122 | }; | 
|---|
| 123 |  | 
|---|
| 124 | /* | 
|---|
| 125 | * LSM_RET_VOID is used as the default value in LSM_HOOK definitions for void | 
|---|
| 126 | * LSM hooks (in include/linux/lsm_hook_defs.h). | 
|---|
| 127 | */ | 
|---|
| 128 | #define LSM_RET_VOID ((void) 0) | 
|---|
| 129 |  | 
|---|
| 130 | /* | 
|---|
| 131 | * Initializing a security_hook_list structure takes | 
|---|
| 132 | * up a lot of space in a source file. This macro takes | 
|---|
| 133 | * care of the common case and reduces the amount of | 
|---|
| 134 | * text involved. | 
|---|
| 135 | */ | 
|---|
| 136 | #define LSM_HOOK_INIT(NAME, HOOK)			\ | 
|---|
| 137 | {						\ | 
|---|
| 138 | .scalls = static_calls_table.NAME,	\ | 
|---|
| 139 | .hook = { .NAME = HOOK }		\ | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | extern void security_add_hooks(struct security_hook_list *hooks, int count, | 
|---|
| 143 | const struct lsm_id *lsmid); | 
|---|
| 144 |  | 
|---|
| 145 | #define LSM_FLAG_LEGACY_MAJOR	BIT(0) | 
|---|
| 146 | #define LSM_FLAG_EXCLUSIVE	BIT(1) | 
|---|
| 147 |  | 
|---|
| 148 | enum lsm_order { | 
|---|
| 149 | LSM_ORDER_FIRST = -1,	/* This is only for capabilities. */ | 
|---|
| 150 | LSM_ORDER_MUTABLE = 0, | 
|---|
| 151 | LSM_ORDER_LAST = 1,	/* This is only for integrity. */ | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 | struct lsm_info { | 
|---|
| 155 | const char *name;	/* Required. */ | 
|---|
| 156 | enum lsm_order order;	/* Optional: default is LSM_ORDER_MUTABLE */ | 
|---|
| 157 | unsigned long flags;	/* Optional: flags describing LSM */ | 
|---|
| 158 | int *enabled;		/* Optional: controlled by CONFIG_LSM */ | 
|---|
| 159 | int (*init)(void);	/* Required. */ | 
|---|
| 160 | struct lsm_blob_sizes *blobs; /* Optional: for blob sharing. */ | 
|---|
| 161 | }; | 
|---|
| 162 |  | 
|---|
| 163 | #define DEFINE_LSM(lsm)							\ | 
|---|
| 164 | static struct lsm_info __lsm_##lsm				\ | 
|---|
| 165 | __used __section(".lsm_info.init")			\ | 
|---|
| 166 | __aligned(sizeof(unsigned long)) | 
|---|
| 167 |  | 
|---|
| 168 | #define DEFINE_EARLY_LSM(lsm)						\ | 
|---|
| 169 | static struct lsm_info __early_lsm_##lsm			\ | 
|---|
| 170 | __used __section(".early_lsm_info.init")		\ | 
|---|
| 171 | __aligned(sizeof(unsigned long)) | 
|---|
| 172 |  | 
|---|
| 173 | /* DO NOT tamper with these variables outside of the LSM framework */ | 
|---|
| 174 | extern char *lsm_names; | 
|---|
| 175 | extern struct lsm_static_calls_table static_calls_table __ro_after_init; | 
|---|
| 176 | extern struct lsm_info __start_lsm_info[], __end_lsm_info[]; | 
|---|
| 177 | extern struct lsm_info __start_early_lsm_info[], __end_early_lsm_info[]; | 
|---|
| 178 |  | 
|---|
| 179 | /** | 
|---|
| 180 | * lsm_get_xattr_slot - Return the next available slot and increment the index | 
|---|
| 181 | * @xattrs: array storing LSM-provided xattrs | 
|---|
| 182 | * @xattr_count: number of already stored xattrs (updated) | 
|---|
| 183 | * | 
|---|
| 184 | * Retrieve the first available slot in the @xattrs array to fill with an xattr, | 
|---|
| 185 | * and increment @xattr_count. | 
|---|
| 186 | * | 
|---|
| 187 | * Return: The slot to fill in @xattrs if non-NULL, NULL otherwise. | 
|---|
| 188 | */ | 
|---|
| 189 | static inline struct xattr *lsm_get_xattr_slot(struct xattr *xattrs, | 
|---|
| 190 | int *xattr_count) | 
|---|
| 191 | { | 
|---|
| 192 | if (unlikely(!xattrs)) | 
|---|
| 193 | return NULL; | 
|---|
| 194 | return &xattrs[(*xattr_count)++]; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | #endif /* ! __LINUX_LSM_HOOKS_H */ | 
|---|
| 198 |  | 
|---|