common.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  773. const struct tomoyo_path_info *filename);
  774. int tomoyo_poll_control(struct file *file, poll_table *wait);
  775. int tomoyo_poll_log(struct file *file, poll_table *wait);
  776. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  777. __printf(2, 3);
  778. int tomoyo_update_domain(struct tomoyo_acl_info *new_entry, const int size,
  779. struct tomoyo_acl_param *param,
  780. bool (*check_duplicate)
  781. (const struct tomoyo_acl_info *,
  782. const struct tomoyo_acl_info *),
  783. bool (*merge_duplicate)
  784. (struct tomoyo_acl_info *, struct tomoyo_acl_info *,
  785. const bool));
  786. int tomoyo_update_policy(struct tomoyo_acl_head *new_entry, const int size,
  787. struct tomoyo_acl_param *param,
  788. bool (*check_duplicate)
  789. (const struct tomoyo_acl_head *,
  790. const struct tomoyo_acl_head *));
  791. int tomoyo_write_aggregator(struct tomoyo_acl_param *param);
  792. int tomoyo_write_file(struct tomoyo_acl_param *param);
  793. int tomoyo_write_group(struct tomoyo_acl_param *param, const u8 type);
  794. int tomoyo_write_transition_control(struct tomoyo_acl_param *param,
  795. const u8 type);
  796. ssize_t tomoyo_read_control(struct tomoyo_io_buffer *head, char __user *buffer,
  797. const int buffer_len);
  798. ssize_t tomoyo_write_control(struct tomoyo_io_buffer *head,
  799. const char __user *buffer, const int buffer_len);
  800. struct tomoyo_condition *tomoyo_get_condition(struct tomoyo_acl_param *param);
  801. struct tomoyo_domain_info *tomoyo_assign_domain(const char *domainname,
  802. const bool transit);
  803. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  804. struct tomoyo_group *tomoyo_get_group(struct tomoyo_acl_param *param,
  805. const u8 idx);
  806. struct tomoyo_policy_namespace *tomoyo_assign_namespace
  807. (const char *domainname);
  808. struct tomoyo_profile *tomoyo_profile(const struct tomoyo_policy_namespace *ns,
  809. const u8 profile);
  810. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  811. const u8 index);
  812. u8 tomoyo_parse_ulong(unsigned long *result, char **str);
  813. void *tomoyo_commit_ok(void *data, const unsigned int size);
  814. void __init tomoyo_load_builtin_policy(void);
  815. void __init tomoyo_mm_init(void);
  816. void tomoyo_check_acl(struct tomoyo_request_info *r,
  817. bool (*check_entry) (struct tomoyo_request_info *,
  818. const struct tomoyo_acl_info *));
  819. void tomoyo_check_profile(void);
  820. void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp);
  821. void tomoyo_del_condition(struct list_head *element);
  822. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  823. void tomoyo_get_attributes(struct tomoyo_obj_info *obj);
  824. void tomoyo_init_policy_namespace(struct tomoyo_policy_namespace *ns);
  825. void tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  826. __printf(2, 3);
  827. void tomoyo_load_policy(const char *filename);
  828. void tomoyo_memory_free(void *ptr);
  829. void tomoyo_normalize_line(unsigned char *buffer);
  830. void tomoyo_notify_gc(struct tomoyo_io_buffer *head, const bool is_register);
  831. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  832. const unsigned long value, const u8 type);
  833. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  834. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  835. void tomoyo_read_log(struct tomoyo_io_buffer *head);
  836. void tomoyo_update_stat(const u8 index);
  837. void tomoyo_warn_oom(const char *function);
  838. void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
  839. __printf(2, 3);
  840. void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
  841. va_list args);
  842. /********** External variable definitions. **********/
  843. extern bool tomoyo_policy_loaded;
  844. extern const char * const tomoyo_condition_keyword
  845. [TOMOYO_MAX_CONDITION_KEYWORD];
  846. extern const char * const tomoyo_dif[TOMOYO_MAX_DOMAIN_INFO_FLAGS];
  847. extern const char * const tomoyo_mac_keywords[TOMOYO_MAX_MAC_INDEX
  848. + TOMOYO_MAX_MAC_CATEGORY_INDEX];
  849. extern const char * const tomoyo_mode[TOMOYO_CONFIG_MAX_MODE];
  850. extern const char * const tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION];
  851. extern const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX];
  852. extern const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION];
  853. extern const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION];
  854. extern const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION];
  855. extern struct list_head tomoyo_condition_list;
  856. extern struct list_head tomoyo_domain_list;
  857. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  858. extern struct list_head tomoyo_namespace_list;
  859. extern struct mutex tomoyo_policy_lock;
  860. extern struct srcu_struct tomoyo_ss;
  861. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  862. extern struct tomoyo_policy_namespace tomoyo_kernel_namespace;
  863. extern unsigned int tomoyo_memory_quota[TOMOYO_MAX_MEMORY_STAT];
  864. extern unsigned int tomoyo_memory_used[TOMOYO_MAX_MEMORY_STAT];
  865. /********** Inlined functions. **********/
  866. /**
  867. * tomoyo_read_lock - Take lock for protecting policy.
  868. *
  869. * Returns index number for tomoyo_read_unlock().
  870. */
  871. static inline int tomoyo_read_lock(void)
  872. {
  873. return srcu_read_lock(&tomoyo_ss);
  874. }
  875. /**
  876. * tomoyo_read_unlock - Release lock for protecting policy.
  877. *
  878. * @idx: Index number returned by tomoyo_read_lock().
  879. *
  880. * Returns nothing.
  881. */
  882. static inline void tomoyo_read_unlock(int idx)
  883. {
  884. srcu_read_unlock(&tomoyo_ss, idx);
  885. }
  886. /**
  887. * tomoyo_sys_getppid - Copy of getppid().
  888. *
  889. * Returns parent process's PID.
  890. *
  891. * Alpha does not have getppid() defined. To be able to build this module on
  892. * Alpha, I have to copy getppid() from kernel/timer.c.
  893. */
  894. static inline pid_t tomoyo_sys_getppid(void)
  895. {
  896. pid_t pid;
  897. rcu_read_lock();
  898. pid = task_tgid_vnr(current->real_parent);
  899. rcu_read_unlock();
  900. return pid;
  901. }
  902. /**
  903. * tomoyo_sys_getpid - Copy of getpid().
  904. *
  905. * Returns current thread's PID.
  906. *
  907. * Alpha does not have getpid() defined. To be able to build this module on
  908. * Alpha, I have to copy getpid() from kernel/timer.c.
  909. */
  910. static inline pid_t tomoyo_sys_getpid(void)
  911. {
  912. return task_tgid_vnr(current);
  913. }
  914. /**
  915. * tomoyo_pathcmp - strcmp() for "struct tomoyo_path_info" structure.
  916. *
  917. * @a: Pointer to "struct tomoyo_path_info".
  918. * @b: Pointer to "struct tomoyo_path_info".
  919. *
  920. * Returns true if @a == @b, false otherwise.
  921. */
  922. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  923. const struct tomoyo_path_info *b)
  924. {
  925. return a->hash != b->hash || strcmp(a->name, b->name);
  926. }
  927. /**
  928. * tomoyo_put_name - Drop reference on "struct tomoyo_name".
  929. *
  930. * @name: Pointer to "struct tomoyo_path_info". Maybe NULL.
  931. *
  932. * Returns nothing.
  933. */
  934. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  935. {
  936. if (name) {
  937. struct tomoyo_name *ptr =
  938. container_of(name, typeof(*ptr), entry);
  939. atomic_dec(&ptr->head.users);
  940. }
  941. }
  942. /**
  943. * tomoyo_put_condition - Drop reference on "struct tomoyo_condition".
  944. *
  945. * @cond: Pointer to "struct tomoyo_condition". Maybe NULL.
  946. *
  947. * Returns nothing.
  948. */
  949. static inline void tomoyo_put_condition(struct tomoyo_condition *cond)
  950. {
  951. if (cond)
  952. atomic_dec(&cond->head.users);
  953. }
  954. /**
  955. * tomoyo_put_group - Drop reference on "struct tomoyo_group".
  956. *
  957. * @group: Pointer to "struct tomoyo_group". Maybe NULL.
  958. *
  959. * Returns nothing.
  960. */
  961. static inline void tomoyo_put_group(struct tomoyo_group *group)
  962. {
  963. if (group)
  964. atomic_dec(&group->head.users);
  965. }
  966. /**
  967. * tomoyo_domain - Get "struct tomoyo_domain_info" for current thread.
  968. *
  969. * Returns pointer to "struct tomoyo_domain_info" for current thread.
  970. */
  971. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  972. {
  973. return current_cred()->security;
  974. }
  975. /**
  976. * tomoyo_real_domain - Get "struct tomoyo_domain_info" for specified thread.
  977. *
  978. * @task: Pointer to "struct task_struct".
  979. *
  980. * Returns pointer to "struct tomoyo_security" for specified thread.
  981. */
  982. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  983. *task)
  984. {
  985. return task_cred_xxx(task, security);
  986. }
  987. /**
  988. * tomoyo_same_name_union - Check for duplicated "struct tomoyo_name_union" entry.
  989. *
  990. * @a: Pointer to "struct tomoyo_name_union".
  991. * @b: Pointer to "struct tomoyo_name_union".
  992. *
  993. * Returns true if @a == @b, false otherwise.
  994. */
  995. static inline bool tomoyo_same_name_union
  996. (const struct tomoyo_name_union *a, const struct tomoyo_name_union *b)
  997. {
  998. return a->filename == b->filename && a->group == b->group;
  999. }
  1000. /**
  1001. * tomoyo_same_number_union - Check for duplicated "struct tomoyo_number_union" entry.
  1002. *
  1003. * @a: Pointer to "struct tomoyo_number_union".
  1004. * @b: Pointer to "struct tomoyo_number_union".
  1005. *
  1006. * Returns true if @a == @b, false otherwise.
  1007. */
  1008. static inline bool tomoyo_same_number_union
  1009. (const struct tomoyo_number_union *a, const struct tomoyo_number_union *b)
  1010. {
  1011. return a->values[0] == b->values[0] && a->values[1] == b->values[1] &&
  1012. a->group == b->group && a->value_type[0] == b->value_type[0] &&
  1013. a->value_type[1] == b->value_type[1];
  1014. }
  1015. /**
  1016. * tomoyo_current_namespace - Get "struct tomoyo_policy_namespace" for current thread.
  1017. *
  1018. * Returns pointer to "struct tomoyo_policy_namespace" for current thread.
  1019. */
  1020. static inline struct tomoyo_policy_namespace *tomoyo_current_namespace(void)
  1021. {
  1022. return tomoyo_domain()->ns;
  1023. }
  1024. #if defined(CONFIG_SLOB)
  1025. /**
  1026. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  1027. *
  1028. * @size: Size to be rounded up.
  1029. *
  1030. * Returns @size.
  1031. *
  1032. * Since SLOB does not round up, this function simply returns @size.
  1033. */
  1034. static inline int tomoyo_round2(size_t size)
  1035. {
  1036. return size;
  1037. }
  1038. #else
  1039. /**
  1040. * tomoyo_round2 - Round up to power of 2 for calculating memory usage.
  1041. *
  1042. * @size: Size to be rounded up.
  1043. *
  1044. * Returns rounded size.
  1045. *
  1046. * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
  1047. * (e.g.) 128 bytes.
  1048. */
  1049. static inline int tomoyo_round2(size_t size)
  1050. {
  1051. #if PAGE_SIZE == 4096
  1052. size_t bsize = 32;
  1053. #else
  1054. size_t bsize = 64;
  1055. #endif
  1056. if (!size)
  1057. return 0;
  1058. while (size > bsize)
  1059. bsize <<= 1;
  1060. return bsize;
  1061. }
  1062. #endif
  1063. /**
  1064. * list_for_each_cookie - iterate over a list with cookie.
  1065. * @pos: the &struct list_head to use as a loop cursor.
  1066. * @head: the head for your list.
  1067. */
  1068. #define list_for_each_cookie(pos, head) \
  1069. if (!pos) \
  1070. pos = srcu_dereference((head)->next, &tomoyo_ss); \
  1071. for ( ; pos != (head); pos = srcu_dereference(pos->next, &tomoyo_ss))
  1072. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */