common.h 13 KB

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