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 | * Authors: |
| 9 | * Casey Schaufler <casey@schaufler-ca.com> |
| 10 | * Ahmed S. Darwish <darwish.07@gmail.com> |
| 11 | * |
| 12 | * Special thanks to the authors of selinuxfs. |
| 13 | * |
| 14 | * Karl MacMillan <kmacmillan@tresys.com> |
| 15 | * James Morris <jmorris@redhat.com> |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/vmalloc.h> |
| 21 | #include <linux/security.h> |
| 22 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 24 | #include <net/net_namespace.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 25 | #include <net/netlabel.h> |
| 26 | #include <net/cipso_ipv4.h> |
| 27 | #include <linux/seq_file.h> |
| 28 | #include <linux/ctype.h> |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 29 | #include <linux/audit.h> |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 30 | #include "smack.h" |
| 31 | |
| 32 | /* |
| 33 | * smackfs pseudo filesystem. |
| 34 | */ |
| 35 | |
| 36 | enum smk_inos { |
| 37 | SMK_ROOT_INO = 2, |
| 38 | SMK_LOAD = 3, /* load policy */ |
| 39 | SMK_CIPSO = 4, /* load label -> CIPSO mapping */ |
| 40 | SMK_DOI = 5, /* CIPSO DOI */ |
| 41 | SMK_DIRECT = 6, /* CIPSO level indicating direct label */ |
| 42 | SMK_AMBIENT = 7, /* internet ambient label */ |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 43 | SMK_NETLBLADDR = 8, /* single label hosts */ |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 44 | SMK_ONLYCAP = 9, /* the only "capable" label */ |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 45 | SMK_LOGGING = 10, /* logging */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 46 | SMK_LOAD_SELF = 11, /* task specific rules */ |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 47 | SMK_ACCESSES = 12, /* access policy */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * List locks |
| 52 | */ |
| 53 | static DEFINE_MUTEX(smack_list_lock); |
| 54 | static DEFINE_MUTEX(smack_cipso_lock); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 55 | static DEFINE_MUTEX(smack_ambient_lock); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 56 | static DEFINE_MUTEX(smk_netlbladdr_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * This is the "ambient" label for network traffic. |
| 60 | * If it isn't somehow marked, use this. |
| 61 | * It can be reset via smackfs/ambient |
| 62 | */ |
| 63 | char *smack_net_ambient = smack_known_floor.smk_known; |
| 64 | |
| 65 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 66 | * This is the level in a CIPSO header that indicates a |
| 67 | * smack label is contained directly in the category set. |
| 68 | * It can be reset via smackfs/direct |
| 69 | */ |
| 70 | int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT; |
| 71 | |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Unless a process is running with this label even |
| 74 | * having CAP_MAC_OVERRIDE isn't enough to grant |
| 75 | * privilege to violate MAC policy. If no label is |
| 76 | * designated (the NULL case) capabilities apply to |
| 77 | * everyone. It is expected that the hat (^) label |
| 78 | * will be used if any label is used. |
| 79 | */ |
| 80 | char *smack_onlycap; |
| 81 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 82 | /* |
| 83 | * Certain IP addresses may be designated as single label hosts. |
| 84 | * Packets are sent there unlabeled, but only from tasks that |
| 85 | * can write to the specified label. |
| 86 | */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 87 | |
| 88 | LIST_HEAD(smk_netlbladdr_list); |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 89 | |
| 90 | /* |
| 91 | * Rule lists are maintained for each label. |
| 92 | * This master list is just for reading /smack/load. |
| 93 | */ |
| 94 | struct smack_master_list { |
| 95 | struct list_head list; |
| 96 | struct smack_rule *smk_rule; |
| 97 | }; |
| 98 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 99 | LIST_HEAD(smack_rule_list); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 100 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 101 | static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 102 | |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 103 | const char *smack_cipso_option = SMACK_CIPSO_OPTION; |
| 104 | |
| 105 | |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 106 | #define SEQ_READ_FINISHED ((loff_t)-1) |
| 107 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 108 | #define SEQ_READ_FINISHED 1 |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 109 | */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 110 | |
| 111 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 112 | * Values for parsing cipso rules |
| 113 | * SMK_DIGITLEN: Length of a digit field in a rule. |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 114 | * SMK_CIPSOMIN: Minimum possible cipso rule length. |
| 115 | * SMK_CIPSOMAX: Maximum possible cipso rule length. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 116 | */ |
| 117 | #define SMK_DIGITLEN 4 |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 118 | #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN) |
| 119 | #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN) |
| 120 | |
| 121 | /* |
| 122 | * Values for parsing MAC rules |
| 123 | * SMK_ACCESS: Maximum possible combination of access permissions |
| 124 | * SMK_ACCESSLEN: Maximum length for a rule access field |
| 125 | * SMK_LOADLEN: Smack rule length |
| 126 | */ |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 127 | #define SMK_OACCESS "rwxa" |
| 128 | #define SMK_ACCESS "rwxat" |
| 129 | #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1) |
| 130 | #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1) |
| 131 | #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN) |
| 132 | #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN) |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 133 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 134 | /** |
| 135 | * smk_netlabel_audit_set - fill a netlbl_audit struct |
| 136 | * @nap: structure to fill |
| 137 | */ |
| 138 | static void smk_netlabel_audit_set(struct netlbl_audit *nap) |
| 139 | { |
| 140 | nap->loginuid = audit_get_loginuid(current); |
| 141 | nap->sessionid = audit_get_sessionid(current); |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 142 | nap->secid = smack_to_secid(smk_of_current()); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Values for parsing single label host rules |
| 147 | * "1.2.3.4 X" |
| 148 | * "192.168.138.129/32 abcdefghijklmnopqrstuvw" |
| 149 | */ |
| 150 | #define SMK_NETLBLADDRMIN 9 |
| 151 | #define SMK_NETLBLADDRMAX 42 |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 152 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 153 | /** |
| 154 | * smk_set_access - add a rule to the rule list |
| 155 | * @srp: the new rule to add |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 156 | * @rule_list: the list of rules |
| 157 | * @rule_lock: the rule list lock |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 158 | * |
| 159 | * Looks through the current subject/object/access list for |
| 160 | * the subject/object pair and replaces the access that was |
| 161 | * there. If the pair isn't found add it with the specified |
| 162 | * access. |
Sergio Luis | 81ea714 | 2008-12-22 01:16:15 -0300 | [diff] [blame] | 163 | * |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 164 | * Returns 1 if a rule was found to exist already, 0 if it is new |
Sergio Luis | 81ea714 | 2008-12-22 01:16:15 -0300 | [diff] [blame] | 165 | * Returns 0 if nothing goes wrong or -ENOMEM if it fails |
| 166 | * during the allocation of the new pair to add. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 167 | */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 168 | static int smk_set_access(struct smack_rule *srp, struct list_head *rule_list, |
| 169 | struct mutex *rule_lock) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 170 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 171 | struct smack_rule *sp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 172 | int found = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 173 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 174 | mutex_lock(rule_lock); |
| 175 | |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 176 | /* |
| 177 | * Because the object label is less likely to match |
| 178 | * than the subject label check it first |
| 179 | */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 180 | list_for_each_entry_rcu(sp, rule_list, list) { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 181 | if (sp->smk_object == srp->smk_object && |
| 182 | sp->smk_subject == srp->smk_subject) { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 183 | found = 1; |
| 184 | sp->smk_access = srp->smk_access; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 185 | break; |
| 186 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 187 | } |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 188 | if (found == 0) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 189 | list_add_rcu(&srp->list, rule_list); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 190 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 191 | mutex_unlock(rule_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 192 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 193 | return found; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 197 | * smk_parse_rule - parse subject, object and access type |
| 198 | * @data: string to be parsed whose size is SMK_LOADLEN |
| 199 | * @rule: parsed entities are stored in here |
| 200 | */ |
| 201 | static int smk_parse_rule(const char *data, struct smack_rule *rule) |
| 202 | { |
| 203 | rule->smk_subject = smk_import(data, 0); |
| 204 | if (rule->smk_subject == NULL) |
| 205 | return -1; |
| 206 | |
| 207 | rule->smk_object = smk_import(data + SMK_LABELLEN, 0); |
| 208 | if (rule->smk_object == NULL) |
| 209 | return -1; |
| 210 | |
| 211 | rule->smk_access = 0; |
| 212 | |
| 213 | switch (data[SMK_LABELLEN + SMK_LABELLEN]) { |
| 214 | case '-': |
| 215 | break; |
| 216 | case 'r': |
| 217 | case 'R': |
| 218 | rule->smk_access |= MAY_READ; |
| 219 | break; |
| 220 | default: |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 1]) { |
| 225 | case '-': |
| 226 | break; |
| 227 | case 'w': |
| 228 | case 'W': |
| 229 | rule->smk_access |= MAY_WRITE; |
| 230 | break; |
| 231 | default: |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 2]) { |
| 236 | case '-': |
| 237 | break; |
| 238 | case 'x': |
| 239 | case 'X': |
| 240 | rule->smk_access |= MAY_EXEC; |
| 241 | break; |
| 242 | default: |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 3]) { |
| 247 | case '-': |
| 248 | break; |
| 249 | case 'a': |
| 250 | case 'A': |
| 251 | rule->smk_access |= MAY_APPEND; |
| 252 | break; |
| 253 | default: |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | switch (data[SMK_LABELLEN + SMK_LABELLEN + 4]) { |
| 258 | case '-': |
| 259 | break; |
| 260 | case 't': |
| 261 | case 'T': |
| 262 | rule->smk_access |= MAY_TRANSMUTE; |
| 263 | break; |
| 264 | default: |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | /** |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 272 | * smk_write_load_list - write() for any /smack/load |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 273 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 274 | * @buf: where to get the data from |
| 275 | * @count: bytes sent |
| 276 | * @ppos: where to start - must be 0 |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 277 | * @rule_list: the list of rules to write to |
| 278 | * @rule_lock: lock for the rule list |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 279 | * |
| 280 | * Get one smack access rule from above. |
| 281 | * The format is exactly: |
| 282 | * char subject[SMK_LABELLEN] |
| 283 | * char object[SMK_LABELLEN] |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 284 | * char access[SMK_ACCESSLEN] |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 285 | * |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 286 | * writes must be SMK_LABELLEN+SMK_LABELLEN+SMK_ACCESSLEN bytes. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 287 | */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 288 | static ssize_t smk_write_load_list(struct file *file, const char __user *buf, |
| 289 | size_t count, loff_t *ppos, |
| 290 | struct list_head *rule_list, |
| 291 | struct mutex *rule_lock) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 292 | { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 293 | struct smack_master_list *smlp; |
| 294 | struct smack_known *skp; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 295 | struct smack_rule *rule; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 296 | char *data; |
| 297 | int rc = -EINVAL; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 298 | int load = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 299 | |
| 300 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 301 | * No partial writes. |
| 302 | * Enough data must be present. |
| 303 | */ |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 304 | if (*ppos != 0) |
| 305 | return -EINVAL; |
| 306 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 307 | * Minor hack for backward compatibility |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 308 | */ |
| 309 | if (count < (SMK_OLOADLEN) || count > SMK_LOADLEN) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 310 | return -EINVAL; |
| 311 | |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 312 | data = kzalloc(SMK_LOADLEN, GFP_KERNEL); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 313 | if (data == NULL) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | if (copy_from_user(data, buf, count) != 0) { |
| 317 | rc = -EFAULT; |
| 318 | goto out; |
| 319 | } |
| 320 | |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 321 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 322 | * More on the minor hack for backward compatibility |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 323 | */ |
| 324 | if (count == (SMK_OLOADLEN)) |
| 325 | data[SMK_OLOADLEN] = '-'; |
| 326 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 327 | rule = kzalloc(sizeof(*rule), GFP_KERNEL); |
| 328 | if (rule == NULL) { |
| 329 | rc = -ENOMEM; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 330 | goto out; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 331 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 332 | |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 333 | if (smk_parse_rule(data, rule)) |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 334 | goto out_free_rule; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 335 | |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 336 | if (rule_list == NULL) { |
| 337 | load = 1; |
| 338 | skp = smk_find_entry(rule->smk_subject); |
| 339 | rule_list = &skp->smk_rules; |
| 340 | rule_lock = &skp->smk_rules_lock; |
| 341 | } |
| 342 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 343 | rc = count; |
| 344 | /* |
| 345 | * smk_set_access returns true if there was already a rule |
| 346 | * for the subject/object pair, and false if it was new. |
| 347 | */ |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 348 | if (!smk_set_access(rule, rule_list, rule_lock)) { |
| 349 | smlp = kzalloc(sizeof(*smlp), GFP_KERNEL); |
| 350 | if (smlp != NULL) { |
| 351 | smlp->smk_rule = rule; |
| 352 | list_add_rcu(&smlp->list, &smack_rule_list); |
| 353 | } else |
| 354 | rc = -ENOMEM; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 355 | goto out; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 356 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 357 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 358 | out_free_rule: |
| 359 | kfree(rule); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 360 | out: |
| 361 | kfree(data); |
| 362 | return rc; |
| 363 | } |
| 364 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * Seq_file read operations for /smack/load |
| 368 | */ |
| 369 | |
| 370 | static void *load_seq_start(struct seq_file *s, loff_t *pos) |
| 371 | { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 372 | struct list_head *list; |
| 373 | |
| 374 | /* |
| 375 | * This is 0 the first time through. |
| 376 | */ |
| 377 | if (s->index == 0) |
| 378 | s->private = &smack_rule_list; |
| 379 | |
| 380 | if (s->private == NULL) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 381 | return NULL; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 382 | |
| 383 | list = s->private; |
| 384 | if (list_empty(list)) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 385 | return NULL; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 386 | |
| 387 | if (s->index == 0) |
| 388 | return list->next; |
| 389 | return list; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static void *load_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 393 | { |
| 394 | struct list_head *list = v; |
| 395 | |
| 396 | if (list_is_last(list, &smack_rule_list)) { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 397 | s->private = NULL; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 400 | s->private = list->next; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 401 | return list->next; |
| 402 | } |
| 403 | |
| 404 | static int load_seq_show(struct seq_file *s, void *v) |
| 405 | { |
| 406 | struct list_head *list = v; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 407 | struct smack_master_list *smlp = |
| 408 | list_entry(list, struct smack_master_list, list); |
| 409 | struct smack_rule *srp = smlp->smk_rule; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 410 | |
| 411 | seq_printf(s, "%s %s", (char *)srp->smk_subject, |
| 412 | (char *)srp->smk_object); |
| 413 | |
| 414 | seq_putc(s, ' '); |
| 415 | |
| 416 | if (srp->smk_access & MAY_READ) |
| 417 | seq_putc(s, 'r'); |
| 418 | if (srp->smk_access & MAY_WRITE) |
| 419 | seq_putc(s, 'w'); |
| 420 | if (srp->smk_access & MAY_EXEC) |
| 421 | seq_putc(s, 'x'); |
| 422 | if (srp->smk_access & MAY_APPEND) |
| 423 | seq_putc(s, 'a'); |
| 424 | if (srp->smk_access & MAY_TRANSMUTE) |
| 425 | seq_putc(s, 't'); |
| 426 | if (srp->smk_access == 0) |
| 427 | seq_putc(s, '-'); |
| 428 | |
| 429 | seq_putc(s, '\n'); |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | static void load_seq_stop(struct seq_file *s, void *v) |
| 435 | { |
| 436 | /* No-op */ |
| 437 | } |
| 438 | |
| 439 | static const struct seq_operations load_seq_ops = { |
| 440 | .start = load_seq_start, |
| 441 | .next = load_seq_next, |
| 442 | .show = load_seq_show, |
| 443 | .stop = load_seq_stop, |
| 444 | }; |
| 445 | |
| 446 | /** |
| 447 | * smk_open_load - open() for /smack/load |
| 448 | * @inode: inode structure representing file |
| 449 | * @file: "load" file pointer |
| 450 | * |
| 451 | * For reading, use load_seq_* seq_file reading operations. |
| 452 | */ |
| 453 | static int smk_open_load(struct inode *inode, struct file *file) |
| 454 | { |
| 455 | return seq_open(file, &load_seq_ops); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * smk_write_load - write() for /smack/load |
| 460 | * @file: file pointer, not actually used |
| 461 | * @buf: where to get the data from |
| 462 | * @count: bytes sent |
| 463 | * @ppos: where to start - must be 0 |
| 464 | * |
| 465 | */ |
| 466 | static ssize_t smk_write_load(struct file *file, const char __user *buf, |
| 467 | size_t count, loff_t *ppos) |
| 468 | { |
| 469 | |
| 470 | /* |
| 471 | * Must have privilege. |
| 472 | * No partial writes. |
| 473 | * Enough data must be present. |
| 474 | */ |
| 475 | if (!capable(CAP_MAC_ADMIN)) |
| 476 | return -EPERM; |
| 477 | |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 478 | /* |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 479 | return smk_write_load_list(file, buf, count, ppos, &smack_rule_list, |
| 480 | &smack_list_lock); |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 481 | */ |
| 482 | return smk_write_load_list(file, buf, count, ppos, NULL, NULL); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 485 | static const struct file_operations smk_load_ops = { |
| 486 | .open = smk_open_load, |
| 487 | .read = seq_read, |
| 488 | .llseek = seq_lseek, |
| 489 | .write = smk_write_load, |
Ahmed S. Darwish | cb622bb | 2008-03-24 12:29:49 -0700 | [diff] [blame] | 490 | .release = seq_release, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 491 | }; |
| 492 | |
| 493 | /** |
| 494 | * smk_cipso_doi - initialize the CIPSO domain |
| 495 | */ |
Casey Schaufler | 30aa4fa | 2008-04-28 02:13:43 -0700 | [diff] [blame] | 496 | static void smk_cipso_doi(void) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 497 | { |
| 498 | int rc; |
| 499 | struct cipso_v4_doi *doip; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 500 | struct netlbl_audit nai; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 501 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 502 | smk_netlabel_audit_set(&nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 503 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 504 | rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 505 | if (rc != 0) |
| 506 | printk(KERN_WARNING "%s:%d remove rc = %d\n", |
| 507 | __func__, __LINE__, rc); |
| 508 | |
| 509 | doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL); |
| 510 | if (doip == NULL) |
| 511 | panic("smack: Failed to initialize cipso DOI.\n"); |
| 512 | doip->map.std = NULL; |
| 513 | doip->doi = smk_cipso_doi_value; |
| 514 | doip->type = CIPSO_V4_MAP_PASS; |
| 515 | doip->tags[0] = CIPSO_V4_TAG_RBITMAP; |
| 516 | for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++) |
| 517 | doip->tags[rc] = CIPSO_V4_TAG_INVALID; |
| 518 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 519 | rc = netlbl_cfg_cipsov4_add(doip, &nai); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 520 | if (rc != 0) { |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 521 | printk(KERN_WARNING "%s:%d cipso add rc = %d\n", |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 522 | __func__, __LINE__, rc); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 523 | kfree(doip); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 524 | return; |
| 525 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 526 | rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 527 | if (rc != 0) { |
| 528 | printk(KERN_WARNING "%s:%d map add rc = %d\n", |
| 529 | __func__, __LINE__, rc); |
| 530 | kfree(doip); |
| 531 | return; |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 532 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 535 | /** |
| 536 | * smk_unlbl_ambient - initialize the unlabeled domain |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 537 | * @oldambient: previous domain string |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 538 | */ |
Casey Schaufler | 30aa4fa | 2008-04-28 02:13:43 -0700 | [diff] [blame] | 539 | static void smk_unlbl_ambient(char *oldambient) |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 540 | { |
| 541 | int rc; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 542 | struct netlbl_audit nai; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 543 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 544 | smk_netlabel_audit_set(&nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 545 | |
| 546 | if (oldambient != NULL) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 547 | rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 548 | if (rc != 0) |
| 549 | printk(KERN_WARNING "%s:%d remove rc = %d\n", |
| 550 | __func__, __LINE__, rc); |
| 551 | } |
| 552 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 553 | rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET, |
| 554 | NULL, NULL, &nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 555 | if (rc != 0) |
| 556 | printk(KERN_WARNING "%s:%d add rc = %d\n", |
| 557 | __func__, __LINE__, rc); |
| 558 | } |
| 559 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 560 | /* |
| 561 | * Seq_file read operations for /smack/cipso |
| 562 | */ |
| 563 | |
| 564 | static void *cipso_seq_start(struct seq_file *s, loff_t *pos) |
| 565 | { |
| 566 | if (*pos == SEQ_READ_FINISHED) |
| 567 | return NULL; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 568 | if (list_empty(&smack_known_list)) |
| 569 | return NULL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 570 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 571 | return smack_known_list.next; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 575 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 576 | struct list_head *list = v; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 577 | |
| 578 | /* |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 579 | * labels with no associated cipso value wont be printed |
| 580 | * in cipso_seq_show |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 581 | */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 582 | if (list_is_last(list, &smack_known_list)) { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 583 | *pos = SEQ_READ_FINISHED; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 586 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 587 | return list->next; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* |
| 591 | * Print cipso labels in format: |
| 592 | * label level[/cat[,cat]] |
| 593 | */ |
| 594 | static int cipso_seq_show(struct seq_file *s, void *v) |
| 595 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 596 | struct list_head *list = v; |
| 597 | struct smack_known *skp = |
| 598 | list_entry(list, struct smack_known, list); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 599 | struct smack_cipso *scp = skp->smk_cipso; |
| 600 | char *cbp; |
| 601 | char sep = '/'; |
| 602 | int cat = 1; |
| 603 | int i; |
| 604 | unsigned char m; |
| 605 | |
| 606 | if (scp == NULL) |
| 607 | return 0; |
| 608 | |
| 609 | seq_printf(s, "%s %3d", (char *)&skp->smk_known, scp->smk_level); |
| 610 | |
| 611 | cbp = scp->smk_catset; |
| 612 | for (i = 0; i < SMK_LABELLEN; i++) |
| 613 | for (m = 0x80; m != 0; m >>= 1) { |
| 614 | if (m & cbp[i]) { |
| 615 | seq_printf(s, "%c%d", sep, cat); |
| 616 | sep = ','; |
| 617 | } |
| 618 | cat++; |
| 619 | } |
| 620 | |
| 621 | seq_putc(s, '\n'); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static void cipso_seq_stop(struct seq_file *s, void *v) |
| 627 | { |
| 628 | /* No-op */ |
| 629 | } |
| 630 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 631 | static const struct seq_operations cipso_seq_ops = { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 632 | .start = cipso_seq_start, |
| 633 | .stop = cipso_seq_stop, |
| 634 | .next = cipso_seq_next, |
| 635 | .show = cipso_seq_show, |
| 636 | }; |
| 637 | |
| 638 | /** |
| 639 | * smk_open_cipso - open() for /smack/cipso |
| 640 | * @inode: inode structure representing file |
| 641 | * @file: "cipso" file pointer |
| 642 | * |
| 643 | * Connect our cipso_seq_* operations with /smack/cipso |
| 644 | * file_operations |
| 645 | */ |
| 646 | static int smk_open_cipso(struct inode *inode, struct file *file) |
| 647 | { |
| 648 | return seq_open(file, &cipso_seq_ops); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * smk_write_cipso - write() for /smack/cipso |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 653 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 654 | * @buf: where to get the data from |
| 655 | * @count: bytes sent |
| 656 | * @ppos: where to start |
| 657 | * |
| 658 | * Accepts only one cipso rule per write call. |
| 659 | * Returns number of bytes written or error code, as appropriate |
| 660 | */ |
| 661 | static ssize_t smk_write_cipso(struct file *file, const char __user *buf, |
| 662 | size_t count, loff_t *ppos) |
| 663 | { |
| 664 | struct smack_known *skp; |
| 665 | struct smack_cipso *scp = NULL; |
| 666 | char mapcatset[SMK_LABELLEN]; |
| 667 | int maplevel; |
| 668 | int cat; |
| 669 | int catlen; |
| 670 | ssize_t rc = -EINVAL; |
| 671 | char *data = NULL; |
| 672 | char *rule; |
| 673 | int ret; |
| 674 | int i; |
| 675 | |
| 676 | /* |
| 677 | * Must have privilege. |
| 678 | * No partial writes. |
| 679 | * Enough data must be present. |
| 680 | */ |
| 681 | if (!capable(CAP_MAC_ADMIN)) |
| 682 | return -EPERM; |
| 683 | if (*ppos != 0) |
| 684 | return -EINVAL; |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 685 | if (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 686 | return -EINVAL; |
| 687 | |
| 688 | data = kzalloc(count + 1, GFP_KERNEL); |
| 689 | if (data == NULL) |
| 690 | return -ENOMEM; |
| 691 | |
| 692 | if (copy_from_user(data, buf, count) != 0) { |
| 693 | rc = -EFAULT; |
| 694 | goto unlockedout; |
| 695 | } |
| 696 | |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 697 | /* labels cannot begin with a '-' */ |
| 698 | if (data[0] == '-') { |
| 699 | rc = -EINVAL; |
| 700 | goto unlockedout; |
| 701 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 702 | data[count] = '\0'; |
| 703 | rule = data; |
| 704 | /* |
| 705 | * Only allow one writer at a time. Writes should be |
| 706 | * quite rare and small in any case. |
| 707 | */ |
| 708 | mutex_lock(&smack_cipso_lock); |
| 709 | |
| 710 | skp = smk_import_entry(rule, 0); |
| 711 | if (skp == NULL) |
| 712 | goto out; |
| 713 | |
Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 714 | rule += SMK_LABELLEN; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 715 | ret = sscanf(rule, "%d", &maplevel); |
| 716 | if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL) |
| 717 | goto out; |
| 718 | |
| 719 | rule += SMK_DIGITLEN; |
| 720 | ret = sscanf(rule, "%d", &catlen); |
| 721 | if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM) |
| 722 | goto out; |
| 723 | |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 724 | if (count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 725 | goto out; |
| 726 | |
| 727 | memset(mapcatset, 0, sizeof(mapcatset)); |
| 728 | |
| 729 | for (i = 0; i < catlen; i++) { |
| 730 | rule += SMK_DIGITLEN; |
| 731 | ret = sscanf(rule, "%d", &cat); |
| 732 | if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL) |
| 733 | goto out; |
| 734 | |
| 735 | smack_catset_bit(cat, mapcatset); |
| 736 | } |
| 737 | |
| 738 | if (skp->smk_cipso == NULL) { |
| 739 | scp = kzalloc(sizeof(struct smack_cipso), GFP_KERNEL); |
| 740 | if (scp == NULL) { |
| 741 | rc = -ENOMEM; |
| 742 | goto out; |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | spin_lock_bh(&skp->smk_cipsolock); |
| 747 | |
| 748 | if (scp == NULL) |
| 749 | scp = skp->smk_cipso; |
| 750 | else |
| 751 | skp->smk_cipso = scp; |
| 752 | |
| 753 | scp->smk_level = maplevel; |
| 754 | memcpy(scp->smk_catset, mapcatset, sizeof(mapcatset)); |
| 755 | |
| 756 | spin_unlock_bh(&skp->smk_cipsolock); |
| 757 | |
| 758 | rc = count; |
| 759 | out: |
| 760 | mutex_unlock(&smack_cipso_lock); |
| 761 | unlockedout: |
| 762 | kfree(data); |
| 763 | return rc; |
| 764 | } |
| 765 | |
| 766 | static const struct file_operations smk_cipso_ops = { |
| 767 | .open = smk_open_cipso, |
| 768 | .read = seq_read, |
| 769 | .llseek = seq_lseek, |
| 770 | .write = smk_write_cipso, |
| 771 | .release = seq_release, |
| 772 | }; |
| 773 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 774 | /* |
| 775 | * Seq_file read operations for /smack/netlabel |
| 776 | */ |
| 777 | |
| 778 | static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos) |
| 779 | { |
| 780 | if (*pos == SEQ_READ_FINISHED) |
| 781 | return NULL; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 782 | if (list_empty(&smk_netlbladdr_list)) |
| 783 | return NULL; |
| 784 | return smk_netlbladdr_list.next; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 788 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 789 | struct list_head *list = v; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 790 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 791 | if (list_is_last(list, &smk_netlbladdr_list)) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 792 | *pos = SEQ_READ_FINISHED; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 793 | return NULL; |
| 794 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 795 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 796 | return list->next; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 797 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 798 | #define BEBITS (sizeof(__be32) * 8) |
| 799 | |
| 800 | /* |
| 801 | * Print host/label pairs |
| 802 | */ |
| 803 | static int netlbladdr_seq_show(struct seq_file *s, void *v) |
| 804 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 805 | struct list_head *list = v; |
| 806 | struct smk_netlbladdr *skp = |
| 807 | list_entry(list, struct smk_netlbladdr, list); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 808 | unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 809 | int maskn; |
| 810 | u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 811 | |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 812 | for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 813 | |
| 814 | seq_printf(s, "%u.%u.%u.%u/%d %s\n", |
| 815 | hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label); |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
| 820 | static void netlbladdr_seq_stop(struct seq_file *s, void *v) |
| 821 | { |
| 822 | /* No-op */ |
| 823 | } |
| 824 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 825 | static const struct seq_operations netlbladdr_seq_ops = { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 826 | .start = netlbladdr_seq_start, |
| 827 | .stop = netlbladdr_seq_stop, |
| 828 | .next = netlbladdr_seq_next, |
| 829 | .show = netlbladdr_seq_show, |
| 830 | }; |
| 831 | |
| 832 | /** |
| 833 | * smk_open_netlbladdr - open() for /smack/netlabel |
| 834 | * @inode: inode structure representing file |
| 835 | * @file: "netlabel" file pointer |
| 836 | * |
| 837 | * Connect our netlbladdr_seq_* operations with /smack/netlabel |
| 838 | * file_operations |
| 839 | */ |
| 840 | static int smk_open_netlbladdr(struct inode *inode, struct file *file) |
| 841 | { |
| 842 | return seq_open(file, &netlbladdr_seq_ops); |
| 843 | } |
| 844 | |
| 845 | /** |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 846 | * smk_netlbladdr_insert |
| 847 | * @new : netlabel to insert |
| 848 | * |
| 849 | * This helper insert netlabel in the smack_netlbladdrs list |
| 850 | * sorted by netmask length (longest to smallest) |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 851 | * locked by &smk_netlbladdr_lock in smk_write_netlbladdr |
| 852 | * |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 853 | */ |
| 854 | static void smk_netlbladdr_insert(struct smk_netlbladdr *new) |
| 855 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 856 | struct smk_netlbladdr *m, *m_next; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 857 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 858 | if (list_empty(&smk_netlbladdr_list)) { |
| 859 | list_add_rcu(&new->list, &smk_netlbladdr_list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | |
Jiri Pirko | 05725f7 | 2009-04-14 20:17:16 +0200 | [diff] [blame] | 863 | m = list_entry_rcu(smk_netlbladdr_list.next, |
| 864 | struct smk_netlbladdr, list); |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 865 | |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 866 | /* the comparison '>' is a bit hacky, but works */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 867 | if (new->smk_mask.s_addr > m->smk_mask.s_addr) { |
| 868 | list_add_rcu(&new->list, &smk_netlbladdr_list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 869 | return; |
| 870 | } |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 871 | |
| 872 | list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) { |
| 873 | if (list_is_last(&m->list, &smk_netlbladdr_list)) { |
| 874 | list_add_rcu(&new->list, &m->list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 875 | return; |
| 876 | } |
Jiri Pirko | 05725f7 | 2009-04-14 20:17:16 +0200 | [diff] [blame] | 877 | m_next = list_entry_rcu(m->list.next, |
| 878 | struct smk_netlbladdr, list); |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 879 | if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) { |
| 880 | list_add_rcu(&new->list, &m->list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 881 | return; |
| 882 | } |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | |
| 887 | /** |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 888 | * smk_write_netlbladdr - write() for /smack/netlabel |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 889 | * @file: file pointer, not actually used |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 890 | * @buf: where to get the data from |
| 891 | * @count: bytes sent |
| 892 | * @ppos: where to start |
| 893 | * |
| 894 | * Accepts only one netlbladdr per write call. |
| 895 | * Returns number of bytes written or error code, as appropriate |
| 896 | */ |
| 897 | static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf, |
| 898 | size_t count, loff_t *ppos) |
| 899 | { |
| 900 | struct smk_netlbladdr *skp; |
| 901 | struct sockaddr_in newname; |
| 902 | char smack[SMK_LABELLEN]; |
| 903 | char *sp; |
Roel Kluin | 6470c07 | 2009-05-21 18:42:54 +0200 | [diff] [blame] | 904 | char data[SMK_NETLBLADDRMAX + 1]; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 905 | char *host = (char *)&newname.sin_addr.s_addr; |
| 906 | int rc; |
| 907 | struct netlbl_audit audit_info; |
| 908 | struct in_addr mask; |
| 909 | unsigned int m; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 910 | int found; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 911 | u32 mask_bits = (1<<31); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 912 | __be32 nsa; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 913 | u32 temp_mask; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * Must have privilege. |
| 917 | * No partial writes. |
| 918 | * Enough data must be present. |
| 919 | * "<addr/mask, as a.b.c.d/e><space><label>" |
| 920 | * "<addr, as a.b.c.d><space><label>" |
| 921 | */ |
| 922 | if (!capable(CAP_MAC_ADMIN)) |
| 923 | return -EPERM; |
| 924 | if (*ppos != 0) |
| 925 | return -EINVAL; |
| 926 | if (count < SMK_NETLBLADDRMIN || count > SMK_NETLBLADDRMAX) |
| 927 | return -EINVAL; |
| 928 | if (copy_from_user(data, buf, count) != 0) |
| 929 | return -EFAULT; |
| 930 | |
| 931 | data[count] = '\0'; |
| 932 | |
| 933 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s", |
| 934 | &host[0], &host[1], &host[2], &host[3], &m, smack); |
| 935 | if (rc != 6) { |
| 936 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s", |
| 937 | &host[0], &host[1], &host[2], &host[3], smack); |
| 938 | if (rc != 5) |
| 939 | return -EINVAL; |
| 940 | m = BEBITS; |
| 941 | } |
| 942 | if (m > BEBITS) |
| 943 | return -EINVAL; |
| 944 | |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 945 | /* if smack begins with '-', its an option, don't import it */ |
| 946 | if (smack[0] != '-') { |
| 947 | sp = smk_import(smack, 0); |
| 948 | if (sp == NULL) |
| 949 | return -EINVAL; |
| 950 | } else { |
| 951 | /* check known options */ |
| 952 | if (strcmp(smack, smack_cipso_option) == 0) |
| 953 | sp = (char *)smack_cipso_option; |
| 954 | else |
| 955 | return -EINVAL; |
| 956 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 957 | |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 958 | for (temp_mask = 0; m > 0; m--) { |
| 959 | temp_mask |= mask_bits; |
| 960 | mask_bits >>= 1; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 961 | } |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 962 | mask.s_addr = cpu_to_be32(temp_mask); |
| 963 | |
| 964 | newname.sin_addr.s_addr &= mask.s_addr; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 965 | /* |
| 966 | * Only allow one writer at a time. Writes should be |
| 967 | * quite rare and small in any case. |
| 968 | */ |
| 969 | mutex_lock(&smk_netlbladdr_lock); |
| 970 | |
| 971 | nsa = newname.sin_addr.s_addr; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 972 | /* try to find if the prefix is already in the list */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 973 | found = 0; |
| 974 | list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 975 | if (skp->smk_host.sin_addr.s_addr == nsa && |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 976 | skp->smk_mask.s_addr == mask.s_addr) { |
| 977 | found = 1; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 978 | break; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 979 | } |
| 980 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 981 | smk_netlabel_audit_set(&audit_info); |
| 982 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 983 | if (found == 0) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 984 | skp = kzalloc(sizeof(*skp), GFP_KERNEL); |
| 985 | if (skp == NULL) |
| 986 | rc = -ENOMEM; |
| 987 | else { |
| 988 | rc = 0; |
| 989 | skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr; |
| 990 | skp->smk_mask.s_addr = mask.s_addr; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 991 | skp->smk_label = sp; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 992 | smk_netlbladdr_insert(skp); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 993 | } |
| 994 | } else { |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 995 | /* we delete the unlabeled entry, only if the previous label |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 996 | * wasn't the special CIPSO option */ |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 997 | if (skp->smk_label != smack_cipso_option) |
| 998 | rc = netlbl_cfg_unlbl_static_del(&init_net, NULL, |
| 999 | &skp->smk_host.sin_addr, &skp->smk_mask, |
| 1000 | PF_INET, &audit_info); |
| 1001 | else |
| 1002 | rc = 0; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1003 | skp->smk_label = sp; |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Now tell netlabel about the single label nature of |
| 1008 | * this host so that incoming packets get labeled. |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1009 | * but only if we didn't get the special CIPSO option |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1010 | */ |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1011 | if (rc == 0 && sp != smack_cipso_option) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1012 | rc = netlbl_cfg_unlbl_static_add(&init_net, NULL, |
| 1013 | &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET, |
| 1014 | smack_to_secid(skp->smk_label), &audit_info); |
| 1015 | |
| 1016 | if (rc == 0) |
| 1017 | rc = count; |
| 1018 | |
| 1019 | mutex_unlock(&smk_netlbladdr_lock); |
| 1020 | |
| 1021 | return rc; |
| 1022 | } |
| 1023 | |
| 1024 | static const struct file_operations smk_netlbladdr_ops = { |
| 1025 | .open = smk_open_netlbladdr, |
| 1026 | .read = seq_read, |
| 1027 | .llseek = seq_lseek, |
| 1028 | .write = smk_write_netlbladdr, |
| 1029 | .release = seq_release, |
| 1030 | }; |
| 1031 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1032 | /** |
| 1033 | * smk_read_doi - read() for /smack/doi |
| 1034 | * @filp: file pointer, not actually used |
| 1035 | * @buf: where to put the result |
| 1036 | * @count: maximum to send along |
| 1037 | * @ppos: where to start |
| 1038 | * |
| 1039 | * Returns number of bytes read or error code, as appropriate |
| 1040 | */ |
| 1041 | static ssize_t smk_read_doi(struct file *filp, char __user *buf, |
| 1042 | size_t count, loff_t *ppos) |
| 1043 | { |
| 1044 | char temp[80]; |
| 1045 | ssize_t rc; |
| 1046 | |
| 1047 | if (*ppos != 0) |
| 1048 | return 0; |
| 1049 | |
| 1050 | sprintf(temp, "%d", smk_cipso_doi_value); |
| 1051 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1052 | |
| 1053 | return rc; |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * smk_write_doi - write() for /smack/doi |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1058 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1059 | * @buf: where to get the data from |
| 1060 | * @count: bytes sent |
| 1061 | * @ppos: where to start |
| 1062 | * |
| 1063 | * Returns number of bytes written or error code, as appropriate |
| 1064 | */ |
| 1065 | static ssize_t smk_write_doi(struct file *file, const char __user *buf, |
| 1066 | size_t count, loff_t *ppos) |
| 1067 | { |
| 1068 | char temp[80]; |
| 1069 | int i; |
| 1070 | |
| 1071 | if (!capable(CAP_MAC_ADMIN)) |
| 1072 | return -EPERM; |
| 1073 | |
| 1074 | if (count >= sizeof(temp) || count == 0) |
| 1075 | return -EINVAL; |
| 1076 | |
| 1077 | if (copy_from_user(temp, buf, count) != 0) |
| 1078 | return -EFAULT; |
| 1079 | |
| 1080 | temp[count] = '\0'; |
| 1081 | |
| 1082 | if (sscanf(temp, "%d", &i) != 1) |
| 1083 | return -EINVAL; |
| 1084 | |
| 1085 | smk_cipso_doi_value = i; |
| 1086 | |
| 1087 | smk_cipso_doi(); |
| 1088 | |
| 1089 | return count; |
| 1090 | } |
| 1091 | |
| 1092 | static const struct file_operations smk_doi_ops = { |
| 1093 | .read = smk_read_doi, |
| 1094 | .write = smk_write_doi, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1095 | .llseek = default_llseek, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1096 | }; |
| 1097 | |
| 1098 | /** |
| 1099 | * smk_read_direct - read() for /smack/direct |
| 1100 | * @filp: file pointer, not actually used |
| 1101 | * @buf: where to put the result |
| 1102 | * @count: maximum to send along |
| 1103 | * @ppos: where to start |
| 1104 | * |
| 1105 | * Returns number of bytes read or error code, as appropriate |
| 1106 | */ |
| 1107 | static ssize_t smk_read_direct(struct file *filp, char __user *buf, |
| 1108 | size_t count, loff_t *ppos) |
| 1109 | { |
| 1110 | char temp[80]; |
| 1111 | ssize_t rc; |
| 1112 | |
| 1113 | if (*ppos != 0) |
| 1114 | return 0; |
| 1115 | |
| 1116 | sprintf(temp, "%d", smack_cipso_direct); |
| 1117 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1118 | |
| 1119 | return rc; |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * smk_write_direct - write() for /smack/direct |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1124 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1125 | * @buf: where to get the data from |
| 1126 | * @count: bytes sent |
| 1127 | * @ppos: where to start |
| 1128 | * |
| 1129 | * Returns number of bytes written or error code, as appropriate |
| 1130 | */ |
| 1131 | static ssize_t smk_write_direct(struct file *file, const char __user *buf, |
| 1132 | size_t count, loff_t *ppos) |
| 1133 | { |
| 1134 | char temp[80]; |
| 1135 | int i; |
| 1136 | |
| 1137 | if (!capable(CAP_MAC_ADMIN)) |
| 1138 | return -EPERM; |
| 1139 | |
| 1140 | if (count >= sizeof(temp) || count == 0) |
| 1141 | return -EINVAL; |
| 1142 | |
| 1143 | if (copy_from_user(temp, buf, count) != 0) |
| 1144 | return -EFAULT; |
| 1145 | |
| 1146 | temp[count] = '\0'; |
| 1147 | |
| 1148 | if (sscanf(temp, "%d", &i) != 1) |
| 1149 | return -EINVAL; |
| 1150 | |
| 1151 | smack_cipso_direct = i; |
| 1152 | |
| 1153 | return count; |
| 1154 | } |
| 1155 | |
| 1156 | static const struct file_operations smk_direct_ops = { |
| 1157 | .read = smk_read_direct, |
| 1158 | .write = smk_write_direct, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1159 | .llseek = default_llseek, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1160 | }; |
| 1161 | |
| 1162 | /** |
| 1163 | * smk_read_ambient - read() for /smack/ambient |
| 1164 | * @filp: file pointer, not actually used |
| 1165 | * @buf: where to put the result |
| 1166 | * @cn: maximum to send along |
| 1167 | * @ppos: where to start |
| 1168 | * |
| 1169 | * Returns number of bytes read or error code, as appropriate |
| 1170 | */ |
| 1171 | static ssize_t smk_read_ambient(struct file *filp, char __user *buf, |
| 1172 | size_t cn, loff_t *ppos) |
| 1173 | { |
| 1174 | ssize_t rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1175 | int asize; |
| 1176 | |
| 1177 | if (*ppos != 0) |
| 1178 | return 0; |
| 1179 | /* |
| 1180 | * Being careful to avoid a problem in the case where |
| 1181 | * smack_net_ambient gets changed in midstream. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1182 | */ |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1183 | mutex_lock(&smack_ambient_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1184 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1185 | asize = strlen(smack_net_ambient) + 1; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1186 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1187 | if (cn >= asize) |
| 1188 | rc = simple_read_from_buffer(buf, cn, ppos, |
| 1189 | smack_net_ambient, asize); |
| 1190 | else |
| 1191 | rc = -EINVAL; |
| 1192 | |
| 1193 | mutex_unlock(&smack_ambient_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1194 | |
| 1195 | return rc; |
| 1196 | } |
| 1197 | |
| 1198 | /** |
| 1199 | * smk_write_ambient - write() for /smack/ambient |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1200 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1201 | * @buf: where to get the data from |
| 1202 | * @count: bytes sent |
| 1203 | * @ppos: where to start |
| 1204 | * |
| 1205 | * Returns number of bytes written or error code, as appropriate |
| 1206 | */ |
| 1207 | static ssize_t smk_write_ambient(struct file *file, const char __user *buf, |
| 1208 | size_t count, loff_t *ppos) |
| 1209 | { |
| 1210 | char in[SMK_LABELLEN]; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1211 | char *oldambient; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1212 | char *smack; |
| 1213 | |
| 1214 | if (!capable(CAP_MAC_ADMIN)) |
| 1215 | return -EPERM; |
| 1216 | |
| 1217 | if (count >= SMK_LABELLEN) |
| 1218 | return -EINVAL; |
| 1219 | |
| 1220 | if (copy_from_user(in, buf, count) != 0) |
| 1221 | return -EFAULT; |
| 1222 | |
| 1223 | smack = smk_import(in, count); |
| 1224 | if (smack == NULL) |
| 1225 | return -EINVAL; |
| 1226 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1227 | mutex_lock(&smack_ambient_lock); |
| 1228 | |
| 1229 | oldambient = smack_net_ambient; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1230 | smack_net_ambient = smack; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1231 | smk_unlbl_ambient(oldambient); |
| 1232 | |
| 1233 | mutex_unlock(&smack_ambient_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1234 | |
| 1235 | return count; |
| 1236 | } |
| 1237 | |
| 1238 | static const struct file_operations smk_ambient_ops = { |
| 1239 | .read = smk_read_ambient, |
| 1240 | .write = smk_write_ambient, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1241 | .llseek = default_llseek, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1242 | }; |
| 1243 | |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1244 | /** |
| 1245 | * smk_read_onlycap - read() for /smack/onlycap |
| 1246 | * @filp: file pointer, not actually used |
| 1247 | * @buf: where to put the result |
| 1248 | * @cn: maximum to send along |
| 1249 | * @ppos: where to start |
| 1250 | * |
| 1251 | * Returns number of bytes read or error code, as appropriate |
| 1252 | */ |
| 1253 | static ssize_t smk_read_onlycap(struct file *filp, char __user *buf, |
| 1254 | size_t cn, loff_t *ppos) |
| 1255 | { |
| 1256 | char *smack = ""; |
| 1257 | ssize_t rc = -EINVAL; |
| 1258 | int asize; |
| 1259 | |
| 1260 | if (*ppos != 0) |
| 1261 | return 0; |
| 1262 | |
| 1263 | if (smack_onlycap != NULL) |
| 1264 | smack = smack_onlycap; |
| 1265 | |
| 1266 | asize = strlen(smack) + 1; |
| 1267 | |
| 1268 | if (cn >= asize) |
| 1269 | rc = simple_read_from_buffer(buf, cn, ppos, smack, asize); |
| 1270 | |
| 1271 | return rc; |
| 1272 | } |
| 1273 | |
| 1274 | /** |
| 1275 | * smk_write_onlycap - write() for /smack/onlycap |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1276 | * @file: file pointer, not actually used |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1277 | * @buf: where to get the data from |
| 1278 | * @count: bytes sent |
| 1279 | * @ppos: where to start |
| 1280 | * |
| 1281 | * Returns number of bytes written or error code, as appropriate |
| 1282 | */ |
| 1283 | static ssize_t smk_write_onlycap(struct file *file, const char __user *buf, |
| 1284 | size_t count, loff_t *ppos) |
| 1285 | { |
| 1286 | char in[SMK_LABELLEN]; |
Casey Schaufler | 676dac4 | 2010-12-02 06:43:39 -0800 | [diff] [blame] | 1287 | char *sp = smk_of_task(current->cred->security); |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1288 | |
| 1289 | if (!capable(CAP_MAC_ADMIN)) |
| 1290 | return -EPERM; |
| 1291 | |
| 1292 | /* |
| 1293 | * This can be done using smk_access() but is done |
| 1294 | * explicitly for clarity. The smk_access() implementation |
| 1295 | * would use smk_access(smack_onlycap, MAY_WRITE) |
| 1296 | */ |
| 1297 | if (smack_onlycap != NULL && smack_onlycap != sp) |
| 1298 | return -EPERM; |
| 1299 | |
| 1300 | if (count >= SMK_LABELLEN) |
| 1301 | return -EINVAL; |
| 1302 | |
| 1303 | if (copy_from_user(in, buf, count) != 0) |
| 1304 | return -EFAULT; |
| 1305 | |
| 1306 | /* |
| 1307 | * Should the null string be passed in unset the onlycap value. |
| 1308 | * This seems like something to be careful with as usually |
| 1309 | * smk_import only expects to return NULL for errors. It |
| 1310 | * is usually the case that a nullstring or "\n" would be |
| 1311 | * bad to pass to smk_import but in fact this is useful here. |
| 1312 | */ |
| 1313 | smack_onlycap = smk_import(in, count); |
| 1314 | |
| 1315 | return count; |
| 1316 | } |
| 1317 | |
| 1318 | static const struct file_operations smk_onlycap_ops = { |
| 1319 | .read = smk_read_onlycap, |
| 1320 | .write = smk_write_onlycap, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1321 | .llseek = default_llseek, |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1322 | }; |
| 1323 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1324 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1325 | * smk_read_logging - read() for /smack/logging |
| 1326 | * @filp: file pointer, not actually used |
| 1327 | * @buf: where to put the result |
| 1328 | * @cn: maximum to send along |
| 1329 | * @ppos: where to start |
| 1330 | * |
| 1331 | * Returns number of bytes read or error code, as appropriate |
| 1332 | */ |
| 1333 | static ssize_t smk_read_logging(struct file *filp, char __user *buf, |
| 1334 | size_t count, loff_t *ppos) |
| 1335 | { |
| 1336 | char temp[32]; |
| 1337 | ssize_t rc; |
| 1338 | |
| 1339 | if (*ppos != 0) |
| 1340 | return 0; |
| 1341 | |
| 1342 | sprintf(temp, "%d\n", log_policy); |
| 1343 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1344 | return rc; |
| 1345 | } |
| 1346 | |
| 1347 | /** |
| 1348 | * smk_write_logging - write() for /smack/logging |
| 1349 | * @file: file pointer, not actually used |
| 1350 | * @buf: where to get the data from |
| 1351 | * @count: bytes sent |
| 1352 | * @ppos: where to start |
| 1353 | * |
| 1354 | * Returns number of bytes written or error code, as appropriate |
| 1355 | */ |
| 1356 | static ssize_t smk_write_logging(struct file *file, const char __user *buf, |
| 1357 | size_t count, loff_t *ppos) |
| 1358 | { |
| 1359 | char temp[32]; |
| 1360 | int i; |
| 1361 | |
| 1362 | if (!capable(CAP_MAC_ADMIN)) |
| 1363 | return -EPERM; |
| 1364 | |
| 1365 | if (count >= sizeof(temp) || count == 0) |
| 1366 | return -EINVAL; |
| 1367 | |
| 1368 | if (copy_from_user(temp, buf, count) != 0) |
| 1369 | return -EFAULT; |
| 1370 | |
| 1371 | temp[count] = '\0'; |
| 1372 | |
| 1373 | if (sscanf(temp, "%d", &i) != 1) |
| 1374 | return -EINVAL; |
| 1375 | if (i < 0 || i > 3) |
| 1376 | return -EINVAL; |
| 1377 | log_policy = i; |
| 1378 | return count; |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | |
| 1383 | static const struct file_operations smk_logging_ops = { |
| 1384 | .read = smk_read_logging, |
| 1385 | .write = smk_write_logging, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1386 | .llseek = default_llseek, |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1387 | }; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1388 | |
| 1389 | /* |
| 1390 | * Seq_file read operations for /smack/load-self |
| 1391 | */ |
| 1392 | |
| 1393 | static void *load_self_seq_start(struct seq_file *s, loff_t *pos) |
| 1394 | { |
| 1395 | struct task_smack *tsp = current_security(); |
| 1396 | |
| 1397 | if (*pos == SEQ_READ_FINISHED) |
| 1398 | return NULL; |
| 1399 | if (list_empty(&tsp->smk_rules)) |
| 1400 | return NULL; |
| 1401 | return tsp->smk_rules.next; |
| 1402 | } |
| 1403 | |
| 1404 | static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1405 | { |
| 1406 | struct task_smack *tsp = current_security(); |
| 1407 | struct list_head *list = v; |
| 1408 | |
| 1409 | if (list_is_last(list, &tsp->smk_rules)) { |
| 1410 | *pos = SEQ_READ_FINISHED; |
| 1411 | return NULL; |
| 1412 | } |
| 1413 | return list->next; |
| 1414 | } |
| 1415 | |
| 1416 | static int load_self_seq_show(struct seq_file *s, void *v) |
| 1417 | { |
| 1418 | struct list_head *list = v; |
| 1419 | struct smack_rule *srp = |
| 1420 | list_entry(list, struct smack_rule, list); |
| 1421 | |
| 1422 | seq_printf(s, "%s %s", (char *)srp->smk_subject, |
| 1423 | (char *)srp->smk_object); |
| 1424 | |
| 1425 | seq_putc(s, ' '); |
| 1426 | |
| 1427 | if (srp->smk_access & MAY_READ) |
| 1428 | seq_putc(s, 'r'); |
| 1429 | if (srp->smk_access & MAY_WRITE) |
| 1430 | seq_putc(s, 'w'); |
| 1431 | if (srp->smk_access & MAY_EXEC) |
| 1432 | seq_putc(s, 'x'); |
| 1433 | if (srp->smk_access & MAY_APPEND) |
| 1434 | seq_putc(s, 'a'); |
| 1435 | if (srp->smk_access & MAY_TRANSMUTE) |
| 1436 | seq_putc(s, 't'); |
| 1437 | if (srp->smk_access == 0) |
| 1438 | seq_putc(s, '-'); |
| 1439 | |
| 1440 | seq_putc(s, '\n'); |
| 1441 | |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | static void load_self_seq_stop(struct seq_file *s, void *v) |
| 1446 | { |
| 1447 | /* No-op */ |
| 1448 | } |
| 1449 | |
| 1450 | static const struct seq_operations load_self_seq_ops = { |
| 1451 | .start = load_self_seq_start, |
| 1452 | .next = load_self_seq_next, |
| 1453 | .show = load_self_seq_show, |
| 1454 | .stop = load_self_seq_stop, |
| 1455 | }; |
| 1456 | |
| 1457 | |
| 1458 | /** |
| 1459 | * smk_open_load_self - open() for /smack/load-self |
| 1460 | * @inode: inode structure representing file |
| 1461 | * @file: "load" file pointer |
| 1462 | * |
| 1463 | * For reading, use load_seq_* seq_file reading operations. |
| 1464 | */ |
| 1465 | static int smk_open_load_self(struct inode *inode, struct file *file) |
| 1466 | { |
| 1467 | return seq_open(file, &load_self_seq_ops); |
| 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * smk_write_load_self - write() for /smack/load-self |
| 1472 | * @file: file pointer, not actually used |
| 1473 | * @buf: where to get the data from |
| 1474 | * @count: bytes sent |
| 1475 | * @ppos: where to start - must be 0 |
| 1476 | * |
| 1477 | */ |
| 1478 | static ssize_t smk_write_load_self(struct file *file, const char __user *buf, |
| 1479 | size_t count, loff_t *ppos) |
| 1480 | { |
| 1481 | struct task_smack *tsp = current_security(); |
| 1482 | |
| 1483 | return smk_write_load_list(file, buf, count, ppos, &tsp->smk_rules, |
| 1484 | &tsp->smk_rules_lock); |
| 1485 | } |
| 1486 | |
| 1487 | static const struct file_operations smk_load_self_ops = { |
| 1488 | .open = smk_open_load_self, |
| 1489 | .read = seq_read, |
| 1490 | .llseek = seq_lseek, |
| 1491 | .write = smk_write_load_self, |
| 1492 | .release = seq_release, |
| 1493 | }; |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 1494 | |
| 1495 | /** |
| 1496 | * smk_write_access - handle access check transaction |
| 1497 | * @file: file pointer |
| 1498 | * @buf: data from user space |
| 1499 | * @count: bytes sent |
| 1500 | * @ppos: where to start - must be 0 |
| 1501 | */ |
| 1502 | static ssize_t smk_write_access(struct file *file, const char __user *buf, |
| 1503 | size_t count, loff_t *ppos) |
| 1504 | { |
| 1505 | struct smack_rule rule; |
| 1506 | char *data; |
| 1507 | |
| 1508 | if (!capable(CAP_MAC_ADMIN)) |
| 1509 | return -EPERM; |
| 1510 | |
| 1511 | data = simple_transaction_get(file, buf, count); |
| 1512 | if (IS_ERR(data)) |
| 1513 | return PTR_ERR(data); |
| 1514 | |
| 1515 | if (count < SMK_LOADLEN || smk_parse_rule(data, &rule)) |
| 1516 | return -EINVAL; |
| 1517 | |
| 1518 | data[0] = smk_access(rule.smk_subject, rule.smk_object, |
| 1519 | rule.smk_access, NULL) == 0; |
| 1520 | |
| 1521 | simple_transaction_set(file, 1); |
| 1522 | return SMK_LOADLEN; |
| 1523 | } |
| 1524 | |
| 1525 | static const struct file_operations smk_access_ops = { |
| 1526 | .write = smk_write_access, |
| 1527 | .read = simple_transaction_read, |
| 1528 | .release = simple_transaction_release, |
| 1529 | .llseek = generic_file_llseek, |
| 1530 | }; |
| 1531 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1532 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1533 | * smk_fill_super - fill the /smackfs superblock |
| 1534 | * @sb: the empty superblock |
| 1535 | * @data: unused |
| 1536 | * @silent: unused |
| 1537 | * |
| 1538 | * Fill in the well known entries for /smack |
| 1539 | * |
| 1540 | * Returns 0 on success, an error code on failure |
| 1541 | */ |
| 1542 | static int smk_fill_super(struct super_block *sb, void *data, int silent) |
| 1543 | { |
| 1544 | int rc; |
| 1545 | struct inode *root_inode; |
| 1546 | |
| 1547 | static struct tree_descr smack_files[] = { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1548 | [SMK_LOAD] = { |
| 1549 | "load", &smk_load_ops, S_IRUGO|S_IWUSR}, |
| 1550 | [SMK_CIPSO] = { |
| 1551 | "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR}, |
| 1552 | [SMK_DOI] = { |
| 1553 | "doi", &smk_doi_ops, S_IRUGO|S_IWUSR}, |
| 1554 | [SMK_DIRECT] = { |
| 1555 | "direct", &smk_direct_ops, S_IRUGO|S_IWUSR}, |
| 1556 | [SMK_AMBIENT] = { |
| 1557 | "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR}, |
| 1558 | [SMK_NETLBLADDR] = { |
| 1559 | "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR}, |
| 1560 | [SMK_ONLYCAP] = { |
| 1561 | "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR}, |
| 1562 | [SMK_LOGGING] = { |
| 1563 | "logging", &smk_logging_ops, S_IRUGO|S_IWUSR}, |
| 1564 | [SMK_LOAD_SELF] = { |
| 1565 | "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO}, |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 1566 | [SMK_ACCESSES] = { |
| 1567 | "access", &smk_access_ops, S_IRUGO|S_IWUSR}, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1568 | /* last one */ |
| 1569 | {""} |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1570 | }; |
| 1571 | |
| 1572 | rc = simple_fill_super(sb, SMACK_MAGIC, smack_files); |
| 1573 | if (rc != 0) { |
| 1574 | printk(KERN_ERR "%s failed %d while creating inodes\n", |
| 1575 | __func__, rc); |
| 1576 | return rc; |
| 1577 | } |
| 1578 | |
| 1579 | root_inode = sb->s_root->d_inode; |
| 1580 | root_inode->i_security = new_inode_smack(smack_known_floor.smk_known); |
| 1581 | |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | /** |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1586 | * smk_mount - get the smackfs superblock |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1587 | * @fs_type: passed along without comment |
| 1588 | * @flags: passed along without comment |
| 1589 | * @dev_name: passed along without comment |
| 1590 | * @data: passed along without comment |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1591 | * |
| 1592 | * Just passes everything along. |
| 1593 | * |
| 1594 | * Returns what the lower level code does. |
| 1595 | */ |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1596 | static struct dentry *smk_mount(struct file_system_type *fs_type, |
| 1597 | int flags, const char *dev_name, void *data) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1598 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1599 | return mount_single(fs_type, flags, data, smk_fill_super); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | static struct file_system_type smk_fs_type = { |
| 1603 | .name = "smackfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 1604 | .mount = smk_mount, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1605 | .kill_sb = kill_litter_super, |
| 1606 | }; |
| 1607 | |
| 1608 | static struct vfsmount *smackfs_mount; |
| 1609 | |
| 1610 | /** |
| 1611 | * init_smk_fs - get the smackfs superblock |
| 1612 | * |
| 1613 | * register the smackfs |
| 1614 | * |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 1615 | * Do not register smackfs if Smack wasn't enabled |
| 1616 | * on boot. We can not put this method normally under the |
| 1617 | * smack_init() code path since the security subsystem get |
| 1618 | * initialized before the vfs caches. |
| 1619 | * |
| 1620 | * Returns true if we were not chosen on boot or if |
| 1621 | * we were chosen and filesystem registration succeeded. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1622 | */ |
| 1623 | static int __init init_smk_fs(void) |
| 1624 | { |
| 1625 | int err; |
| 1626 | |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 1627 | if (!security_module_enable(&smack_ops)) |
| 1628 | return 0; |
| 1629 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1630 | err = register_filesystem(&smk_fs_type); |
| 1631 | if (!err) { |
| 1632 | smackfs_mount = kern_mount(&smk_fs_type); |
| 1633 | if (IS_ERR(smackfs_mount)) { |
| 1634 | printk(KERN_ERR "smackfs: could not mount!\n"); |
| 1635 | err = PTR_ERR(smackfs_mount); |
| 1636 | smackfs_mount = NULL; |
| 1637 | } |
| 1638 | } |
| 1639 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1640 | smk_cipso_doi(); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1641 | smk_unlbl_ambient(NULL); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1642 | |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame^] | 1643 | mutex_init(&smack_known_floor.smk_rules_lock); |
| 1644 | mutex_init(&smack_known_hat.smk_rules_lock); |
| 1645 | mutex_init(&smack_known_huh.smk_rules_lock); |
| 1646 | mutex_init(&smack_known_invalid.smk_rules_lock); |
| 1647 | mutex_init(&smack_known_star.smk_rules_lock); |
| 1648 | mutex_init(&smack_known_web.smk_rules_lock); |
| 1649 | |
| 1650 | INIT_LIST_HEAD(&smack_known_floor.smk_rules); |
| 1651 | INIT_LIST_HEAD(&smack_known_hat.smk_rules); |
| 1652 | INIT_LIST_HEAD(&smack_known_huh.smk_rules); |
| 1653 | INIT_LIST_HEAD(&smack_known_invalid.smk_rules); |
| 1654 | INIT_LIST_HEAD(&smack_known_star.smk_rules); |
| 1655 | INIT_LIST_HEAD(&smack_known_web.smk_rules); |
| 1656 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1657 | return err; |
| 1658 | } |
| 1659 | |
| 1660 | __initcall(init_smk_fs); |