common.h 35 KB

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