common.h 25 KB

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