/security/tomoyo/common.h

https://bitbucket.org/abioy/linux · C Header · 757 lines · 313 code · 61 blank · 383 comment · 10 complexity · cc9e479717d0f698325b3a7bb5b0de16 MD5 · raw file

  1. /*
  2. * security/tomoyo/common.h
  3. *
  4. * Header file for TOMOYO.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #ifndef _SECURITY_TOMOYO_COMMON_H
  9. #define _SECURITY_TOMOYO_COMMON_H
  10. #include <linux/ctype.h>
  11. #include <linux/string.h>
  12. #include <linux/mm.h>
  13. #include <linux/file.h>
  14. #include <linux/kmod.h>
  15. #include <linux/fs.h>
  16. #include <linux/sched.h>
  17. #include <linux/namei.h>
  18. #include <linux/mount.h>
  19. #include <linux/list.h>
  20. #include <linux/cred.h>
  21. struct linux_binprm;
  22. /********** Constants definitions. **********/
  23. /*
  24. * TOMOYO uses this hash only when appending a string into the string
  25. * table. Frequency of appending strings is very low. So we don't need
  26. * large (e.g. 64k) hash size. 256 will be sufficient.
  27. */
  28. #define TOMOYO_HASH_BITS 8
  29. #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
  30. /*
  31. * This is the max length of a token.
  32. *
  33. * A token consists of only ASCII printable characters.
  34. * Non printable characters in a token is represented in \ooo style
  35. * octal string. Thus, \ itself is represented as \\.
  36. */
  37. #define TOMOYO_MAX_PATHNAME_LEN 4000
  38. /* Profile number is an integer between 0 and 255. */
  39. #define TOMOYO_MAX_PROFILES 256
  40. /* Keywords for ACLs. */
  41. #define TOMOYO_KEYWORD_ALIAS "alias "
  42. #define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
  43. #define TOMOYO_KEYWORD_DELETE "delete "
  44. #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
  45. #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
  46. #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
  47. #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
  48. #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
  49. #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
  50. #define TOMOYO_KEYWORD_SELECT "select "
  51. #define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
  52. #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
  53. /* A domain definition starts with <kernel>. */
  54. #define TOMOYO_ROOT_NAME "<kernel>"
  55. #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
  56. /* Index numbers for Access Controls. */
  57. enum tomoyo_mac_index {
  58. TOMOYO_MAC_FOR_FILE, /* domain_policy.conf */
  59. TOMOYO_MAX_ACCEPT_ENTRY,
  60. TOMOYO_VERBOSE,
  61. TOMOYO_MAX_CONTROL_INDEX
  62. };
  63. /* Index numbers for Access Controls. */
  64. enum tomoyo_acl_entry_type_index {
  65. TOMOYO_TYPE_PATH_ACL,
  66. TOMOYO_TYPE_PATH2_ACL,
  67. };
  68. /* Index numbers for File Controls. */
  69. /*
  70. * TYPE_READ_WRITE_ACL is special. TYPE_READ_WRITE_ACL is automatically set
  71. * if both TYPE_READ_ACL and TYPE_WRITE_ACL are set. Both TYPE_READ_ACL and
  72. * TYPE_WRITE_ACL are automatically set if TYPE_READ_WRITE_ACL is set.
  73. * TYPE_READ_WRITE_ACL is automatically cleared if either TYPE_READ_ACL or
  74. * TYPE_WRITE_ACL is cleared. Both TYPE_READ_ACL and TYPE_WRITE_ACL are
  75. * automatically cleared if TYPE_READ_WRITE_ACL is cleared.
  76. */
  77. enum tomoyo_path_acl_index {
  78. TOMOYO_TYPE_READ_WRITE,
  79. TOMOYO_TYPE_EXECUTE,
  80. TOMOYO_TYPE_READ,
  81. TOMOYO_TYPE_WRITE,
  82. TOMOYO_TYPE_CREATE,
  83. TOMOYO_TYPE_UNLINK,
  84. TOMOYO_TYPE_MKDIR,
  85. TOMOYO_TYPE_RMDIR,
  86. TOMOYO_TYPE_MKFIFO,
  87. TOMOYO_TYPE_MKSOCK,
  88. TOMOYO_TYPE_MKBLOCK,
  89. TOMOYO_TYPE_MKCHAR,
  90. TOMOYO_TYPE_TRUNCATE,
  91. TOMOYO_TYPE_SYMLINK,
  92. TOMOYO_TYPE_REWRITE,
  93. TOMOYO_TYPE_IOCTL,
  94. TOMOYO_TYPE_CHMOD,
  95. TOMOYO_TYPE_CHOWN,
  96. TOMOYO_TYPE_CHGRP,
  97. TOMOYO_TYPE_CHROOT,
  98. TOMOYO_TYPE_MOUNT,
  99. TOMOYO_TYPE_UMOUNT,
  100. TOMOYO_MAX_PATH_OPERATION
  101. };
  102. enum tomoyo_path2_acl_index {
  103. TOMOYO_TYPE_LINK,
  104. TOMOYO_TYPE_RENAME,
  105. TOMOYO_TYPE_PIVOT_ROOT,
  106. TOMOYO_MAX_PATH2_OPERATION
  107. };
  108. enum tomoyo_securityfs_interface_index {
  109. TOMOYO_DOMAINPOLICY,
  110. TOMOYO_EXCEPTIONPOLICY,
  111. TOMOYO_DOMAIN_STATUS,
  112. TOMOYO_PROCESS_STATUS,
  113. TOMOYO_MEMINFO,
  114. TOMOYO_SELFDOMAIN,
  115. TOMOYO_VERSION,
  116. TOMOYO_PROFILE,
  117. TOMOYO_MANAGER
  118. };
  119. /********** Structure definitions. **********/
  120. /*
  121. * tomoyo_page_buffer is a structure which is used for holding a pathname
  122. * obtained from "struct dentry" and "struct vfsmount" pair.
  123. * As of now, it is 4096 bytes. If users complain that 4096 bytes is too small
  124. * (because TOMOYO escapes non ASCII printable characters using \ooo format),
  125. * we will make the buffer larger.
  126. */
  127. struct tomoyo_page_buffer {
  128. char buffer[4096];
  129. };
  130. /*
  131. * tomoyo_path_info is a structure which is used for holding a string data
  132. * used by TOMOYO.
  133. * This structure has several fields for supporting pattern matching.
  134. *
  135. * (1) "name" is the '\0' terminated string data.
  136. * (2) "hash" is full_name_hash(name, strlen(name)).
  137. * This allows tomoyo_pathcmp() to compare by hash before actually compare
  138. * using strcmp().
  139. * (3) "const_len" is the length of the initial segment of "name" which
  140. * consists entirely of non wildcard characters. In other words, the length
  141. * which we can compare two strings using strncmp().
  142. * (4) "is_dir" is a bool which is true if "name" ends with "/",
  143. * false otherwise.
  144. * TOMOYO distinguishes directory and non-directory. A directory ends with
  145. * "/" and non-directory does not end with "/".
  146. * (5) "is_patterned" is a bool which is true if "name" contains wildcard
  147. * characters, false otherwise. This allows TOMOYO to use "hash" and
  148. * strcmp() for string comparison if "is_patterned" is false.
  149. */
  150. struct tomoyo_path_info {
  151. const char *name;
  152. u32 hash; /* = full_name_hash(name, strlen(name)) */
  153. u16 const_len; /* = tomoyo_const_part_length(name) */
  154. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  155. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  156. };
  157. /*
  158. * tomoyo_name_entry is a structure which is used for linking
  159. * "struct tomoyo_path_info" into tomoyo_name_list .
  160. */
  161. struct tomoyo_name_entry {
  162. struct list_head list;
  163. atomic_t users;
  164. struct tomoyo_path_info entry;
  165. };
  166. /*
  167. * tomoyo_path_info_with_data is a structure which is used for holding a
  168. * pathname obtained from "struct dentry" and "struct vfsmount" pair.
  169. *
  170. * "struct tomoyo_path_info_with_data" consists of "struct tomoyo_path_info"
  171. * and buffer for the pathname, while "struct tomoyo_page_buffer" consists of
  172. * buffer for the pathname only.
  173. *
  174. * "struct tomoyo_path_info_with_data" is intended to allow TOMOYO to release
  175. * both "struct tomoyo_path_info" and buffer for the pathname by single kfree()
  176. * so that we don't need to return two pointers to the caller. If the caller
  177. * puts "struct tomoyo_path_info" on stack memory, we will be able to remove
  178. * "struct tomoyo_path_info_with_data".
  179. */
  180. struct tomoyo_path_info_with_data {
  181. /* Keep "head" first, for this pointer is passed to kfree(). */
  182. struct tomoyo_path_info head;
  183. char barrier1[16]; /* Safeguard for overrun. */
  184. char body[TOMOYO_MAX_PATHNAME_LEN];
  185. char barrier2[16]; /* Safeguard for overrun. */
  186. };
  187. /*
  188. * tomoyo_acl_info is a structure which is used for holding
  189. *
  190. * (1) "list" which is linked to the ->acl_info_list of
  191. * "struct tomoyo_domain_info"
  192. * (2) "type" which tells type of the entry (either
  193. * "struct tomoyo_path_acl" or "struct tomoyo_path2_acl").
  194. *
  195. * Packing "struct tomoyo_acl_info" allows
  196. * "struct tomoyo_path_acl" to embed "u8" + "u16" and
  197. * "struct tomoyo_path2_acl" to embed "u8"
  198. * without enlarging their structure size.
  199. */
  200. struct tomoyo_acl_info {
  201. struct list_head list;
  202. u8 type;
  203. } __packed;
  204. /*
  205. * tomoyo_domain_info is a structure which is used for holding permissions
  206. * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  207. * It has following fields.
  208. *
  209. * (1) "list" which is linked to tomoyo_domain_list .
  210. * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
  211. * (3) "domainname" which holds the name of the domain.
  212. * (4) "profile" which remembers profile number assigned to this domain.
  213. * (5) "is_deleted" is a bool which is true if this domain is marked as
  214. * "deleted", false otherwise.
  215. * (6) "quota_warned" is a bool which is used for suppressing warning message
  216. * when learning mode learned too much entries.
  217. * (7) "ignore_global_allow_read" is a bool which is true if this domain
  218. * should ignore "allow_read" directive in exception policy.
  219. * (8) "transition_failed" is a bool which is set to true when this domain was
  220. * unable to create a new domain at tomoyo_find_next_domain() because the
  221. * name of the domain to be created was too long or it could not allocate
  222. * memory. If set to true, more than one process continued execve()
  223. * without domain transition.
  224. * (9) "users" is an atomic_t that holds how many "struct cred"->security
  225. * are referring this "struct tomoyo_domain_info". If is_deleted == true
  226. * and users == 0, this struct will be kfree()d upon next garbage
  227. * collection.
  228. *
  229. * A domain's lifecycle is an analogy of files on / directory.
  230. * Multiple domains with the same domainname cannot be created (as with
  231. * creating files with the same filename fails with -EEXIST).
  232. * If a process reached a domain, that process can reside in that domain after
  233. * that domain is marked as "deleted" (as with a process can access an already
  234. * open()ed file after that file was unlink()ed).
  235. */
  236. struct tomoyo_domain_info {
  237. struct list_head list;
  238. struct list_head acl_info_list;
  239. /* Name of this domain. Never NULL. */
  240. const struct tomoyo_path_info *domainname;
  241. u8 profile; /* Profile number to use. */
  242. bool is_deleted; /* Delete flag. */
  243. bool quota_warned; /* Quota warnning flag. */
  244. bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
  245. bool transition_failed; /* Domain transition failed flag. */
  246. atomic_t users; /* Number of referring credentials. */
  247. };
  248. /*
  249. * tomoyo_path_acl is a structure which is used for holding an
  250. * entry with one pathname operation (e.g. open(), mkdir()).
  251. * It has following fields.
  252. *
  253. * (1) "head" which is a "struct tomoyo_acl_info".
  254. * (2) "perm" which is a bitmask of permitted operations.
  255. * (3) "filename" is the pathname.
  256. *
  257. * Directives held by this structure are "allow_read/write", "allow_execute",
  258. * "allow_read", "allow_write", "allow_create", "allow_unlink", "allow_mkdir",
  259. * "allow_rmdir", "allow_mkfifo", "allow_mksock", "allow_mkblock",
  260. * "allow_mkchar", "allow_truncate", "allow_symlink", "allow_rewrite",
  261. * "allow_chmod", "allow_chown", "allow_chgrp", "allow_chroot", "allow_mount"
  262. * and "allow_unmount".
  263. */
  264. struct tomoyo_path_acl {
  265. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  266. u8 perm_high;
  267. u16 perm;
  268. /* Pointer to single pathname. */
  269. const struct tomoyo_path_info *filename;
  270. };
  271. /*
  272. * tomoyo_path2_acl is a structure which is used for holding an
  273. * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
  274. * It has following fields.
  275. *
  276. * (1) "head" which is a "struct tomoyo_acl_info".
  277. * (2) "perm" which is a bitmask of permitted operations.
  278. * (3) "filename1" is the source/old pathname.
  279. * (4) "filename2" is the destination/new pathname.
  280. *
  281. * Directives held by this structure are "allow_rename", "allow_link" and
  282. * "allow_pivot_root".
  283. */
  284. struct tomoyo_path2_acl {
  285. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  286. u8 perm;
  287. /* Pointer to single pathname. */
  288. const struct tomoyo_path_info *filename1;
  289. /* Pointer to single pathname. */
  290. const struct tomoyo_path_info *filename2;
  291. };
  292. /*
  293. * tomoyo_io_buffer is a structure which is used for reading and modifying
  294. * configuration via /sys/kernel/security/tomoyo/ interface.
  295. * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
  296. * cursors.
  297. *
  298. * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
  299. * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
  300. * entry has a list of "struct tomoyo_acl_info", we need two cursors when
  301. * reading (one is for traversing tomoyo_domain_list and the other is for
  302. * traversing "struct tomoyo_acl_info"->acl_info_list ).
  303. *
  304. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  305. * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
  306. * domain with the domainname specified by the rest of that line (NULL is set
  307. * if seek failed).
  308. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  309. * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
  310. * line (->write_var1 is set to NULL if a domain was deleted).
  311. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  312. * neither "select " nor "delete ", an entry or a domain specified by that line
  313. * is appended.
  314. */
  315. struct tomoyo_io_buffer {
  316. int (*read) (struct tomoyo_io_buffer *);
  317. int (*write) (struct tomoyo_io_buffer *);
  318. /* Exclusive lock for this structure. */
  319. struct mutex io_sem;
  320. /* Index returned by tomoyo_read_lock(). */
  321. int reader_idx;
  322. /* The position currently reading from. */
  323. struct list_head *read_var1;
  324. /* Extra variables for reading. */
  325. struct list_head *read_var2;
  326. /* The position currently writing to. */
  327. struct tomoyo_domain_info *write_var1;
  328. /* The step for reading. */
  329. int read_step;
  330. /* Buffer for reading. */
  331. char *read_buf;
  332. /* EOF flag for reading. */
  333. bool read_eof;
  334. /* Read domain ACL of specified PID? */
  335. bool read_single_domain;
  336. /* Extra variable for reading. */
  337. u8 read_bit;
  338. /* Bytes available for reading. */
  339. int read_avail;
  340. /* Size of read buffer. */
  341. int readbuf_size;
  342. /* Buffer for writing. */
  343. char *write_buf;
  344. /* Bytes available for writing. */
  345. int write_avail;
  346. /* Size of write buffer. */
  347. int writebuf_size;
  348. };
  349. /*
  350. * tomoyo_globally_readable_file_entry is a structure which is used for holding
  351. * "allow_read" entries.
  352. * It has following fields.
  353. *
  354. * (1) "list" which is linked to tomoyo_globally_readable_list .
  355. * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
  356. * (3) "is_deleted" is a bool which is true if marked as deleted, false
  357. * otherwise.
  358. */
  359. struct tomoyo_globally_readable_file_entry {
  360. struct list_head list;
  361. const struct tomoyo_path_info *filename;
  362. bool is_deleted;
  363. };
  364. /*
  365. * tomoyo_pattern_entry is a structure which is used for holding
  366. * "tomoyo_pattern_list" entries.
  367. * It has following fields.
  368. *
  369. * (1) "list" which is linked to tomoyo_pattern_list .
  370. * (2) "pattern" is a pathname pattern which is used for converting pathnames
  371. * to pathname patterns during learning mode.
  372. * (3) "is_deleted" is a bool which is true if marked as deleted, false
  373. * otherwise.
  374. */
  375. struct tomoyo_pattern_entry {
  376. struct list_head list;
  377. const struct tomoyo_path_info *pattern;
  378. bool is_deleted;
  379. };
  380. /*
  381. * tomoyo_no_rewrite_entry is a structure which is used for holding
  382. * "deny_rewrite" entries.
  383. * It has following fields.
  384. *
  385. * (1) "list" which is linked to tomoyo_no_rewrite_list .
  386. * (2) "pattern" is a pathname which is by default not permitted to modify
  387. * already existing content.
  388. * (3) "is_deleted" is a bool which is true if marked as deleted, false
  389. * otherwise.
  390. */
  391. struct tomoyo_no_rewrite_entry {
  392. struct list_head list;
  393. const struct tomoyo_path_info *pattern;
  394. bool is_deleted;
  395. };
  396. /*
  397. * tomoyo_domain_initializer_entry is a structure which is used for holding
  398. * "initialize_domain" and "no_initialize_domain" entries.
  399. * It has following fields.
  400. *
  401. * (1) "list" which is linked to tomoyo_domain_initializer_list .
  402. * (2) "domainname" which is "a domainname" or "the last component of a
  403. * domainname". This field is NULL if "from" clause is not specified.
  404. * (3) "program" which is a program's pathname.
  405. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  406. * otherwise.
  407. * (5) "is_not" is a bool which is true if "no_initialize_domain", false
  408. * otherwise.
  409. * (6) "is_last_name" is a bool which is true if "domainname" is "the last
  410. * component of a domainname", false otherwise.
  411. */
  412. struct tomoyo_domain_initializer_entry {
  413. struct list_head list;
  414. const struct tomoyo_path_info *domainname; /* This may be NULL */
  415. const struct tomoyo_path_info *program;
  416. bool is_deleted;
  417. bool is_not; /* True if this entry is "no_initialize_domain". */
  418. /* True if the domainname is tomoyo_get_last_name(). */
  419. bool is_last_name;
  420. };
  421. /*
  422. * tomoyo_domain_keeper_entry is a structure which is used for holding
  423. * "keep_domain" and "no_keep_domain" entries.
  424. * It has following fields.
  425. *
  426. * (1) "list" which is linked to tomoyo_domain_keeper_list .
  427. * (2) "domainname" which is "a domainname" or "the last component of a
  428. * domainname".
  429. * (3) "program" which is a program's pathname.
  430. * This field is NULL if "from" clause is not specified.
  431. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  432. * otherwise.
  433. * (5) "is_not" is a bool which is true if "no_initialize_domain", false
  434. * otherwise.
  435. * (6) "is_last_name" is a bool which is true if "domainname" is "the last
  436. * component of a domainname", false otherwise.
  437. */
  438. struct tomoyo_domain_keeper_entry {
  439. struct list_head list;
  440. const struct tomoyo_path_info *domainname;
  441. const struct tomoyo_path_info *program; /* This may be NULL */
  442. bool is_deleted;
  443. bool is_not; /* True if this entry is "no_keep_domain". */
  444. /* True if the domainname is tomoyo_get_last_name(). */
  445. bool is_last_name;
  446. };
  447. /*
  448. * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
  449. * It has following fields.
  450. *
  451. * (1) "list" which is linked to tomoyo_alias_list .
  452. * (2) "original_name" which is a dereferenced pathname.
  453. * (3) "aliased_name" which is a symlink's pathname.
  454. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  455. * otherwise.
  456. */
  457. struct tomoyo_alias_entry {
  458. struct list_head list;
  459. const struct tomoyo_path_info *original_name;
  460. const struct tomoyo_path_info *aliased_name;
  461. bool is_deleted;
  462. };
  463. /*
  464. * tomoyo_policy_manager_entry is a structure which is used for holding list of
  465. * domainnames or programs which are permitted to modify configuration via
  466. * /sys/kernel/security/tomoyo/ interface.
  467. * It has following fields.
  468. *
  469. * (1) "list" which is linked to tomoyo_policy_manager_list .
  470. * (2) "manager" is a domainname or a program's pathname.
  471. * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
  472. * otherwise.
  473. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  474. * otherwise.
  475. */
  476. struct tomoyo_policy_manager_entry {
  477. struct list_head list;
  478. /* A path to program or a domainname. */
  479. const struct tomoyo_path_info *manager;
  480. bool is_domain; /* True if manager is a domainname. */
  481. bool is_deleted; /* True if this entry is deleted. */
  482. };
  483. /********** Function prototypes. **********/
  484. /* Check whether the domain has too many ACL entries to hold. */
  485. bool tomoyo_domain_quota_is_ok(struct tomoyo_domain_info * const domain);
  486. /* Transactional sprintf() for policy dump. */
  487. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  488. __attribute__ ((format(printf, 2, 3)));
  489. /* Check whether the domainname is correct. */
  490. bool tomoyo_is_correct_domain(const unsigned char *domainname);
  491. /* Check whether the token is correct. */
  492. bool tomoyo_is_correct_path(const char *filename, const s8 start_type,
  493. const s8 pattern_type, const s8 end_type);
  494. /* Check whether the token can be a domainname. */
  495. bool tomoyo_is_domain_def(const unsigned char *buffer);
  496. /* Check whether the given filename matches the given pattern. */
  497. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  498. const struct tomoyo_path_info *pattern);
  499. /* Read "alias" entry in exception policy. */
  500. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
  501. /*
  502. * Read "initialize_domain" and "no_initialize_domain" entry
  503. * in exception policy.
  504. */
  505. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
  506. /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
  507. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
  508. /* Read "file_pattern" entry in exception policy. */
  509. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
  510. /* Read "allow_read" entry in exception policy. */
  511. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
  512. /* Read "deny_rewrite" entry in exception policy. */
  513. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
  514. /* Write domain policy violation warning message to console? */
  515. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  516. /* Convert double path operation to operation name. */
  517. const char *tomoyo_path22keyword(const u8 operation);
  518. /* Get the last component of the given domainname. */
  519. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
  520. /* Get warning message. */
  521. const char *tomoyo_get_msg(const bool is_enforce);
  522. /* Convert single path operation to operation name. */
  523. const char *tomoyo_path2keyword(const u8 operation);
  524. /* Create "alias" entry in exception policy. */
  525. int tomoyo_write_alias_policy(char *data, const bool is_delete);
  526. /*
  527. * Create "initialize_domain" and "no_initialize_domain" entry
  528. * in exception policy.
  529. */
  530. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  531. const bool is_delete);
  532. /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
  533. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  534. const bool is_delete);
  535. /*
  536. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  537. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  538. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  539. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  540. * "allow_link" entry in domain policy.
  541. */
  542. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  543. const bool is_delete);
  544. /* Create "allow_read" entry in exception policy. */
  545. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
  546. /* Create "deny_rewrite" entry in exception policy. */
  547. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
  548. /* Create "file_pattern" entry in exception policy. */
  549. int tomoyo_write_pattern_policy(char *data, const bool is_delete);
  550. /* Find a domain by the given name. */
  551. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  552. /* Find or create a domain by the given name. */
  553. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  554. domainname,
  555. const u8 profile);
  556. /* Check mode for specified functionality. */
  557. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  558. const u8 index);
  559. /* Fill in "struct tomoyo_path_info" members. */
  560. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  561. /* Run policy loader when /sbin/init starts. */
  562. void tomoyo_load_policy(const char *filename);
  563. /* Convert binary string to ascii string. */
  564. int tomoyo_encode(char *buffer, int buflen, const char *str);
  565. /* Returns realpath(3) of the given pathname but ignores chroot'ed root. */
  566. int tomoyo_realpath_from_path2(struct path *path, char *newname,
  567. int newname_len);
  568. /*
  569. * Returns realpath(3) of the given pathname but ignores chroot'ed root.
  570. * These functions use kzalloc(), so the caller must call kfree()
  571. * if these functions didn't return NULL.
  572. */
  573. char *tomoyo_realpath(const char *pathname);
  574. /*
  575. * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
  576. */
  577. char *tomoyo_realpath_nofollow(const char *pathname);
  578. /* Same with tomoyo_realpath() except that the pathname is already solved. */
  579. char *tomoyo_realpath_from_path(struct path *path);
  580. /* Check memory quota. */
  581. bool tomoyo_memory_ok(void *ptr);
  582. /*
  583. * Keep the given name on the RAM.
  584. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  585. */
  586. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  587. /* Check for memory usage. */
  588. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  589. /* Set memory quota. */
  590. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  591. /* Initialize realpath related code. */
  592. void __init tomoyo_realpath_init(void);
  593. int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
  594. const struct tomoyo_path_info *filename);
  595. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  596. struct path *path, const int flag);
  597. int tomoyo_path_perm(const u8 operation, struct path *path);
  598. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  599. struct path *path2);
  600. int tomoyo_check_rewrite_permission(struct file *filp);
  601. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  602. /* Run garbage collector. */
  603. void tomoyo_run_gc(void);
  604. void tomoyo_memory_free(void *ptr);
  605. /********** External variable definitions. **********/
  606. /* Lock for GC. */
  607. extern struct srcu_struct tomoyo_ss;
  608. /* The list for "struct tomoyo_domain_info". */
  609. extern struct list_head tomoyo_domain_list;
  610. extern struct list_head tomoyo_domain_initializer_list;
  611. extern struct list_head tomoyo_domain_keeper_list;
  612. extern struct list_head tomoyo_alias_list;
  613. extern struct list_head tomoyo_globally_readable_list;
  614. extern struct list_head tomoyo_pattern_list;
  615. extern struct list_head tomoyo_no_rewrite_list;
  616. extern struct list_head tomoyo_policy_manager_list;
  617. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  618. extern struct mutex tomoyo_name_list_lock;
  619. /* Lock for protecting policy. */
  620. extern struct mutex tomoyo_policy_lock;
  621. /* Has /sbin/init started? */
  622. extern bool tomoyo_policy_loaded;
  623. /* The kernel's domain. */
  624. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  625. /********** Inlined functions. **********/
  626. static inline int tomoyo_read_lock(void)
  627. {
  628. return srcu_read_lock(&tomoyo_ss);
  629. }
  630. static inline void tomoyo_read_unlock(int idx)
  631. {
  632. srcu_read_unlock(&tomoyo_ss, idx);
  633. }
  634. /* strcmp() for "struct tomoyo_path_info" structure. */
  635. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  636. const struct tomoyo_path_info *b)
  637. {
  638. return a->hash != b->hash || strcmp(a->name, b->name);
  639. }
  640. /**
  641. * tomoyo_is_valid - Check whether the character is a valid char.
  642. *
  643. * @c: The character to check.
  644. *
  645. * Returns true if @c is a valid character, false otherwise.
  646. */
  647. static inline bool tomoyo_is_valid(const unsigned char c)
  648. {
  649. return c > ' ' && c < 127;
  650. }
  651. /**
  652. * tomoyo_is_invalid - Check whether the character is an invalid char.
  653. *
  654. * @c: The character to check.
  655. *
  656. * Returns true if @c is an invalid character, false otherwise.
  657. */
  658. static inline bool tomoyo_is_invalid(const unsigned char c)
  659. {
  660. return c && (c <= ' ' || c >= 127);
  661. }
  662. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  663. {
  664. if (name) {
  665. struct tomoyo_name_entry *ptr =
  666. container_of(name, struct tomoyo_name_entry, entry);
  667. atomic_dec(&ptr->users);
  668. }
  669. }
  670. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  671. {
  672. return current_cred()->security;
  673. }
  674. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  675. *task)
  676. {
  677. return task_cred_xxx(task, security);
  678. }
  679. /**
  680. * list_for_each_cookie - iterate over a list with cookie.
  681. * @pos: the &struct list_head to use as a loop cursor.
  682. * @cookie: the &struct list_head to use as a cookie.
  683. * @head: the head for your list.
  684. *
  685. * Same with list_for_each_rcu() except that this primitive uses @cookie
  686. * so that we can continue iteration.
  687. * @cookie must be NULL when iteration starts, and @cookie will become
  688. * NULL when iteration finishes.
  689. */
  690. #define list_for_each_cookie(pos, cookie, head) \
  691. for (({ if (!cookie) \
  692. cookie = head; }), \
  693. pos = rcu_dereference((cookie)->next); \
  694. prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
  695. (cookie) = pos, pos = rcu_dereference(pos->next))
  696. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */