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