common.h 39 KB

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