common.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. uid_t uid;
  517. gid_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. bool is_domain; /* True if manager is a domainname. */
  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(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. struct path *path, const int flag);
  884. int 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, struct path *path,
  895. const unsigned int mode, unsigned int dev);
  896. int tomoyo_mount_permission(const char *dev_name, 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, struct path *path1,
  901. struct path *path2);
  902. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  903. unsigned long number);
  904. int tomoyo_path_perm(const u8 operation, struct path *path,
  905. const char *target);
  906. unsigned int tomoyo_poll_control(struct file *file, poll_table *wait);
  907. unsigned int 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(time_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) */