file.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /*
  2. * security/tomoyo/file.c
  3. *
  4. * Pathname restriction functions.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/slab.h>
  10. /* Keyword array for operations with one pathname. */
  11. const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
  12. [TOMOYO_TYPE_READ_WRITE] = "read/write",
  13. [TOMOYO_TYPE_EXECUTE] = "execute",
  14. [TOMOYO_TYPE_READ] = "read",
  15. [TOMOYO_TYPE_WRITE] = "write",
  16. [TOMOYO_TYPE_UNLINK] = "unlink",
  17. [TOMOYO_TYPE_RMDIR] = "rmdir",
  18. [TOMOYO_TYPE_TRUNCATE] = "truncate",
  19. [TOMOYO_TYPE_SYMLINK] = "symlink",
  20. [TOMOYO_TYPE_REWRITE] = "rewrite",
  21. [TOMOYO_TYPE_CHROOT] = "chroot",
  22. [TOMOYO_TYPE_UMOUNT] = "unmount",
  23. };
  24. /* Keyword array for operations with one pathname and three numbers. */
  25. const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
  26. [TOMOYO_TYPE_MKBLOCK] = "mkblock",
  27. [TOMOYO_TYPE_MKCHAR] = "mkchar",
  28. };
  29. /* Keyword array for operations with two pathnames. */
  30. const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
  31. [TOMOYO_TYPE_LINK] = "link",
  32. [TOMOYO_TYPE_RENAME] = "rename",
  33. [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
  34. };
  35. /* Keyword array for operations with one pathname and one number. */
  36. const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  37. [TOMOYO_TYPE_CREATE] = "create",
  38. [TOMOYO_TYPE_MKDIR] = "mkdir",
  39. [TOMOYO_TYPE_MKFIFO] = "mkfifo",
  40. [TOMOYO_TYPE_MKSOCK] = "mksock",
  41. [TOMOYO_TYPE_IOCTL] = "ioctl",
  42. [TOMOYO_TYPE_CHMOD] = "chmod",
  43. [TOMOYO_TYPE_CHOWN] = "chown",
  44. [TOMOYO_TYPE_CHGRP] = "chgrp",
  45. };
  46. static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
  47. [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN,
  48. [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
  49. [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
  50. [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
  51. [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
  52. [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
  53. [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
  54. [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
  55. [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE,
  56. [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
  57. [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
  58. };
  59. static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
  60. [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
  61. [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
  62. };
  63. static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
  64. [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
  65. [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
  66. [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
  67. };
  68. static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  69. [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
  70. [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
  71. [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
  72. [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
  73. [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
  74. [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
  75. [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
  76. [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
  77. };
  78. void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
  79. {
  80. if (!ptr)
  81. return;
  82. if (ptr->is_group)
  83. tomoyo_put_path_group(ptr->group);
  84. else
  85. tomoyo_put_name(ptr->filename);
  86. }
  87. bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  88. const struct tomoyo_name_union *ptr)
  89. {
  90. if (ptr->is_group)
  91. return tomoyo_path_matches_group(name, ptr->group);
  92. return tomoyo_path_matches_pattern(name, ptr->filename);
  93. }
  94. void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
  95. {
  96. if (ptr && ptr->is_group)
  97. tomoyo_put_number_group(ptr->group);
  98. }
  99. bool tomoyo_compare_number_union(const unsigned long value,
  100. const struct tomoyo_number_union *ptr)
  101. {
  102. if (ptr->is_group)
  103. return tomoyo_number_matches_group(value, value, ptr->group);
  104. return value >= ptr->values[0] && value <= ptr->values[1];
  105. }
  106. static void tomoyo_add_slash(struct tomoyo_path_info *buf)
  107. {
  108. if (buf->is_dir)
  109. return;
  110. /*
  111. * This is OK because tomoyo_encode() reserves space for appending "/".
  112. */
  113. strcat((char *) buf->name, "/");
  114. tomoyo_fill_path_info(buf);
  115. }
  116. /**
  117. * tomoyo_strendswith - Check whether the token ends with the given token.
  118. *
  119. * @name: The token to check.
  120. * @tail: The token to find.
  121. *
  122. * Returns true if @name ends with @tail, false otherwise.
  123. */
  124. static bool tomoyo_strendswith(const char *name, const char *tail)
  125. {
  126. int len;
  127. if (!name || !tail)
  128. return false;
  129. len = strlen(name) - strlen(tail);
  130. return len >= 0 && !strcmp(name + len, tail);
  131. }
  132. /**
  133. * tomoyo_get_realpath - Get realpath.
  134. *
  135. * @buf: Pointer to "struct tomoyo_path_info".
  136. * @path: Pointer to "struct path".
  137. *
  138. * Returns true on success, false otherwise.
  139. */
  140. static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
  141. {
  142. buf->name = tomoyo_realpath_from_path(path);
  143. if (buf->name) {
  144. tomoyo_fill_path_info(buf);
  145. return true;
  146. }
  147. return false;
  148. }
  149. /**
  150. * tomoyo_audit_path_log - Audit path request log.
  151. *
  152. * @r: Pointer to "struct tomoyo_request_info".
  153. *
  154. * Returns 0 on success, negative value otherwise.
  155. */
  156. static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
  157. {
  158. const char *operation = tomoyo_path_keyword[r->param.path.operation];
  159. const struct tomoyo_path_info *filename = r->param.path.filename;
  160. if (r->granted)
  161. return 0;
  162. tomoyo_warn_log(r, "%s %s", operation, filename->name);
  163. return tomoyo_supervisor(r, "allow_%s %s\n", operation,
  164. tomoyo_file_pattern(filename));
  165. }
  166. /**
  167. * tomoyo_audit_path2_log - Audit path/path request log.
  168. *
  169. * @r: Pointer to "struct tomoyo_request_info".
  170. *
  171. * Returns 0 on success, negative value otherwise.
  172. */
  173. static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
  174. {
  175. const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
  176. const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
  177. const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
  178. if (r->granted)
  179. return 0;
  180. tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
  181. filename2->name);
  182. return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
  183. tomoyo_file_pattern(filename1),
  184. tomoyo_file_pattern(filename2));
  185. }
  186. /**
  187. * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
  188. *
  189. * @r: Pointer to "struct tomoyo_request_info".
  190. *
  191. * Returns 0 on success, negative value otherwise.
  192. */
  193. static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
  194. {
  195. const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
  196. const struct tomoyo_path_info *filename = r->param.mkdev.filename;
  197. const unsigned int major = r->param.mkdev.major;
  198. const unsigned int minor = r->param.mkdev.minor;
  199. const unsigned int mode = r->param.mkdev.mode;
  200. if (r->granted)
  201. return 0;
  202. tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
  203. major, minor);
  204. return tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", operation,
  205. tomoyo_file_pattern(filename), mode, major,
  206. minor);
  207. }
  208. /**
  209. * tomoyo_audit_path_number_log - Audit path/number request log.
  210. *
  211. * @r: Pointer to "struct tomoyo_request_info".
  212. * @error: Error code.
  213. *
  214. * Returns 0 on success, negative value otherwise.
  215. */
  216. static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
  217. {
  218. const u8 type = r->param.path_number.operation;
  219. u8 radix;
  220. const struct tomoyo_path_info *filename = r->param.path_number.filename;
  221. const char *operation = tomoyo_path_number_keyword[type];
  222. char buffer[64];
  223. if (r->granted)
  224. return 0;
  225. switch (type) {
  226. case TOMOYO_TYPE_CREATE:
  227. case TOMOYO_TYPE_MKDIR:
  228. case TOMOYO_TYPE_MKFIFO:
  229. case TOMOYO_TYPE_MKSOCK:
  230. case TOMOYO_TYPE_CHMOD:
  231. radix = TOMOYO_VALUE_TYPE_OCTAL;
  232. break;
  233. case TOMOYO_TYPE_IOCTL:
  234. radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
  235. break;
  236. default:
  237. radix = TOMOYO_VALUE_TYPE_DECIMAL;
  238. break;
  239. }
  240. tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
  241. radix);
  242. tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
  243. return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
  244. tomoyo_file_pattern(filename), buffer);
  245. }
  246. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  247. const char *filename2,
  248. struct tomoyo_domain_info *const domain,
  249. const bool is_delete);
  250. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  251. struct tomoyo_domain_info *const domain,
  252. const bool is_delete);
  253. /*
  254. * tomoyo_globally_readable_list is used for holding list of pathnames which
  255. * are by default allowed to be open()ed for reading by any process.
  256. *
  257. * An entry is added by
  258. *
  259. * # echo 'allow_read /lib/libc-2.5.so' > \
  260. * /sys/kernel/security/tomoyo/exception_policy
  261. *
  262. * and is deleted by
  263. *
  264. * # echo 'delete allow_read /lib/libc-2.5.so' > \
  265. * /sys/kernel/security/tomoyo/exception_policy
  266. *
  267. * and all entries are retrieved by
  268. *
  269. * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
  270. *
  271. * In the example above, any process is allowed to
  272. * open("/lib/libc-2.5.so", O_RDONLY).
  273. * One exception is, if the domain which current process belongs to is marked
  274. * as "ignore_global_allow_read", current process can't do so unless explicitly
  275. * given "allow_read /lib/libc-2.5.so" to the domain which current process
  276. * belongs to.
  277. */
  278. LIST_HEAD(tomoyo_globally_readable_list);
  279. static bool tomoyo_same_globally_readable(const struct tomoyo_acl_head *a,
  280. const struct tomoyo_acl_head *b)
  281. {
  282. return container_of(a, struct tomoyo_globally_readable_file_entry,
  283. head)->filename ==
  284. container_of(b, struct tomoyo_globally_readable_file_entry,
  285. head)->filename;
  286. }
  287. /**
  288. * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
  289. *
  290. * @filename: Filename unconditionally permitted to open() for reading.
  291. * @is_delete: True if it is a delete request.
  292. *
  293. * Returns 0 on success, negative value otherwise.
  294. *
  295. * Caller holds tomoyo_read_lock().
  296. */
  297. static int tomoyo_update_globally_readable_entry(const char *filename,
  298. const bool is_delete)
  299. {
  300. struct tomoyo_globally_readable_file_entry e = { };
  301. int error;
  302. if (!tomoyo_correct_word(filename))
  303. return -EINVAL;
  304. e.filename = tomoyo_get_name(filename);
  305. if (!e.filename)
  306. return -ENOMEM;
  307. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  308. &tomoyo_globally_readable_list,
  309. tomoyo_same_globally_readable);
  310. tomoyo_put_name(e.filename);
  311. return error;
  312. }
  313. /**
  314. * tomoyo_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
  315. *
  316. * @filename: The filename to check.
  317. *
  318. * Returns true if any domain can open @filename for reading, false otherwise.
  319. *
  320. * Caller holds tomoyo_read_lock().
  321. */
  322. static bool tomoyo_globally_readable_file(const struct tomoyo_path_info *
  323. filename)
  324. {
  325. struct tomoyo_globally_readable_file_entry *ptr;
  326. bool found = false;
  327. list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list,
  328. head.list) {
  329. if (!ptr->head.is_deleted &&
  330. tomoyo_path_matches_pattern(filename, ptr->filename)) {
  331. found = true;
  332. break;
  333. }
  334. }
  335. return found;
  336. }
  337. /**
  338. * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
  339. *
  340. * @data: String to parse.
  341. * @is_delete: True if it is a delete request.
  342. *
  343. * Returns 0 on success, negative value otherwise.
  344. *
  345. * Caller holds tomoyo_read_lock().
  346. */
  347. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
  348. {
  349. return tomoyo_update_globally_readable_entry(data, is_delete);
  350. }
  351. /**
  352. * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
  353. *
  354. * @head: Pointer to "struct tomoyo_io_buffer".
  355. *
  356. * Returns true on success, false otherwise.
  357. *
  358. * Caller holds tomoyo_read_lock().
  359. */
  360. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
  361. {
  362. struct list_head *pos;
  363. bool done = true;
  364. list_for_each_cookie(pos, head->read_var2,
  365. &tomoyo_globally_readable_list) {
  366. struct tomoyo_globally_readable_file_entry *ptr;
  367. ptr = list_entry(pos,
  368. struct tomoyo_globally_readable_file_entry,
  369. head.list);
  370. if (ptr->head.is_deleted)
  371. continue;
  372. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
  373. ptr->filename->name);
  374. if (!done)
  375. break;
  376. }
  377. return done;
  378. }
  379. /* tomoyo_pattern_list is used for holding list of pathnames which are used for
  380. * converting pathnames to pathname patterns during learning mode.
  381. *
  382. * An entry is added by
  383. *
  384. * # echo 'file_pattern /proc/\$/mounts' > \
  385. * /sys/kernel/security/tomoyo/exception_policy
  386. *
  387. * and is deleted by
  388. *
  389. * # echo 'delete file_pattern /proc/\$/mounts' > \
  390. * /sys/kernel/security/tomoyo/exception_policy
  391. *
  392. * and all entries are retrieved by
  393. *
  394. * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
  395. *
  396. * In the example above, if a process which belongs to a domain which is in
  397. * learning mode requested open("/proc/1/mounts", O_RDONLY),
  398. * "allow_read /proc/\$/mounts" is automatically added to the domain which that
  399. * process belongs to.
  400. *
  401. * It is not a desirable behavior that we have to use /proc/\$/ instead of
  402. * /proc/self/ when current process needs to access only current process's
  403. * information. As of now, LSM version of TOMOYO is using __d_path() for
  404. * calculating pathname. Non LSM version of TOMOYO is using its own function
  405. * which pretends as if /proc/self/ is not a symlink; so that we can forbid
  406. * current process from accessing other process's information.
  407. */
  408. LIST_HEAD(tomoyo_pattern_list);
  409. static bool tomoyo_same_pattern(const struct tomoyo_acl_head *a,
  410. const struct tomoyo_acl_head *b)
  411. {
  412. return container_of(a, struct tomoyo_pattern_entry, head)->pattern ==
  413. container_of(b, struct tomoyo_pattern_entry, head)->pattern;
  414. }
  415. /**
  416. * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
  417. *
  418. * @pattern: Pathname pattern.
  419. * @is_delete: True if it is a delete request.
  420. *
  421. * Returns 0 on success, negative value otherwise.
  422. *
  423. * Caller holds tomoyo_read_lock().
  424. */
  425. static int tomoyo_update_file_pattern_entry(const char *pattern,
  426. const bool is_delete)
  427. {
  428. struct tomoyo_pattern_entry e = { };
  429. int error;
  430. if (!tomoyo_correct_word(pattern))
  431. return -EINVAL;
  432. e.pattern = tomoyo_get_name(pattern);
  433. if (!e.pattern)
  434. return -ENOMEM;
  435. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  436. &tomoyo_pattern_list,
  437. tomoyo_same_pattern);
  438. tomoyo_put_name(e.pattern);
  439. return error;
  440. }
  441. /**
  442. * tomoyo_file_pattern - Get patterned pathname.
  443. *
  444. * @filename: The filename to find patterned pathname.
  445. *
  446. * Returns pointer to pathname pattern if matched, @filename otherwise.
  447. *
  448. * Caller holds tomoyo_read_lock().
  449. */
  450. const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
  451. {
  452. struct tomoyo_pattern_entry *ptr;
  453. const struct tomoyo_path_info *pattern = NULL;
  454. list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, head.list) {
  455. if (ptr->head.is_deleted)
  456. continue;
  457. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  458. continue;
  459. pattern = ptr->pattern;
  460. if (tomoyo_strendswith(pattern->name, "/\\*")) {
  461. /* Do nothing. Try to find the better match. */
  462. } else {
  463. /* This would be the better match. Use this. */
  464. break;
  465. }
  466. }
  467. if (pattern)
  468. filename = pattern;
  469. return filename->name;
  470. }
  471. /**
  472. * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
  473. *
  474. * @data: String to parse.
  475. * @is_delete: True if it is a delete request.
  476. *
  477. * Returns 0 on success, negative value otherwise.
  478. *
  479. * Caller holds tomoyo_read_lock().
  480. */
  481. int tomoyo_write_pattern_policy(char *data, const bool is_delete)
  482. {
  483. return tomoyo_update_file_pattern_entry(data, is_delete);
  484. }
  485. /**
  486. * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
  487. *
  488. * @head: Pointer to "struct tomoyo_io_buffer".
  489. *
  490. * Returns true on success, false otherwise.
  491. *
  492. * Caller holds tomoyo_read_lock().
  493. */
  494. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
  495. {
  496. struct list_head *pos;
  497. bool done = true;
  498. list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
  499. struct tomoyo_pattern_entry *ptr;
  500. ptr = list_entry(pos, struct tomoyo_pattern_entry, head.list);
  501. if (ptr->head.is_deleted)
  502. continue;
  503. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
  504. "%s\n", ptr->pattern->name);
  505. if (!done)
  506. break;
  507. }
  508. return done;
  509. }
  510. /*
  511. * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
  512. * default forbidden to modify already written content of a file.
  513. *
  514. * An entry is added by
  515. *
  516. * # echo 'deny_rewrite /var/log/messages' > \
  517. * /sys/kernel/security/tomoyo/exception_policy
  518. *
  519. * and is deleted by
  520. *
  521. * # echo 'delete deny_rewrite /var/log/messages' > \
  522. * /sys/kernel/security/tomoyo/exception_policy
  523. *
  524. * and all entries are retrieved by
  525. *
  526. * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
  527. *
  528. * In the example above, if a process requested to rewrite /var/log/messages ,
  529. * the process can't rewrite unless the domain which that process belongs to
  530. * has "allow_rewrite /var/log/messages" entry.
  531. *
  532. * It is not a desirable behavior that we have to add "\040(deleted)" suffix
  533. * when we want to allow rewriting already unlink()ed file. As of now,
  534. * LSM version of TOMOYO is using __d_path() for calculating pathname.
  535. * Non LSM version of TOMOYO is using its own function which doesn't append
  536. * " (deleted)" suffix if the file is already unlink()ed; so that we don't
  537. * need to worry whether the file is already unlink()ed or not.
  538. */
  539. LIST_HEAD(tomoyo_no_rewrite_list);
  540. static bool tomoyo_same_no_rewrite(const struct tomoyo_acl_head *a,
  541. const struct tomoyo_acl_head *b)
  542. {
  543. return container_of(a, struct tomoyo_no_rewrite_entry, head)->pattern
  544. == container_of(b, struct tomoyo_no_rewrite_entry, head)
  545. ->pattern;
  546. }
  547. /**
  548. * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
  549. *
  550. * @pattern: Pathname pattern that are not rewritable by default.
  551. * @is_delete: True if it is a delete request.
  552. *
  553. * Returns 0 on success, negative value otherwise.
  554. *
  555. * Caller holds tomoyo_read_lock().
  556. */
  557. static int tomoyo_update_no_rewrite_entry(const char *pattern,
  558. const bool is_delete)
  559. {
  560. struct tomoyo_no_rewrite_entry e = { };
  561. int error;
  562. if (!tomoyo_correct_word(pattern))
  563. return -EINVAL;
  564. e.pattern = tomoyo_get_name(pattern);
  565. if (!e.pattern)
  566. return -ENOMEM;
  567. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  568. &tomoyo_no_rewrite_list,
  569. tomoyo_same_no_rewrite);
  570. tomoyo_put_name(e.pattern);
  571. return error;
  572. }
  573. /**
  574. * tomoyo_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
  575. *
  576. * @filename: Filename to check.
  577. *
  578. * Returns true if @filename is specified by "deny_rewrite" directive,
  579. * false otherwise.
  580. *
  581. * Caller holds tomoyo_read_lock().
  582. */
  583. static bool tomoyo_no_rewrite_file(const struct tomoyo_path_info *filename)
  584. {
  585. struct tomoyo_no_rewrite_entry *ptr;
  586. bool found = false;
  587. list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, head.list) {
  588. if (ptr->head.is_deleted)
  589. continue;
  590. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  591. continue;
  592. found = true;
  593. break;
  594. }
  595. return found;
  596. }
  597. /**
  598. * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
  599. *
  600. * @data: String to parse.
  601. * @is_delete: True if it is a delete request.
  602. *
  603. * Returns 0 on success, negative value otherwise.
  604. *
  605. * Caller holds tomoyo_read_lock().
  606. */
  607. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
  608. {
  609. return tomoyo_update_no_rewrite_entry(data, is_delete);
  610. }
  611. /**
  612. * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
  613. *
  614. * @head: Pointer to "struct tomoyo_io_buffer".
  615. *
  616. * Returns true on success, false otherwise.
  617. *
  618. * Caller holds tomoyo_read_lock().
  619. */
  620. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
  621. {
  622. struct list_head *pos;
  623. bool done = true;
  624. list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
  625. struct tomoyo_no_rewrite_entry *ptr;
  626. ptr = list_entry(pos, struct tomoyo_no_rewrite_entry,
  627. head.list);
  628. if (ptr->head.is_deleted)
  629. continue;
  630. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
  631. "%s\n", ptr->pattern->name);
  632. if (!done)
  633. break;
  634. }
  635. return done;
  636. }
  637. static bool tomoyo_check_path_acl(const struct tomoyo_request_info *r,
  638. const struct tomoyo_acl_info *ptr)
  639. {
  640. const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
  641. head);
  642. return (acl->perm & (1 << r->param.path.operation)) &&
  643. tomoyo_compare_name_union(r->param.path.filename, &acl->name);
  644. }
  645. static bool tomoyo_check_path_number_acl(const struct tomoyo_request_info *r,
  646. const struct tomoyo_acl_info *ptr)
  647. {
  648. const struct tomoyo_path_number_acl *acl =
  649. container_of(ptr, typeof(*acl), head);
  650. return (acl->perm & (1 << r->param.path_number.operation)) &&
  651. tomoyo_compare_number_union(r->param.path_number.number,
  652. &acl->number) &&
  653. tomoyo_compare_name_union(r->param.path_number.filename,
  654. &acl->name);
  655. }
  656. static bool tomoyo_check_path2_acl(const struct tomoyo_request_info *r,
  657. const struct tomoyo_acl_info *ptr)
  658. {
  659. const struct tomoyo_path2_acl *acl =
  660. container_of(ptr, typeof(*acl), head);
  661. return (acl->perm & (1 << r->param.path2.operation)) &&
  662. tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
  663. && tomoyo_compare_name_union(r->param.path2.filename2,
  664. &acl->name2);
  665. }
  666. static bool tomoyo_check_mkdev_acl(const struct tomoyo_request_info *r,
  667. const struct tomoyo_acl_info *ptr)
  668. {
  669. const struct tomoyo_mkdev_acl *acl =
  670. container_of(ptr, typeof(*acl), head);
  671. return (acl->perm & (1 << r->param.mkdev.operation)) &&
  672. tomoyo_compare_number_union(r->param.mkdev.mode,
  673. &acl->mode) &&
  674. tomoyo_compare_number_union(r->param.mkdev.major,
  675. &acl->major) &&
  676. tomoyo_compare_number_union(r->param.mkdev.minor,
  677. &acl->minor) &&
  678. tomoyo_compare_name_union(r->param.mkdev.filename,
  679. &acl->name);
  680. }
  681. static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
  682. const struct tomoyo_acl_info *b)
  683. {
  684. const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
  685. const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
  686. return tomoyo_same_acl_head(&p1->head, &p2->head) &&
  687. tomoyo_same_name_union(&p1->name, &p2->name);
  688. }
  689. static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
  690. struct tomoyo_acl_info *b,
  691. const bool is_delete)
  692. {
  693. u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
  694. ->perm;
  695. u16 perm = *a_perm;
  696. const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
  697. if (is_delete) {
  698. perm &= ~b_perm;
  699. if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
  700. perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
  701. else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  702. perm &= ~TOMOYO_RW_MASK;
  703. } else {
  704. perm |= b_perm;
  705. if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
  706. perm |= (1 << TOMOYO_TYPE_READ_WRITE);
  707. else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
  708. perm |= TOMOYO_RW_MASK;
  709. }
  710. *a_perm = perm;
  711. return !perm;
  712. }
  713. /**
  714. * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
  715. *
  716. * @type: Type of operation.
  717. * @filename: Filename.
  718. * @domain: Pointer to "struct tomoyo_domain_info".
  719. * @is_delete: True if it is a delete request.
  720. *
  721. * Returns 0 on success, negative value otherwise.
  722. *
  723. * Caller holds tomoyo_read_lock().
  724. */
  725. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  726. struct tomoyo_domain_info * const domain,
  727. const bool is_delete)
  728. {
  729. struct tomoyo_path_acl e = {
  730. .head.type = TOMOYO_TYPE_PATH_ACL,
  731. .perm = 1 << type
  732. };
  733. int error;
  734. if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
  735. e.perm |= TOMOYO_RW_MASK;
  736. if (!tomoyo_parse_name_union(filename, &e.name))
  737. return -EINVAL;
  738. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  739. tomoyo_same_path_acl,
  740. tomoyo_merge_path_acl);
  741. tomoyo_put_name_union(&e.name);
  742. return error;
  743. }
  744. static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
  745. const struct tomoyo_acl_info *b)
  746. {
  747. const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1),
  748. head);
  749. const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2),
  750. head);
  751. return tomoyo_same_acl_head(&p1->head, &p2->head)
  752. && tomoyo_same_name_union(&p1->name, &p2->name)
  753. && tomoyo_same_number_union(&p1->mode, &p2->mode)
  754. && tomoyo_same_number_union(&p1->major, &p2->major)
  755. && tomoyo_same_number_union(&p1->minor, &p2->minor);
  756. }
  757. static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
  758. struct tomoyo_acl_info *b,
  759. const bool is_delete)
  760. {
  761. u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
  762. head)->perm;
  763. u8 perm = *a_perm;
  764. const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
  765. ->perm;
  766. if (is_delete)
  767. perm &= ~b_perm;
  768. else
  769. perm |= b_perm;
  770. *a_perm = perm;
  771. return !perm;
  772. }
  773. /**
  774. * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
  775. *
  776. * @type: Type of operation.
  777. * @filename: Filename.
  778. * @mode: Create mode.
  779. * @major: Device major number.
  780. * @minor: Device minor number.
  781. * @domain: Pointer to "struct tomoyo_domain_info".
  782. * @is_delete: True if it is a delete request.
  783. *
  784. * Returns 0 on success, negative value otherwise.
  785. *
  786. * Caller holds tomoyo_read_lock().
  787. */
  788. static int tomoyo_update_mkdev_acl(const u8 type, const char *filename,
  789. char *mode, char *major, char *minor,
  790. struct tomoyo_domain_info * const
  791. domain, const bool is_delete)
  792. {
  793. struct tomoyo_mkdev_acl e = {
  794. .head.type = TOMOYO_TYPE_MKDEV_ACL,
  795. .perm = 1 << type
  796. };
  797. int error = is_delete ? -ENOENT : -ENOMEM;
  798. if (!tomoyo_parse_name_union(filename, &e.name) ||
  799. !tomoyo_parse_number_union(mode, &e.mode) ||
  800. !tomoyo_parse_number_union(major, &e.major) ||
  801. !tomoyo_parse_number_union(minor, &e.minor))
  802. goto out;
  803. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  804. tomoyo_same_mkdev_acl,
  805. tomoyo_merge_mkdev_acl);
  806. out:
  807. tomoyo_put_name_union(&e.name);
  808. tomoyo_put_number_union(&e.mode);
  809. tomoyo_put_number_union(&e.major);
  810. tomoyo_put_number_union(&e.minor);
  811. return error;
  812. }
  813. static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
  814. const struct tomoyo_acl_info *b)
  815. {
  816. const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
  817. const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
  818. return tomoyo_same_acl_head(&p1->head, &p2->head)
  819. && tomoyo_same_name_union(&p1->name1, &p2->name1)
  820. && tomoyo_same_name_union(&p1->name2, &p2->name2);
  821. }
  822. static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
  823. struct tomoyo_acl_info *b,
  824. const bool is_delete)
  825. {
  826. u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
  827. ->perm;
  828. u8 perm = *a_perm;
  829. const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
  830. if (is_delete)
  831. perm &= ~b_perm;
  832. else
  833. perm |= b_perm;
  834. *a_perm = perm;
  835. return !perm;
  836. }
  837. /**
  838. * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
  839. *
  840. * @type: Type of operation.
  841. * @filename1: First filename.
  842. * @filename2: Second filename.
  843. * @domain: Pointer to "struct tomoyo_domain_info".
  844. * @is_delete: True if it is a delete request.
  845. *
  846. * Returns 0 on success, negative value otherwise.
  847. *
  848. * Caller holds tomoyo_read_lock().
  849. */
  850. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  851. const char *filename2,
  852. struct tomoyo_domain_info * const domain,
  853. const bool is_delete)
  854. {
  855. struct tomoyo_path2_acl e = {
  856. .head.type = TOMOYO_TYPE_PATH2_ACL,
  857. .perm = 1 << type
  858. };
  859. int error = is_delete ? -ENOENT : -ENOMEM;
  860. if (!tomoyo_parse_name_union(filename1, &e.name1) ||
  861. !tomoyo_parse_name_union(filename2, &e.name2))
  862. goto out;
  863. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  864. tomoyo_same_path2_acl,
  865. tomoyo_merge_path2_acl);
  866. out:
  867. tomoyo_put_name_union(&e.name1);
  868. tomoyo_put_name_union(&e.name2);
  869. return error;
  870. }
  871. /**
  872. * tomoyo_path_permission - Check permission for single path operation.
  873. *
  874. * @r: Pointer to "struct tomoyo_request_info".
  875. * @operation: Type of operation.
  876. * @filename: Filename to check.
  877. *
  878. * Returns 0 on success, negative value otherwise.
  879. *
  880. * Caller holds tomoyo_read_lock().
  881. */
  882. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  883. const struct tomoyo_path_info *filename)
  884. {
  885. int error;
  886. next:
  887. r->type = tomoyo_p2mac[operation];
  888. r->mode = tomoyo_get_mode(r->profile, r->type);
  889. if (r->mode == TOMOYO_CONFIG_DISABLED)
  890. return 0;
  891. r->param_type = TOMOYO_TYPE_PATH_ACL;
  892. r->param.path.filename = filename;
  893. r->param.path.operation = operation;
  894. do {
  895. tomoyo_check_acl(r, tomoyo_check_path_acl);
  896. if (!r->granted && operation == TOMOYO_TYPE_READ &&
  897. !r->domain->ignore_global_allow_read &&
  898. tomoyo_globally_readable_file(filename))
  899. r->granted = true;
  900. error = tomoyo_audit_path_log(r);
  901. /*
  902. * Do not retry for execute request, for alias may have
  903. * changed.
  904. */
  905. } while (error == TOMOYO_RETRY_REQUEST &&
  906. operation != TOMOYO_TYPE_EXECUTE);
  907. /*
  908. * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
  909. * we need to check "allow_rewrite" permission if the filename is
  910. * specified by "deny_rewrite" keyword.
  911. */
  912. if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
  913. tomoyo_no_rewrite_file(filename)) {
  914. operation = TOMOYO_TYPE_REWRITE;
  915. goto next;
  916. }
  917. return error;
  918. }
  919. static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
  920. const struct tomoyo_acl_info *b)
  921. {
  922. const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
  923. head);
  924. const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
  925. head);
  926. return tomoyo_same_acl_head(&p1->head, &p2->head)
  927. && tomoyo_same_name_union(&p1->name, &p2->name)
  928. && tomoyo_same_number_union(&p1->number, &p2->number);
  929. }
  930. static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
  931. struct tomoyo_acl_info *b,
  932. const bool is_delete)
  933. {
  934. u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
  935. head)->perm;
  936. u8 perm = *a_perm;
  937. const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
  938. ->perm;
  939. if (is_delete)
  940. perm &= ~b_perm;
  941. else
  942. perm |= b_perm;
  943. *a_perm = perm;
  944. return !perm;
  945. }
  946. /**
  947. * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
  948. *
  949. * @type: Type of operation.
  950. * @filename: Filename.
  951. * @number: Number.
  952. * @domain: Pointer to "struct tomoyo_domain_info".
  953. * @is_delete: True if it is a delete request.
  954. *
  955. * Returns 0 on success, negative value otherwise.
  956. */
  957. static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
  958. char *number,
  959. struct tomoyo_domain_info * const
  960. domain,
  961. const bool is_delete)
  962. {
  963. struct tomoyo_path_number_acl e = {
  964. .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
  965. .perm = 1 << type
  966. };
  967. int error = is_delete ? -ENOENT : -ENOMEM;
  968. if (!tomoyo_parse_name_union(filename, &e.name))
  969. return -EINVAL;
  970. if (!tomoyo_parse_number_union(number, &e.number))
  971. goto out;
  972. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  973. tomoyo_same_path_number_acl,
  974. tomoyo_merge_path_number_acl);
  975. out:
  976. tomoyo_put_name_union(&e.name);
  977. tomoyo_put_number_union(&e.number);
  978. return error;
  979. }
  980. /**
  981. * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  982. *
  983. * @r: Pointer to "strct tomoyo_request_info".
  984. * @filename: Filename to check.
  985. * @number: Number.
  986. *
  987. * Returns 0 on success, negative value otherwise.
  988. *
  989. * Caller holds tomoyo_read_lock().
  990. */
  991. static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
  992. const u8 type,
  993. const struct tomoyo_path_info *filename,
  994. const unsigned long number)
  995. {
  996. int error;
  997. if (!filename)
  998. return 0;
  999. r->param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
  1000. r->param.path_number.operation = type;
  1001. r->param.path_number.filename = filename;
  1002. r->param.path_number.number = number;
  1003. do {
  1004. tomoyo_check_acl(r, tomoyo_check_path_number_acl);
  1005. error = tomoyo_audit_path_number_log(r);
  1006. } while (error == TOMOYO_RETRY_REQUEST);
  1007. return error;
  1008. }
  1009. /**
  1010. * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  1011. *
  1012. * @type: Type of operation.
  1013. * @path: Pointer to "struct path".
  1014. * @number: Number.
  1015. *
  1016. * Returns 0 on success, negative value otherwise.
  1017. */
  1018. int tomoyo_path_number_perm(const u8 type, struct path *path,
  1019. unsigned long number)
  1020. {
  1021. struct tomoyo_request_info r;
  1022. int error = -ENOMEM;
  1023. struct tomoyo_path_info buf;
  1024. int idx;
  1025. if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
  1026. == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
  1027. return 0;
  1028. idx = tomoyo_read_lock();
  1029. if (!tomoyo_get_realpath(&buf, path))
  1030. goto out;
  1031. if (type == TOMOYO_TYPE_MKDIR)
  1032. tomoyo_add_slash(&buf);
  1033. error = tomoyo_path_number_perm2(&r, type, &buf, number);
  1034. out:
  1035. kfree(buf.name);
  1036. tomoyo_read_unlock(idx);
  1037. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1038. error = 0;
  1039. return error;
  1040. }
  1041. /**
  1042. * tomoyo_check_open_permission - Check permission for "read" and "write".
  1043. *
  1044. * @domain: Pointer to "struct tomoyo_domain_info".
  1045. * @path: Pointer to "struct path".
  1046. * @flag: Flags for open().
  1047. *
  1048. * Returns 0 on success, negative value otherwise.
  1049. */
  1050. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  1051. struct path *path, const int flag)
  1052. {
  1053. const u8 acc_mode = ACC_MODE(flag);
  1054. int error = -ENOMEM;
  1055. struct tomoyo_path_info buf;
  1056. struct tomoyo_request_info r;
  1057. int idx;
  1058. if (!path->mnt ||
  1059. (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
  1060. return 0;
  1061. buf.name = NULL;
  1062. r.mode = TOMOYO_CONFIG_DISABLED;
  1063. idx = tomoyo_read_lock();
  1064. if (!tomoyo_get_realpath(&buf, path))
  1065. goto out;
  1066. error = 0;
  1067. /*
  1068. * If the filename is specified by "deny_rewrite" keyword,
  1069. * we need to check "allow_rewrite" permission when the filename is not
  1070. * opened for append mode or the filename is truncated at open time.
  1071. */
  1072. if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
  1073. && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
  1074. != TOMOYO_CONFIG_DISABLED) {
  1075. if (!tomoyo_get_realpath(&buf, path)) {
  1076. error = -ENOMEM;
  1077. goto out;
  1078. }
  1079. if (tomoyo_no_rewrite_file(&buf))
  1080. error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
  1081. &buf);
  1082. }
  1083. if (!error && acc_mode &&
  1084. tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
  1085. != TOMOYO_CONFIG_DISABLED) {
  1086. u8 operation;
  1087. if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
  1088. error = -ENOMEM;
  1089. goto out;
  1090. }
  1091. if (acc_mode == (MAY_READ | MAY_WRITE))
  1092. operation = TOMOYO_TYPE_READ_WRITE;
  1093. else if (acc_mode == MAY_READ)
  1094. operation = TOMOYO_TYPE_READ;
  1095. else
  1096. operation = TOMOYO_TYPE_WRITE;
  1097. error = tomoyo_path_permission(&r, operation, &buf);
  1098. }
  1099. out:
  1100. kfree(buf.name);
  1101. tomoyo_read_unlock(idx);
  1102. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1103. error = 0;
  1104. return error;
  1105. }
  1106. /**
  1107. * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
  1108. *
  1109. * @operation: Type of operation.
  1110. * @path: Pointer to "struct path".
  1111. *
  1112. * Returns 0 on success, negative value otherwise.
  1113. */
  1114. int tomoyo_path_perm(const u8 operation, struct path *path)
  1115. {
  1116. int error = -ENOMEM;
  1117. struct tomoyo_path_info buf;
  1118. struct tomoyo_request_info r;
  1119. int idx;
  1120. if (!path->mnt)
  1121. return 0;
  1122. if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
  1123. == TOMOYO_CONFIG_DISABLED)
  1124. return 0;
  1125. buf.name = NULL;
  1126. idx = tomoyo_read_lock();
  1127. if (!tomoyo_get_realpath(&buf, path))
  1128. goto out;
  1129. switch (operation) {
  1130. case TOMOYO_TYPE_REWRITE:
  1131. if (!tomoyo_no_rewrite_file(&buf)) {
  1132. error = 0;
  1133. goto out;
  1134. }
  1135. break;
  1136. case TOMOYO_TYPE_RMDIR:
  1137. case TOMOYO_TYPE_CHROOT:
  1138. case TOMOYO_TYPE_UMOUNT:
  1139. tomoyo_add_slash(&buf);
  1140. break;
  1141. }
  1142. error = tomoyo_path_permission(&r, operation, &buf);
  1143. out:
  1144. kfree(buf.name);
  1145. tomoyo_read_unlock(idx);
  1146. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1147. error = 0;
  1148. return error;
  1149. }
  1150. /**
  1151. * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
  1152. *
  1153. * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
  1154. * @path: Pointer to "struct path".
  1155. * @mode: Create mode.
  1156. * @dev: Device number.
  1157. *
  1158. * Returns 0 on success, negative value otherwise.
  1159. */
  1160. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  1161. const unsigned int mode, unsigned int dev)
  1162. {
  1163. struct tomoyo_request_info r;
  1164. int error = -ENOMEM;
  1165. struct tomoyo_path_info buf;
  1166. int idx;
  1167. if (!path->mnt ||
  1168. tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
  1169. == TOMOYO_CONFIG_DISABLED)
  1170. return 0;
  1171. idx = tomoyo_read_lock();
  1172. error = -ENOMEM;
  1173. if (tomoyo_get_realpath(&buf, path)) {
  1174. dev = new_decode_dev(dev);
  1175. r.param_type = TOMOYO_TYPE_MKDEV_ACL;
  1176. r.param.mkdev.filename = &buf;
  1177. r.param.mkdev.operation = operation;
  1178. r.param.mkdev.mode = mode;
  1179. r.param.mkdev.major = MAJOR(dev);
  1180. r.param.mkdev.minor = MINOR(dev);
  1181. tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
  1182. error = tomoyo_audit_mkdev_log(&r);
  1183. kfree(buf.name);
  1184. }
  1185. tomoyo_read_unlock(idx);
  1186. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1187. error = 0;
  1188. return error;
  1189. }
  1190. /**
  1191. * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
  1192. *
  1193. * @operation: Type of operation.
  1194. * @path1: Pointer to "struct path".
  1195. * @path2: Pointer to "struct path".
  1196. *
  1197. * Returns 0 on success, negative value otherwise.
  1198. */
  1199. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  1200. struct path *path2)
  1201. {
  1202. int error = -ENOMEM;
  1203. struct tomoyo_path_info buf1;
  1204. struct tomoyo_path_info buf2;
  1205. struct tomoyo_request_info r;
  1206. int idx;
  1207. if (!path1->mnt || !path2->mnt ||
  1208. tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
  1209. == TOMOYO_CONFIG_DISABLED)
  1210. return 0;
  1211. buf1.name = NULL;
  1212. buf2.name = NULL;
  1213. idx = tomoyo_read_lock();
  1214. if (!tomoyo_get_realpath(&buf1, path1) ||
  1215. !tomoyo_get_realpath(&buf2, path2))
  1216. goto out;
  1217. switch (operation) {
  1218. struct dentry *dentry;
  1219. case TOMOYO_TYPE_RENAME:
  1220. case TOMOYO_TYPE_LINK:
  1221. dentry = path1->dentry;
  1222. if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
  1223. break;
  1224. /* fall through */
  1225. case TOMOYO_TYPE_PIVOT_ROOT:
  1226. tomoyo_add_slash(&buf1);
  1227. tomoyo_add_slash(&buf2);
  1228. break;
  1229. }
  1230. r.param_type = TOMOYO_TYPE_PATH2_ACL;
  1231. r.param.path2.operation = operation;
  1232. r.param.path2.filename1 = &buf1;
  1233. r.param.path2.filename2 = &buf2;
  1234. do {
  1235. tomoyo_check_acl(&r, tomoyo_check_path2_acl);
  1236. error = tomoyo_audit_path2_log(&r);
  1237. } while (error == TOMOYO_RETRY_REQUEST);
  1238. out:
  1239. kfree(buf1.name);
  1240. kfree(buf2.name);
  1241. tomoyo_read_unlock(idx);
  1242. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1243. error = 0;
  1244. return error;
  1245. }
  1246. /**
  1247. * tomoyo_write_file_policy - Update file related list.
  1248. *
  1249. * @data: String to parse.
  1250. * @domain: Pointer to "struct tomoyo_domain_info".
  1251. * @is_delete: True if it is a delete request.
  1252. *
  1253. * Returns 0 on success, negative value otherwise.
  1254. *
  1255. * Caller holds tomoyo_read_lock().
  1256. */
  1257. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  1258. const bool is_delete)
  1259. {
  1260. char *w[5];
  1261. u8 type;
  1262. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
  1263. return -EINVAL;
  1264. if (strncmp(w[0], "allow_", 6))
  1265. goto out;
  1266. w[0] += 6;
  1267. for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
  1268. if (strcmp(w[0], tomoyo_path_keyword[type]))
  1269. continue;
  1270. return tomoyo_update_path_acl(type, w[1], domain, is_delete);
  1271. }
  1272. if (!w[2][0])
  1273. goto out;
  1274. for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
  1275. if (strcmp(w[0], tomoyo_path2_keyword[type]))
  1276. continue;
  1277. return tomoyo_update_path2_acl(type, w[1], w[2], domain,
  1278. is_delete);
  1279. }
  1280. for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
  1281. if (strcmp(w[0], tomoyo_path_number_keyword[type]))
  1282. continue;
  1283. return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
  1284. is_delete);
  1285. }
  1286. if (!w[3][0] || !w[4][0])
  1287. goto out;
  1288. for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++) {
  1289. if (strcmp(w[0], tomoyo_mkdev_keyword[type]))
  1290. continue;
  1291. return tomoyo_update_mkdev_acl(type, w[1], w[2], w[3],
  1292. w[4], domain, is_delete);
  1293. }
  1294. out:
  1295. return -EINVAL;
  1296. }