common.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. #include <linux/poll.h>
  22. struct linux_binprm;
  23. /********** Constants definitions. **********/
  24. /*
  25. * TOMOYO uses this hash only when appending a string into the string
  26. * table. Frequency of appending strings is very low. So we don't need
  27. * large (e.g. 64k) hash size. 256 will be sufficient.
  28. */
  29. #define TOMOYO_HASH_BITS 8
  30. #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
  31. #define TOMOYO_EXEC_TMPSIZE 4096
  32. /* Profile number is an integer between 0 and 255. */
  33. #define TOMOYO_MAX_PROFILES 256
  34. /* Group number is an integer between 0 and 255. */
  35. #define TOMOYO_MAX_ACL_GROUPS 256
  36. /* Index numbers for operation mode. */
  37. enum tomoyo_mode_index {
  38. TOMOYO_CONFIG_DISABLED,
  39. TOMOYO_CONFIG_LEARNING,
  40. TOMOYO_CONFIG_PERMISSIVE,
  41. TOMOYO_CONFIG_ENFORCING,
  42. TOMOYO_CONFIG_MAX_MODE,
  43. TOMOYO_CONFIG_WANT_REJECT_LOG = 64,
  44. TOMOYO_CONFIG_WANT_GRANT_LOG = 128,
  45. TOMOYO_CONFIG_USE_DEFAULT = 255,
  46. };
  47. /* Index numbers for entry type. */
  48. enum tomoyo_policy_id {
  49. TOMOYO_ID_GROUP,
  50. TOMOYO_ID_PATH_GROUP,
  51. TOMOYO_ID_NUMBER_GROUP,
  52. TOMOYO_ID_TRANSITION_CONTROL,
  53. TOMOYO_ID_AGGREGATOR,
  54. TOMOYO_ID_MANAGER,
  55. TOMOYO_ID_NAME,
  56. TOMOYO_ID_ACL,
  57. TOMOYO_ID_DOMAIN,
  58. TOMOYO_MAX_POLICY
  59. };
  60. /* Index numbers for group entries. */
  61. enum tomoyo_group_id {
  62. TOMOYO_PATH_GROUP,
  63. TOMOYO_NUMBER_GROUP,
  64. TOMOYO_MAX_GROUP
  65. };
  66. /* Index numbers for type of numeric values. */
  67. enum tomoyo_value_type {
  68. TOMOYO_VALUE_TYPE_INVALID,
  69. TOMOYO_VALUE_TYPE_DECIMAL,
  70. TOMOYO_VALUE_TYPE_OCTAL,
  71. TOMOYO_VALUE_TYPE_HEXADECIMAL,
  72. };
  73. /* Index numbers for domain transition control keywords. */
  74. enum tomoyo_transition_type {
  75. /* Do not change this order, */
  76. TOMOYO_TRANSITION_CONTROL_NO_RESET,
  77. TOMOYO_TRANSITION_CONTROL_RESET,
  78. TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
  79. TOMOYO_TRANSITION_CONTROL_INITIALIZE,
  80. TOMOYO_TRANSITION_CONTROL_NO_KEEP,
  81. TOMOYO_TRANSITION_CONTROL_KEEP,
  82. TOMOYO_MAX_TRANSITION_TYPE
  83. };
  84. /* Index numbers for Access Controls. */
  85. enum tomoyo_acl_entry_type_index {
  86. TOMOYO_TYPE_PATH_ACL,
  87. TOMOYO_TYPE_PATH2_ACL,
  88. TOMOYO_TYPE_PATH_NUMBER_ACL,
  89. TOMOYO_TYPE_MKDEV_ACL,
  90. TOMOYO_TYPE_MOUNT_ACL,
  91. };
  92. /* Index numbers for access controls with one pathname. */
  93. enum tomoyo_path_acl_index {
  94. TOMOYO_TYPE_EXECUTE,
  95. TOMOYO_TYPE_READ,
  96. TOMOYO_TYPE_WRITE,
  97. TOMOYO_TYPE_APPEND,
  98. TOMOYO_TYPE_UNLINK,
  99. TOMOYO_TYPE_GETATTR,
  100. TOMOYO_TYPE_RMDIR,
  101. TOMOYO_TYPE_TRUNCATE,
  102. TOMOYO_TYPE_SYMLINK,
  103. TOMOYO_TYPE_CHROOT,
  104. TOMOYO_TYPE_UMOUNT,
  105. TOMOYO_MAX_PATH_OPERATION
  106. };
  107. enum tomoyo_memory_stat_type {
  108. TOMOYO_MEMORY_POLICY,
  109. TOMOYO_MEMORY_AUDIT,
  110. TOMOYO_MEMORY_QUERY,
  111. TOMOYO_MAX_MEMORY_STAT
  112. };
  113. enum tomoyo_mkdev_acl_index {
  114. TOMOYO_TYPE_MKBLOCK,
  115. TOMOYO_TYPE_MKCHAR,
  116. TOMOYO_MAX_MKDEV_OPERATION
  117. };
  118. /* Index numbers for access controls with two pathnames. */
  119. enum tomoyo_path2_acl_index {
  120. TOMOYO_TYPE_LINK,
  121. TOMOYO_TYPE_RENAME,
  122. TOMOYO_TYPE_PIVOT_ROOT,
  123. TOMOYO_MAX_PATH2_OPERATION
  124. };
  125. /* Index numbers for access controls with one pathname and one number. */
  126. enum tomoyo_path_number_acl_index {
  127. TOMOYO_TYPE_CREATE,
  128. TOMOYO_TYPE_MKDIR,
  129. TOMOYO_TYPE_MKFIFO,
  130. TOMOYO_TYPE_MKSOCK,
  131. TOMOYO_TYPE_IOCTL,
  132. TOMOYO_TYPE_CHMOD,
  133. TOMOYO_TYPE_CHOWN,
  134. TOMOYO_TYPE_CHGRP,
  135. TOMOYO_MAX_PATH_NUMBER_OPERATION
  136. };
  137. /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
  138. enum tomoyo_securityfs_interface_index {
  139. TOMOYO_DOMAINPOLICY,
  140. TOMOYO_EXCEPTIONPOLICY,
  141. TOMOYO_DOMAIN_STATUS,
  142. TOMOYO_PROCESS_STATUS,
  143. TOMOYO_MEMINFO,
  144. TOMOYO_SELFDOMAIN,
  145. TOMOYO_AUDIT,
  146. TOMOYO_VERSION,
  147. TOMOYO_PROFILE,
  148. TOMOYO_QUERY,
  149. TOMOYO_MANAGER
  150. };
  151. /* Index numbers for special mount operations. */
  152. enum tomoyo_special_mount {
  153. TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
  154. TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
  155. TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
  156. TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
  157. TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
  158. TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
  159. TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
  160. TOMOYO_MAX_SPECIAL_MOUNT
  161. };
  162. /* Index numbers for functionality. */
  163. enum tomoyo_mac_index {
  164. TOMOYO_MAC_FILE_EXECUTE,
  165. TOMOYO_MAC_FILE_OPEN,
  166. TOMOYO_MAC_FILE_CREATE,
  167. TOMOYO_MAC_FILE_UNLINK,
  168. TOMOYO_MAC_FILE_GETATTR,
  169. TOMOYO_MAC_FILE_MKDIR,
  170. TOMOYO_MAC_FILE_RMDIR,
  171. TOMOYO_MAC_FILE_MKFIFO,
  172. TOMOYO_MAC_FILE_MKSOCK,
  173. TOMOYO_MAC_FILE_TRUNCATE,
  174. TOMOYO_MAC_FILE_SYMLINK,
  175. TOMOYO_MAC_FILE_MKBLOCK,
  176. TOMOYO_MAC_FILE_MKCHAR,
  177. TOMOYO_MAC_FILE_LINK,
  178. TOMOYO_MAC_FILE_RENAME,
  179. TOMOYO_MAC_FILE_CHMOD,
  180. TOMOYO_MAC_FILE_CHOWN,
  181. TOMOYO_MAC_FILE_CHGRP,
  182. TOMOYO_MAC_FILE_IOCTL,
  183. TOMOYO_MAC_FILE_CHROOT,
  184. TOMOYO_MAC_FILE_MOUNT,
  185. TOMOYO_MAC_FILE_UMOUNT,
  186. TOMOYO_MAC_FILE_PIVOT_ROOT,
  187. TOMOYO_MAX_MAC_INDEX
  188. };
  189. /* Index numbers for category of functionality. */
  190. enum tomoyo_mac_category_index {
  191. TOMOYO_MAC_CATEGORY_FILE,
  192. TOMOYO_MAX_MAC_CATEGORY_INDEX
  193. };
  194. /*
  195. * Retry this request. Returned by tomoyo_supervisor() if policy violation has
  196. * occurred in enforcing mode and the userspace daemon decided to retry.
  197. *
  198. * We must choose a positive value in order to distinguish "granted" (which is
  199. * 0) and "rejected" (which is a negative value) and "retry".
  200. */
  201. #define TOMOYO_RETRY_REQUEST 1
  202. /* Index numbers for profile's PREFERENCE values. */
  203. enum tomoyo_pref_index {
  204. TOMOYO_PREF_MAX_AUDIT_LOG,
  205. TOMOYO_PREF_MAX_LEARNING_ENTRY,
  206. TOMOYO_MAX_PREF
  207. };
  208. /********** Structure definitions. **********/
  209. /* Common header for holding ACL entries. */
  210. struct tomoyo_acl_head {
  211. struct list_head list;
  212. bool is_deleted;
  213. } __packed;
  214. /* Common header for shared entries. */
  215. struct tomoyo_shared_acl_head {
  216. struct list_head list;
  217. atomic_t users;
  218. } __packed;
  219. struct tomoyo_policy_namespace;
  220. /* Structure for request info. */
  221. struct tomoyo_request_info {
  222. struct tomoyo_domain_info *domain;
  223. /* For holding parameters. */
  224. union {
  225. struct {
  226. const struct tomoyo_path_info *filename;
  227. /* For using wildcards at tomoyo_find_next_domain(). */
  228. const struct tomoyo_path_info *matched_path;
  229. /* One of values in "enum tomoyo_path_acl_index". */
  230. u8 operation;
  231. } path;
  232. struct {
  233. const struct tomoyo_path_info *filename1;
  234. const struct tomoyo_path_info *filename2;
  235. /* One of values in "enum tomoyo_path2_acl_index". */
  236. u8 operation;
  237. } path2;
  238. struct {
  239. const struct tomoyo_path_info *filename;
  240. unsigned int mode;
  241. unsigned int major;
  242. unsigned int minor;
  243. /* One of values in "enum tomoyo_mkdev_acl_index". */
  244. u8 operation;
  245. } mkdev;
  246. struct {
  247. const struct tomoyo_path_info *filename;
  248. unsigned long number;
  249. /*
  250. * One of values in
  251. * "enum tomoyo_path_number_acl_index".
  252. */
  253. u8 operation;
  254. } path_number;
  255. struct {
  256. const struct tomoyo_path_info *type;
  257. const struct tomoyo_path_info *dir;
  258. const struct tomoyo_path_info *dev;
  259. unsigned long flags;
  260. int need_dev;
  261. } mount;
  262. } param;
  263. u8 param_type;
  264. bool granted;
  265. u8 retry;
  266. u8 profile;
  267. u8 mode; /* One of tomoyo_mode_index . */
  268. u8 type;
  269. };
  270. /* Structure for holding a token. */
  271. struct tomoyo_path_info {
  272. const char *name;
  273. u32 hash; /* = full_name_hash(name, strlen(name)) */
  274. u16 const_len; /* = tomoyo_const_part_length(name) */
  275. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  276. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  277. };
  278. /* Structure for holding string data. */
  279. struct tomoyo_name {
  280. struct tomoyo_shared_acl_head head;
  281. struct tomoyo_path_info entry;
  282. };
  283. /* Structure for holding a word. */
  284. struct tomoyo_name_union {
  285. /* Either @filename or @group is NULL. */
  286. const struct tomoyo_path_info *filename;
  287. struct tomoyo_group *group;
  288. };
  289. /* Structure for holding a number. */
  290. struct tomoyo_number_union {
  291. unsigned long values[2];
  292. struct tomoyo_group *group; /* Maybe NULL. */
  293. /* One of values in "enum tomoyo_value_type". */
  294. u8 value_type[2];
  295. };
  296. /* Structure for "path_group"/"number_group" directive. */
  297. struct tomoyo_group {
  298. struct tomoyo_shared_acl_head head;
  299. const struct tomoyo_path_info *group_name;
  300. struct list_head member_list;
  301. };
  302. /* Structure for "path_group" directive. */
  303. struct tomoyo_path_group {
  304. struct tomoyo_acl_head head;
  305. const struct tomoyo_path_info *member_name;
  306. };
  307. /* Structure for "number_group" directive. */
  308. struct tomoyo_number_group {
  309. struct tomoyo_acl_head head;
  310. struct tomoyo_number_union number;
  311. };
  312. /* Common header for individual entries. */
  313. struct tomoyo_acl_info {
  314. struct list_head list;
  315. bool is_deleted;
  316. u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
  317. } __packed;
  318. /* Structure for domain information. */
  319. struct tomoyo_domain_info {
  320. struct list_head list;
  321. struct list_head acl_info_list;
  322. /* Name of this domain. Never NULL. */
  323. const struct tomoyo_path_info *domainname;
  324. /* Namespace for this domain. Never NULL. */
  325. struct tomoyo_policy_namespace *ns;
  326. u8 profile; /* Profile number to use. */
  327. u8 group; /* Group number to use. */
  328. bool is_deleted; /* Delete flag. */
  329. bool quota_warned; /* Quota warnning flag. */
  330. bool transition_failed; /* Domain transition failed flag. */
  331. atomic_t users; /* Number of referring credentials. */
  332. };
  333. /*
  334. * Structure for "file execute", "file read", "file write", "file append",
  335. * "file unlink", "file getattr", "file rmdir", "file truncate",
  336. * "file symlink", "file chroot" and "file unmount" directive.
  337. */
  338. struct tomoyo_path_acl {
  339. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  340. u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
  341. struct tomoyo_name_union name;
  342. };
  343. /*
  344. * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
  345. * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
  346. */
  347. struct tomoyo_path_number_acl {
  348. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
  349. /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
  350. u8 perm;
  351. struct tomoyo_name_union name;
  352. struct tomoyo_number_union number;
  353. };
  354. /* Structure for "file mkblock" and "file mkchar" directive. */
  355. struct tomoyo_mkdev_acl {
  356. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
  357. u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
  358. struct tomoyo_name_union name;
  359. struct tomoyo_number_union mode;
  360. struct tomoyo_number_union major;
  361. struct tomoyo_number_union minor;
  362. };
  363. /*
  364. * Structure for "file rename", "file link" and "file pivot_root" directive.
  365. */
  366. struct tomoyo_path2_acl {
  367. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  368. u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
  369. struct tomoyo_name_union name1;
  370. struct tomoyo_name_union name2;
  371. };
  372. /* Structure for "file mount" directive. */
  373. struct tomoyo_mount_acl {
  374. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
  375. struct tomoyo_name_union dev_name;
  376. struct tomoyo_name_union dir_name;
  377. struct tomoyo_name_union fs_type;
  378. struct tomoyo_number_union flags;
  379. };
  380. /* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
  381. struct tomoyo_acl_param {
  382. char *data;
  383. struct list_head *list;
  384. struct tomoyo_policy_namespace *ns;
  385. bool is_delete;
  386. };
  387. #define TOMOYO_MAX_IO_READ_QUEUE 64
  388. /*
  389. * Structure for reading/writing policy via /sys/kernel/security/tomoyo
  390. * interfaces.
  391. */
  392. struct tomoyo_io_buffer {
  393. void (*read) (struct tomoyo_io_buffer *);
  394. int (*write) (struct tomoyo_io_buffer *);
  395. int (*poll) (struct file *file, poll_table *wait);
  396. /* Exclusive lock for this structure. */
  397. struct mutex io_sem;
  398. char __user *read_user_buf;
  399. int read_user_buf_avail;
  400. struct {
  401. struct list_head *ns;
  402. struct list_head *domain;
  403. struct list_head *group;
  404. struct list_head *acl;
  405. int avail;
  406. int step;
  407. int query_index;
  408. u16 index;
  409. u8 acl_group_index;
  410. u8 bit;
  411. u8 w_pos;
  412. bool eof;
  413. bool print_this_domain_only;
  414. bool print_transition_related_only;
  415. const char *w[TOMOYO_MAX_IO_READ_QUEUE];
  416. } r;
  417. struct {
  418. struct tomoyo_policy_namespace *ns;
  419. /* The position currently writing to. */
  420. struct tomoyo_domain_info *domain;
  421. /* Bytes available for writing. */
  422. int avail;
  423. bool is_delete;
  424. } w;
  425. /* Buffer for reading. */
  426. char *read_buf;
  427. /* Size of read buffer. */
  428. int readbuf_size;
  429. /* Buffer for writing. */
  430. char *write_buf;
  431. /* Size of write buffer. */
  432. int writebuf_size;
  433. /* Type of this interface. */
  434. u8 type;
  435. /* Users counter protected by tomoyo_io_buffer_list_lock. */
  436. u8 users;
  437. /* List for telling GC not to kfree() elements. */
  438. struct list_head list;
  439. };
  440. /*
  441. * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
  442. * "no_keep_domain" keyword.
  443. */
  444. struct tomoyo_transition_control {
  445. struct tomoyo_acl_head head;
  446. u8 type; /* One of values in "enum tomoyo_transition_type". */
  447. /* True if the domainname is tomoyo_get_last_name(). */
  448. bool is_last_name;
  449. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  450. const struct tomoyo_path_info *program; /* Maybe NULL */
  451. };
  452. /* Structure for "aggregator" keyword. */
  453. struct tomoyo_aggregator {
  454. struct tomoyo_acl_head head;
  455. const struct tomoyo_path_info *original_name;
  456. const struct tomoyo_path_info *aggregated_name;
  457. };
  458. /* Structure for policy manager. */
  459. struct tomoyo_manager {
  460. struct tomoyo_acl_head head;
  461. bool is_domain; /* True if manager is a domainname. */
  462. /* A path to program or a domainname. */
  463. const struct tomoyo_path_info *manager;
  464. };
  465. struct tomoyo_preference {
  466. unsigned int learning_max_entry;
  467. bool enforcing_verbose;
  468. bool learning_verbose;
  469. bool permissive_verbose;
  470. };
  471. /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
  472. struct tomoyo_profile {
  473. const struct tomoyo_path_info *comment;
  474. struct tomoyo_preference *learning;
  475. struct tomoyo_preference *permissive;
  476. struct tomoyo_preference *enforcing;
  477. struct tomoyo_preference preference;
  478. u8 default_config;
  479. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  480. unsigned int pref[TOMOYO_MAX_PREF];
  481. };
  482. /* Structure for representing YYYY/MM/DD hh/mm/ss. */
  483. struct tomoyo_time {
  484. u16 year;
  485. u8 month;
  486. u8 day;
  487. u8 hour;
  488. u8 min;
  489. u8 sec;
  490. };
  491. /* Structure for policy namespace. */
  492. struct tomoyo_policy_namespace {
  493. /* Profile table. Memory is allocated as needed. */
  494. struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
  495. /* List of "struct tomoyo_group". */
  496. struct list_head group_list[TOMOYO_MAX_GROUP];
  497. /* List of policy. */
  498. struct list_head policy_list[TOMOYO_MAX_POLICY];
  499. /* The global ACL referred by "use_group" keyword. */
  500. struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
  501. /* List for connecting to tomoyo_namespace_list list. */
  502. struct list_head namespace_list;
  503. /* Profile version. Currently only 20100903 is defined. */
  504. unsigned int profile_version;
  505. /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
  506. const char *name;
  507. };
  508. /********** Function prototypes. **********/
  509. void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
  510. bool tomoyo_str_starts(char **src, const char *find);
  511. const char *tomoyo_get_exe(void);
  512. void tomoyo_normalize_line(unsigned char *buffer);
  513. void tomoyo_check_profile(void);
  514. int tomoyo_open_control(const u8 type, struct file *file);
  515. int tomoyo_close_control(struct tomoyo_io_buffer *head);
  516. int tomoyo_poll_control(struct file *file, poll_table *wait);
  517. int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  518. const int buffer_len);
  519. int tomoyo_write_control(struct tomoyo_io_buffer *head,
  520. const char __user *buffer, const int buffer_len);
  521. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  522. void tomoyo_warn_oom(const char *function);
  523. const struct tomoyo_path_info *
  524. tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  525. const struct tomoyo_name_union *ptr);
  526. bool tomoyo_compare_number_union(const unsigned long value,
  527. const struct tomoyo_number_union *ptr);
  528. int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
  529. const u8 index);
  530. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  531. __attribute__ ((format(printf, 2, 3)));
  532. bool tomoyo_correct_domain(const unsigned char *domainname);
  533. bool tomoyo_correct_path(const char *filename);
  534. bool tomoyo_correct_word(const char *string);
  535. bool tomoyo_domain_def(const unsigned char *buffer);
  536. bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
  537. struct tomoyo_name_union *ptr);
  538. const struct tomoyo_path_info *
  539. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  540. const struct tomoyo_group *group);
  541. bool tomoyo_number_matches_group(const unsigned long min,
  542. const unsigned long max,
  543. const struct tomoyo_group *group);
  544. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  545. const struct tomoyo_path_info *pattern);
  546. bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
  547. struct tomoyo_number_union *ptr);
  548. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  549. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  550. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  551. struct tomoyo_domain_info *domain,
  552. const u8 index);
  553. int tomoyo_mount_permission(char *dev_name, struct path *path,
  554. const char *type, unsigned long flags,
  555. void *data_page);
  556. int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
  557. int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
  558. const u8 type);
  559. int tomoyo_write_file(struct tomoyo_acl_param *param);
  560. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
  561. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  562. __attribute__ ((format(printf, 2, 3)));
  563. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  564. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  565. const bool transit);
  566. struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
  567. const u8 profile);
  568. struct tomoyo_policy_namespace *tomoyo_assign_namespace
  569. (const char *domainname);
  570. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  571. const u8 idx);
  572. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  573. const u8 index);
  574. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  575. void tomoyo_load_policy(const char *filename);
  576. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  577. char *tomoyo_encode(const char *str);
  578. char *tomoyo_realpath_nofollow(const char *pathname);
  579. char *tomoyo_realpath_from_path(struct path *path);
  580. bool tomoyo_memory_ok(void *ptr);
  581. void *tomoyo_commit_ok(void *data, const unsigned int size);
  582. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  583. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  584. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  585. void __init tomoyo_mm_init(void);
  586. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  587. const struct tomoyo_path_info *filename);
  588. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  589. struct path *path, const int flag);
  590. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  591. unsigned long number);
  592. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  593. const unsigned int mode, unsigned int dev);
  594. int tomoyo_path_perm(const u8 operation, struct path *path);
  595. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  596. struct path *path2);
  597. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  598. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  599. const unsigned long value, const u8 type);
  600. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  601. void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
  602. void tomoyo_memory_free(void *ptr);
  603. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  604. struct tomoyo_acl_param *param,
  605. bool (*check_duplicate) (const struct tomoyo_acl_info
  606. *,
  607. const struct tomoyo_acl_info
  608. *),
  609. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  610. struct tomoyo_acl_info *,
  611. const bool));
  612. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  613. struct tomoyo_acl_param *param,
  614. bool (*check_duplicate) (const struct tomoyo_acl_head
  615. *,
  616. const struct tomoyo_acl_head
  617. *));
  618. void tomoyo_check_acl(struct tomoyo_request_info *r,
  619. bool (*check_entry) (struct tomoyo_request_info *,
  620. const struct tomoyo_acl_info *));
  621. char *tomoyo_read_token(struct tomoyo_acl_param *param);
  622. bool tomoyo_permstr(const char *string, const char *keyword);
  623. const char *tomoyo_yesno(const unsigned int value);
  624. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  625. __attribute__ ((format(printf, 2, 3)));
  626. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  627. va_list args);
  628. void tomoyo_read_log(struct tomoyo_io_buffer *head);
  629. int tomoyo_poll_log(struct file *file, poll_table *wait);
  630. char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
  631. va_list args);
  632. /********** External variable definitions. **********/
  633. /* Lock for GC. */
  634. extern struct srcu_struct tomoyo_ss;
  635. /* The list for "struct tomoyo_domain_info". */
  636. extern struct list_head tomoyo_domain_list;
  637. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  638. /* Lock for protecting policy. */
  639. extern struct mutex tomoyo_policy_lock;
  640. /* Has /sbin/init started? */
  641. extern bool tomoyo_policy_loaded;
  642. /* The kernel's domain. */
  643. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  644. extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  645. extern struct list_head tomoyo_namespace_list;
  646. extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  647. extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
  648. extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
  649. extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  650. extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
  651. extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
  652. extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  653. extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
  654. extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  655. extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  656. /********** Inlined functions. **********/
  657. /**
  658. * tomoyo_read_lock - Take lock for protecting policy.
  659. *
  660. * Returns index number for tomoyo_read_unlock().
  661. */
  662. static inline int tomoyo_read_lock(void)
  663. {
  664. return srcu_read_lock(&tomoyo_ss);
  665. }
  666. /**
  667. * tomoyo_read_unlock - Release lock for protecting policy.
  668. *
  669. * @idx: Index number returned by tomoyo_read_lock().
  670. *
  671. * Returns nothing.
  672. */
  673. static inline void tomoyo_read_unlock(int idx)
  674. {
  675. srcu_read_unlock(&tomoyo_ss, idx);
  676. }
  677. /**
  678. * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
  679. *
  680. * @a: Pointer to "struct tomoyo_path_info".
  681. * @b: Pointer to "struct tomoyo_path_info".
  682. *
  683. * Returns true if @a == @b, false otherwise.
  684. */
  685. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  686. const struct tomoyo_path_info *b)
  687. {
  688. return a->hash != b->hash || strcmp(a->name, b->name);
  689. }
  690. /**
  691. * tomoyo_put_name - Drop reference on "struct tomoyo_name".
  692. *
  693. * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
  694. *
  695. * Returns nothing.
  696. */
  697. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  698. {
  699. if (name) {
  700. struct tomoyo_name *ptr =
  701. container_of(name, typeof(*ptr), entry);
  702. atomic_dec(&ptr->head.users);
  703. }
  704. }
  705. /**
  706. * tomoyo_put_group - Drop reference on "struct tomoyo_group".
  707. *
  708. * @group: Pointer to "struct tomoyo_group". Maybe NULL.
  709. *
  710. * Returns nothing.
  711. */
  712. static inline void tomoyo_put_group(struct tomoyo_group *group)
  713. {
  714. if (group)
  715. atomic_dec(&group->head.users);
  716. }
  717. /**
  718. * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
  719. *
  720. * Returns pointer to "struct tomoyo_domain_info" for current thread.
  721. */
  722. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  723. {
  724. return current_cred()->security;
  725. }
  726. /**
  727. * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
  728. *
  729. * @task: Pointer to "struct task_struct".
  730. *
  731. * Returns pointer to "struct tomoyo_security" for specified thread.
  732. */
  733. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  734. *task)
  735. {
  736. return task_cred_xxx(task, security);
  737. }
  738. /**
  739. * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
  740. *
  741. * @a: Pointer to "struct tomoyo_name_union".
  742. * @b: Pointer to "struct tomoyo_name_union".
  743. *
  744. * Returns true if @a == @b, false otherwise.
  745. */
  746. static inline bool tomoyo_same_name_union
  747. (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
  748. {
  749. return a->filename == b->filename && a->group == b->group;
  750. }
  751. /**
  752. * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
  753. *
  754. * @a: Pointer to "struct tomoyo_number_union".
  755. * @b: Pointer to "struct tomoyo_number_union".
  756. *
  757. * Returns true if @a == @b, false otherwise.
  758. */
  759. static inline bool tomoyo_same_number_union
  760. (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
  761. {
  762. return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
  763. a->group == b->group && a->value_type[0] == b->value_type[0] &&
  764. a->value_type[1] == b->value_type[1];
  765. }
  766. /**
  767. * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
  768. *
  769. * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
  770. */
  771. static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
  772. {
  773. return tomoyo_domain()->ns;
  774. }
  775. #if defined(CONFIG_SLOB)
  776. /**
  777. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  778. *
  779. * @size: Size to be rounded up.
  780. *
  781. * Returns @size.
  782. *
  783. * Since SLOB does not round up, this function simply returns @size.
  784. */
  785. static inline int tomoyo_round2(size_t size)
  786. {
  787. return size;
  788. }
  789. #else
  790. /**
  791. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  792. *
  793. * @size: Size to be rounded up.
  794. *
  795. * Returns rounded size.
  796. *
  797. * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
  798. * (e.g.) 128 bytes.
  799. */
  800. static inline int tomoyo_round2(size_t size)
  801. {
  802. #if PAGE_SIZE == 4096
  803. size_t bsize = 32;
  804. #else
  805. size_t bsize = 64;
  806. #endif
  807. if (!size)
  808. return 0;
  809. while (size > bsize)
  810. bsize <<= 1;
  811. return bsize;
  812. }
  813. #endif
  814. /**
  815. * list_for_each_cookie - iterate over a list with cookie.
  816. * @pos: the &struct list_head to use as a loop cursor.
  817. * @head: the head for your list.
  818. */
  819. #define list_for_each_cookie(pos, head) \
  820. if (!pos) \
  821. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  822. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  823. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */