common.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. /* Index numbers for operation mode. */
  35. enum tomoyo_mode_index {
  36. TOMOYO_CONFIG_DISABLED,
  37. TOMOYO_CONFIG_LEARNING,
  38. TOMOYO_CONFIG_PERMISSIVE,
  39. TOMOYO_CONFIG_ENFORCING,
  40. TOMOYO_CONFIG_USE_DEFAULT = 255
  41. };
  42. /* Index numbers for entry type. */
  43. enum tomoyo_policy_id {
  44. TOMOYO_ID_GROUP,
  45. TOMOYO_ID_PATH_GROUP,
  46. TOMOYO_ID_NUMBER_GROUP,
  47. TOMOYO_ID_TRANSITION_CONTROL,
  48. TOMOYO_ID_AGGREGATOR,
  49. TOMOYO_ID_MANAGER,
  50. TOMOYO_ID_NAME,
  51. TOMOYO_ID_ACL,
  52. TOMOYO_ID_DOMAIN,
  53. TOMOYO_MAX_POLICY
  54. };
  55. /* Index numbers for group entries. */
  56. enum tomoyo_group_id {
  57. TOMOYO_PATH_GROUP,
  58. TOMOYO_NUMBER_GROUP,
  59. TOMOYO_MAX_GROUP
  60. };
  61. /* A domain definition starts with <kernel>. */
  62. #define TOMOYO_ROOT_NAME "<kernel>"
  63. #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
  64. /* Index numbers for type of numeric values. */
  65. enum tomoyo_value_type {
  66. TOMOYO_VALUE_TYPE_INVALID,
  67. TOMOYO_VALUE_TYPE_DECIMAL,
  68. TOMOYO_VALUE_TYPE_OCTAL,
  69. TOMOYO_VALUE_TYPE_HEXADECIMAL,
  70. };
  71. /* Index numbers for domain transition control keywords. */
  72. enum tomoyo_transition_type {
  73. /* Do not change this order, */
  74. TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
  75. TOMOYO_TRANSITION_CONTROL_INITIALIZE,
  76. TOMOYO_TRANSITION_CONTROL_NO_KEEP,
  77. TOMOYO_TRANSITION_CONTROL_KEEP,
  78. TOMOYO_MAX_TRANSITION_TYPE
  79. };
  80. /* Index numbers for Access Controls. */
  81. enum tomoyo_acl_entry_type_index {
  82. TOMOYO_TYPE_PATH_ACL,
  83. TOMOYO_TYPE_PATH2_ACL,
  84. TOMOYO_TYPE_PATH_NUMBER_ACL,
  85. TOMOYO_TYPE_MKDEV_ACL,
  86. TOMOYO_TYPE_MOUNT_ACL,
  87. };
  88. /* Index numbers for access controls with one pathname. */
  89. enum tomoyo_path_acl_index {
  90. TOMOYO_TYPE_EXECUTE,
  91. TOMOYO_TYPE_READ,
  92. TOMOYO_TYPE_WRITE,
  93. TOMOYO_TYPE_APPEND,
  94. TOMOYO_TYPE_UNLINK,
  95. TOMOYO_TYPE_GETATTR,
  96. TOMOYO_TYPE_RMDIR,
  97. TOMOYO_TYPE_TRUNCATE,
  98. TOMOYO_TYPE_SYMLINK,
  99. TOMOYO_TYPE_CHROOT,
  100. TOMOYO_TYPE_UMOUNT,
  101. TOMOYO_MAX_PATH_OPERATION
  102. };
  103. enum tomoyo_mkdev_acl_index {
  104. TOMOYO_TYPE_MKBLOCK,
  105. TOMOYO_TYPE_MKCHAR,
  106. TOMOYO_MAX_MKDEV_OPERATION
  107. };
  108. /* Index numbers for access controls with two pathnames. */
  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. /* Index numbers for access controls with one pathname and one number. */
  116. enum tomoyo_path_number_acl_index {
  117. TOMOYO_TYPE_CREATE,
  118. TOMOYO_TYPE_MKDIR,
  119. TOMOYO_TYPE_MKFIFO,
  120. TOMOYO_TYPE_MKSOCK,
  121. TOMOYO_TYPE_IOCTL,
  122. TOMOYO_TYPE_CHMOD,
  123. TOMOYO_TYPE_CHOWN,
  124. TOMOYO_TYPE_CHGRP,
  125. TOMOYO_MAX_PATH_NUMBER_OPERATION
  126. };
  127. /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
  128. enum tomoyo_securityfs_interface_index {
  129. TOMOYO_DOMAINPOLICY,
  130. TOMOYO_EXCEPTIONPOLICY,
  131. TOMOYO_DOMAIN_STATUS,
  132. TOMOYO_PROCESS_STATUS,
  133. TOMOYO_MEMINFO,
  134. TOMOYO_SELFDOMAIN,
  135. TOMOYO_VERSION,
  136. TOMOYO_PROFILE,
  137. TOMOYO_QUERY,
  138. TOMOYO_MANAGER
  139. };
  140. /* Index numbers for special mount operations. */
  141. enum tomoyo_special_mount {
  142. TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
  143. TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
  144. TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
  145. TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
  146. TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
  147. TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
  148. TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
  149. TOMOYO_MAX_SPECIAL_MOUNT
  150. };
  151. /* Index numbers for functionality. */
  152. enum tomoyo_mac_index {
  153. TOMOYO_MAC_FILE_EXECUTE,
  154. TOMOYO_MAC_FILE_OPEN,
  155. TOMOYO_MAC_FILE_CREATE,
  156. TOMOYO_MAC_FILE_UNLINK,
  157. TOMOYO_MAC_FILE_GETATTR,
  158. TOMOYO_MAC_FILE_MKDIR,
  159. TOMOYO_MAC_FILE_RMDIR,
  160. TOMOYO_MAC_FILE_MKFIFO,
  161. TOMOYO_MAC_FILE_MKSOCK,
  162. TOMOYO_MAC_FILE_TRUNCATE,
  163. TOMOYO_MAC_FILE_SYMLINK,
  164. TOMOYO_MAC_FILE_MKBLOCK,
  165. TOMOYO_MAC_FILE_MKCHAR,
  166. TOMOYO_MAC_FILE_LINK,
  167. TOMOYO_MAC_FILE_RENAME,
  168. TOMOYO_MAC_FILE_CHMOD,
  169. TOMOYO_MAC_FILE_CHOWN,
  170. TOMOYO_MAC_FILE_CHGRP,
  171. TOMOYO_MAC_FILE_IOCTL,
  172. TOMOYO_MAC_FILE_CHROOT,
  173. TOMOYO_MAC_FILE_MOUNT,
  174. TOMOYO_MAC_FILE_UMOUNT,
  175. TOMOYO_MAC_FILE_PIVOT_ROOT,
  176. TOMOYO_MAX_MAC_INDEX
  177. };
  178. /* Index numbers for category of functionality. */
  179. enum tomoyo_mac_category_index {
  180. TOMOYO_MAC_CATEGORY_FILE,
  181. TOMOYO_MAX_MAC_CATEGORY_INDEX
  182. };
  183. /*
  184. * Retry this request. Returned by tomoyo_supervisor() if policy violation has
  185. * occurred in enforcing mode and the userspace daemon decided to retry.
  186. *
  187. * We must choose a positive value in order to distinguish "granted" (which is
  188. * 0) and "rejected" (which is a negative value) and "retry".
  189. */
  190. #define TOMOYO_RETRY_REQUEST 1
  191. /********** Structure definitions. **********/
  192. /* Common header for holding ACL entries. */
  193. struct tomoyo_acl_head {
  194. struct list_head list;
  195. bool is_deleted;
  196. } __packed;
  197. /* Common header for shared entries. */
  198. struct tomoyo_shared_acl_head {
  199. struct list_head list;
  200. atomic_t users;
  201. } __packed;
  202. /* Structure for request info. */
  203. struct tomoyo_request_info {
  204. struct tomoyo_domain_info *domain;
  205. /* For holding parameters. */
  206. union {
  207. struct {
  208. const struct tomoyo_path_info *filename;
  209. /* For using wildcards at tomoyo_find_next_domain(). */
  210. const struct tomoyo_path_info *matched_path;
  211. /* One of values in "enum tomoyo_path_acl_index". */
  212. u8 operation;
  213. } path;
  214. struct {
  215. const struct tomoyo_path_info *filename1;
  216. const struct tomoyo_path_info *filename2;
  217. /* One of values in "enum tomoyo_path2_acl_index". */
  218. u8 operation;
  219. } path2;
  220. struct {
  221. const struct tomoyo_path_info *filename;
  222. unsigned int mode;
  223. unsigned int major;
  224. unsigned int minor;
  225. /* One of values in "enum tomoyo_mkdev_acl_index". */
  226. u8 operation;
  227. } mkdev;
  228. struct {
  229. const struct tomoyo_path_info *filename;
  230. unsigned long number;
  231. /*
  232. * One of values in
  233. * "enum tomoyo_path_number_acl_index".
  234. */
  235. u8 operation;
  236. } path_number;
  237. struct {
  238. const struct tomoyo_path_info *type;
  239. const struct tomoyo_path_info *dir;
  240. const struct tomoyo_path_info *dev;
  241. unsigned long flags;
  242. int need_dev;
  243. } mount;
  244. } param;
  245. u8 param_type;
  246. bool granted;
  247. u8 retry;
  248. u8 profile;
  249. u8 mode; /* One of tomoyo_mode_index . */
  250. u8 type;
  251. };
  252. /* Structure for holding a token. */
  253. struct tomoyo_path_info {
  254. const char *name;
  255. u32 hash; /* = full_name_hash(name, strlen(name)) */
  256. u16 const_len; /* = tomoyo_const_part_length(name) */
  257. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  258. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  259. };
  260. /* Structure for holding string data. */
  261. struct tomoyo_name {
  262. struct tomoyo_shared_acl_head head;
  263. struct tomoyo_path_info entry;
  264. };
  265. /* Structure for holding a word. */
  266. struct tomoyo_name_union {
  267. /* Either @filename or @group is NULL. */
  268. const struct tomoyo_path_info *filename;
  269. struct tomoyo_group *group;
  270. };
  271. /* Structure for holding a number. */
  272. struct tomoyo_number_union {
  273. unsigned long values[2];
  274. struct tomoyo_group *group; /* Maybe NULL. */
  275. /* One of values in "enum tomoyo_value_type". */
  276. u8 value_type[2];
  277. };
  278. /* Structure for "path_group"/"number_group" directive. */
  279. struct tomoyo_group {
  280. struct tomoyo_shared_acl_head head;
  281. const struct tomoyo_path_info *group_name;
  282. struct list_head member_list;
  283. };
  284. /* Structure for "path_group" directive. */
  285. struct tomoyo_path_group {
  286. struct tomoyo_acl_head head;
  287. const struct tomoyo_path_info *member_name;
  288. };
  289. /* Structure for "number_group" directive. */
  290. struct tomoyo_number_group {
  291. struct tomoyo_acl_head head;
  292. struct tomoyo_number_union number;
  293. };
  294. /* Common header for individual entries. */
  295. struct tomoyo_acl_info {
  296. struct list_head list;
  297. bool is_deleted;
  298. u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
  299. } __packed;
  300. /* Structure for domain information. */
  301. struct tomoyo_domain_info {
  302. struct list_head list;
  303. struct list_head acl_info_list;
  304. /* Name of this domain. Never NULL. */
  305. const struct tomoyo_path_info *domainname;
  306. u8 profile; /* Profile number to use. */
  307. bool is_deleted; /* Delete flag. */
  308. bool quota_warned; /* Quota warnning flag. */
  309. bool transition_failed; /* Domain transition failed flag. */
  310. atomic_t users; /* Number of referring credentials. */
  311. };
  312. /*
  313. * Structure for "file execute", "file read", "file write", "file append",
  314. * "file unlink", "file getattr", "file rmdir", "file truncate",
  315. * "file symlink", "file chroot" and "file unmount" directive.
  316. */
  317. struct tomoyo_path_acl {
  318. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  319. u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
  320. struct tomoyo_name_union name;
  321. };
  322. /*
  323. * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
  324. * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
  325. */
  326. struct tomoyo_path_number_acl {
  327. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
  328. /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
  329. u8 perm;
  330. struct tomoyo_name_union name;
  331. struct tomoyo_number_union number;
  332. };
  333. /* Structure for "file mkblock" and "file mkchar" directive. */
  334. struct tomoyo_mkdev_acl {
  335. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
  336. u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
  337. struct tomoyo_name_union name;
  338. struct tomoyo_number_union mode;
  339. struct tomoyo_number_union major;
  340. struct tomoyo_number_union minor;
  341. };
  342. /*
  343. * Structure for "file rename", "file link" and "file pivot_root" directive.
  344. */
  345. struct tomoyo_path2_acl {
  346. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  347. u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
  348. struct tomoyo_name_union name1;
  349. struct tomoyo_name_union name2;
  350. };
  351. /* Structure for "file mount" directive. */
  352. struct tomoyo_mount_acl {
  353. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
  354. struct tomoyo_name_union dev_name;
  355. struct tomoyo_name_union dir_name;
  356. struct tomoyo_name_union fs_type;
  357. struct tomoyo_number_union flags;
  358. };
  359. #define TOMOYO_MAX_IO_READ_QUEUE 32
  360. /*
  361. * Structure for reading/writing policy via /sys/kernel/security/tomoyo
  362. * interfaces.
  363. */
  364. struct tomoyo_io_buffer {
  365. void (*read) (struct tomoyo_io_buffer *);
  366. int (*write) (struct tomoyo_io_buffer *);
  367. int (*poll) (struct file *file, poll_table *wait);
  368. /* Exclusive lock for this structure. */
  369. struct mutex io_sem;
  370. /* Index returned by tomoyo_read_lock(). */
  371. int reader_idx;
  372. char __user *read_user_buf;
  373. int read_user_buf_avail;
  374. struct {
  375. struct list_head *domain;
  376. struct list_head *group;
  377. struct list_head *acl;
  378. int avail;
  379. int step;
  380. int query_index;
  381. u16 index;
  382. u8 bit;
  383. u8 w_pos;
  384. bool eof;
  385. bool print_this_domain_only;
  386. bool print_execute_only;
  387. const char *w[TOMOYO_MAX_IO_READ_QUEUE];
  388. } r;
  389. struct {
  390. /* The position currently writing to. */
  391. struct tomoyo_domain_info *domain;
  392. /* Bytes available for writing. */
  393. int avail;
  394. } w;
  395. /* Buffer for reading. */
  396. char *read_buf;
  397. /* Size of read buffer. */
  398. int readbuf_size;
  399. /* Buffer for writing. */
  400. char *write_buf;
  401. /* Size of write buffer. */
  402. int writebuf_size;
  403. /* Type of this interface. */
  404. u8 type;
  405. };
  406. /*
  407. * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
  408. * "no_keep_domain" keyword.
  409. */
  410. struct tomoyo_transition_control {
  411. struct tomoyo_acl_head head;
  412. u8 type; /* One of values in "enum tomoyo_transition_type". */
  413. /* True if the domainname is tomoyo_get_last_name(). */
  414. bool is_last_name;
  415. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  416. const struct tomoyo_path_info *program; /* Maybe NULL */
  417. };
  418. /* Structure for "aggregator" keyword. */
  419. struct tomoyo_aggregator {
  420. struct tomoyo_acl_head head;
  421. const struct tomoyo_path_info *original_name;
  422. const struct tomoyo_path_info *aggregated_name;
  423. };
  424. /* Structure for policy manager. */
  425. struct tomoyo_manager {
  426. struct tomoyo_acl_head head;
  427. bool is_domain; /* True if manager is a domainname. */
  428. /* A path to program or a domainname. */
  429. const struct tomoyo_path_info *manager;
  430. };
  431. struct tomoyo_preference {
  432. unsigned int learning_max_entry;
  433. bool enforcing_verbose;
  434. bool learning_verbose;
  435. bool permissive_verbose;
  436. };
  437. /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
  438. struct tomoyo_profile {
  439. const struct tomoyo_path_info *comment;
  440. struct tomoyo_preference *learning;
  441. struct tomoyo_preference *permissive;
  442. struct tomoyo_preference *enforcing;
  443. struct tomoyo_preference preference;
  444. u8 default_config;
  445. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  446. };
  447. /********** Function prototypes. **********/
  448. bool tomoyo_str_starts(char **src, const char *find);
  449. const char *tomoyo_get_exe(void);
  450. void tomoyo_normalize_line(unsigned char *buffer);
  451. void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
  452. __attribute__ ((format(printf, 2, 3)));
  453. void tomoyo_check_profile(void);
  454. int tomoyo_open_control(const u8 type, struct file *file);
  455. int tomoyo_close_control(struct tomoyo_io_buffer *head);
  456. int tomoyo_poll_control(struct file *file, poll_table *wait);
  457. int tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  458. const int buffer_len);
  459. int tomoyo_write_control(struct tomoyo_io_buffer *head,
  460. const char __user *buffer, const int buffer_len);
  461. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  462. void tomoyo_warn_oom(const char *function);
  463. const struct tomoyo_path_info *
  464. tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  465. const struct tomoyo_name_union *ptr);
  466. bool tomoyo_compare_number_union(const unsigned long value,
  467. const struct tomoyo_number_union *ptr);
  468. int tomoyo_get_mode(const u8 profile, const u8 index);
  469. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  470. __attribute__ ((format(printf, 2, 3)));
  471. bool tomoyo_correct_domain(const unsigned char *domainname);
  472. bool tomoyo_correct_path(const char *filename);
  473. bool tomoyo_correct_word(const char *string);
  474. bool tomoyo_domain_def(const unsigned char *buffer);
  475. bool tomoyo_parse_name_union(const char *filename,
  476. struct tomoyo_name_union *ptr);
  477. const struct tomoyo_path_info *
  478. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  479. const struct tomoyo_group *group);
  480. bool tomoyo_number_matches_group(const unsigned long min,
  481. const unsigned long max,
  482. const struct tomoyo_group *group);
  483. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  484. const struct tomoyo_path_info *pattern);
  485. bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
  486. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  487. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  488. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  489. struct tomoyo_domain_info *domain,
  490. const u8 index);
  491. int tomoyo_mount_permission(char *dev_name, struct path *path,
  492. const char *type, unsigned long flags,
  493. void *data_page);
  494. int tomoyo_write_aggregator(char *data, const bool is_delete);
  495. int tomoyo_write_transition_control(char *data, const bool is_delete,
  496. const u8 type);
  497. int tomoyo_write_file(char *data, struct tomoyo_domain_info *domain,
  498. const bool is_delete);
  499. int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
  500. const bool is_delete);
  501. int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
  502. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  503. __attribute__ ((format(printf, 2, 3)));
  504. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  505. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  506. const u8 profile);
  507. struct tomoyo_profile *tomoyo_profile(const u8 profile);
  508. struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
  509. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  510. const u8 index);
  511. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  512. void tomoyo_load_policy(const char *filename);
  513. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  514. char *tomoyo_encode(const char *str);
  515. char *tomoyo_realpath_nofollow(const char *pathname);
  516. char *tomoyo_realpath_from_path(struct path *path);
  517. bool tomoyo_memory_ok(void *ptr);
  518. void *tomoyo_commit_ok(void *data, const unsigned int size);
  519. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  520. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  521. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  522. void __init tomoyo_mm_init(void);
  523. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  524. const struct tomoyo_path_info *filename);
  525. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  526. struct path *path, const int flag);
  527. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  528. unsigned long number);
  529. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  530. const unsigned int mode, unsigned int dev);
  531. int tomoyo_path_perm(const u8 operation, struct path *path);
  532. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  533. struct path *path2);
  534. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  535. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  536. const unsigned long value, const u8 type);
  537. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  538. void tomoyo_run_gc(void);
  539. void tomoyo_memory_free(void *ptr);
  540. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  541. bool is_delete, struct tomoyo_domain_info *domain,
  542. bool (*check_duplicate) (const struct tomoyo_acl_info
  543. *,
  544. const struct tomoyo_acl_info
  545. *),
  546. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  547. struct tomoyo_acl_info *,
  548. const bool));
  549. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  550. bool is_delete, struct list_head *list,
  551. bool (*check_duplicate) (const struct tomoyo_acl_head
  552. *,
  553. const struct tomoyo_acl_head
  554. *));
  555. void tomoyo_check_acl(struct tomoyo_request_info *r,
  556. bool (*check_entry) (struct tomoyo_request_info *,
  557. const struct tomoyo_acl_info *));
  558. /********** External variable definitions. **********/
  559. /* Lock for GC. */
  560. extern struct srcu_struct tomoyo_ss;
  561. /* The list for "struct tomoyo_domain_info". */
  562. extern struct list_head tomoyo_domain_list;
  563. extern struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
  564. extern struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
  565. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  566. /* Lock for protecting policy. */
  567. extern struct mutex tomoyo_policy_lock;
  568. /* Has /sbin/init started? */
  569. extern bool tomoyo_policy_loaded;
  570. /* The kernel's domain. */
  571. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  572. extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  573. extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
  574. extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
  575. extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  576. extern unsigned int tomoyo_quota_for_query;
  577. extern unsigned int tomoyo_query_memory_size;
  578. /********** Inlined functions. **********/
  579. /**
  580. * tomoyo_read_lock - Take lock for protecting policy.
  581. *
  582. * Returns index number for tomoyo_read_unlock().
  583. */
  584. static inline int tomoyo_read_lock(void)
  585. {
  586. return srcu_read_lock(&tomoyo_ss);
  587. }
  588. /**
  589. * tomoyo_read_unlock - Release lock for protecting policy.
  590. *
  591. * @idx: Index number returned by tomoyo_read_lock().
  592. *
  593. * Returns nothing.
  594. */
  595. static inline void tomoyo_read_unlock(int idx)
  596. {
  597. srcu_read_unlock(&tomoyo_ss, idx);
  598. }
  599. /**
  600. * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
  601. *
  602. * @a: Pointer to "struct tomoyo_path_info".
  603. * @b: Pointer to "struct tomoyo_path_info".
  604. *
  605. * Returns true if @a == @b, false otherwise.
  606. */
  607. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  608. const struct tomoyo_path_info *b)
  609. {
  610. return a->hash != b->hash || strcmp(a->name, b->name);
  611. }
  612. /**
  613. * tomoyo_put_name - Drop reference on "struct tomoyo_name".
  614. *
  615. * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
  616. *
  617. * Returns nothing.
  618. */
  619. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  620. {
  621. if (name) {
  622. struct tomoyo_name *ptr =
  623. container_of(name, typeof(*ptr), entry);
  624. atomic_dec(&ptr->head.users);
  625. }
  626. }
  627. /**
  628. * tomoyo_put_group - Drop reference on "struct tomoyo_group".
  629. *
  630. * @group: Pointer to "struct tomoyo_group". Maybe NULL.
  631. *
  632. * Returns nothing.
  633. */
  634. static inline void tomoyo_put_group(struct tomoyo_group *group)
  635. {
  636. if (group)
  637. atomic_dec(&group->head.users);
  638. }
  639. /**
  640. * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
  641. *
  642. * Returns pointer to "struct tomoyo_domain_info" for current thread.
  643. */
  644. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  645. {
  646. return current_cred()->security;
  647. }
  648. /**
  649. * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
  650. *
  651. * @task: Pointer to "struct task_struct".
  652. *
  653. * Returns pointer to "struct tomoyo_security" for specified thread.
  654. */
  655. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  656. *task)
  657. {
  658. return task_cred_xxx(task, security);
  659. }
  660. /**
  661. * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
  662. *
  663. * @a: Pointer to "struct tomoyo_name_union".
  664. * @b: Pointer to "struct tomoyo_name_union".
  665. *
  666. * Returns true if @a == @b, false otherwise.
  667. */
  668. static inline bool tomoyo_same_name_union
  669. (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
  670. {
  671. return a->filename == b->filename && a->group == b->group;
  672. }
  673. /**
  674. * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
  675. *
  676. * @a: Pointer to "struct tomoyo_number_union".
  677. * @b: Pointer to "struct tomoyo_number_union".
  678. *
  679. * Returns true if @a == @b, false otherwise.
  680. */
  681. static inline bool tomoyo_same_number_union
  682. (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
  683. {
  684. return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
  685. a->group == b->group && a->value_type[0] == b->value_type[0] &&
  686. a->value_type[1] == b->value_type[1];
  687. }
  688. /**
  689. * list_for_each_cookie - iterate over a list with cookie.
  690. * @pos: the &struct list_head to use as a loop cursor.
  691. * @head: the head for your list.
  692. */
  693. #define list_for_each_cookie(pos, head) \
  694. if (!pos) \
  695. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  696. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  697. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */