common.h 18 KB

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