common.h 30 KB

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