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