common.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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. struct linux_binprm;
  23. /********** Constants definitions. **********/
  24. /*
  25. * TOMOYO uses this hash only when appending a string into the string
  26. * table. Frequency of appending strings is very low. So we don't need
  27. * large (e.g. 64k) hash size. 256 will be sufficient.
  28. */
  29. #define TOMOYO_HASH_BITS 8
  30. #define TOMOYO_MAX_HASH (1u<<TOMOYO_HASH_BITS)
  31. #define TOMOYO_EXEC_TMPSIZE 4096
  32. /* Profile number is an integer between 0 and 255. */
  33. #define TOMOYO_MAX_PROFILES 256
  34. enum tomoyo_mode_index {
  35. TOMOYO_CONFIG_DISABLED,
  36. TOMOYO_CONFIG_LEARNING,
  37. TOMOYO_CONFIG_PERMISSIVE,
  38. TOMOYO_CONFIG_ENFORCING
  39. };
  40. /* Keywords for ACLs. */
  41. #define TOMOYO_KEYWORD_ALIAS "alias "
  42. #define TOMOYO_KEYWORD_ALLOW_MOUNT "allow_mount "
  43. #define TOMOYO_KEYWORD_ALLOW_READ "allow_read "
  44. #define TOMOYO_KEYWORD_DELETE "delete "
  45. #define TOMOYO_KEYWORD_DENY_REWRITE "deny_rewrite "
  46. #define TOMOYO_KEYWORD_FILE_PATTERN "file_pattern "
  47. #define TOMOYO_KEYWORD_INITIALIZE_DOMAIN "initialize_domain "
  48. #define TOMOYO_KEYWORD_KEEP_DOMAIN "keep_domain "
  49. #define TOMOYO_KEYWORD_NO_INITIALIZE_DOMAIN "no_initialize_domain "
  50. #define TOMOYO_KEYWORD_NO_KEEP_DOMAIN "no_keep_domain "
  51. #define TOMOYO_KEYWORD_PATH_GROUP "path_group "
  52. #define TOMOYO_KEYWORD_NUMBER_GROUP "number_group "
  53. #define TOMOYO_KEYWORD_SELECT "select "
  54. #define TOMOYO_KEYWORD_USE_PROFILE "use_profile "
  55. #define TOMOYO_KEYWORD_IGNORE_GLOBAL_ALLOW_READ "ignore_global_allow_read"
  56. #define TOMOYO_KEYWORD_QUOTA_EXCEEDED "quota_exceeded"
  57. #define TOMOYO_KEYWORD_TRANSITION_FAILED "transition_failed"
  58. /* A domain definition starts with <kernel>. */
  59. #define TOMOYO_ROOT_NAME "<kernel>"
  60. #define TOMOYO_ROOT_NAME_LEN (sizeof(TOMOYO_ROOT_NAME) - 1)
  61. /* Value type definition. */
  62. #define TOMOYO_VALUE_TYPE_INVALID 0
  63. #define TOMOYO_VALUE_TYPE_DECIMAL 1
  64. #define TOMOYO_VALUE_TYPE_OCTAL 2
  65. #define TOMOYO_VALUE_TYPE_HEXADECIMAL 3
  66. /* Index numbers for Access Controls. */
  67. enum tomoyo_mac_index {
  68. TOMOYO_MAC_FOR_FILE, /* domain_policy.conf */
  69. TOMOYO_MAX_ACCEPT_ENTRY,
  70. TOMOYO_VERBOSE,
  71. TOMOYO_MAX_CONTROL_INDEX
  72. };
  73. /* Index numbers for Access Controls. */
  74. enum tomoyo_acl_entry_type_index {
  75. TOMOYO_TYPE_PATH_ACL,
  76. TOMOYO_TYPE_PATH2_ACL,
  77. TOMOYO_TYPE_PATH_NUMBER_ACL,
  78. TOMOYO_TYPE_PATH_NUMBER3_ACL,
  79. TOMOYO_TYPE_MOUNT_ACL,
  80. };
  81. /* Index numbers for File Controls. */
  82. /*
  83. * TOMOYO_TYPE_READ_WRITE is special. TOMOYO_TYPE_READ_WRITE is automatically
  84. * set if both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are set.
  85. * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically set if
  86. * TOMOYO_TYPE_READ_WRITE is set.
  87. * TOMOYO_TYPE_READ_WRITE is automatically cleared if either TOMOYO_TYPE_READ
  88. * or TOMOYO_TYPE_WRITE is cleared.
  89. * Both TOMOYO_TYPE_READ and TOMOYO_TYPE_WRITE are automatically cleared if
  90. * TOMOYO_TYPE_READ_WRITE is cleared.
  91. */
  92. enum tomoyo_path_acl_index {
  93. TOMOYO_TYPE_READ_WRITE,
  94. TOMOYO_TYPE_EXECUTE,
  95. TOMOYO_TYPE_READ,
  96. TOMOYO_TYPE_WRITE,
  97. TOMOYO_TYPE_UNLINK,
  98. TOMOYO_TYPE_RMDIR,
  99. TOMOYO_TYPE_TRUNCATE,
  100. TOMOYO_TYPE_SYMLINK,
  101. TOMOYO_TYPE_REWRITE,
  102. TOMOYO_TYPE_CHROOT,
  103. TOMOYO_TYPE_UMOUNT,
  104. TOMOYO_MAX_PATH_OPERATION
  105. };
  106. enum tomoyo_path_number3_acl_index {
  107. TOMOYO_TYPE_MKBLOCK,
  108. TOMOYO_TYPE_MKCHAR,
  109. TOMOYO_MAX_PATH_NUMBER3_OPERATION
  110. };
  111. enum tomoyo_path2_acl_index {
  112. TOMOYO_TYPE_LINK,
  113. TOMOYO_TYPE_RENAME,
  114. TOMOYO_TYPE_PIVOT_ROOT,
  115. TOMOYO_MAX_PATH2_OPERATION
  116. };
  117. enum tomoyo_path_number_acl_index {
  118. TOMOYO_TYPE_CREATE,
  119. TOMOYO_TYPE_MKDIR,
  120. TOMOYO_TYPE_MKFIFO,
  121. TOMOYO_TYPE_MKSOCK,
  122. TOMOYO_TYPE_IOCTL,
  123. TOMOYO_TYPE_CHMOD,
  124. TOMOYO_TYPE_CHOWN,
  125. TOMOYO_TYPE_CHGRP,
  126. TOMOYO_MAX_PATH_NUMBER_OPERATION
  127. };
  128. enum tomoyo_securityfs_interface_index {
  129. TOMOYO_DOMAINPOLICY,
  130. TOMOYO_EXCEPTIONPOLICY,
  131. TOMOYO_DOMAIN_STATUS,
  132. TOMOYO_PROCESS_STATUS,
  133. TOMOYO_MEMINFO,
  134. TOMOYO_SELFDOMAIN,
  135. TOMOYO_VERSION,
  136. TOMOYO_PROFILE,
  137. TOMOYO_QUERY,
  138. TOMOYO_MANAGER
  139. };
  140. #define TOMOYO_RETRY_REQUEST 1 /* Retry this request. */
  141. /********** Structure definitions. **********/
  142. /*
  143. * tomoyo_request_info is a structure which is used for holding
  144. *
  145. * (1) Domain information of current process.
  146. * (2) How many retries are made for this request.
  147. * (3) Profile number used for this request.
  148. * (4) Access control mode of the profile.
  149. */
  150. struct tomoyo_request_info {
  151. struct tomoyo_domain_info *domain;
  152. u8 retry;
  153. u8 profile;
  154. u8 mode; /* One of tomoyo_mode_index . */
  155. };
  156. /*
  157. * tomoyo_path_info is a structure which is used for holding a string data
  158. * used by TOMOYO.
  159. * This structure has several fields for supporting pattern matching.
  160. *
  161. * (1) "name" is the '\0' terminated string data.
  162. * (2) "hash" is full_name_hash(name, strlen(name)).
  163. * This allows tomoyo_pathcmp() to compare by hash before actually compare
  164. * using strcmp().
  165. * (3) "const_len" is the length of the initial segment of "name" which
  166. * consists entirely of non wildcard characters. In other words, the length
  167. * which we can compare two strings using strncmp().
  168. * (4) "is_dir" is a bool which is true if "name" ends with "/",
  169. * false otherwise.
  170. * TOMOYO distinguishes directory and non-directory. A directory ends with
  171. * "/" and non-directory does not end with "/".
  172. * (5) "is_patterned" is a bool which is true if "name" contains wildcard
  173. * characters, false otherwise. This allows TOMOYO to use "hash" and
  174. * strcmp() for string comparison if "is_patterned" is false.
  175. */
  176. struct tomoyo_path_info {
  177. const char *name;
  178. u32 hash; /* = full_name_hash(name, strlen(name)) */
  179. u16 const_len; /* = tomoyo_const_part_length(name) */
  180. bool is_dir; /* = tomoyo_strendswith(name, "/") */
  181. bool is_patterned; /* = tomoyo_path_contains_pattern(name) */
  182. };
  183. /*
  184. * tomoyo_name_entry is a structure which is used for linking
  185. * "struct tomoyo_path_info" into tomoyo_name_list .
  186. */
  187. struct tomoyo_name_entry {
  188. struct list_head list;
  189. atomic_t users;
  190. struct tomoyo_path_info entry;
  191. };
  192. struct tomoyo_name_union {
  193. const struct tomoyo_path_info *filename;
  194. struct tomoyo_path_group *group;
  195. u8 is_group;
  196. };
  197. struct tomoyo_number_union {
  198. unsigned long values[2];
  199. struct tomoyo_number_group *group;
  200. u8 min_type;
  201. u8 max_type;
  202. u8 is_group;
  203. };
  204. /* Structure for "path_group" directive. */
  205. struct tomoyo_path_group {
  206. struct list_head list;
  207. const struct tomoyo_path_info *group_name;
  208. struct list_head member_list;
  209. atomic_t users;
  210. };
  211. /* Structure for "number_group" directive. */
  212. struct tomoyo_number_group {
  213. struct list_head list;
  214. const struct tomoyo_path_info *group_name;
  215. struct list_head member_list;
  216. atomic_t users;
  217. };
  218. /* Structure for "path_group" directive. */
  219. struct tomoyo_path_group_member {
  220. struct list_head list;
  221. bool is_deleted;
  222. const struct tomoyo_path_info *member_name;
  223. };
  224. /* Structure for "number_group" directive. */
  225. struct tomoyo_number_group_member {
  226. struct list_head list;
  227. bool is_deleted;
  228. struct tomoyo_number_union number;
  229. };
  230. /*
  231. * tomoyo_acl_info is a structure which is used for holding
  232. *
  233. * (1) "list" which is linked to the ->acl_info_list of
  234. * "struct tomoyo_domain_info"
  235. * (2) "type" which tells type of the entry (either
  236. * "struct tomoyo_path_acl" or "struct tomoyo_path2_acl").
  237. *
  238. * Packing "struct tomoyo_acl_info" allows
  239. * "struct tomoyo_path_acl" to embed "u8" + "u16" and
  240. * "struct tomoyo_path2_acl" to embed "u8"
  241. * without enlarging their structure size.
  242. */
  243. struct tomoyo_acl_info {
  244. struct list_head list;
  245. u8 type;
  246. } __packed;
  247. /*
  248. * tomoyo_domain_info is a structure which is used for holding permissions
  249. * (e.g. "allow_read /lib/libc-2.5.so") given to each domain.
  250. * It has following fields.
  251. *
  252. * (1) "list" which is linked to tomoyo_domain_list .
  253. * (2) "acl_info_list" which is linked to "struct tomoyo_acl_info".
  254. * (3) "domainname" which holds the name of the domain.
  255. * (4) "profile" which remembers profile number assigned to this domain.
  256. * (5) "is_deleted" is a bool which is true if this domain is marked as
  257. * "deleted", false otherwise.
  258. * (6) "quota_warned" is a bool which is used for suppressing warning message
  259. * when learning mode learned too much entries.
  260. * (7) "ignore_global_allow_read" is a bool which is true if this domain
  261. * should ignore "allow_read" directive in exception policy.
  262. * (8) "transition_failed" is a bool which is set to true when this domain was
  263. * unable to create a new domain at tomoyo_find_next_domain() because the
  264. * name of the domain to be created was too long or it could not allocate
  265. * memory. If set to true, more than one process continued execve()
  266. * without domain transition.
  267. * (9) "users" is an atomic_t that holds how many "struct cred"->security
  268. * are referring this "struct tomoyo_domain_info". If is_deleted == true
  269. * and users == 0, this struct will be kfree()d upon next garbage
  270. * collection.
  271. *
  272. * A domain's lifecycle is an analogy of files on / directory.
  273. * Multiple domains with the same domainname cannot be created (as with
  274. * creating files with the same filename fails with -EEXIST).
  275. * If a process reached a domain, that process can reside in that domain after
  276. * that domain is marked as "deleted" (as with a process can access an already
  277. * open()ed file after that file was unlink()ed).
  278. */
  279. struct tomoyo_domain_info {
  280. struct list_head list;
  281. struct list_head acl_info_list;
  282. /* Name of this domain. Never NULL. */
  283. const struct tomoyo_path_info *domainname;
  284. u8 profile; /* Profile number to use. */
  285. bool is_deleted; /* Delete flag. */
  286. bool quota_warned; /* Quota warnning flag. */
  287. bool ignore_global_allow_read; /* Ignore "allow_read" flag. */
  288. bool transition_failed; /* Domain transition failed flag. */
  289. atomic_t users; /* Number of referring credentials. */
  290. };
  291. /*
  292. * tomoyo_path_acl is a structure which is used for holding an
  293. * entry with one pathname operation (e.g. open(), mkdir()).
  294. * It has following fields.
  295. *
  296. * (1) "head" which is a "struct tomoyo_acl_info".
  297. * (2) "perm" which is a bitmask of permitted operations.
  298. * (3) "name" is the pathname.
  299. *
  300. * Directives held by this structure are "allow_read/write", "allow_execute",
  301. * "allow_read", "allow_write", "allow_unlink", "allow_rmdir",
  302. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_chroot" and
  303. * "allow_unmount".
  304. */
  305. struct tomoyo_path_acl {
  306. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_ACL */
  307. u16 perm;
  308. struct tomoyo_name_union name;
  309. };
  310. /*
  311. * tomoyo_path_number_acl is a structure which is used for holding an
  312. * entry with one pathname and one number operation.
  313. * It has following fields.
  314. *
  315. * (1) "head" which is a "struct tomoyo_acl_info".
  316. * (2) "perm" which is a bitmask of permitted operations.
  317. * (3) "name" is the pathname.
  318. * (4) "number" is the numeric value.
  319. *
  320. * Directives held by this structure are "allow_create", "allow_mkdir",
  321. * "allow_ioctl", "allow_mkfifo", "allow_mksock", "allow_chmod", "allow_chown"
  322. * and "allow_chgrp".
  323. *
  324. */
  325. struct tomoyo_path_number_acl {
  326. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER_ACL */
  327. u8 perm;
  328. struct tomoyo_name_union name;
  329. struct tomoyo_number_union number;
  330. };
  331. /*
  332. * tomoyo_path_number3_acl is a structure which is used for holding an
  333. * entry with one pathname and three numbers operation.
  334. * It has following fields.
  335. *
  336. * (1) "head" which is a "struct tomoyo_acl_info".
  337. * (2) "perm" which is a bitmask of permitted operations.
  338. * (3) "mode" is the create mode.
  339. * (4) "major" is the major number of device node.
  340. * (5) "minor" is the minor number of device node.
  341. *
  342. * Directives held by this structure are "allow_mkchar", "allow_mkblock".
  343. *
  344. */
  345. struct tomoyo_path_number3_acl {
  346. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH_NUMBER3_ACL */
  347. u8 perm;
  348. struct tomoyo_name_union name;
  349. struct tomoyo_number_union mode;
  350. struct tomoyo_number_union major;
  351. struct tomoyo_number_union minor;
  352. };
  353. /*
  354. * tomoyo_path2_acl is a structure which is used for holding an
  355. * entry with two pathnames operation (i.e. link(), rename() and pivot_root()).
  356. * It has following fields.
  357. *
  358. * (1) "head" which is a "struct tomoyo_acl_info".
  359. * (2) "perm" which is a bitmask of permitted operations.
  360. * (3) "name1" is the source/old pathname.
  361. * (4) "name2" is the destination/new pathname.
  362. *
  363. * Directives held by this structure are "allow_rename", "allow_link" and
  364. * "allow_pivot_root".
  365. */
  366. struct tomoyo_path2_acl {
  367. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_PATH2_ACL */
  368. u8 perm;
  369. struct tomoyo_name_union name1;
  370. struct tomoyo_name_union name2;
  371. };
  372. /*
  373. * tomoyo_mount_acl is a structure which is used for holding an
  374. * entry for mount operation.
  375. * It has following fields.
  376. *
  377. * (1) "head" which is a "struct tomoyo_acl_info".
  378. * (2) "is_deleted" is boolean.
  379. * (3) "dev_name" is the device name.
  380. * (4) "dir_name" is the mount point.
  381. * (5) "flags" is the mount flags.
  382. *
  383. * Directives held by this structure are "allow_rename", "allow_link" and
  384. * "allow_pivot_root".
  385. */
  386. struct tomoyo_mount_acl {
  387. struct tomoyo_acl_info head; /* type = TOMOYO_TYPE_MOUNT_ACL */
  388. bool is_deleted;
  389. struct tomoyo_name_union dev_name;
  390. struct tomoyo_name_union dir_name;
  391. struct tomoyo_name_union fs_type;
  392. struct tomoyo_number_union flags;
  393. };
  394. /*
  395. * tomoyo_io_buffer is a structure which is used for reading and modifying
  396. * configuration via /sys/kernel/security/tomoyo/ interface.
  397. * It has many fields. ->read_var1 , ->read_var2 , ->write_var1 are used as
  398. * cursors.
  399. *
  400. * Since the content of /sys/kernel/security/tomoyo/domain_policy is a list of
  401. * "struct tomoyo_domain_info" entries and each "struct tomoyo_domain_info"
  402. * entry has a list of "struct tomoyo_acl_info", we need two cursors when
  403. * reading (one is for traversing tomoyo_domain_list and the other is for
  404. * traversing "struct tomoyo_acl_info"->acl_info_list ).
  405. *
  406. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  407. * "select ", TOMOYO seeks the cursor ->read_var1 and ->write_var1 to the
  408. * domain with the domainname specified by the rest of that line (NULL is set
  409. * if seek failed).
  410. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  411. * "delete ", TOMOYO deletes an entry or a domain specified by the rest of that
  412. * line (->write_var1 is set to NULL if a domain was deleted).
  413. * If a line written to /sys/kernel/security/tomoyo/domain_policy starts with
  414. * neither "select " nor "delete ", an entry or a domain specified by that line
  415. * is appended.
  416. */
  417. struct tomoyo_io_buffer {
  418. int (*read) (struct tomoyo_io_buffer *);
  419. int (*write) (struct tomoyo_io_buffer *);
  420. int (*poll) (struct file *file, poll_table *wait);
  421. /* Exclusive lock for this structure. */
  422. struct mutex io_sem;
  423. /* Index returned by tomoyo_read_lock(). */
  424. int reader_idx;
  425. /* The position currently reading from. */
  426. struct list_head *read_var1;
  427. /* Extra variables for reading. */
  428. struct list_head *read_var2;
  429. /* The position currently writing to. */
  430. struct tomoyo_domain_info *write_var1;
  431. /* The step for reading. */
  432. int read_step;
  433. /* Buffer for reading. */
  434. char *read_buf;
  435. /* EOF flag for reading. */
  436. bool read_eof;
  437. /* Read domain ACL of specified PID? */
  438. bool read_single_domain;
  439. /* Extra variable for reading. */
  440. u8 read_bit;
  441. /* Bytes available for reading. */
  442. int read_avail;
  443. /* Size of read buffer. */
  444. int readbuf_size;
  445. /* Buffer for writing. */
  446. char *write_buf;
  447. /* Bytes available for writing. */
  448. int write_avail;
  449. /* Size of write buffer. */
  450. int writebuf_size;
  451. /* Type of this interface. */
  452. u8 type;
  453. };
  454. /*
  455. * tomoyo_globally_readable_file_entry is a structure which is used for holding
  456. * "allow_read" entries.
  457. * It has following fields.
  458. *
  459. * (1) "list" which is linked to tomoyo_globally_readable_list .
  460. * (2) "filename" is a pathname which is allowed to open(O_RDONLY).
  461. * (3) "is_deleted" is a bool which is true if marked as deleted, false
  462. * otherwise.
  463. */
  464. struct tomoyo_globally_readable_file_entry {
  465. struct list_head list;
  466. const struct tomoyo_path_info *filename;
  467. bool is_deleted;
  468. };
  469. /*
  470. * tomoyo_pattern_entry is a structure which is used for holding
  471. * "tomoyo_pattern_list" entries.
  472. * It has following fields.
  473. *
  474. * (1) "list" which is linked to tomoyo_pattern_list .
  475. * (2) "pattern" is a pathname pattern which is used for converting pathnames
  476. * to pathname patterns during learning mode.
  477. * (3) "is_deleted" is a bool which is true if marked as deleted, false
  478. * otherwise.
  479. */
  480. struct tomoyo_pattern_entry {
  481. struct list_head list;
  482. const struct tomoyo_path_info *pattern;
  483. bool is_deleted;
  484. };
  485. /*
  486. * tomoyo_no_rewrite_entry is a structure which is used for holding
  487. * "deny_rewrite" entries.
  488. * It has following fields.
  489. *
  490. * (1) "list" which is linked to tomoyo_no_rewrite_list .
  491. * (2) "pattern" is a pathname which is by default not permitted to modify
  492. * already existing content.
  493. * (3) "is_deleted" is a bool which is true if marked as deleted, false
  494. * otherwise.
  495. */
  496. struct tomoyo_no_rewrite_entry {
  497. struct list_head list;
  498. const struct tomoyo_path_info *pattern;
  499. bool is_deleted;
  500. };
  501. /*
  502. * tomoyo_domain_initializer_entry is a structure which is used for holding
  503. * "initialize_domain" and "no_initialize_domain" entries.
  504. * It has following fields.
  505. *
  506. * (1) "list" which is linked to tomoyo_domain_initializer_list .
  507. * (2) "domainname" which is "a domainname" or "the last component of a
  508. * domainname". This field is NULL if "from" clause is not specified.
  509. * (3) "program" which is a program's pathname.
  510. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  511. * otherwise.
  512. * (5) "is_not" is a bool which is true if "no_initialize_domain", false
  513. * otherwise.
  514. * (6) "is_last_name" is a bool which is true if "domainname" is "the last
  515. * component of a domainname", false otherwise.
  516. */
  517. struct tomoyo_domain_initializer_entry {
  518. struct list_head list;
  519. const struct tomoyo_path_info *domainname; /* This may be NULL */
  520. const struct tomoyo_path_info *program;
  521. bool is_deleted;
  522. bool is_not; /* True if this entry is "no_initialize_domain". */
  523. /* True if the domainname is tomoyo_get_last_name(). */
  524. bool is_last_name;
  525. };
  526. /*
  527. * tomoyo_domain_keeper_entry is a structure which is used for holding
  528. * "keep_domain" and "no_keep_domain" entries.
  529. * It has following fields.
  530. *
  531. * (1) "list" which is linked to tomoyo_domain_keeper_list .
  532. * (2) "domainname" which is "a domainname" or "the last component of a
  533. * domainname".
  534. * (3) "program" which is a program's pathname.
  535. * This field is NULL if "from" clause is not specified.
  536. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  537. * otherwise.
  538. * (5) "is_not" is a bool which is true if "no_initialize_domain", false
  539. * otherwise.
  540. * (6) "is_last_name" is a bool which is true if "domainname" is "the last
  541. * component of a domainname", false otherwise.
  542. */
  543. struct tomoyo_domain_keeper_entry {
  544. struct list_head list;
  545. const struct tomoyo_path_info *domainname;
  546. const struct tomoyo_path_info *program; /* This may be NULL */
  547. bool is_deleted;
  548. bool is_not; /* True if this entry is "no_keep_domain". */
  549. /* True if the domainname is tomoyo_get_last_name(). */
  550. bool is_last_name;
  551. };
  552. /*
  553. * tomoyo_alias_entry is a structure which is used for holding "alias" entries.
  554. * It has following fields.
  555. *
  556. * (1) "list" which is linked to tomoyo_alias_list .
  557. * (2) "original_name" which is a dereferenced pathname.
  558. * (3) "aliased_name" which is a symlink's pathname.
  559. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  560. * otherwise.
  561. */
  562. struct tomoyo_alias_entry {
  563. struct list_head list;
  564. const struct tomoyo_path_info *original_name;
  565. const struct tomoyo_path_info *aliased_name;
  566. bool is_deleted;
  567. };
  568. /*
  569. * tomoyo_policy_manager_entry is a structure which is used for holding list of
  570. * domainnames or programs which are permitted to modify configuration via
  571. * /sys/kernel/security/tomoyo/ interface.
  572. * It has following fields.
  573. *
  574. * (1) "list" which is linked to tomoyo_policy_manager_list .
  575. * (2) "manager" is a domainname or a program's pathname.
  576. * (3) "is_domain" is a bool which is true if "manager" is a domainname, false
  577. * otherwise.
  578. * (4) "is_deleted" is a bool which is true if marked as deleted, false
  579. * otherwise.
  580. */
  581. struct tomoyo_policy_manager_entry {
  582. struct list_head list;
  583. /* A path to program or a domainname. */
  584. const struct tomoyo_path_info *manager;
  585. bool is_domain; /* True if manager is a domainname. */
  586. bool is_deleted; /* True if this entry is deleted. */
  587. };
  588. /********** Function prototypes. **********/
  589. extern asmlinkage long sys_getpid(void);
  590. extern asmlinkage long sys_getppid(void);
  591. /* Check whether the given string starts with the given keyword. */
  592. bool tomoyo_str_starts(char **src, const char *find);
  593. /* Get tomoyo_realpath() of current process. */
  594. const char *tomoyo_get_exe(void);
  595. /* Format string. */
  596. void tomoyo_normalize_line(unsigned char *buffer);
  597. /* Print warning or error message on console. */
  598. void tomoyo_warn_log(struct tomoyo_request_info *r, const char *fmt, ...)
  599. __attribute__ ((format(printf, 2, 3)));
  600. /* Check all profiles currently assigned to domains are defined. */
  601. void tomoyo_check_profile(void);
  602. /* Open operation for /sys/kernel/security/tomoyo/ interface. */
  603. int tomoyo_open_control(const u8 type, struct file *file);
  604. /* Close /sys/kernel/security/tomoyo/ interface. */
  605. int tomoyo_close_control(struct file *file);
  606. /* Read operation for /sys/kernel/security/tomoyo/ interface. */
  607. int tomoyo_read_control(struct file *file, char __user *buffer,
  608. const int buffer_len);
  609. /* Write operation for /sys/kernel/security/tomoyo/ interface. */
  610. int tomoyo_write_control(struct file *file, const char __user *buffer,
  611. const int buffer_len);
  612. /* Check whether the domain has too many ACL entries to hold. */
  613. bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r);
  614. /* Print out of memory warning message. */
  615. void tomoyo_warn_oom(const char *function);
  616. /* Check whether the given name matches the given name_union. */
  617. bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  618. const struct tomoyo_name_union *ptr);
  619. /* Check whether the given number matches the given number_union. */
  620. bool tomoyo_compare_number_union(const unsigned long value,
  621. const struct tomoyo_number_union *ptr);
  622. /* Transactional sprintf() for policy dump. */
  623. bool tomoyo_io_printf(struct tomoyo_io_buffer *head, const char *fmt, ...)
  624. __attribute__ ((format(printf, 2, 3)));
  625. /* Check whether the domainname is correct. */
  626. bool tomoyo_is_correct_domain(const unsigned char *domainname);
  627. /* Check whether the token is correct. */
  628. bool tomoyo_is_correct_path(const char *filename);
  629. bool tomoyo_is_correct_word(const char *string);
  630. /* Check whether the token can be a domainname. */
  631. bool tomoyo_is_domain_def(const unsigned char *buffer);
  632. bool tomoyo_parse_name_union(const char *filename,
  633. struct tomoyo_name_union *ptr);
  634. /* Check whether the given filename matches the given path_group. */
  635. bool tomoyo_path_matches_group(const struct tomoyo_path_info *pathname,
  636. const struct tomoyo_path_group *group);
  637. /* Check whether the given value matches the given number_group. */
  638. bool tomoyo_number_matches_group(const unsigned long min,
  639. const unsigned long max,
  640. const struct tomoyo_number_group *group);
  641. /* Check whether the given filename matches the given pattern. */
  642. bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename,
  643. const struct tomoyo_path_info *pattern);
  644. bool tomoyo_print_number_union(struct tomoyo_io_buffer *head,
  645. const struct tomoyo_number_union *ptr);
  646. bool tomoyo_parse_number_union(char *data, struct tomoyo_number_union *num);
  647. /* Read "alias" entry in exception policy. */
  648. bool tomoyo_read_alias_policy(struct tomoyo_io_buffer *head);
  649. /*
  650. * Read "initialize_domain" and "no_initialize_domain" entry
  651. * in exception policy.
  652. */
  653. bool tomoyo_read_domain_initializer_policy(struct tomoyo_io_buffer *head);
  654. /* Read "keep_domain" and "no_keep_domain" entry in exception policy. */
  655. bool tomoyo_read_domain_keeper_policy(struct tomoyo_io_buffer *head);
  656. /* Read "file_pattern" entry in exception policy. */
  657. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head);
  658. /* Read "path_group" entry in exception policy. */
  659. bool tomoyo_read_path_group_policy(struct tomoyo_io_buffer *head);
  660. /* Read "number_group" entry in exception policy. */
  661. bool tomoyo_read_number_group_policy(struct tomoyo_io_buffer *head);
  662. /* Read "allow_read" entry in exception policy. */
  663. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head);
  664. /* Read "deny_rewrite" entry in exception policy. */
  665. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head);
  666. /* Tokenize a line. */
  667. bool tomoyo_tokenize(char *buffer, char *w[], size_t size);
  668. /* Write domain policy violation warning message to console? */
  669. bool tomoyo_verbose_mode(const struct tomoyo_domain_info *domain);
  670. /* Convert double path operation to operation name. */
  671. const char *tomoyo_path22keyword(const u8 operation);
  672. const char *tomoyo_path_number2keyword(const u8 operation);
  673. const char *tomoyo_path_number32keyword(const u8 operation);
  674. /* Get the last component of the given domainname. */
  675. const char *tomoyo_get_last_name(const struct tomoyo_domain_info *domain);
  676. /* Convert single path operation to operation name. */
  677. const char *tomoyo_path2keyword(const u8 operation);
  678. /* Fill "struct tomoyo_request_info". */
  679. int tomoyo_init_request_info(struct tomoyo_request_info *r,
  680. struct tomoyo_domain_info *domain);
  681. /* Check permission for mount operation. */
  682. int tomoyo_mount_permission(char *dev_name, struct path *path, char *type,
  683. unsigned long flags, void *data_page);
  684. /* Create "alias" entry in exception policy. */
  685. int tomoyo_write_alias_policy(char *data, const bool is_delete);
  686. /*
  687. * Create "initialize_domain" and "no_initialize_domain" entry
  688. * in exception policy.
  689. */
  690. int tomoyo_write_domain_initializer_policy(char *data, const bool is_not,
  691. const bool is_delete);
  692. /* Create "keep_domain" and "no_keep_domain" entry in exception policy. */
  693. int tomoyo_write_domain_keeper_policy(char *data, const bool is_not,
  694. const bool is_delete);
  695. /*
  696. * Create "allow_read/write", "allow_execute", "allow_read", "allow_write",
  697. * "allow_create", "allow_unlink", "allow_mkdir", "allow_rmdir",
  698. * "allow_mkfifo", "allow_mksock", "allow_mkblock", "allow_mkchar",
  699. * "allow_truncate", "allow_symlink", "allow_rewrite", "allow_rename" and
  700. * "allow_link" entry in domain policy.
  701. */
  702. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  703. const bool is_delete);
  704. /* Create "allow_read" entry in exception policy. */
  705. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete);
  706. /* Create "allow_mount" entry in domain policy. */
  707. int tomoyo_write_mount_policy(char *data, struct tomoyo_domain_info *domain,
  708. const bool is_delete);
  709. /* Create "deny_rewrite" entry in exception policy. */
  710. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete);
  711. /* Create "file_pattern" entry in exception policy. */
  712. int tomoyo_write_pattern_policy(char *data, const bool is_delete);
  713. /* Create "path_group" entry in exception policy. */
  714. int tomoyo_write_path_group_policy(char *data, const bool is_delete);
  715. int tomoyo_supervisor(struct tomoyo_request_info *r, const char *fmt, ...)
  716. __attribute__ ((format(printf, 2, 3)));
  717. /* Create "number_group" entry in exception policy. */
  718. int tomoyo_write_number_group_policy(char *data, const bool is_delete);
  719. /* Find a domain by the given name. */
  720. struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname);
  721. /* Find or create a domain by the given name. */
  722. struct tomoyo_domain_info *tomoyo_find_or_assign_new_domain(const char *
  723. domainname,
  724. const u8 profile);
  725. /* Allocate memory for "struct tomoyo_path_group". */
  726. struct tomoyo_path_group *tomoyo_get_path_group(const char *group_name);
  727. struct tomoyo_number_group *tomoyo_get_number_group(const char *group_name);
  728. /* Check mode for specified functionality. */
  729. unsigned int tomoyo_check_flags(const struct tomoyo_domain_info *domain,
  730. const u8 index);
  731. /* Fill in "struct tomoyo_path_info" members. */
  732. void tomoyo_fill_path_info(struct tomoyo_path_info *ptr);
  733. /* Run policy loader when /sbin/init starts. */
  734. void tomoyo_load_policy(const char *filename);
  735. void tomoyo_put_number_union(struct tomoyo_number_union *ptr);
  736. /* Convert binary string to ascii string. */
  737. char *tomoyo_encode(const char *str);
  738. /*
  739. * Returns realpath(3) of the given pathname but ignores chroot'ed root.
  740. * These functions use kzalloc(), so the caller must call kfree()
  741. * if these functions didn't return NULL.
  742. */
  743. char *tomoyo_realpath(const char *pathname);
  744. /*
  745. * Same with tomoyo_realpath() except that it doesn't follow the final symlink.
  746. */
  747. char *tomoyo_realpath_nofollow(const char *pathname);
  748. /* Same with tomoyo_realpath() except that the pathname is already solved. */
  749. char *tomoyo_realpath_from_path(struct path *path);
  750. /* Get patterned pathname. */
  751. const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename);
  752. /* Check memory quota. */
  753. bool tomoyo_memory_ok(void *ptr);
  754. void *tomoyo_commit_ok(void *data, const unsigned int size);
  755. /*
  756. * Keep the given name on the RAM.
  757. * The RAM is shared, so NEVER try to modify or kfree() the returned name.
  758. */
  759. const struct tomoyo_path_info *tomoyo_get_name(const char *name);
  760. /* Check for memory usage. */
  761. int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head);
  762. /* Set memory quota. */
  763. int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head);
  764. /* Initialize mm related code. */
  765. void __init tomoyo_mm_init(void);
  766. int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
  767. const struct tomoyo_path_info *filename);
  768. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  769. struct path *path, const int flag);
  770. int tomoyo_path_number_perm(const u8 operation, struct path *path,
  771. unsigned long number);
  772. int tomoyo_path_number3_perm(const u8 operation, struct path *path,
  773. const unsigned int mode, unsigned int dev);
  774. int tomoyo_path_perm(const u8 operation, struct path *path);
  775. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  776. struct path *path2);
  777. int tomoyo_find_next_domain(struct linux_binprm *bprm);
  778. void tomoyo_print_ulong(char *buffer, const int buffer_len,
  779. const unsigned long value, const u8 type);
  780. /* Drop refcount on tomoyo_name_union. */
  781. void tomoyo_put_name_union(struct tomoyo_name_union *ptr);
  782. /* Run garbage collector. */
  783. void tomoyo_run_gc(void);
  784. void tomoyo_memory_free(void *ptr);
  785. /********** External variable definitions. **********/
  786. /* Lock for GC. */
  787. extern struct srcu_struct tomoyo_ss;
  788. /* The list for "struct tomoyo_domain_info". */
  789. extern struct list_head tomoyo_domain_list;
  790. extern struct list_head tomoyo_path_group_list;
  791. extern struct list_head tomoyo_number_group_list;
  792. extern struct list_head tomoyo_domain_initializer_list;
  793. extern struct list_head tomoyo_domain_keeper_list;
  794. extern struct list_head tomoyo_alias_list;
  795. extern struct list_head tomoyo_globally_readable_list;
  796. extern struct list_head tomoyo_pattern_list;
  797. extern struct list_head tomoyo_no_rewrite_list;
  798. extern struct list_head tomoyo_policy_manager_list;
  799. extern struct list_head tomoyo_name_list[TOMOYO_MAX_HASH];
  800. /* Lock for protecting policy. */
  801. extern struct mutex tomoyo_policy_lock;
  802. /* Has /sbin/init started? */
  803. extern bool tomoyo_policy_loaded;
  804. /* The kernel's domain. */
  805. extern struct tomoyo_domain_info tomoyo_kernel_domain;
  806. extern unsigned int tomoyo_quota_for_query;
  807. extern unsigned int tomoyo_query_memory_size;
  808. /********** Inlined functions. **********/
  809. static inline int tomoyo_read_lock(void)
  810. {
  811. return srcu_read_lock(&tomoyo_ss);
  812. }
  813. static inline void tomoyo_read_unlock(int idx)
  814. {
  815. srcu_read_unlock(&tomoyo_ss, idx);
  816. }
  817. /* strcmp() for "struct tomoyo_path_info" structure. */
  818. static inline bool tomoyo_pathcmp(const struct tomoyo_path_info *a,
  819. const struct tomoyo_path_info *b)
  820. {
  821. return a->hash != b->hash || strcmp(a->name, b->name);
  822. }
  823. /**
  824. * tomoyo_is_valid - Check whether the character is a valid char.
  825. *
  826. * @c: The character to check.
  827. *
  828. * Returns true if @c is a valid character, false otherwise.
  829. */
  830. static inline bool tomoyo_is_valid(const unsigned char c)
  831. {
  832. return c > ' ' && c < 127;
  833. }
  834. /**
  835. * tomoyo_is_invalid - Check whether the character is an invalid char.
  836. *
  837. * @c: The character to check.
  838. *
  839. * Returns true if @c is an invalid character, false otherwise.
  840. */
  841. static inline bool tomoyo_is_invalid(const unsigned char c)
  842. {
  843. return c && (c <= ' ' || c >= 127);
  844. }
  845. static inline void tomoyo_put_name(const struct tomoyo_path_info *name)
  846. {
  847. if (name) {
  848. struct tomoyo_name_entry *ptr =
  849. container_of(name, struct tomoyo_name_entry, entry);
  850. atomic_dec(&ptr->users);
  851. }
  852. }
  853. static inline void tomoyo_put_path_group(struct tomoyo_path_group *group)
  854. {
  855. if (group)
  856. atomic_dec(&group->users);
  857. }
  858. static inline void tomoyo_put_number_group(struct tomoyo_number_group *group)
  859. {
  860. if (group)
  861. atomic_dec(&group->users);
  862. }
  863. static inline struct tomoyo_domain_info *tomoyo_domain(void)
  864. {
  865. return current_cred()->security;
  866. }
  867. static inline struct tomoyo_domain_info *tomoyo_real_domain(struct task_struct
  868. *task)
  869. {
  870. return task_cred_xxx(task, security);
  871. }
  872. static inline bool tomoyo_is_same_acl_head(const struct tomoyo_acl_info *p1,
  873. const struct tomoyo_acl_info *p2)
  874. {
  875. return p1->type == p2->type;
  876. }
  877. static inline bool tomoyo_is_same_name_union
  878. (const struct tomoyo_name_union *p1, const struct tomoyo_name_union *p2)
  879. {
  880. return p1->filename == p2->filename && p1->group == p2->group &&
  881. p1->is_group == p2->is_group;
  882. }
  883. static inline bool tomoyo_is_same_number_union
  884. (const struct tomoyo_number_union *p1, const struct tomoyo_number_union *p2)
  885. {
  886. return p1->values[0] == p2->values[0] && p1->values[1] == p2->values[1]
  887. && p1->group == p2->group && p1->min_type == p2->min_type &&
  888. p1->max_type == p2->max_type && p1->is_group == p2->is_group;
  889. }
  890. static inline bool tomoyo_is_same_path_acl(const struct tomoyo_path_acl *p1,
  891. const struct tomoyo_path_acl *p2)
  892. {
  893. return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
  894. tomoyo_is_same_name_union(&p1->name, &p2->name);
  895. }
  896. static inline bool tomoyo_is_same_path_number3_acl
  897. (const struct tomoyo_path_number3_acl *p1,
  898. const struct tomoyo_path_number3_acl *p2)
  899. {
  900. return tomoyo_is_same_acl_head(&p1->head, &p2->head)
  901. && tomoyo_is_same_name_union(&p1->name, &p2->name)
  902. && tomoyo_is_same_number_union(&p1->mode, &p2->mode)
  903. && tomoyo_is_same_number_union(&p1->major, &p2->major)
  904. && tomoyo_is_same_number_union(&p1->minor, &p2->minor);
  905. }
  906. static inline bool tomoyo_is_same_path2_acl(const struct tomoyo_path2_acl *p1,
  907. const struct tomoyo_path2_acl *p2)
  908. {
  909. return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
  910. tomoyo_is_same_name_union(&p1->name1, &p2->name1) &&
  911. tomoyo_is_same_name_union(&p1->name2, &p2->name2);
  912. }
  913. static inline bool tomoyo_is_same_path_number_acl
  914. (const struct tomoyo_path_number_acl *p1,
  915. const struct tomoyo_path_number_acl *p2)
  916. {
  917. return tomoyo_is_same_acl_head(&p1->head, &p2->head)
  918. && tomoyo_is_same_name_union(&p1->name, &p2->name)
  919. && tomoyo_is_same_number_union(&p1->number, &p2->number);
  920. }
  921. static inline bool tomoyo_is_same_mount_acl(const struct tomoyo_mount_acl *p1,
  922. const struct tomoyo_mount_acl *p2)
  923. {
  924. return tomoyo_is_same_acl_head(&p1->head, &p2->head) &&
  925. tomoyo_is_same_name_union(&p1->dev_name, &p2->dev_name) &&
  926. tomoyo_is_same_name_union(&p1->dir_name, &p2->dir_name) &&
  927. tomoyo_is_same_name_union(&p1->fs_type, &p2->fs_type) &&
  928. tomoyo_is_same_number_union(&p1->flags, &p2->flags);
  929. }
  930. static inline bool tomoyo_is_same_domain_initializer_entry
  931. (const struct tomoyo_domain_initializer_entry *p1,
  932. const struct tomoyo_domain_initializer_entry *p2)
  933. {
  934. return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
  935. && p1->domainname == p2->domainname
  936. && p1->program == p2->program;
  937. }
  938. static inline bool tomoyo_is_same_domain_keeper_entry
  939. (const struct tomoyo_domain_keeper_entry *p1,
  940. const struct tomoyo_domain_keeper_entry *p2)
  941. {
  942. return p1->is_not == p2->is_not && p1->is_last_name == p2->is_last_name
  943. && p1->domainname == p2->domainname
  944. && p1->program == p2->program;
  945. }
  946. static inline bool tomoyo_is_same_alias_entry
  947. (const struct tomoyo_alias_entry *p1, const struct tomoyo_alias_entry *p2)
  948. {
  949. return p1->original_name == p2->original_name &&
  950. p1->aliased_name == p2->aliased_name;
  951. }
  952. /**
  953. * list_for_each_cookie - iterate over a list with cookie.
  954. * @pos: the &struct list_head to use as a loop cursor.
  955. * @cookie: the &struct list_head to use as a cookie.
  956. * @head: the head for your list.
  957. *
  958. * Same with list_for_each_rcu() except that this primitive uses @cookie
  959. * so that we can continue iteration.
  960. * @cookie must be NULL when iteration starts, and @cookie will become
  961. * NULL when iteration finishes.
  962. */
  963. #define list_for_each_cookie(pos, cookie, head) \
  964. for (({ if (!cookie) \
  965. cookie = head; }), \
  966. pos = rcu_dereference((cookie)->next); \
  967. prefetch(pos->next), pos != (head) || ((cookie) = NULL); \
  968. (cookie) = pos, pos = rcu_dereference(pos->next))
  969. #endif /* !defined(_SECURITY_TOMOYO_COMMON_H) */