common.h 33 KB

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