common.h 40 KB

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