policy_ns.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * AppArmor security module
  3. *
  4. * This file contains AppArmor policy definitions.
  5. *
  6. * Copyright (C) 1998-2008 Novell/SUSE
  7. * Copyright 2009-2017 Canonical Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation, version 2 of the
  12. * License.
  13. */
  14. #ifndef __AA_NAMESPACE_H
  15. #define __AA_NAMESPACE_H
  16. #include <linux/kref.h>
  17. #include "apparmor.h"
  18. #include "apparmorfs.h"
  19. #include "label.h"
  20. #include "policy.h"
  21. /* struct aa_ns_acct - accounting of profiles in namespace
  22. * @max_size: maximum space allowed for all profiles in namespace
  23. * @max_count: maximum number of profiles that can be in this namespace
  24. * @size: current size of profiles
  25. * @count: current count of profiles (includes null profiles)
  26. */
  27. struct aa_ns_acct {
  28. int max_size;
  29. int max_count;
  30. int size;
  31. int count;
  32. };
  33. /* struct aa_ns - namespace for a set of profiles
  34. * @base: common policy
  35. * @parent: parent of namespace
  36. * @lock: lock for modifying the object
  37. * @acct: accounting for the namespace
  38. * @unconfined: special unconfined profile for the namespace
  39. * @sub_ns: list of namespaces under the current namespace.
  40. * @uniq_null: uniq value used for null learning profiles
  41. * @uniq_id: a unique id count for the profiles in the namespace
  42. * @level: level of ns within the tree hierarchy
  43. * @dents: dentries for the namespaces file entries in apparmorfs
  44. *
  45. * An aa_ns defines the set profiles that are searched to determine which
  46. * profile to attach to a task. Profiles can not be shared between aa_ns
  47. * and profile names within a namespace are guaranteed to be unique. When
  48. * profiles in separate namespaces have the same name they are NOT considered
  49. * to be equivalent.
  50. *
  51. * Namespaces are hierarchical and only namespaces and profiles below the
  52. * current namespace are visible.
  53. *
  54. * Namespace names must be unique and can not contain the characters :/\0
  55. */
  56. struct aa_ns {
  57. struct aa_policy base;
  58. struct aa_ns *parent;
  59. struct mutex lock;
  60. struct aa_ns_acct acct;
  61. struct aa_profile *unconfined;
  62. struct list_head sub_ns;
  63. atomic_t uniq_null;
  64. long uniq_id;
  65. int level;
  66. long revision;
  67. wait_queue_head_t wait;
  68. struct aa_labelset labels;
  69. struct list_head rawdata_list;
  70. struct dentry *dents[AAFS_NS_SIZEOF];
  71. };
  72. extern struct aa_ns *root_ns;
  73. extern const char *aa_hidden_ns_name;
  74. #define ns_unconfined(NS) (&(NS)->unconfined->label)
  75. bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns);
  76. const char *aa_ns_name(struct aa_ns *parent, struct aa_ns *child, bool subns);
  77. void aa_free_ns(struct aa_ns *ns);
  78. int aa_alloc_root_ns(void);
  79. void aa_free_root_ns(void);
  80. void aa_free_ns_kref(struct kref *kref);
  81. struct aa_ns *aa_find_ns(struct aa_ns *root, const char *name);
  82. struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n);
  83. struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, const char *hname, size_t n);
  84. struct aa_ns *aa_lookupn_ns(struct aa_ns *view, const char *name, size_t n);
  85. struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name,
  86. struct dentry *dir);
  87. struct aa_ns *aa_prepare_ns(struct aa_ns *root, const char *name);
  88. void __aa_remove_ns(struct aa_ns *ns);
  89. static inline struct aa_profile *aa_deref_parent(struct aa_profile *p)
  90. {
  91. return rcu_dereference_protected(p->parent,
  92. mutex_is_locked(&p->ns->lock));
  93. }
  94. /**
  95. * aa_get_ns - increment references count on @ns
  96. * @ns: namespace to increment reference count of (MAYBE NULL)
  97. *
  98. * Returns: pointer to @ns, if @ns is NULL returns NULL
  99. * Requires: @ns must be held with valid refcount when called
  100. */
  101. static inline struct aa_ns *aa_get_ns(struct aa_ns *ns)
  102. {
  103. if (ns)
  104. aa_get_profile(ns->unconfined);
  105. return ns;
  106. }
  107. /**
  108. * aa_put_ns - decrement refcount on @ns
  109. * @ns: namespace to put reference of
  110. *
  111. * Decrement reference count of @ns and if no longer in use free it
  112. */
  113. static inline void aa_put_ns(struct aa_ns *ns)
  114. {
  115. if (ns)
  116. aa_put_profile(ns->unconfined);
  117. }
  118. /**
  119. * __aa_findn_ns - find a namespace on a list by @name
  120. * @head: list to search for namespace on (NOT NULL)
  121. * @name: name of namespace to look for (NOT NULL)
  122. * @n: length of @name
  123. * Returns: unrefcounted namespace
  124. *
  125. * Requires: rcu_read_lock be held
  126. */
  127. static inline struct aa_ns *__aa_findn_ns(struct list_head *head,
  128. const char *name, size_t n)
  129. {
  130. return (struct aa_ns *)__policy_strn_find(head, name, n);
  131. }
  132. static inline struct aa_ns *__aa_find_ns(struct list_head *head,
  133. const char *name)
  134. {
  135. return __aa_findn_ns(head, name, strlen(name));
  136. }
  137. static inline struct aa_ns *__aa_lookup_ns(struct aa_ns *base,
  138. const char *hname)
  139. {
  140. return __aa_lookupn_ns(base, hname, strlen(hname));
  141. }
  142. static inline struct aa_ns *aa_lookup_ns(struct aa_ns *view, const char *name)
  143. {
  144. return aa_lookupn_ns(view, name, strlen(name));
  145. }
  146. #endif /* AA_NAMESPACE_H */