common.h 32 KB

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