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