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