common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * security/tomoyo/common.h
  3. *
  4. * Common functions for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0 2009/04/01
  9. *
  10. */
  11. #ifndef _SECURITY_TOMOYO_COMMON_H
  12. #define _SECURITY_TOMOYO_COMMON_H
  13. #include <linux/ctype.h>
  14. #include <linux/string.h>
  15. #include <linux/mm.h>
  16. #include <linux/file.h>
  17. #include <linux/kmod.h>
  18. #include <linux/fs.h>
  19. #include <linux/sched.h>
  20. #include <linux/namei.h>
  21. #include <linux/mount.h>
  22. #include <linux/list.h>
  23. struct dentry;
  24. struct vfsmount;
  25. /* Temporary buffer for holding pathnames. */
  26. struct tomoyo_page_buffer {
  27. char buffer[4096];
  28. };
  29. /* Structure for holding a token. */
  30. struct tomoyo_path_info {
  31. const char *name;
  32. u32 hash; /* = full_name_hash(name, strlen(name)) */
  33. u16 const_len; /* = tomoyo_const_part_length(name) */
  34. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  35. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  36. u16 depth; /* = tomoyo_path_depth(name) */
  37. };
  38. /*
  39. * This is the max length of a token.
  40. *
  41. * A token consists of only ASCII printable characters.
  42. * Non printable characters in a token is represented in \ooo style
  43. * octal string. Thus, \ itself is represented as \\.
  44. */
  45. #define TOMOYO_MAX_PATHNAME_LEN 4000
  46. /* Structure for holding requested pathname. */
  47. struct tomoyo_path_info_with_data {
  48. /* Keep "head" first, for this pointer is passed to tomoyo_free(). */
  49. struct tomoyo_path_info head;
  50. char barrier1[16]; /* Safeguard for overrun. */
  51. char body[TOMOYO_MAX_PATHNAME_LEN];
  52. char barrier2[16]; /* Safeguard for overrun. */
  53. };
  54. /*
  55. * Common header for holding ACL entries.
  56. *
  57. * Packing "struct tomoyo_acl_info" allows
  58. * "struct tomoyo_single_path_acl_record" to embed "u16" and
  59. * "struct tomoyo_double_path_acl_record" to embed "u8"
  60. * without enlarging their structure size.
  61. */
  62. struct tomoyo_acl_info {
  63. struct list_head list;
  64. /*
  65. * Type of this ACL entry.
  66. *
  67. * MSB is is_deleted flag.
  68. */
  69. u8 type;
  70. } __packed;
  71. /* This ACL entry is deleted. */
  72. #define TOMOYO_ACL_DELETED 0x80
  73. /* Structure for domain information. */
  74. struct tomoyo_domain_info {
  75. struct list_head list;
  76. struct list_head acl_info_list;
  77. /* Name of this domain. Never NULL. */
  78. const struct tomoyo_path_info *domainname;
  79. u8 profile; /* Profile number to use. */
  80. bool is_deleted; /* Delete flag. */
  81. bool quota_warned; /* Quota warnning flag. */
  82. /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */
  83. u8 flags;
  84. };
  85. /* Profile number is an integer between 0 and 255. */
  86. #define TOMOYO_MAX_PROFILES 256
  87. /* Ignore "allow_read" directive in exception policy. */
  88. #define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1
  89. /*
  90. * This domain was unable to create a new domain at tomoyo_find_next_domain()
  91. * because the name of the domain to be created was too long or
  92. * it could not allocate memory.
  93. * More than one process continued execve() without domain transition.
  94. */
  95. #define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2
  96. /*
  97. * Structure for "allow_read/write", "allow_execute", "allow_read",
  98. * "allow_write", "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  99. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  100. * "allow_truncate", "allow_symlink" and "allow_rewrite" directive.
  101. */
  102. struct tomoyo_single_path_acl_record {
  103. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
  104. u16 perm;
  105. /* Pointer to single pathname. */
  106. const struct tomoyo_path_info *filename;
  107. };
  108. /* Structure for "allow_rename" and "allow_link" directive. */
  109. struct tomoyo_double_path_acl_record {
  110. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */
  111. u8 perm;
  112. /* Pointer to single pathname. */
  113. const struct tomoyo_path_info *filename1;
  114. /* Pointer to single pathname. */
  115. const struct tomoyo_path_info *filename2;
  116. };
  117. /* Keywords for ACLs. */
  118. #define TOMOYO_KEYWORD_ALIAS "alias "
  119. #define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
  120. #define TOMOYO_KEYWORD_DELETE "delete "
  121. #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
  122. #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
  123. #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
  124. #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
  125. #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
  126. #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
  127. #define TOMOYO_KEYWORD_SELECT "select "
  128. #define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
  129. #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
  130. /* A domain definition starts with <kernel>. */
  131. #define TOMOYO_ROOT_NAME "<kernel>"
  132. #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
  133. /* Index numbers for Access Controls. */
  134. #define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */
  135. #define TOMOYO_MAX_ACCEPT_ENTRY 1
  136. #define TOMOYO_VERBOSE 2
  137. #define TOMOYO_MAX_CONTROL_INDEX 3
  138. /* Structure for reading/writing policy via securityfs interfaces. */
  139. struct tomoyo_io_buffer {
  140. int (*read) (struct tomoyo_io_buffer *);
  141. int (*write) (struct tomoyo_io_buffer *);
  142. /* Exclusive lock for this structure. */
  143. struct mutex io_sem;
  144. /* The position currently reading from. */
  145. struct list_head *read_var1;
  146. /* Extra variables for reading. */
  147. struct list_head *read_var2;
  148. /* The position currently writing to. */
  149. struct tomoyo_domain_info *write_var1;
  150. /* The step for reading. */
  151. int read_step;
  152. /* Buffer for reading. */
  153. char *read_buf;
  154. /* EOF flag for reading. */
  155. bool read_eof;
  156. /* Read domain ACL of specified PID? */
  157. bool read_single_domain;
  158. /* Extra variable for reading. */
  159. u8 read_bit;
  160. /* Bytes available for reading. */
  161. int read_avail;
  162. /* Size of read buffer. */
  163. int readbuf_size;
  164. /* Buffer for writing. */
  165. char *write_buf;
  166. /* Bytes available for writing. */
  167. int write_avail;
  168. /* Size of write buffer. */
  169. int writebuf_size;
  170. };
  171. /* Check whether the domain has too many ACL entries to hold. */
  172. bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
  173. /* Transactional sprintf() for policy dump. */
  174. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  175. __attribute__ ((format(printf, 2, 3)));
  176. /* Check whether the domainname is correct. */
  177. bool tomoyo_is_correct_domain(const unsigned char *domainname,
  178. const char *function);
  179. /* Check whether the token is correct. */
  180. bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
  181. const s8 pattern_type, const s8 end_type,
  182. const char *function);
  183. /* Check whether the token can be a domainname. */
  184. bool tomoyo_is_domain_def(const unsigned char *buffer);
  185. /* Check whether the given filename matches the given pattern. */
  186. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  187. const struct tomoyo_path_info *pattern);
  188. /* Read "alias" entry in exception policy. */
  189. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
  190. /*
  191. * Read "initialize_domain" and "no_initialize_domain" entry
  192. * in exception policy.
  193. */
  194. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
  195. /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
  196. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
  197. /* Read "file_pattern" entry in exception policy. */
  198. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
  199. /* Read "allow_read" entry in exception policy. */
  200. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
  201. /* Read "deny_rewrite" entry in exception policy. */
  202. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
  203. /* Write domain policy violation warning message to console? */
  204. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  205. /* Convert double path operation to operation name. */
  206. const char *tomoyo_dp2keyword(const u8 operation);
  207. /* Get the last component of the given domainname. */
  208. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
  209. /* Get warning message. */
  210. const char *tomoyo_get_msg(const bool is_enforce);
  211. /* Convert single path operation to operation name. */
  212. const char *tomoyo_sp2keyword(const u8 operation);
  213. /* Delete a domain. */
  214. int tomoyo_delete_domain(char *data);
  215. /* Create "alias" entry in exception policy. */
  216. int tomoyo_write_alias_policy(char *data, const bool is_delete);
  217. /*
  218. * Create "initialize_domain" and "no_initialize_domain" entry
  219. * in exception policy.
  220. */
  221. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  222. const bool is_delete);
  223. /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
  224. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  225. const bool is_delete);
  226. /*
  227. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  228. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  229. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  230. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  231. * "allow_link" entry in domain policy.
  232. */
  233. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  234. const bool is_delete);
  235. /* Create "allow_read" entry in exception policy. */
  236. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
  237. /* Create "deny_rewrite" entry in exception policy. */
  238. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
  239. /* Create "file_pattern" entry in exception policy. */
  240. int tomoyo_write_pattern_policy(char *data, const bool is_delete);
  241. /* Find a domain by the given name. */
  242. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  243. /* Find or create a domain by the given name. */
  244. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  245. domainname,
  246. const u8 profile);
  247. /* Check mode for specified functionality. */
  248. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  249. const u8 index);
  250. /* Allocate memory for structures. */
  251. void *tomoyo_alloc_acl_element(const u8 acl_type);
  252. /* Fill in "struct tomoyo_path_info" members. */
  253. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  254. /* Run policy loader when /sbin/init starts. */
  255. void tomoyo_load_policy(const char *filename);
  256. /* Change "struct tomoyo_domain_info"->flags. */
  257. void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain,
  258. const bool is_delete, const u8 flags);
  259. /* strcmp() for "struct tomoyo_path_info" structure. */
  260. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  261. const struct tomoyo_path_info *b)
  262. {
  263. return a->hash != b->hash || strcmp(a->name, b->name);
  264. }
  265. /* Get type of an ACL entry. */
  266. static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr)
  267. {
  268. return ptr->type & ~TOMOYO_ACL_DELETED;
  269. }
  270. /* Get type of an ACL entry. */
  271. static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr)
  272. {
  273. return ptr->type;
  274. }
  275. /**
  276. * tomoyo_is_valid - Check whether the character is a valid char.
  277. *
  278. * @c: The character to check.
  279. *
  280. * Returns true if @c is a valid character, false otherwise.
  281. */
  282. static inline bool tomoyo_is_valid(const unsigned char c)
  283. {
  284. return c > ' ' && c < 127;
  285. }
  286. /**
  287. * tomoyo_is_invalid - Check whether the character is an invalid char.
  288. *
  289. * @c: The character to check.
  290. *
  291. * Returns true if @c is an invalid character, false otherwise.
  292. */
  293. static inline bool tomoyo_is_invalid(const unsigned char c)
  294. {
  295. return c && (c <= ' ' || c >= 127);
  296. }
  297. /* The list for "struct tomoyo_domain_info". */
  298. extern struct list_head tomoyo_domain_list;
  299. extern struct rw_semaphore tomoyo_domain_list_lock;
  300. /* Lock for domain->acl_info_list. */
  301. extern struct rw_semaphore tomoyo_domain_acl_info_list_lock;
  302. /* Has /sbin/init started? */
  303. extern bool tomoyo_policy_loaded;
  304. /* The kernel's domain. */
  305. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  306. /**
  307. * list_for_each_cookie - iterate over a list with cookie.
  308. * @pos: the &struct list_head to use as a loop cursor.
  309. * @cookie: the &struct list_head to use as a cookie.
  310. * @head: the head for your list.
  311. *
  312. * Same with list_for_each() except that this primitive uses @cookie
  313. * so that we can continue iteration.
  314. * @cookie must be NULL when iteration starts, and @cookie will become
  315. * NULL when iteration finishes.
  316. */
  317. #define list_for_each_cookie(pos, cookie, head) \
  318. for (({ if (!cookie) \
  319. cookie = head; }), \
  320. pos = (cookie)->next; \
  321. prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
  322. (cookie) = pos, pos = pos->next)
  323. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */