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