file.c 35 KB

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