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