common.h 40 KB

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