Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 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> |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 18 | #include <linux/security.h> |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 19 | #include <linux/in.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 20 | #include <net/netlabel.h> |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame^] | 21 | #include <linux/list.h> |
| 22 | #include <linux/rculist.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * Why 23? CIPSO is constrained to 30, so a 32 byte buffer is |
| 26 | * bigger than can be used, and 24 is the next lower multiple |
| 27 | * of 8, and there are too many issues if there isn't space set |
| 28 | * aside for the terminating null byte. |
| 29 | */ |
| 30 | #define SMK_MAXLEN 23 |
| 31 | #define SMK_LABELLEN (SMK_MAXLEN+1) |
| 32 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 33 | struct superblock_smack { |
| 34 | char *smk_root; |
| 35 | char *smk_floor; |
| 36 | char *smk_hat; |
| 37 | char *smk_default; |
| 38 | int smk_initialized; |
| 39 | spinlock_t smk_sblock; /* for initialization */ |
| 40 | }; |
| 41 | |
| 42 | struct socket_smack { |
| 43 | char *smk_out; /* outbound label */ |
| 44 | char *smk_in; /* inbound label */ |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 45 | int smk_labeled; /* label scheme */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 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 | struct mutex smk_lock; /* initialization lock */ |
| 55 | int smk_flags; /* smack inode flags */ |
| 56 | }; |
| 57 | |
| 58 | #define SMK_INODE_INSTANT 0x01 /* inode is instantiated */ |
| 59 | |
| 60 | /* |
| 61 | * A label access rule. |
| 62 | */ |
| 63 | struct smack_rule { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame^] | 64 | struct list_head list; |
| 65 | char *smk_subject; |
| 66 | char *smk_object; |
| 67 | int smk_access; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /* |
| 71 | * An entry in the table mapping smack values to |
| 72 | * CIPSO level/category-set values. |
| 73 | */ |
| 74 | struct smack_cipso { |
| 75 | int smk_level; |
| 76 | char smk_catset[SMK_LABELLEN]; |
| 77 | }; |
| 78 | |
| 79 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 80 | * An entry in the table identifying hosts. |
| 81 | */ |
| 82 | struct smk_netlbladdr { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame^] | 83 | struct list_head list; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 84 | struct sockaddr_in smk_host; /* network address */ |
| 85 | struct in_addr smk_mask; /* network mask */ |
| 86 | char *smk_label; /* label */ |
| 87 | }; |
| 88 | |
| 89 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 90 | * This is the repository for labels seen so that it is |
| 91 | * not necessary to keep allocating tiny chuncks of memory |
| 92 | * and so that they can be shared. |
| 93 | * |
| 94 | * Labels are never modified in place. Anytime a label |
| 95 | * is imported (e.g. xattrset on a file) the list is checked |
| 96 | * for it and it is added if it doesn't exist. The address |
| 97 | * is passed out in either case. Entries are added, but |
| 98 | * never deleted. |
| 99 | * |
| 100 | * Since labels are hanging around anyway it doesn't |
| 101 | * hurt to maintain a secid for those awkward situations |
| 102 | * where kernel components that ought to use LSM independent |
| 103 | * interfaces don't. The secid should go away when all of |
| 104 | * these components have been repaired. |
| 105 | * |
| 106 | * If there is a cipso value associated with the label it |
| 107 | * gets stored here, too. This will most likely be rare as |
| 108 | * the cipso direct mapping in used internally. |
| 109 | */ |
| 110 | struct smack_known { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame^] | 111 | struct list_head list; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 112 | char smk_known[SMK_LABELLEN]; |
| 113 | u32 smk_secid; |
| 114 | struct smack_cipso *smk_cipso; |
| 115 | spinlock_t smk_cipsolock; /* for changing cipso map */ |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | * Mount options |
| 120 | */ |
| 121 | #define SMK_FSDEFAULT "smackfsdef=" |
| 122 | #define SMK_FSFLOOR "smackfsfloor=" |
| 123 | #define SMK_FSHAT "smackfshat=" |
| 124 | #define SMK_FSROOT "smackfsroot=" |
| 125 | |
| 126 | /* |
| 127 | * xattr names |
| 128 | */ |
| 129 | #define XATTR_SMACK_SUFFIX "SMACK64" |
| 130 | #define XATTR_SMACK_IPIN "SMACK64IPIN" |
| 131 | #define XATTR_SMACK_IPOUT "SMACK64IPOUT" |
| 132 | #define XATTR_NAME_SMACK XATTR_SECURITY_PREFIX XATTR_SMACK_SUFFIX |
| 133 | #define XATTR_NAME_SMACKIPIN XATTR_SECURITY_PREFIX XATTR_SMACK_IPIN |
| 134 | #define XATTR_NAME_SMACKIPOUT XATTR_SECURITY_PREFIX XATTR_SMACK_IPOUT |
| 135 | |
| 136 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 137 | * How communications on this socket are treated. |
| 138 | * Usually it's determined by the underlying netlabel code |
| 139 | * but there are certain cases, including single label hosts |
| 140 | * and potentially single label interfaces for which the |
| 141 | * treatment can not be known in advance. |
| 142 | * |
| 143 | * The possibility of additional labeling schemes being |
| 144 | * introduced in the future exists as well. |
| 145 | */ |
| 146 | #define SMACK_UNLABELED_SOCKET 0 |
| 147 | #define SMACK_CIPSO_SOCKET 1 |
| 148 | |
| 149 | /* |
| 150 | * smackfs magic number |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 151 | * smackfs macic number |
| 152 | */ |
| 153 | #define SMACK_MAGIC 0x43415d53 /* "SMAC" */ |
| 154 | |
| 155 | /* |
| 156 | * A limit on the number of entries in the lists |
| 157 | * makes some of the list administration easier. |
| 158 | */ |
| 159 | #define SMACK_LIST_MAX 10000 |
| 160 | |
| 161 | /* |
| 162 | * CIPSO defaults. |
| 163 | */ |
| 164 | #define SMACK_CIPSO_DOI_DEFAULT 3 /* Historical */ |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 165 | #define SMACK_CIPSO_DOI_INVALID -1 /* Not a DOI */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 166 | #define SMACK_CIPSO_DIRECT_DEFAULT 250 /* Arbitrary */ |
| 167 | #define SMACK_CIPSO_MAXCATVAL 63 /* Bigger gets harder */ |
| 168 | #define SMACK_CIPSO_MAXLEVEL 255 /* CIPSO 2.2 standard */ |
| 169 | #define SMACK_CIPSO_MAXCATNUM 239 /* CIPSO 2.2 standard */ |
| 170 | |
| 171 | /* |
| 172 | * Just to make the common cases easier to deal with |
| 173 | */ |
| 174 | #define MAY_ANY (MAY_READ | MAY_WRITE | MAY_APPEND | MAY_EXEC) |
| 175 | #define MAY_ANYREAD (MAY_READ | MAY_EXEC) |
| 176 | #define MAY_ANYWRITE (MAY_WRITE | MAY_APPEND) |
| 177 | #define MAY_READWRITE (MAY_READ | MAY_WRITE) |
| 178 | #define MAY_NOT 0 |
| 179 | |
| 180 | /* |
| 181 | * These functions are in smack_lsm.c |
| 182 | */ |
| 183 | struct inode_smack *new_inode_smack(char *); |
| 184 | |
| 185 | /* |
| 186 | * These functions are in smack_access.c |
| 187 | */ |
| 188 | int smk_access(char *, char *, int); |
| 189 | int smk_curacc(char *, u32); |
| 190 | int smack_to_cipso(const char *, struct smack_cipso *); |
| 191 | void smack_from_cipso(u32, char *, char *); |
| 192 | char *smack_from_secid(const u32); |
| 193 | char *smk_import(const char *, int); |
| 194 | struct smack_known *smk_import_entry(const char *, int); |
| 195 | u32 smack_to_secid(const char *); |
| 196 | |
| 197 | /* |
| 198 | * Shared data. |
| 199 | */ |
| 200 | extern int smack_cipso_direct; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 201 | extern char *smack_net_ambient; |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 202 | extern char *smack_onlycap; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 203 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 204 | extern struct smack_known smack_known_floor; |
| 205 | extern struct smack_known smack_known_hat; |
| 206 | extern struct smack_known smack_known_huh; |
| 207 | extern struct smack_known smack_known_invalid; |
| 208 | extern struct smack_known smack_known_star; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 209 | extern struct smack_known smack_known_web; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 210 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame^] | 211 | extern struct list_head smack_known_list; |
| 212 | extern struct list_head smack_rule_list; |
| 213 | extern struct list_head smk_netlbladdr_list; |
| 214 | |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 215 | extern struct security_operations smack_ops; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * Stricly for CIPSO level manipulation. |
| 219 | * Set the category bit number in a smack label sized buffer. |
| 220 | */ |
| 221 | static inline void smack_catset_bit(int cat, char *catsetp) |
| 222 | { |
| 223 | if (cat > SMK_LABELLEN * 8) |
| 224 | return; |
| 225 | |
| 226 | catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Present a pointer to the smack label in an inode blob. |
| 231 | */ |
| 232 | static inline char *smk_of_inode(const struct inode *isp) |
| 233 | { |
| 234 | struct inode_smack *sip = isp->i_security; |
| 235 | return sip->smk_inode; |
| 236 | } |
| 237 | |
| 238 | #endif /* _SECURITY_SMACK_H */ |