common.h 33 KB

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