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