file.c 35 KB

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