file.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*
  2. * security/tomoyo/file.c
  3. *
  4. * Pathname restriction functions.
  5. *
  6. * Copyright (C) 2005-2010 NTT DATA CORPORATION
  7. */
  8. #include "common.h"
  9. #include <linux/slab.h>
  10. /* Keyword array for operations with one pathname. */
  11. const char *tomoyo_path_keyword[TOMOYO_MAX_PATH_OPERATION] = {
  12. [TOMOYO_TYPE_READ_WRITE] = "read/write",
  13. [TOMOYO_TYPE_EXECUTE] = "execute",
  14. [TOMOYO_TYPE_READ] = "read",
  15. [TOMOYO_TYPE_WRITE] = "write",
  16. [TOMOYO_TYPE_UNLINK] = "unlink",
  17. [TOMOYO_TYPE_RMDIR] = "rmdir",
  18. [TOMOYO_TYPE_TRUNCATE] = "truncate",
  19. [TOMOYO_TYPE_SYMLINK] = "symlink",
  20. [TOMOYO_TYPE_REWRITE] = "rewrite",
  21. [TOMOYO_TYPE_CHROOT] = "chroot",
  22. [TOMOYO_TYPE_UMOUNT] = "unmount",
  23. };
  24. /* Keyword array for operations with one pathname and three numbers. */
  25. const char *tomoyo_mkdev_keyword[TOMOYO_MAX_MKDEV_OPERATION] = {
  26. [TOMOYO_TYPE_MKBLOCK] = "mkblock",
  27. [TOMOYO_TYPE_MKCHAR] = "mkchar",
  28. };
  29. /* Keyword array for operations with two pathnames. */
  30. const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
  31. [TOMOYO_TYPE_LINK] = "link",
  32. [TOMOYO_TYPE_RENAME] = "rename",
  33. [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
  34. };
  35. /* Keyword array for operations with one pathname and one number. */
  36. const char *tomoyo_path_number_keyword[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  37. [TOMOYO_TYPE_CREATE] = "create",
  38. [TOMOYO_TYPE_MKDIR] = "mkdir",
  39. [TOMOYO_TYPE_MKFIFO] = "mkfifo",
  40. [TOMOYO_TYPE_MKSOCK] = "mksock",
  41. [TOMOYO_TYPE_IOCTL] = "ioctl",
  42. [TOMOYO_TYPE_CHMOD] = "chmod",
  43. [TOMOYO_TYPE_CHOWN] = "chown",
  44. [TOMOYO_TYPE_CHGRP] = "chgrp",
  45. };
  46. static const u8 tomoyo_p2mac[TOMOYO_MAX_PATH_OPERATION] = {
  47. [TOMOYO_TYPE_READ_WRITE] = TOMOYO_MAC_FILE_OPEN,
  48. [TOMOYO_TYPE_EXECUTE] = TOMOYO_MAC_FILE_EXECUTE,
  49. [TOMOYO_TYPE_READ] = TOMOYO_MAC_FILE_OPEN,
  50. [TOMOYO_TYPE_WRITE] = TOMOYO_MAC_FILE_OPEN,
  51. [TOMOYO_TYPE_UNLINK] = TOMOYO_MAC_FILE_UNLINK,
  52. [TOMOYO_TYPE_RMDIR] = TOMOYO_MAC_FILE_RMDIR,
  53. [TOMOYO_TYPE_TRUNCATE] = TOMOYO_MAC_FILE_TRUNCATE,
  54. [TOMOYO_TYPE_SYMLINK] = TOMOYO_MAC_FILE_SYMLINK,
  55. [TOMOYO_TYPE_REWRITE] = TOMOYO_MAC_FILE_REWRITE,
  56. [TOMOYO_TYPE_CHROOT] = TOMOYO_MAC_FILE_CHROOT,
  57. [TOMOYO_TYPE_UMOUNT] = TOMOYO_MAC_FILE_UMOUNT,
  58. };
  59. static const u8 tomoyo_pnnn2mac[TOMOYO_MAX_MKDEV_OPERATION] = {
  60. [TOMOYO_TYPE_MKBLOCK] = TOMOYO_MAC_FILE_MKBLOCK,
  61. [TOMOYO_TYPE_MKCHAR] = TOMOYO_MAC_FILE_MKCHAR,
  62. };
  63. static const u8 tomoyo_pp2mac[TOMOYO_MAX_PATH2_OPERATION] = {
  64. [TOMOYO_TYPE_LINK] = TOMOYO_MAC_FILE_LINK,
  65. [TOMOYO_TYPE_RENAME] = TOMOYO_MAC_FILE_RENAME,
  66. [TOMOYO_TYPE_PIVOT_ROOT] = TOMOYO_MAC_FILE_PIVOT_ROOT,
  67. };
  68. static const u8 tomoyo_pn2mac[TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  69. [TOMOYO_TYPE_CREATE] = TOMOYO_MAC_FILE_CREATE,
  70. [TOMOYO_TYPE_MKDIR] = TOMOYO_MAC_FILE_MKDIR,
  71. [TOMOYO_TYPE_MKFIFO] = TOMOYO_MAC_FILE_MKFIFO,
  72. [TOMOYO_TYPE_MKSOCK] = TOMOYO_MAC_FILE_MKSOCK,
  73. [TOMOYO_TYPE_IOCTL] = TOMOYO_MAC_FILE_IOCTL,
  74. [TOMOYO_TYPE_CHMOD] = TOMOYO_MAC_FILE_CHMOD,
  75. [TOMOYO_TYPE_CHOWN] = TOMOYO_MAC_FILE_CHOWN,
  76. [TOMOYO_TYPE_CHGRP] = TOMOYO_MAC_FILE_CHGRP,
  77. };
  78. void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
  79. {
  80. if (!ptr)
  81. return;
  82. if (ptr->is_group)
  83. tomoyo_put_group(ptr->group);
  84. else
  85. tomoyo_put_name(ptr->filename);
  86. }
  87. const struct tomoyo_path_info *
  88. tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  89. const struct tomoyo_name_union *ptr)
  90. {
  91. if (ptr->is_group)
  92. return tomoyo_path_matches_group(name, ptr->group);
  93. if (tomoyo_path_matches_pattern(name, ptr->filename))
  94. return ptr->filename;
  95. return NULL;
  96. }
  97. void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
  98. {
  99. if (ptr && ptr->is_group)
  100. tomoyo_put_group(ptr->group);
  101. }
  102. bool tomoyo_compare_number_union(const unsigned long value,
  103. const struct tomoyo_number_union *ptr)
  104. {
  105. if (ptr->is_group)
  106. return tomoyo_number_matches_group(value, value, ptr->group);
  107. return value >= ptr->values[0] && value <= ptr->values[1];
  108. }
  109. static void tomoyo_add_slash(struct tomoyo_path_info *buf)
  110. {
  111. if (buf->is_dir)
  112. return;
  113. /*
  114. * This is OK because tomoyo_encode() reserves space for appending "/".
  115. */
  116. strcat((char *) buf->name, "/");
  117. tomoyo_fill_path_info(buf);
  118. }
  119. /**
  120. * tomoyo_strendswith - Check whether the token ends with the given token.
  121. *
  122. * @name: The token to check.
  123. * @tail: The token to find.
  124. *
  125. * Returns true if @name ends with @tail, false otherwise.
  126. */
  127. static bool tomoyo_strendswith(const char *name, const char *tail)
  128. {
  129. int len;
  130. if (!name || !tail)
  131. return false;
  132. len = strlen(name) - strlen(tail);
  133. return len >= 0 && !strcmp(name + len, tail);
  134. }
  135. /**
  136. * tomoyo_get_realpath - Get realpath.
  137. *
  138. * @buf: Pointer to "struct tomoyo_path_info".
  139. * @path: Pointer to "struct path".
  140. *
  141. * Returns true on success, false otherwise.
  142. */
  143. static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
  144. {
  145. buf->name = tomoyo_realpath_from_path(path);
  146. if (buf->name) {
  147. tomoyo_fill_path_info(buf);
  148. return true;
  149. }
  150. return false;
  151. }
  152. /**
  153. * tomoyo_audit_path_log - Audit path request log.
  154. *
  155. * @r: Pointer to "struct tomoyo_request_info".
  156. *
  157. * Returns 0 on success, negative value otherwise.
  158. */
  159. static int tomoyo_audit_path_log(struct tomoyo_request_info *r)
  160. {
  161. const char *operation = tomoyo_path_keyword[r->param.path.operation];
  162. const struct tomoyo_path_info *filename = r->param.path.filename;
  163. if (r->granted)
  164. return 0;
  165. tomoyo_warn_log(r, "%s %s", operation, filename->name);
  166. return tomoyo_supervisor(r, "allow_%s %s\n", operation,
  167. tomoyo_pattern(filename));
  168. }
  169. /**
  170. * tomoyo_audit_path2_log - Audit path/path request log.
  171. *
  172. * @r: Pointer to "struct tomoyo_request_info".
  173. *
  174. * Returns 0 on success, negative value otherwise.
  175. */
  176. static int tomoyo_audit_path2_log(struct tomoyo_request_info *r)
  177. {
  178. const char *operation = tomoyo_path2_keyword[r->param.path2.operation];
  179. const struct tomoyo_path_info *filename1 = r->param.path2.filename1;
  180. const struct tomoyo_path_info *filename2 = r->param.path2.filename2;
  181. if (r->granted)
  182. return 0;
  183. tomoyo_warn_log(r, "%s %s %s", operation, filename1->name,
  184. filename2->name);
  185. return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
  186. tomoyo_pattern(filename1),
  187. tomoyo_pattern(filename2));
  188. }
  189. /**
  190. * tomoyo_audit_mkdev_log - Audit path/number/number/number request log.
  191. *
  192. * @r: Pointer to "struct tomoyo_request_info".
  193. *
  194. * Returns 0 on success, negative value otherwise.
  195. */
  196. static int tomoyo_audit_mkdev_log(struct tomoyo_request_info *r)
  197. {
  198. const char *operation = tomoyo_mkdev_keyword[r->param.mkdev.operation];
  199. const struct tomoyo_path_info *filename = r->param.mkdev.filename;
  200. const unsigned int major = r->param.mkdev.major;
  201. const unsigned int minor = r->param.mkdev.minor;
  202. const unsigned int mode = r->param.mkdev.mode;
  203. if (r->granted)
  204. return 0;
  205. tomoyo_warn_log(r, "%s %s 0%o %u %u", operation, filename->name, mode,
  206. major, minor);
  207. return tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", operation,
  208. tomoyo_pattern(filename), mode, major, minor);
  209. }
  210. /**
  211. * tomoyo_audit_path_number_log - Audit path/number request log.
  212. *
  213. * @r: Pointer to "struct tomoyo_request_info".
  214. * @error: Error code.
  215. *
  216. * Returns 0 on success, negative value otherwise.
  217. */
  218. static int tomoyo_audit_path_number_log(struct tomoyo_request_info *r)
  219. {
  220. const u8 type = r->param.path_number.operation;
  221. u8 radix;
  222. const struct tomoyo_path_info *filename = r->param.path_number.filename;
  223. const char *operation = tomoyo_path_number_keyword[type];
  224. char buffer[64];
  225. if (r->granted)
  226. return 0;
  227. switch (type) {
  228. case TOMOYO_TYPE_CREATE:
  229. case TOMOYO_TYPE_MKDIR:
  230. case TOMOYO_TYPE_MKFIFO:
  231. case TOMOYO_TYPE_MKSOCK:
  232. case TOMOYO_TYPE_CHMOD:
  233. radix = TOMOYO_VALUE_TYPE_OCTAL;
  234. break;
  235. case TOMOYO_TYPE_IOCTL:
  236. radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
  237. break;
  238. default:
  239. radix = TOMOYO_VALUE_TYPE_DECIMAL;
  240. break;
  241. }
  242. tomoyo_print_ulong(buffer, sizeof(buffer), r->param.path_number.number,
  243. radix);
  244. tomoyo_warn_log(r, "%s %s %s", operation, filename->name, buffer);
  245. return tomoyo_supervisor(r, "allow_%s %s %s\n", operation,
  246. tomoyo_pattern(filename), buffer);
  247. }
  248. static bool tomoyo_same_globally_readable(const struct tomoyo_acl_head *a,
  249. const struct tomoyo_acl_head *b)
  250. {
  251. return container_of(a, struct tomoyo_readable_file,
  252. head)->filename ==
  253. container_of(b, struct tomoyo_readable_file,
  254. head)->filename;
  255. }
  256. /**
  257. * tomoyo_update_globally_readable_entry - Update "struct tomoyo_readable_file" list.
  258. *
  259. * @filename: Filename unconditionally permitted to open() for reading.
  260. * @is_delete: True if it is a delete request.
  261. *
  262. * Returns 0 on success, negative value otherwise.
  263. *
  264. * Caller holds tomoyo_read_lock().
  265. */
  266. static int tomoyo_update_globally_readable_entry(const char *filename,
  267. const bool is_delete)
  268. {
  269. struct tomoyo_readable_file e = { };
  270. int error;
  271. if (!tomoyo_correct_word(filename))
  272. return -EINVAL;
  273. e.filename = tomoyo_get_name(filename);
  274. if (!e.filename)
  275. return -ENOMEM;
  276. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  277. &tomoyo_policy_list
  278. [TOMOYO_ID_GLOBALLY_READABLE],
  279. tomoyo_same_globally_readable);
  280. tomoyo_put_name(e.filename);
  281. return error;
  282. }
  283. /**
  284. * tomoyo_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
  285. *
  286. * @filename: The filename to check.
  287. *
  288. * Returns true if any domain can open @filename for reading, false otherwise.
  289. *
  290. * Caller holds tomoyo_read_lock().
  291. */
  292. static bool tomoyo_globally_readable_file(const struct tomoyo_path_info *
  293. filename)
  294. {
  295. struct tomoyo_readable_file *ptr;
  296. bool found = false;
  297. list_for_each_entry_rcu(ptr, &tomoyo_policy_list
  298. [TOMOYO_ID_GLOBALLY_READABLE], head.list) {
  299. if (!ptr->head.is_deleted &&
  300. tomoyo_path_matches_pattern(filename, ptr->filename)) {
  301. found = true;
  302. break;
  303. }
  304. }
  305. return found;
  306. }
  307. /**
  308. * tomoyo_write_globally_readable - Write "struct tomoyo_readable_file" list.
  309. *
  310. * @data: String to parse.
  311. * @is_delete: True if it is a delete request.
  312. *
  313. * Returns 0 on success, negative value otherwise.
  314. *
  315. * Caller holds tomoyo_read_lock().
  316. */
  317. int tomoyo_write_globally_readable(char *data, const bool is_delete)
  318. {
  319. return tomoyo_update_globally_readable_entry(data, is_delete);
  320. }
  321. static bool tomoyo_same_pattern(const struct tomoyo_acl_head *a,
  322. const struct tomoyo_acl_head *b)
  323. {
  324. return container_of(a, struct tomoyo_no_pattern, head)->pattern ==
  325. container_of(b, struct tomoyo_no_pattern, head)->pattern;
  326. }
  327. /**
  328. * tomoyo_update_file_pattern_entry - Update "struct tomoyo_no_pattern" list.
  329. *
  330. * @pattern: Pathname pattern.
  331. * @is_delete: True if it is a delete request.
  332. *
  333. * Returns 0 on success, negative value otherwise.
  334. *
  335. * Caller holds tomoyo_read_lock().
  336. */
  337. static int tomoyo_update_file_pattern_entry(const char *pattern,
  338. const bool is_delete)
  339. {
  340. struct tomoyo_no_pattern e = { };
  341. int error;
  342. if (!tomoyo_correct_word(pattern))
  343. return -EINVAL;
  344. e.pattern = tomoyo_get_name(pattern);
  345. if (!e.pattern)
  346. return -ENOMEM;
  347. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  348. &tomoyo_policy_list[TOMOYO_ID_PATTERN],
  349. tomoyo_same_pattern);
  350. tomoyo_put_name(e.pattern);
  351. return error;
  352. }
  353. /**
  354. * tomoyo_pattern - Get patterned pathname.
  355. *
  356. * @filename: The filename to find patterned pathname.
  357. *
  358. * Returns pointer to pathname pattern if matched, @filename otherwise.
  359. *
  360. * Caller holds tomoyo_read_lock().
  361. */
  362. const char *tomoyo_pattern(const struct tomoyo_path_info *filename)
  363. {
  364. struct tomoyo_no_pattern *ptr;
  365. const struct tomoyo_path_info *pattern = NULL;
  366. list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_PATTERN],
  367. head.list) {
  368. if (ptr->head.is_deleted)
  369. continue;
  370. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  371. continue;
  372. pattern = ptr->pattern;
  373. if (tomoyo_strendswith(pattern->name, "/\\*")) {
  374. /* Do nothing. Try to find the better match. */
  375. } else {
  376. /* This would be the better match. Use this. */
  377. break;
  378. }
  379. }
  380. if (pattern)
  381. filename = pattern;
  382. return filename->name;
  383. }
  384. /**
  385. * tomoyo_write_pattern - Write "struct tomoyo_no_pattern" list.
  386. *
  387. * @data: String to parse.
  388. * @is_delete: True if it is a delete request.
  389. *
  390. * Returns 0 on success, negative value otherwise.
  391. *
  392. * Caller holds tomoyo_read_lock().
  393. */
  394. int tomoyo_write_pattern(char *data, const bool is_delete)
  395. {
  396. return tomoyo_update_file_pattern_entry(data, is_delete);
  397. }
  398. static bool tomoyo_same_no_rewrite(const struct tomoyo_acl_head *a,
  399. const struct tomoyo_acl_head *b)
  400. {
  401. return container_of(a, struct tomoyo_no_rewrite, head)->pattern
  402. == container_of(b, struct tomoyo_no_rewrite, head)
  403. ->pattern;
  404. }
  405. /**
  406. * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite" list.
  407. *
  408. * @pattern: Pathname pattern that are not rewritable by default.
  409. * @is_delete: True if it is a delete request.
  410. *
  411. * Returns 0 on success, negative value otherwise.
  412. *
  413. * Caller holds tomoyo_read_lock().
  414. */
  415. static int tomoyo_update_no_rewrite_entry(const char *pattern,
  416. const bool is_delete)
  417. {
  418. struct tomoyo_no_rewrite e = { };
  419. int error;
  420. if (!tomoyo_correct_word(pattern))
  421. return -EINVAL;
  422. e.pattern = tomoyo_get_name(pattern);
  423. if (!e.pattern)
  424. return -ENOMEM;
  425. error = tomoyo_update_policy(&e.head, sizeof(e), is_delete,
  426. &tomoyo_policy_list[TOMOYO_ID_NO_REWRITE],
  427. tomoyo_same_no_rewrite);
  428. tomoyo_put_name(e.pattern);
  429. return error;
  430. }
  431. /**
  432. * tomoyo_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
  433. *
  434. * @filename: Filename to check.
  435. *
  436. * Returns true if @filename is specified by "deny_rewrite" directive,
  437. * false otherwise.
  438. *
  439. * Caller holds tomoyo_read_lock().
  440. */
  441. static bool tomoyo_no_rewrite_file(const struct tomoyo_path_info *filename)
  442. {
  443. struct tomoyo_no_rewrite *ptr;
  444. bool found = false;
  445. list_for_each_entry_rcu(ptr, &tomoyo_policy_list[TOMOYO_ID_NO_REWRITE],
  446. head.list) {
  447. if (ptr->head.is_deleted)
  448. continue;
  449. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  450. continue;
  451. found = true;
  452. break;
  453. }
  454. return found;
  455. }
  456. /**
  457. * tomoyo_write_no_rewrite - Write "struct tomoyo_no_rewrite" list.
  458. *
  459. * @data: String to parse.
  460. * @is_delete: True if it is a delete request.
  461. *
  462. * Returns 0 on success, negative value otherwise.
  463. *
  464. * Caller holds tomoyo_read_lock().
  465. */
  466. int tomoyo_write_no_rewrite(char *data, const bool is_delete)
  467. {
  468. return tomoyo_update_no_rewrite_entry(data, is_delete);
  469. }
  470. static bool tomoyo_check_path_acl(struct tomoyo_request_info *r,
  471. const struct tomoyo_acl_info *ptr)
  472. {
  473. const struct tomoyo_path_acl *acl = container_of(ptr, typeof(*acl),
  474. head);
  475. if (acl->perm & (1 << r->param.path.operation)) {
  476. r->param.path.matched_path =
  477. tomoyo_compare_name_union(r->param.path.filename,
  478. &acl->name);
  479. return r->param.path.matched_path != NULL;
  480. }
  481. return false;
  482. }
  483. static bool tomoyo_check_path_number_acl(struct tomoyo_request_info *r,
  484. const struct tomoyo_acl_info *ptr)
  485. {
  486. const struct tomoyo_path_number_acl *acl =
  487. container_of(ptr, typeof(*acl), head);
  488. return (acl->perm & (1 << r->param.path_number.operation)) &&
  489. tomoyo_compare_number_union(r->param.path_number.number,
  490. &acl->number) &&
  491. tomoyo_compare_name_union(r->param.path_number.filename,
  492. &acl->name);
  493. }
  494. static bool tomoyo_check_path2_acl(struct tomoyo_request_info *r,
  495. const struct tomoyo_acl_info *ptr)
  496. {
  497. const struct tomoyo_path2_acl *acl =
  498. container_of(ptr, typeof(*acl), head);
  499. return (acl->perm & (1 << r->param.path2.operation)) &&
  500. tomoyo_compare_name_union(r->param.path2.filename1, &acl->name1)
  501. && tomoyo_compare_name_union(r->param.path2.filename2,
  502. &acl->name2);
  503. }
  504. static bool tomoyo_check_mkdev_acl(struct tomoyo_request_info *r,
  505. const struct tomoyo_acl_info *ptr)
  506. {
  507. const struct tomoyo_mkdev_acl *acl =
  508. container_of(ptr, typeof(*acl), head);
  509. return (acl->perm & (1 << r->param.mkdev.operation)) &&
  510. tomoyo_compare_number_union(r->param.mkdev.mode,
  511. &acl->mode) &&
  512. tomoyo_compare_number_union(r->param.mkdev.major,
  513. &acl->major) &&
  514. tomoyo_compare_number_union(r->param.mkdev.minor,
  515. &acl->minor) &&
  516. tomoyo_compare_name_union(r->param.mkdev.filename,
  517. &acl->name);
  518. }
  519. static bool tomoyo_same_path_acl(const struct tomoyo_acl_info *a,
  520. const struct tomoyo_acl_info *b)
  521. {
  522. const struct tomoyo_path_acl *p1 = container_of(a, typeof(*p1), head);
  523. const struct tomoyo_path_acl *p2 = container_of(b, typeof(*p2), head);
  524. return tomoyo_same_acl_head(&p1->head, &p2->head) &&
  525. tomoyo_same_name_union(&p1->name, &p2->name);
  526. }
  527. static bool tomoyo_merge_path_acl(struct tomoyo_acl_info *a,
  528. struct tomoyo_acl_info *b,
  529. const bool is_delete)
  530. {
  531. u16 * const a_perm = &container_of(a, struct tomoyo_path_acl, head)
  532. ->perm;
  533. u16 perm = *a_perm;
  534. const u16 b_perm = container_of(b, struct tomoyo_path_acl, head)->perm;
  535. if (is_delete) {
  536. perm &= ~b_perm;
  537. if ((perm & TOMOYO_RW_MASK) != TOMOYO_RW_MASK)
  538. perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
  539. else if (!(perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  540. perm &= ~TOMOYO_RW_MASK;
  541. } else {
  542. perm |= b_perm;
  543. if ((perm & TOMOYO_RW_MASK) == TOMOYO_RW_MASK)
  544. perm |= (1 << TOMOYO_TYPE_READ_WRITE);
  545. else if (perm & (1 << TOMOYO_TYPE_READ_WRITE))
  546. perm |= TOMOYO_RW_MASK;
  547. }
  548. *a_perm = perm;
  549. return !perm;
  550. }
  551. /**
  552. * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
  553. *
  554. * @type: Type of operation.
  555. * @filename: Filename.
  556. * @domain: Pointer to "struct tomoyo_domain_info".
  557. * @is_delete: True if it is a delete request.
  558. *
  559. * Returns 0 on success, negative value otherwise.
  560. *
  561. * Caller holds tomoyo_read_lock().
  562. */
  563. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  564. struct tomoyo_domain_info * const domain,
  565. const bool is_delete)
  566. {
  567. struct tomoyo_path_acl e = {
  568. .head.type = TOMOYO_TYPE_PATH_ACL,
  569. .perm = 1 << type
  570. };
  571. int error;
  572. if (e.perm == (1 << TOMOYO_TYPE_READ_WRITE))
  573. e.perm |= TOMOYO_RW_MASK;
  574. if (!tomoyo_parse_name_union(filename, &e.name))
  575. return -EINVAL;
  576. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  577. tomoyo_same_path_acl,
  578. tomoyo_merge_path_acl);
  579. tomoyo_put_name_union(&e.name);
  580. return error;
  581. }
  582. static bool tomoyo_same_mkdev_acl(const struct tomoyo_acl_info *a,
  583. const struct tomoyo_acl_info *b)
  584. {
  585. const struct tomoyo_mkdev_acl *p1 = container_of(a, typeof(*p1),
  586. head);
  587. const struct tomoyo_mkdev_acl *p2 = container_of(b, typeof(*p2),
  588. head);
  589. return tomoyo_same_acl_head(&p1->head, &p2->head)
  590. && tomoyo_same_name_union(&p1->name, &p2->name)
  591. && tomoyo_same_number_union(&p1->mode, &p2->mode)
  592. && tomoyo_same_number_union(&p1->major, &p2->major)
  593. && tomoyo_same_number_union(&p1->minor, &p2->minor);
  594. }
  595. static bool tomoyo_merge_mkdev_acl(struct tomoyo_acl_info *a,
  596. struct tomoyo_acl_info *b,
  597. const bool is_delete)
  598. {
  599. u8 *const a_perm = &container_of(a, struct tomoyo_mkdev_acl,
  600. head)->perm;
  601. u8 perm = *a_perm;
  602. const u8 b_perm = container_of(b, struct tomoyo_mkdev_acl, head)
  603. ->perm;
  604. if (is_delete)
  605. perm &= ~b_perm;
  606. else
  607. perm |= b_perm;
  608. *a_perm = perm;
  609. return !perm;
  610. }
  611. /**
  612. * tomoyo_update_mkdev_acl - Update "struct tomoyo_mkdev_acl" list.
  613. *
  614. * @type: Type of operation.
  615. * @filename: Filename.
  616. * @mode: Create mode.
  617. * @major: Device major number.
  618. * @minor: Device minor number.
  619. * @domain: Pointer to "struct tomoyo_domain_info".
  620. * @is_delete: True if it is a delete request.
  621. *
  622. * Returns 0 on success, negative value otherwise.
  623. *
  624. * Caller holds tomoyo_read_lock().
  625. */
  626. static int tomoyo_update_mkdev_acl(const u8 type, const char *filename,
  627. char *mode, char *major, char *minor,
  628. struct tomoyo_domain_info * const
  629. domain, const bool is_delete)
  630. {
  631. struct tomoyo_mkdev_acl e = {
  632. .head.type = TOMOYO_TYPE_MKDEV_ACL,
  633. .perm = 1 << type
  634. };
  635. int error = is_delete ? -ENOENT : -ENOMEM;
  636. if (!tomoyo_parse_name_union(filename, &e.name) ||
  637. !tomoyo_parse_number_union(mode, &e.mode) ||
  638. !tomoyo_parse_number_union(major, &e.major) ||
  639. !tomoyo_parse_number_union(minor, &e.minor))
  640. goto out;
  641. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  642. tomoyo_same_mkdev_acl,
  643. tomoyo_merge_mkdev_acl);
  644. out:
  645. tomoyo_put_name_union(&e.name);
  646. tomoyo_put_number_union(&e.mode);
  647. tomoyo_put_number_union(&e.major);
  648. tomoyo_put_number_union(&e.minor);
  649. return error;
  650. }
  651. static bool tomoyo_same_path2_acl(const struct tomoyo_acl_info *a,
  652. const struct tomoyo_acl_info *b)
  653. {
  654. const struct tomoyo_path2_acl *p1 = container_of(a, typeof(*p1), head);
  655. const struct tomoyo_path2_acl *p2 = container_of(b, typeof(*p2), head);
  656. return tomoyo_same_acl_head(&p1->head, &p2->head)
  657. && tomoyo_same_name_union(&p1->name1, &p2->name1)
  658. && tomoyo_same_name_union(&p1->name2, &p2->name2);
  659. }
  660. static bool tomoyo_merge_path2_acl(struct tomoyo_acl_info *a,
  661. struct tomoyo_acl_info *b,
  662. const bool is_delete)
  663. {
  664. u8 * const a_perm = &container_of(a, struct tomoyo_path2_acl, head)
  665. ->perm;
  666. u8 perm = *a_perm;
  667. const u8 b_perm = container_of(b, struct tomoyo_path2_acl, head)->perm;
  668. if (is_delete)
  669. perm &= ~b_perm;
  670. else
  671. perm |= b_perm;
  672. *a_perm = perm;
  673. return !perm;
  674. }
  675. /**
  676. * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
  677. *
  678. * @type: Type of operation.
  679. * @filename1: First filename.
  680. * @filename2: Second filename.
  681. * @domain: Pointer to "struct tomoyo_domain_info".
  682. * @is_delete: True if it is a delete request.
  683. *
  684. * Returns 0 on success, negative value otherwise.
  685. *
  686. * Caller holds tomoyo_read_lock().
  687. */
  688. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  689. const char *filename2,
  690. struct tomoyo_domain_info * const domain,
  691. const bool is_delete)
  692. {
  693. struct tomoyo_path2_acl e = {
  694. .head.type = TOMOYO_TYPE_PATH2_ACL,
  695. .perm = 1 << type
  696. };
  697. int error = is_delete ? -ENOENT : -ENOMEM;
  698. if (!tomoyo_parse_name_union(filename1, &e.name1) ||
  699. !tomoyo_parse_name_union(filename2, &e.name2))
  700. goto out;
  701. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  702. tomoyo_same_path2_acl,
  703. tomoyo_merge_path2_acl);
  704. out:
  705. tomoyo_put_name_union(&e.name1);
  706. tomoyo_put_name_union(&e.name2);
  707. return error;
  708. }
  709. /**
  710. * tomoyo_path_permission - Check permission for single path operation.
  711. *
  712. * @r: Pointer to "struct tomoyo_request_info".
  713. * @operation: Type of operation.
  714. * @filename: Filename to check.
  715. *
  716. * Returns 0 on success, negative value otherwise.
  717. *
  718. * Caller holds tomoyo_read_lock().
  719. */
  720. int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  721. const struct tomoyo_path_info *filename)
  722. {
  723. int error;
  724. next:
  725. r->type = tomoyo_p2mac[operation];
  726. r->mode = tomoyo_get_mode(r->profile, r->type);
  727. if (r->mode == TOMOYO_CONFIG_DISABLED)
  728. return 0;
  729. r->param_type = TOMOYO_TYPE_PATH_ACL;
  730. r->param.path.filename = filename;
  731. r->param.path.operation = operation;
  732. do {
  733. tomoyo_check_acl(r, tomoyo_check_path_acl);
  734. if (!r->granted && operation == TOMOYO_TYPE_READ &&
  735. !r->domain->ignore_global_allow_read &&
  736. tomoyo_globally_readable_file(filename))
  737. r->granted = true;
  738. error = tomoyo_audit_path_log(r);
  739. /*
  740. * Do not retry for execute request, for alias may have
  741. * changed.
  742. */
  743. } while (error == TOMOYO_RETRY_REQUEST &&
  744. operation != TOMOYO_TYPE_EXECUTE);
  745. /*
  746. * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
  747. * we need to check "allow_rewrite" permission if the filename is
  748. * specified by "deny_rewrite" keyword.
  749. */
  750. if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
  751. tomoyo_no_rewrite_file(filename)) {
  752. operation = TOMOYO_TYPE_REWRITE;
  753. goto next;
  754. }
  755. return error;
  756. }
  757. static bool tomoyo_same_path_number_acl(const struct tomoyo_acl_info *a,
  758. const struct tomoyo_acl_info *b)
  759. {
  760. const struct tomoyo_path_number_acl *p1 = container_of(a, typeof(*p1),
  761. head);
  762. const struct tomoyo_path_number_acl *p2 = container_of(b, typeof(*p2),
  763. head);
  764. return tomoyo_same_acl_head(&p1->head, &p2->head)
  765. && tomoyo_same_name_union(&p1->name, &p2->name)
  766. && tomoyo_same_number_union(&p1->number, &p2->number);
  767. }
  768. static bool tomoyo_merge_path_number_acl(struct tomoyo_acl_info *a,
  769. struct tomoyo_acl_info *b,
  770. const bool is_delete)
  771. {
  772. u8 * const a_perm = &container_of(a, struct tomoyo_path_number_acl,
  773. head)->perm;
  774. u8 perm = *a_perm;
  775. const u8 b_perm = container_of(b, struct tomoyo_path_number_acl, head)
  776. ->perm;
  777. if (is_delete)
  778. perm &= ~b_perm;
  779. else
  780. perm |= b_perm;
  781. *a_perm = perm;
  782. return !perm;
  783. }
  784. /**
  785. * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
  786. *
  787. * @type: Type of operation.
  788. * @filename: Filename.
  789. * @number: Number.
  790. * @domain: Pointer to "struct tomoyo_domain_info".
  791. * @is_delete: True if it is a delete request.
  792. *
  793. * Returns 0 on success, negative value otherwise.
  794. */
  795. static int tomoyo_update_path_number_acl(const u8 type, const char *filename,
  796. char *number,
  797. struct tomoyo_domain_info * const
  798. domain,
  799. const bool is_delete)
  800. {
  801. struct tomoyo_path_number_acl e = {
  802. .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
  803. .perm = 1 << type
  804. };
  805. int error = is_delete ? -ENOENT : -ENOMEM;
  806. if (!tomoyo_parse_name_union(filename, &e.name))
  807. return -EINVAL;
  808. if (!tomoyo_parse_number_union(number, &e.number))
  809. goto out;
  810. error = tomoyo_update_domain(&e.head, sizeof(e), is_delete, domain,
  811. tomoyo_same_path_number_acl,
  812. tomoyo_merge_path_number_acl);
  813. out:
  814. tomoyo_put_name_union(&e.name);
  815. tomoyo_put_number_union(&e.number);
  816. return error;
  817. }
  818. /**
  819. * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  820. *
  821. * @type: Type of operation.
  822. * @path: Pointer to "struct path".
  823. * @number: Number.
  824. *
  825. * Returns 0 on success, negative value otherwise.
  826. */
  827. int tomoyo_path_number_perm(const u8 type, struct path *path,
  828. unsigned long number)
  829. {
  830. struct tomoyo_request_info r;
  831. int error = -ENOMEM;
  832. struct tomoyo_path_info buf;
  833. int idx;
  834. if (tomoyo_init_request_info(&r, NULL, tomoyo_pn2mac[type])
  835. == TOMOYO_CONFIG_DISABLED || !path->mnt || !path->dentry)
  836. return 0;
  837. idx = tomoyo_read_lock();
  838. if (!tomoyo_get_realpath(&buf, path))
  839. goto out;
  840. if (type == TOMOYO_TYPE_MKDIR)
  841. tomoyo_add_slash(&buf);
  842. r.param_type = TOMOYO_TYPE_PATH_NUMBER_ACL;
  843. r.param.path_number.operation = type;
  844. r.param.path_number.filename = &buf;
  845. r.param.path_number.number = number;
  846. do {
  847. tomoyo_check_acl(&r, tomoyo_check_path_number_acl);
  848. error = tomoyo_audit_path_number_log(&r);
  849. } while (error == TOMOYO_RETRY_REQUEST);
  850. kfree(buf.name);
  851. out:
  852. tomoyo_read_unlock(idx);
  853. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  854. error = 0;
  855. return error;
  856. }
  857. /**
  858. * tomoyo_check_open_permission - Check permission for "read" and "write".
  859. *
  860. * @domain: Pointer to "struct tomoyo_domain_info".
  861. * @path: Pointer to "struct path".
  862. * @flag: Flags for open().
  863. *
  864. * Returns 0 on success, negative value otherwise.
  865. */
  866. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  867. struct path *path, const int flag)
  868. {
  869. const u8 acc_mode = ACC_MODE(flag);
  870. int error = 0;
  871. struct tomoyo_path_info buf;
  872. struct tomoyo_request_info r;
  873. int idx;
  874. if (!path->mnt ||
  875. (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode)))
  876. return 0;
  877. buf.name = NULL;
  878. r.mode = TOMOYO_CONFIG_DISABLED;
  879. idx = tomoyo_read_lock();
  880. /*
  881. * If the filename is specified by "deny_rewrite" keyword,
  882. * we need to check "allow_rewrite" permission when the filename is not
  883. * opened for append mode or the filename is truncated at open time.
  884. */
  885. if ((acc_mode & MAY_WRITE) && !(flag & O_APPEND)
  886. && tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_REWRITE)
  887. != TOMOYO_CONFIG_DISABLED) {
  888. if (!tomoyo_get_realpath(&buf, path)) {
  889. error = -ENOMEM;
  890. goto out;
  891. }
  892. if (tomoyo_no_rewrite_file(&buf))
  893. error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE,
  894. &buf);
  895. }
  896. if (!error && acc_mode &&
  897. tomoyo_init_request_info(&r, domain, TOMOYO_MAC_FILE_OPEN)
  898. != TOMOYO_CONFIG_DISABLED) {
  899. u8 operation;
  900. if (!buf.name && !tomoyo_get_realpath(&buf, path)) {
  901. error = -ENOMEM;
  902. goto out;
  903. }
  904. if (acc_mode == (MAY_READ | MAY_WRITE))
  905. operation = TOMOYO_TYPE_READ_WRITE;
  906. else if (acc_mode == MAY_READ)
  907. operation = TOMOYO_TYPE_READ;
  908. else
  909. operation = TOMOYO_TYPE_WRITE;
  910. error = tomoyo_path_permission(&r, operation, &buf);
  911. }
  912. out:
  913. kfree(buf.name);
  914. tomoyo_read_unlock(idx);
  915. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  916. error = 0;
  917. return error;
  918. }
  919. /**
  920. * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
  921. *
  922. * @operation: Type of operation.
  923. * @path: Pointer to "struct path".
  924. *
  925. * Returns 0 on success, negative value otherwise.
  926. */
  927. int tomoyo_path_perm(const u8 operation, struct path *path)
  928. {
  929. int error = -ENOMEM;
  930. struct tomoyo_path_info buf;
  931. struct tomoyo_request_info r;
  932. int idx;
  933. if (!path->mnt)
  934. return 0;
  935. if (tomoyo_init_request_info(&r, NULL, tomoyo_p2mac[operation])
  936. == TOMOYO_CONFIG_DISABLED)
  937. return 0;
  938. buf.name = NULL;
  939. idx = tomoyo_read_lock();
  940. if (!tomoyo_get_realpath(&buf, path))
  941. goto out;
  942. switch (operation) {
  943. case TOMOYO_TYPE_REWRITE:
  944. if (!tomoyo_no_rewrite_file(&buf)) {
  945. error = 0;
  946. goto out;
  947. }
  948. break;
  949. case TOMOYO_TYPE_RMDIR:
  950. case TOMOYO_TYPE_CHROOT:
  951. tomoyo_add_slash(&buf);
  952. break;
  953. }
  954. error = tomoyo_path_permission(&r, operation, &buf);
  955. out:
  956. kfree(buf.name);
  957. tomoyo_read_unlock(idx);
  958. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  959. error = 0;
  960. return error;
  961. }
  962. /**
  963. * tomoyo_mkdev_perm - Check permission for "mkblock" and "mkchar".
  964. *
  965. * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
  966. * @path: Pointer to "struct path".
  967. * @mode: Create mode.
  968. * @dev: Device number.
  969. *
  970. * Returns 0 on success, negative value otherwise.
  971. */
  972. int tomoyo_mkdev_perm(const u8 operation, struct path *path,
  973. const unsigned int mode, unsigned int dev)
  974. {
  975. struct tomoyo_request_info r;
  976. int error = -ENOMEM;
  977. struct tomoyo_path_info buf;
  978. int idx;
  979. if (!path->mnt ||
  980. tomoyo_init_request_info(&r, NULL, tomoyo_pnnn2mac[operation])
  981. == TOMOYO_CONFIG_DISABLED)
  982. return 0;
  983. idx = tomoyo_read_lock();
  984. error = -ENOMEM;
  985. if (tomoyo_get_realpath(&buf, path)) {
  986. dev = new_decode_dev(dev);
  987. r.param_type = TOMOYO_TYPE_MKDEV_ACL;
  988. r.param.mkdev.filename = &buf;
  989. r.param.mkdev.operation = operation;
  990. r.param.mkdev.mode = mode;
  991. r.param.mkdev.major = MAJOR(dev);
  992. r.param.mkdev.minor = MINOR(dev);
  993. tomoyo_check_acl(&r, tomoyo_check_mkdev_acl);
  994. error = tomoyo_audit_mkdev_log(&r);
  995. kfree(buf.name);
  996. }
  997. tomoyo_read_unlock(idx);
  998. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  999. error = 0;
  1000. return error;
  1001. }
  1002. /**
  1003. * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
  1004. *
  1005. * @operation: Type of operation.
  1006. * @path1: Pointer to "struct path".
  1007. * @path2: Pointer to "struct path".
  1008. *
  1009. * Returns 0 on success, negative value otherwise.
  1010. */
  1011. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  1012. struct path *path2)
  1013. {
  1014. int error = -ENOMEM;
  1015. struct tomoyo_path_info buf1;
  1016. struct tomoyo_path_info buf2;
  1017. struct tomoyo_request_info r;
  1018. int idx;
  1019. if (!path1->mnt || !path2->mnt ||
  1020. tomoyo_init_request_info(&r, NULL, tomoyo_pp2mac[operation])
  1021. == TOMOYO_CONFIG_DISABLED)
  1022. return 0;
  1023. buf1.name = NULL;
  1024. buf2.name = NULL;
  1025. idx = tomoyo_read_lock();
  1026. if (!tomoyo_get_realpath(&buf1, path1) ||
  1027. !tomoyo_get_realpath(&buf2, path2))
  1028. goto out;
  1029. switch (operation) {
  1030. struct dentry *dentry;
  1031. case TOMOYO_TYPE_RENAME:
  1032. case TOMOYO_TYPE_LINK:
  1033. dentry = path1->dentry;
  1034. if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
  1035. break;
  1036. /* fall through */
  1037. case TOMOYO_TYPE_PIVOT_ROOT:
  1038. tomoyo_add_slash(&buf1);
  1039. tomoyo_add_slash(&buf2);
  1040. break;
  1041. }
  1042. r.param_type = TOMOYO_TYPE_PATH2_ACL;
  1043. r.param.path2.operation = operation;
  1044. r.param.path2.filename1 = &buf1;
  1045. r.param.path2.filename2 = &buf2;
  1046. do {
  1047. tomoyo_check_acl(&r, tomoyo_check_path2_acl);
  1048. error = tomoyo_audit_path2_log(&r);
  1049. } while (error == TOMOYO_RETRY_REQUEST);
  1050. out:
  1051. kfree(buf1.name);
  1052. kfree(buf2.name);
  1053. tomoyo_read_unlock(idx);
  1054. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1055. error = 0;
  1056. return error;
  1057. }
  1058. /**
  1059. * tomoyo_write_file - Update file related list.
  1060. *
  1061. * @data: String to parse.
  1062. * @domain: Pointer to "struct tomoyo_domain_info".
  1063. * @is_delete: True if it is a delete request.
  1064. *
  1065. * Returns 0 on success, negative value otherwise.
  1066. *
  1067. * Caller holds tomoyo_read_lock().
  1068. */
  1069. int tomoyo_write_file(char *data, struct tomoyo_domain_info *domain,
  1070. const bool is_delete)
  1071. {
  1072. char *w[5];
  1073. u8 type;
  1074. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
  1075. return -EINVAL;
  1076. if (strncmp(w[0], "allow_", 6))
  1077. goto out;
  1078. w[0] += 6;
  1079. for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
  1080. if (strcmp(w[0], tomoyo_path_keyword[type]))
  1081. continue;
  1082. return tomoyo_update_path_acl(type, w[1], domain, is_delete);
  1083. }
  1084. if (!w[2][0])
  1085. goto out;
  1086. for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
  1087. if (strcmp(w[0], tomoyo_path2_keyword[type]))
  1088. continue;
  1089. return tomoyo_update_path2_acl(type, w[1], w[2], domain,
  1090. is_delete);
  1091. }
  1092. for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
  1093. if (strcmp(w[0], tomoyo_path_number_keyword[type]))
  1094. continue;
  1095. return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
  1096. is_delete);
  1097. }
  1098. if (!w[3][0] || !w[4][0])
  1099. goto out;
  1100. for (type = 0; type < TOMOYO_MAX_MKDEV_OPERATION; type++) {
  1101. if (strcmp(w[0], tomoyo_mkdev_keyword[type]))
  1102. continue;
  1103. return tomoyo_update_mkdev_acl(type, w[1], w[2], w[3],
  1104. w[4], domain, is_delete);
  1105. }
  1106. out:
  1107. return -EINVAL;
  1108. }