file.c 36 KB

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