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. /* For using wildcards at tomoyo_find_next_domain(). */
  223. const struct tomoyo_path_info *matched_path;
  224. u8 operation;
  225. } path;
  226. struct {
  227. const struct tomoyo_path_info *filename1;
  228. const struct tomoyo_path_info *filename2;
  229. u8 operation;
  230. } path2;
  231. struct {
  232. const struct tomoyo_path_info *filename;
  233. unsigned int mode;
  234. unsigned int major;
  235. unsigned int minor;
  236. u8 operation;
  237. } mkdev;
  238. struct {
  239. const struct tomoyo_path_info *filename;
  240. unsigned long number;
  241. u8 operation;
  242. } path_number;
  243. struct {
  244. const struct tomoyo_path_info *type;
  245. const struct tomoyo_path_info *dir;
  246. const struct tomoyo_path_info *dev;
  247. unsigned long flags;
  248. int need_dev;
  249. } mount;
  250. } param;
  251. u8 param_type;
  252. bool granted;
  253. u8 retry;
  254. u8 profile;
  255. u8 mode; /* One of tomoyo_mode_index . */
  256. u8 type;
  257. };
  258. /*
  259. * tomoyo_path_info is a structure which is used for holding a string data
  260. * used by TOMOYO.
  261. * This structure has several fields for supporting pattern matching.
  262. *
  263. * (1) "name" is the '\0' terminated string data.
  264. * (2) "hash" is full_name_hash(name, strlen(name)).
  265. * This allows tomoyo_pathcmp() to compare by hash before actually compare
  266. * using strcmp().
  267. * (3) "const_len" is the length of the initial segment of "name" which
  268. * consists entirely of non wildcard characters. In other words, the length
  269. * which we can compare two strings using strncmp().
  270. * (4) "is_dir" is a bool which is true if "name" ends with "/",
  271. * false otherwise.
  272. * TOMOYO distinguishes directory and non-directory. A directory ends with
  273. * "/" and non-directory does not end with "/".
  274. * (5) "is_patterned" is a bool which is true if "name" contains wildcard
  275. * characters, false otherwise. This allows TOMOYO to use "hash" and
  276. * strcmp() for string comparison if "is_patterned" is false.
  277. */
  278. struct tomoyo_path_info {
  279. const char *name;
  280. u32 hash; /* = full_name_hash(name, strlen(name)) */
  281. u16 const_len; /* = tomoyo_const_part_length(name) */
  282. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  283. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  284. };
  285. /*
  286. * tomoyo_name is a structure which is used for linking
  287. * "struct tomoyo_path_info" into tomoyo_name_list .
  288. */
  289. struct tomoyo_name {
  290. struct list_head list;
  291. atomic_t users;
  292. struct tomoyo_path_info entry;
  293. };
  294. struct tomoyo_name_union {
  295. const struct tomoyo_path_info *filename;
  296. struct tomoyo_group *group;
  297. u8 is_group;
  298. };
  299. struct tomoyo_number_union {
  300. unsigned long values[2];
  301. struct tomoyo_group *group;
  302. u8 min_type;
  303. u8 max_type;
  304. u8 is_group;
  305. };
  306. /* Structure for "path_group"/"number_group" directive. */
  307. struct tomoyo_group {
  308. struct list_head list;
  309. const struct tomoyo_path_info *group_name;
  310. struct list_head member_list;
  311. atomic_t users;
  312. };
  313. /* Structure for "path_group" directive. */
  314. struct tomoyo_path_group {
  315. struct tomoyo_acl_head head;
  316. const struct tomoyo_path_info *member_name;
  317. };
  318. /* Structure for "number_group" directive. */
  319. struct tomoyo_number_group {
  320. struct tomoyo_acl_head head;
  321. struct tomoyo_number_union number;
  322. };
  323. /*
  324. * tomoyo_acl_info is a structure which is used for holding
  325. *
  326. * (1) "list" which is linked to the ->acl_info_list of
  327. * "struct tomoyo_domain_info"
  328. * (2) "is_deleted" is a bool which is true if this domain is marked as
  329. * "deleted", false otherwise.
  330. * (3) "type" which tells type of the entry.
  331. *
  332. * Packing "struct tomoyo_acl_info" allows
  333. * "struct tomoyo_path_acl" to embed "u16" and "struct tomoyo_path2_acl"
  334. * "struct tomoyo_path_number_acl" "struct tomoyo_mkdev_acl" to embed
  335. * "u8" without enlarging their structure size.
  336. */
  337. struct tomoyo_acl_info {
  338. struct list_head list;
  339. bool is_deleted;
  340. u8 type; /* = one of values in "enum tomoyo_acl_entry_type_index". */
  341. } __packed;
  342. /*
  343. * tomoyo_domain_info is a structure which is used for holding permissions
  344. * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  345. * It has following fields.
  346. *
  347. * (1) "list" which is linked to tomoyo_domain_list .
  348. * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
  349. * (3) "domainname" which holds the name of the domain.
  350. * (4) "profile" which remembers profile number assigned to this domain.
  351. * (5) "is_deleted" is a bool which is true if this domain is marked as
  352. * "deleted", false otherwise.
  353. * (6) "quota_warned" is a bool which is used for suppressing warning message
  354. * when learning mode learned too much entries.
  355. * (7) "ignore_global_allow_read" is a bool which is true if this domain
  356. * should ignore "allow_read" directive in exception policy.
  357. * (8) "transition_failed" is a bool which is set to true when this domain was
  358. * unable to create a new domain at tomoyo_find_next_domain() because the
  359. * name of the domain to be created was too long or it could not allocate
  360. * memory. If set to true, more than one process continued execve()
  361. * without domain transition.
  362. * (9) "users" is an atomic_t that holds how many "struct cred"->security
  363. * are referring this "struct tomoyo_domain_info". If is_deleted == true
  364. * and users == 0, this struct will be kfree()d upon next garbage
  365. * collection.
  366. *
  367. * A domain's lifecycle is an analogy of files on / directory.
  368. * Multiple domains with the same domainname cannot be created (as with
  369. * creating files with the same filename fails with -EEXIST).
  370. * If a process reached a domain, that process can reside in that domain after
  371. * that domain is marked as "deleted" (as with a process can access an already
  372. * open()ed file after that file was unlink()ed).
  373. */
  374. struct tomoyo_domain_info {
  375. struct list_head list;
  376. struct list_head acl_info_list;
  377. /* Name of this domain. Never NULL. */
  378. const struct tomoyo_path_info *domainname;
  379. u8 profile; /* Profile number to use. */
  380. bool is_deleted; /* Delete flag. */
  381. bool quota_warned; /* Quota warnning flag. */
  382. bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
  383. bool transition_failed; /* Domain transition failed flag. */
  384. atomic_t users; /* Number of referring credentials. */
  385. };
  386. /*
  387. * tomoyo_path_acl is a structure which is used for holding an
  388. * entry with one pathname operation (e.g. open(), mkdir()).
  389. * It has following fields.
  390. *
  391. * (1) "head" which is a "struct tomoyo_acl_info".
  392. * (2) "perm" which is a bitmask of permitted operations.
  393. * (3) "name" is the pathname.
  394. *
  395. * Directives held by this structure are "allow_read/write", "allow_execute",
  396. * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
  397. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
  398. * "allow_unmount".
  399. */
  400. struct tomoyo_path_acl {
  401. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  402. u16 perm;
  403. struct tomoyo_name_union name;
  404. };
  405. /*
  406. * tomoyo_path_number_acl is a structure which is used for holding an
  407. * entry with one pathname and one number operation.
  408. * It has following fields.
  409. *
  410. * (1) "head" which is a "struct tomoyo_acl_info".
  411. * (2) "perm" which is a bitmask of permitted operations.
  412. * (3) "name" is the pathname.
  413. * (4) "number" is the numeric value.
  414. *
  415. * Directives held by this structure are "allow_create", "allow_mkdir",
  416. * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
  417. * and "allow_chgrp".
  418. *
  419. */
  420. struct tomoyo_path_number_acl {
  421. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
  422. u8 perm;
  423. struct tomoyo_name_union name;
  424. struct tomoyo_number_union number;
  425. };
  426. /*
  427. * tomoyo_mkdev_acl is a structure which is used for holding an
  428. * entry with one pathname and three numbers operation.
  429. * It has following fields.
  430. *
  431. * (1) "head" which is a "struct tomoyo_acl_info".
  432. * (2) "perm" which is a bitmask of permitted operations.
  433. * (3) "mode" is the create mode.
  434. * (4) "major" is the major number of device node.
  435. * (5) "minor" is the minor number of device node.
  436. *
  437. * Directives held by this structure are "allow_mkchar", "allow_mkblock".
  438. *
  439. */
  440. struct tomoyo_mkdev_acl {
  441. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
  442. u8 perm;
  443. struct tomoyo_name_union name;
  444. struct tomoyo_number_union mode;
  445. struct tomoyo_number_union major;
  446. struct tomoyo_number_union minor;
  447. };
  448. /*
  449. * tomoyo_path2_acl is a structure which is used for holding an
  450. * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
  451. * It has following fields.
  452. *
  453. * (1) "head" which is a "struct tomoyo_acl_info".
  454. * (2) "perm" which is a bitmask of permitted operations.
  455. * (3) "name1" is the source/old pathname.
  456. * (4) "name2" is the destination/new pathname.
  457. *
  458. * Directives held by this structure are "allow_rename", "allow_link" and
  459. * "allow_pivot_root".
  460. */
  461. struct tomoyo_path2_acl {
  462. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  463. u8 perm;
  464. struct tomoyo_name_union name1;
  465. struct tomoyo_name_union name2;
  466. };
  467. /*
  468. * tomoyo_mount_acl is a structure which is used for holding an
  469. * entry for mount operation.
  470. * It has following fields.
  471. *
  472. * (1) "head" which is a "struct tomoyo_acl_info".
  473. * (2) "dev_name" is the device name.
  474. * (3) "dir_name" is the mount point.
  475. * (4) "fs_type" is the filesystem type.
  476. * (5) "flags" is the mount flags.
  477. *
  478. * Directive held by this structure is "allow_mount".
  479. */
  480. struct tomoyo_mount_acl {
  481. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
  482. struct tomoyo_name_union dev_name;
  483. struct tomoyo_name_union dir_name;
  484. struct tomoyo_name_union fs_type;
  485. struct tomoyo_number_union flags;
  486. };
  487. #define TOMOYO_MAX_IO_READ_QUEUE 32
  488. /*
  489. * Structure for reading/writing policy via /sys/kernel/security/tomoyo
  490. * interfaces.
  491. */
  492. struct tomoyo_io_buffer {
  493. void (*read) (struct tomoyo_io_buffer *);
  494. int (*write) (struct tomoyo_io_buffer *);
  495. int (*poll) (struct file *file, poll_table *wait);
  496. /* Exclusive lock for this structure. */
  497. struct mutex io_sem;
  498. /* Index returned by tomoyo_read_lock(). */
  499. int reader_idx;
  500. char __user *read_user_buf;
  501. int read_user_buf_avail;
  502. struct {
  503. struct list_head *domain;
  504. struct list_head *group;
  505. struct list_head *acl;
  506. int avail;
  507. int step;
  508. int query_index;
  509. u16 index;
  510. u8 bit;
  511. u8 w_pos;
  512. bool eof;
  513. bool print_this_domain_only;
  514. bool print_execute_only;
  515. const char *w[TOMOYO_MAX_IO_READ_QUEUE];
  516. } r;
  517. /* The position currently writing to. */
  518. struct tomoyo_domain_info *write_var1;
  519. /* Buffer for reading. */
  520. char *read_buf;
  521. /* Size of read buffer. */
  522. int readbuf_size;
  523. /* Buffer for writing. */
  524. char *write_buf;
  525. /* Bytes available for writing. */
  526. int write_avail;
  527. /* Size of write buffer. */
  528. int writebuf_size;
  529. /* Type of this interface. */
  530. u8 type;
  531. };
  532. /*
  533. * tomoyo_readable_file is a structure which is used for holding
  534. * "allow_read" entries.
  535. * It has following fields.
  536. *
  537. * (1) "head" is "struct tomoyo_acl_head".
  538. * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
  539. */
  540. struct tomoyo_readable_file {
  541. struct tomoyo_acl_head head;
  542. const struct tomoyo_path_info *filename;
  543. };
  544. /*
  545. * tomoyo_no_pattern is a structure which is used for holding
  546. * "file_pattern" entries.
  547. * It has following fields.
  548. *
  549. * (1) "head" is "struct tomoyo_acl_head".
  550. * (2) "pattern" is a pathname pattern which is used for converting pathnames
  551. * to pathname patterns during learning mode.
  552. */
  553. struct tomoyo_no_pattern {
  554. struct tomoyo_acl_head head;
  555. const struct tomoyo_path_info *pattern;
  556. };
  557. /*
  558. * tomoyo_no_rewrite is a structure which is used for holding
  559. * "deny_rewrite" entries.
  560. * It has following fields.
  561. *
  562. * (1) "head" is "struct tomoyo_acl_head".
  563. * (2) "pattern" is a pathname which is by default not permitted to modify
  564. * already existing content.
  565. */
  566. struct tomoyo_no_rewrite {
  567. struct tomoyo_acl_head head;
  568. const struct tomoyo_path_info *pattern;
  569. };
  570. /*
  571. * tomoyo_transition_control is a structure which is used for holding
  572. * "initialize_domain"/"no_initialize_domain"/"keep_domain"/"no_keep_domain"
  573. * entries.
  574. * It has following fields.
  575. *
  576. * (1) "head" is "struct tomoyo_acl_head".
  577. * (2) "type" is type of this entry.
  578. * (3) "is_last_name" is a bool which is true if "domainname" is "the last
  579. * component of a domainname", false otherwise.
  580. * (4) "domainname" which is "a domainname" or "the last component of a
  581. * domainname".
  582. * (5) "program" which is a program's pathname.
  583. */
  584. struct tomoyo_transition_control {
  585. struct tomoyo_acl_head head;
  586. u8 type; /* One of values in "enum tomoyo_transition_type". */
  587. /* True if the domainname is tomoyo_get_last_name(). */
  588. bool is_last_name;
  589. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  590. const struct tomoyo_path_info *program; /* Maybe NULL */
  591. };
  592. /*
  593. * tomoyo_aggregator is a structure which is used for holding
  594. * "aggregator" entries.
  595. * It has following fields.
  596. *
  597. * (1) "head" is "struct tomoyo_acl_head".
  598. * (2) "original_name" which is originally requested name.
  599. * (3) "aggregated_name" which is name to rewrite.
  600. */
  601. struct tomoyo_aggregator {
  602. struct tomoyo_acl_head head;
  603. const struct tomoyo_path_info *original_name;
  604. const struct tomoyo_path_info *aggregated_name;
  605. };
  606. /*
  607. * tomoyo_manager is a structure which is used for holding list of
  608. * domainnames or programs which are permitted to modify configuration via
  609. * /sys/kernel/security/tomoyo/ interface.
  610. * It has following fields.
  611. *
  612. * (1) "head" is "struct tomoyo_acl_head".
  613. * (2) "is_domain" is a bool which is true if "manager" is a domainname, false
  614. * otherwise.
  615. * (3) "manager" is a domainname or a program's pathname.
  616. */
  617. struct tomoyo_manager {
  618. struct tomoyo_acl_head head;
  619. bool is_domain; /* True if manager is a domainname. */
  620. /* A path to program or a domainname. */
  621. const struct tomoyo_path_info *manager;
  622. };
  623. struct tomoyo_preference {
  624. unsigned int learning_max_entry;
  625. bool enforcing_verbose;
  626. bool learning_verbose;
  627. bool permissive_verbose;
  628. };
  629. struct tomoyo_profile {
  630. const struct tomoyo_path_info *comment;
  631. struct tomoyo_preference *learning;
  632. struct tomoyo_preference *permissive;
  633. struct tomoyo_preference *enforcing;
  634. struct tomoyo_preference preference;
  635. u8 default_config;
  636. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  637. };
  638. /********** Function prototypes. **********/
  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. /* Poll operation for /sys/kernel/security/tomoyo/ interface. */
  655. int tomoyo_poll_control(struct file *file, poll_table *wait);
  656. /* Read operation for /sys/kernel/security/tomoyo/ interface. */
  657. int tomoyo_read_control(struct file *file, char __user *buffer,
  658. const int buffer_len);
  659. /* Write operation for /sys/kernel/security/tomoyo/ interface. */
  660. int tomoyo_write_control(struct file *file, const char __user *buffer,
  661. const int buffer_len);
  662. /* Check whether the domain has too many ACL entries to hold. */
  663. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  664. /* Print out of memory warning message. */
  665. void tomoyo_warn_oom(const char *function);
  666. /* Check whether the given name matches the given name_union. */
  667. const struct tomoyo_path_info *
  668. tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  669. const struct tomoyo_name_union *ptr);
  670. /* Check whether the given number matches the given number_union. */
  671. bool tomoyo_compare_number_union(const unsigned long value,
  672. const struct tomoyo_number_union *ptr);
  673. int tomoyo_get_mode(const u8 profile, const u8 index);
  674. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  675. __attribute__ ((format(printf, 2, 3)));
  676. /* Check whether the domainname is correct. */
  677. bool tomoyo_correct_domain(const unsigned char *domainname);
  678. /* Check whether the token is correct. */
  679. bool tomoyo_correct_path(const char *filename);
  680. bool tomoyo_correct_word(const char *string);
  681. /* Check whether the token can be a domainname. */
  682. bool tomoyo_domain_def(const unsigned char *buffer);
  683. bool tomoyo_parse_name_union(const char *filename,
  684. struct tomoyo_name_union *ptr);
  685. /* Check whether the given filename matches the given path_group. */
  686. const struct tomoyo_path_info *
  687. tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  688. const struct tomoyo_group *group);
  689. /* Check whether the given value matches the given number_group. */
  690. bool tomoyo_number_matches_group(const unsigned long min,
  691. const unsigned long max,
  692. const struct tomoyo_group *group);
  693. /* Check whether the given filename matches the given pattern. */
  694. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  695. const struct tomoyo_path_info *pattern);
  696. bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
  697. /* Tokenize a line. */
  698. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  699. /* Write domain policy violation warning message to console? */
  700. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  701. /* Fill "struct tomoyo_request_info". */
  702. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  703. struct tomoyo_domain_info *domain,
  704. const u8 index);
  705. /* Check permission for mount operation. */
  706. int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
  707. unsigned long flags, void *data_page);
  708. /* Create "aggregator" entry in exception policy. */
  709. int tomoyo_write_aggregator(char *data, const bool is_delete);
  710. int tomoyo_write_transition_control(char *data, const bool is_delete,
  711. const u8 type);
  712. /*
  713. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  714. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  715. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  716. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  717. * "allow_link" entry in domain policy.
  718. */
  719. int tomoyo_write_file(char *data, struct tomoyo_domain_info *domain,
  720. const bool is_delete);
  721. /* Create "allow_read" entry in exception policy. */
  722. int tomoyo_write_globally_readable(char *data, const bool is_delete);
  723. /* Create "allow_mount" entry in domain policy. */
  724. int tomoyo_write_mount(char *data, struct tomoyo_domain_info *domain,
  725. const bool is_delete);
  726. /* Create "deny_rewrite" entry in exception policy. */
  727. int tomoyo_write_no_rewrite(char *data, const bool is_delete);
  728. /* Create "file_pattern" entry in exception policy. */
  729. int tomoyo_write_pattern(char *data, const bool is_delete);
  730. /* Create "path_group"/"number_group" entry in exception policy. */
  731. int tomoyo_write_group(char *data, const bool is_delete, const u8 type);
  732. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  733. __attribute__ ((format(printf, 2, 3)));
  734. /* Find a domain by the given name. */
  735. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  736. /* Find or create a domain by the given name. */
  737. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  738. const u8 profile);
  739. struct tomoyo_profile *tomoyo_profile(const u8 profile);
  740. /*
  741. * Allocate memory for "struct tomoyo_path_group"/"struct tomoyo_number_group".
  742. */
  743. struct tomoyo_group *tomoyo_get_group(const char *group_name, const u8 type);
  744. /* Check mode for specified functionality. */
  745. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  746. const u8 index);
  747. /* Fill in "struct tomoyo_path_info" members. */
  748. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  749. /* Run policy loader when /sbin/init starts. */
  750. void tomoyo_load_policy(const char *filename);
  751. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  752. /* Convert binary string to ascii string. */
  753. char *tomoyo_encode(const char *str);
  754. /*
  755. * Returns realpath(3) of the given pathname except that
  756. * ignores chroot'ed root and does not follow the final symlink.
  757. */
  758. char *tomoyo_realpath_nofollow(const char *pathname);
  759. /*
  760. * Returns realpath(3) of the given pathname except that
  761. * ignores chroot'ed root and the pathname is already solved.
  762. */
  763. char *tomoyo_realpath_from_path(struct path *path);
  764. /* Get patterned pathname. */
  765. const char *tomoyo_pattern(const struct tomoyo_path_info *filename);
  766. /* Check memory quota. */
  767. bool tomoyo_memory_ok(void *ptr);
  768. void *tomoyo_commit_ok(void *data, const unsigned int size);
  769. /*
  770. * Keep the given name on the RAM.
  771. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  772. */
  773. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  774. /* Check for memory usage. */
  775. void tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  776. /* Set memory quota. */
  777. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  778. /* Initialize mm related code. */
  779. void __init tomoyo_mm_init(void);
  780. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  781. const struct tomoyo_path_info *filename);
  782. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  783. struct path *path, const int flag);
  784. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  785. unsigned long number);
  786. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  787. const unsigned int mode, unsigned int dev);
  788. int tomoyo_path_perm(const u8 operation, struct path *path);
  789. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  790. struct path *path2);
  791. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  792. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  793. const unsigned long value, const u8 type);
  794. /* Drop refcount on tomoyo_name_union. */
  795. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  796. /* Run garbage collector. */
  797. void tomoyo_run_gc(void);
  798. void tomoyo_memory_free(void *ptr);
  799. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  800. bool is_delete, struct tomoyo_domain_info *domain,
  801. bool (*check_duplicate) (const struct tomoyo_acl_info
  802. *,
  803. const struct tomoyo_acl_info
  804. *),
  805. bool (*merge_duplicate) (struct tomoyo_acl_info *,
  806. struct tomoyo_acl_info *,
  807. const bool));
  808. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  809. bool is_delete, struct list_head *list,
  810. bool (*check_duplicate) (const struct tomoyo_acl_head
  811. *,
  812. const struct tomoyo_acl_head
  813. *));
  814. void tomoyo_check_acl(struct tomoyo_request_info *r,
  815. bool (*check_entry) (struct tomoyo_request_info *,
  816. const struct tomoyo_acl_info *));
  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 *ptr =
  878. container_of(name, typeof(*ptr), 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) */