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 <net/netlabel.h> 19 20/* 21 * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is 22 * bigger than can be used, and 24 is the next lower multiple 23 * of 8, and there are too many issues if there isn't space set 24 * aside for the terminating null byte. 25 */ 26#define SMK_MAXLEN 23 27#define SMK_LABELLEN (SMK_MAXLEN+1) 28 29struct superblock_smack { 30 char *smk_root; 31 char *smk_floor; 32 char *smk_hat; 33 char *smk_default; 34 int smk_initialized; 35 spinlock_t smk_sblock; /* for initialization */ 36}; 37 38struct socket_smack { 39 char *smk_out; /* outbound label */ 40 char *smk_in; /* inbound label */ 41 char smk_packet[SMK_LABELLEN]; /* TCP peer label */ 42}; 43 44/* 45 * Inode smack data 46 */ 47struct inode_smack { 48 char *smk_inode; /* label of the fso */ 49 struct mutex smk_lock; /* initialization lock */ 50 int smk_flags; /* smack inode flags */ 51}; 52 53#define SMK_INODE_INSTANT 0x01 /* inode is instantiated */ 54 55/* 56 * A label access rule. 57 */ 58struct smack_rule { 59 char *smk_subject; 60 char *smk_object; 61 int smk_access; 62}; 63 64/* 65 * An entry in the table of permitted label accesses. 66 */ 67struct smk_list_entry { 68 struct smk_list_entry *smk_next; 69 struct smack_rule smk_rule; 70}; 71 72/* 73 * An entry in the table mapping smack values to 74 * CIPSO level/category-set values. 75 */ 76struct smack_cipso { 77 int smk_level; 78 char smk_catset[SMK_LABELLEN]; 79}; 80 81/* 82 * This is the repository for labels seen so that it is 83 * not necessary to keep allocating tiny chuncks of memory 84 * and so that they can be shared. 85 * 86 * Labels are never modified in place. Anytime a label 87 * is imported (e.g. xattrset on a file) the list is checked 88 * for it and it is added if it doesn't exist. The address 89 * is passed out in either case. Entries are added, but 90 * never deleted. 91 * 92 * Since labels are hanging around anyway it doesn't 93 * hurt to maintain a secid for those awkward situations 94 * where kernel components that ought to use LSM independent 95 * interfaces don't. The secid should go away when all of 96 * these components have been repaired. 97 * 98 * If there is a cipso value associated with the label it 99 * gets stored here, too. This will most likely be rare as 100 * the cipso direct mapping in used internally. 101 */ 102struct smack_known { 103 struct smack_known *smk_next; 104 char smk_known[SMK_LABELLEN]; 105 u32 smk_secid; 106 struct smack_cipso *smk_cipso; 107 spinlock_t smk_cipsolock; /* for changing cipso map */ 108}; 109 110/* 111 * Mount options 112 */ 113#define SMK_FSDEFAULT "smackfsdef=" 114#define SMK_FSFLOOR "smackfsfloor=" 115#define SMK_FSHAT "smackfshat=" 116#define SMK_FSROOT "smackfsroot=" 117 118/* 119 * xattr names 120 */ 121#define XATTR_SMACK_SUFFIX "SMACK64" 122#define XATTR_SMACK_IPIN "SMACK64IPIN" 123#define XATTR_SMACK_IPOUT "SMACK64IPOUT" 124#define XATTR_NAME_SMACK XATTR_SECURITY_PREFIX XATTR_SMACK_SUFFIX 125#define XATTR_NAME_SMACKIPIN XATTR_SECURITY_PREFIX XATTR_SMACK_IPIN 126#define XATTR_NAME_SMACKIPOUT XATTR_SECURITY_PREFIX XATTR_SMACK_IPOUT 127 128/* 129 * smackfs macic number 130 */ 131#define SMACK_MAGIC 0x43415d53 /* "SMAC" */ 132 133/* 134 * A limit on the number of entries in the lists 135 * makes some of the list administration easier. 136 */ 137#define SMACK_LIST_MAX 10000 138 139/* 140 * CIPSO defaults. 141 */ 142#define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */ 143#define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */ 144#define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */ 145#define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */ 146#define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */ 147 148/* 149 * Just to make the common cases easier to deal with 150 */ 151#define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) 152#define MAY_ANYREAD (MAY_READ | MAY_EXEC) 153#define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND) 154#define MAY_READWRITE (MAY_READ | MAY_WRITE) 155#define MAY_NOT 0 156 157/* 158 * These functions are in smack_lsm.c 159 */ 160struct inode_smack *new_inode_smack(char *); 161 162/* 163 * These functions are in smack_access.c 164 */ 165int smk_access(char *, char *, int); 166int smk_curacc(char *, u32); 167int smack_to_cipso(const char *, struct smack_cipso *); 168void smack_from_cipso(u32, char *, char *); 169char *smack_from_secid(const u32); 170char *smk_import(const char *, int); 171struct smack_known *smk_import_entry(const char *, int); 172u32 smack_to_secid(const char *); 173 174/* 175 * Shared data. 176 */ 177extern int smack_cipso_direct; 178extern int smack_net_nltype; 179extern char *smack_net_ambient; 180 181extern struct smack_known *smack_known; 182extern struct smack_known smack_known_floor; 183extern struct smack_known smack_known_hat; 184extern struct smack_known smack_known_huh; 185extern struct smack_known smack_known_invalid; 186extern struct smack_known smack_known_star; 187extern struct smack_known smack_known_unset; 188 189extern struct smk_list_entry *smack_list; 190 191/* 192 * Stricly for CIPSO level manipulation. 193 * Set the category bit number in a smack label sized buffer. 194 */ 195static inline void smack_catset_bit(int cat, char *catsetp) 196{ 197 if (cat > SMK_LABELLEN * 8) 198 return; 199 200 catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8); 201} 202 203/* 204 * Present a pointer to the smack label in an inode blob. 205 */ 206static inline char *smk_of_inode(const struct inode *isp) 207{ 208 struct inode_smack *sip = isp->i_security; 209 return sip->smk_inode; 210} 211 212#endif /* _SECURITY_SMACK_H */ 213

