file.c 38 KB

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