/** * aa_ns_visible - test if @view is visible from @curr * @curr: namespace to treat as the parent (NOT NULL) * @view: namespace to test if visible from @curr (NOT NULL) * @subns: whether view of a subns is allowed * * Returns: true if @view is visible from @curr else false
*/ bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns)
{ if (curr == view) returntrue;
if (!subns) returnfalse;
for ( ; view; view = view->parent) { if (view->parent == curr) returntrue;
}
returnfalse;
}
/** * aa_ns_name - Find the ns name to display for @view from @curr * @curr: current namespace (NOT NULL) * @view: namespace attempting to view (NOT NULL) * @subns: are subns visible * * Returns: name of @view visible from @curr
*/ constchar *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns)
{ /* if view == curr then the namespace name isn't displayed */ if (curr == view) return"";
if (aa_ns_visible(curr, view, subns)) { /* at this point if a ns is visible it is in a view ns * thus the curr ns.hname is a prefix of its name. * Only output the virtualized portion of the name * Add + 2 to skip over // separating curr hname prefix * from the visible tail of the views hname
*/ return view->base.hname + strlen(curr->base.hname) + 2;
}
/** * aa_free_ns - free a profile namespace * @ns: the namespace to free (MAYBE NULL) * * Requires: All references to the namespace must have been put, if the * namespace was referenced by a profile confining a task,
*/ void aa_free_ns(struct aa_ns *ns)
{ if (!ns) return;
/** * __aa_lookupn_ns - lookup the namespace matching @hname * @view: namespace to search in (NOT NULL) * @hname: hierarchical ns name (NOT NULL) * @n: length of @hname * * Requires: rcu_read_lock be held * * Returns: unrefcounted ns pointer or NULL if not found * * Do a relative name lookup, recursing through profile tree.
*/ struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, constchar *hname, size_t n)
{ struct aa_ns *ns = view; constchar *split;
if (n) return __aa_findn_ns(&ns->sub_ns, hname, n); return NULL;
}
/** * aa_lookupn_ns - look up a policy namespace relative to @view * @view: namespace to search in (NOT NULL) * @name: name of namespace to find (NOT NULL) * @n: length of @name * * Returns: a refcounted namespace on the list, or NULL if no namespace * called @name exists. * * refcount released by caller
*/ struct aa_ns *aa_lookupn_ns(struct aa_ns *view, constchar *name, size_t n)
{ struct aa_ns *ns = NULL;
ns = alloc_ns(parent->base.hname, name); if (!ns) return ERR_PTR(-ENOMEM);
ns->level = parent->level + 1;
mutex_lock_nested(&ns->lock, ns->level);
error = __aafs_ns_mkdir(ns, ns_subns_dir(parent), name, dir); if (error) {
AA_ERROR("Failed to create interface for ns %s\n",
ns->base.name);
mutex_unlock(&ns->lock);
aa_free_ns(ns); return ERR_PTR(error);
}
ns->parent = aa_get_ns(parent);
list_add_rcu(&ns->base.list, &parent->sub_ns); /* add list ref */
aa_get_ns(ns);
mutex_unlock(&ns->lock);
return ns;
}
/** * __aa_find_or_create_ns - create an ns, fail if it already exists * @parent: the parent of the namespace being created * @name: the name of the namespace * @dir: if not null the dir to put the ns entries in * * Returns: the a refcounted ns that has been add or an ERR_PTR
*/ struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, constchar *name, struct dentry *dir)
{ struct aa_ns *ns;
AA_BUG(!mutex_is_locked(&parent->lock));
/* try and find the specified ns */ /* released by caller */
ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name)); if (!ns)
ns = __aa_create_ns(parent, name, dir); else
ns = ERR_PTR(-EEXIST);
/* return ref */ return ns;
}
/** * aa_prepare_ns - find an existing or create a new namespace of @name * @parent: ns to treat as parent * @name: the namespace to find or add (NOT NULL) * * Returns: refcounted namespace or PTR_ERR if failed to create one
*/ struct aa_ns *aa_prepare_ns(struct aa_ns *parent, constchar *name)
{ struct aa_ns *ns;
mutex_lock_nested(&parent->lock, parent->level); /* try and find the specified ns and if it doesn't exist create it */ /* released by caller */
ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name)); if (!ns)
ns = __aa_create_ns(parent, name, NULL);
mutex_unlock(&parent->lock);
/** * __aa_remove_ns - remove a namespace and all its children * @ns: namespace to be removed (NOT NULL) * * Requires: ns->parent->lock be held and ns removed from parent.
*/ void __aa_remove_ns(struct aa_ns *ns)
{ /* remove ns from namespace list */
list_del_rcu(&ns->base.list);
destroy_ns(ns);
aa_put_ns(ns);
}
/** * __ns_list_release - remove all profile namespaces on the list put refs * @head: list of profile namespaces (NOT NULL) * * Requires: namespace lock be held
*/ staticvoid __ns_list_release(struct list_head *head)
{ struct aa_ns *ns, *tmp;