1 /* 2 * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, version 2. 7 * 8 * Author: 9 * Casey Schaufler <casey@schaufler-ca.com> 10 * 11 */ 12 13 #ifndef _SECURITY_SMACK_H 14 #define _SECURITY_SMACK_H 15 16 #include <linux/capability.h> 17 #include <linux/spinlock.h> 18 #include <linux/security.h> 19 #include <linux/in.h> 20 #include <net/netlabel.h> 21 #include <linux/list.h> 22 #include <linux/rculist.h> 23 #include <linux/lsm_audit.h> 24 25 /* 26 * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is 27 * bigger than can be used, and 24 is the next lower multiple 28 * of 8, and there are too many issues if there isn't space set 29 * aside for the terminating null byte. 30 */ 31 #define SMK_MAXLEN 23 32 #define SMK_LABELLEN (SMK_MAXLEN+1) 33 34 struct superblock_smack { 35 char *smk_root; 36 char *smk_floor; 37 char *smk_hat; 38 char *smk_default; 39 int smk_initialized; 40 spinlock_t smk_sblock; /* for initialization */ 41 }; 42 43 struct socket_smack { 44 char *smk_out; /* outbound label */ 45 char *smk_in; /* inbound label */ 46 char smk_packet[SMK_LABELLEN]; /* TCP peer label */ 47 }; 48 49 /* 50 * Inode smack data 51 */ 52 struct inode_smack { 53 char *smk_inode; /* label of the fso */ 54 char *smk_task; /* label of the task */ 55 struct mutex smk_lock; /* initialization lock */ 56 int smk_flags; /* smack inode flags */ 57 }; 58 59 struct task_smack { 60 char *smk_task; /* label used for access control */ 61 char *smk_forked; /* label when forked */ 62 }; 63 64 #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */ 65 #define SMK_INODE_TRANSMUTE 0x02 /* directory is transmuting */ 66 67 /* 68 * A label access rule. 69 */ 70 struct smack_rule { 71 struct list_head list; 72 char *smk_subject; 73 char *smk_object; 74 int smk_access; 75 }; 76 77 /* 78 * An entry in the table mapping smack values to 79 * CIPSO level/category-set values. 80 */ 81 struct smack_cipso { 82 int smk_level; 83 char smk_catset[SMK_LABELLEN]; 84 }; 85 86 /* 87 * An entry in the table identifying hosts. 88 */ 89 struct smk_netlbladdr { 90 struct list_head list; 91 struct sockaddr_in smk_host; /* network address */ 92 struct in_addr smk_mask; /* network mask */ 93 char *smk_label; /* label */ 94 }; 95 96 /* 97 * This is the repository for labels seen so that it is 98 * not necessary to keep allocating tiny chuncks of memory 99 * and so that they can be shared. 100 * 101 * Labels are never modified in place. Anytime a label 102 * is imported (e.g. xattrset on a file) the list is checked 103 * for it and it is added if it doesn't exist. The address 104 * is passed out in either case. Entries are added, but 105 * never deleted. 106 * 107 * Since labels are hanging around anyway it doesn't 108 * hurt to maintain a secid for those awkward situations 109 * where kernel components that ought to use LSM independent 110 * interfaces don't. The secid should go away when all of 111 * these components have been repaired. 112 * 113 * If there is a cipso value associated with the label it 114 * gets stored here, too. This will most likely be rare as 115 * the cipso direct mapping in used internally. 116 */ 117 struct smack_known { 118 struct list_head list; 119 char smk_known[SMK_LABELLEN]; 120 u32 smk_secid; 121 struct smack_cipso *smk_cipso; 122 spinlock_t smk_cipsolock; /* for changing cipso map */ 123 }; 124 125 /* 126 * Mount options 127 */ 128 #define SMK_FSDEFAULT "smackfsdef=" 129 #define SMK_FSFLOOR "smackfsfloor=" 130 #define SMK_FSHAT "smackfshat=" 131 #define SMK_FSROOT "smackfsroot=" 132 133 #define SMACK_CIPSO_OPTION "-CIPSO" 134 135 /* 136 * How communications on this socket are treated. 137 * Usually it's determined by the underlying netlabel code 138 * but there are certain cases, including single label hosts 139 * and potentially single label interfaces for which the 140 * treatment can not be known in advance. 141 * 142 * The possibility of additional labeling schemes being 143 * introduced in the future exists as well. 144 */ 145 #define SMACK_UNLABELED_SOCKET 0 146 #define SMACK_CIPSO_SOCKET 1 147 148 /* 149 * smackfs magic number 150 * smackfs macic number 151 */ 152 #define SMACK_MAGIC 0x43415d53 /* "SMAC" */ 153 154 /* 155 * A limit on the number of entries in the lists 156 * makes some of the list administration easier. 157 */ 158 #define SMACK_LIST_MAX 10000 159 160 /* 161 * CIPSO defaults. 162 */ 163 #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */ 164 #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */ 165 #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */ 166 #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */ 167 #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */ 168 #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */ 169 170 /* 171 * Flag for transmute access 172 */ 173 #define MAY_TRANSMUTE 64 174 /* 175 * Just to make the common cases easier to deal with 176 */ 177 #define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) 178 #define MAY_ANYREAD (MAY_READ | MAY_EXEC) 179 #define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND) 180 #define MAY_READWRITE (MAY_READ | MAY_WRITE) 181 #define MAY_NOT 0 182 183 /* 184 * Number of access types used by Smack (rwxa) 185 */ 186 #define SMK_NUM_ACCESS_TYPE 4 187 188 /* 189 * Smack audit data; is empty if CONFIG_AUDIT not set 190 * to save some stack 191 */ 192 struct smk_audit_info { 193 #ifdef CONFIG_AUDIT 194 struct common_audit_data a; 195 #endif 196 }; 197 /* 198 * These functions are in smack_lsm.c 199 */ 200 struct inode_smack *new_inode_smack(char *); 201 202 /* 203 * These functions are in smack_access.c 204 */ 205 int smk_access_entry(char *, char *); 206 int smk_access(char *, char *, int, struct smk_audit_info *); 207 int smk_curacc(char *, u32, struct smk_audit_info *); 208 int smack_to_cipso(const char *, struct smack_cipso *); 209 void smack_from_cipso(u32, char *, char *); 210 char *smack_from_secid(const u32); 211 char *smk_import(const char *, int); 212 struct smack_known *smk_import_entry(const char *, int); 213 u32 smack_to_secid(const char *); 214 215 /* 216 * Shared data. 217 */ 218 extern int smack_cipso_direct; 219 extern char *smack_net_ambient; 220 extern char *smack_onlycap; 221 extern const char *smack_cipso_option; 222 223 extern struct smack_known smack_known_floor; 224 extern struct smack_known smack_known_hat; 225 extern struct smack_known smack_known_huh; 226 extern struct smack_known smack_known_invalid; 227 extern struct smack_known smack_known_star; 228 extern struct smack_known smack_known_web; 229 230 extern struct list_head smack_known_list; 231 extern struct list_head smack_rule_list; 232 extern struct list_head smk_netlbladdr_list; 233 234 extern struct security_operations smack_ops; 235 236 /* 237 * Stricly for CIPSO level manipulation. 238 * Set the category bit number in a smack label sized buffer. 239 */ 240 static inline void smack_catset_bit(int cat, char *catsetp) 241 { 242 if (cat > SMK_LABELLEN * 8) 243 return; 244 245 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8); 246 } 247 248 /* 249 * Is the directory transmuting? 250 */ 251 static inline int smk_inode_transmutable(const struct inode *isp) 252 { 253 struct inode_smack *sip = isp->i_security; 254 return (sip->smk_flags & SMK_INODE_TRANSMUTE) != 0; 255 } 256 257 /* 258 * Present a pointer to the smack label in an inode blob. 259 */ 260 static inline char *smk_of_inode(const struct inode *isp) 261 { 262 struct inode_smack *sip = isp->i_security; 263 return sip->smk_inode; 264 } 265 266 /* 267 * Present a pointer to the smack label in an task blob. 268 */ 269 static inline char *smk_of_task(const struct task_smack *tsp) 270 { 271 return tsp->smk_task; 272 } 273 274 /* 275 * Present a pointer to the forked smack label in an task blob. 276 */ 277 static inline char *smk_of_forked(const struct task_smack *tsp) 278 { 279 return tsp->smk_forked; 280 } 281 282 /* 283 * Present a pointer to the smack label in the current task blob. 284 */ 285 static inline char *smk_of_current(void) 286 { 287 return smk_of_task(current_security()); 288 } 289 290 /* 291 * logging functions 292 */ 293 #define SMACK_AUDIT_DENIED 0x1 294 #define SMACK_AUDIT_ACCEPT 0x2 295 extern int log_policy; 296 297 void smack_log(char *subject_label, char *object_label, 298 int request, 299 int result, struct smk_audit_info *auditdata); 300 301 #ifdef CONFIG_AUDIT 302 303 /* 304 * some inline functions to set up audit data 305 * they do nothing if CONFIG_AUDIT is not set 306 * 307 */ 308 static inline void smk_ad_init(struct smk_audit_info *a, const char *func, 309 char type) 310 { 311 memset(a, 0, sizeof(*a)); 312 a->a.type = type; 313 a->a.smack_audit_data.function = func; 314 } 315 316 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a, 317 struct task_struct *t) 318 { 319 a->a.u.tsk = t; 320 } 321 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a, 322 struct dentry *d) 323 { 324 a->a.u.fs.path.dentry = d; 325 } 326 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a, 327 struct vfsmount *m) 328 { 329 a->a.u.fs.path.mnt = m; 330 } 331 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a, 332 struct inode *i) 333 { 334 a->a.u.fs.inode = i; 335 } 336 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a, 337 struct path p) 338 { 339 a->a.u.fs.path = p; 340 } 341 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a, 342 struct sock *sk) 343 { 344 a->a.u.net.sk = sk; 345 } 346 347 #else /* no AUDIT */ 348 349 static inline void smk_ad_init(struct smk_audit_info *a, const char *func, 350 char type) 351 { 352 } 353 static inline void smk_ad_setfield_u_tsk(struct smk_audit_info *a, 354 struct task_struct *t) 355 { 356 } 357 static inline void smk_ad_setfield_u_fs_path_dentry(struct smk_audit_info *a, 358 struct dentry *d) 359 { 360 } 361 static inline void smk_ad_setfield_u_fs_path_mnt(struct smk_audit_info *a, 362 struct vfsmount *m) 363 { 364 } 365 static inline void smk_ad_setfield_u_fs_inode(struct smk_audit_info *a, 366 struct inode *i) 367 { 368 } 369 static inline void smk_ad_setfield_u_fs_path(struct smk_audit_info *a, 370 struct path p) 371 { 372 } 373 static inline void smk_ad_setfield_u_net_sk(struct smk_audit_info *a, 374 struct sock *sk) 375 { 376 } 377 #endif 378 379 #endif /* _SECURITY_SMACK_H */ 380