common.h 28 KB

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