file.c 39 KB

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