common.h 13 KB

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