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