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