common.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. /* Index returned by tomoyo_read_lock(). */
  399. int reader_idx;
  400. char __user *read_user_buf;
  401. int read_user_buf_avail;
  402. struct {
  403. struct list_head *ns;
  404. struct list_head *domain;
  405. struct list_head *group;
  406. struct list_head *acl;
  407. int avail;
  408. int step;
  409. int query_index;
  410. u16 index;
  411. u8 acl_group_index;
  412. u8 bit;
  413. u8 w_pos;
  414. bool eof;
  415. bool print_this_domain_only;
  416. bool print_transition_related_only;
  417. const char *w[TOMOYO_MAX_IO_READ_QUEUE];
  418. } r;
  419. struct {
  420. struct tomoyo_policy_namespace *ns;
  421. /* The position currently writing to. */
  422. struct tomoyo_domain_info *domain;
  423. /* Bytes available for writing. */
  424. int avail;
  425. bool is_delete;
  426. } w;
  427. /* Buffer for reading. */
  428. char *read_buf;
  429. /* Size of read buffer. */
  430. int readbuf_size;
  431. /* Buffer for writing. */
  432. char *write_buf;
  433. /* Size of write buffer. */
  434. int writebuf_size;
  435. /* Type of this interface. */
  436. u8 type;
  437. };
  438. /*
  439. * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
  440. * "no_keep_domain" keyword.
  441. */
  442. struct tomoyo_transition_control {
  443. struct tomoyo_acl_head head;
  444. u8 type; /* One of values in "enum tomoyo_transition_type". */
  445. /* True if the domainname is tomoyo_get_last_name(). */
  446. bool is_last_name;
  447. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  448. const struct tomoyo_path_info *program; /* Maybe NULL */
  449. };
  450. /* Structure for "aggregator" keyword. */
  451. struct tomoyo_aggregator {
  452. struct tomoyo_acl_head head;
  453. const struct tomoyo_path_info *original_name;
  454. const struct tomoyo_path_info *aggregated_name;
  455. };
  456. /* Structure for policy manager. */
  457. struct tomoyo_manager {
  458. struct tomoyo_acl_head head;
  459. bool is_domain; /* True if manager is a domainname. */
  460. /* A path to program or a domainname. */
  461. const struct tomoyo_path_info *manager;
  462. };
  463. struct tomoyo_preference {
  464. unsigned int learning_max_entry;
  465. bool enforcing_verbose;
  466. bool learning_verbose;
  467. bool permissive_verbose;
  468. };
  469. /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
  470. struct tomoyo_profile {
  471. const struct tomoyo_path_info *comment;
  472. struct tomoyo_preference *learning;
  473. struct tomoyo_preference *permissive;
  474. struct tomoyo_preference *enforcing;
  475. struct tomoyo_preference preference;
  476. u8 default_config;
  477. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  478. unsigned int pref[TOMOYO_MAX_PREF];
  479. };
  480. /* Structure for representing YYYY/MM/DD hh/mm/ss. */
  481. struct tomoyo_time {
  482. u16 year;
  483. u8 month;
  484. u8 day;
  485. u8 hour;
  486. u8 min;
  487. u8 sec;
  488. };
  489. /* Structure for policy namespace. */
  490. struct tomoyo_policy_namespace {
  491. /* Profile table. Memory is allocated as needed. */
  492. struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
  493. /* List of "struct tomoyo_group". */
  494. struct list_head group_list[TOMOYO_MAX_GROUP];
  495. /* List of policy. */
  496. struct list_head policy_list[TOMOYO_MAX_POLICY];
  497. /* The global ACL referred by "use_group" keyword. */
  498. struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
  499. /* List for connecting to tomoyo_namespace_list list. */
  500. struct list_head namespace_list;
  501. /* Profile version. Currently only 20100903 is defined. */
  502. unsigned int profile_version;
  503. /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
  504. const char *name;
  505. };
  506. /********** Function prototypes. **********/
  507. void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
  508. bool tomoyo_str_starts(char **src, const char *find);
  509. const char *tomoyo_get_exe(void);
  510. void tomoyo_normalize_line(unsigned char *buffer);
  511. void tomoyo_check_profile(void);
  512. int tomoyo_open_control(const u8 type, struct file *file);
  513. int tomoyo_close_control(struct tomoyo_io_buffer *head);
  514. int tomoyo_poll_control(struct file *file, poll_table *wait);
  515. int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  516. const int buffer_len);
  517. int tomoyo_write_control(struct tomoyo_io_buffer *head,
  518. const char __user *buffer, const int buffer_len);
  519. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  520. void tomoyo_warn_oom(const char *function);
  521. const struct tomoyo_path_info *
  522. tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  523. const struct tomoyo_name_union *ptr);
  524. bool tomoyo_compare_number_union(const unsigned long value,
  525. const struct tomoyo_number_union *ptr);
  526. int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
  527. const u8 index);
  528. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  529. __attribute__ ((format(printf, 2, 3)));
  530. bool tomoyo_correct_domain(const unsigned char *domainname);
  531. bool tomoyo_correct_path(const char *filename);
  532. bool tomoyo_correct_word(const char *string);
  533. bool tomoyo_domain_def(const unsigned char *buffer);
  534. bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
  535. struct tomoyo_name_union *ptr);
  536. const struct tomoyo_path_info *
  537. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  538. const struct tomoyo_group *group);
  539. bool tomoyo_number_matches_group(const unsigned long min,
  540. const unsigned long max,
  541. const struct tomoyo_group *group);
  542. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  543. const struct tomoyo_path_info *pattern);
  544. bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
  545. struct tomoyo_number_union *ptr);
  546. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  547. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  548. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  549. struct tomoyo_domain_info *domain,
  550. const u8 index);
  551. int tomoyo_mount_permission(char *dev_name, struct path *path,
  552. const char *type, unsigned long flags,
  553. void *data_page);
  554. int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
  555. int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
  556. const u8 type);
  557. int tomoyo_write_file(struct tomoyo_acl_param *param);
  558. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
  559. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  560. __attribute__ ((format(printf, 2, 3)));
  561. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  562. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  563. const bool transit);
  564. struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
  565. const u8 profile);
  566. struct tomoyo_policy_namespace *tomoyo_assign_namespace
  567. (const char *domainname);
  568. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  569. const u8 idx);
  570. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  571. const u8 index);
  572. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  573. void tomoyo_load_policy(const char *filename);
  574. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  575. char *tomoyo_encode(const char *str);
  576. char *tomoyo_realpath_nofollow(const char *pathname);
  577. char *tomoyo_realpath_from_path(struct path *path);
  578. bool tomoyo_memory_ok(void *ptr);
  579. void *tomoyo_commit_ok(void *data, const unsigned int size);
  580. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  581. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  582. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  583. void __init tomoyo_mm_init(void);
  584. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  585. const struct tomoyo_path_info *filename);
  586. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  587. struct path *path, const int flag);
  588. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  589. unsigned long number);
  590. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  591. const unsigned int mode, unsigned int dev);
  592. int tomoyo_path_perm(const u8 operation, struct path *path);
  593. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  594. struct path *path2);
  595. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  596. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  597. const unsigned long value, const u8 type);
  598. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  599. void tomoyo_run_gc(void);
  600. void tomoyo_memory_free(void *ptr);
  601. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  602. struct tomoyo_acl_param *param,
  603. bool (*check_duplicate) (const struct tomoyo_acl_info
  604. *,
  605. const struct tomoyo_acl_info
  606. *),
  607. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  608. struct tomoyo_acl_info *,
  609. const bool));
  610. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  611. struct tomoyo_acl_param *param,
  612. bool (*check_duplicate) (const struct tomoyo_acl_head
  613. *,
  614. const struct tomoyo_acl_head
  615. *));
  616. void tomoyo_check_acl(struct tomoyo_request_info *r,
  617. bool (*check_entry) (struct tomoyo_request_info *,
  618. const struct tomoyo_acl_info *));
  619. char *tomoyo_read_token(struct tomoyo_acl_param *param);
  620. bool tomoyo_permstr(const char *string, const char *keyword);
  621. const char *tomoyo_yesno(const unsigned int value);
  622. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  623. __attribute__ ((format(printf, 2, 3)));
  624. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  625. va_list args);
  626. void tomoyo_read_log(struct tomoyo_io_buffer *head);
  627. int tomoyo_poll_log(struct file *file, poll_table *wait);
  628. char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
  629. va_list args);
  630. /********** External variable definitions. **********/
  631. /* Lock for GC. */
  632. extern struct srcu_struct tomoyo_ss;
  633. /* The list for "struct tomoyo_domain_info". */
  634. extern struct list_head tomoyo_domain_list;
  635. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  636. /* Lock for protecting policy. */
  637. extern struct mutex tomoyo_policy_lock;
  638. /* Has /sbin/init started? */
  639. extern bool tomoyo_policy_loaded;
  640. /* The kernel's domain. */
  641. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  642. extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  643. extern struct list_head tomoyo_namespace_list;
  644. extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  645. extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
  646. extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
  647. extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  648. extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
  649. extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
  650. extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  651. extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
  652. extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  653. extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  654. /********** Inlined functions. **********/
  655. /**
  656. * tomoyo_read_lock - Take lock for protecting policy.
  657. *
  658. * Returns index number for tomoyo_read_unlock().
  659. */
  660. static inline int tomoyo_read_lock(void)
  661. {
  662. return srcu_read_lock(&tomoyo_ss);
  663. }
  664. /**
  665. * tomoyo_read_unlock - Release lock for protecting policy.
  666. *
  667. * @idx: Index number returned by tomoyo_read_lock().
  668. *
  669. * Returns nothing.
  670. */
  671. static inline void tomoyo_read_unlock(int idx)
  672. {
  673. srcu_read_unlock(&tomoyo_ss, idx);
  674. }
  675. /**
  676. * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
  677. *
  678. * @a: Pointer to "struct tomoyo_path_info".
  679. * @b: Pointer to "struct tomoyo_path_info".
  680. *
  681. * Returns true if @a == @b, false otherwise.
  682. */
  683. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  684. const struct tomoyo_path_info *b)
  685. {
  686. return a->hash != b->hash || strcmp(a->name, b->name);
  687. }
  688. /**
  689. * tomoyo_put_name - Drop reference on "struct tomoyo_name".
  690. *
  691. * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
  692. *
  693. * Returns nothing.
  694. */
  695. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  696. {
  697. if (name) {
  698. struct tomoyo_name *ptr =
  699. container_of(name, typeof(*ptr), entry);
  700. atomic_dec(&ptr->head.users);
  701. }
  702. }
  703. /**
  704. * tomoyo_put_group - Drop reference on "struct tomoyo_group".
  705. *
  706. * @group: Pointer to "struct tomoyo_group". Maybe NULL.
  707. *
  708. * Returns nothing.
  709. */
  710. static inline void tomoyo_put_group(struct tomoyo_group *group)
  711. {
  712. if (group)
  713. atomic_dec(&group->head.users);
  714. }
  715. /**
  716. * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
  717. *
  718. * Returns pointer to "struct tomoyo_domain_info" for current thread.
  719. */
  720. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  721. {
  722. return current_cred()->security;
  723. }
  724. /**
  725. * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
  726. *
  727. * @task: Pointer to "struct task_struct".
  728. *
  729. * Returns pointer to "struct tomoyo_security" for specified thread.
  730. */
  731. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  732. *task)
  733. {
  734. return task_cred_xxx(task, security);
  735. }
  736. /**
  737. * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
  738. *
  739. * @a: Pointer to "struct tomoyo_name_union".
  740. * @b: Pointer to "struct tomoyo_name_union".
  741. *
  742. * Returns true if @a == @b, false otherwise.
  743. */
  744. static inline bool tomoyo_same_name_union
  745. (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
  746. {
  747. return a->filename == b->filename && a->group == b->group;
  748. }
  749. /**
  750. * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
  751. *
  752. * @a: Pointer to "struct tomoyo_number_union".
  753. * @b: Pointer to "struct tomoyo_number_union".
  754. *
  755. * Returns true if @a == @b, false otherwise.
  756. */
  757. static inline bool tomoyo_same_number_union
  758. (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
  759. {
  760. return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
  761. a->group == b->group && a->value_type[0] == b->value_type[0] &&
  762. a->value_type[1] == b->value_type[1];
  763. }
  764. /**
  765. * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
  766. *
  767. * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
  768. */
  769. static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
  770. {
  771. return tomoyo_domain()->ns;
  772. }
  773. #if defined(CONFIG_SLOB)
  774. /**
  775. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  776. *
  777. * @size: Size to be rounded up.
  778. *
  779. * Returns @size.
  780. *
  781. * Since SLOB does not round up, this function simply returns @size.
  782. */
  783. static inline int tomoyo_round2(size_t size)
  784. {
  785. return size;
  786. }
  787. #else
  788. /**
  789. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  790. *
  791. * @size: Size to be rounded up.
  792. *
  793. * Returns rounded size.
  794. *
  795. * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
  796. * (e.g.) 128 bytes.
  797. */
  798. static inline int tomoyo_round2(size_t size)
  799. {
  800. #if PAGE_SIZE == 4096
  801. size_t bsize = 32;
  802. #else
  803. size_t bsize = 64;
  804. #endif
  805. if (!size)
  806. return 0;
  807. while (size > bsize)
  808. bsize <<= 1;
  809. return bsize;
  810. }
  811. #endif
  812. /**
  813. * list_for_each_cookie - iterate over a list with cookie.
  814. * @pos: the &struct list_head to use as a loop cursor.
  815. * @head: the head for your list.
  816. */
  817. #define list_for_each_cookie(pos, head) \
  818. if (!pos) \
  819. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  820. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  821. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */