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/cipso_ipv4.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/ctype.h> |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 28 | #include <linux/audit.h> |
Casey Schaufler | 958d2c2 | 2013-04-02 11:41:18 -0700 | [diff] [blame] | 29 | #include <linux/magic.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 | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 48 | SMK_MAPPED = 13, /* CIPSO level indicating mapped label */ |
| 49 | SMK_LOAD2 = 14, /* load policy with long labels */ |
| 50 | SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */ |
| 51 | SMK_ACCESS2 = 16, /* make an access check with long labels */ |
| 52 | SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */ |
Rafal Krypa | 449543b | 2012-07-11 17:49:30 +0200 | [diff] [blame] | 53 | SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */ |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 54 | SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * List locks |
| 59 | */ |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 60 | static DEFINE_MUTEX(smack_cipso_lock); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 61 | static DEFINE_MUTEX(smack_ambient_lock); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 62 | static DEFINE_MUTEX(smk_netlbladdr_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * This is the "ambient" label for network traffic. |
| 66 | * If it isn't somehow marked, use this. |
| 67 | * It can be reset via smackfs/ambient |
| 68 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 69 | struct smack_known *smack_net_ambient; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 70 | |
| 71 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 72 | * This is the level in a CIPSO header that indicates a |
| 73 | * smack label is contained directly in the category set. |
| 74 | * It can be reset via smackfs/direct |
| 75 | */ |
| 76 | int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT; |
| 77 | |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 78 | /* |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 79 | * This is the level in a CIPSO header that indicates a |
| 80 | * secid is contained directly in the category set. |
| 81 | * It can be reset via smackfs/mapped |
| 82 | */ |
| 83 | int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT; |
| 84 | |
| 85 | /* |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 86 | * Unless a process is running with this label even |
| 87 | * having CAP_MAC_OVERRIDE isn't enough to grant |
| 88 | * privilege to violate MAC policy. If no label is |
| 89 | * designated (the NULL case) capabilities apply to |
| 90 | * everyone. It is expected that the hat (^) label |
| 91 | * will be used if any label is used. |
| 92 | */ |
| 93 | char *smack_onlycap; |
| 94 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 95 | /* |
| 96 | * Certain IP addresses may be designated as single label hosts. |
| 97 | * Packets are sent there unlabeled, but only from tasks that |
| 98 | * can write to the specified label. |
| 99 | */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 100 | |
| 101 | LIST_HEAD(smk_netlbladdr_list); |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Rule lists are maintained for each label. |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 105 | * This master list is just for reading /smack/load and /smack/load2. |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 106 | */ |
| 107 | struct smack_master_list { |
| 108 | struct list_head list; |
| 109 | struct smack_rule *smk_rule; |
| 110 | }; |
| 111 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 112 | LIST_HEAD(smack_rule_list); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 113 | |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 114 | struct smack_parsed_rule { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 115 | struct smack_known *smk_subject; |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 116 | char *smk_object; |
| 117 | int smk_access1; |
| 118 | int smk_access2; |
| 119 | }; |
| 120 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 121 | static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 122 | |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 123 | const char *smack_cipso_option = SMACK_CIPSO_OPTION; |
| 124 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 125 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 126 | * Values for parsing cipso rules |
| 127 | * SMK_DIGITLEN: Length of a digit field in a rule. |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 128 | * SMK_CIPSOMIN: Minimum possible cipso rule length. |
| 129 | * SMK_CIPSOMAX: Maximum possible cipso rule length. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 130 | */ |
| 131 | #define SMK_DIGITLEN 4 |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 132 | #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN) |
| 133 | #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN) |
| 134 | |
| 135 | /* |
| 136 | * Values for parsing MAC rules |
| 137 | * SMK_ACCESS: Maximum possible combination of access permissions |
| 138 | * SMK_ACCESSLEN: Maximum length for a rule access field |
| 139 | * SMK_LOADLEN: Smack rule length |
| 140 | */ |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 141 | #define SMK_OACCESS "rwxa" |
| 142 | #define SMK_ACCESS "rwxat" |
| 143 | #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1) |
| 144 | #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1) |
| 145 | #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN) |
| 146 | #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN) |
Ahmed S. Darwish | b500ce8 | 2008-03-13 12:32:34 -0700 | [diff] [blame] | 147 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 148 | /* |
| 149 | * Stricly for CIPSO level manipulation. |
| 150 | * Set the category bit number in a smack label sized buffer. |
| 151 | */ |
| 152 | static inline void smack_catset_bit(unsigned int cat, char *catsetp) |
| 153 | { |
| 154 | if (cat == 0 || cat > (SMK_CIPSOLEN * 8)) |
| 155 | return; |
| 156 | |
| 157 | catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8); |
| 158 | } |
| 159 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 160 | /** |
| 161 | * smk_netlabel_audit_set - fill a netlbl_audit struct |
| 162 | * @nap: structure to fill |
| 163 | */ |
| 164 | static void smk_netlabel_audit_set(struct netlbl_audit *nap) |
| 165 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 166 | struct smack_known *skp = smk_of_current(); |
| 167 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 168 | nap->loginuid = audit_get_loginuid(current); |
| 169 | nap->sessionid = audit_get_sessionid(current); |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 170 | nap->secid = skp->smk_secid; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 174 | * Value for parsing single label host rules |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 175 | * "1.2.3.4 X" |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 176 | */ |
| 177 | #define SMK_NETLBLADDRMIN 9 |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 178 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 179 | /** |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 180 | * smk_set_access - add a rule to the rule list or replace an old rule |
| 181 | * @srp: the rule to add or replace |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 182 | * @rule_list: the list of rules |
| 183 | * @rule_lock: the rule list lock |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 184 | * @global: if non-zero, indicates a global rule |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 185 | * |
| 186 | * Looks through the current subject/object/access list for |
| 187 | * the subject/object pair and replaces the access that was |
| 188 | * there. If the pair isn't found add it with the specified |
| 189 | * access. |
Sergio Luis | 81ea714 | 2008-12-22 01:16:15 -0300 | [diff] [blame] | 190 | * |
| 191 | * Returns 0 if nothing goes wrong or -ENOMEM if it fails |
| 192 | * during the allocation of the new pair to add. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 193 | */ |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 194 | static int smk_set_access(struct smack_parsed_rule *srp, |
| 195 | struct list_head *rule_list, |
| 196 | struct mutex *rule_lock, int global) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 197 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 198 | struct smack_rule *sp; |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 199 | struct smack_master_list *smlp; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 200 | int found = 0; |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 201 | int rc = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 202 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 203 | mutex_lock(rule_lock); |
| 204 | |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Because the object label is less likely to match |
| 207 | * than the subject label check it first |
| 208 | */ |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 209 | list_for_each_entry_rcu(sp, rule_list, list) { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 210 | if (sp->smk_object == srp->smk_object && |
| 211 | sp->smk_subject == srp->smk_subject) { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 212 | found = 1; |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 213 | sp->smk_access |= srp->smk_access1; |
| 214 | sp->smk_access &= ~srp->smk_access2; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 215 | break; |
| 216 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 219 | if (found == 0) { |
| 220 | sp = kzalloc(sizeof(*sp), GFP_KERNEL); |
| 221 | if (sp == NULL) { |
| 222 | rc = -ENOMEM; |
| 223 | goto out; |
| 224 | } |
| 225 | |
| 226 | sp->smk_subject = srp->smk_subject; |
| 227 | sp->smk_object = srp->smk_object; |
| 228 | sp->smk_access = srp->smk_access1 & ~srp->smk_access2; |
| 229 | |
| 230 | list_add_rcu(&sp->list, rule_list); |
| 231 | /* |
| 232 | * If this is a global as opposed to self and a new rule |
| 233 | * it needs to get added for reporting. |
| 234 | */ |
| 235 | if (global) { |
| 236 | smlp = kzalloc(sizeof(*smlp), GFP_KERNEL); |
| 237 | if (smlp != NULL) { |
| 238 | smlp->smk_rule = sp; |
| 239 | list_add_rcu(&smlp->list, &smack_rule_list); |
| 240 | } else |
| 241 | rc = -ENOMEM; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | out: |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 246 | mutex_unlock(rule_lock); |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 247 | return rc; |
| 248 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 249 | |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 250 | /** |
| 251 | * smk_perm_from_str - parse smack accesses from a text string |
| 252 | * @string: a text string that contains a Smack accesses code |
| 253 | * |
| 254 | * Returns an integer with respective bits set for specified accesses. |
| 255 | */ |
| 256 | static int smk_perm_from_str(const char *string) |
| 257 | { |
| 258 | int perm = 0; |
| 259 | const char *cp; |
| 260 | |
| 261 | for (cp = string; ; cp++) |
| 262 | switch (*cp) { |
| 263 | case '-': |
| 264 | break; |
| 265 | case 'r': |
| 266 | case 'R': |
| 267 | perm |= MAY_READ; |
| 268 | break; |
| 269 | case 'w': |
| 270 | case 'W': |
| 271 | perm |= MAY_WRITE; |
| 272 | break; |
| 273 | case 'x': |
| 274 | case 'X': |
| 275 | perm |= MAY_EXEC; |
| 276 | break; |
| 277 | case 'a': |
| 278 | case 'A': |
| 279 | perm |= MAY_APPEND; |
| 280 | break; |
| 281 | case 't': |
| 282 | case 'T': |
| 283 | perm |= MAY_TRANSMUTE; |
| 284 | break; |
| 285 | default: |
| 286 | return perm; |
| 287 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 291 | * smk_fill_rule - Fill Smack rule from strings |
| 292 | * @subject: subject label string |
| 293 | * @object: object label string |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 294 | * @access1: access string |
| 295 | * @access2: string with permissions to be removed |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 296 | * @rule: Smack rule |
| 297 | * @import: if non-zero, import labels |
Casey Schaufler | 3518721 | 2012-06-18 19:01:36 -0700 | [diff] [blame] | 298 | * @len: label length limit |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 299 | * |
| 300 | * Returns 0 on success, -1 on failure |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 301 | */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 302 | static int smk_fill_rule(const char *subject, const char *object, |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 303 | const char *access1, const char *access2, |
| 304 | struct smack_parsed_rule *rule, int import, |
| 305 | int len) |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 306 | { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 307 | const char *cp; |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 308 | struct smack_known *skp; |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 309 | |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 310 | if (import) { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 311 | rule->smk_subject = smk_import_entry(subject, len); |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 312 | if (rule->smk_subject == NULL) |
| 313 | return -1; |
| 314 | |
Casey Schaufler | 3518721 | 2012-06-18 19:01:36 -0700 | [diff] [blame] | 315 | rule->smk_object = smk_import(object, len); |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 316 | if (rule->smk_object == NULL) |
| 317 | return -1; |
| 318 | } else { |
Casey Schaufler | 3518721 | 2012-06-18 19:01:36 -0700 | [diff] [blame] | 319 | cp = smk_parse_smack(subject, len); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 320 | if (cp == NULL) |
| 321 | return -1; |
| 322 | skp = smk_find_entry(cp); |
| 323 | kfree(cp); |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 324 | if (skp == NULL) |
| 325 | return -1; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 326 | rule->smk_subject = skp; |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 327 | |
Casey Schaufler | 3518721 | 2012-06-18 19:01:36 -0700 | [diff] [blame] | 328 | cp = smk_parse_smack(object, len); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 329 | if (cp == NULL) |
| 330 | return -1; |
| 331 | skp = smk_find_entry(cp); |
| 332 | kfree(cp); |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 333 | if (skp == NULL) |
| 334 | return -1; |
| 335 | rule->smk_object = skp->smk_known; |
| 336 | } |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 337 | |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 338 | rule->smk_access1 = smk_perm_from_str(access1); |
| 339 | if (access2) |
| 340 | rule->smk_access2 = smk_perm_from_str(access2); |
| 341 | else |
| 342 | rule->smk_access2 = ~rule->smk_access1; |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 343 | |
Casey Schaufler | 3518721 | 2012-06-18 19:01:36 -0700 | [diff] [blame] | 344 | return 0; |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | /** |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 348 | * smk_parse_rule - parse Smack rule from load string |
| 349 | * @data: string to be parsed whose size is SMK_LOADLEN |
| 350 | * @rule: Smack rule |
| 351 | * @import: if non-zero, import labels |
| 352 | * |
| 353 | * Returns 0 on success, -1 on errors. |
| 354 | */ |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 355 | static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule, |
| 356 | int import) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 357 | { |
| 358 | int rc; |
| 359 | |
| 360 | rc = smk_fill_rule(data, data + SMK_LABELLEN, |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 361 | data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule, |
| 362 | import, SMK_LABELLEN); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 363 | return rc; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * smk_parse_long_rule - parse Smack rule from rule string |
| 368 | * @data: string to be parsed, null terminated |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 369 | * @rule: Will be filled with Smack parsed rule |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 370 | * @import: if non-zero, import labels |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 371 | * @tokens: numer of substrings expected in data |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 372 | * |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 373 | * Returns number of processed bytes on success, -1 on failure. |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 374 | */ |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 375 | static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule, |
| 376 | int import, int tokens) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 377 | { |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 378 | ssize_t cnt = 0; |
| 379 | char *tok[4]; |
| 380 | int i; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 381 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 382 | /* |
| 383 | * Parsing the rule in-place, filling all white-spaces with '\0' |
| 384 | */ |
| 385 | for (i = 0; i < tokens; ++i) { |
| 386 | while (isspace(data[cnt])) |
| 387 | data[cnt++] = '\0'; |
Alan Cox | 3b9fc37 | 2012-07-26 14:47:11 -0700 | [diff] [blame] | 388 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 389 | if (data[cnt] == '\0') |
| 390 | /* Unexpected end of data */ |
| 391 | return -1; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 392 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 393 | tok[i] = data + cnt; |
| 394 | |
| 395 | while (data[cnt] && !isspace(data[cnt])) |
| 396 | ++cnt; |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 397 | } |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 398 | while (isspace(data[cnt])) |
| 399 | data[cnt++] = '\0'; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 400 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 401 | while (i < 4) |
| 402 | tok[i++] = NULL; |
| 403 | |
| 404 | if (smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0)) |
| 405 | return -1; |
| 406 | |
| 407 | return cnt; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */ |
| 411 | #define SMK_LONG_FMT 1 /* Variable long label format */ |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 412 | #define SMK_CHANGE_FMT 2 /* Rule modification format */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 413 | /** |
| 414 | * smk_write_rules_list - write() for any /smack rule file |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 415 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 416 | * @buf: where to get the data from |
| 417 | * @count: bytes sent |
| 418 | * @ppos: where to start - must be 0 |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 419 | * @rule_list: the list of rules to write to |
| 420 | * @rule_lock: lock for the rule list |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 421 | * @format: /smack/load or /smack/load2 or /smack/change-rule format. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 422 | * |
| 423 | * Get one smack access rule from above. |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 424 | * The format for SMK_LONG_FMT is: |
| 425 | * "subject<whitespace>object<whitespace>access[<whitespace>...]" |
| 426 | * The format for SMK_FIXED24_FMT is exactly: |
| 427 | * "subject object rwxat" |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 428 | * The format for SMK_CHANGE_FMT is: |
| 429 | * "subject<whitespace>object<whitespace> |
| 430 | * acc_enable<whitespace>acc_disable[<whitespace>...]" |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 431 | */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 432 | static ssize_t smk_write_rules_list(struct file *file, const char __user *buf, |
| 433 | size_t count, loff_t *ppos, |
| 434 | struct list_head *rule_list, |
| 435 | struct mutex *rule_lock, int format) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 436 | { |
Tomasz Stanislawski | 470043b | 2013-06-06 09:30:50 +0200 | [diff] [blame] | 437 | struct smack_parsed_rule rule; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 438 | char *data; |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 439 | int rc; |
| 440 | int trunc = 0; |
| 441 | int tokens; |
| 442 | ssize_t cnt = 0; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 443 | |
| 444 | /* |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 445 | * No partial writes. |
| 446 | * Enough data must be present. |
| 447 | */ |
Jarkko Sakkinen | 5c6d112 | 2010-12-07 13:34:01 +0200 | [diff] [blame] | 448 | if (*ppos != 0) |
| 449 | return -EINVAL; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 450 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 451 | if (format == SMK_FIXED24_FMT) { |
| 452 | /* |
| 453 | * Minor hack for backward compatibility |
| 454 | */ |
| 455 | if (count != SMK_OLOADLEN && count != SMK_LOADLEN) |
| 456 | return -EINVAL; |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 457 | } else { |
| 458 | if (count >= PAGE_SIZE) { |
| 459 | count = PAGE_SIZE - 1; |
| 460 | trunc = 1; |
| 461 | } |
| 462 | } |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 463 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 464 | data = kmalloc(count + 1, GFP_KERNEL); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 465 | if (data == NULL) |
| 466 | return -ENOMEM; |
| 467 | |
| 468 | if (copy_from_user(data, buf, count) != 0) { |
| 469 | rc = -EFAULT; |
| 470 | goto out; |
| 471 | } |
| 472 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 473 | /* |
| 474 | * In case of parsing only part of user buf, |
| 475 | * avoid having partial rule at the data buffer |
| 476 | */ |
| 477 | if (trunc) { |
| 478 | while (count > 0 && (data[count - 1] != '\n')) |
| 479 | --count; |
| 480 | if (count == 0) { |
| 481 | rc = -EINVAL; |
Tomasz Stanislawski | 470043b | 2013-06-06 09:30:50 +0200 | [diff] [blame] | 482 | goto out; |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | data[count] = '\0'; |
| 487 | tokens = (format == SMK_CHANGE_FMT ? 4 : 3); |
| 488 | while (cnt < count) { |
| 489 | if (format == SMK_FIXED24_FMT) { |
| 490 | rc = smk_parse_rule(data, &rule, 1); |
| 491 | if (rc != 0) { |
| 492 | rc = -EINVAL; |
| 493 | goto out; |
| 494 | } |
| 495 | cnt = count; |
| 496 | } else { |
| 497 | rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens); |
| 498 | if (rc <= 0) { |
| 499 | rc = -EINVAL; |
| 500 | goto out; |
| 501 | } |
| 502 | cnt += rc; |
| 503 | } |
| 504 | |
| 505 | if (rule_list == NULL) |
| 506 | rc = smk_set_access(&rule, &rule.smk_subject->smk_rules, |
| 507 | &rule.smk_subject->smk_rules_lock, 1); |
| 508 | else |
| 509 | rc = smk_set_access(&rule, rule_list, rule_lock, 0); |
| 510 | |
| 511 | if (rc) |
Tomasz Stanislawski | 470043b | 2013-06-06 09:30:50 +0200 | [diff] [blame] | 512 | goto out; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 515 | rc = cnt; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 516 | out: |
| 517 | kfree(data); |
| 518 | return rc; |
| 519 | } |
| 520 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 521 | /* |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 522 | * Core logic for smackfs seq list operations. |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 523 | */ |
| 524 | |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 525 | static void *smk_seq_start(struct seq_file *s, loff_t *pos, |
| 526 | struct list_head *head) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 527 | { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 528 | struct list_head *list; |
| 529 | |
| 530 | /* |
| 531 | * This is 0 the first time through. |
| 532 | */ |
| 533 | if (s->index == 0) |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 534 | s->private = head; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 535 | |
| 536 | if (s->private == NULL) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 537 | return NULL; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 538 | |
| 539 | list = s->private; |
| 540 | if (list_empty(list)) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 541 | return NULL; |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 542 | |
| 543 | if (s->index == 0) |
| 544 | return list->next; |
| 545 | return list; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 548 | static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos, |
| 549 | struct list_head *head) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 550 | { |
| 551 | struct list_head *list = v; |
| 552 | |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 553 | if (list_is_last(list, head)) { |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 554 | s->private = NULL; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 555 | return NULL; |
| 556 | } |
Casey Schaufler | 272cd7a | 2011-09-20 12:24:36 -0700 | [diff] [blame] | 557 | s->private = list->next; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 558 | return list->next; |
| 559 | } |
| 560 | |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 561 | static void smk_seq_stop(struct seq_file *s, void *v) |
| 562 | { |
| 563 | /* No-op */ |
| 564 | } |
| 565 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 566 | static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max) |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 567 | { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 568 | /* |
| 569 | * Don't show any rules with label names too long for |
| 570 | * interface file (/smack/load or /smack/load2) |
| 571 | * because you should expect to be able to write |
| 572 | * anything you read back. |
| 573 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 574 | if (strlen(srp->smk_subject->smk_known) >= max || |
| 575 | strlen(srp->smk_object) >= max) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 576 | return; |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 577 | |
Rafal Krypa | 65ee7f4 | 2012-07-09 19:36:34 +0200 | [diff] [blame] | 578 | if (srp->smk_access == 0) |
| 579 | return; |
| 580 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 581 | seq_printf(s, "%s %s", srp->smk_subject->smk_known, srp->smk_object); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 582 | |
| 583 | seq_putc(s, ' '); |
| 584 | |
| 585 | if (srp->smk_access & MAY_READ) |
| 586 | seq_putc(s, 'r'); |
| 587 | if (srp->smk_access & MAY_WRITE) |
| 588 | seq_putc(s, 'w'); |
| 589 | if (srp->smk_access & MAY_EXEC) |
| 590 | seq_putc(s, 'x'); |
| 591 | if (srp->smk_access & MAY_APPEND) |
| 592 | seq_putc(s, 'a'); |
| 593 | if (srp->smk_access & MAY_TRANSMUTE) |
| 594 | seq_putc(s, 't'); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 595 | |
| 596 | seq_putc(s, '\n'); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* |
| 600 | * Seq_file read operations for /smack/load |
| 601 | */ |
| 602 | |
| 603 | static void *load2_seq_start(struct seq_file *s, loff_t *pos) |
| 604 | { |
| 605 | return smk_seq_start(s, pos, &smack_rule_list); |
| 606 | } |
| 607 | |
| 608 | static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 609 | { |
| 610 | return smk_seq_next(s, v, pos, &smack_rule_list); |
| 611 | } |
| 612 | |
| 613 | static int load_seq_show(struct seq_file *s, void *v) |
| 614 | { |
| 615 | struct list_head *list = v; |
| 616 | struct smack_master_list *smlp = |
| 617 | list_entry(list, struct smack_master_list, list); |
| 618 | |
| 619 | smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 624 | static const struct seq_operations load_seq_ops = { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 625 | .start = load2_seq_start, |
| 626 | .next = load2_seq_next, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 627 | .show = load_seq_show, |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 628 | .stop = smk_seq_stop, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 629 | }; |
| 630 | |
| 631 | /** |
| 632 | * smk_open_load - open() for /smack/load |
| 633 | * @inode: inode structure representing file |
| 634 | * @file: "load" file pointer |
| 635 | * |
| 636 | * For reading, use load_seq_* seq_file reading operations. |
| 637 | */ |
| 638 | static int smk_open_load(struct inode *inode, struct file *file) |
| 639 | { |
| 640 | return seq_open(file, &load_seq_ops); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * smk_write_load - write() for /smack/load |
| 645 | * @file: file pointer, not actually used |
| 646 | * @buf: where to get the data from |
| 647 | * @count: bytes sent |
| 648 | * @ppos: where to start - must be 0 |
| 649 | * |
| 650 | */ |
| 651 | static ssize_t smk_write_load(struct file *file, const char __user *buf, |
| 652 | size_t count, loff_t *ppos) |
| 653 | { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 654 | /* |
| 655 | * Must have privilege. |
| 656 | * No partial writes. |
| 657 | * Enough data must be present. |
| 658 | */ |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 659 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 660 | return -EPERM; |
| 661 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 662 | return smk_write_rules_list(file, buf, count, ppos, NULL, NULL, |
| 663 | SMK_FIXED24_FMT); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 666 | static const struct file_operations smk_load_ops = { |
| 667 | .open = smk_open_load, |
| 668 | .read = seq_read, |
| 669 | .llseek = seq_lseek, |
| 670 | .write = smk_write_load, |
Ahmed S. Darwish | cb622bb | 2008-03-24 12:29:49 -0700 | [diff] [blame] | 671 | .release = seq_release, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 672 | }; |
| 673 | |
| 674 | /** |
| 675 | * smk_cipso_doi - initialize the CIPSO domain |
| 676 | */ |
Casey Schaufler | 30aa4fa | 2008-04-28 02:13:43 -0700 | [diff] [blame] | 677 | static void smk_cipso_doi(void) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 678 | { |
| 679 | int rc; |
| 680 | struct cipso_v4_doi *doip; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 681 | struct netlbl_audit nai; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 682 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 683 | smk_netlabel_audit_set(&nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 684 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 685 | rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 686 | if (rc != 0) |
| 687 | printk(KERN_WARNING "%s:%d remove rc = %d\n", |
| 688 | __func__, __LINE__, rc); |
| 689 | |
| 690 | doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL); |
| 691 | if (doip == NULL) |
| 692 | panic("smack: Failed to initialize cipso DOI.\n"); |
| 693 | doip->map.std = NULL; |
| 694 | doip->doi = smk_cipso_doi_value; |
| 695 | doip->type = CIPSO_V4_MAP_PASS; |
| 696 | doip->tags[0] = CIPSO_V4_TAG_RBITMAP; |
| 697 | for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++) |
| 698 | doip->tags[rc] = CIPSO_V4_TAG_INVALID; |
| 699 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 700 | rc = netlbl_cfg_cipsov4_add(doip, &nai); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 701 | if (rc != 0) { |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 702 | printk(KERN_WARNING "%s:%d cipso add rc = %d\n", |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 703 | __func__, __LINE__, rc); |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 704 | kfree(doip); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 705 | return; |
| 706 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 707 | rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai); |
Paul Moore | 6c2e8ac | 2008-12-31 12:54:11 -0500 | [diff] [blame] | 708 | if (rc != 0) { |
| 709 | printk(KERN_WARNING "%s:%d map add rc = %d\n", |
| 710 | __func__, __LINE__, rc); |
| 711 | kfree(doip); |
| 712 | return; |
Paul Moore | b1edeb1 | 2008-10-10 10:16:31 -0400 | [diff] [blame] | 713 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 714 | } |
| 715 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 716 | /** |
| 717 | * smk_unlbl_ambient - initialize the unlabeled domain |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 718 | * @oldambient: previous domain string |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 719 | */ |
Casey Schaufler | 30aa4fa | 2008-04-28 02:13:43 -0700 | [diff] [blame] | 720 | static void smk_unlbl_ambient(char *oldambient) |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 721 | { |
| 722 | int rc; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 723 | struct netlbl_audit nai; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 724 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 725 | smk_netlabel_audit_set(&nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 726 | |
| 727 | if (oldambient != NULL) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 728 | rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 729 | if (rc != 0) |
| 730 | printk(KERN_WARNING "%s:%d remove rc = %d\n", |
| 731 | __func__, __LINE__, rc); |
| 732 | } |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 733 | if (smack_net_ambient == NULL) |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 734 | smack_net_ambient = &smack_known_floor; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 735 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 736 | rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET, |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 737 | NULL, NULL, &nai); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 738 | if (rc != 0) |
| 739 | printk(KERN_WARNING "%s:%d add rc = %d\n", |
| 740 | __func__, __LINE__, rc); |
| 741 | } |
| 742 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 743 | /* |
| 744 | * Seq_file read operations for /smack/cipso |
| 745 | */ |
| 746 | |
| 747 | static void *cipso_seq_start(struct seq_file *s, loff_t *pos) |
| 748 | { |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 749 | return smk_seq_start(s, pos, &smack_known_list); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 753 | { |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 754 | return smk_seq_next(s, v, pos, &smack_known_list); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | /* |
| 758 | * Print cipso labels in format: |
| 759 | * label level[/cat[,cat]] |
| 760 | */ |
| 761 | static int cipso_seq_show(struct seq_file *s, void *v) |
| 762 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 763 | struct list_head *list = v; |
| 764 | struct smack_known *skp = |
| 765 | list_entry(list, struct smack_known, list); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 766 | struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 767 | char sep = '/'; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 768 | int i; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 769 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 770 | /* |
| 771 | * Don't show a label that could not have been set using |
| 772 | * /smack/cipso. This is in support of the notion that |
| 773 | * anything read from /smack/cipso ought to be writeable |
| 774 | * to /smack/cipso. |
| 775 | * |
| 776 | * /smack/cipso2 should be used instead. |
| 777 | */ |
| 778 | if (strlen(skp->smk_known) >= SMK_LABELLEN) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 779 | return 0; |
| 780 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 781 | seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 782 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 783 | for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0; |
| 784 | i = netlbl_secattr_catmap_walk(cmp, i + 1)) { |
| 785 | seq_printf(s, "%c%d", sep, i); |
| 786 | sep = ','; |
| 787 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 788 | |
| 789 | seq_putc(s, '\n'); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 794 | static const struct seq_operations cipso_seq_ops = { |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 795 | .start = cipso_seq_start, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 796 | .next = cipso_seq_next, |
| 797 | .show = cipso_seq_show, |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 798 | .stop = smk_seq_stop, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | /** |
| 802 | * smk_open_cipso - open() for /smack/cipso |
| 803 | * @inode: inode structure representing file |
| 804 | * @file: "cipso" file pointer |
| 805 | * |
| 806 | * Connect our cipso_seq_* operations with /smack/cipso |
| 807 | * file_operations |
| 808 | */ |
| 809 | static int smk_open_cipso(struct inode *inode, struct file *file) |
| 810 | { |
| 811 | return seq_open(file, &cipso_seq_ops); |
| 812 | } |
| 813 | |
| 814 | /** |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 815 | * smk_set_cipso - do the work for write() for cipso and cipso2 |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 816 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 817 | * @buf: where to get the data from |
| 818 | * @count: bytes sent |
| 819 | * @ppos: where to start |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 820 | * @format: /smack/cipso or /smack/cipso2 |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 821 | * |
| 822 | * Accepts only one cipso rule per write call. |
| 823 | * Returns number of bytes written or error code, as appropriate |
| 824 | */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 825 | static ssize_t smk_set_cipso(struct file *file, const char __user *buf, |
| 826 | size_t count, loff_t *ppos, int format) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 827 | { |
| 828 | struct smack_known *skp; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 829 | struct netlbl_lsm_secattr ncats; |
| 830 | char mapcatset[SMK_CIPSOLEN]; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 831 | int maplevel; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 832 | unsigned int cat; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 833 | int catlen; |
| 834 | ssize_t rc = -EINVAL; |
| 835 | char *data = NULL; |
| 836 | char *rule; |
| 837 | int ret; |
| 838 | int i; |
| 839 | |
| 840 | /* |
| 841 | * Must have privilege. |
| 842 | * No partial writes. |
| 843 | * Enough data must be present. |
| 844 | */ |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 845 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 846 | return -EPERM; |
| 847 | if (*ppos != 0) |
| 848 | return -EINVAL; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 849 | if (format == SMK_FIXED24_FMT && |
| 850 | (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 851 | return -EINVAL; |
| 852 | |
| 853 | data = kzalloc(count + 1, GFP_KERNEL); |
| 854 | if (data == NULL) |
| 855 | return -ENOMEM; |
| 856 | |
| 857 | if (copy_from_user(data, buf, count) != 0) { |
| 858 | rc = -EFAULT; |
| 859 | goto unlockedout; |
| 860 | } |
| 861 | |
| 862 | data[count] = '\0'; |
| 863 | rule = data; |
| 864 | /* |
| 865 | * Only allow one writer at a time. Writes should be |
| 866 | * quite rare and small in any case. |
| 867 | */ |
| 868 | mutex_lock(&smack_cipso_lock); |
| 869 | |
| 870 | skp = smk_import_entry(rule, 0); |
| 871 | if (skp == NULL) |
| 872 | goto out; |
| 873 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 874 | if (format == SMK_FIXED24_FMT) |
| 875 | rule += SMK_LABELLEN; |
| 876 | else |
Passion,Zhao | 0fcfee6 | 2013-06-03 11:42:24 +0800 | [diff] [blame] | 877 | rule += strlen(skp->smk_known) + 1; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 878 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 879 | ret = sscanf(rule, "%d", &maplevel); |
| 880 | if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL) |
| 881 | goto out; |
| 882 | |
| 883 | rule += SMK_DIGITLEN; |
| 884 | ret = sscanf(rule, "%d", &catlen); |
| 885 | if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM) |
| 886 | goto out; |
| 887 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 888 | if (format == SMK_FIXED24_FMT && |
| 889 | count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 890 | goto out; |
| 891 | |
| 892 | memset(mapcatset, 0, sizeof(mapcatset)); |
| 893 | |
| 894 | for (i = 0; i < catlen; i++) { |
| 895 | rule += SMK_DIGITLEN; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 896 | ret = sscanf(rule, "%u", &cat); |
Casey Schaufler | 677264e | 2013-06-28 13:47:07 -0700 | [diff] [blame] | 897 | if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 898 | goto out; |
| 899 | |
| 900 | smack_catset_bit(cat, mapcatset); |
| 901 | } |
| 902 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 903 | rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN); |
| 904 | if (rc >= 0) { |
| 905 | netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat); |
| 906 | skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat; |
| 907 | skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl; |
| 908 | rc = count; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 911 | out: |
| 912 | mutex_unlock(&smack_cipso_lock); |
| 913 | unlockedout: |
| 914 | kfree(data); |
| 915 | return rc; |
| 916 | } |
| 917 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 918 | /** |
| 919 | * smk_write_cipso - write() for /smack/cipso |
| 920 | * @file: file pointer, not actually used |
| 921 | * @buf: where to get the data from |
| 922 | * @count: bytes sent |
| 923 | * @ppos: where to start |
| 924 | * |
| 925 | * Accepts only one cipso rule per write call. |
| 926 | * Returns number of bytes written or error code, as appropriate |
| 927 | */ |
| 928 | static ssize_t smk_write_cipso(struct file *file, const char __user *buf, |
| 929 | size_t count, loff_t *ppos) |
| 930 | { |
| 931 | return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT); |
| 932 | } |
| 933 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 934 | static const struct file_operations smk_cipso_ops = { |
| 935 | .open = smk_open_cipso, |
| 936 | .read = seq_read, |
| 937 | .llseek = seq_lseek, |
| 938 | .write = smk_write_cipso, |
| 939 | .release = seq_release, |
| 940 | }; |
| 941 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 942 | /* |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 943 | * Seq_file read operations for /smack/cipso2 |
| 944 | */ |
| 945 | |
| 946 | /* |
| 947 | * Print cipso labels in format: |
| 948 | * label level[/cat[,cat]] |
| 949 | */ |
| 950 | static int cipso2_seq_show(struct seq_file *s, void *v) |
| 951 | { |
| 952 | struct list_head *list = v; |
| 953 | struct smack_known *skp = |
| 954 | list_entry(list, struct smack_known, list); |
| 955 | struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat; |
| 956 | char sep = '/'; |
| 957 | int i; |
| 958 | |
| 959 | seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl); |
| 960 | |
| 961 | for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0; |
| 962 | i = netlbl_secattr_catmap_walk(cmp, i + 1)) { |
| 963 | seq_printf(s, "%c%d", sep, i); |
| 964 | sep = ','; |
| 965 | } |
| 966 | |
| 967 | seq_putc(s, '\n'); |
| 968 | |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static const struct seq_operations cipso2_seq_ops = { |
| 973 | .start = cipso_seq_start, |
| 974 | .next = cipso_seq_next, |
| 975 | .show = cipso2_seq_show, |
| 976 | .stop = smk_seq_stop, |
| 977 | }; |
| 978 | |
| 979 | /** |
| 980 | * smk_open_cipso2 - open() for /smack/cipso2 |
| 981 | * @inode: inode structure representing file |
| 982 | * @file: "cipso2" file pointer |
| 983 | * |
| 984 | * Connect our cipso_seq_* operations with /smack/cipso2 |
| 985 | * file_operations |
| 986 | */ |
| 987 | static int smk_open_cipso2(struct inode *inode, struct file *file) |
| 988 | { |
| 989 | return seq_open(file, &cipso2_seq_ops); |
| 990 | } |
| 991 | |
| 992 | /** |
| 993 | * smk_write_cipso2 - write() for /smack/cipso2 |
| 994 | * @file: file pointer, not actually used |
| 995 | * @buf: where to get the data from |
| 996 | * @count: bytes sent |
| 997 | * @ppos: where to start |
| 998 | * |
| 999 | * Accepts only one cipso rule per write call. |
| 1000 | * Returns number of bytes written or error code, as appropriate |
| 1001 | */ |
| 1002 | static ssize_t smk_write_cipso2(struct file *file, const char __user *buf, |
| 1003 | size_t count, loff_t *ppos) |
| 1004 | { |
| 1005 | return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT); |
| 1006 | } |
| 1007 | |
| 1008 | static const struct file_operations smk_cipso2_ops = { |
| 1009 | .open = smk_open_cipso2, |
| 1010 | .read = seq_read, |
| 1011 | .llseek = seq_lseek, |
| 1012 | .write = smk_write_cipso2, |
| 1013 | .release = seq_release, |
| 1014 | }; |
| 1015 | |
| 1016 | /* |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1017 | * Seq_file read operations for /smack/netlabel |
| 1018 | */ |
| 1019 | |
| 1020 | static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos) |
| 1021 | { |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 1022 | return smk_seq_start(s, pos, &smk_netlbladdr_list); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1026 | { |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 1027 | return smk_seq_next(s, v, pos, &smk_netlbladdr_list); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1028 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1029 | #define BEBITS (sizeof(__be32) * 8) |
| 1030 | |
| 1031 | /* |
| 1032 | * Print host/label pairs |
| 1033 | */ |
| 1034 | static int netlbladdr_seq_show(struct seq_file *s, void *v) |
| 1035 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1036 | struct list_head *list = v; |
| 1037 | struct smk_netlbladdr *skp = |
| 1038 | list_entry(list, struct smk_netlbladdr, list); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1039 | unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1040 | int maskn; |
| 1041 | u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1042 | |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1043 | for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1044 | |
| 1045 | seq_printf(s, "%u.%u.%u.%u/%d %s\n", |
| 1046 | hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label); |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 1051 | static const struct seq_operations netlbladdr_seq_ops = { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1052 | .start = netlbladdr_seq_start, |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1053 | .next = netlbladdr_seq_next, |
| 1054 | .show = netlbladdr_seq_show, |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 1055 | .stop = smk_seq_stop, |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1056 | }; |
| 1057 | |
| 1058 | /** |
| 1059 | * smk_open_netlbladdr - open() for /smack/netlabel |
| 1060 | * @inode: inode structure representing file |
| 1061 | * @file: "netlabel" file pointer |
| 1062 | * |
| 1063 | * Connect our netlbladdr_seq_* operations with /smack/netlabel |
| 1064 | * file_operations |
| 1065 | */ |
| 1066 | static int smk_open_netlbladdr(struct inode *inode, struct file *file) |
| 1067 | { |
| 1068 | return seq_open(file, &netlbladdr_seq_ops); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1072 | * smk_netlbladdr_insert |
| 1073 | * @new : netlabel to insert |
| 1074 | * |
| 1075 | * This helper insert netlabel in the smack_netlbladdrs list |
| 1076 | * sorted by netmask length (longest to smallest) |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1077 | * locked by &smk_netlbladdr_lock in smk_write_netlbladdr |
| 1078 | * |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1079 | */ |
| 1080 | static void smk_netlbladdr_insert(struct smk_netlbladdr *new) |
| 1081 | { |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1082 | struct smk_netlbladdr *m, *m_next; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1083 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1084 | if (list_empty(&smk_netlbladdr_list)) { |
| 1085 | list_add_rcu(&new->list, &smk_netlbladdr_list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1086 | return; |
| 1087 | } |
| 1088 | |
Jiri Pirko | 05725f7 | 2009-04-14 20:17:16 +0200 | [diff] [blame] | 1089 | m = list_entry_rcu(smk_netlbladdr_list.next, |
| 1090 | struct smk_netlbladdr, list); |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1091 | |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1092 | /* the comparison '>' is a bit hacky, but works */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1093 | if (new->smk_mask.s_addr > m->smk_mask.s_addr) { |
| 1094 | list_add_rcu(&new->list, &smk_netlbladdr_list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1095 | return; |
| 1096 | } |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1097 | |
| 1098 | list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) { |
| 1099 | if (list_is_last(&m->list, &smk_netlbladdr_list)) { |
| 1100 | list_add_rcu(&new->list, &m->list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1101 | return; |
| 1102 | } |
Jiri Pirko | 05725f7 | 2009-04-14 20:17:16 +0200 | [diff] [blame] | 1103 | m_next = list_entry_rcu(m->list.next, |
| 1104 | struct smk_netlbladdr, list); |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1105 | if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) { |
| 1106 | list_add_rcu(&new->list, &m->list); |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1107 | return; |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | /** |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1114 | * smk_write_netlbladdr - write() for /smack/netlabel |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1115 | * @file: file pointer, not actually used |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1116 | * @buf: where to get the data from |
| 1117 | * @count: bytes sent |
| 1118 | * @ppos: where to start |
| 1119 | * |
| 1120 | * Accepts only one netlbladdr per write call. |
| 1121 | * Returns number of bytes written or error code, as appropriate |
| 1122 | */ |
| 1123 | static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf, |
| 1124 | size_t count, loff_t *ppos) |
| 1125 | { |
| 1126 | struct smk_netlbladdr *skp; |
| 1127 | struct sockaddr_in newname; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1128 | char *smack; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1129 | char *sp; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1130 | char *data; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1131 | char *host = (char *)&newname.sin_addr.s_addr; |
| 1132 | int rc; |
| 1133 | struct netlbl_audit audit_info; |
| 1134 | struct in_addr mask; |
| 1135 | unsigned int m; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1136 | int found; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1137 | u32 mask_bits = (1<<31); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1138 | __be32 nsa; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1139 | u32 temp_mask; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1140 | |
| 1141 | /* |
| 1142 | * Must have privilege. |
| 1143 | * No partial writes. |
| 1144 | * Enough data must be present. |
| 1145 | * "<addr/mask, as a.b.c.d/e><space><label>" |
| 1146 | * "<addr, as a.b.c.d><space><label>" |
| 1147 | */ |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1148 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1149 | return -EPERM; |
| 1150 | if (*ppos != 0) |
| 1151 | return -EINVAL; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1152 | if (count < SMK_NETLBLADDRMIN) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1153 | return -EINVAL; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1154 | |
| 1155 | data = kzalloc(count + 1, GFP_KERNEL); |
| 1156 | if (data == NULL) |
| 1157 | return -ENOMEM; |
| 1158 | |
| 1159 | if (copy_from_user(data, buf, count) != 0) { |
| 1160 | rc = -EFAULT; |
| 1161 | goto free_data_out; |
| 1162 | } |
| 1163 | |
| 1164 | smack = kzalloc(count + 1, GFP_KERNEL); |
| 1165 | if (smack == NULL) { |
| 1166 | rc = -ENOMEM; |
| 1167 | goto free_data_out; |
| 1168 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1169 | |
| 1170 | data[count] = '\0'; |
| 1171 | |
| 1172 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s", |
| 1173 | &host[0], &host[1], &host[2], &host[3], &m, smack); |
| 1174 | if (rc != 6) { |
| 1175 | rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s", |
| 1176 | &host[0], &host[1], &host[2], &host[3], smack); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1177 | if (rc != 5) { |
| 1178 | rc = -EINVAL; |
| 1179 | goto free_out; |
| 1180 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1181 | m = BEBITS; |
| 1182 | } |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1183 | if (m > BEBITS) { |
| 1184 | rc = -EINVAL; |
| 1185 | goto free_out; |
| 1186 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1187 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1188 | /* |
| 1189 | * If smack begins with '-', it is an option, don't import it |
| 1190 | */ |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1191 | if (smack[0] != '-') { |
| 1192 | sp = smk_import(smack, 0); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1193 | if (sp == NULL) { |
| 1194 | rc = -EINVAL; |
| 1195 | goto free_out; |
| 1196 | } |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1197 | } else { |
| 1198 | /* check known options */ |
| 1199 | if (strcmp(smack, smack_cipso_option) == 0) |
| 1200 | sp = (char *)smack_cipso_option; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1201 | else { |
| 1202 | rc = -EINVAL; |
| 1203 | goto free_out; |
| 1204 | } |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1205 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1206 | |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1207 | for (temp_mask = 0; m > 0; m--) { |
| 1208 | temp_mask |= mask_bits; |
| 1209 | mask_bits >>= 1; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1210 | } |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1211 | mask.s_addr = cpu_to_be32(temp_mask); |
| 1212 | |
| 1213 | newname.sin_addr.s_addr &= mask.s_addr; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1214 | /* |
| 1215 | * Only allow one writer at a time. Writes should be |
| 1216 | * quite rare and small in any case. |
| 1217 | */ |
| 1218 | mutex_lock(&smk_netlbladdr_lock); |
| 1219 | |
| 1220 | nsa = newname.sin_addr.s_addr; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1221 | /* try to find if the prefix is already in the list */ |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1222 | found = 0; |
| 1223 | list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1224 | if (skp->smk_host.sin_addr.s_addr == nsa && |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1225 | skp->smk_mask.s_addr == mask.s_addr) { |
| 1226 | found = 1; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1227 | break; |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1228 | } |
| 1229 | } |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1230 | smk_netlabel_audit_set(&audit_info); |
| 1231 | |
Etienne Basset | 7198e2e | 2009-03-24 20:53:24 +0100 | [diff] [blame] | 1232 | if (found == 0) { |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1233 | skp = kzalloc(sizeof(*skp), GFP_KERNEL); |
| 1234 | if (skp == NULL) |
| 1235 | rc = -ENOMEM; |
| 1236 | else { |
| 1237 | rc = 0; |
| 1238 | skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr; |
| 1239 | skp->smk_mask.s_addr = mask.s_addr; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1240 | skp->smk_label = sp; |
etienne | 113a0e4 | 2009-03-04 07:33:51 +0100 | [diff] [blame] | 1241 | smk_netlbladdr_insert(skp); |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1242 | } |
| 1243 | } else { |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1244 | /* we delete the unlabeled entry, only if the previous label |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1245 | * wasn't the special CIPSO option */ |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1246 | if (skp->smk_label != smack_cipso_option) |
| 1247 | rc = netlbl_cfg_unlbl_static_del(&init_net, NULL, |
| 1248 | &skp->smk_host.sin_addr, &skp->smk_mask, |
| 1249 | PF_INET, &audit_info); |
| 1250 | else |
| 1251 | rc = 0; |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1252 | skp->smk_label = sp; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Now tell netlabel about the single label nature of |
| 1257 | * this host so that incoming packets get labeled. |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1258 | * but only if we didn't get the special CIPSO option |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1259 | */ |
Etienne Basset | 4303154 | 2009-03-27 17:11:01 -0400 | [diff] [blame] | 1260 | if (rc == 0 && sp != smack_cipso_option) |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1261 | rc = netlbl_cfg_unlbl_static_add(&init_net, NULL, |
| 1262 | &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET, |
| 1263 | smack_to_secid(skp->smk_label), &audit_info); |
| 1264 | |
| 1265 | if (rc == 0) |
| 1266 | rc = count; |
| 1267 | |
| 1268 | mutex_unlock(&smk_netlbladdr_lock); |
| 1269 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1270 | free_out: |
| 1271 | kfree(smack); |
| 1272 | free_data_out: |
| 1273 | kfree(data); |
| 1274 | |
Casey Schaufler | 6d3dc07 | 2008-12-31 12:54:12 -0500 | [diff] [blame] | 1275 | return rc; |
| 1276 | } |
| 1277 | |
| 1278 | static const struct file_operations smk_netlbladdr_ops = { |
| 1279 | .open = smk_open_netlbladdr, |
| 1280 | .read = seq_read, |
| 1281 | .llseek = seq_lseek, |
| 1282 | .write = smk_write_netlbladdr, |
| 1283 | .release = seq_release, |
| 1284 | }; |
| 1285 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1286 | /** |
| 1287 | * smk_read_doi - read() for /smack/doi |
| 1288 | * @filp: file pointer, not actually used |
| 1289 | * @buf: where to put the result |
| 1290 | * @count: maximum to send along |
| 1291 | * @ppos: where to start |
| 1292 | * |
| 1293 | * Returns number of bytes read or error code, as appropriate |
| 1294 | */ |
| 1295 | static ssize_t smk_read_doi(struct file *filp, char __user *buf, |
| 1296 | size_t count, loff_t *ppos) |
| 1297 | { |
| 1298 | char temp[80]; |
| 1299 | ssize_t rc; |
| 1300 | |
| 1301 | if (*ppos != 0) |
| 1302 | return 0; |
| 1303 | |
| 1304 | sprintf(temp, "%d", smk_cipso_doi_value); |
| 1305 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1306 | |
| 1307 | return rc; |
| 1308 | } |
| 1309 | |
| 1310 | /** |
| 1311 | * smk_write_doi - write() for /smack/doi |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1312 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1313 | * @buf: where to get the data from |
| 1314 | * @count: bytes sent |
| 1315 | * @ppos: where to start |
| 1316 | * |
| 1317 | * Returns number of bytes written or error code, as appropriate |
| 1318 | */ |
| 1319 | static ssize_t smk_write_doi(struct file *file, const char __user *buf, |
| 1320 | size_t count, loff_t *ppos) |
| 1321 | { |
| 1322 | char temp[80]; |
| 1323 | int i; |
| 1324 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1325 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1326 | return -EPERM; |
| 1327 | |
| 1328 | if (count >= sizeof(temp) || count == 0) |
| 1329 | return -EINVAL; |
| 1330 | |
| 1331 | if (copy_from_user(temp, buf, count) != 0) |
| 1332 | return -EFAULT; |
| 1333 | |
| 1334 | temp[count] = '\0'; |
| 1335 | |
| 1336 | if (sscanf(temp, "%d", &i) != 1) |
| 1337 | return -EINVAL; |
| 1338 | |
| 1339 | smk_cipso_doi_value = i; |
| 1340 | |
| 1341 | smk_cipso_doi(); |
| 1342 | |
| 1343 | return count; |
| 1344 | } |
| 1345 | |
| 1346 | static const struct file_operations smk_doi_ops = { |
| 1347 | .read = smk_read_doi, |
| 1348 | .write = smk_write_doi, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1349 | .llseek = default_llseek, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1350 | }; |
| 1351 | |
| 1352 | /** |
| 1353 | * smk_read_direct - read() for /smack/direct |
| 1354 | * @filp: file pointer, not actually used |
| 1355 | * @buf: where to put the result |
| 1356 | * @count: maximum to send along |
| 1357 | * @ppos: where to start |
| 1358 | * |
| 1359 | * Returns number of bytes read or error code, as appropriate |
| 1360 | */ |
| 1361 | static ssize_t smk_read_direct(struct file *filp, char __user *buf, |
| 1362 | size_t count, loff_t *ppos) |
| 1363 | { |
| 1364 | char temp[80]; |
| 1365 | ssize_t rc; |
| 1366 | |
| 1367 | if (*ppos != 0) |
| 1368 | return 0; |
| 1369 | |
| 1370 | sprintf(temp, "%d", smack_cipso_direct); |
| 1371 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1372 | |
| 1373 | return rc; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * smk_write_direct - write() for /smack/direct |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1378 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1379 | * @buf: where to get the data from |
| 1380 | * @count: bytes sent |
| 1381 | * @ppos: where to start |
| 1382 | * |
| 1383 | * Returns number of bytes written or error code, as appropriate |
| 1384 | */ |
| 1385 | static ssize_t smk_write_direct(struct file *file, const char __user *buf, |
| 1386 | size_t count, loff_t *ppos) |
| 1387 | { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1388 | struct smack_known *skp; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1389 | char temp[80]; |
| 1390 | int i; |
| 1391 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1392 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1393 | return -EPERM; |
| 1394 | |
| 1395 | if (count >= sizeof(temp) || count == 0) |
| 1396 | return -EINVAL; |
| 1397 | |
| 1398 | if (copy_from_user(temp, buf, count) != 0) |
| 1399 | return -EFAULT; |
| 1400 | |
| 1401 | temp[count] = '\0'; |
| 1402 | |
| 1403 | if (sscanf(temp, "%d", &i) != 1) |
| 1404 | return -EINVAL; |
| 1405 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1406 | /* |
| 1407 | * Don't do anything if the value hasn't actually changed. |
| 1408 | * If it is changing reset the level on entries that were |
| 1409 | * set up to be direct when they were created. |
| 1410 | */ |
| 1411 | if (smack_cipso_direct != i) { |
| 1412 | mutex_lock(&smack_known_lock); |
| 1413 | list_for_each_entry_rcu(skp, &smack_known_list, list) |
| 1414 | if (skp->smk_netlabel.attr.mls.lvl == |
| 1415 | smack_cipso_direct) |
| 1416 | skp->smk_netlabel.attr.mls.lvl = i; |
| 1417 | smack_cipso_direct = i; |
| 1418 | mutex_unlock(&smack_known_lock); |
| 1419 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1420 | |
| 1421 | return count; |
| 1422 | } |
| 1423 | |
| 1424 | static const struct file_operations smk_direct_ops = { |
| 1425 | .read = smk_read_direct, |
| 1426 | .write = smk_write_direct, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1427 | .llseek = default_llseek, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1428 | }; |
| 1429 | |
| 1430 | /** |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1431 | * smk_read_mapped - read() for /smack/mapped |
| 1432 | * @filp: file pointer, not actually used |
| 1433 | * @buf: where to put the result |
| 1434 | * @count: maximum to send along |
| 1435 | * @ppos: where to start |
| 1436 | * |
| 1437 | * Returns number of bytes read or error code, as appropriate |
| 1438 | */ |
| 1439 | static ssize_t smk_read_mapped(struct file *filp, char __user *buf, |
| 1440 | size_t count, loff_t *ppos) |
| 1441 | { |
| 1442 | char temp[80]; |
| 1443 | ssize_t rc; |
| 1444 | |
| 1445 | if (*ppos != 0) |
| 1446 | return 0; |
| 1447 | |
| 1448 | sprintf(temp, "%d", smack_cipso_mapped); |
| 1449 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1450 | |
| 1451 | return rc; |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * smk_write_mapped - write() for /smack/mapped |
| 1456 | * @file: file pointer, not actually used |
| 1457 | * @buf: where to get the data from |
| 1458 | * @count: bytes sent |
| 1459 | * @ppos: where to start |
| 1460 | * |
| 1461 | * Returns number of bytes written or error code, as appropriate |
| 1462 | */ |
| 1463 | static ssize_t smk_write_mapped(struct file *file, const char __user *buf, |
| 1464 | size_t count, loff_t *ppos) |
| 1465 | { |
| 1466 | struct smack_known *skp; |
| 1467 | char temp[80]; |
| 1468 | int i; |
| 1469 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1470 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1471 | return -EPERM; |
| 1472 | |
| 1473 | if (count >= sizeof(temp) || count == 0) |
| 1474 | return -EINVAL; |
| 1475 | |
| 1476 | if (copy_from_user(temp, buf, count) != 0) |
| 1477 | return -EFAULT; |
| 1478 | |
| 1479 | temp[count] = '\0'; |
| 1480 | |
| 1481 | if (sscanf(temp, "%d", &i) != 1) |
| 1482 | return -EINVAL; |
| 1483 | |
| 1484 | /* |
| 1485 | * Don't do anything if the value hasn't actually changed. |
| 1486 | * If it is changing reset the level on entries that were |
| 1487 | * set up to be mapped when they were created. |
| 1488 | */ |
| 1489 | if (smack_cipso_mapped != i) { |
| 1490 | mutex_lock(&smack_known_lock); |
| 1491 | list_for_each_entry_rcu(skp, &smack_known_list, list) |
| 1492 | if (skp->smk_netlabel.attr.mls.lvl == |
| 1493 | smack_cipso_mapped) |
| 1494 | skp->smk_netlabel.attr.mls.lvl = i; |
| 1495 | smack_cipso_mapped = i; |
| 1496 | mutex_unlock(&smack_known_lock); |
| 1497 | } |
| 1498 | |
| 1499 | return count; |
| 1500 | } |
| 1501 | |
| 1502 | static const struct file_operations smk_mapped_ops = { |
| 1503 | .read = smk_read_mapped, |
| 1504 | .write = smk_write_mapped, |
| 1505 | .llseek = default_llseek, |
| 1506 | }; |
| 1507 | |
| 1508 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1509 | * smk_read_ambient - read() for /smack/ambient |
| 1510 | * @filp: file pointer, not actually used |
| 1511 | * @buf: where to put the result |
| 1512 | * @cn: maximum to send along |
| 1513 | * @ppos: where to start |
| 1514 | * |
| 1515 | * Returns number of bytes read or error code, as appropriate |
| 1516 | */ |
| 1517 | static ssize_t smk_read_ambient(struct file *filp, char __user *buf, |
| 1518 | size_t cn, loff_t *ppos) |
| 1519 | { |
| 1520 | ssize_t rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1521 | int asize; |
| 1522 | |
| 1523 | if (*ppos != 0) |
| 1524 | return 0; |
| 1525 | /* |
| 1526 | * Being careful to avoid a problem in the case where |
| 1527 | * smack_net_ambient gets changed in midstream. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1528 | */ |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1529 | mutex_lock(&smack_ambient_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1530 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1531 | asize = strlen(smack_net_ambient->smk_known) + 1; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1532 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1533 | if (cn >= asize) |
| 1534 | rc = simple_read_from_buffer(buf, cn, ppos, |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1535 | smack_net_ambient->smk_known, |
| 1536 | asize); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1537 | else |
| 1538 | rc = -EINVAL; |
| 1539 | |
| 1540 | mutex_unlock(&smack_ambient_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1541 | |
| 1542 | return rc; |
| 1543 | } |
| 1544 | |
| 1545 | /** |
| 1546 | * smk_write_ambient - write() for /smack/ambient |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1547 | * @file: file pointer, not actually used |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1548 | * @buf: where to get the data from |
| 1549 | * @count: bytes sent |
| 1550 | * @ppos: where to start |
| 1551 | * |
| 1552 | * Returns number of bytes written or error code, as appropriate |
| 1553 | */ |
| 1554 | static ssize_t smk_write_ambient(struct file *file, const char __user *buf, |
| 1555 | size_t count, loff_t *ppos) |
| 1556 | { |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1557 | struct smack_known *skp; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1558 | char *oldambient; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1559 | char *data; |
| 1560 | int rc = count; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1561 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1562 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1563 | return -EPERM; |
| 1564 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1565 | data = kzalloc(count + 1, GFP_KERNEL); |
| 1566 | if (data == NULL) |
| 1567 | return -ENOMEM; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1568 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1569 | if (copy_from_user(data, buf, count) != 0) { |
| 1570 | rc = -EFAULT; |
| 1571 | goto out; |
| 1572 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1573 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1574 | skp = smk_import_entry(data, count); |
| 1575 | if (skp == NULL) { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1576 | rc = -EINVAL; |
| 1577 | goto out; |
| 1578 | } |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1579 | |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1580 | mutex_lock(&smack_ambient_lock); |
| 1581 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1582 | oldambient = smack_net_ambient->smk_known; |
| 1583 | smack_net_ambient = skp; |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 1584 | smk_unlbl_ambient(oldambient); |
| 1585 | |
| 1586 | mutex_unlock(&smack_ambient_lock); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1587 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1588 | out: |
| 1589 | kfree(data); |
| 1590 | return rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | static const struct file_operations smk_ambient_ops = { |
| 1594 | .read = smk_read_ambient, |
| 1595 | .write = smk_write_ambient, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1596 | .llseek = default_llseek, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1597 | }; |
| 1598 | |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1599 | /** |
| 1600 | * smk_read_onlycap - read() for /smack/onlycap |
| 1601 | * @filp: file pointer, not actually used |
| 1602 | * @buf: where to put the result |
| 1603 | * @cn: maximum to send along |
| 1604 | * @ppos: where to start |
| 1605 | * |
| 1606 | * Returns number of bytes read or error code, as appropriate |
| 1607 | */ |
| 1608 | static ssize_t smk_read_onlycap(struct file *filp, char __user *buf, |
| 1609 | size_t cn, loff_t *ppos) |
| 1610 | { |
| 1611 | char *smack = ""; |
| 1612 | ssize_t rc = -EINVAL; |
| 1613 | int asize; |
| 1614 | |
| 1615 | if (*ppos != 0) |
| 1616 | return 0; |
| 1617 | |
| 1618 | if (smack_onlycap != NULL) |
| 1619 | smack = smack_onlycap; |
| 1620 | |
| 1621 | asize = strlen(smack) + 1; |
| 1622 | |
| 1623 | if (cn >= asize) |
| 1624 | rc = simple_read_from_buffer(buf, cn, ppos, smack, asize); |
| 1625 | |
| 1626 | return rc; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * smk_write_onlycap - write() for /smack/onlycap |
Randy Dunlap | 251a2a9 | 2009-02-18 11:42:33 -0800 | [diff] [blame] | 1631 | * @file: file pointer, not actually used |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1632 | * @buf: where to get the data from |
| 1633 | * @count: bytes sent |
| 1634 | * @ppos: where to start |
| 1635 | * |
| 1636 | * Returns number of bytes written or error code, as appropriate |
| 1637 | */ |
| 1638 | static ssize_t smk_write_onlycap(struct file *file, const char __user *buf, |
| 1639 | size_t count, loff_t *ppos) |
| 1640 | { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1641 | char *data; |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1642 | struct smack_known *skp = smk_of_task(current->cred->security); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1643 | int rc = count; |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1644 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1645 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1646 | return -EPERM; |
| 1647 | |
| 1648 | /* |
| 1649 | * This can be done using smk_access() but is done |
| 1650 | * explicitly for clarity. The smk_access() implementation |
| 1651 | * would use smk_access(smack_onlycap, MAY_WRITE) |
| 1652 | */ |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1653 | if (smack_onlycap != NULL && smack_onlycap != skp->smk_known) |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1654 | return -EPERM; |
| 1655 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1656 | data = kzalloc(count, GFP_KERNEL); |
| 1657 | if (data == NULL) |
| 1658 | return -ENOMEM; |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1659 | |
| 1660 | /* |
| 1661 | * Should the null string be passed in unset the onlycap value. |
| 1662 | * This seems like something to be careful with as usually |
| 1663 | * smk_import only expects to return NULL for errors. It |
| 1664 | * is usually the case that a nullstring or "\n" would be |
| 1665 | * bad to pass to smk_import but in fact this is useful here. |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1666 | * |
| 1667 | * smk_import will also reject a label beginning with '-', |
| 1668 | * so "-usecapabilities" will also work. |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1669 | */ |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1670 | if (copy_from_user(data, buf, count) != 0) |
| 1671 | rc = -EFAULT; |
| 1672 | else |
| 1673 | smack_onlycap = smk_import(data, count); |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1674 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1675 | kfree(data); |
| 1676 | return rc; |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | static const struct file_operations smk_onlycap_ops = { |
| 1680 | .read = smk_read_onlycap, |
| 1681 | .write = smk_write_onlycap, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1682 | .llseek = default_llseek, |
Casey Schaufler | 1544623 | 2008-07-30 15:37:11 -0700 | [diff] [blame] | 1683 | }; |
| 1684 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 1685 | /** |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1686 | * smk_read_logging - read() for /smack/logging |
| 1687 | * @filp: file pointer, not actually used |
| 1688 | * @buf: where to put the result |
| 1689 | * @cn: maximum to send along |
| 1690 | * @ppos: where to start |
| 1691 | * |
| 1692 | * Returns number of bytes read or error code, as appropriate |
| 1693 | */ |
| 1694 | static ssize_t smk_read_logging(struct file *filp, char __user *buf, |
| 1695 | size_t count, loff_t *ppos) |
| 1696 | { |
| 1697 | char temp[32]; |
| 1698 | ssize_t rc; |
| 1699 | |
| 1700 | if (*ppos != 0) |
| 1701 | return 0; |
| 1702 | |
| 1703 | sprintf(temp, "%d\n", log_policy); |
| 1704 | rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp)); |
| 1705 | return rc; |
| 1706 | } |
| 1707 | |
| 1708 | /** |
| 1709 | * smk_write_logging - write() for /smack/logging |
| 1710 | * @file: file pointer, not actually used |
| 1711 | * @buf: where to get the data from |
| 1712 | * @count: bytes sent |
| 1713 | * @ppos: where to start |
| 1714 | * |
| 1715 | * Returns number of bytes written or error code, as appropriate |
| 1716 | */ |
| 1717 | static ssize_t smk_write_logging(struct file *file, const char __user *buf, |
| 1718 | size_t count, loff_t *ppos) |
| 1719 | { |
| 1720 | char temp[32]; |
| 1721 | int i; |
| 1722 | |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1723 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1724 | return -EPERM; |
| 1725 | |
| 1726 | if (count >= sizeof(temp) || count == 0) |
| 1727 | return -EINVAL; |
| 1728 | |
| 1729 | if (copy_from_user(temp, buf, count) != 0) |
| 1730 | return -EFAULT; |
| 1731 | |
| 1732 | temp[count] = '\0'; |
| 1733 | |
| 1734 | if (sscanf(temp, "%d", &i) != 1) |
| 1735 | return -EINVAL; |
| 1736 | if (i < 0 || i > 3) |
| 1737 | return -EINVAL; |
| 1738 | log_policy = i; |
| 1739 | return count; |
| 1740 | } |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | static const struct file_operations smk_logging_ops = { |
| 1745 | .read = smk_read_logging, |
| 1746 | .write = smk_write_logging, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1747 | .llseek = default_llseek, |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 1748 | }; |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1749 | |
| 1750 | /* |
| 1751 | * Seq_file read operations for /smack/load-self |
| 1752 | */ |
| 1753 | |
| 1754 | static void *load_self_seq_start(struct seq_file *s, loff_t *pos) |
| 1755 | { |
| 1756 | struct task_smack *tsp = current_security(); |
| 1757 | |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 1758 | return smk_seq_start(s, pos, &tsp->smk_rules); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1762 | { |
| 1763 | struct task_smack *tsp = current_security(); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1764 | |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 1765 | return smk_seq_next(s, v, pos, &tsp->smk_rules); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | static int load_self_seq_show(struct seq_file *s, void *v) |
| 1769 | { |
| 1770 | struct list_head *list = v; |
| 1771 | struct smack_rule *srp = |
| 1772 | list_entry(list, struct smack_rule, list); |
| 1773 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1774 | smk_rule_show(s, srp, SMK_LABELLEN); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1775 | |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1779 | static const struct seq_operations load_self_seq_ops = { |
| 1780 | .start = load_self_seq_start, |
| 1781 | .next = load_self_seq_next, |
| 1782 | .show = load_self_seq_show, |
Casey Schaufler | 4080956 | 2011-11-10 15:02:22 -0800 | [diff] [blame] | 1783 | .stop = smk_seq_stop, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1784 | }; |
| 1785 | |
| 1786 | |
| 1787 | /** |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1788 | * smk_open_load_self - open() for /smack/load-self2 |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1789 | * @inode: inode structure representing file |
| 1790 | * @file: "load" file pointer |
| 1791 | * |
| 1792 | * For reading, use load_seq_* seq_file reading operations. |
| 1793 | */ |
| 1794 | static int smk_open_load_self(struct inode *inode, struct file *file) |
| 1795 | { |
| 1796 | return seq_open(file, &load_self_seq_ops); |
| 1797 | } |
| 1798 | |
| 1799 | /** |
| 1800 | * smk_write_load_self - write() for /smack/load-self |
| 1801 | * @file: file pointer, not actually used |
| 1802 | * @buf: where to get the data from |
| 1803 | * @count: bytes sent |
| 1804 | * @ppos: where to start - must be 0 |
| 1805 | * |
| 1806 | */ |
| 1807 | static ssize_t smk_write_load_self(struct file *file, const char __user *buf, |
| 1808 | size_t count, loff_t *ppos) |
| 1809 | { |
| 1810 | struct task_smack *tsp = current_security(); |
| 1811 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1812 | return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, |
| 1813 | &tsp->smk_rules_lock, SMK_FIXED24_FMT); |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | static const struct file_operations smk_load_self_ops = { |
| 1817 | .open = smk_open_load_self, |
| 1818 | .read = seq_read, |
| 1819 | .llseek = seq_lseek, |
| 1820 | .write = smk_write_load_self, |
| 1821 | .release = seq_release, |
| 1822 | }; |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 1823 | |
| 1824 | /** |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1825 | * smk_user_access - handle access check transaction |
| 1826 | * @file: file pointer |
| 1827 | * @buf: data from user space |
| 1828 | * @count: bytes sent |
| 1829 | * @ppos: where to start - must be 0 |
| 1830 | */ |
| 1831 | static ssize_t smk_user_access(struct file *file, const char __user *buf, |
| 1832 | size_t count, loff_t *ppos, int format) |
| 1833 | { |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 1834 | struct smack_parsed_rule rule; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1835 | char *data; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1836 | int res; |
| 1837 | |
| 1838 | data = simple_transaction_get(file, buf, count); |
| 1839 | if (IS_ERR(data)) |
| 1840 | return PTR_ERR(data); |
| 1841 | |
| 1842 | if (format == SMK_FIXED24_FMT) { |
| 1843 | if (count < SMK_LOADLEN) |
| 1844 | return -EINVAL; |
| 1845 | res = smk_parse_rule(data, &rule, 0); |
| 1846 | } else { |
| 1847 | /* |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 1848 | * simple_transaction_get() returns null-terminated data |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1849 | */ |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 1850 | res = smk_parse_long_rule(data, &rule, 0, 3); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
Rafal Krypa | 10289b0 | 2013-08-09 11:47:07 +0200 | [diff] [blame] | 1853 | if (res < 0) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1854 | return -EINVAL; |
| 1855 | |
Casey Schaufler | 2f823ff | 2013-05-22 18:43:03 -0700 | [diff] [blame] | 1856 | res = smk_access(rule.smk_subject, rule.smk_object, |
| 1857 | rule.smk_access1, NULL); |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1858 | data[0] = res == 0 ? '1' : '0'; |
| 1859 | data[1] = '\0'; |
| 1860 | |
| 1861 | simple_transaction_set(file, 2); |
| 1862 | |
| 1863 | if (format == SMK_FIXED24_FMT) |
| 1864 | return SMK_LOADLEN; |
| 1865 | return count; |
| 1866 | } |
| 1867 | |
| 1868 | /** |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 1869 | * smk_write_access - handle access check transaction |
| 1870 | * @file: file pointer |
| 1871 | * @buf: data from user space |
| 1872 | * @count: bytes sent |
| 1873 | * @ppos: where to start - must be 0 |
| 1874 | */ |
| 1875 | static ssize_t smk_write_access(struct file *file, const char __user *buf, |
| 1876 | size_t count, loff_t *ppos) |
| 1877 | { |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1878 | return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT); |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static const struct file_operations smk_access_ops = { |
| 1882 | .write = smk_write_access, |
| 1883 | .read = simple_transaction_read, |
| 1884 | .release = simple_transaction_release, |
| 1885 | .llseek = generic_file_llseek, |
| 1886 | }; |
| 1887 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1888 | |
| 1889 | /* |
| 1890 | * Seq_file read operations for /smack/load2 |
| 1891 | */ |
| 1892 | |
| 1893 | static int load2_seq_show(struct seq_file *s, void *v) |
| 1894 | { |
| 1895 | struct list_head *list = v; |
| 1896 | struct smack_master_list *smlp = |
| 1897 | list_entry(list, struct smack_master_list, list); |
| 1898 | |
| 1899 | smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL); |
| 1900 | |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
| 1904 | static const struct seq_operations load2_seq_ops = { |
| 1905 | .start = load2_seq_start, |
| 1906 | .next = load2_seq_next, |
| 1907 | .show = load2_seq_show, |
| 1908 | .stop = smk_seq_stop, |
| 1909 | }; |
| 1910 | |
| 1911 | /** |
| 1912 | * smk_open_load2 - open() for /smack/load2 |
| 1913 | * @inode: inode structure representing file |
| 1914 | * @file: "load2" file pointer |
| 1915 | * |
| 1916 | * For reading, use load2_seq_* seq_file reading operations. |
| 1917 | */ |
| 1918 | static int smk_open_load2(struct inode *inode, struct file *file) |
| 1919 | { |
| 1920 | return seq_open(file, &load2_seq_ops); |
| 1921 | } |
| 1922 | |
| 1923 | /** |
| 1924 | * smk_write_load2 - write() for /smack/load2 |
| 1925 | * @file: file pointer, not actually used |
| 1926 | * @buf: where to get the data from |
| 1927 | * @count: bytes sent |
| 1928 | * @ppos: where to start - must be 0 |
| 1929 | * |
| 1930 | */ |
| 1931 | static ssize_t smk_write_load2(struct file *file, const char __user *buf, |
| 1932 | size_t count, loff_t *ppos) |
| 1933 | { |
| 1934 | /* |
| 1935 | * Must have privilege. |
| 1936 | */ |
Casey Schaufler | 1880eff | 2012-06-05 15:28:30 -0700 | [diff] [blame] | 1937 | if (!smack_privileged(CAP_MAC_ADMIN)) |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 1938 | return -EPERM; |
| 1939 | |
| 1940 | return smk_write_rules_list(file, buf, count, ppos, NULL, NULL, |
| 1941 | SMK_LONG_FMT); |
| 1942 | } |
| 1943 | |
| 1944 | static const struct file_operations smk_load2_ops = { |
| 1945 | .open = smk_open_load2, |
| 1946 | .read = seq_read, |
| 1947 | .llseek = seq_lseek, |
| 1948 | .write = smk_write_load2, |
| 1949 | .release = seq_release, |
| 1950 | }; |
| 1951 | |
| 1952 | /* |
| 1953 | * Seq_file read operations for /smack/load-self2 |
| 1954 | */ |
| 1955 | |
| 1956 | static void *load_self2_seq_start(struct seq_file *s, loff_t *pos) |
| 1957 | { |
| 1958 | struct task_smack *tsp = current_security(); |
| 1959 | |
| 1960 | return smk_seq_start(s, pos, &tsp->smk_rules); |
| 1961 | } |
| 1962 | |
| 1963 | static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos) |
| 1964 | { |
| 1965 | struct task_smack *tsp = current_security(); |
| 1966 | |
| 1967 | return smk_seq_next(s, v, pos, &tsp->smk_rules); |
| 1968 | } |
| 1969 | |
| 1970 | static int load_self2_seq_show(struct seq_file *s, void *v) |
| 1971 | { |
| 1972 | struct list_head *list = v; |
| 1973 | struct smack_rule *srp = |
| 1974 | list_entry(list, struct smack_rule, list); |
| 1975 | |
| 1976 | smk_rule_show(s, srp, SMK_LONGLABEL); |
| 1977 | |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | static const struct seq_operations load_self2_seq_ops = { |
| 1982 | .start = load_self2_seq_start, |
| 1983 | .next = load_self2_seq_next, |
| 1984 | .show = load_self2_seq_show, |
| 1985 | .stop = smk_seq_stop, |
| 1986 | }; |
| 1987 | |
| 1988 | /** |
| 1989 | * smk_open_load_self2 - open() for /smack/load-self2 |
| 1990 | * @inode: inode structure representing file |
| 1991 | * @file: "load" file pointer |
| 1992 | * |
| 1993 | * For reading, use load_seq_* seq_file reading operations. |
| 1994 | */ |
| 1995 | static int smk_open_load_self2(struct inode *inode, struct file *file) |
| 1996 | { |
| 1997 | return seq_open(file, &load_self2_seq_ops); |
| 1998 | } |
| 1999 | |
| 2000 | /** |
| 2001 | * smk_write_load_self2 - write() for /smack/load-self2 |
| 2002 | * @file: file pointer, not actually used |
| 2003 | * @buf: where to get the data from |
| 2004 | * @count: bytes sent |
| 2005 | * @ppos: where to start - must be 0 |
| 2006 | * |
| 2007 | */ |
| 2008 | static ssize_t smk_write_load_self2(struct file *file, const char __user *buf, |
| 2009 | size_t count, loff_t *ppos) |
| 2010 | { |
| 2011 | struct task_smack *tsp = current_security(); |
| 2012 | |
| 2013 | return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules, |
| 2014 | &tsp->smk_rules_lock, SMK_LONG_FMT); |
| 2015 | } |
| 2016 | |
| 2017 | static const struct file_operations smk_load_self2_ops = { |
| 2018 | .open = smk_open_load_self2, |
| 2019 | .read = seq_read, |
| 2020 | .llseek = seq_lseek, |
| 2021 | .write = smk_write_load_self2, |
| 2022 | .release = seq_release, |
| 2023 | }; |
| 2024 | |
| 2025 | /** |
| 2026 | * smk_write_access2 - handle access check transaction |
| 2027 | * @file: file pointer |
| 2028 | * @buf: data from user space |
| 2029 | * @count: bytes sent |
| 2030 | * @ppos: where to start - must be 0 |
| 2031 | */ |
| 2032 | static ssize_t smk_write_access2(struct file *file, const char __user *buf, |
| 2033 | size_t count, loff_t *ppos) |
| 2034 | { |
| 2035 | return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT); |
| 2036 | } |
| 2037 | |
| 2038 | static const struct file_operations smk_access2_ops = { |
| 2039 | .write = smk_write_access2, |
| 2040 | .read = simple_transaction_read, |
| 2041 | .release = simple_transaction_release, |
| 2042 | .llseek = generic_file_llseek, |
| 2043 | }; |
| 2044 | |
Etienne Basset | ecfcc53 | 2009-04-08 20:40:06 +0200 | [diff] [blame] | 2045 | /** |
Rafal Krypa | 449543b | 2012-07-11 17:49:30 +0200 | [diff] [blame] | 2046 | * smk_write_revoke_subj - write() for /smack/revoke-subject |
| 2047 | * @file: file pointer |
| 2048 | * @buf: data from user space |
| 2049 | * @count: bytes sent |
| 2050 | * @ppos: where to start - must be 0 |
| 2051 | */ |
| 2052 | static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf, |
| 2053 | size_t count, loff_t *ppos) |
| 2054 | { |
| 2055 | char *data = NULL; |
| 2056 | const char *cp = NULL; |
| 2057 | struct smack_known *skp; |
| 2058 | struct smack_rule *sp; |
| 2059 | struct list_head *rule_list; |
| 2060 | struct mutex *rule_lock; |
| 2061 | int rc = count; |
| 2062 | |
| 2063 | if (*ppos != 0) |
| 2064 | return -EINVAL; |
| 2065 | |
| 2066 | if (!smack_privileged(CAP_MAC_ADMIN)) |
| 2067 | return -EPERM; |
| 2068 | |
| 2069 | if (count == 0 || count > SMK_LONGLABEL) |
| 2070 | return -EINVAL; |
| 2071 | |
| 2072 | data = kzalloc(count, GFP_KERNEL); |
| 2073 | if (data == NULL) |
| 2074 | return -ENOMEM; |
| 2075 | |
| 2076 | if (copy_from_user(data, buf, count) != 0) { |
| 2077 | rc = -EFAULT; |
| 2078 | goto free_out; |
| 2079 | } |
| 2080 | |
| 2081 | cp = smk_parse_smack(data, count); |
| 2082 | if (cp == NULL) { |
| 2083 | rc = -EINVAL; |
| 2084 | goto free_out; |
| 2085 | } |
| 2086 | |
| 2087 | skp = smk_find_entry(cp); |
Rafal Krypa | d15d9fad | 2012-11-27 16:28:11 +0100 | [diff] [blame] | 2088 | if (skp == NULL) |
Rafal Krypa | 449543b | 2012-07-11 17:49:30 +0200 | [diff] [blame] | 2089 | goto free_out; |
Rafal Krypa | 449543b | 2012-07-11 17:49:30 +0200 | [diff] [blame] | 2090 | |
| 2091 | rule_list = &skp->smk_rules; |
| 2092 | rule_lock = &skp->smk_rules_lock; |
| 2093 | |
| 2094 | mutex_lock(rule_lock); |
| 2095 | |
| 2096 | list_for_each_entry_rcu(sp, rule_list, list) |
| 2097 | sp->smk_access = 0; |
| 2098 | |
| 2099 | mutex_unlock(rule_lock); |
| 2100 | |
| 2101 | free_out: |
| 2102 | kfree(data); |
| 2103 | kfree(cp); |
| 2104 | return rc; |
| 2105 | } |
| 2106 | |
| 2107 | static const struct file_operations smk_revoke_subj_ops = { |
| 2108 | .write = smk_write_revoke_subj, |
| 2109 | .read = simple_transaction_read, |
| 2110 | .release = simple_transaction_release, |
| 2111 | .llseek = generic_file_llseek, |
| 2112 | }; |
| 2113 | |
Casey Schaufler | e930723 | 2012-11-01 18:14:32 -0700 | [diff] [blame] | 2114 | static struct kset *smackfs_kset; |
| 2115 | /** |
| 2116 | * smk_init_sysfs - initialize /sys/fs/smackfs |
| 2117 | * |
| 2118 | */ |
| 2119 | static int smk_init_sysfs(void) |
| 2120 | { |
| 2121 | smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj); |
| 2122 | if (!smackfs_kset) |
| 2123 | return -ENOMEM; |
| 2124 | return 0; |
| 2125 | } |
| 2126 | |
Rafal Krypa | 449543b | 2012-07-11 17:49:30 +0200 | [diff] [blame] | 2127 | /** |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 2128 | * smk_write_change_rule - write() for /smack/change-rule |
| 2129 | * @file: file pointer |
| 2130 | * @buf: data from user space |
| 2131 | * @count: bytes sent |
| 2132 | * @ppos: where to start - must be 0 |
| 2133 | */ |
| 2134 | static ssize_t smk_write_change_rule(struct file *file, const char __user *buf, |
| 2135 | size_t count, loff_t *ppos) |
| 2136 | { |
| 2137 | /* |
| 2138 | * Must have privilege. |
| 2139 | */ |
| 2140 | if (!capable(CAP_MAC_ADMIN)) |
| 2141 | return -EPERM; |
| 2142 | |
| 2143 | return smk_write_rules_list(file, buf, count, ppos, NULL, NULL, |
| 2144 | SMK_CHANGE_FMT); |
| 2145 | } |
| 2146 | |
| 2147 | static const struct file_operations smk_change_rule_ops = { |
| 2148 | .write = smk_write_change_rule, |
| 2149 | .read = simple_transaction_read, |
| 2150 | .release = simple_transaction_release, |
| 2151 | .llseek = generic_file_llseek, |
| 2152 | }; |
| 2153 | |
| 2154 | /** |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2155 | * smk_fill_super - fill the /smackfs superblock |
| 2156 | * @sb: the empty superblock |
| 2157 | * @data: unused |
| 2158 | * @silent: unused |
| 2159 | * |
| 2160 | * Fill in the well known entries for /smack |
| 2161 | * |
| 2162 | * Returns 0 on success, an error code on failure |
| 2163 | */ |
| 2164 | static int smk_fill_super(struct super_block *sb, void *data, int silent) |
| 2165 | { |
| 2166 | int rc; |
| 2167 | struct inode *root_inode; |
| 2168 | |
| 2169 | static struct tree_descr smack_files[] = { |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 2170 | [SMK_LOAD] = { |
| 2171 | "load", &smk_load_ops, S_IRUGO|S_IWUSR}, |
| 2172 | [SMK_CIPSO] = { |
| 2173 | "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR}, |
| 2174 | [SMK_DOI] = { |
| 2175 | "doi", &smk_doi_ops, S_IRUGO|S_IWUSR}, |
| 2176 | [SMK_DIRECT] = { |
| 2177 | "direct", &smk_direct_ops, S_IRUGO|S_IWUSR}, |
| 2178 | [SMK_AMBIENT] = { |
| 2179 | "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR}, |
| 2180 | [SMK_NETLBLADDR] = { |
| 2181 | "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR}, |
| 2182 | [SMK_ONLYCAP] = { |
| 2183 | "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR}, |
| 2184 | [SMK_LOGGING] = { |
| 2185 | "logging", &smk_logging_ops, S_IRUGO|S_IWUSR}, |
| 2186 | [SMK_LOAD_SELF] = { |
| 2187 | "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO}, |
Jarkko Sakkinen | 828716c | 2011-09-08 10:12:01 +0300 | [diff] [blame] | 2188 | [SMK_ACCESSES] = { |
Jarkko Sakkinen | 0e94ae1 | 2011-10-18 21:21:36 +0300 | [diff] [blame] | 2189 | "access", &smk_access_ops, S_IRUGO|S_IWUGO}, |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2190 | [SMK_MAPPED] = { |
| 2191 | "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR}, |
| 2192 | [SMK_LOAD2] = { |
| 2193 | "load2", &smk_load2_ops, S_IRUGO|S_IWUSR}, |
| 2194 | [SMK_LOAD_SELF2] = { |
| 2195 | "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO}, |
| 2196 | [SMK_ACCESS2] = { |
| 2197 | "access2", &smk_access2_ops, S_IRUGO|S_IWUGO}, |
| 2198 | [SMK_CIPSO2] = { |
| 2199 | "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR}, |
Rafal Krypa | 449543b | 2012-07-11 17:49:30 +0200 | [diff] [blame] | 2200 | [SMK_REVOKE_SUBJ] = { |
| 2201 | "revoke-subject", &smk_revoke_subj_ops, |
| 2202 | S_IRUGO|S_IWUSR}, |
Rafal Krypa | e05b6f9 | 2013-01-10 19:42:00 +0100 | [diff] [blame] | 2203 | [SMK_CHANGE_RULE] = { |
| 2204 | "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR}, |
Casey Schaufler | 7898e1f | 2011-01-17 08:05:27 -0800 | [diff] [blame] | 2205 | /* last one */ |
| 2206 | {""} |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2207 | }; |
| 2208 | |
| 2209 | rc = simple_fill_super(sb, SMACK_MAGIC, smack_files); |
| 2210 | if (rc != 0) { |
| 2211 | printk(KERN_ERR "%s failed %d while creating inodes\n", |
| 2212 | __func__, rc); |
| 2213 | return rc; |
| 2214 | } |
| 2215 | |
| 2216 | root_inode = sb->s_root->d_inode; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2217 | |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
| 2221 | /** |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 2222 | * smk_mount - get the smackfs superblock |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2223 | * @fs_type: passed along without comment |
| 2224 | * @flags: passed along without comment |
| 2225 | * @dev_name: passed along without comment |
| 2226 | * @data: passed along without comment |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2227 | * |
| 2228 | * Just passes everything along. |
| 2229 | * |
| 2230 | * Returns what the lower level code does. |
| 2231 | */ |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 2232 | static struct dentry *smk_mount(struct file_system_type *fs_type, |
| 2233 | int flags, const char *dev_name, void *data) |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2234 | { |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 2235 | return mount_single(fs_type, flags, data, smk_fill_super); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | static struct file_system_type smk_fs_type = { |
| 2239 | .name = "smackfs", |
Al Viro | fc14f2f | 2010-07-25 01:48:30 +0400 | [diff] [blame] | 2240 | .mount = smk_mount, |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2241 | .kill_sb = kill_litter_super, |
| 2242 | }; |
| 2243 | |
| 2244 | static struct vfsmount *smackfs_mount; |
| 2245 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2246 | static int __init smk_preset_netlabel(struct smack_known *skp) |
| 2247 | { |
| 2248 | skp->smk_netlabel.domain = skp->smk_known; |
| 2249 | skp->smk_netlabel.flags = |
| 2250 | NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL; |
| 2251 | return smk_netlbl_mls(smack_cipso_direct, skp->smk_known, |
| 2252 | &skp->smk_netlabel, strlen(skp->smk_known)); |
| 2253 | } |
| 2254 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2255 | /** |
| 2256 | * init_smk_fs - get the smackfs superblock |
| 2257 | * |
| 2258 | * register the smackfs |
| 2259 | * |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 2260 | * Do not register smackfs if Smack wasn't enabled |
| 2261 | * on boot. We can not put this method normally under the |
| 2262 | * smack_init() code path since the security subsystem get |
| 2263 | * initialized before the vfs caches. |
| 2264 | * |
| 2265 | * Returns true if we were not chosen on boot or if |
| 2266 | * we were chosen and filesystem registration succeeded. |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2267 | */ |
| 2268 | static int __init init_smk_fs(void) |
| 2269 | { |
| 2270 | int err; |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2271 | int rc; |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2272 | |
Ahmed S. Darwish | 076c54c | 2008-03-06 18:09:10 +0200 | [diff] [blame] | 2273 | if (!security_module_enable(&smack_ops)) |
| 2274 | return 0; |
| 2275 | |
Casey Schaufler | e930723 | 2012-11-01 18:14:32 -0700 | [diff] [blame] | 2276 | err = smk_init_sysfs(); |
| 2277 | if (err) |
| 2278 | printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n"); |
| 2279 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2280 | err = register_filesystem(&smk_fs_type); |
| 2281 | if (!err) { |
| 2282 | smackfs_mount = kern_mount(&smk_fs_type); |
| 2283 | if (IS_ERR(smackfs_mount)) { |
| 2284 | printk(KERN_ERR "smackfs: could not mount!\n"); |
| 2285 | err = PTR_ERR(smackfs_mount); |
| 2286 | smackfs_mount = NULL; |
| 2287 | } |
| 2288 | } |
| 2289 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2290 | smk_cipso_doi(); |
Casey Schaufler | 4bc87e6 | 2008-02-15 15:24:25 -0800 | [diff] [blame] | 2291 | smk_unlbl_ambient(NULL); |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2292 | |
Casey Schaufler | f7112e6 | 2012-05-06 15:22:02 -0700 | [diff] [blame] | 2293 | rc = smk_preset_netlabel(&smack_known_floor); |
| 2294 | if (err == 0 && rc < 0) |
| 2295 | err = rc; |
| 2296 | rc = smk_preset_netlabel(&smack_known_hat); |
| 2297 | if (err == 0 && rc < 0) |
| 2298 | err = rc; |
| 2299 | rc = smk_preset_netlabel(&smack_known_huh); |
| 2300 | if (err == 0 && rc < 0) |
| 2301 | err = rc; |
| 2302 | rc = smk_preset_netlabel(&smack_known_invalid); |
| 2303 | if (err == 0 && rc < 0) |
| 2304 | err = rc; |
| 2305 | rc = smk_preset_netlabel(&smack_known_star); |
| 2306 | if (err == 0 && rc < 0) |
| 2307 | err = rc; |
| 2308 | rc = smk_preset_netlabel(&smack_known_web); |
| 2309 | if (err == 0 && rc < 0) |
| 2310 | err = rc; |
| 2311 | |
Casey Schaufler | e114e47 | 2008-02-04 22:29:50 -0800 | [diff] [blame] | 2312 | return err; |
| 2313 | } |
| 2314 | |
| 2315 | __initcall(init_smk_fs); |