common.h 40 KB

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