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