aboutsummaryrefslogtreecommitdiffstats
path: root/scripts
Commit message (Expand)AuthorAgeFilesLines
...
* build: add script to sign packagesPaul Spooren2019-10-211-0/+27
* build: create JSON files containing image infoPaul Spooren2019-09-291-0/+55
* build: fix xconfig targetleo chung2019-09-251-1/+1
* build: fix host menu config targets using ncursesPetr Štetiar2019-09-251-2/+0
* build: make device tree arg really optional in mkits.shJohann Neuhauser2019-09-191-5/+4
* download.dl: fix @KERNEL mirror urlsYousong Zhou2019-09-161-4/+4
* scripts/feeds: fix accepting "-" in feed type stringFelix Fietkau2019-09-151-1/+1
* images: fix boot failures on NAND with small sub pagesJonas Gorski2019-09-143-16/+60
* scripts/feeds: fix 'src-include' directiveBjørn Mork2019-09-021-1/+1
* Fix handling of BUILD_SUFFIX in remote-gdb scriptThomas Langer2019-09-011-4/+4
* scripts/feeds: allow adding parameters to feedsJo-Philipp Wich2019-08-281-18/+30
* scripts/dl_github_archive.py: fix python3 transitionYousong Zhou2019-08-201-4/+5
* download.pl: use https://source.openwrt.orgYousong Zhou2019-08-181-2/+1
* build: allow overriding default selection state for devicesJonas Gorski2019-08-132-2/+4
* scripts/ubinize-image.sh: fix buildbot breakagePetr Štetiar2019-07-301-1/+1
* scripts/jungo-image: convert to Python 3 with 2-to-3Petr Štetiar2019-07-261-26/+26
* scripts/dl_github_archive.py: convert to Python 3 with 2-to-3Petr Štetiar2019-07-261-5/+5
* scripts: time.pl: Don't print the time on stderrAlban Bedel2019-07-031-1/+1
* scripts/feeds: add src-include methodBjørn Mork2019-07-011-11/+26
* metadata: handle ABI version rebuild tracking for transient dependenciesJo-Philipp Wich2019-06-211-0/+12
* scripts/config: fix *c_shipped build depency trackingJonas Gorski2019-06-201-4/+4
* metadata: ensure one dependency provider to be y if a package is yJonas Gorski2019-06-201-2/+2
* config: fix relational operators for bool and tristate symbolsNicolas Pitre2019-06-201-1/+4
* config: regenerate *_shipped sourcesJonas Gorski2019-06-202-243/+302
* toolchain: Add GCC 9.1.0 releaseJoseph Benden2019-06-161-1/+1
* build: add support to && in DEPENDSEneas U de Queiroz2019-05-311-2/+18
* build: fix STAGING_DIR cleaning when filenames contain spacesJeffery To2019-05-171-0/+1
* build: add a config option for enabling a testing version of the target kernelFelix Fietkau2019-05-112-5/+13
* scripts/qemustart: add notes on adding new optionsYousong Zhou2019-04-261-0/+5
* scripts/qemustart: fix network device emulationYousong Zhou2019-04-261-11/+17
* scripts/qemustart: no network by default and enable with -nYousong Zhou2019-04-261-5/+7
* scripts/qemustart: fix usage with networking supportHans Dedecker2019-04-241-4/+4
* build: switch default target from ar71xx to ath79Kevin Darbyshire-Bryant2019-04-111-1/+1
* scripts/qemustart: Allow specifying custom rootfs for maltaPetr Štetiar2019-04-081-0/+11
* scripts/qemustart: Allow specifying custom kernel for x86Petr Štetiar2019-04-081-1/+9
* scripts/qemustart: Allow usage without networkingPetr Štetiar2019-04-081-10/+31
* IB: fix generating .profile.mk for profiles without metadataDaniel Golle2019-03-271-1/+1
* scripts/gen_image_generic.sh: fail on errorsStijn Tintel2019-03-271-1/+1
* IB: include SUPPORTED_DEVICES in 'make info' outputDaniel Golle2019-03-112-0/+5
* ib: display whether profile comes with image metadataDaniel Golle2019-03-062-0/+3
* scripts: make eva_ramboot.py offset configurableDavid Bauer2019-02-281-11/+19
* build: add helpers for generating QSDK sysupgrade compatible imagesPiotr Dymacz2019-02-251-0/+59
* scripts/qemustart: allow machine selection with new option --machineYousong Zhou2019-02-221-6/+12
* scripts: ipkg-make-index.sh: dereference symbolic linksJo-Philipp Wich2019-01-311-1/+1
* build: fix STAGING_DIR cleaning for packagesJeffery To2019-01-301-1/+1
* build: add a script for generating Linksys factory imagesOever González2019-01-261-0/+67
* build: extend ABI_VERSION suffixing to providesJo-Philipp Wich2019-01-241-4/+10
* scripts: ipkg-remove: handle existing .ipk files without SourceName fieldJo-Philipp Wich2019-01-211-0/+5
* build: add ABI_VERSION to binary package namesJo-Philipp Wich2019-01-191-0/+15
* build: expose ABI version in .packageauxvarsJo-Philipp Wich2019-01-192-0/+10
8'>598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
/*
 * Implementation of the kernel access vector cache (AVC).
 *
 * Authors:  Stephen Smalley, <sds@epoch.ncsc.mil>
 *           James Morris <jmorris@redhat.com>
 *
 * Update:   KaiGai, Kohei <kaigai@ak.jp.nec.com>
 *     Replaced the avc_lock spinlock by RCU.
 *
 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License version 2,
 *      as published by the Free Software Foundation.
 */
 
/* Ported to Xen 3.0, George Coker, <gscoker@alpha.ncsc.mil> */
 
#include <xen/lib.h>
#include <xen/xmalloc.h>
#include <xen/types.h>
#include <xen/list.h>
#include <xen/spinlock.h>
#include <xen/prefetch.h>
#include <xen/kernel.h>
#include <xen/sched.h>
#include <xen/init.h>
#include <xen/rcupdate.h>
#include <asm/atomic.h>
#include <asm/current.h>

#include "avc.h"
#include "avc_ss.h"

static const struct av_perm_to_string av_perm_to_string[] = {
#define S_(c, v, s) { c, v, s },
#include "av_perm_to_string.h"
#undef S_
};

static const char *class_to_string[] = {
#define S_(s) s,
#include "class_to_string.h"
#undef S_
};

#define TB_(s) static const char * s [] = {
#define TE_(s) };
#define S_(s) s,
#include "common_perm_to_string.h"
#undef TB_
#undef TE_
#undef S_

static const struct av_inherit av_inherit[] = {
#define S_(c, i, b) { .tclass = c, .common_pts = common_##i##_perm_to_string, \
		      .common_base = b },
#include "av_inherit.h"
#undef S_
};

const struct selinux_class_perm selinux_class_perm = {
	.av_perm_to_string = av_perm_to_string,
	.av_pts_len = ARRAY_SIZE(av_perm_to_string),
	.class_to_string = class_to_string,
	.cts_len = ARRAY_SIZE(class_to_string),
	.av_inherit = av_inherit,
	.av_inherit_len = ARRAY_SIZE(av_inherit)
};

#define AVC_CACHE_SLOTS            512
#define AVC_DEF_CACHE_THRESHOLD        512
#define AVC_CACHE_RECLAIM        16

#ifdef FLASK_AVC_STATS
#define avc_cache_stats_incr(field)                 \
do {                                \
    __get_cpu_var(avc_cache_stats).field++;        \
} while (0)
#else
#define avc_cache_stats_incr(field)    do {} while (0)
#endif

struct avc_entry {
    u32            ssid;
    u32            tsid;
    u16            tclass;
    struct av_decision    avd;
};

struct avc_node {
    struct avc_entry    ae;
    struct hlist_node   list; /* anchored in avc_cache->slots[i] */
    struct rcu_head     rhead;
};

struct avc_cache {
    struct hlist_head    slots[AVC_CACHE_SLOTS]; /* head for avc_node->list */
    spinlock_t        slots_lock[AVC_CACHE_SLOTS]; /* lock for writes */
    atomic_t        lru_hint;    /* LRU hint for reclaim scan */
    atomic_t        active_nodes;
    u32            latest_notif;    /* latest revocation notification */
};

struct avc_callback_node {
    int (*callback) (u32 event, u32 ssid, u32 tsid,
                     u16 tclass, u32 perms,
                     u32 *out_retained);
    u32 events;
    u32 ssid;
    u32 tsid;
    u16 tclass;
    u32 perms;
    struct avc_callback_node *next;
};

/* Exported via Flask hypercall */
unsigned int avc_cache_threshold = AVC_DEF_CACHE_THRESHOLD;

#ifdef FLASK_AVC_STATS
DEFINE_PER_CPU(struct avc_cache_stats, avc_cache_stats) = { 0 };
#endif

static struct avc_cache avc_cache;
static struct avc_callback_node *avc_callbacks;

static inline int avc_hash(u32 ssid, u32 tsid, u16 tclass)
{
    return (ssid ^ (tsid<<2) ^ (tclass<<4)) & (AVC_CACHE_SLOTS - 1);
}

/**
 * avc_dump_av - Display an access vector in human-readable form.
 * @tclass: target security class
 * @av: access vector
 */
static void avc_dump_av(u16 tclass, u32 av)
{
    const char **common_pts = NULL;
    u32 common_base = 0;
    int i, i2, perm;

    if ( av == 0 )
    {
        printk(" null");
        return;
    }

    for ( i = 0; i < ARRAY_SIZE(av_inherit); i++ )
    {
        if (av_inherit[i].tclass == tclass)
        {
            common_pts = av_inherit[i].common_pts;
            common_base = av_inherit[i].common_base;
            break;
        }
    }

    printk(" {");
    i = 0;
    perm = 1;
    while ( perm < common_base )
    {
        if (perm & av)
        {
            printk(" %s", common_pts[i]);
            av &= ~perm;
        }
        i++;
        perm <<= 1;
    }

    while ( i < sizeof(av) * 8 )
    {
        if ( perm & av )
        {
            for ( i2 = 0; i2 < ARRAY_SIZE(av_perm_to_string); i2++ )
            {
                if ( (av_perm_to_string[i2].tclass == tclass) &&
                    (av_perm_to_string[i2].value == perm) )
                    break;
            }
            if ( i2 < ARRAY_SIZE(av_perm_to_string) )
            {
                printk(" %s", av_perm_to_string[i2].name);
                av &= ~perm;
            }
        }
        i++;
        perm <<= 1;
    }

    if ( av )
        printk(" 0x%x", av);

    printk(" }");
}

/**
 * avc_dump_query - Display a SID pair and a class in human-readable form.
 * @ssid: source security identifier
 * @tsid: target security identifier
 * @tclass: target security class
 */
static void avc_dump_query(u32 ssid, u32 tsid, u16 tclass)
{
    int rc;
    char *scontext;
    u32 scontext_len;

    rc = security_sid_to_context(ssid, &scontext, &scontext_len);
    if ( rc )
        printk("ssid=%d", ssid);
    else
    {
        printk("scontext=%s", scontext);
        xfree(scontext);
    }

    rc = security_sid_to_context(tsid, &scontext, &scontext_len);
    if ( rc )
        printk(" tsid=%d", tsid);
    else
    {
        printk(" tcontext=%s", scontext);
        xfree(scontext);
    }

    printk(" tclass=%s", class_to_string[tclass]);
}

/**
 * avc_init - Initialize the AVC.
 *
 * Initialize the access vector cache.
 */
void __init avc_init(void)
{
    int i;

    for ( i = 0; i < AVC_CACHE_SLOTS; i++ )
    {
        INIT_HLIST_HEAD(&avc_cache.slots[i]);
        spin_lock_init(&avc_cache.slots_lock[i]);
    }
    atomic_set(&avc_cache.active_nodes, 0);
    atomic_set(&avc_cache.lru_hint, 0);

    printk("AVC INITIALIZED\n");
}

int avc_get_hash_stats(char *buf, uint32_t size)
{
    int i, chain_len, max_chain_len, slots_used;
    struct avc_node *node;
    struct hlist_head *head;

    rcu_read_lock();

    slots_used = 0;
    max_chain_len = 0;
    for ( i = 0; i < AVC_CACHE_SLOTS; i++ )
    {
        head = &avc_cache.slots[i];
        if ( !hlist_empty(head) )
        {
	    struct hlist_node *next;

            slots_used++;
            chain_len = 0;
            hlist_for_each_entry_rcu(node, next, head, list)
                chain_len++;
            if ( chain_len > max_chain_len )
                max_chain_len = chain_len;
        }
    }

    rcu_read_unlock();
    
    return snprintf(buf, size, "entries: %d\nbuckets used: %d/%d\n"
                                "longest chain: %d\n",
                                atomic_read(&avc_cache.active_nodes),
                                slots_used, AVC_CACHE_SLOTS, max_chain_len);
}

static void avc_node_free(struct rcu_head *rhead)
{
    struct avc_node *node = container_of(rhead, struct avc_node, rhead);
    xfree(node);
    avc_cache_stats_incr(frees);
}

static void avc_node_delete(struct avc_node *node)
{
    hlist_del_rcu(&node->list);
    call_rcu(&node->rhead, avc_node_free);
    atomic_dec(&avc_cache.active_nodes);
}

static void avc_node_kill(struct avc_node *node)
{
    xfree(node);
    avc_cache_stats_incr(frees);
    atomic_dec(&avc_cache.active_nodes);
}

static void avc_node_replace(struct avc_node *new, struct avc_node *old)
{
    hlist_replace_rcu(&old->list, &new->list);
    call_rcu(&old->rhead, avc_node_free);
    atomic_dec(&avc_cache.active_nodes);
}

static inline int avc_reclaim_node(void)
{
    struct avc_node *node;
    int hvalue, try, ecx;
    unsigned long flags;
    struct hlist_head *head;
    struct hlist_node *next;
    spinlock_t *lock;

    for ( try = 0, ecx = 0; try < AVC_CACHE_SLOTS; try++ )
    {
        atomic_inc(&avc_cache.lru_hint);
        hvalue =  atomic_read(&avc_cache.lru_hint) & (AVC_CACHE_SLOTS - 1);
        head = &avc_cache.slots[hvalue];
        lock = &avc_cache.slots_lock[hvalue];

        spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flags);
        rcu_read_lock();
        hlist_for_each_entry(node, next, head, list)
        {
                avc_node_delete(node);
                avc_cache_stats_incr(reclaims);
                ecx++;
                if ( ecx >= AVC_CACHE_RECLAIM )
                {
		  rcu_read_unlock();
		  spin_unlock_irqrestore(lock, flags);
		  goto out;
                }
        }
        rcu_read_unlock();
        spin_unlock_irqrestore(lock, flags);
    }    
out:
    return ecx;
}

static struct avc_node *avc_alloc_node(void)
{
    struct avc_node *node;

    node = xmalloc(struct avc_node);
    if (!node)
        goto out;

    memset(node, 0, sizeof(*node));
    INIT_RCU_HEAD(&node->rhead);
    INIT_HLIST_NODE(&node->list);
    avc_cache_stats_incr(allocations);

    atomic_inc(&avc_cache.active_nodes);
    if ( atomic_read(&avc_cache.active_nodes) > avc_cache_threshold )
        avc_reclaim_node();

out:
    return node;
}

static void avc_node_populate(struct avc_node *node, u32 ssid, u32 tsid,
                              u16 tclass, struct av_decision *avd)
{
    node->ae.ssid = ssid;
    node->ae.tsid = tsid;
    node->ae.tclass = tclass;
    memcpy(&node->ae.avd, avd, sizeof(node->ae.avd));
}

static inline struct avc_node *avc_search_node(u32 ssid, u32 tsid, u16 tclass)
{
    struct avc_node *node, *ret = NULL;
    int hvalue;
    struct hlist_head *head;
    struct hlist_node *next;

    hvalue = avc_hash(ssid, tsid, tclass);
    head = &avc_cache.slots[hvalue];
    hlist_for_each_entry_rcu(node, next, head, list)
    {
        if ( ssid == node->ae.ssid &&
	     tclass == node->ae.tclass &&
	     tsid == node->ae.tsid )
        {
            ret = node;
            break;
        }
    }

    return ret;
}

/**
 * avc_lookup - Look up an AVC entry.
 * @ssid: source security identifier
 * @tsid: target security identifier
 * @tclass: target security class
 * @requested: requested permissions, interpreted based on @tclass
 *
 * Look up an AVC entry that is valid for the
 * @requested permissions between the SID pair
 * (@ssid, @tsid), interpreting the permissions
 * based on @tclass.  If a valid AVC entry exists,
 * then this function return the avc_node.
 * Otherwise, this function returns NULL.
 */
static struct avc_node *avc_lookup(u32 ssid, u32 tsid, u16 tclass)
{
    struct avc_node *node;

    avc_cache_stats_incr(lookups);
    node = avc_search_node(ssid, tsid, tclass);

    if ( node )
        avc_cache_stats_incr(hits);
    else
        avc_cache_stats_incr(misses);

    return node;
}

static int avc_latest_notif_update(int seqno, int is_insert)
{
    int ret = 0;
    static DEFINE_SPINLOCK(notif_lock);
    unsigned long flag;

    spin_lock_irqsave(&notif_lock, flag);
    if ( is_insert )
    {
        if ( seqno < avc_cache.latest_notif )
        {
            printk(KERN_WARNING "avc:  seqno %d < latest_notif %d\n",
                   seqno, avc_cache.latest_notif);
            ret = -EAGAIN;
        }
    }
    else
    {
        if ( seqno > avc_cache.latest_notif )
            avc_cache.latest_notif = seqno;
    }
    spin_unlock_irqrestore(&notif_lock, flag);

    return ret;
}

/**
 * avc_insert - Insert an AVC entry.
 * @ssid: source security identifier
 * @tsid: target security identifier
 * @tclass: target security class
 * @ae: AVC entry
 *
 * Insert an AVC entry for the SID pair
 * (@ssid, @tsid) and class @tclass.
 * The access vectors and the sequence number are
 * normally provided by the security server in
 * response to a security_compute_av() call.  If the
 * sequence number @ae->avd.seqno is not less than the latest
 * revocation notification, then the function copies
 * the access vectors into a cache entry, returns
 * avc_node inserted. Otherwise, this function returns NULL.
 */
static struct avc_node *avc_insert(u32 ssid, u32 tsid, u16 tclass,
                                   struct av_decision *avd)
{
    struct avc_node *pos, *node = NULL;
    int hvalue;
    unsigned long flag;

    if ( avc_latest_notif_update(avd->seqno, 1) )
        goto out;

    node = avc_alloc_node();
    if ( node )
    {
        struct hlist_head *head;
        struct hlist_node *next;
        spinlock_t *lock;

        hvalue = avc_hash(ssid, tsid, tclass);
        avc_node_populate(node, ssid, tsid, tclass, avd);

        head = &avc_cache.slots[hvalue];
        lock = &avc_cache.slots_lock[hvalue];

        spin_lock_irqsave(lock, flag);
        hlist_for_each_entry(pos, next, head, list)
        {
            if ( pos->ae.ssid == ssid &&
                 pos->ae.tsid == tsid &&
                 pos->ae.tclass == tclass )
            {
                avc_node_replace(node, pos);
                goto found;
            }
        }
        hlist_add_head_rcu(&node->list, head);
found:
        spin_unlock_irqrestore(lock, flag);
    }
out:
    return node;
}

/**
 * avc_audit - Audit the granting or denial of permissions.
 * @ssid: source security identifier
 * @tsid: target security identifier
 * @tclass: target security class
 * @requested: requested permissions
 * @avd: access vector decisions
 * @result: result from avc_has_perm_noaudit
 * @a:  auxiliary audit data
 *
 * Audit the granting or denial of permissions in accordance
 * with the policy.  This function is typically called by
 * avc_has_perm() after a permission check, but can also be
 * called directly by callers who use avc_has_perm_noaudit()
 * in order to separate the permission check from the auditing.
 * For example, this separation is useful when the permission check must
 * be performed under a lock, to allow the lock to be released
 * before calling the auditing code.
 */
void avc_audit(u32 ssid, u32 tsid, u16 tclass, u32 requested,
               struct av_decision *avd, int result, struct avc_audit_data *a)
{
    struct domain *d = current->domain;
    u32 denied, audited;

    denied = requested & ~avd->allowed;
    if ( denied )
    {
        audited = denied;
        if ( !(audited & avd->auditdeny) )
            return;
    }
    else if ( result )
    {
        audited = denied = requested;
    }
    else
    {
        audited = requested;
        if ( !(audited & avd->auditallow) )
            return;
    }

    printk("avc:  %s ", denied ? "denied" : "granted");
    avc_dump_av(tclass, audited);
    printk(" for ");

    if ( a && a->d )
        d = a->d;
    if ( d )
        printk("domid=%d ", d->domain_id);
    if ( a && a->device )
        printk("device=0x%lx ", a->device);

    avc_dump_query(ssid, tsid, tclass);
    printk("\n");
}

/**
 * avc_add_callback - Register a callback for security events.
 * @callback: callback function
 * @events: security events
 * @ssid: source security identifier or %SECSID_WILD
 * @tsid: target security identifier or %SECSID_WILD
 * @tclass: target security class
 * @perms: permissions
 *
 * Register a callback function for events in the set @events
 * related to the SID pair (@ssid, @tsid) and
 * and the permissions @perms, interpreting
 * @perms based on @tclass.  Returns %0 on success or
 * -%ENOMEM if insufficient memory exists to add the callback.
 */
int avc_add_callback(int (*callback)(u32 event, u32 ssid, u32 tsid, u16 tclass,
                u32 perms, u32 *out_retained), u32 events, u32 ssid, u32 tsid,
                                                        u16 tclass, u32 perms)
{
    struct avc_callback_node *c;
    int rc = 0;

    c = xmalloc(struct avc_callback_node);
    if ( !c )
    {
        rc = -ENOMEM;
        goto out;
    }

    c->callback = callback;
    c->events = events;
    c->ssid = ssid;
    c->tsid = tsid;
    c->perms = perms;
    c->next = avc_callbacks;
    avc_callbacks = c;
out:
    return rc;
}

static inline int avc_sidcmp(u32 x, u32 y)
{
    return (x == y || x == SECSID_WILD || y == SECSID_WILD);
}

/**
 * avc_update_node Update an AVC entry
 * @event : Updating event
 * @perms : Permission mask bits
 * @ssid,@tsid,@tclass : identifier of an AVC entry
 *
 * if a valid AVC entry doesn't exist,this function returns -ENOENT.
 * if kmalloc() called internal returns NULL, this function returns -ENOMEM.
 * otherwise, this function update the AVC entry. The original AVC-entry object
 * will release later by RCU.
 */
static int avc_update_node(u32 event, u32 perms, u32 ssid, u32 tsid, u16 tclass,
			   u32 seqno)
{
    int hvalue, rc = 0;
    unsigned long flag;
    struct avc_node *pos, *node, *orig = NULL;
    struct hlist_head *head;
    struct hlist_node *next;
    spinlock_t *lock;
    
    node = avc_alloc_node();
    if ( !node )
    {
        rc = -ENOMEM;
        goto out;
    }

    hvalue = avc_hash(ssid, tsid, tclass);    

    head = &avc_cache.slots[hvalue];
    lock = &avc_cache.slots_lock[hvalue];

    spin_lock_irqsave(lock, flag);

    hlist_for_each_entry(pos, next, head, list)
    {
        if ( ssid == pos->ae.ssid &&
	     tsid == pos->ae.tsid &&
	     tclass == pos->ae.tclass &&
	     seqno == pos->ae.avd.seqno )
        {
            orig = pos;
            break;
        }
    }

    if ( !orig )
    {
        rc = -ENOENT;
        avc_node_kill(node);
        goto out_unlock;
    }

    /*
     * Copy and replace original node.
     */

    avc_node_populate(node, ssid, tsid, tclass, &orig->ae.avd);

    switch ( event )
    {
    case AVC_CALLBACK_GRANT:
        node->ae.avd.allowed |= perms;
    break;
    case AVC_CALLBACK_TRY_REVOKE:
    case AVC_CALLBACK_REVOKE:
        node->ae.avd.allowed &= ~perms;
    break;
    case AVC_CALLBACK_AUDITALLOW_ENABLE:
        node->ae.avd.auditallow |= perms;
    break;
    case AVC_CALLBACK_AUDITALLOW_DISABLE:
        node->ae.avd.auditallow &= ~perms;
    break;
    case AVC_CALLBACK_AUDITDENY_ENABLE:
        node->ae.avd.auditdeny |= perms;
    break;
    case AVC_CALLBACK_AUDITDENY_DISABLE:
        node->ae.avd.auditdeny &= ~perms;
    break;
    }
    avc_node_replace(node, orig);
out_unlock:
    spin_unlock_irqrestore(lock, flag);
out:
    return rc;
}

/**
 * avc_ss_reset - Flush the cache and revalidate migrated permissions.
 * @seqno: policy sequence number
 */
int avc_ss_reset(u32 seqno)
{
    struct avc_callback_node *c;
    int i, rc = 0, tmprc;
    unsigned long flag;
    struct avc_node *node;
    struct hlist_head *head;
    struct hlist_node *next;
    spinlock_t *lock;

    for ( i = 0; i < AVC_CACHE_SLOTS; i++ )
    {
        head = &avc_cache.slots[i];
        lock = &avc_cache.slots_lock[i];

        spin_lock_irqsave(lock, flag);
        rcu_read_lock();
        hlist_for_each_entry(node, next, head, list)
            avc_node_delete(node);
        rcu_read_unlock();
        spin_unlock_irqrestore(lock, flag);
    }
    
    for ( c = avc_callbacks; c; c = c->next )
    {
        if ( c->events & AVC_CALLBACK_RESET )
        {
            tmprc = c->callback(AVC_CALLBACK_RESET,
                                0, 0, 0, 0, NULL);
            /* save the first error encountered for the return
               value and continue processing the callbacks */
            if ( !rc )
                rc = tmprc;
        }
    }

    avc_latest_notif_update(seqno, 0);
    return rc;
}

/**
 * avc_has_perm_noaudit - Check permissions but perform no auditing.
 * @ssid: source security identifier
 * @tsid: target security identifier
 * @tclass: target security class
 * @requested: requested permissions, interpreted based on @tclass
 * @avd: access vector decisions
 *
 * Check the AVC to determine whether the @requested permissions are granted
 * for the SID pair (@ssid, @tsid), interpreting the permissions
 * based on @tclass, and call the security server on a cache miss to obtain
 * a new decision and add it to the cache.  Return a copy of the decisions
 * in @avd.  Return %0 if all @requested permissions are granted,
 * -%EACCES if any permissions are denied, or another -errno upon
 * other errors.  This function is typically called by avc_has_perm(),
 * but may also be called directly to separate permission checking from
 * auditing, e.g. in cases where a lock must be held for the check but
 * should be released for the auditing.
 */
int avc_has_perm_noaudit(u32 ssid, u32 tsid, u16 tclass, u32 requested,
                         struct av_decision *in_avd)
{
    struct avc_node *node;
    struct av_decision avd_entry, *avd;
    int rc = 0;
    u32 denied;

    BUG_ON(!requested);

    rcu_read_lock();

    node = avc_lookup(ssid, tsid, tclass);
    if ( !node )
    {
        rcu_read_unlock();

        if ( in_avd )
            avd = in_avd;
        else
            avd = &avd_entry;

        rc = security_compute_av(ssid,tsid,tclass,requested,avd);
        if ( rc )
            goto out;
        rcu_read_lock();
        node = avc_insert(ssid,tsid,tclass,avd);
    } else {
        if ( in_avd )
            memcpy(in_avd, &node->ae.avd, sizeof(*in_avd));
        avd = &node->ae.avd;
    }

    denied = requested & ~(avd->allowed);

    if ( denied )
    {
        if ( !flask_enforcing || (avd->flags & AVD_FLAGS_PERMISSIVE) )
            avc_update_node(AVC_CALLBACK_GRANT,requested,
                            ssid,tsid,tclass,avd->seqno);
        else
            rc = -EACCES;
    }

    rcu_read_unlock();
out:
    return rc;
}

/**
 * avc_has_perm - Check permissions and perform any appropriate auditing.
 * @ssid: source security identifier
 * @tsid: target security identifier
 * @tclass: target security class
 * @requested: requested permissions, interpreted based on @tclass
 * @auditdata: auxiliary audit data
 *
 * Check the AVC to determine whether the @requested permissions are granted
 * for the SID pair (@ssid, @tsid), interpreting the permissions
 * based on @tclass, and call the security server on a cache miss to obtain
 * a new decision and add it to the cache.  Audit the granting or denial of
 * permissions in accordance with the policy.  Return %0 if all @requested
 * permissions are granted, -%EACCES if any permissions are denied, or
 * another -errno upon other errors.
 */
int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
                 u32 requested, struct avc_audit_data *auditdata)
{
    struct av_decision avd;
    int rc;

    rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, &avd);
    avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
    return rc;
}