common.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * security/tomoyo/common.h
  4. *
  5. * Header file for TOMOYO.
  6. *
  7. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  8. */
  9. #ifndef _SECURITY_TOMOYO_COMMON_H
  10. #define _SECURITY_TOMOYO_COMMON_H
  11. #define pr_fmt(fmt) fmt
  12. #include <linux/ctype.h>
  13. #include <linux/string.h>
  14. #include <linux/mm.h>
  15. #include <linux/file.h>
  16. #include <linux/kmod.h>
  17. #include <linux/fs.h>
  18. #include <linux/sched.h>
  19. #include <linux/namei.h>
  20. #include <linux/mount.h>
  21. #include <linux/list.h>
  22. #include <linux/cred.h>
  23. #include <linux/poll.h>
  24. #include <linux/binfmts.h>
  25. #include <linux/highmem.h>
  26. #include <linux/net.h>
  27. #include <linux/inet.h>
  28. #include <linux/in.h>
  29. #include <linux/in6.h>
  30. #include <linux/un.h>
  31. #include <linux/lsm_hooks.h>
  32. #include <net/sock.h>
  33. #include <net/af_unix.h>
  34. #include <net/ip.h>
  35. #include <net/ipv6.h>
  36. #include <net/udp.h>
  37. /********** Constants definitions. **********/
  38. /*
  39. * TOMOYO uses this hash only when appending a string into the string
  40. * table. Frequency of appending strings is very low. So we don't need
  41. * large (e.g. 64k) hash size. 256 will be sufficient.
  42. */
  43. #define TOMOYO_HASH_BITS 8
  44. #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
  45. /*
  46. * TOMOYO checks only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET.
  47. * Therefore, we don't need SOCK_MAX.
  48. */
  49. #define TOMOYO_SOCK_MAX 6
  50. #define TOMOYO_EXEC_TMPSIZE 4096
  51. /* Garbage collector is trying to kfree() this element. */
  52. #define TOMOYO_GC_IN_PROGRESS -1
  53. /* Profile number is an integer between 0 and 255. */
  54. #define TOMOYO_MAX_PROFILES 256
  55. /* Group number is an integer between 0 and 255. */
  56. #define TOMOYO_MAX_ACL_GROUPS 256
  57. /* Index numbers for "struct tomoyo_condition". */
  58. enum tomoyo_conditions_index {
  59. TOMOYO_TASK_UID, /* current_uid() */
  60. TOMOYO_TASK_EUID, /* current_euid() */
  61. TOMOYO_TASK_SUID, /* current_suid() */
  62. TOMOYO_TASK_FSUID, /* current_fsuid() */
  63. TOMOYO_TASK_GID, /* current_gid() */
  64. TOMOYO_TASK_EGID, /* current_egid() */
  65. TOMOYO_TASK_SGID, /* current_sgid() */
  66. TOMOYO_TASK_FSGID, /* current_fsgid() */
  67. TOMOYO_TASK_PID, /* sys_getpid() */
  68. TOMOYO_TASK_PPID, /* sys_getppid() */
  69. TOMOYO_EXEC_ARGC, /* "struct linux_binprm *"->argc */
  70. TOMOYO_EXEC_ENVC, /* "struct linux_binprm *"->envc */
  71. TOMOYO_TYPE_IS_SOCKET, /* S_IFSOCK */
  72. TOMOYO_TYPE_IS_SYMLINK, /* S_IFLNK */
  73. TOMOYO_TYPE_IS_FILE, /* S_IFREG */
  74. TOMOYO_TYPE_IS_BLOCK_DEV, /* S_IFBLK */
  75. TOMOYO_TYPE_IS_DIRECTORY, /* S_IFDIR */
  76. TOMOYO_TYPE_IS_CHAR_DEV, /* S_IFCHR */
  77. TOMOYO_TYPE_IS_FIFO, /* S_IFIFO */
  78. TOMOYO_MODE_SETUID, /* S_ISUID */
  79. TOMOYO_MODE_SETGID, /* S_ISGID */
  80. TOMOYO_MODE_STICKY, /* S_ISVTX */
  81. TOMOYO_MODE_OWNER_READ, /* S_IRUSR */
  82. TOMOYO_MODE_OWNER_WRITE, /* S_IWUSR */
  83. TOMOYO_MODE_OWNER_EXECUTE, /* S_IXUSR */
  84. TOMOYO_MODE_GROUP_READ, /* S_IRGRP */
  85. TOMOYO_MODE_GROUP_WRITE, /* S_IWGRP */
  86. TOMOYO_MODE_GROUP_EXECUTE, /* S_IXGRP */
  87. TOMOYO_MODE_OTHERS_READ, /* S_IROTH */
  88. TOMOYO_MODE_OTHERS_WRITE, /* S_IWOTH */
  89. TOMOYO_MODE_OTHERS_EXECUTE, /* S_IXOTH */
  90. TOMOYO_EXEC_REALPATH,
  91. TOMOYO_SYMLINK_TARGET,
  92. TOMOYO_PATH1_UID,
  93. TOMOYO_PATH1_GID,
  94. TOMOYO_PATH1_INO,
  95. TOMOYO_PATH1_MAJOR,
  96. TOMOYO_PATH1_MINOR,
  97. TOMOYO_PATH1_PERM,
  98. TOMOYO_PATH1_TYPE,
  99. TOMOYO_PATH1_DEV_MAJOR,
  100. TOMOYO_PATH1_DEV_MINOR,
  101. TOMOYO_PATH2_UID,
  102. TOMOYO_PATH2_GID,
  103. TOMOYO_PATH2_INO,
  104. TOMOYO_PATH2_MAJOR,
  105. TOMOYO_PATH2_MINOR,
  106. TOMOYO_PATH2_PERM,
  107. TOMOYO_PATH2_TYPE,
  108. TOMOYO_PATH2_DEV_MAJOR,
  109. TOMOYO_PATH2_DEV_MINOR,
  110. TOMOYO_PATH1_PARENT_UID,
  111. TOMOYO_PATH1_PARENT_GID,
  112. TOMOYO_PATH1_PARENT_INO,
  113. TOMOYO_PATH1_PARENT_PERM,
  114. TOMOYO_PATH2_PARENT_UID,
  115. TOMOYO_PATH2_PARENT_GID,
  116. TOMOYO_PATH2_PARENT_INO,
  117. TOMOYO_PATH2_PARENT_PERM,
  118. TOMOYO_MAX_CONDITION_KEYWORD,
  119. TOMOYO_NUMBER_UNION,
  120. TOMOYO_NAME_UNION,
  121. TOMOYO_ARGV_ENTRY,
  122. TOMOYO_ENVP_ENTRY,
  123. };
  124. /* Index numbers for stat(). */
  125. enum tomoyo_path_stat_index {
  126. /* Do not change this order. */
  127. TOMOYO_PATH1,
  128. TOMOYO_PATH1_PARENT,
  129. TOMOYO_PATH2,
  130. TOMOYO_PATH2_PARENT,
  131. TOMOYO_MAX_PATH_STAT
  132. };
  133. /* Index numbers for operation mode. */
  134. enum tomoyo_mode_index {
  135. TOMOYO_CONFIG_DISABLED,
  136. TOMOYO_CONFIG_LEARNING,
  137. TOMOYO_CONFIG_PERMISSIVE,
  138. TOMOYO_CONFIG_ENFORCING,
  139. TOMOYO_CONFIG_MAX_MODE,
  140. TOMOYO_CONFIG_WANT_REJECT_LOG = 64,
  141. TOMOYO_CONFIG_WANT_GRANT_LOG = 128,
  142. TOMOYO_CONFIG_USE_DEFAULT = 255,
  143. };
  144. /* Index numbers for entry type. */
  145. enum tomoyo_policy_id {
  146. TOMOYO_ID_GROUP,
  147. TOMOYO_ID_ADDRESS_GROUP,
  148. TOMOYO_ID_PATH_GROUP,
  149. TOMOYO_ID_NUMBER_GROUP,
  150. TOMOYO_ID_TRANSITION_CONTROL,
  151. TOMOYO_ID_AGGREGATOR,
  152. TOMOYO_ID_MANAGER,
  153. TOMOYO_ID_CONDITION,
  154. TOMOYO_ID_NAME,
  155. TOMOYO_ID_ACL,
  156. TOMOYO_ID_DOMAIN,
  157. TOMOYO_MAX_POLICY
  158. };
  159. /* Index numbers for domain's attributes. */
  160. enum tomoyo_domain_info_flags_index {
  161. /* Quota warnning flag. */
  162. TOMOYO_DIF_QUOTA_WARNED,
  163. /*
  164. * This domain was unable to create a new domain at
  165. * tomoyo_find_next_domain() because the name of the domain to be
  166. * created was too long or it could not allocate memory.
  167. * More than one process continued execve() without domain transition.
  168. */
  169. TOMOYO_DIF_TRANSITION_FAILED,
  170. TOMOYO_MAX_DOMAIN_INFO_FLAGS
  171. };
  172. /* Index numbers for audit type. */
  173. enum tomoyo_grant_log {
  174. /* Follow profile's configuration. */
  175. TOMOYO_GRANTLOG_AUTO,
  176. /* Do not generate grant log. */
  177. TOMOYO_GRANTLOG_NO,
  178. /* Generate grant_log. */
  179. TOMOYO_GRANTLOG_YES,
  180. };
  181. /* Index numbers for group entries. */
  182. enum tomoyo_group_id {
  183. TOMOYO_PATH_GROUP,
  184. TOMOYO_NUMBER_GROUP,
  185. TOMOYO_ADDRESS_GROUP,
  186. TOMOYO_MAX_GROUP
  187. };
  188. /* Index numbers for type of numeric values. */
  189. enum tomoyo_value_type {
  190. TOMOYO_VALUE_TYPE_INVALID,
  191. TOMOYO_VALUE_TYPE_DECIMAL,
  192. TOMOYO_VALUE_TYPE_OCTAL,
  193. TOMOYO_VALUE_TYPE_HEXADECIMAL,
  194. };
  195. /* Index numbers for domain transition control keywords. */
  196. enum tomoyo_transition_type {
  197. /* Do not change this order, */
  198. TOMOYO_TRANSITION_CONTROL_NO_RESET,
  199. TOMOYO_TRANSITION_CONTROL_RESET,
  200. TOMOYO_TRANSITION_CONTROL_NO_INITIALIZE,
  201. TOMOYO_TRANSITION_CONTROL_INITIALIZE,
  202. TOMOYO_TRANSITION_CONTROL_NO_KEEP,
  203. TOMOYO_TRANSITION_CONTROL_KEEP,
  204. TOMOYO_MAX_TRANSITION_TYPE
  205. };
  206. /* Index numbers for Access Controls. */
  207. enum tomoyo_acl_entry_type_index {
  208. TOMOYO_TYPE_PATH_ACL,
  209. TOMOYO_TYPE_PATH2_ACL,
  210. TOMOYO_TYPE_PATH_NUMBER_ACL,
  211. TOMOYO_TYPE_MKDEV_ACL,
  212. TOMOYO_TYPE_MOUNT_ACL,
  213. TOMOYO_TYPE_INET_ACL,
  214. TOMOYO_TYPE_UNIX_ACL,
  215. TOMOYO_TYPE_ENV_ACL,
  216. TOMOYO_TYPE_MANUAL_TASK_ACL,
  217. };
  218. /* Index numbers for access controls with one pathname. */
  219. enum tomoyo_path_acl_index {
  220. TOMOYO_TYPE_EXECUTE,
  221. TOMOYO_TYPE_READ,
  222. TOMOYO_TYPE_WRITE,
  223. TOMOYO_TYPE_APPEND,
  224. TOMOYO_TYPE_UNLINK,
  225. TOMOYO_TYPE_GETATTR,
  226. TOMOYO_TYPE_RMDIR,
  227. TOMOYO_TYPE_TRUNCATE,
  228. TOMOYO_TYPE_SYMLINK,
  229. TOMOYO_TYPE_CHROOT,
  230. TOMOYO_TYPE_UMOUNT,
  231. TOMOYO_MAX_PATH_OPERATION
  232. };
  233. /* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
  234. enum tomoyo_memory_stat_type {
  235. TOMOYO_MEMORY_POLICY,
  236. TOMOYO_MEMORY_AUDIT,
  237. TOMOYO_MEMORY_QUERY,
  238. TOMOYO_MAX_MEMORY_STAT
  239. };
  240. enum tomoyo_mkdev_acl_index {
  241. TOMOYO_TYPE_MKBLOCK,
  242. TOMOYO_TYPE_MKCHAR,
  243. TOMOYO_MAX_MKDEV_OPERATION
  244. };
  245. /* Index numbers for socket operations. */
  246. enum tomoyo_network_acl_index {
  247. TOMOYO_NETWORK_BIND, /* bind() operation. */
  248. TOMOYO_NETWORK_LISTEN, /* listen() operation. */
  249. TOMOYO_NETWORK_CONNECT, /* connect() operation. */
  250. TOMOYO_NETWORK_SEND, /* send() operation. */
  251. TOMOYO_MAX_NETWORK_OPERATION
  252. };
  253. /* Index numbers for access controls with two pathnames. */
  254. enum tomoyo_path2_acl_index {
  255. TOMOYO_TYPE_LINK,
  256. TOMOYO_TYPE_RENAME,
  257. TOMOYO_TYPE_PIVOT_ROOT,
  258. TOMOYO_MAX_PATH2_OPERATION
  259. };
  260. /* Index numbers for access controls with one pathname and one number. */
  261. enum tomoyo_path_number_acl_index {
  262. TOMOYO_TYPE_CREATE,
  263. TOMOYO_TYPE_MKDIR,
  264. TOMOYO_TYPE_MKFIFO,
  265. TOMOYO_TYPE_MKSOCK,
  266. TOMOYO_TYPE_IOCTL,
  267. TOMOYO_TYPE_CHMOD,
  268. TOMOYO_TYPE_CHOWN,
  269. TOMOYO_TYPE_CHGRP,
  270. TOMOYO_MAX_PATH_NUMBER_OPERATION
  271. };
  272. /* Index numbers for /sys/kernel/security/tomoyo/ interfaces. */
  273. enum tomoyo_securityfs_interface_index {
  274. TOMOYO_DOMAINPOLICY,
  275. TOMOYO_EXCEPTIONPOLICY,
  276. TOMOYO_PROCESS_STATUS,
  277. TOMOYO_STAT,
  278. TOMOYO_AUDIT,
  279. TOMOYO_VERSION,
  280. TOMOYO_PROFILE,
  281. TOMOYO_QUERY,
  282. TOMOYO_MANAGER
  283. };
  284. /* Index numbers for special mount operations. */
  285. enum tomoyo_special_mount {
  286. TOMOYO_MOUNT_BIND, /* mount --bind /source /dest */
  287. TOMOYO_MOUNT_MOVE, /* mount --move /old /new */
  288. TOMOYO_MOUNT_REMOUNT, /* mount -o remount /dir */
  289. TOMOYO_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
  290. TOMOYO_MOUNT_MAKE_PRIVATE, /* mount --make-private /dir */
  291. TOMOYO_MOUNT_MAKE_SLAVE, /* mount --make-slave /dir */
  292. TOMOYO_MOUNT_MAKE_SHARED, /* mount --make-shared /dir */
  293. TOMOYO_MAX_SPECIAL_MOUNT
  294. };
  295. /* Index numbers for functionality. */
  296. enum tomoyo_mac_index {
  297. TOMOYO_MAC_FILE_EXECUTE,
  298. TOMOYO_MAC_FILE_OPEN,
  299. TOMOYO_MAC_FILE_CREATE,
  300. TOMOYO_MAC_FILE_UNLINK,
  301. TOMOYO_MAC_FILE_GETATTR,
  302. TOMOYO_MAC_FILE_MKDIR,
  303. TOMOYO_MAC_FILE_RMDIR,
  304. TOMOYO_MAC_FILE_MKFIFO,
  305. TOMOYO_MAC_FILE_MKSOCK,
  306. TOMOYO_MAC_FILE_TRUNCATE,
  307. TOMOYO_MAC_FILE_SYMLINK,
  308. TOMOYO_MAC_FILE_MKBLOCK,
  309. TOMOYO_MAC_FILE_MKCHAR,
  310. TOMOYO_MAC_FILE_LINK,
  311. TOMOYO_MAC_FILE_RENAME,
  312. TOMOYO_MAC_FILE_CHMOD,
  313. TOMOYO_MAC_FILE_CHOWN,
  314. TOMOYO_MAC_FILE_CHGRP,
  315. TOMOYO_MAC_FILE_IOCTL,
  316. TOMOYO_MAC_FILE_CHROOT,
  317. TOMOYO_MAC_FILE_MOUNT,
  318. TOMOYO_MAC_FILE_UMOUNT,
  319. TOMOYO_MAC_FILE_PIVOT_ROOT,
  320. TOMOYO_MAC_NETWORK_INET_STREAM_BIND,
  321. TOMOYO_MAC_NETWORK_INET_STREAM_LISTEN,
  322. TOMOYO_MAC_NETWORK_INET_STREAM_CONNECT,
  323. TOMOYO_MAC_NETWORK_INET_DGRAM_BIND,
  324. TOMOYO_MAC_NETWORK_INET_DGRAM_SEND,
  325. TOMOYO_MAC_NETWORK_INET_RAW_BIND,
  326. TOMOYO_MAC_NETWORK_INET_RAW_SEND,
  327. TOMOYO_MAC_NETWORK_UNIX_STREAM_BIND,
  328. TOMOYO_MAC_NETWORK_UNIX_STREAM_LISTEN,
  329. TOMOYO_MAC_NETWORK_UNIX_STREAM_CONNECT,
  330. TOMOYO_MAC_NETWORK_UNIX_DGRAM_BIND,
  331. TOMOYO_MAC_NETWORK_UNIX_DGRAM_SEND,
  332. TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_BIND,
  333. TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
  334. TOMOYO_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
  335. TOMOYO_MAC_ENVIRON,
  336. TOMOYO_MAX_MAC_INDEX
  337. };
  338. /* Index numbers for category of functionality. */
  339. enum tomoyo_mac_category_index {
  340. TOMOYO_MAC_CATEGORY_FILE,
  341. TOMOYO_MAC_CATEGORY_NETWORK,
  342. TOMOYO_MAC_CATEGORY_MISC,
  343. TOMOYO_MAX_MAC_CATEGORY_INDEX
  344. };
  345. /*
  346. * Retry this request. Returned by tomoyo_supervisor() if policy violation has
  347. * occurred in enforcing mode and the userspace daemon decided to retry.
  348. *
  349. * We must choose a positive value in order to distinguish "granted" (which is
  350. * 0) and "rejected" (which is a negative value) and "retry".
  351. */
  352. #define TOMOYO_RETRY_REQUEST 1
  353. /* Index numbers for /sys/kernel/security/tomoyo/stat interface. */
  354. enum tomoyo_policy_stat_type {
  355. /* Do not change this order. */
  356. TOMOYO_STAT_POLICY_UPDATES,
  357. TOMOYO_STAT_POLICY_LEARNING, /* == TOMOYO_CONFIG_LEARNING */
  358. TOMOYO_STAT_POLICY_PERMISSIVE, /* == TOMOYO_CONFIG_PERMISSIVE */
  359. TOMOYO_STAT_POLICY_ENFORCING, /* == TOMOYO_CONFIG_ENFORCING */
  360. TOMOYO_MAX_POLICY_STAT
  361. };
  362. /* Index numbers for profile's PREFERENCE values. */
  363. enum tomoyo_pref_index {
  364. TOMOYO_PREF_MAX_AUDIT_LOG,
  365. TOMOYO_PREF_MAX_LEARNING_ENTRY,
  366. TOMOYO_MAX_PREF
  367. };
  368. /********** Structure definitions. **********/
  369. /* Common header for holding ACL entries. */
  370. struct tomoyo_acl_head {
  371. struct list_head list;
  372. s8 is_deleted; /* true or false or TOMOYO_GC_IN_PROGRESS */
  373. } __packed;
  374. /* Common header for shared entries. */
  375. struct tomoyo_shared_acl_head {
  376. struct list_head list;
  377. atomic_t users;
  378. } __packed;
  379. struct tomoyo_policy_namespace;
  380. /* Structure for request info. */
  381. struct tomoyo_request_info {
  382. /*
  383. * For holding parameters specific to operations which deal files.
  384. * NULL if not dealing files.
  385. */
  386. struct tomoyo_obj_info *obj;
  387. /*
  388. * For holding parameters specific to execve() request.
  389. * NULL if not dealing execve().
  390. */
  391. struct tomoyo_execve *ee;
  392. struct tomoyo_domain_info *domain;
  393. /* For holding parameters. */
  394. union {
  395. struct {
  396. const struct tomoyo_path_info *filename;
  397. /* For using wildcards at tomoyo_find_next_domain(). */
  398. const struct tomoyo_path_info *matched_path;
  399. /* One of values in "enum tomoyo_path_acl_index". */
  400. u8 operation;
  401. } path;
  402. struct {
  403. const struct tomoyo_path_info *filename1;
  404. const struct tomoyo_path_info *filename2;
  405. /* One of values in "enum tomoyo_path2_acl_index". */
  406. u8 operation;
  407. } path2;
  408. struct {
  409. const struct tomoyo_path_info *filename;
  410. unsigned int mode;
  411. unsigned int major;
  412. unsigned int minor;
  413. /* One of values in "enum tomoyo_mkdev_acl_index". */
  414. u8 operation;
  415. } mkdev;
  416. struct {
  417. const struct tomoyo_path_info *filename;
  418. unsigned long number;
  419. /*
  420. * One of values in
  421. * "enum tomoyo_path_number_acl_index".
  422. */
  423. u8 operation;
  424. } path_number;
  425. struct {
  426. const struct tomoyo_path_info *name;
  427. } environ;
  428. struct {
  429. const __be32 *address;
  430. u16 port;
  431. /* One of values smaller than TOMOYO_SOCK_MAX. */
  432. u8 protocol;
  433. /* One of values in "enum tomoyo_network_acl_index". */
  434. u8 operation;
  435. bool is_ipv6;
  436. } inet_network;
  437. struct {
  438. const struct tomoyo_path_info *address;
  439. /* One of values smaller than TOMOYO_SOCK_MAX. */
  440. u8 protocol;
  441. /* One of values in "enum tomoyo_network_acl_index". */
  442. u8 operation;
  443. } unix_network;
  444. struct {
  445. const struct tomoyo_path_info *type;
  446. const struct tomoyo_path_info *dir;
  447. const struct tomoyo_path_info *dev;
  448. unsigned long flags;
  449. int need_dev;
  450. } mount;
  451. struct {
  452. const struct tomoyo_path_info *domainname;
  453. } task;
  454. } param;
  455. struct tomoyo_acl_info *matched_acl;
  456. u8 param_type;
  457. bool granted;
  458. u8 retry;
  459. u8 profile;
  460. u8 mode; /* One of tomoyo_mode_index . */
  461. u8 type;
  462. };
  463. /* Structure for holding a token. */
  464. struct tomoyo_path_info {
  465. const char *name;
  466. u32 hash; /* = full_name_hash(name, strlen(name)) */
  467. u16 const_len; /* = tomoyo_const_part_length(name) */
  468. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  469. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  470. };
  471. /* Structure for holding string data. */
  472. struct tomoyo_name {
  473. struct tomoyo_shared_acl_head head;
  474. struct tomoyo_path_info entry;
  475. };
  476. /* Structure for holding a word. */
  477. struct tomoyo_name_union {
  478. /* Either @filename or @group is NULL. */
  479. const struct tomoyo_path_info *filename;
  480. struct tomoyo_group *group;
  481. };
  482. /* Structure for holding a number. */
  483. struct tomoyo_number_union {
  484. unsigned long values[2];
  485. struct tomoyo_group *group; /* Maybe NULL. */
  486. /* One of values in "enum tomoyo_value_type". */
  487. u8 value_type[2];
  488. };
  489. /* Structure for holding an IP address. */
  490. struct tomoyo_ipaddr_union {
  491. struct in6_addr ip[2]; /* Big endian. */
  492. struct tomoyo_group *group; /* Pointer to address group. */
  493. bool is_ipv6; /* Valid only if @group == NULL. */
  494. };
  495. /* Structure for "path_group"/"number_group"/"address_group" directive. */
  496. struct tomoyo_group {
  497. struct tomoyo_shared_acl_head head;
  498. const struct tomoyo_path_info *group_name;
  499. struct list_head member_list;
  500. };
  501. /* Structure for "path_group" directive. */
  502. struct tomoyo_path_group {
  503. struct tomoyo_acl_head head;
  504. const struct tomoyo_path_info *member_name;
  505. };
  506. /* Structure for "number_group" directive. */
  507. struct tomoyo_number_group {
  508. struct tomoyo_acl_head head;
  509. struct tomoyo_number_union number;
  510. };
  511. /* Structure for "address_group" directive. */
  512. struct tomoyo_address_group {
  513. struct tomoyo_acl_head head;
  514. /* Structure for holding an IP address. */
  515. struct tomoyo_ipaddr_union address;
  516. };
  517. /* Subset of "struct stat". Used by conditional ACL and audit logs. */
  518. struct tomoyo_mini_stat {
  519. kuid_t uid;
  520. kgid_t gid;
  521. ino_t ino;
  522. umode_t mode;
  523. dev_t dev;
  524. dev_t rdev;
  525. };
  526. /* Structure for dumping argv[] and envp[] of "struct linux_binprm". */
  527. struct tomoyo_page_dump {
  528. struct page *page; /* Previously dumped page. */
  529. char *data; /* Contents of "page". Size is PAGE_SIZE. */
  530. };
  531. /* Structure for attribute checks in addition to pathname checks. */
  532. struct tomoyo_obj_info {
  533. /*
  534. * True if tomoyo_get_attributes() was already called, false otherwise.
  535. */
  536. bool validate_done;
  537. /* True if @stat[] is valid. */
  538. bool stat_valid[TOMOYO_MAX_PATH_STAT];
  539. /* First pathname. Initialized with { NULL, NULL } if no path. */
  540. struct path path1;
  541. /* Second pathname. Initialized with { NULL, NULL } if no path. */
  542. struct path path2;
  543. /*
  544. * Information on @path1, @path1's parent directory, @path2, @path2's
  545. * parent directory.
  546. */
  547. struct tomoyo_mini_stat stat[TOMOYO_MAX_PATH_STAT];
  548. /*
  549. * Content of symbolic link to be created. NULL for operations other
  550. * than symlink().
  551. */
  552. struct tomoyo_path_info *symlink_target;
  553. };
  554. /* Structure for argv[]. */
  555. struct tomoyo_argv {
  556. unsigned long index;
  557. const struct tomoyo_path_info *value;
  558. bool is_not;
  559. };
  560. /* Structure for envp[]. */
  561. struct tomoyo_envp {
  562. const struct tomoyo_path_info *name;
  563. const struct tomoyo_path_info *value;
  564. bool is_not;
  565. };
  566. /* Structure for execve() operation. */
  567. struct tomoyo_execve {
  568. struct tomoyo_request_info r;
  569. struct tomoyo_obj_info obj;
  570. struct linux_binprm *bprm;
  571. const struct tomoyo_path_info *transition;
  572. /* For dumping argv[] and envp[]. */
  573. struct tomoyo_page_dump dump;
  574. /* For temporary use. */
  575. char *tmp; /* Size is TOMOYO_EXEC_TMPSIZE bytes */
  576. };
  577. /* Structure for entries which follows "struct tomoyo_condition". */
  578. struct tomoyo_condition_element {
  579. /*
  580. * Left hand operand. A "struct tomoyo_argv" for TOMOYO_ARGV_ENTRY, a
  581. * "struct tomoyo_envp" for TOMOYO_ENVP_ENTRY is attached to the tail
  582. * of the array of this struct.
  583. */
  584. u8 left;
  585. /*
  586. * Right hand operand. A "struct tomoyo_number_union" for
  587. * TOMOYO_NUMBER_UNION, a "struct tomoyo_name_union" for
  588. * TOMOYO_NAME_UNION is attached to the tail of the array of this
  589. * struct.
  590. */
  591. u8 right;
  592. /* Equation operator. True if equals or overlaps, false otherwise. */
  593. bool equals;
  594. };
  595. /* Structure for optional arguments. */
  596. struct tomoyo_condition {
  597. struct tomoyo_shared_acl_head head;
  598. u32 size; /* Memory size allocated for this entry. */
  599. u16 condc; /* Number of conditions in this struct. */
  600. u16 numbers_count; /* Number of "struct tomoyo_number_union values". */
  601. u16 names_count; /* Number of "struct tomoyo_name_union names". */
  602. u16 argc; /* Number of "struct tomoyo_argv". */
  603. u16 envc; /* Number of "struct tomoyo_envp". */
  604. u8 grant_log; /* One of values in "enum tomoyo_grant_log". */
  605. const struct tomoyo_path_info *transit; /* Maybe NULL. */
  606. /*
  607. * struct tomoyo_condition_element condition[condc];
  608. * struct tomoyo_number_union values[numbers_count];
  609. * struct tomoyo_name_union names[names_count];
  610. * struct tomoyo_argv argv[argc];
  611. * struct tomoyo_envp envp[envc];
  612. */
  613. };
  614. /* Common header for individual entries. */
  615. struct tomoyo_acl_info {
  616. struct list_head list;
  617. struct tomoyo_condition *cond; /* Maybe NULL. */
  618. s8 is_deleted; /* true or false or TOMOYO_GC_IN_PROGRESS */
  619. u8 type; /* One of values in "enum tomoyo_acl_entry_type_index". */
  620. } __packed;
  621. /* Structure for domain information. */
  622. struct tomoyo_domain_info {
  623. struct list_head list;
  624. struct list_head acl_info_list;
  625. /* Name of this domain. Never NULL. */
  626. const struct tomoyo_path_info *domainname;
  627. /* Namespace for this domain. Never NULL. */
  628. struct tomoyo_policy_namespace *ns;
  629. /* Group numbers to use. */
  630. unsigned long group[TOMOYO_MAX_ACL_GROUPS / BITS_PER_LONG];
  631. u8 profile; /* Profile number to use. */
  632. bool is_deleted; /* Delete flag. */
  633. bool flags[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
  634. atomic_t users; /* Number of referring tasks. */
  635. };
  636. /*
  637. * Structure for "task manual_domain_transition" directive.
  638. */
  639. struct tomoyo_task_acl {
  640. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MANUAL_TASK_ACL */
  641. /* Pointer to domainname. */
  642. const struct tomoyo_path_info *domainname;
  643. };
  644. /*
  645. * Structure for "file execute", "file read", "file write", "file append",
  646. * "file unlink", "file getattr", "file rmdir", "file truncate",
  647. * "file symlink", "file chroot" and "file unmount" directive.
  648. */
  649. struct tomoyo_path_acl {
  650. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  651. u16 perm; /* Bitmask of values in "enum tomoyo_path_acl_index". */
  652. struct tomoyo_name_union name;
  653. };
  654. /*
  655. * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
  656. * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
  657. */
  658. struct tomoyo_path_number_acl {
  659. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
  660. /* Bitmask of values in "enum tomoyo_path_number_acl_index". */
  661. u8 perm;
  662. struct tomoyo_name_union name;
  663. struct tomoyo_number_union number;
  664. };
  665. /* Structure for "file mkblock" and "file mkchar" directive. */
  666. struct tomoyo_mkdev_acl {
  667. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MKDEV_ACL */
  668. u8 perm; /* Bitmask of values in "enum tomoyo_mkdev_acl_index". */
  669. struct tomoyo_name_union name;
  670. struct tomoyo_number_union mode;
  671. struct tomoyo_number_union major;
  672. struct tomoyo_number_union minor;
  673. };
  674. /*
  675. * Structure for "file rename", "file link" and "file pivot_root" directive.
  676. */
  677. struct tomoyo_path2_acl {
  678. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  679. u8 perm; /* Bitmask of values in "enum tomoyo_path2_acl_index". */
  680. struct tomoyo_name_union name1;
  681. struct tomoyo_name_union name2;
  682. };
  683. /* Structure for "file mount" directive. */
  684. struct tomoyo_mount_acl {
  685. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
  686. struct tomoyo_name_union dev_name;
  687. struct tomoyo_name_union dir_name;
  688. struct tomoyo_name_union fs_type;
  689. struct tomoyo_number_union flags;
  690. };
  691. /* Structure for "misc env" directive in domain policy. */
  692. struct tomoyo_env_acl {
  693. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_ENV_ACL */
  694. const struct tomoyo_path_info *env; /* environment variable */
  695. };
  696. /* Structure for "network inet" directive. */
  697. struct tomoyo_inet_acl {
  698. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_INET_ACL */
  699. u8 protocol;
  700. u8 perm; /* Bitmask of values in "enum tomoyo_network_acl_index" */
  701. struct tomoyo_ipaddr_union address;
  702. struct tomoyo_number_union port;
  703. };
  704. /* Structure for "network unix" directive. */
  705. struct tomoyo_unix_acl {
  706. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_UNIX_ACL */
  707. u8 protocol;
  708. u8 perm; /* Bitmask of values in "enum tomoyo_network_acl_index" */
  709. struct tomoyo_name_union name;
  710. };
  711. /* Structure for holding a line from /sys/kernel/security/tomoyo/ interface. */
  712. struct tomoyo_acl_param {
  713. char *data;
  714. struct list_head *list;
  715. struct tomoyo_policy_namespace *ns;
  716. bool is_delete;
  717. };
  718. #define TOMOYO_MAX_IO_READ_QUEUE 64
  719. /*
  720. * Structure for reading/writing policy via /sys/kernel/security/tomoyo
  721. * interfaces.
  722. */
  723. struct tomoyo_io_buffer {
  724. void (*read)(struct tomoyo_io_buffer *head);
  725. int (*write)(struct tomoyo_io_buffer *head);
  726. __poll_t (*poll)(struct file *file, poll_table *wait);
  727. /* Exclusive lock for this structure. */
  728. struct mutex io_sem;
  729. char __user *read_user_buf;
  730. size_t read_user_buf_avail;
  731. struct {
  732. struct list_head *ns;
  733. struct list_head *domain;
  734. struct list_head *group;
  735. struct list_head *acl;
  736. size_t avail;
  737. unsigned int step;
  738. unsigned int query_index;
  739. u16 index;
  740. u16 cond_index;
  741. u8 acl_group_index;
  742. u8 cond_step;
  743. u8 bit;
  744. u8 w_pos;
  745. bool eof;
  746. bool print_this_domain_only;
  747. bool print_transition_related_only;
  748. bool print_cond_part;
  749. const char *w[TOMOYO_MAX_IO_READ_QUEUE];
  750. } r;
  751. struct {
  752. struct tomoyo_policy_namespace *ns;
  753. /* The position currently writing to. */
  754. struct tomoyo_domain_info *domain;
  755. /* Bytes available for writing. */
  756. size_t avail;
  757. bool is_delete;
  758. } w;
  759. /* Buffer for reading. */
  760. char *read_buf;
  761. /* Size of read buffer. */
  762. size_t readbuf_size;
  763. /* Buffer for writing. */
  764. char *write_buf;
  765. /* Size of write buffer. */
  766. size_t writebuf_size;
  767. /* Type of this interface. */
  768. enum tomoyo_securityfs_interface_index type;
  769. /* Users counter protected by tomoyo_io_buffer_list_lock. */
  770. u8 users;
  771. /* List for telling GC not to kfree() elements. */
  772. struct list_head list;
  773. };
  774. /*
  775. * Structure for "initialize_domain"/"no_initialize_domain"/"keep_domain"/
  776. * "no_keep_domain" keyword.
  777. */
  778. struct tomoyo_transition_control {
  779. struct tomoyo_acl_head head;
  780. u8 type; /* One of values in "enum tomoyo_transition_type". */
  781. /* True if the domainname is tomoyo_get_last_name(). */
  782. bool is_last_name;
  783. const struct tomoyo_path_info *domainname; /* Maybe NULL */
  784. const struct tomoyo_path_info *program; /* Maybe NULL */
  785. };
  786. /* Structure for "aggregator" keyword. */
  787. struct tomoyo_aggregator {
  788. struct tomoyo_acl_head head;
  789. const struct tomoyo_path_info *original_name;
  790. const struct tomoyo_path_info *aggregated_name;
  791. };
  792. /* Structure for policy manager. */
  793. struct tomoyo_manager {
  794. struct tomoyo_acl_head head;
  795. /* A path to program or a domainname. */
  796. const struct tomoyo_path_info *manager;
  797. };
  798. struct tomoyo_preference {
  799. unsigned int learning_max_entry;
  800. bool enforcing_verbose;
  801. bool learning_verbose;
  802. bool permissive_verbose;
  803. };
  804. /* Structure for /sys/kernel/security/tomnoyo/profile interface. */
  805. struct tomoyo_profile {
  806. const struct tomoyo_path_info *comment;
  807. struct tomoyo_preference *learning;
  808. struct tomoyo_preference *permissive;
  809. struct tomoyo_preference *enforcing;
  810. struct tomoyo_preference preference;
  811. u8 default_config;
  812. u8 config[TOMOYO_MAX_MAC_INDEX + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  813. unsigned int pref[TOMOYO_MAX_PREF];
  814. };
  815. /* Structure for representing YYYY/MM/DD hh/mm/ss. */
  816. struct tomoyo_time {
  817. u16 year;
  818. u8 month;
  819. u8 day;
  820. u8 hour;
  821. u8 min;
  822. u8 sec;
  823. };
  824. /* Structure for policy namespace. */
  825. struct tomoyo_policy_namespace {
  826. /* Profile table. Memory is allocated as needed. */
  827. struct tomoyo_profile *profile_ptr[TOMOYO_MAX_PROFILES];
  828. /* List of "struct tomoyo_group". */
  829. struct list_head group_list[TOMOYO_MAX_GROUP];
  830. /* List of policy. */
  831. struct list_head policy_list[TOMOYO_MAX_POLICY];
  832. /* The global ACL referred by "use_group" keyword. */
  833. struct list_head acl_group[TOMOYO_MAX_ACL_GROUPS];
  834. /* List for connecting to tomoyo_namespace_list list. */
  835. struct list_head namespace_list;
  836. /* Profile version. Currently only 20150505 is defined. */
  837. unsigned int profile_version;
  838. /* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
  839. const char *name;
  840. };
  841. /* Structure for "struct task_struct"->security. */
  842. struct tomoyo_task {
  843. struct tomoyo_domain_info *domain_info;
  844. struct tomoyo_domain_info *old_domain_info;
  845. };
  846. /********** Function prototypes. **********/
  847. bool tomoyo_address_matches_group(const bool is_ipv6, const __be32 *address,
  848. const struct tomoyo_group *group);
  849. bool tomoyo_compare_number_union(const unsigned long value,
  850. const struct tomoyo_number_union *ptr);
  851. bool tomoyo_condition(struct tomoyo_request_info *r,
  852. const struct tomoyo_condition *cond);
  853. bool tomoyo_correct_domain(const unsigned char *domainname);
  854. bool tomoyo_correct_path(const char *filename);
  855. bool tomoyo_correct_word(const char *string);
  856. bool tomoyo_domain_def(const unsigned char *buffer);
  857. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  858. bool tomoyo_dump_page(struct linux_binprm *bprm, unsigned long pos,
  859. struct tomoyo_page_dump *dump);
  860. bool tomoyo_memory_ok(void *ptr);
  861. bool tomoyo_number_matches_group(const unsigned long min,
  862. const unsigned long max,
  863. const struct tomoyo_group *group);
  864. bool tomoyo_parse_ipaddr_union(struct tomoyo_acl_param *param,
  865. struct tomoyo_ipaddr_union *ptr);
  866. bool tomoyo_parse_name_union(struct tomoyo_acl_param *param,
  867. struct tomoyo_name_union *ptr);
  868. bool tomoyo_parse_number_union(struct tomoyo_acl_param *param,
  869. struct tomoyo_number_union *ptr);
  870. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  871. const struct tomoyo_path_info *pattern);
  872. bool tomoyo_permstr(const char *string, const char *keyword);
  873. bool tomoyo_str_starts(char **src, const char *find);
  874. char *tomoyo_encode(const char *str);
  875. char *tomoyo_encode2(const char *str, int str_len);
  876. char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
  877. va_list args);
  878. char *tomoyo_read_token(struct tomoyo_acl_param *param);
  879. char *tomoyo_realpath_from_path(const struct path *path);
  880. char *tomoyo_realpath_nofollow(const char *pathname);
  881. const char *tomoyo_get_exe(void);
  882. const char *tomoyo_yesno(const unsigned int value);
  883. const struct tomoyo_path_info *tomoyo_compare_name_union
  884. (const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
  885. const struct tomoyo_path_info *tomoyo_get_domainname
  886. (struct tomoyo_acl_param *param);
  887. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  888. const struct tomoyo_path_info *tomoyo_path_matches_group
  889. (const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);
  890. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  891. const struct path *path, const int flag);
  892. void tomoyo_close_control(struct tomoyo_io_buffer *head);
  893. int tomoyo_env_perm(struct tomoyo_request_info *r, const char *env);
  894. int tomoyo_execute_permission(struct tomoyo_request_info *r,
  895. const struct tomoyo_path_info *filename);
  896. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  897. int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
  898. const u8 index);
  899. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  900. struct tomoyo_domain_info *domain,
  901. const u8 index);
  902. int tomoyo_mkdev_perm(const u8 operation, const struct path *path,
  903. const unsigned int mode, unsigned int dev);
  904. int tomoyo_mount_permission(const char *dev_name, const struct path *path,
  905. const char *type, unsigned long flags,
  906. void *data_page);
  907. int tomoyo_open_control(const u8 type, struct file *file);
  908. int tomoyo_path2_perm(const u8 operation, const struct path *path1,
  909. const struct path *path2);
  910. int tomoyo_path_number_perm(const u8 operation, const struct path *path,
  911. unsigned long number);
  912. int tomoyo_path_perm(const u8 operation, const struct path *path,
  913. const char *target);
  914. __poll_t tomoyo_poll_control(struct file *file, poll_table *wait);
  915. __poll_t tomoyo_poll_log(struct file *file, poll_table *wait);
  916. int tomoyo_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
  917. int addr_len);
  918. int tomoyo_socket_connect_permission(struct socket *sock,
  919. struct sockaddr *addr, int addr_len);
  920. int tomoyo_socket_listen_permission(struct socket *sock);
  921. int tomoyo_socket_sendmsg_permission(struct socket *sock, struct msghdr *msg,
  922. int size);
  923. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  924. __printf(2, 3);
  925. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  926. struct tomoyo_acl_param *param,
  927. bool (*check_duplicate)
  928. (const struct tomoyo_acl_info *,
  929. const struct tomoyo_acl_info *),
  930. bool (*merge_duplicate)
  931. (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
  932. const bool));
  933. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  934. struct tomoyo_acl_param *param,
  935. bool (*check_duplicate)
  936. (const struct tomoyo_acl_head *,
  937. const struct tomoyo_acl_head *));
  938. int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
  939. int tomoyo_write_file(struct tomoyo_acl_param *param);
  940. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
  941. int tomoyo_write_misc(struct tomoyo_acl_param *param);
  942. int tomoyo_write_inet_network(struct tomoyo_acl_param *param);
  943. int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
  944. const u8 type);
  945. int tomoyo_write_unix_network(struct tomoyo_acl_param *param);
  946. ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  947. const int buffer_len);
  948. ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
  949. const char __user *buffer, const int buffer_len);
  950. struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
  951. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  952. const bool transit);
  953. struct tomoyo_domain_info *tomoyo_domain(void);
  954. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  955. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  956. const u8 idx);
  957. struct tomoyo_policy_namespace *tomoyo_assign_namespace
  958. (const char *domainname);
  959. struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
  960. const u8 profile);
  961. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  962. const u8 index);
  963. u8 tomoyo_parse_ulong(unsigned long *result, char **str);
  964. void *tomoyo_commit_ok(void *data, const unsigned int size);
  965. void __init tomoyo_load_builtin_policy(void);
  966. void __init tomoyo_mm_init(void);
  967. void tomoyo_check_acl(struct tomoyo_request_info *r,
  968. bool (*check_entry)(struct tomoyo_request_info *,
  969. const struct tomoyo_acl_info *));
  970. void tomoyo_check_profile(void);
  971. void tomoyo_convert_time(time64_t time, struct tomoyo_time *stamp);
  972. void tomoyo_del_condition(struct list_head *element);
  973. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  974. void tomoyo_get_attributes(struct tomoyo_obj_info *obj);
  975. void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
  976. void tomoyo_load_policy(const char *filename);
  977. void tomoyo_normalize_line(unsigned char *buffer);
  978. void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
  979. void tomoyo_print_ip(char *buf, const unsigned int size,
  980. const struct tomoyo_ipaddr_union *ptr);
  981. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  982. const unsigned long value, const u8 type);
  983. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  984. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  985. void tomoyo_read_log(struct tomoyo_io_buffer *head);
  986. void tomoyo_update_stat(const u8 index);
  987. void tomoyo_warn_oom(const char *function);
  988. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  989. __printf(2, 3);
  990. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  991. va_list args);
  992. /********** External variable definitions. **********/
  993. extern bool tomoyo_policy_loaded;
  994. extern int tomoyo_enabled;
  995. extern const char * const tomoyo_condition_keyword
  996. [TOMOYO_MAX_CONDITION_KEYWORD];
  997. extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
  998. extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
  999. + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  1000. extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
  1001. extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  1002. extern const char * const tomoyo_proto_keyword[TOMOYO_SOCK_MAX];
  1003. extern const char * const tomoyo_socket_keyword[TOMOYO_MAX_NETWORK_OPERATION];
  1004. extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
  1005. extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  1006. extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
  1007. extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
  1008. extern struct list_head tomoyo_condition_list;
  1009. extern struct list_head tomoyo_domain_list;
  1010. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  1011. extern struct list_head tomoyo_namespace_list;
  1012. extern struct mutex tomoyo_policy_lock;
  1013. extern struct srcu_struct tomoyo_ss;
  1014. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  1015. extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  1016. extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  1017. extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  1018. extern struct lsm_blob_sizes tomoyo_blob_sizes;
  1019. /********** Inlined functions. **********/
  1020. /**
  1021. * tomoyo_read_lock - Take lock for protecting policy.
  1022. *
  1023. * Returns index number for tomoyo_read_unlock().
  1024. */
  1025. static inline int tomoyo_read_lock(void)
  1026. {
  1027. return srcu_read_lock(&tomoyo_ss);
  1028. }
  1029. /**
  1030. * tomoyo_read_unlock - Release lock for protecting policy.
  1031. *
  1032. * @idx: Index number returned by tomoyo_read_lock().
  1033. *
  1034. * Returns nothing.
  1035. */
  1036. static inline void tomoyo_read_unlock(int idx)
  1037. {
  1038. srcu_read_unlock(&tomoyo_ss, idx);
  1039. }
  1040. /**
  1041. * tomoyo_sys_getppid - Copy of getppid().
  1042. *
  1043. * Returns parent process's PID.
  1044. *
  1045. * Alpha does not have getppid() defined. To be able to build this module on
  1046. * Alpha, I have to copy getppid() from kernel/timer.c.
  1047. */
  1048. static inline pid_t tomoyo_sys_getppid(void)
  1049. {
  1050. pid_t pid;
  1051. rcu_read_lock();
  1052. pid = task_tgid_vnr(rcu_dereference(current->real_parent));
  1053. rcu_read_unlock();
  1054. return pid;
  1055. }
  1056. /**
  1057. * tomoyo_sys_getpid - Copy of getpid().
  1058. *
  1059. * Returns current thread's PID.
  1060. *
  1061. * Alpha does not have getpid() defined. To be able to build this module on
  1062. * Alpha, I have to copy getpid() from kernel/timer.c.
  1063. */
  1064. static inline pid_t tomoyo_sys_getpid(void)
  1065. {
  1066. return task_tgid_vnr(current);
  1067. }
  1068. /**
  1069. * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
  1070. *
  1071. * @a: Pointer to "struct tomoyo_path_info".
  1072. * @b: Pointer to "struct tomoyo_path_info".
  1073. *
  1074. * Returns true if @a == @b, false otherwise.
  1075. */
  1076. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  1077. const struct tomoyo_path_info *b)
  1078. {
  1079. return a->hash != b->hash || strcmp(a->name, b->name);
  1080. }
  1081. /**
  1082. * tomoyo_put_name - Drop reference on "struct tomoyo_name".
  1083. *
  1084. * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
  1085. *
  1086. * Returns nothing.
  1087. */
  1088. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  1089. {
  1090. if (name) {
  1091. struct tomoyo_name *ptr =
  1092. container_of(name, typeof(*ptr), entry);
  1093. atomic_dec(&ptr->head.users);
  1094. }
  1095. }
  1096. /**
  1097. * tomoyo_put_condition - Drop reference on "struct tomoyo_condition".
  1098. *
  1099. * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
  1100. *
  1101. * Returns nothing.
  1102. */
  1103. static inline void tomoyo_put_condition(struct tomoyo_condition *cond)
  1104. {
  1105. if (cond)
  1106. atomic_dec(&cond->head.users);
  1107. }
  1108. /**
  1109. * tomoyo_put_group - Drop reference on "struct tomoyo_group".
  1110. *
  1111. * @group: Pointer to "struct tomoyo_group". Maybe NULL.
  1112. *
  1113. * Returns nothing.
  1114. */
  1115. static inline void tomoyo_put_group(struct tomoyo_group *group)
  1116. {
  1117. if (group)
  1118. atomic_dec(&group->head.users);
  1119. }
  1120. /**
  1121. * tomoyo_task - Get "struct tomoyo_task" for specified thread.
  1122. *
  1123. * @task - Pointer to "struct task_struct".
  1124. *
  1125. * Returns pointer to "struct tomoyo_task" for specified thread.
  1126. */
  1127. static inline struct tomoyo_task *tomoyo_task(struct task_struct *task)
  1128. {
  1129. return task->security + tomoyo_blob_sizes.lbs_task;
  1130. }
  1131. /**
  1132. * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
  1133. *
  1134. * @a: Pointer to "struct tomoyo_name_union".
  1135. * @b: Pointer to "struct tomoyo_name_union".
  1136. *
  1137. * Returns true if @a == @b, false otherwise.
  1138. */
  1139. static inline bool tomoyo_same_name_union
  1140. (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
  1141. {
  1142. return a->filename == b->filename && a->group == b->group;
  1143. }
  1144. /**
  1145. * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
  1146. *
  1147. * @a: Pointer to "struct tomoyo_number_union".
  1148. * @b: Pointer to "struct tomoyo_number_union".
  1149. *
  1150. * Returns true if @a == @b, false otherwise.
  1151. */
  1152. static inline bool tomoyo_same_number_union
  1153. (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
  1154. {
  1155. return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
  1156. a->group == b->group && a->value_type[0] == b->value_type[0] &&
  1157. a->value_type[1] == b->value_type[1];
  1158. }
  1159. /**
  1160. * tomoyo_same_ipaddr_union - Check for duplicated "struct tomoyo_ipaddr_union" entry.
  1161. *
  1162. * @a: Pointer to "struct tomoyo_ipaddr_union".
  1163. * @b: Pointer to "struct tomoyo_ipaddr_union".
  1164. *
  1165. * Returns true if @a == @b, false otherwise.
  1166. */
  1167. static inline bool tomoyo_same_ipaddr_union
  1168. (const struct tomoyo_ipaddr_union *a, const struct tomoyo_ipaddr_union *b)
  1169. {
  1170. return !memcmp(a->ip, b->ip, sizeof(a->ip)) && a->group == b->group &&
  1171. a->is_ipv6 == b->is_ipv6;
  1172. }
  1173. /**
  1174. * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
  1175. *
  1176. * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
  1177. */
  1178. static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
  1179. {
  1180. return tomoyo_domain()->ns;
  1181. }
  1182. #if defined(CONFIG_SLOB)
  1183. /**
  1184. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  1185. *
  1186. * @size: Size to be rounded up.
  1187. *
  1188. * Returns @size.
  1189. *
  1190. * Since SLOB does not round up, this function simply returns @size.
  1191. */
  1192. static inline int tomoyo_round2(size_t size)
  1193. {
  1194. return size;
  1195. }
  1196. #else
  1197. /**
  1198. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  1199. *
  1200. * @size: Size to be rounded up.
  1201. *
  1202. * Returns rounded size.
  1203. *
  1204. * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
  1205. * (e.g.) 128 bytes.
  1206. */
  1207. static inline int tomoyo_round2(size_t size)
  1208. {
  1209. #if PAGE_SIZE == 4096
  1210. size_t bsize = 32;
  1211. #else
  1212. size_t bsize = 64;
  1213. #endif
  1214. if (!size)
  1215. return 0;
  1216. while (size > bsize)
  1217. bsize <<= 1;
  1218. return bsize;
  1219. }
  1220. #endif
  1221. /**
  1222. * list_for_each_cookie - iterate over a list with cookie.
  1223. * @pos: the &struct list_head to use as a loop cursor.
  1224. * @head: the head for your list.
  1225. */
  1226. #define list_for_each_cookie(pos, head) \
  1227. if (!pos) \
  1228. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  1229. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  1230. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */