common.h 35 KB

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