file.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. /*
  2. * security/tomoyo/file.c
  3. *
  4. * Implementation of the Domain-Based Mandatory Access Control.
  5. *
  6. * Copyright (C) 2005-2009 NTT DATA CORPORATION
  7. *
  8. * Version: 2.2.0-pre 2009/02/01
  9. *
  10. */
  11. #include "common.h"
  12. #include "tomoyo.h"
  13. #include "realpath.h"
  14. #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
  15. /* Structure for "allow_read" keyword. */
  16. struct tomoyo_globally_readable_file_entry {
  17. struct list_head list;
  18. const struct tomoyo_path_info *filename;
  19. bool is_deleted;
  20. };
  21. /* Structure for "file_pattern" keyword. */
  22. struct tomoyo_pattern_entry {
  23. struct list_head list;
  24. const struct tomoyo_path_info *pattern;
  25. bool is_deleted;
  26. };
  27. /* Structure for "deny_rewrite" keyword. */
  28. struct tomoyo_no_rewrite_entry {
  29. struct list_head list;
  30. const struct tomoyo_path_info *pattern;
  31. bool is_deleted;
  32. };
  33. /* Keyword array for single path operations. */
  34. static const char *tomoyo_sp_keyword[TOMOYO_MAX_SINGLE_PATH_OPERATION] = {
  35. [TOMOYO_TYPE_READ_WRITE_ACL] = "read/write",
  36. [TOMOYO_TYPE_EXECUTE_ACL] = "execute",
  37. [TOMOYO_TYPE_READ_ACL] = "read",
  38. [TOMOYO_TYPE_WRITE_ACL] = "write",
  39. [TOMOYO_TYPE_CREATE_ACL] = "create",
  40. [TOMOYO_TYPE_UNLINK_ACL] = "unlink",
  41. [TOMOYO_TYPE_MKDIR_ACL] = "mkdir",
  42. [TOMOYO_TYPE_RMDIR_ACL] = "rmdir",
  43. [TOMOYO_TYPE_MKFIFO_ACL] = "mkfifo",
  44. [TOMOYO_TYPE_MKSOCK_ACL] = "mksock",
  45. [TOMOYO_TYPE_MKBLOCK_ACL] = "mkblock",
  46. [TOMOYO_TYPE_MKCHAR_ACL] = "mkchar",
  47. [TOMOYO_TYPE_TRUNCATE_ACL] = "truncate",
  48. [TOMOYO_TYPE_SYMLINK_ACL] = "symlink",
  49. [TOMOYO_TYPE_REWRITE_ACL] = "rewrite",
  50. };
  51. /* Keyword array for double path operations. */
  52. static const char *tomoyo_dp_keyword[TOMOYO_MAX_DOUBLE_PATH_OPERATION] = {
  53. [TOMOYO_TYPE_LINK_ACL] = "link",
  54. [TOMOYO_TYPE_RENAME_ACL] = "rename",
  55. };
  56. /**
  57. * tomoyo_sp2keyword - Get the name of single path operation.
  58. *
  59. * @operation: Type of operation.
  60. *
  61. * Returns the name of single path operation.
  62. */
  63. const char *tomoyo_sp2keyword(const u8 operation)
  64. {
  65. return (operation < TOMOYO_MAX_SINGLE_PATH_OPERATION)
  66. ? tomoyo_sp_keyword[operation] : NULL;
  67. }
  68. /**
  69. * tomoyo_dp2keyword - Get the name of double path operation.
  70. *
  71. * @operation: Type of operation.
  72. *
  73. * Returns the name of double path operation.
  74. */
  75. const char *tomoyo_dp2keyword(const u8 operation)
  76. {
  77. return (operation < TOMOYO_MAX_DOUBLE_PATH_OPERATION)
  78. ? tomoyo_dp_keyword[operation] : NULL;
  79. }
  80. /**
  81. * tomoyo_strendswith - Check whether the token ends with the given token.
  82. *
  83. * @name: The token to check.
  84. * @tail: The token to find.
  85. *
  86. * Returns true if @name ends with @tail, false otherwise.
  87. */
  88. static bool tomoyo_strendswith(const char *name, const char *tail)
  89. {
  90. int len;
  91. if (!name || !tail)
  92. return false;
  93. len = strlen(name) - strlen(tail);
  94. return len >= 0 && !strcmp(name + len, tail);
  95. }
  96. /**
  97. * tomoyo_get_path - Get realpath.
  98. *
  99. * @path: Pointer to "struct path".
  100. *
  101. * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise.
  102. */
  103. static struct tomoyo_path_info *tomoyo_get_path(struct path *path)
  104. {
  105. int error;
  106. struct tomoyo_path_info_with_data *buf = tomoyo_alloc(sizeof(*buf));
  107. if (!buf)
  108. return NULL;
  109. /* Reserve one byte for appending "/". */
  110. error = tomoyo_realpath_from_path2(path, buf->body,
  111. sizeof(buf->body) - 2);
  112. if (!error) {
  113. buf->head.name = buf->body;
  114. tomoyo_fill_path_info(&buf->head);
  115. return &buf->head;
  116. }
  117. tomoyo_free(buf);
  118. return NULL;
  119. }
  120. /* Lock for domain->acl_info_list. */
  121. DECLARE_RWSEM(tomoyo_domain_acl_info_list_lock);
  122. static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
  123. const char *filename2,
  124. struct tomoyo_domain_info *
  125. const domain, const bool is_delete);
  126. static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
  127. struct tomoyo_domain_info *
  128. const domain, const bool is_delete);
  129. /* The list for "struct tomoyo_globally_readable_file_entry". */
  130. static LIST_HEAD(tomoyo_globally_readable_list);
  131. static DECLARE_RWSEM(tomoyo_globally_readable_list_lock);
  132. /**
  133. * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
  134. *
  135. * @filename: Filename unconditionally permitted to open() for reading.
  136. * @is_delete: True if it is a delete request.
  137. *
  138. * Returns 0 on success, negative value otherwise.
  139. */
  140. static int tomoyo_update_globally_readable_entry(const char *filename,
  141. const bool is_delete)
  142. {
  143. struct tomoyo_globally_readable_file_entry *new_entry;
  144. struct tomoyo_globally_readable_file_entry *ptr;
  145. const struct tomoyo_path_info *saved_filename;
  146. int error = -ENOMEM;
  147. if (!tomoyo_is_correct_path(filename, 1, 0, -1, __func__))
  148. return -EINVAL;
  149. saved_filename = tomoyo_save_name(filename);
  150. if (!saved_filename)
  151. return -ENOMEM;
  152. /***** EXCLUSIVE SECTION START *****/
  153. down_write(&tomoyo_globally_readable_list_lock);
  154. list_for_each_entry(ptr, &tomoyo_globally_readable_list, list) {
  155. if (ptr->filename != saved_filename)
  156. continue;
  157. ptr->is_deleted = is_delete;
  158. error = 0;
  159. goto out;
  160. }
  161. if (is_delete) {
  162. error = -ENOENT;
  163. goto out;
  164. }
  165. new_entry = tomoyo_alloc_element(sizeof(*new_entry));
  166. if (!new_entry)
  167. goto out;
  168. new_entry->filename = saved_filename;
  169. list_add_tail(&new_entry->list, &tomoyo_globally_readable_list);
  170. error = 0;
  171. out:
  172. up_write(&tomoyo_globally_readable_list_lock);
  173. /***** EXCLUSIVE SECTION END *****/
  174. return error;
  175. }
  176. /**
  177. * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
  178. *
  179. * @filename: The filename to check.
  180. *
  181. * Returns true if any domain can open @filename for reading, false otherwise.
  182. */
  183. static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
  184. filename)
  185. {
  186. struct tomoyo_globally_readable_file_entry *ptr;
  187. bool found = false;
  188. down_read(&tomoyo_globally_readable_list_lock);
  189. list_for_each_entry(ptr, &tomoyo_globally_readable_list, list) {
  190. if (!ptr->is_deleted &&
  191. tomoyo_path_matches_pattern(filename, ptr->filename)) {
  192. found = true;
  193. break;
  194. }
  195. }
  196. up_read(&tomoyo_globally_readable_list_lock);
  197. return found;
  198. }
  199. /**
  200. * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
  201. *
  202. * @data: String to parse.
  203. * @is_delete: True if it is a delete request.
  204. *
  205. * Returns 0 on success, negative value otherwise.
  206. */
  207. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
  208. {
  209. return tomoyo_update_globally_readable_entry(data, is_delete);
  210. }
  211. /**
  212. * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
  213. *
  214. * @head: Pointer to "struct tomoyo_io_buffer".
  215. *
  216. * Returns true on success, false otherwise.
  217. */
  218. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
  219. {
  220. struct list_head *pos;
  221. bool done = true;
  222. down_read(&tomoyo_globally_readable_list_lock);
  223. list_for_each_cookie(pos, head->read_var2,
  224. &tomoyo_globally_readable_list) {
  225. struct tomoyo_globally_readable_file_entry *ptr;
  226. ptr = list_entry(pos,
  227. struct tomoyo_globally_readable_file_entry,
  228. list);
  229. if (ptr->is_deleted)
  230. continue;
  231. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
  232. ptr->filename->name)) {
  233. done = false;
  234. break;
  235. }
  236. }
  237. up_read(&tomoyo_globally_readable_list_lock);
  238. return done;
  239. }
  240. /* The list for "struct tomoyo_pattern_entry". */
  241. static LIST_HEAD(tomoyo_pattern_list);
  242. static DECLARE_RWSEM(tomoyo_pattern_list_lock);
  243. /**
  244. * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
  245. *
  246. * @pattern: Pathname pattern.
  247. * @is_delete: True if it is a delete request.
  248. *
  249. * Returns 0 on success, negative value otherwise.
  250. */
  251. static int tomoyo_update_file_pattern_entry(const char *pattern,
  252. const bool is_delete)
  253. {
  254. struct tomoyo_pattern_entry *new_entry;
  255. struct tomoyo_pattern_entry *ptr;
  256. const struct tomoyo_path_info *saved_pattern;
  257. int error = -ENOMEM;
  258. if (!tomoyo_is_correct_path(pattern, 0, 1, 0, __func__))
  259. return -EINVAL;
  260. saved_pattern = tomoyo_save_name(pattern);
  261. if (!saved_pattern)
  262. return -ENOMEM;
  263. /***** EXCLUSIVE SECTION START *****/
  264. down_write(&tomoyo_pattern_list_lock);
  265. list_for_each_entry(ptr, &tomoyo_pattern_list, list) {
  266. if (saved_pattern != ptr->pattern)
  267. continue;
  268. ptr->is_deleted = is_delete;
  269. error = 0;
  270. goto out;
  271. }
  272. if (is_delete) {
  273. error = -ENOENT;
  274. goto out;
  275. }
  276. new_entry = tomoyo_alloc_element(sizeof(*new_entry));
  277. if (!new_entry)
  278. goto out;
  279. new_entry->pattern = saved_pattern;
  280. list_add_tail(&new_entry->list, &tomoyo_pattern_list);
  281. error = 0;
  282. out:
  283. up_write(&tomoyo_pattern_list_lock);
  284. /***** EXCLUSIVE SECTION END *****/
  285. return error;
  286. }
  287. /**
  288. * tomoyo_get_file_pattern - Get patterned pathname.
  289. *
  290. * @filename: The filename to find patterned pathname.
  291. *
  292. * Returns pointer to pathname pattern if matched, @filename otherwise.
  293. */
  294. static const struct tomoyo_path_info *
  295. tomoyo_get_file_pattern(const struct tomoyo_path_info *filename)
  296. {
  297. struct tomoyo_pattern_entry *ptr;
  298. const struct tomoyo_path_info *pattern = NULL;
  299. down_read(&tomoyo_pattern_list_lock);
  300. list_for_each_entry(ptr, &tomoyo_pattern_list, list) {
  301. if (ptr->is_deleted)
  302. continue;
  303. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  304. continue;
  305. pattern = ptr->pattern;
  306. if (tomoyo_strendswith(pattern->name, "/\\*")) {
  307. /* Do nothing. Try to find the better match. */
  308. } else {
  309. /* This would be the better match. Use this. */
  310. break;
  311. }
  312. }
  313. up_read(&tomoyo_pattern_list_lock);
  314. if (pattern)
  315. filename = pattern;
  316. return filename;
  317. }
  318. /**
  319. * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
  320. *
  321. * @data: String to parse.
  322. * @is_delete: True if it is a delete request.
  323. *
  324. * Returns 0 on success, negative value otherwise.
  325. */
  326. int tomoyo_write_pattern_policy(char *data, const bool is_delete)
  327. {
  328. return tomoyo_update_file_pattern_entry(data, is_delete);
  329. }
  330. /**
  331. * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
  332. *
  333. * @head: Pointer to "struct tomoyo_io_buffer".
  334. *
  335. * Returns true on success, false otherwise.
  336. */
  337. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
  338. {
  339. struct list_head *pos;
  340. bool done = true;
  341. down_read(&tomoyo_pattern_list_lock);
  342. list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
  343. struct tomoyo_pattern_entry *ptr;
  344. ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
  345. if (ptr->is_deleted)
  346. continue;
  347. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN "%s\n",
  348. ptr->pattern->name)) {
  349. done = false;
  350. break;
  351. }
  352. }
  353. up_read(&tomoyo_pattern_list_lock);
  354. return done;
  355. }
  356. /* The list for "struct tomoyo_no_rewrite_entry". */
  357. static LIST_HEAD(tomoyo_no_rewrite_list);
  358. static DECLARE_RWSEM(tomoyo_no_rewrite_list_lock);
  359. /**
  360. * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
  361. *
  362. * @pattern: Pathname pattern that are not rewritable by default.
  363. * @is_delete: True if it is a delete request.
  364. *
  365. * Returns 0 on success, negative value otherwise.
  366. */
  367. static int tomoyo_update_no_rewrite_entry(const char *pattern,
  368. const bool is_delete)
  369. {
  370. struct tomoyo_no_rewrite_entry *new_entry, *ptr;
  371. const struct tomoyo_path_info *saved_pattern;
  372. int error = -ENOMEM;
  373. if (!tomoyo_is_correct_path(pattern, 0, 0, 0, __func__))
  374. return -EINVAL;
  375. saved_pattern = tomoyo_save_name(pattern);
  376. if (!saved_pattern)
  377. return -ENOMEM;
  378. /***** EXCLUSIVE SECTION START *****/
  379. down_write(&tomoyo_no_rewrite_list_lock);
  380. list_for_each_entry(ptr, &tomoyo_no_rewrite_list, list) {
  381. if (ptr->pattern != saved_pattern)
  382. continue;
  383. ptr->is_deleted = is_delete;
  384. error = 0;
  385. goto out;
  386. }
  387. if (is_delete) {
  388. error = -ENOENT;
  389. goto out;
  390. }
  391. new_entry = tomoyo_alloc_element(sizeof(*new_entry));
  392. if (!new_entry)
  393. goto out;
  394. new_entry->pattern = saved_pattern;
  395. list_add_tail(&new_entry->list, &tomoyo_no_rewrite_list);
  396. error = 0;
  397. out:
  398. up_write(&tomoyo_no_rewrite_list_lock);
  399. /***** EXCLUSIVE SECTION END *****/
  400. return error;
  401. }
  402. /**
  403. * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
  404. *
  405. * @filename: Filename to check.
  406. *
  407. * Returns true if @filename is specified by "deny_rewrite" directive,
  408. * false otherwise.
  409. */
  410. static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
  411. {
  412. struct tomoyo_no_rewrite_entry *ptr;
  413. bool found = false;
  414. down_read(&tomoyo_no_rewrite_list_lock);
  415. list_for_each_entry(ptr, &tomoyo_no_rewrite_list, list) {
  416. if (ptr->is_deleted)
  417. continue;
  418. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  419. continue;
  420. found = true;
  421. break;
  422. }
  423. up_read(&tomoyo_no_rewrite_list_lock);
  424. return found;
  425. }
  426. /**
  427. * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
  428. *
  429. * @data: String to parse.
  430. * @is_delete: True if it is a delete request.
  431. *
  432. * Returns 0 on success, negative value otherwise.
  433. */
  434. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
  435. {
  436. return tomoyo_update_no_rewrite_entry(data, is_delete);
  437. }
  438. /**
  439. * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
  440. *
  441. * @head: Pointer to "struct tomoyo_io_buffer".
  442. *
  443. * Returns true on success, false otherwise.
  444. */
  445. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
  446. {
  447. struct list_head *pos;
  448. bool done = true;
  449. down_read(&tomoyo_no_rewrite_list_lock);
  450. list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
  451. struct tomoyo_no_rewrite_entry *ptr;
  452. ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
  453. if (ptr->is_deleted)
  454. continue;
  455. if (!tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE "%s\n",
  456. ptr->pattern->name)) {
  457. done = false;
  458. break;
  459. }
  460. }
  461. up_read(&tomoyo_no_rewrite_list_lock);
  462. return done;
  463. }
  464. /**
  465. * tomoyo_update_file_acl - Update file's read/write/execute ACL.
  466. *
  467. * @filename: Filename.
  468. * @perm: Permission (between 1 to 7).
  469. * @domain: Pointer to "struct tomoyo_domain_info".
  470. * @is_delete: True if it is a delete request.
  471. *
  472. * Returns 0 on success, negative value otherwise.
  473. *
  474. * This is legacy support interface for older policy syntax.
  475. * Current policy syntax uses "allow_read/write" instead of "6",
  476. * "allow_read" instead of "4", "allow_write" instead of "2",
  477. * "allow_execute" instead of "1".
  478. */
  479. static int tomoyo_update_file_acl(const char *filename, u8 perm,
  480. struct tomoyo_domain_info * const domain,
  481. const bool is_delete)
  482. {
  483. if (perm > 7 || !perm) {
  484. printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
  485. __func__, perm, filename);
  486. return -EINVAL;
  487. }
  488. if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
  489. /*
  490. * Only 'allow_mkdir' and 'allow_rmdir' are valid for
  491. * directory permissions.
  492. */
  493. return 0;
  494. if (perm & 4)
  495. tomoyo_update_single_path_acl(TOMOYO_TYPE_READ_ACL, filename,
  496. domain, is_delete);
  497. if (perm & 2)
  498. tomoyo_update_single_path_acl(TOMOYO_TYPE_WRITE_ACL, filename,
  499. domain, is_delete);
  500. if (perm & 1)
  501. tomoyo_update_single_path_acl(TOMOYO_TYPE_EXECUTE_ACL,
  502. filename, domain, is_delete);
  503. return 0;
  504. }
  505. /**
  506. * tomoyo_check_single_path_acl2 - Check permission for single path operation.
  507. *
  508. * @domain: Pointer to "struct tomoyo_domain_info".
  509. * @filename: Filename to check.
  510. * @perm: Permission.
  511. * @may_use_pattern: True if patterned ACL is permitted.
  512. *
  513. * Returns 0 on success, -EPERM otherwise.
  514. */
  515. static int tomoyo_check_single_path_acl2(const struct tomoyo_domain_info *
  516. domain,
  517. const struct tomoyo_path_info *
  518. filename,
  519. const u16 perm,
  520. const bool may_use_pattern)
  521. {
  522. struct tomoyo_acl_info *ptr;
  523. int error = -EPERM;
  524. down_read(&tomoyo_domain_acl_info_list_lock);
  525. list_for_each_entry(ptr, &domain->acl_info_list, list) {
  526. struct tomoyo_single_path_acl_record *acl;
  527. if (tomoyo_acl_type2(ptr) != TOMOYO_TYPE_SINGLE_PATH_ACL)
  528. continue;
  529. acl = container_of(ptr, struct tomoyo_single_path_acl_record,
  530. head);
  531. if (!(acl->perm & perm))
  532. continue;
  533. if (may_use_pattern || !acl->filename->is_patterned) {
  534. if (!tomoyo_path_matches_pattern(filename,
  535. acl->filename))
  536. continue;
  537. } else {
  538. continue;
  539. }
  540. error = 0;
  541. break;
  542. }
  543. up_read(&tomoyo_domain_acl_info_list_lock);
  544. return error;
  545. }
  546. /**
  547. * tomoyo_check_file_acl - Check permission for opening files.
  548. *
  549. * @domain: Pointer to "struct tomoyo_domain_info".
  550. * @filename: Filename to check.
  551. * @operation: Mode ("read" or "write" or "read/write" or "execute").
  552. *
  553. * Returns 0 on success, -EPERM otherwise.
  554. */
  555. static int tomoyo_check_file_acl(const struct tomoyo_domain_info *domain,
  556. const struct tomoyo_path_info *filename,
  557. const u8 operation)
  558. {
  559. u16 perm = 0;
  560. if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
  561. return 0;
  562. if (operation == 6)
  563. perm = 1 << TOMOYO_TYPE_READ_WRITE_ACL;
  564. else if (operation == 4)
  565. perm = 1 << TOMOYO_TYPE_READ_ACL;
  566. else if (operation == 2)
  567. perm = 1 << TOMOYO_TYPE_WRITE_ACL;
  568. else if (operation == 1)
  569. perm = 1 << TOMOYO_TYPE_EXECUTE_ACL;
  570. else
  571. BUG();
  572. return tomoyo_check_single_path_acl2(domain, filename, perm,
  573. operation != 1);
  574. }
  575. /**
  576. * tomoyo_check_file_perm2 - Check permission for opening files.
  577. *
  578. * @domain: Pointer to "struct tomoyo_domain_info".
  579. * @filename: Filename to check.
  580. * @perm: Mode ("read" or "write" or "read/write" or "execute").
  581. * @operation: Operation name passed used for verbose mode.
  582. * @mode: Access control mode.
  583. *
  584. * Returns 0 on success, negative value otherwise.
  585. */
  586. static int tomoyo_check_file_perm2(struct tomoyo_domain_info * const domain,
  587. const struct tomoyo_path_info *filename,
  588. const u8 perm, const char *operation,
  589. const u8 mode)
  590. {
  591. const bool is_enforce = (mode == 3);
  592. const char *msg = "<unknown>";
  593. int error = 0;
  594. if (!filename)
  595. return 0;
  596. error = tomoyo_check_file_acl(domain, filename, perm);
  597. if (error && perm == 4 &&
  598. (domain->flags & TOMOYO_DOMAIN_FLAGS_IGNORE_GLOBAL_ALLOW_READ) == 0
  599. && tomoyo_is_globally_readable_file(filename))
  600. error = 0;
  601. if (perm == 6)
  602. msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_WRITE_ACL);
  603. else if (perm == 4)
  604. msg = tomoyo_sp2keyword(TOMOYO_TYPE_READ_ACL);
  605. else if (perm == 2)
  606. msg = tomoyo_sp2keyword(TOMOYO_TYPE_WRITE_ACL);
  607. else if (perm == 1)
  608. msg = tomoyo_sp2keyword(TOMOYO_TYPE_EXECUTE_ACL);
  609. else
  610. BUG();
  611. if (!error)
  612. return 0;
  613. if (tomoyo_verbose_mode(domain))
  614. printk(KERN_WARNING "TOMOYO-%s: Access '%s(%s) %s' denied "
  615. "for %s\n", tomoyo_get_msg(is_enforce), msg, operation,
  616. filename->name, tomoyo_get_last_name(domain));
  617. if (is_enforce)
  618. return error;
  619. if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
  620. /* Don't use patterns for execute permission. */
  621. const struct tomoyo_path_info *patterned_file = (perm != 1) ?
  622. tomoyo_get_file_pattern(filename) : filename;
  623. tomoyo_update_file_acl(patterned_file->name, perm,
  624. domain, false);
  625. }
  626. return 0;
  627. }
  628. /**
  629. * tomoyo_write_file_policy - Update file related list.
  630. *
  631. * @data: String to parse.
  632. * @domain: Pointer to "struct tomoyo_domain_info".
  633. * @is_delete: True if it is a delete request.
  634. *
  635. * Returns 0 on success, negative value otherwise.
  636. */
  637. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  638. const bool is_delete)
  639. {
  640. char *filename = strchr(data, ' ');
  641. char *filename2;
  642. unsigned int perm;
  643. u8 type;
  644. if (!filename)
  645. return -EINVAL;
  646. *filename++ = '\0';
  647. if (sscanf(data, "%u", &perm) == 1)
  648. return tomoyo_update_file_acl(filename, (u8) perm, domain,
  649. is_delete);
  650. if (strncmp(data, "allow_", 6))
  651. goto out;
  652. data += 6;
  653. for (type = 0; type < TOMOYO_MAX_SINGLE_PATH_OPERATION; type++) {
  654. if (strcmp(data, tomoyo_sp_keyword[type]))
  655. continue;
  656. return tomoyo_update_single_path_acl(type, filename,
  657. domain, is_delete);
  658. }
  659. filename2 = strchr(filename, ' ');
  660. if (!filename2)
  661. goto out;
  662. *filename2++ = '\0';
  663. for (type = 0; type < TOMOYO_MAX_DOUBLE_PATH_OPERATION; type++) {
  664. if (strcmp(data, tomoyo_dp_keyword[type]))
  665. continue;
  666. return tomoyo_update_double_path_acl(type, filename, filename2,
  667. domain, is_delete);
  668. }
  669. out:
  670. return -EINVAL;
  671. }
  672. /**
  673. * tomoyo_update_single_path_acl - Update "struct tomoyo_single_path_acl_record" list.
  674. *
  675. * @type: Type of operation.
  676. * @filename: Filename.
  677. * @domain: Pointer to "struct tomoyo_domain_info".
  678. * @is_delete: True if it is a delete request.
  679. *
  680. * Returns 0 on success, negative value otherwise.
  681. */
  682. static int tomoyo_update_single_path_acl(const u8 type, const char *filename,
  683. struct tomoyo_domain_info *
  684. const domain, const bool is_delete)
  685. {
  686. static const u16 rw_mask =
  687. (1 << TOMOYO_TYPE_READ_ACL) | (1 << TOMOYO_TYPE_WRITE_ACL);
  688. const struct tomoyo_path_info *saved_filename;
  689. struct tomoyo_acl_info *ptr;
  690. struct tomoyo_single_path_acl_record *acl;
  691. int error = -ENOMEM;
  692. const u16 perm = 1 << type;
  693. if (!domain)
  694. return -EINVAL;
  695. if (!tomoyo_is_correct_path(filename, 0, 0, 0, __func__))
  696. return -EINVAL;
  697. saved_filename = tomoyo_save_name(filename);
  698. if (!saved_filename)
  699. return -ENOMEM;
  700. /***** EXCLUSIVE SECTION START *****/
  701. down_write(&tomoyo_domain_acl_info_list_lock);
  702. if (is_delete)
  703. goto delete;
  704. list_for_each_entry(ptr, &domain->acl_info_list, list) {
  705. if (tomoyo_acl_type1(ptr) != TOMOYO_TYPE_SINGLE_PATH_ACL)
  706. continue;
  707. acl = container_of(ptr, struct tomoyo_single_path_acl_record,
  708. head);
  709. if (acl->filename != saved_filename)
  710. continue;
  711. /* Special case. Clear all bits if marked as deleted. */
  712. if (ptr->type & TOMOYO_ACL_DELETED)
  713. acl->perm = 0;
  714. acl->perm |= perm;
  715. if ((acl->perm & rw_mask) == rw_mask)
  716. acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE_ACL;
  717. else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL))
  718. acl->perm |= rw_mask;
  719. ptr->type &= ~TOMOYO_ACL_DELETED;
  720. error = 0;
  721. goto out;
  722. }
  723. /* Not found. Append it to the tail. */
  724. acl = tomoyo_alloc_acl_element(TOMOYO_TYPE_SINGLE_PATH_ACL);
  725. if (!acl)
  726. goto out;
  727. acl->perm = perm;
  728. if (perm == (1 << TOMOYO_TYPE_READ_WRITE_ACL))
  729. acl->perm |= rw_mask;
  730. acl->filename = saved_filename;
  731. list_add_tail(&acl->head.list, &domain->acl_info_list);
  732. error = 0;
  733. goto out;
  734. delete:
  735. error = -ENOENT;
  736. list_for_each_entry(ptr, &domain->acl_info_list, list) {
  737. if (tomoyo_acl_type2(ptr) != TOMOYO_TYPE_SINGLE_PATH_ACL)
  738. continue;
  739. acl = container_of(ptr, struct tomoyo_single_path_acl_record,
  740. head);
  741. if (acl->filename != saved_filename)
  742. continue;
  743. acl->perm &= ~perm;
  744. if ((acl->perm & rw_mask) != rw_mask)
  745. acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE_ACL);
  746. else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE_ACL)))
  747. acl->perm &= ~rw_mask;
  748. if (!acl->perm)
  749. ptr->type |= TOMOYO_ACL_DELETED;
  750. error = 0;
  751. break;
  752. }
  753. out:
  754. up_write(&tomoyo_domain_acl_info_list_lock);
  755. /***** EXCLUSIVE SECTION END *****/
  756. return error;
  757. }
  758. /**
  759. * tomoyo_update_double_path_acl - Update "struct tomoyo_double_path_acl_record" list.
  760. *
  761. * @type: Type of operation.
  762. * @filename1: First filename.
  763. * @filename2: Second filename.
  764. * @domain: Pointer to "struct tomoyo_domain_info".
  765. * @is_delete: True if it is a delete request.
  766. *
  767. * Returns 0 on success, negative value otherwise.
  768. */
  769. static int tomoyo_update_double_path_acl(const u8 type, const char *filename1,
  770. const char *filename2,
  771. struct tomoyo_domain_info *
  772. const domain, const bool is_delete)
  773. {
  774. const struct tomoyo_path_info *saved_filename1;
  775. const struct tomoyo_path_info *saved_filename2;
  776. struct tomoyo_acl_info *ptr;
  777. struct tomoyo_double_path_acl_record *acl;
  778. int error = -ENOMEM;
  779. const u8 perm = 1 << type;
  780. if (!domain)
  781. return -EINVAL;
  782. if (!tomoyo_is_correct_path(filename1, 0, 0, 0, __func__) ||
  783. !tomoyo_is_correct_path(filename2, 0, 0, 0, __func__))
  784. return -EINVAL;
  785. saved_filename1 = tomoyo_save_name(filename1);
  786. saved_filename2 = tomoyo_save_name(filename2);
  787. if (!saved_filename1 || !saved_filename2)
  788. return -ENOMEM;
  789. /***** EXCLUSIVE SECTION START *****/
  790. down_write(&tomoyo_domain_acl_info_list_lock);
  791. if (is_delete)
  792. goto delete;
  793. list_for_each_entry(ptr, &domain->acl_info_list, list) {
  794. if (tomoyo_acl_type1(ptr) != TOMOYO_TYPE_DOUBLE_PATH_ACL)
  795. continue;
  796. acl = container_of(ptr, struct tomoyo_double_path_acl_record,
  797. head);
  798. if (acl->filename1 != saved_filename1 ||
  799. acl->filename2 != saved_filename2)
  800. continue;
  801. /* Special case. Clear all bits if marked as deleted. */
  802. if (ptr->type & TOMOYO_ACL_DELETED)
  803. acl->perm = 0;
  804. acl->perm |= perm;
  805. ptr->type &= ~TOMOYO_ACL_DELETED;
  806. error = 0;
  807. goto out;
  808. }
  809. /* Not found. Append it to the tail. */
  810. acl = tomoyo_alloc_acl_element(TOMOYO_TYPE_DOUBLE_PATH_ACL);
  811. if (!acl)
  812. goto out;
  813. acl->perm = perm;
  814. acl->filename1 = saved_filename1;
  815. acl->filename2 = saved_filename2;
  816. list_add_tail(&acl->head.list, &domain->acl_info_list);
  817. error = 0;
  818. goto out;
  819. delete:
  820. error = -ENOENT;
  821. list_for_each_entry(ptr, &domain->acl_info_list, list) {
  822. if (tomoyo_acl_type2(ptr) != TOMOYO_TYPE_DOUBLE_PATH_ACL)
  823. continue;
  824. acl = container_of(ptr, struct tomoyo_double_path_acl_record,
  825. head);
  826. if (acl->filename1 != saved_filename1 ||
  827. acl->filename2 != saved_filename2)
  828. continue;
  829. acl->perm &= ~perm;
  830. if (!acl->perm)
  831. ptr->type |= TOMOYO_ACL_DELETED;
  832. error = 0;
  833. break;
  834. }
  835. out:
  836. up_write(&tomoyo_domain_acl_info_list_lock);
  837. /***** EXCLUSIVE SECTION END *****/
  838. return error;
  839. }
  840. /**
  841. * tomoyo_check_single_path_acl - Check permission for single path operation.
  842. *
  843. * @domain: Pointer to "struct tomoyo_domain_info".
  844. * @type: Type of operation.
  845. * @filename: Filename to check.
  846. *
  847. * Returns 0 on success, negative value otherwise.
  848. */
  849. static int tomoyo_check_single_path_acl(struct tomoyo_domain_info *domain,
  850. const u8 type,
  851. const struct tomoyo_path_info *filename)
  852. {
  853. if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
  854. return 0;
  855. return tomoyo_check_single_path_acl2(domain, filename, 1 << type, 1);
  856. }
  857. /**
  858. * tomoyo_check_double_path_acl - Check permission for double path operation.
  859. *
  860. * @domain: Pointer to "struct tomoyo_domain_info".
  861. * @type: Type of operation.
  862. * @filename1: First filename to check.
  863. * @filename2: Second filename to check.
  864. *
  865. * Returns 0 on success, -EPERM otherwise.
  866. */
  867. static int tomoyo_check_double_path_acl(const struct tomoyo_domain_info *domain,
  868. const u8 type,
  869. const struct tomoyo_path_info *
  870. filename1,
  871. const struct tomoyo_path_info *
  872. filename2)
  873. {
  874. struct tomoyo_acl_info *ptr;
  875. const u8 perm = 1 << type;
  876. int error = -EPERM;
  877. if (!tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE))
  878. return 0;
  879. down_read(&tomoyo_domain_acl_info_list_lock);
  880. list_for_each_entry(ptr, &domain->acl_info_list, list) {
  881. struct tomoyo_double_path_acl_record *acl;
  882. if (tomoyo_acl_type2(ptr) != TOMOYO_TYPE_DOUBLE_PATH_ACL)
  883. continue;
  884. acl = container_of(ptr, struct tomoyo_double_path_acl_record,
  885. head);
  886. if (!(acl->perm & perm))
  887. continue;
  888. if (!tomoyo_path_matches_pattern(filename1, acl->filename1))
  889. continue;
  890. if (!tomoyo_path_matches_pattern(filename2, acl->filename2))
  891. continue;
  892. error = 0;
  893. break;
  894. }
  895. up_read(&tomoyo_domain_acl_info_list_lock);
  896. return error;
  897. }
  898. /**
  899. * tomoyo_check_single_path_permission2 - Check permission for single path operation.
  900. *
  901. * @domain: Pointer to "struct tomoyo_domain_info".
  902. * @operation: Type of operation.
  903. * @filename: Filename to check.
  904. * @mode: Access control mode.
  905. *
  906. * Returns 0 on success, negative value otherwise.
  907. */
  908. static int tomoyo_check_single_path_permission2(struct tomoyo_domain_info *
  909. const domain, u8 operation,
  910. const struct tomoyo_path_info *
  911. filename, const u8 mode)
  912. {
  913. const char *msg;
  914. int error;
  915. const bool is_enforce = (mode == 3);
  916. if (!mode)
  917. return 0;
  918. next:
  919. error = tomoyo_check_single_path_acl(domain, operation, filename);
  920. msg = tomoyo_sp2keyword(operation);
  921. if (!error)
  922. goto ok;
  923. if (tomoyo_verbose_mode(domain))
  924. printk(KERN_WARNING "TOMOYO-%s: Access '%s %s' denied for %s\n",
  925. tomoyo_get_msg(is_enforce), msg, filename->name,
  926. tomoyo_get_last_name(domain));
  927. if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
  928. const char *name = tomoyo_get_file_pattern(filename)->name;
  929. tomoyo_update_single_path_acl(operation, name, domain, false);
  930. }
  931. if (!is_enforce)
  932. error = 0;
  933. ok:
  934. /*
  935. * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
  936. * we need to check "allow_rewrite" permission if the filename is
  937. * specified by "deny_rewrite" keyword.
  938. */
  939. if (!error && operation == TOMOYO_TYPE_TRUNCATE_ACL &&
  940. tomoyo_is_no_rewrite_file(filename)) {
  941. operation = TOMOYO_TYPE_REWRITE_ACL;
  942. goto next;
  943. }
  944. return error;
  945. }
  946. /**
  947. * tomoyo_check_file_perm - Check permission for sysctl()'s "read" and "write".
  948. *
  949. * @domain: Pointer to "struct tomoyo_domain_info".
  950. * @filename: Filename to check.
  951. * @perm: Mode ("read" or "write" or "read/write").
  952. * Returns 0 on success, negative value otherwise.
  953. */
  954. int tomoyo_check_file_perm(struct tomoyo_domain_info *domain,
  955. const char *filename, const u8 perm)
  956. {
  957. struct tomoyo_path_info name;
  958. const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
  959. if (!mode)
  960. return 0;
  961. name.name = filename;
  962. tomoyo_fill_path_info(&name);
  963. return tomoyo_check_file_perm2(domain, &name, perm, "sysctl", mode);
  964. }
  965. /**
  966. * tomoyo_check_exec_perm - Check permission for "execute".
  967. *
  968. * @domain: Pointer to "struct tomoyo_domain_info".
  969. * @filename: Check permission for "execute".
  970. * @tmp: Buffer for temporary use.
  971. *
  972. * Returns 0 on success, negativevalue otherwise.
  973. */
  974. int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
  975. const struct tomoyo_path_info *filename,
  976. struct tomoyo_page_buffer *tmp)
  977. {
  978. const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
  979. if (!mode)
  980. return 0;
  981. return tomoyo_check_file_perm2(domain, filename, 1, "do_execve", mode);
  982. }
  983. /**
  984. * tomoyo_check_open_permission - Check permission for "read" and "write".
  985. *
  986. * @domain: Pointer to "struct tomoyo_domain_info".
  987. * @path: Pointer to "struct path".
  988. * @flag: Flags for open().
  989. *
  990. * Returns 0 on success, negative value otherwise.
  991. */
  992. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  993. struct path *path, const int flag)
  994. {
  995. const u8 acc_mode = ACC_MODE(flag);
  996. int error = -ENOMEM;
  997. struct tomoyo_path_info *buf;
  998. const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
  999. const bool is_enforce = (mode == 3);
  1000. if (!mode || !path->mnt)
  1001. return 0;
  1002. if (acc_mode == 0)
  1003. return 0;
  1004. if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
  1005. /*
  1006. * I don't check directories here because mkdir() and rmdir()
  1007. * don't call me.
  1008. */
  1009. return 0;
  1010. buf = tomoyo_get_path(path);
  1011. if (!buf)
  1012. goto out;
  1013. error = 0;
  1014. /*
  1015. * If the filename is specified by "deny_rewrite" keyword,
  1016. * we need to check "allow_rewrite" permission when the filename is not
  1017. * opened for append mode or the filename is truncated at open time.
  1018. */
  1019. if ((acc_mode & MAY_WRITE) &&
  1020. ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
  1021. (tomoyo_is_no_rewrite_file(buf))) {
  1022. error = tomoyo_check_single_path_permission2(domain,
  1023. TOMOYO_TYPE_REWRITE_ACL,
  1024. buf, mode);
  1025. }
  1026. if (!error)
  1027. error = tomoyo_check_file_perm2(domain, buf, acc_mode, "open",
  1028. mode);
  1029. if (!error && (flag & O_TRUNC))
  1030. error = tomoyo_check_single_path_permission2(domain,
  1031. TOMOYO_TYPE_TRUNCATE_ACL,
  1032. buf, mode);
  1033. out:
  1034. tomoyo_free(buf);
  1035. if (!is_enforce)
  1036. error = 0;
  1037. return error;
  1038. }
  1039. /**
  1040. * tomoyo_check_1path_perm - Check permission for "create", "unlink", "mkdir", "rmdir", "mkfifo", "mksock", "mkblock", "mkchar", "truncate" and "symlink".
  1041. *
  1042. * @domain: Pointer to "struct tomoyo_domain_info".
  1043. * @operation: Type of operation.
  1044. * @path: Pointer to "struct path".
  1045. *
  1046. * Returns 0 on success, negative value otherwise.
  1047. */
  1048. int tomoyo_check_1path_perm(struct tomoyo_domain_info *domain,
  1049. const u8 operation, struct path *path)
  1050. {
  1051. int error = -ENOMEM;
  1052. struct tomoyo_path_info *buf;
  1053. const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
  1054. const bool is_enforce = (mode == 3);
  1055. if (!mode || !path->mnt)
  1056. return 0;
  1057. buf = tomoyo_get_path(path);
  1058. if (!buf)
  1059. goto out;
  1060. switch (operation) {
  1061. case TOMOYO_TYPE_MKDIR_ACL:
  1062. case TOMOYO_TYPE_RMDIR_ACL:
  1063. if (!buf->is_dir) {
  1064. /*
  1065. * tomoyo_get_path() reserves space for appending "/."
  1066. */
  1067. strcat((char *) buf->name, "/");
  1068. tomoyo_fill_path_info(buf);
  1069. }
  1070. }
  1071. error = tomoyo_check_single_path_permission2(domain, operation, buf,
  1072. mode);
  1073. out:
  1074. tomoyo_free(buf);
  1075. if (!is_enforce)
  1076. error = 0;
  1077. return error;
  1078. }
  1079. /**
  1080. * tomoyo_check_rewrite_permission - Check permission for "rewrite".
  1081. *
  1082. * @domain: Pointer to "struct tomoyo_domain_info".
  1083. * @filp: Pointer to "struct file".
  1084. *
  1085. * Returns 0 on success, negative value otherwise.
  1086. */
  1087. int tomoyo_check_rewrite_permission(struct tomoyo_domain_info *domain,
  1088. struct file *filp)
  1089. {
  1090. int error = -ENOMEM;
  1091. const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
  1092. const bool is_enforce = (mode == 3);
  1093. struct tomoyo_path_info *buf;
  1094. if (!mode || !filp->f_path.mnt)
  1095. return 0;
  1096. buf = tomoyo_get_path(&filp->f_path);
  1097. if (!buf)
  1098. goto out;
  1099. if (!tomoyo_is_no_rewrite_file(buf)) {
  1100. error = 0;
  1101. goto out;
  1102. }
  1103. error = tomoyo_check_single_path_permission2(domain,
  1104. TOMOYO_TYPE_REWRITE_ACL,
  1105. buf, mode);
  1106. out:
  1107. tomoyo_free(buf);
  1108. if (!is_enforce)
  1109. error = 0;
  1110. return error;
  1111. }
  1112. /**
  1113. * tomoyo_check_2path_perm - Check permission for "rename" and "link".
  1114. *
  1115. * @domain: Pointer to "struct tomoyo_domain_info".
  1116. * @operation: Type of operation.
  1117. * @path1: Pointer to "struct path".
  1118. * @path2: Pointer to "struct path".
  1119. *
  1120. * Returns 0 on success, negative value otherwise.
  1121. */
  1122. int tomoyo_check_2path_perm(struct tomoyo_domain_info * const domain,
  1123. const u8 operation, struct path *path1,
  1124. struct path *path2)
  1125. {
  1126. int error = -ENOMEM;
  1127. struct tomoyo_path_info *buf1, *buf2;
  1128. const u8 mode = tomoyo_check_flags(domain, TOMOYO_MAC_FOR_FILE);
  1129. const bool is_enforce = (mode == 3);
  1130. const char *msg;
  1131. if (!mode || !path1->mnt || !path2->mnt)
  1132. return 0;
  1133. buf1 = tomoyo_get_path(path1);
  1134. buf2 = tomoyo_get_path(path2);
  1135. if (!buf1 || !buf2)
  1136. goto out;
  1137. {
  1138. struct dentry *dentry = path1->dentry;
  1139. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
  1140. /*
  1141. * tomoyo_get_path() reserves space for appending "/."
  1142. */
  1143. if (!buf1->is_dir) {
  1144. strcat((char *) buf1->name, "/");
  1145. tomoyo_fill_path_info(buf1);
  1146. }
  1147. if (!buf2->is_dir) {
  1148. strcat((char *) buf2->name, "/");
  1149. tomoyo_fill_path_info(buf2);
  1150. }
  1151. }
  1152. }
  1153. error = tomoyo_check_double_path_acl(domain, operation, buf1, buf2);
  1154. msg = tomoyo_dp2keyword(operation);
  1155. if (!error)
  1156. goto out;
  1157. if (tomoyo_verbose_mode(domain))
  1158. printk(KERN_WARNING "TOMOYO-%s: Access '%s %s %s' "
  1159. "denied for %s\n", tomoyo_get_msg(is_enforce),
  1160. msg, buf1->name, buf2->name,
  1161. tomoyo_get_last_name(domain));
  1162. if (mode == 1 && tomoyo_domain_quota_is_ok(domain)) {
  1163. const char *name1 = tomoyo_get_file_pattern(buf1)->name;
  1164. const char *name2 = tomoyo_get_file_pattern(buf2)->name;
  1165. tomoyo_update_double_path_acl(operation, name1, name2, domain,
  1166. false);
  1167. }
  1168. out:
  1169. tomoyo_free(buf1);
  1170. tomoyo_free(buf2);
  1171. if (!is_enforce)
  1172. error = 0;
  1173. return error;
  1174. }