common.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. /*
  26. * tomoyo_page_buffer is a structure which is used for holding a pathname
  27. * obtained from "struct dentry" and "struct vfsmount" pair.
  28. * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small
  29. * (because TOMOYO escapes non ASCII printable characters using \ooo format),
  30. * we will make the buffer larger.
  31. */
  32. struct tomoyo_page_buffer {
  33. char buffer[4096];
  34. };
  35. /*
  36. * tomoyo_path_info is a structure which is used for holding a string data
  37. * used by TOMOYO.
  38. * This structure has several fields for supporting pattern matching.
  39. *
  40. * (1) "name" is the '\0' terminated string data.
  41. * (2) "hash" is full_name_hash(name, strlen(name)).
  42. * This allows tomoyo_pathcmp() to compare by hash before actually compare
  43. * using strcmp().
  44. * (3) "const_len" is the length of the initial segment of "name" which
  45. * consists entirely of non wildcard characters. In other words, the length
  46. * which we can compare two strings using strncmp().
  47. * (4) "is_dir" is a bool which is true if "name" ends with "/",
  48. * false otherwise.
  49. * TOMOYO distinguishes directory and non-directory. A directory ends with
  50. * "/" and non-directory does not end with "/".
  51. * (5) "is_patterned" is a bool which is true if "name" contains wildcard
  52. * characters, false otherwise. This allows TOMOYO to use "hash" and
  53. * strcmp() for string comparison if "is_patterned" is false.
  54. */
  55. struct tomoyo_path_info {
  56. const char *name;
  57. u32 hash; /* = full_name_hash(name, strlen(name)) */
  58. u16 const_len; /* = tomoyo_const_part_length(name) */
  59. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  60. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  61. };
  62. /*
  63. * This is the max length of a token.
  64. *
  65. * A token consists of only ASCII printable characters.
  66. * Non printable characters in a token is represented in \ooo style
  67. * octal string. Thus, \ itself is represented as \\.
  68. */
  69. #define TOMOYO_MAX_PATHNAME_LEN 4000
  70. /*
  71. * tomoyo_path_info_with_data is a structure which is used for holding a
  72. * pathname obtained from "struct dentry" and "struct vfsmount" pair.
  73. *
  74. * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
  75. * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
  76. * buffer for the pathname only.
  77. *
  78. * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
  79. * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
  80. * so that we don't need to return two pointers to the caller. If the caller
  81. * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
  82. * "struct tomoyo_path_info_with_data".
  83. */
  84. struct tomoyo_path_info_with_data {
  85. /* Keep "head" first, for this pointer is passed to tomoyo_free(). */
  86. struct tomoyo_path_info head;
  87. char barrier1[16]; /* Safeguard for overrun. */
  88. char body[TOMOYO_MAX_PATHNAME_LEN];
  89. char barrier2[16]; /* Safeguard for overrun. */
  90. };
  91. /*
  92. * tomoyo_acl_info is a structure which is used for holding
  93. *
  94. * (1) "list" which is linked to the ->acl_info_list of
  95. * "struct tomoyo_domain_info"
  96. * (2) "type" which tells
  97. * (a) type & 0x7F : type of the entry (either
  98. * "struct tomoyo_single_path_acl_record" or
  99. * "struct tomoyo_double_path_acl_record")
  100. * (b) type & 0x80 : whether the entry is marked as "deleted".
  101. *
  102. * Packing "struct tomoyo_acl_info" allows
  103. * "struct tomoyo_single_path_acl_record" to embed "u16" and
  104. * "struct tomoyo_double_path_acl_record" to embed "u8"
  105. * without enlarging their structure size.
  106. */
  107. struct tomoyo_acl_info {
  108. struct list_head list;
  109. /*
  110. * Type of this ACL entry.
  111. *
  112. * MSB is is_deleted flag.
  113. */
  114. u8 type;
  115. } __packed;
  116. /* This ACL entry is deleted. */
  117. #define TOMOYO_ACL_DELETED 0x80
  118. /*
  119. * tomoyo_domain_info is a structure which is used for holding permissions
  120. * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  121. * It has following fields.
  122. *
  123. * (1) "list" which is linked to tomoyo_domain_list .
  124. * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
  125. * (3) "domainname" which holds the name of the domain.
  126. * (4) "profile" which remembers profile number assigned to this domain.
  127. * (5) "is_deleted" is a bool which is true if this domain is marked as
  128. * "deleted", false otherwise.
  129. * (6) "quota_warned" is a bool which is used for suppressing warning message
  130. * when learning mode learned too much entries.
  131. * (7) "flags" which remembers this domain's attributes.
  132. *
  133. * A domain's lifecycle is an analogy of files on / directory.
  134. * Multiple domains with the same domainname cannot be created (as with
  135. * creating files with the same filename fails with -EEXIST).
  136. * If a process reached a domain, that process can reside in that domain after
  137. * that domain is marked as "deleted" (as with a process can access an already
  138. * open()ed file after that file was unlink()ed).
  139. */
  140. struct tomoyo_domain_info {
  141. struct list_head list;
  142. struct list_head acl_info_list;
  143. /* Name of this domain. Never NULL. */
  144. const struct tomoyo_path_info *domainname;
  145. u8 profile; /* Profile number to use. */
  146. bool is_deleted; /* Delete flag. */
  147. bool quota_warned; /* Quota warnning flag. */
  148. /* DOMAIN_FLAGS_*. Use tomoyo_set_domain_flag() to modify. */
  149. u8 flags;
  150. };
  151. /* Profile number is an integer between 0 and 255. */
  152. #define TOMOYO_MAX_PROFILES 256
  153. /* Ignore "allow_read" directive in exception policy. */
  154. #define TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ 1
  155. /*
  156. * This domain was unable to create a new domain at tomoyo_find_next_domain()
  157. * because the name of the domain to be created was too long or
  158. * it could not allocate memory.
  159. * More than one process continued execve() without domain transition.
  160. */
  161. #define TOMOYO_DOMAIN_FLAGS_TRANSITION_FAILED 2
  162. /*
  163. * tomoyo_single_path_acl_record is a structure which is used for holding an
  164. * entry with one pathname operation (e.g. open(), mkdir()).
  165. * It has following fields.
  166. *
  167. * (1) "head" which is a "struct tomoyo_acl_info".
  168. * (2) "perm" which is a bitmask of permitted operations.
  169. * (3) "filename" is the pathname.
  170. *
  171. * Directives held by this structure are "allow_read/write", "allow_execute",
  172. * "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
  173. * "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock",
  174. * "allow_mkchar", "allow_truncate", "allow_symlink" and "allow_rewrite".
  175. */
  176. struct tomoyo_single_path_acl_record {
  177. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_SINGLE_PATH_ACL */
  178. u16 perm;
  179. /* Pointer to single pathname. */
  180. const struct tomoyo_path_info *filename;
  181. };
  182. /*
  183. * tomoyo_double_path_acl_record is a structure which is used for holding an
  184. * entry with two pathnames operation (i.e. link() and rename()).
  185. * It has following fields.
  186. *
  187. * (1) "head" which is a "struct tomoyo_acl_info".
  188. * (2) "perm" which is a bitmask of permitted operations.
  189. * (3) "filename1" is the source/old pathname.
  190. * (4) "filename2" is the destination/new pathname.
  191. *
  192. * Directives held by this structure are "allow_rename" and "allow_link".
  193. */
  194. struct tomoyo_double_path_acl_record {
  195. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_DOUBLE_PATH_ACL */
  196. u8 perm;
  197. /* Pointer to single pathname. */
  198. const struct tomoyo_path_info *filename1;
  199. /* Pointer to single pathname. */
  200. const struct tomoyo_path_info *filename2;
  201. };
  202. /* Keywords for ACLs. */
  203. #define TOMOYO_KEYWORD_ALIAS "alias "
  204. #define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
  205. #define TOMOYO_KEYWORD_DELETE "delete "
  206. #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
  207. #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
  208. #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
  209. #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
  210. #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
  211. #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
  212. #define TOMOYO_KEYWORD_SELECT "select "
  213. #define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
  214. #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
  215. /* A domain definition starts with <kernel>. */
  216. #define TOMOYO_ROOT_NAME "<kernel>"
  217. #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
  218. /* Index numbers for Access Controls. */
  219. #define TOMOYO_MAC_FOR_FILE 0 /* domain_policy.conf */
  220. #define TOMOYO_MAX_ACCEPT_ENTRY 1
  221. #define TOMOYO_VERBOSE 2
  222. #define TOMOYO_MAX_CONTROL_INDEX 3
  223. /*
  224. * tomoyo_io_buffer is a structure which is used for reading and modifying
  225. * configuration via /sys/kernel/security/tomoyo/ interface.
  226. * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
  227. * cursors.
  228. *
  229. * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
  230. * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
  231. * entry has a list of "struct tomoyo_acl_info", we need two cursors when
  232. * reading (one is for traversing tomoyo_domain_list and the other is for
  233. * traversing "struct tomoyo_acl_info"->acl_info_list ).
  234. *
  235. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  236. * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
  237. * domain with the domainname specified by the rest of that line (NULL is set
  238. * if seek failed).
  239. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  240. * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
  241. * line (->write_var1 is set to NULL if a domain was deleted).
  242. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  243. * neither "select " nor "delete ", an entry or a domain specified by that line
  244. * is appended.
  245. */
  246. struct tomoyo_io_buffer {
  247. int (*read) (struct tomoyo_io_buffer *);
  248. int (*write) (struct tomoyo_io_buffer *);
  249. /* Exclusive lock for this structure. */
  250. struct mutex io_sem;
  251. /* The position currently reading from. */
  252. struct list_head *read_var1;
  253. /* Extra variables for reading. */
  254. struct list_head *read_var2;
  255. /* The position currently writing to. */
  256. struct tomoyo_domain_info *write_var1;
  257. /* The step for reading. */
  258. int read_step;
  259. /* Buffer for reading. */
  260. char *read_buf;
  261. /* EOF flag for reading. */
  262. bool read_eof;
  263. /* Read domain ACL of specified PID? */
  264. bool read_single_domain;
  265. /* Extra variable for reading. */
  266. u8 read_bit;
  267. /* Bytes available for reading. */
  268. int read_avail;
  269. /* Size of read buffer. */
  270. int readbuf_size;
  271. /* Buffer for writing. */
  272. char *write_buf;
  273. /* Bytes available for writing. */
  274. int write_avail;
  275. /* Size of write buffer. */
  276. int writebuf_size;
  277. };
  278. /* Check whether the domain has too many ACL entries to hold. */
  279. bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
  280. /* Transactional sprintf() for policy dump. */
  281. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  282. __attribute__ ((format(printf, 2, 3)));
  283. /* Check whether the domainname is correct. */
  284. bool tomoyo_is_correct_domain(const unsigned char *domainname,
  285. const char *function);
  286. /* Check whether the token is correct. */
  287. bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
  288. const s8 pattern_type, const s8 end_type,
  289. const char *function);
  290. /* Check whether the token can be a domainname. */
  291. bool tomoyo_is_domain_def(const unsigned char *buffer);
  292. /* Check whether the given filename matches the given pattern. */
  293. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  294. const struct tomoyo_path_info *pattern);
  295. /* Read "alias" entry in exception policy. */
  296. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
  297. /*
  298. * Read "initialize_domain" and "no_initialize_domain" entry
  299. * in exception policy.
  300. */
  301. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
  302. /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
  303. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
  304. /* Read "file_pattern" entry in exception policy. */
  305. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
  306. /* Read "allow_read" entry in exception policy. */
  307. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
  308. /* Read "deny_rewrite" entry in exception policy. */
  309. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
  310. /* Write domain policy violation warning message to console? */
  311. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  312. /* Convert double path operation to operation name. */
  313. const char *tomoyo_dp2keyword(const u8 operation);
  314. /* Get the last component of the given domainname. */
  315. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
  316. /* Get warning message. */
  317. const char *tomoyo_get_msg(const bool is_enforce);
  318. /* Convert single path operation to operation name. */
  319. const char *tomoyo_sp2keyword(const u8 operation);
  320. /* Create "alias" entry in exception policy. */
  321. int tomoyo_write_alias_policy(char *data, const bool is_delete);
  322. /*
  323. * Create "initialize_domain" and "no_initialize_domain" entry
  324. * in exception policy.
  325. */
  326. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  327. const bool is_delete);
  328. /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
  329. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  330. const bool is_delete);
  331. /*
  332. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  333. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  334. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  335. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  336. * "allow_link" entry in domain policy.
  337. */
  338. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  339. const bool is_delete);
  340. /* Create "allow_read" entry in exception policy. */
  341. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
  342. /* Create "deny_rewrite" entry in exception policy. */
  343. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
  344. /* Create "file_pattern" entry in exception policy. */
  345. int tomoyo_write_pattern_policy(char *data, const bool is_delete);
  346. /* Find a domain by the given name. */
  347. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  348. /* Find or create a domain by the given name. */
  349. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  350. domainname,
  351. const u8 profile);
  352. /* Check mode for specified functionality. */
  353. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  354. const u8 index);
  355. /* Allocate memory for structures. */
  356. void *tomoyo_alloc_acl_element(const u8 acl_type);
  357. /* Fill in "struct tomoyo_path_info" members. */
  358. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  359. /* Run policy loader when /sbin/init starts. */
  360. void tomoyo_load_policy(const char *filename);
  361. /* Change "struct tomoyo_domain_info"->flags. */
  362. void tomoyo_set_domain_flag(struct tomoyo_domain_info *domain,
  363. const bool is_delete, const u8 flags);
  364. /* strcmp() for "struct tomoyo_path_info" structure. */
  365. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  366. const struct tomoyo_path_info *b)
  367. {
  368. return a->hash != b->hash || strcmp(a->name, b->name);
  369. }
  370. /* Get type of an ACL entry. */
  371. static inline u8 tomoyo_acl_type1(struct tomoyo_acl_info *ptr)
  372. {
  373. return ptr->type & ~TOMOYO_ACL_DELETED;
  374. }
  375. /* Get type of an ACL entry. */
  376. static inline u8 tomoyo_acl_type2(struct tomoyo_acl_info *ptr)
  377. {
  378. return ptr->type;
  379. }
  380. /**
  381. * tomoyo_is_valid - Check whether the character is a valid char.
  382. *
  383. * @c: The character to check.
  384. *
  385. * Returns true if @c is a valid character, false otherwise.
  386. */
  387. static inline bool tomoyo_is_valid(const unsigned char c)
  388. {
  389. return c > ' ' && c < 127;
  390. }
  391. /**
  392. * tomoyo_is_invalid - Check whether the character is an invalid char.
  393. *
  394. * @c: The character to check.
  395. *
  396. * Returns true if @c is an invalid character, false otherwise.
  397. */
  398. static inline bool tomoyo_is_invalid(const unsigned char c)
  399. {
  400. return c && (c <= ' ' || c >= 127);
  401. }
  402. /* The list for "struct tomoyo_domain_info". */
  403. extern struct list_head tomoyo_domain_list;
  404. extern struct rw_semaphore tomoyo_domain_list_lock;
  405. /* Lock for domain->acl_info_list. */
  406. extern struct rw_semaphore tomoyo_domain_acl_info_list_lock;
  407. /* Has /sbin/init started? */
  408. extern bool tomoyo_policy_loaded;
  409. /* The kernel's domain. */
  410. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  411. /**
  412. * list_for_each_cookie - iterate over a list with cookie.
  413. * @pos: the &struct list_head to use as a loop cursor.
  414. * @cookie: the &struct list_head to use as a cookie.
  415. * @head: the head for your list.
  416. *
  417. * Same with list_for_each() except that this primitive uses @cookie
  418. * so that we can continue iteration.
  419. * @cookie must be NULL when iteration starts, and @cookie will become
  420. * NULL when iteration finishes.
  421. */
  422. #define list_for_each_cookie(pos, cookie, head) \
  423. for (({ if (!cookie) \
  424. cookie = head; }), \
  425. pos = (cookie)->next; \
  426. prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
  427. (cookie) = pos, pos = pos->next)
  428. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */