common.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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 struct tomoyo_path_info *tomoyo_compare_name_union
  883. (const struct tomoyo_path_info *name, const struct tomoyo_name_union *ptr);
  884. const struct tomoyo_path_info *tomoyo_get_domainname
  885. (struct tomoyo_acl_param *param);
  886. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  887. const struct tomoyo_path_info *tomoyo_path_matches_group
  888. (const struct tomoyo_path_info *pathname, const struct tomoyo_group *group);
  889. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  890. const struct path *path, const int flag);
  891. void tomoyo_close_control(struct tomoyo_io_buffer *head);
  892. int tomoyo_env_perm(struct tomoyo_request_info *r, const char *env);
  893. int tomoyo_execute_permission(struct tomoyo_request_info *r,
  894. const struct tomoyo_path_info *filename);
  895. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  896. int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile,
  897. const u8 index);
  898. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  899. struct tomoyo_domain_info *domain,
  900. const u8 index);
  901. int tomoyo_mkdev_perm(const u8 operation, const struct path *path,
  902. const unsigned int mode, unsigned int dev);
  903. int tomoyo_mount_permission(const char *dev_name, const struct path *path,
  904. const char *type, unsigned long flags,
  905. void *data_page);
  906. int tomoyo_open_control(const u8 type, struct file *file);
  907. int tomoyo_path2_perm(const u8 operation, const struct path *path1,
  908. const struct path *path2);
  909. int tomoyo_path_number_perm(const u8 operation, const struct path *path,
  910. unsigned long number);
  911. int tomoyo_path_perm(const u8 operation, const struct path *path,
  912. const char *target);
  913. __poll_t tomoyo_poll_control(struct file *file, poll_table *wait);
  914. __poll_t tomoyo_poll_log(struct file *file, poll_table *wait);
  915. int tomoyo_socket_bind_permission(struct socket *sock, struct sockaddr *addr,
  916. int addr_len);
  917. int tomoyo_socket_connect_permission(struct socket *sock,
  918. struct sockaddr *addr, int addr_len);
  919. int tomoyo_socket_listen_permission(struct socket *sock);
  920. int tomoyo_socket_sendmsg_permission(struct socket *sock, struct msghdr *msg,
  921. int size);
  922. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  923. __printf(2, 3);
  924. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  925. struct tomoyo_acl_param *param,
  926. bool (*check_duplicate)
  927. (const struct tomoyo_acl_info *,
  928. const struct tomoyo_acl_info *),
  929. bool (*merge_duplicate)
  930. (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
  931. const bool));
  932. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  933. struct tomoyo_acl_param *param,
  934. bool (*check_duplicate)
  935. (const struct tomoyo_acl_head *,
  936. const struct tomoyo_acl_head *));
  937. int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
  938. int tomoyo_write_file(struct tomoyo_acl_param *param);
  939. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
  940. int tomoyo_write_misc(struct tomoyo_acl_param *param);
  941. int tomoyo_write_inet_network(struct tomoyo_acl_param *param);
  942. int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
  943. const u8 type);
  944. int tomoyo_write_unix_network(struct tomoyo_acl_param *param);
  945. ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  946. const int buffer_len);
  947. ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
  948. const char __user *buffer, const int buffer_len);
  949. struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
  950. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  951. const bool transit);
  952. struct tomoyo_domain_info *tomoyo_domain(void);
  953. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  954. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  955. const u8 idx);
  956. struct tomoyo_policy_namespace *tomoyo_assign_namespace
  957. (const char *domainname);
  958. struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
  959. const u8 profile);
  960. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  961. const u8 index);
  962. u8 tomoyo_parse_ulong(unsigned long *result, char **str);
  963. void *tomoyo_commit_ok(void *data, const unsigned int size);
  964. void __init tomoyo_load_builtin_policy(void);
  965. void __init tomoyo_mm_init(void);
  966. void tomoyo_check_acl(struct tomoyo_request_info *r,
  967. bool (*check_entry)(struct tomoyo_request_info *,
  968. const struct tomoyo_acl_info *));
  969. void tomoyo_check_profile(void);
  970. void tomoyo_convert_time(time64_t time, struct tomoyo_time *stamp);
  971. void tomoyo_del_condition(struct list_head *element);
  972. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  973. void tomoyo_get_attributes(struct tomoyo_obj_info *obj);
  974. void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
  975. void tomoyo_load_policy(const char *filename);
  976. void tomoyo_normalize_line(unsigned char *buffer);
  977. void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
  978. void tomoyo_print_ip(char *buf, const unsigned int size,
  979. const struct tomoyo_ipaddr_union *ptr);
  980. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  981. const unsigned long value, const u8 type);
  982. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  983. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  984. void tomoyo_read_log(struct tomoyo_io_buffer *head);
  985. void tomoyo_update_stat(const u8 index);
  986. void tomoyo_warn_oom(const char *function);
  987. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  988. __printf(2, 3);
  989. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  990. va_list args);
  991. /********** External variable definitions. **********/
  992. extern bool tomoyo_policy_loaded;
  993. extern int tomoyo_enabled;
  994. extern const char * const tomoyo_condition_keyword
  995. [TOMOYO_MAX_CONDITION_KEYWORD];
  996. extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
  997. extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
  998. + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  999. extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
  1000. extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  1001. extern const char * const tomoyo_proto_keyword[TOMOYO_SOCK_MAX];
  1002. extern const char * const tomoyo_socket_keyword[TOMOYO_MAX_NETWORK_OPERATION];
  1003. extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
  1004. extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  1005. extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
  1006. extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
  1007. extern struct list_head tomoyo_condition_list;
  1008. extern struct list_head tomoyo_domain_list;
  1009. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  1010. extern struct list_head tomoyo_namespace_list;
  1011. extern struct mutex tomoyo_policy_lock;
  1012. extern struct srcu_struct tomoyo_ss;
  1013. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  1014. extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  1015. extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  1016. extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  1017. extern struct lsm_blob_sizes tomoyo_blob_sizes;
  1018. /********** Inlined functions. **********/
  1019. /**
  1020. * tomoyo_read_lock - Take lock for protecting policy.
  1021. *
  1022. * Returns index number for tomoyo_read_unlock().
  1023. */
  1024. static inline int tomoyo_read_lock(void)
  1025. {
  1026. return srcu_read_lock(&tomoyo_ss);
  1027. }
  1028. /**
  1029. * tomoyo_read_unlock - Release lock for protecting policy.
  1030. *
  1031. * @idx: Index number returned by tomoyo_read_lock().
  1032. *
  1033. * Returns nothing.
  1034. */
  1035. static inline void tomoyo_read_unlock(int idx)
  1036. {
  1037. srcu_read_unlock(&tomoyo_ss, idx);
  1038. }
  1039. /**
  1040. * tomoyo_sys_getppid - Copy of getppid().
  1041. *
  1042. * Returns parent process's PID.
  1043. *
  1044. * Alpha does not have getppid() defined. To be able to build this module on
  1045. * Alpha, I have to copy getppid() from kernel/timer.c.
  1046. */
  1047. static inline pid_t tomoyo_sys_getppid(void)
  1048. {
  1049. pid_t pid;
  1050. rcu_read_lock();
  1051. pid = task_tgid_vnr(rcu_dereference(current->real_parent));
  1052. rcu_read_unlock();
  1053. return pid;
  1054. }
  1055. /**
  1056. * tomoyo_sys_getpid - Copy of getpid().
  1057. *
  1058. * Returns current thread's PID.
  1059. *
  1060. * Alpha does not have getpid() defined. To be able to build this module on
  1061. * Alpha, I have to copy getpid() from kernel/timer.c.
  1062. */
  1063. static inline pid_t tomoyo_sys_getpid(void)
  1064. {
  1065. return task_tgid_vnr(current);
  1066. }
  1067. /**
  1068. * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
  1069. *
  1070. * @a: Pointer to "struct tomoyo_path_info".
  1071. * @b: Pointer to "struct tomoyo_path_info".
  1072. *
  1073. * Returns true if @a == @b, false otherwise.
  1074. */
  1075. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  1076. const struct tomoyo_path_info *b)
  1077. {
  1078. return a->hash != b->hash || strcmp(a->name, b->name);
  1079. }
  1080. /**
  1081. * tomoyo_put_name - Drop reference on "struct tomoyo_name".
  1082. *
  1083. * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
  1084. *
  1085. * Returns nothing.
  1086. */
  1087. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  1088. {
  1089. if (name) {
  1090. struct tomoyo_name *ptr =
  1091. container_of(name, typeof(*ptr), entry);
  1092. atomic_dec(&ptr->head.users);
  1093. }
  1094. }
  1095. /**
  1096. * tomoyo_put_condition - Drop reference on "struct tomoyo_condition".
  1097. *
  1098. * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
  1099. *
  1100. * Returns nothing.
  1101. */
  1102. static inline void tomoyo_put_condition(struct tomoyo_condition *cond)
  1103. {
  1104. if (cond)
  1105. atomic_dec(&cond->head.users);
  1106. }
  1107. /**
  1108. * tomoyo_put_group - Drop reference on "struct tomoyo_group".
  1109. *
  1110. * @group: Pointer to "struct tomoyo_group". Maybe NULL.
  1111. *
  1112. * Returns nothing.
  1113. */
  1114. static inline void tomoyo_put_group(struct tomoyo_group *group)
  1115. {
  1116. if (group)
  1117. atomic_dec(&group->head.users);
  1118. }
  1119. /**
  1120. * tomoyo_task - Get "struct tomoyo_task" for specified thread.
  1121. *
  1122. * @task - Pointer to "struct task_struct".
  1123. *
  1124. * Returns pointer to "struct tomoyo_task" for specified thread.
  1125. */
  1126. static inline struct tomoyo_task *tomoyo_task(struct task_struct *task)
  1127. {
  1128. return task->security + tomoyo_blob_sizes.lbs_task;
  1129. }
  1130. /**
  1131. * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
  1132. *
  1133. * @a: Pointer to "struct tomoyo_name_union".
  1134. * @b: Pointer to "struct tomoyo_name_union".
  1135. *
  1136. * Returns true if @a == @b, false otherwise.
  1137. */
  1138. static inline bool tomoyo_same_name_union
  1139. (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
  1140. {
  1141. return a->filename == b->filename && a->group == b->group;
  1142. }
  1143. /**
  1144. * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
  1145. *
  1146. * @a: Pointer to "struct tomoyo_number_union".
  1147. * @b: Pointer to "struct tomoyo_number_union".
  1148. *
  1149. * Returns true if @a == @b, false otherwise.
  1150. */
  1151. static inline bool tomoyo_same_number_union
  1152. (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
  1153. {
  1154. return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
  1155. a->group == b->group && a->value_type[0] == b->value_type[0] &&
  1156. a->value_type[1] == b->value_type[1];
  1157. }
  1158. /**
  1159. * tomoyo_same_ipaddr_union - Check for duplicated "struct tomoyo_ipaddr_union" entry.
  1160. *
  1161. * @a: Pointer to "struct tomoyo_ipaddr_union".
  1162. * @b: Pointer to "struct tomoyo_ipaddr_union".
  1163. *
  1164. * Returns true if @a == @b, false otherwise.
  1165. */
  1166. static inline bool tomoyo_same_ipaddr_union
  1167. (const struct tomoyo_ipaddr_union *a, const struct tomoyo_ipaddr_union *b)
  1168. {
  1169. return !memcmp(a->ip, b->ip, sizeof(a->ip)) && a->group == b->group &&
  1170. a->is_ipv6 == b->is_ipv6;
  1171. }
  1172. /**
  1173. * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
  1174. *
  1175. * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
  1176. */
  1177. static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
  1178. {
  1179. return tomoyo_domain()->ns;
  1180. }
  1181. #if defined(CONFIG_SLOB)
  1182. /**
  1183. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  1184. *
  1185. * @size: Size to be rounded up.
  1186. *
  1187. * Returns @size.
  1188. *
  1189. * Since SLOB does not round up, this function simply returns @size.
  1190. */
  1191. static inline int tomoyo_round2(size_t size)
  1192. {
  1193. return size;
  1194. }
  1195. #else
  1196. /**
  1197. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  1198. *
  1199. * @size: Size to be rounded up.
  1200. *
  1201. * Returns rounded size.
  1202. *
  1203. * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
  1204. * (e.g.) 128 bytes.
  1205. */
  1206. static inline int tomoyo_round2(size_t size)
  1207. {
  1208. #if PAGE_SIZE == 4096
  1209. size_t bsize = 32;
  1210. #else
  1211. size_t bsize = 64;
  1212. #endif
  1213. if (!size)
  1214. return 0;
  1215. while (size > bsize)
  1216. bsize <<= 1;
  1217. return bsize;
  1218. }
  1219. #endif
  1220. /**
  1221. * list_for_each_cookie - iterate over a list with cookie.
  1222. * @pos: the &struct list_head to use as a loop cursor.
  1223. * @head: the head for your list.
  1224. */
  1225. #define list_for_each_cookie(pos, head) \
  1226. if (!pos) \
  1227. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  1228. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  1229. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */