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