common.h 28 KB

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