common.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. #define TOMOYO_MAX_IO_READ_QUEUE 32
  486. /*
  487. * Structure for reading/writing policy via /sys/kernel/security/tomoyo
  488. * interfaces.
  489. */
  490. struct tomoyo_io_buffer {
  491. void (*read) (struct tomoyo_io_buffer *);
  492. int (*write) (struct tomoyo_io_buffer *);
  493. int (*poll) (struct file *file, poll_table *wait);
  494. /* Exclusive lock for this structure. */
  495. struct mutex io_sem;
  496. /* Index returned by tomoyo_read_lock(). */
  497. int reader_idx;
  498. char __user *read_user_buf;
  499. int read_user_buf_avail;
  500. struct {
  501. struct list_head *domain;
  502. struct list_head *group;
  503. struct list_head *acl;
  504. int avail;
  505. int step;
  506. int query_index;
  507. u16 index;
  508. u8 bit;
  509. u8 w_pos;
  510. bool eof;
  511. bool print_this_domain_only;
  512. bool print_execute_only;
  513. const char *w[TOMOYO_MAX_IO_READ_QUEUE];
  514. } r;
  515. /* The position currently writing to. */
  516. struct tomoyo_domain_info *write_var1;
  517. /* Buffer for reading. */
  518. char *read_buf;
  519. /* Size of read buffer. */
  520. int readbuf_size;
  521. /* Buffer for writing. */
  522. char *write_buf;
  523. /* Bytes available for writing. */
  524. int write_avail;
  525. /* Size of write buffer. */
  526. int writebuf_size;
  527. /* Type of this interface. */
  528. u8 type;
  529. };
  530. /*
  531. * tomoyo_globally_readable_file_entry is a structure which is used for holding
  532. * "allow_read" entries.
  533. * It has following fields.
  534. *
  535. * (1) "head" is "struct tomoyo_acl_head".
  536. * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
  537. */
  538. struct tomoyo_globally_readable_file_entry {
  539. struct tomoyo_acl_head head;
  540. const struct tomoyo_path_info *filename;
  541. };
  542. /*
  543. * tomoyo_pattern_entry is a structure which is used for holding
  544. * "file_pattern" entries.
  545. * It has following fields.
  546. *
  547. * (1) "head" is "struct tomoyo_acl_head".
  548. * (2) "pattern" is a pathname pattern which is used for converting pathnames
  549. * to pathname patterns during learning mode.
  550. */
  551. struct tomoyo_pattern_entry {
  552. struct tomoyo_acl_head head;
  553. const struct tomoyo_path_info *pattern;
  554. };
  555. /*
  556. * tomoyo_no_rewrite_entry is a structure which is used for holding
  557. * "deny_rewrite" entries.
  558. * It has following fields.
  559. *
  560. * (1) "head" is "struct tomoyo_acl_head".
  561. * (2) "pattern" is a pathname which is by default not permitted to modify
  562. * already existing content.
  563. */
  564. struct tomoyo_no_rewrite_entry {
  565. struct tomoyo_acl_head head;
  566. const struct tomoyo_path_info *pattern;
  567. };
  568. /*
  569. * tomoyo_transition_control is a structure which is used for holding
  570. * "initialize_domain"/"no_initialize_domain"/"keep_domain"/"no_keep_domain"
  571. * entries.
  572. * It has following fields.
  573. *
  574. * (1) "head" is "struct tomoyo_acl_head".
  575. * (2) "type" is type of this entry.
  576. * (3) "is_last_name" is a bool which is true if "domainname" is "the last
  577. * component of a domainname", false otherwise.
  578. * (4) "domainname" which is "a domainname" or "the last component of a
  579. * domainname".
  580. * (5) "program" which is a program's pathname.
  581. */
  582. struct tomoyo_transition_control {
  583. struct tomoyo_acl_head head;
  584. u8 type; /* One of values in "enum tomoyo_transition_type". */
  585. /* True if the domainname is tomoyo_get_last_name(). */
  586. bool is_last_name;
  587. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  588. const struct tomoyo_path_info *program; /* Maybe NULL */
  589. };
  590. /*
  591. * tomoyo_aggregator_entry is a structure which is used for holding
  592. * "aggregator" entries.
  593. * It has following fields.
  594. *
  595. * (1) "head" is "struct tomoyo_acl_head".
  596. * (2) "original_name" which is originally requested name.
  597. * (3) "aggregated_name" which is name to rewrite.
  598. */
  599. struct tomoyo_aggregator_entry {
  600. struct tomoyo_acl_head head;
  601. const struct tomoyo_path_info *original_name;
  602. const struct tomoyo_path_info *aggregated_name;
  603. };
  604. /*
  605. * tomoyo_policy_manager_entry is a structure which is used for holding list of
  606. * domainnames or programs which are permitted to modify configuration via
  607. * /sys/kernel/security/tomoyo/ interface.
  608. * It has following fields.
  609. *
  610. * (1) "head" is "struct tomoyo_acl_head".
  611. * (2) "is_domain" is a bool which is true if "manager" is a domainname, false
  612. * otherwise.
  613. * (3) "manager" is a domainname or a program's pathname.
  614. */
  615. struct tomoyo_policy_manager_entry {
  616. struct tomoyo_acl_head head;
  617. bool is_domain; /* True if manager is a domainname. */
  618. /* A path to program or a domainname. */
  619. const struct tomoyo_path_info *manager;
  620. };
  621. struct tomoyo_preference {
  622. unsigned int learning_max_entry;
  623. bool enforcing_verbose;
  624. bool learning_verbose;
  625. bool permissive_verbose;
  626. };
  627. struct tomoyo_profile {
  628. const struct tomoyo_path_info *comment;
  629. struct tomoyo_preference *learning;
  630. struct tomoyo_preference *permissive;
  631. struct tomoyo_preference *enforcing;
  632. struct tomoyo_preference preference;
  633. u8 default_config;
  634. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  635. };
  636. /********** Function prototypes. **********/
  637. extern asmlinkage long sys_getpid(void);
  638. extern asmlinkage long sys_getppid(void);
  639. /* Check whether the given string starts with the given keyword. */
  640. bool tomoyo_str_starts(char **src, const char *find);
  641. /* Get tomoyo_realpath() of current process. */
  642. const char *tomoyo_get_exe(void);
  643. /* Format string. */
  644. void tomoyo_normalize_line(unsigned char *buffer);
  645. /* Print warning or error message on console. */
  646. void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
  647. __attribute__ ((format(printf, 2, 3)));
  648. /* Check all profiles currently assigned to domains are defined. */
  649. void tomoyo_check_profile(void);
  650. /* Open operation for /sys/kernel/security/tomoyo/ interface. */
  651. int tomoyo_open_control(const u8 type, struct file *file);
  652. /* Close /sys/kernel/security/tomoyo/ interface. */
  653. int tomoyo_close_control(struct file *file);
  654. /* Read operation for /sys/kernel/security/tomoyo/ interface. */
  655. int tomoyo_read_control(struct file *file, char __user *buffer,
  656. const int buffer_len);
  657. /* Write operation for /sys/kernel/security/tomoyo/ interface. */
  658. int tomoyo_write_control(struct file *file, const char __user *buffer,
  659. const int buffer_len);
  660. /* Check whether the domain has too many ACL entries to hold. */
  661. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  662. /* Print out of memory warning message. */
  663. void tomoyo_warn_oom(const char *function);
  664. /* Check whether the given name matches the given name_union. */
  665. bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  666. const struct tomoyo_name_union *ptr);
  667. /* Check whether the given number matches the given number_union. */
  668. bool tomoyo_compare_number_union(const unsigned long value,
  669. const struct tomoyo_number_union *ptr);
  670. int tomoyo_get_mode(const u8 profile, const u8 index);
  671. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  672. __attribute__ ((format(printf, 2, 3)));
  673. /* Check whether the domainname is correct. */
  674. bool tomoyo_correct_domain(const unsigned char *domainname);
  675. /* Check whether the token is correct. */
  676. bool tomoyo_correct_path(const char *filename);
  677. bool tomoyo_correct_word(const char *string);
  678. /* Check whether the token can be a domainname. */
  679. bool tomoyo_domain_def(const unsigned char *buffer);
  680. bool tomoyo_parse_name_union(const char *filename,
  681. struct tomoyo_name_union *ptr);
  682. /* Check whether the given filename matches the given path_group. */
  683. bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  684. const struct tomoyo_group *group);
  685. /* Check whether the given value matches the given number_group. */
  686. bool tomoyo_number_matches_group(const unsigned long min,
  687. const unsigned long max,
  688. const struct tomoyo_group *group);
  689. /* Check whether the given filename matches the given pattern. */
  690. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  691. const struct tomoyo_path_info *pattern);
  692. bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
  693. /* Tokenize a line. */
  694. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  695. /* Write domain policy violation warning message to console? */
  696. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  697. /* Get the last component of the given domainname. */
  698. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
  699. /* Fill "struct tomoyo_request_info". */
  700. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  701. struct tomoyo_domain_info *domain,
  702. const u8 index);
  703. /* Check permission for mount operation. */
  704. int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
  705. unsigned long flags, void *data_page);
  706. /* Create "aggregator" entry in exception policy. */
  707. int tomoyo_write_aggregator_policy(char *data, const bool is_delete);
  708. int tomoyo_write_transition_control(char *data, const bool is_delete,
  709. const u8 type);
  710. /*
  711. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  712. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  713. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  714. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  715. * "allow_link" entry in domain policy.
  716. */
  717. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  718. const bool is_delete);
  719. /* Create "allow_read" entry in exception policy. */
  720. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
  721. /* Create "allow_mount" entry in domain policy. */
  722. int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
  723. const bool is_delete);
  724. /* Create "deny_rewrite" entry in exception policy. */
  725. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
  726. /* Create "file_pattern" entry in exception policy. */
  727. int tomoyo_write_pattern_policy(char *data, const bool is_delete);
  728. /* Create "path_group"/"number_group" entry in exception policy. */
  729. int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
  730. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  731. __attribute__ ((format(printf, 2, 3)));
  732. /* Find a domain by the given name. */
  733. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  734. /* Find or create a domain by the given name. */
  735. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  736. domainname,
  737. const u8 profile);
  738. struct tomoyo_profile *tomoyo_profile(const u8 profile);
  739. /*
  740. * Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  741. */
  742. struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
  743. /* Check mode for specified functionality. */
  744. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  745. const u8 index);
  746. /* Fill in "struct tomoyo_path_info" members. */
  747. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  748. /* Run policy loader when /sbin/init starts. */
  749. void tomoyo_load_policy(const char *filename);
  750. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  751. /* Convert binary string to ascii string. */
  752. char *tomoyo_encode(const char *str);
  753. /*
  754. * Returns realpath(3) of the given pathname except that
  755. * ignores chroot'ed root and does not follow the final symlink.
  756. */
  757. char *tomoyo_realpath_nofollow(const char *pathname);
  758. /*
  759. * Returns realpath(3) of the given pathname except that
  760. * ignores chroot'ed root and the pathname is already solved.
  761. */
  762. char *tomoyo_realpath_from_path(struct path *path);
  763. /* Get patterned pathname. */
  764. const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
  765. /* Check memory quota. */
  766. bool tomoyo_memory_ok(void *ptr);
  767. void *tomoyo_commit_ok(void *data, const unsigned int size);
  768. /*
  769. * Keep the given name on the RAM.
  770. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  771. */
  772. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  773. /* Check for memory usage. */
  774. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  775. /* Set memory quota. */
  776. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  777. /* Initialize mm related code. */
  778. void __init tomoyo_mm_init(void);
  779. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  780. const struct tomoyo_path_info *filename);
  781. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  782. struct path *path, const int flag);
  783. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  784. unsigned long number);
  785. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  786. const unsigned int mode, unsigned int dev);
  787. int tomoyo_path_perm(const u8 operation, struct path *path);
  788. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  789. struct path *path2);
  790. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  791. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  792. const unsigned long value, const u8 type);
  793. /* Drop refcount on tomoyo_name_union. */
  794. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  795. /* Run garbage collector. */
  796. void tomoyo_run_gc(void);
  797. void tomoyo_memory_free(void *ptr);
  798. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  799. bool is_delete, struct tomoyo_domain_info *domain,
  800. bool (*check_duplicate) (const struct tomoyo_acl_info
  801. *,
  802. const struct tomoyo_acl_info
  803. *),
  804. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  805. struct tomoyo_acl_info *,
  806. const bool));
  807. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  808. bool is_delete, struct list_head *list,
  809. bool (*check_duplicate) (const struct tomoyo_acl_head
  810. *,
  811. const struct tomoyo_acl_head
  812. *));
  813. void tomoyo_check_acl(struct tomoyo_request_info *r,
  814. bool (*check_entry) (const struct tomoyo_request_info *,
  815. const struct tomoyo_acl_info *));
  816. const char *tomoyo_last_word(const char *name);
  817. /********** External variable definitions. **********/
  818. /* Lock for GC. */
  819. extern struct srcu_struct tomoyo_ss;
  820. /* The list for "struct tomoyo_domain_info". */
  821. extern struct list_head tomoyo_domain_list;
  822. extern struct list_head tomoyo_policy_list[TOMOYO_MAX_POLICY];
  823. extern struct list_head tomoyo_group_list[TOMOYO_MAX_GROUP];
  824. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  825. /* Lock for protecting policy. */
  826. extern struct mutex tomoyo_policy_lock;
  827. /* Has /sbin/init started? */
  828. extern bool tomoyo_policy_loaded;
  829. /* The kernel's domain. */
  830. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  831. extern const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  832. extern const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION];
  833. extern const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION];
  834. extern const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  835. extern unsigned int tomoyo_quota_for_query;
  836. extern unsigned int tomoyo_query_memory_size;
  837. /********** Inlined functions. **********/
  838. static inline int tomoyo_read_lock(void)
  839. {
  840. return srcu_read_lock(&tomoyo_ss);
  841. }
  842. static inline void tomoyo_read_unlock(int idx)
  843. {
  844. srcu_read_unlock(&tomoyo_ss, idx);
  845. }
  846. /* strcmp() for "struct tomoyo_path_info" structure. */
  847. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  848. const struct tomoyo_path_info *b)
  849. {
  850. return a->hash != b->hash || strcmp(a->name, b->name);
  851. }
  852. /**
  853. * tomoyo_valid - Check whether the character is a valid char.
  854. *
  855. * @c: The character to check.
  856. *
  857. * Returns true if @c is a valid character, false otherwise.
  858. */
  859. static inline bool tomoyo_valid(const unsigned char c)
  860. {
  861. return c > ' ' && c < 127;
  862. }
  863. /**
  864. * tomoyo_invalid - Check whether the character is an invalid char.
  865. *
  866. * @c: The character to check.
  867. *
  868. * Returns true if @c is an invalid character, false otherwise.
  869. */
  870. static inline bool tomoyo_invalid(const unsigned char c)
  871. {
  872. return c && (c <= ' ' || c >= 127);
  873. }
  874. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  875. {
  876. if (name) {
  877. struct tomoyo_name_entry *ptr =
  878. container_of(name, struct tomoyo_name_entry, entry);
  879. atomic_dec(&ptr->users);
  880. }
  881. }
  882. static inline void tomoyo_put_group(struct tomoyo_group *group)
  883. {
  884. if (group)
  885. atomic_dec(&group->users);
  886. }
  887. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  888. {
  889. return current_cred()->security;
  890. }
  891. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  892. *task)
  893. {
  894. return task_cred_xxx(task, security);
  895. }
  896. static inline bool tomoyo_same_acl_head(const struct tomoyo_acl_info *p1,
  897. const struct tomoyo_acl_info *p2)
  898. {
  899. return p1->type == p2->type;
  900. }
  901. static inline bool tomoyo_same_name_union
  902. (const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
  903. {
  904. return p1->filename == p2->filename && p1->group == p2->group &&
  905. p1->is_group == p2->is_group;
  906. }
  907. static inline bool tomoyo_same_number_union
  908. (const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
  909. {
  910. return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
  911. && p1->group == p2->group && p1->min_type == p2->min_type &&
  912. p1->max_type == p2->max_type && p1->is_group == p2->is_group;
  913. }
  914. /**
  915. * list_for_each_cookie - iterate over a list with cookie.
  916. * @pos: the &struct list_head to use as a loop cursor.
  917. * @head: the head for your list.
  918. */
  919. #define list_for_each_cookie(pos, head) \
  920. if (!pos) \
  921. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  922. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  923. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */