common.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. enum tomoyo_mode_index {
  35. TOMOYO_CONFIG_DISABLED,
  36. TOMOYO_CONFIG_LEARNING,
  37. TOMOYO_CONFIG_PERMISSIVE,
  38. TOMOYO_CONFIG_ENFORCING,
  39. TOMOYO_CONFIG_USE_DEFAULT = 255
  40. };
  41. enum tomoyo_policy_id {
  42. TOMOYO_ID_GROUP,
  43. TOMOYO_ID_PATH_GROUP,
  44. TOMOYO_ID_NUMBER_GROUP,
  45. TOMOYO_ID_TRANSITION_CONTROL,
  46. TOMOYO_ID_AGGREGATOR,
  47. TOMOYO_ID_GLOBALLY_READABLE,
  48. TOMOYO_ID_PATTERN,
  49. TOMOYO_ID_NO_REWRITE,
  50. TOMOYO_ID_MANAGER,
  51. TOMOYO_ID_NAME,
  52. TOMOYO_ID_ACL,
  53. TOMOYO_ID_DOMAIN,
  54. TOMOYO_MAX_POLICY
  55. };
  56. enum tomoyo_group_id {
  57. TOMOYO_PATH_GROUP,
  58. TOMOYO_NUMBER_GROUP,
  59. TOMOYO_MAX_GROUP
  60. };
  61. /* Keywords for ACLs. */
  62. #define TOMOYO_KEYWORD_AGGREGATOR "aggregator "
  63. #define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
  64. #define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
  65. #define TOMOYO_KEYWORD_DELETE "delete "
  66. #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
  67. #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
  68. #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
  69. #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
  70. #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
  71. #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
  72. #define TOMOYO_KEYWORD_PATH_GROUP "path_group "
  73. #define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
  74. #define TOMOYO_KEYWORD_SELECT "select "
  75. #define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
  76. #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
  77. #define TOMOYO_KEYWORD_QUOTA_EXCEEDED "quota_exceeded"
  78. #define TOMOYO_KEYWORD_TRANSITION_FAILED "transition_failed"
  79. /* A domain definition starts with <kernel>. */
  80. #define TOMOYO_ROOT_NAME "<kernel>"
  81. #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
  82. /* Value type definition. */
  83. #define TOMOYO_VALUE_TYPE_INVALID 0
  84. #define TOMOYO_VALUE_TYPE_DECIMAL 1
  85. #define TOMOYO_VALUE_TYPE_OCTAL 2
  86. #define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
  87. enum tomoyo_transition_type {
  88. /* Do not change this order, */
  89. TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
  90. TOMOYO_TRANSITION_CONTROL_INITIALIZE,
  91. TOMOYO_TRANSITION_CONTROL_NO_KEEP,
  92. TOMOYO_TRANSITION_CONTROL_KEEP,
  93. TOMOYO_MAX_TRANSITION_TYPE
  94. };
  95. /* Index numbers for Access Controls. */
  96. enum tomoyo_acl_entry_type_index {
  97. TOMOYO_TYPE_PATH_ACL,
  98. TOMOYO_TYPE_PATH2_ACL,
  99. TOMOYO_TYPE_PATH_NUMBER_ACL,
  100. TOMOYO_TYPE_MKDEV_ACL,
  101. TOMOYO_TYPE_MOUNT_ACL,
  102. };
  103. /* Index numbers for File Controls. */
  104. /*
  105. * TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
  106. * set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
  107. * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
  108. * TOMOYO_TYPE_READ_WRITE is set.
  109. * TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
  110. * or TOMOYO_TYPE_WRITE is cleared.
  111. * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
  112. * TOMOYO_TYPE_READ_WRITE is cleared.
  113. */
  114. enum tomoyo_path_acl_index {
  115. TOMOYO_TYPE_READ_WRITE,
  116. TOMOYO_TYPE_EXECUTE,
  117. TOMOYO_TYPE_READ,
  118. TOMOYO_TYPE_WRITE,
  119. TOMOYO_TYPE_UNLINK,
  120. TOMOYO_TYPE_RMDIR,
  121. TOMOYO_TYPE_TRUNCATE,
  122. TOMOYO_TYPE_SYMLINK,
  123. TOMOYO_TYPE_REWRITE,
  124. TOMOYO_TYPE_CHROOT,
  125. TOMOYO_TYPE_UMOUNT,
  126. TOMOYO_MAX_PATH_OPERATION
  127. };
  128. #define TOMOYO_RW_MASK ((1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE))
  129. enum tomoyo_mkdev_acl_index {
  130. TOMOYO_TYPE_MKBLOCK,
  131. TOMOYO_TYPE_MKCHAR,
  132. TOMOYO_MAX_MKDEV_OPERATION
  133. };
  134. enum tomoyo_path2_acl_index {
  135. TOMOYO_TYPE_LINK,
  136. TOMOYO_TYPE_RENAME,
  137. TOMOYO_TYPE_PIVOT_ROOT,
  138. TOMOYO_MAX_PATH2_OPERATION
  139. };
  140. enum tomoyo_path_number_acl_index {
  141. TOMOYO_TYPE_CREATE,
  142. TOMOYO_TYPE_MKDIR,
  143. TOMOYO_TYPE_MKFIFO,
  144. TOMOYO_TYPE_MKSOCK,
  145. TOMOYO_TYPE_IOCTL,
  146. TOMOYO_TYPE_CHMOD,
  147. TOMOYO_TYPE_CHOWN,
  148. TOMOYO_TYPE_CHGRP,
  149. TOMOYO_MAX_PATH_NUMBER_OPERATION
  150. };
  151. enum tomoyo_securityfs_interface_index {
  152. TOMOYO_DOMAINPOLICY,
  153. TOMOYO_EXCEPTIONPOLICY,
  154. TOMOYO_DOMAIN_STATUS,
  155. TOMOYO_PROCESS_STATUS,
  156. TOMOYO_MEMINFO,
  157. TOMOYO_SELFDOMAIN,
  158. TOMOYO_VERSION,
  159. TOMOYO_PROFILE,
  160. TOMOYO_QUERY,
  161. TOMOYO_MANAGER
  162. };
  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_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_REWRITE,
  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. enum tomoyo_mac_category_index {
  190. TOMOYO_MAC_CATEGORY_FILE,
  191. TOMOYO_MAX_MAC_CATEGORY_INDEX
  192. };
  193. #define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
  194. /********** Structure definitions. **********/
  195. /*
  196. * tomoyo_acl_head is a structure which is used for holding elements not in
  197. * domain policy.
  198. * It has following fields.
  199. *
  200. * (1) "list" which is linked to tomoyo_policy_list[] .
  201. * (2) "is_deleted" is a bool which is true if marked as deleted, false
  202. * otherwise.
  203. */
  204. struct tomoyo_acl_head {
  205. struct list_head list;
  206. bool is_deleted;
  207. } __packed;
  208. /*
  209. * tomoyo_request_info is a structure which is used for holding
  210. *
  211. * (1) Domain information of current process.
  212. * (2) How many retries are made for this request.
  213. * (3) Profile number used for this request.
  214. * (4) Access control mode of the profile.
  215. */
  216. struct tomoyo_request_info {
  217. struct tomoyo_domain_info *domain;
  218. /* For holding parameters. */
  219. union {
  220. struct {
  221. const struct tomoyo_path_info *filename;
  222. u8 operation;
  223. } path;
  224. struct {
  225. const struct tomoyo_path_info *filename1;
  226. const struct tomoyo_path_info *filename2;
  227. u8 operation;
  228. } path2;
  229. struct {
  230. const struct tomoyo_path_info *filename;
  231. unsigned int mode;
  232. unsigned int major;
  233. unsigned int minor;
  234. u8 operation;
  235. } mkdev;
  236. struct {
  237. const struct tomoyo_path_info *filename;
  238. unsigned long number;
  239. u8 operation;
  240. } path_number;
  241. struct {
  242. const struct tomoyo_path_info *type;
  243. const struct tomoyo_path_info *dir;
  244. const struct tomoyo_path_info *dev;
  245. unsigned long flags;
  246. int need_dev;
  247. } mount;
  248. } param;
  249. u8 param_type;
  250. bool granted;
  251. u8 retry;
  252. u8 profile;
  253. u8 mode; /* One of tomoyo_mode_index . */
  254. u8 type;
  255. };
  256. /*
  257. * tomoyo_path_info is a structure which is used for holding a string data
  258. * used by TOMOYO.
  259. * This structure has several fields for supporting pattern matching.
  260. *
  261. * (1) "name" is the '\0' terminated string data.
  262. * (2) "hash" is full_name_hash(name, strlen(name)).
  263. * This allows tomoyo_pathcmp() to compare by hash before actually compare
  264. * using strcmp().
  265. * (3) "const_len" is the length of the initial segment of "name" which
  266. * consists entirely of non wildcard characters. In other words, the length
  267. * which we can compare two strings using strncmp().
  268. * (4) "is_dir" is a bool which is true if "name" ends with "/",
  269. * false otherwise.
  270. * TOMOYO distinguishes directory and non-directory. A directory ends with
  271. * "/" and non-directory does not end with "/".
  272. * (5) "is_patterned" is a bool which is true if "name" contains wildcard
  273. * characters, false otherwise. This allows TOMOYO to use "hash" and
  274. * strcmp() for string comparison if "is_patterned" is false.
  275. */
  276. struct tomoyo_path_info {
  277. const char *name;
  278. u32 hash; /* = full_name_hash(name, strlen(name)) */
  279. u16 const_len; /* = tomoyo_const_part_length(name) */
  280. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  281. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  282. };
  283. /*
  284. * tomoyo_name_entry is a structure which is used for linking
  285. * "struct tomoyo_path_info" into tomoyo_name_list .
  286. */
  287. struct tomoyo_name_entry {
  288. struct list_head list;
  289. atomic_t users;
  290. struct tomoyo_path_info entry;
  291. };
  292. struct tomoyo_name_union {
  293. const struct tomoyo_path_info *filename;
  294. struct tomoyo_group *group;
  295. u8 is_group;
  296. };
  297. struct tomoyo_number_union {
  298. unsigned long values[2];
  299. struct tomoyo_group *group;
  300. u8 min_type;
  301. u8 max_type;
  302. u8 is_group;
  303. };
  304. /* Structure for "path_group"/"number_group" directive. */
  305. struct tomoyo_group {
  306. struct list_head list;
  307. const struct tomoyo_path_info *group_name;
  308. struct list_head member_list;
  309. atomic_t users;
  310. };
  311. /* Structure for "path_group" directive. */
  312. struct tomoyo_path_group {
  313. struct tomoyo_acl_head head;
  314. const struct tomoyo_path_info *member_name;
  315. };
  316. /* Structure for "number_group" directive. */
  317. struct tomoyo_number_group {
  318. struct tomoyo_acl_head head;
  319. struct tomoyo_number_union number;
  320. };
  321. /*
  322. * tomoyo_acl_info is a structure which is used for holding
  323. *
  324. * (1) "list" which is linked to the ->acl_info_list of
  325. * "struct tomoyo_domain_info"
  326. * (2) "is_deleted" is a bool which is true if this domain is marked as
  327. * "deleted", false otherwise.
  328. * (3) "type" which tells type of the entry.
  329. *
  330. * Packing "struct tomoyo_acl_info" allows
  331. * "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
  332. * "struct tomoyo_path_number_acl" "struct tomoyo_mkdev_acl" to embed
  333. * "u8" without enlarging their structure size.
  334. */
  335. struct tomoyo_acl_info {
  336. struct list_head list;
  337. bool is_deleted;
  338. u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
  339. } __packed;
  340. /*
  341. * tomoyo_domain_info is a structure which is used for holding permissions
  342. * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  343. * It has following fields.
  344. *
  345. * (1) "list" which is linked to tomoyo_domain_list .
  346. * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
  347. * (3) "domainname" which holds the name of the domain.
  348. * (4) "profile" which remembers profile number assigned to this domain.
  349. * (5) "is_deleted" is a bool which is true if this domain is marked as
  350. * "deleted", false otherwise.
  351. * (6) "quota_warned" is a bool which is used for suppressing warning message
  352. * when learning mode learned too much entries.
  353. * (7) "ignore_global_allow_read" is a bool which is true if this domain
  354. * should ignore "allow_read" directive in exception policy.
  355. * (8) "transition_failed" is a bool which is set to true when this domain was
  356. * unable to create a new domain at tomoyo_find_next_domain() because the
  357. * name of the domain to be created was too long or it could not allocate
  358. * memory. If set to true, more than one process continued execve()
  359. * without domain transition.
  360. * (9) "users" is an atomic_t that holds how many "struct cred"->security
  361. * are referring this "struct tomoyo_domain_info". If is_deleted == true
  362. * and users == 0, this struct will be kfree()d upon next garbage
  363. * collection.
  364. *
  365. * A domain's lifecycle is an analogy of files on / directory.
  366. * Multiple domains with the same domainname cannot be created (as with
  367. * creating files with the same filename fails with -EEXIST).
  368. * If a process reached a domain, that process can reside in that domain after
  369. * that domain is marked as "deleted" (as with a process can access an already
  370. * open()ed file after that file was unlink()ed).
  371. */
  372. struct tomoyo_domain_info {
  373. struct list_head list;
  374. struct list_head acl_info_list;
  375. /* Name of this domain. Never NULL. */
  376. const struct tomoyo_path_info *domainname;
  377. u8 profile; /* Profile number to use. */
  378. bool is_deleted; /* Delete flag. */
  379. bool quota_warned; /* Quota warnning flag. */
  380. bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
  381. bool transition_failed; /* Domain transition failed flag. */
  382. atomic_t users; /* Number of referring credentials. */
  383. };
  384. /*
  385. * tomoyo_path_acl is a structure which is used for holding an
  386. * entry with one pathname operation (e.g. open(), mkdir()).
  387. * It has following fields.
  388. *
  389. * (1) "head" which is a "struct tomoyo_acl_info".
  390. * (2) "perm" which is a bitmask of permitted operations.
  391. * (3) "name" is the pathname.
  392. *
  393. * Directives held by this structure are "allow_read/write", "allow_execute",
  394. * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
  395. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
  396. * "allow_unmount".
  397. */
  398. struct tomoyo_path_acl {
  399. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  400. u16 perm;
  401. struct tomoyo_name_union name;
  402. };
  403. /*
  404. * tomoyo_path_number_acl is a structure which is used for holding an
  405. * entry with one pathname and one number operation.
  406. * It has following fields.
  407. *
  408. * (1) "head" which is a "struct tomoyo_acl_info".
  409. * (2) "perm" which is a bitmask of permitted operations.
  410. * (3) "name" is the pathname.
  411. * (4) "number" is the numeric value.
  412. *
  413. * Directives held by this structure are "allow_create", "allow_mkdir",
  414. * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
  415. * and "allow_chgrp".
  416. *
  417. */
  418. struct tomoyo_path_number_acl {
  419. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
  420. u8 perm;
  421. struct tomoyo_name_union name;
  422. struct tomoyo_number_union number;
  423. };
  424. /*
  425. * tomoyo_mkdev_acl is a structure which is used for holding an
  426. * entry with one pathname and three numbers operation.
  427. * It has following fields.
  428. *
  429. * (1) "head" which is a "struct tomoyo_acl_info".
  430. * (2) "perm" which is a bitmask of permitted operations.
  431. * (3) "mode" is the create mode.
  432. * (4) "major" is the major number of device node.
  433. * (5) "minor" is the minor number of device node.
  434. *
  435. * Directives held by this structure are "allow_mkchar", "allow_mkblock".
  436. *
  437. */
  438. struct tomoyo_mkdev_acl {
  439. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
  440. u8 perm;
  441. struct tomoyo_name_union name;
  442. struct tomoyo_number_union mode;
  443. struct tomoyo_number_union major;
  444. struct tomoyo_number_union minor;
  445. };
  446. /*
  447. * tomoyo_path2_acl is a structure which is used for holding an
  448. * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
  449. * It has following fields.
  450. *
  451. * (1) "head" which is a "struct tomoyo_acl_info".
  452. * (2) "perm" which is a bitmask of permitted operations.
  453. * (3) "name1" is the source/old pathname.
  454. * (4) "name2" is the destination/new pathname.
  455. *
  456. * Directives held by this structure are "allow_rename", "allow_link" and
  457. * "allow_pivot_root".
  458. */
  459. struct tomoyo_path2_acl {
  460. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  461. u8 perm;
  462. struct tomoyo_name_union name1;
  463. struct tomoyo_name_union name2;
  464. };
  465. /*
  466. * tomoyo_mount_acl is a structure which is used for holding an
  467. * entry for mount operation.
  468. * It has following fields.
  469. *
  470. * (1) "head" which is a "struct tomoyo_acl_info".
  471. * (2) "dev_name" is the device name.
  472. * (3) "dir_name" is the mount point.
  473. * (4) "fs_type" is the filesystem type.
  474. * (5) "flags" is the mount flags.
  475. *
  476. * Directive held by this structure is "allow_mount".
  477. */
  478. struct tomoyo_mount_acl {
  479. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
  480. struct tomoyo_name_union dev_name;
  481. struct tomoyo_name_union dir_name;
  482. struct tomoyo_name_union fs_type;
  483. struct tomoyo_number_union flags;
  484. };
  485. /*
  486. * tomoyo_io_buffer is a structure which is used for reading and modifying
  487. * configuration via /sys/kernel/security/tomoyo/ interface.
  488. * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
  489. * cursors.
  490. *
  491. * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
  492. * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
  493. * entry has a list of "struct tomoyo_acl_info", we need two cursors when
  494. * reading (one is for traversing tomoyo_domain_list and the other is for
  495. * traversing "struct tomoyo_acl_info"->acl_info_list ).
  496. *
  497. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  498. * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
  499. * domain with the domainname specified by the rest of that line (NULL is set
  500. * if seek failed).
  501. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  502. * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
  503. * line (->write_var1 is set to NULL if a domain was deleted).
  504. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  505. * neither "select " nor "delete ", an entry or a domain specified by that line
  506. * is appended.
  507. */
  508. struct tomoyo_io_buffer {
  509. void (*read) (struct tomoyo_io_buffer *);
  510. int (*write) (struct tomoyo_io_buffer *);
  511. int (*poll) (struct file *file, poll_table *wait);
  512. /* Exclusive lock for this structure. */
  513. struct mutex io_sem;
  514. /* Index returned by tomoyo_read_lock(). */
  515. int reader_idx;
  516. /* The position currently reading from. */
  517. struct list_head *read_var1;
  518. /* Extra variables for reading. */
  519. struct list_head *read_var2;
  520. /* The position currently writing to. */
  521. struct tomoyo_domain_info *write_var1;
  522. /* The step for reading. */
  523. int read_step;
  524. /* Buffer for reading. */
  525. char *read_buf;
  526. /* EOF flag for reading. */
  527. bool read_eof;
  528. /* Read domain ACL of specified PID? */
  529. bool read_single_domain;
  530. /* Extra variable for reading. */
  531. u8 read_bit;
  532. /* Read only TOMOYO_TYPE_EXECUTE */
  533. bool print_execute_only;
  534. /* Bytes available for reading. */
  535. int read_avail;
  536. /* Size of read buffer. */
  537. int readbuf_size;
  538. /* Buffer for writing. */
  539. char *write_buf;
  540. /* Bytes available for writing. */
  541. int write_avail;
  542. /* Size of write buffer. */
  543. int writebuf_size;
  544. /* Type of this interface. */
  545. u8 type;
  546. };
  547. /*
  548. * tomoyo_globally_readable_file_entry is a structure which is used for holding
  549. * "allow_read" entries.
  550. * It has following fields.
  551. *
  552. * (1) "head" is "struct tomoyo_acl_head".
  553. * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
  554. */
  555. struct tomoyo_globally_readable_file_entry {
  556. struct tomoyo_acl_head head;
  557. const struct tomoyo_path_info *filename;
  558. };
  559. /*
  560. * tomoyo_pattern_entry is a structure which is used for holding
  561. * "file_pattern" entries.
  562. * It has following fields.
  563. *
  564. * (1) "head" is "struct tomoyo_acl_head".
  565. * (2) "pattern" is a pathname pattern which is used for converting pathnames
  566. * to pathname patterns during learning mode.
  567. */
  568. struct tomoyo_pattern_entry {
  569. struct tomoyo_acl_head head;
  570. const struct tomoyo_path_info *pattern;
  571. };
  572. /*
  573. * tomoyo_no_rewrite_entry is a structure which is used for holding
  574. * "deny_rewrite" entries.
  575. * It has following fields.
  576. *
  577. * (1) "head" is "struct tomoyo_acl_head".
  578. * (2) "pattern" is a pathname which is by default not permitted to modify
  579. * already existing content.
  580. */
  581. struct tomoyo_no_rewrite_entry {
  582. struct tomoyo_acl_head head;
  583. const struct tomoyo_path_info *pattern;
  584. };
  585. /*
  586. * tomoyo_transition_control is a structure which is used for holding
  587. * "initialize_domain"/"no_initialize_domain"/"keep_domain"/"no_keep_domain"
  588. * entries.
  589. * It has following fields.
  590. *
  591. * (1) "head" is "struct tomoyo_acl_head".
  592. * (2) "type" is type of this entry.
  593. * (3) "is_last_name" is a bool which is true if "domainname" is "the last
  594. * component of a domainname", false otherwise.
  595. * (4) "domainname" which is "a domainname" or "the last component of a
  596. * domainname".
  597. * (5) "program" which is a program's pathname.
  598. */
  599. struct tomoyo_transition_control {
  600. struct tomoyo_acl_head head;
  601. u8 type; /* One of values in "enum tomoyo_transition_type". */
  602. /* True if the domainname is tomoyo_get_last_name(). */
  603. bool is_last_name;
  604. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  605. const struct tomoyo_path_info *program; /* Maybe NULL */
  606. };
  607. /*
  608. * tomoyo_aggregator_entry is a structure which is used for holding
  609. * "aggregator" entries.
  610. * It has following fields.
  611. *
  612. * (1) "head" is "struct tomoyo_acl_head".
  613. * (2) "original_name" which is originally requested name.
  614. * (3) "aggregated_name" which is name to rewrite.
  615. */
  616. struct tomoyo_aggregator_entry {
  617. struct tomoyo_acl_head head;
  618. const struct tomoyo_path_info *original_name;
  619. const struct tomoyo_path_info *aggregated_name;
  620. };
  621. /*
  622. * tomoyo_policy_manager_entry is a structure which is used for holding list of
  623. * domainnames or programs which are permitted to modify configuration via
  624. * /sys/kernel/security/tomoyo/ interface.
  625. * It has following fields.
  626. *
  627. * (1) "head" is "struct tomoyo_acl_head".
  628. * (2) "is_domain" is a bool which is true if "manager" is a domainname, false
  629. * otherwise.
  630. * (3) "manager" is a domainname or a program's pathname.
  631. */
  632. struct tomoyo_policy_manager_entry {
  633. struct tomoyo_acl_head head;
  634. bool is_domain; /* True if manager is a domainname. */
  635. /* A path to program or a domainname. */
  636. const struct tomoyo_path_info *manager;
  637. };
  638. struct tomoyo_preference {
  639. unsigned int learning_max_entry;
  640. bool enforcing_verbose;
  641. bool learning_verbose;
  642. bool permissive_verbose;
  643. };
  644. struct tomoyo_profile {
  645. const struct tomoyo_path_info *comment;
  646. struct tomoyo_preference *learning;
  647. struct tomoyo_preference *permissive;
  648. struct tomoyo_preference *enforcing;
  649. struct tomoyo_preference preference;
  650. u8 default_config;
  651. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  652. };
  653. /********** Function prototypes. **********/
  654. extern asmlinkage long sys_getpid(void);
  655. extern asmlinkage long sys_getppid(void);
  656. /* Check whether the given string starts with the given keyword. */
  657. bool tomoyo_str_starts(char **src, const char *find);
  658. /* Get tomoyo_realpath() of current process. */
  659. const char *tomoyo_get_exe(void);
  660. /* Format string. */
  661. void tomoyo_normalize_line(unsigned char *buffer);
  662. /* Print warning or error message on console. */
  663. void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
  664. __attribute__ ((format(printf, 2, 3)));
  665. /* Check all profiles currently assigned to domains are defined. */
  666. void tomoyo_check_profile(void);
  667. /* Open operation for /sys/kernel/security/tomoyo/ interface. */
  668. int tomoyo_open_control(const u8 type, struct file *file);
  669. /* Close /sys/kernel/security/tomoyo/ interface. */
  670. int tomoyo_close_control(struct file *file);
  671. /* Read operation for /sys/kernel/security/tomoyo/ interface. */
  672. int tomoyo_read_control(struct file *file, char __user *buffer,
  673. const int buffer_len);
  674. /* Write operation for /sys/kernel/security/tomoyo/ interface. */
  675. int tomoyo_write_control(struct file *file, const char __user *buffer,
  676. const int buffer_len);
  677. /* Check whether the domain has too many ACL entries to hold. */
  678. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  679. /* Print out of memory warning message. */
  680. void tomoyo_warn_oom(const char *function);
  681. /* Check whether the given name matches the given name_union. */
  682. bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  683. const struct tomoyo_name_union *ptr);
  684. /* Check whether the given number matches the given number_union. */
  685. bool tomoyo_compare_number_union(const unsigned long value,
  686. const struct tomoyo_number_union *ptr);
  687. int tomoyo_get_mode(const u8 profile, const u8 index);
  688. /* Transactional sprintf() for policy dump. */
  689. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  690. __attribute__ ((format(printf, 2, 3)));
  691. /* Check whether the domainname is correct. */
  692. bool tomoyo_correct_domain(const unsigned char *domainname);
  693. /* Check whether the token is correct. */
  694. bool tomoyo_correct_path(const char *filename);
  695. bool tomoyo_correct_word(const char *string);
  696. /* Check whether the token can be a domainname. */
  697. bool tomoyo_domain_def(const unsigned char *buffer);
  698. bool tomoyo_parse_name_union(const char *filename,
  699. struct tomoyo_name_union *ptr);
  700. /* Check whether the given filename matches the given path_group. */
  701. bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  702. const struct tomoyo_group *group);
  703. /* Check whether the given value matches the given number_group. */
  704. bool tomoyo_number_matches_group(const unsigned long min,
  705. const unsigned long max,
  706. const struct tomoyo_group *group);
  707. /* Check whether the given filename matches the given pattern. */
  708. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  709. const struct tomoyo_path_info *pattern);
  710. bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
  711. const struct tomoyo_number_union *ptr);
  712. bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
  713. /* Tokenize a line. */
  714. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  715. /* Write domain policy violation warning message to console? */
  716. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  717. /* Get the last component of the given domainname. */
  718. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
  719. /* Fill "struct tomoyo_request_info". */
  720. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  721. struct tomoyo_domain_info *domain,
  722. const u8 index);
  723. /* Check permission for mount operation. */
  724. int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
  725. unsigned long flags, void *data_page);
  726. /* Create "aggregator" entry in exception policy. */
  727. int tomoyo_write_aggregator_policy(char *data, const bool is_delete);
  728. int tomoyo_write_transition_control(char *data, const bool is_delete,
  729. const u8 type);
  730. /*
  731. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  732. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  733. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  734. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  735. * "allow_link" entry in domain policy.
  736. */
  737. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  738. const bool is_delete);
  739. /* Create "allow_read" entry in exception policy. */
  740. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
  741. /* Create "allow_mount" entry in domain policy. */
  742. int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
  743. const bool is_delete);
  744. /* Create "deny_rewrite" entry in exception policy. */
  745. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
  746. /* Create "file_pattern" entry in exception policy. */
  747. int tomoyo_write_pattern_policy(char *data, const bool is_delete);
  748. /* Create "path_group"/"number_group" entry in exception policy. */
  749. int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
  750. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  751. __attribute__ ((format(printf, 2, 3)));
  752. /* Find a domain by the given name. */
  753. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  754. /* Find or create a domain by the given name. */
  755. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  756. domainname,
  757. const u8 profile);
  758. struct tomoyo_profile *tomoyo_profile(const u8 profile);
  759. /*
  760. * Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  761. */
  762. struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
  763. /* Check mode for specified functionality. */
  764. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  765. const u8 index);
  766. /* Fill in "struct tomoyo_path_info" members. */
  767. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  768. /* Run policy loader when /sbin/init starts. */
  769. void tomoyo_load_policy(const char *filename);
  770. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  771. /* Convert binary string to ascii string. */
  772. char *tomoyo_encode(const char *str);
  773. /*
  774. * Returns realpath(3) of the given pathname except that
  775. * ignores chroot'ed root and does not follow the final symlink.
  776. */
  777. char *tomoyo_realpath_nofollow(const char *pathname);
  778. /*
  779. * Returns realpath(3) of the given pathname except that
  780. * ignores chroot'ed root and the pathname is already solved.
  781. */
  782. char *tomoyo_realpath_from_path(struct path *path);
  783. /* Get patterned pathname. */
  784. const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
  785. /* Check memory quota. */
  786. bool tomoyo_memory_ok(void *ptr);
  787. void *tomoyo_commit_ok(void *data, const unsigned int size);
  788. /*
  789. * Keep the given name on the RAM.
  790. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  791. */
  792. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  793. /* Check for memory usage. */
  794. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  795. /* Set memory quota. */
  796. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  797. /* Initialize mm related code. */
  798. void __init tomoyo_mm_init(void);
  799. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  800. const struct tomoyo_path_info *filename);
  801. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  802. struct path *path, const int flag);
  803. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  804. unsigned long number);
  805. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  806. const unsigned int mode, unsigned int dev);
  807. int tomoyo_path_perm(const u8 operation, struct path *path);
  808. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  809. struct path *path2);
  810. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  811. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  812. const unsigned long value, const u8 type);
  813. /* Drop refcount on tomoyo_name_union. */
  814. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  815. /* Run garbage collector. */
  816. void tomoyo_run_gc(void);
  817. void tomoyo_memory_free(void *ptr);
  818. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  819. bool is_delete, struct tomoyo_domain_info *domain,
  820. bool (*check_duplicate) (const struct tomoyo_acl_info
  821. *,
  822. const struct tomoyo_acl_info
  823. *),
  824. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  825. struct tomoyo_acl_info *,
  826. const bool));
  827. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  828. bool is_delete, struct list_head *list,
  829. bool (*check_duplicate) (const struct tomoyo_acl_head
  830. *,
  831. const struct tomoyo_acl_head
  832. *));
  833. void tomoyo_check_acl(struct tomoyo_request_info *r,
  834. bool (*check_entry) (const struct tomoyo_request_info *,
  835. const struct tomoyo_acl_info *));
  836. const char *tomoyo_last_word(const char *name);
  837. /********** External variable definitions. **********/
  838. /* Lock for GC. */
  839. extern struct srcu_struct tomoyo_ss;
  840. /* The list for "struct tomoyo_domain_info". */
  841. extern struct list_head tomoyo_domain_list;
  842. extern struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
  843. extern struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
  844. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  845. /* Lock for protecting policy. */
  846. extern struct mutex tomoyo_policy_lock;
  847. /* Has /sbin/init started? */
  848. extern bool tomoyo_policy_loaded;
  849. /* The kernel's domain. */
  850. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  851. extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  852. extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
  853. extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
  854. extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  855. extern unsigned int tomoyo_quota_for_query;
  856. extern unsigned int tomoyo_query_memory_size;
  857. /********** Inlined functions. **********/
  858. static inline int tomoyo_read_lock(void)
  859. {
  860. return srcu_read_lock(&tomoyo_ss);
  861. }
  862. static inline void tomoyo_read_unlock(int idx)
  863. {
  864. srcu_read_unlock(&tomoyo_ss, idx);
  865. }
  866. /* strcmp() for "struct tomoyo_path_info" structure. */
  867. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  868. const struct tomoyo_path_info *b)
  869. {
  870. return a->hash != b->hash || strcmp(a->name, b->name);
  871. }
  872. /**
  873. * tomoyo_valid - Check whether the character is a valid char.
  874. *
  875. * @c: The character to check.
  876. *
  877. * Returns true if @c is a valid character, false otherwise.
  878. */
  879. static inline bool tomoyo_valid(const unsigned char c)
  880. {
  881. return c > ' ' && c < 127;
  882. }
  883. /**
  884. * tomoyo_invalid - Check whether the character is an invalid char.
  885. *
  886. * @c: The character to check.
  887. *
  888. * Returns true if @c is an invalid character, false otherwise.
  889. */
  890. static inline bool tomoyo_invalid(const unsigned char c)
  891. {
  892. return c && (c <= ' ' || c >= 127);
  893. }
  894. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  895. {
  896. if (name) {
  897. struct tomoyo_name_entry *ptr =
  898. container_of(name, struct tomoyo_name_entry, entry);
  899. atomic_dec(&ptr->users);
  900. }
  901. }
  902. static inline void tomoyo_put_group(struct tomoyo_group *group)
  903. {
  904. if (group)
  905. atomic_dec(&group->users);
  906. }
  907. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  908. {
  909. return current_cred()->security;
  910. }
  911. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  912. *task)
  913. {
  914. return task_cred_xxx(task, security);
  915. }
  916. static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *p1,
  917. const struct tomoyo_acl_info *p2)
  918. {
  919. return p1->type == p2->type;
  920. }
  921. static inline bool tomoyo_same_name_union
  922. (const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
  923. {
  924. return p1->filename == p2->filename && p1->group == p2->group &&
  925. p1->is_group == p2->is_group;
  926. }
  927. static inline bool tomoyo_same_number_union
  928. (const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
  929. {
  930. return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
  931. && p1->group == p2->group && p1->min_type == p2->min_type &&
  932. p1->max_type == p2->max_type && p1->is_group == p2->is_group;
  933. }
  934. /**
  935. * list_for_each_cookie - iterate over a list with cookie.
  936. * @pos: the &struct list_head to use as a loop cursor.
  937. * @head: the head for your list.
  938. */
  939. #define list_for_each_cookie(pos, head) \
  940. if (!pos) \
  941. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  942. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  943. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */