file.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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. static 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. static const char *tomoyo_path_number3_keyword
  26. [TOMOYO_MAX_PATH_NUMBER3_OPERATION] = {
  27. [TOMOYO_TYPE_MKBLOCK] = "mkblock",
  28. [TOMOYO_TYPE_MKCHAR] = "mkchar",
  29. };
  30. /* Keyword array for operations with two pathnames. */
  31. static const char *tomoyo_path2_keyword[TOMOYO_MAX_PATH2_OPERATION] = {
  32. [TOMOYO_TYPE_LINK] = "link",
  33. [TOMOYO_TYPE_RENAME] = "rename",
  34. [TOMOYO_TYPE_PIVOT_ROOT] = "pivot_root",
  35. };
  36. /* Keyword array for operations with one pathname and one number. */
  37. static const char *tomoyo_path_number_keyword
  38. [TOMOYO_MAX_PATH_NUMBER_OPERATION] = {
  39. [TOMOYO_TYPE_CREATE] = "create",
  40. [TOMOYO_TYPE_MKDIR] = "mkdir",
  41. [TOMOYO_TYPE_MKFIFO] = "mkfifo",
  42. [TOMOYO_TYPE_MKSOCK] = "mksock",
  43. [TOMOYO_TYPE_IOCTL] = "ioctl",
  44. [TOMOYO_TYPE_CHMOD] = "chmod",
  45. [TOMOYO_TYPE_CHOWN] = "chown",
  46. [TOMOYO_TYPE_CHGRP] = "chgrp",
  47. };
  48. void tomoyo_put_name_union(struct tomoyo_name_union *ptr)
  49. {
  50. if (!ptr)
  51. return;
  52. if (ptr->is_group)
  53. tomoyo_put_path_group(ptr->group);
  54. else
  55. tomoyo_put_name(ptr->filename);
  56. }
  57. bool tomoyo_compare_name_union(const struct tomoyo_path_info *name,
  58. const struct tomoyo_name_union *ptr)
  59. {
  60. if (ptr->is_group)
  61. return tomoyo_path_matches_group(name, ptr->group);
  62. return tomoyo_path_matches_pattern(name, ptr->filename);
  63. }
  64. void tomoyo_put_number_union(struct tomoyo_number_union *ptr)
  65. {
  66. if (ptr && ptr->is_group)
  67. tomoyo_put_number_group(ptr->group);
  68. }
  69. bool tomoyo_compare_number_union(const unsigned long value,
  70. const struct tomoyo_number_union *ptr)
  71. {
  72. if (ptr->is_group)
  73. return tomoyo_number_matches_group(value, value, ptr->group);
  74. return value >= ptr->values[0] && value <= ptr->values[1];
  75. }
  76. /**
  77. * tomoyo_path2keyword - Get the name of single path operation.
  78. *
  79. * @operation: Type of operation.
  80. *
  81. * Returns the name of single path operation.
  82. */
  83. const char *tomoyo_path2keyword(const u8 operation)
  84. {
  85. return (operation < TOMOYO_MAX_PATH_OPERATION)
  86. ? tomoyo_path_keyword[operation] : NULL;
  87. }
  88. /**
  89. * tomoyo_path_number32keyword - Get the name of path/number/number/number operations.
  90. *
  91. * @operation: Type of operation.
  92. *
  93. * Returns the name of path/number/number/number operation.
  94. */
  95. const char *tomoyo_path_number32keyword(const u8 operation)
  96. {
  97. return (operation < TOMOYO_MAX_PATH_NUMBER3_OPERATION)
  98. ? tomoyo_path_number3_keyword[operation] : NULL;
  99. }
  100. /**
  101. * tomoyo_path22keyword - Get the name of double path operation.
  102. *
  103. * @operation: Type of operation.
  104. *
  105. * Returns the name of double path operation.
  106. */
  107. const char *tomoyo_path22keyword(const u8 operation)
  108. {
  109. return (operation < TOMOYO_MAX_PATH2_OPERATION)
  110. ? tomoyo_path2_keyword[operation] : NULL;
  111. }
  112. /**
  113. * tomoyo_path_number2keyword - Get the name of path/number operations.
  114. *
  115. * @operation: Type of operation.
  116. *
  117. * Returns the name of path/number operation.
  118. */
  119. const char *tomoyo_path_number2keyword(const u8 operation)
  120. {
  121. return (operation < TOMOYO_MAX_PATH_NUMBER_OPERATION)
  122. ? tomoyo_path_number_keyword[operation] : NULL;
  123. }
  124. static void tomoyo_add_slash(struct tomoyo_path_info *buf)
  125. {
  126. if (buf->is_dir)
  127. return;
  128. /*
  129. * This is OK because tomoyo_encode() reserves space for appending "/".
  130. */
  131. strcat((char *) buf->name, "/");
  132. tomoyo_fill_path_info(buf);
  133. }
  134. /**
  135. * tomoyo_strendswith - Check whether the token ends with the given token.
  136. *
  137. * @name: The token to check.
  138. * @tail: The token to find.
  139. *
  140. * Returns true if @name ends with @tail, false otherwise.
  141. */
  142. static bool tomoyo_strendswith(const char *name, const char *tail)
  143. {
  144. int len;
  145. if (!name || !tail)
  146. return false;
  147. len = strlen(name) - strlen(tail);
  148. return len >= 0 && !strcmp(name + len, tail);
  149. }
  150. /**
  151. * tomoyo_get_realpath - Get realpath.
  152. *
  153. * @buf: Pointer to "struct tomoyo_path_info".
  154. * @path: Pointer to "struct path".
  155. *
  156. * Returns true on success, false otherwise.
  157. */
  158. static bool tomoyo_get_realpath(struct tomoyo_path_info *buf, struct path *path)
  159. {
  160. buf->name = tomoyo_realpath_from_path(path);
  161. if (buf->name) {
  162. tomoyo_fill_path_info(buf);
  163. return true;
  164. }
  165. return false;
  166. }
  167. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  168. const char *filename2,
  169. struct tomoyo_domain_info *const domain,
  170. const bool is_delete);
  171. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  172. struct tomoyo_domain_info *const domain,
  173. const bool is_delete);
  174. /*
  175. * tomoyo_globally_readable_list is used for holding list of pathnames which
  176. * are by default allowed to be open()ed for reading by any process.
  177. *
  178. * An entry is added by
  179. *
  180. * # echo 'allow_read /lib/libc-2.5.so' > \
  181. * /sys/kernel/security/tomoyo/exception_policy
  182. *
  183. * and is deleted by
  184. *
  185. * # echo 'delete allow_read /lib/libc-2.5.so' > \
  186. * /sys/kernel/security/tomoyo/exception_policy
  187. *
  188. * and all entries are retrieved by
  189. *
  190. * # grep ^allow_read /sys/kernel/security/tomoyo/exception_policy
  191. *
  192. * In the example above, any process is allowed to
  193. * open("/lib/libc-2.5.so", O_RDONLY).
  194. * One exception is, if the domain which current process belongs to is marked
  195. * as "ignore_global_allow_read", current process can't do so unless explicitly
  196. * given "allow_read /lib/libc-2.5.so" to the domain which current process
  197. * belongs to.
  198. */
  199. LIST_HEAD(tomoyo_globally_readable_list);
  200. /**
  201. * tomoyo_update_globally_readable_entry - Update "struct tomoyo_globally_readable_file_entry" list.
  202. *
  203. * @filename: Filename unconditionally permitted to open() for reading.
  204. * @is_delete: True if it is a delete request.
  205. *
  206. * Returns 0 on success, negative value otherwise.
  207. *
  208. * Caller holds tomoyo_read_lock().
  209. */
  210. static int tomoyo_update_globally_readable_entry(const char *filename,
  211. const bool is_delete)
  212. {
  213. struct tomoyo_globally_readable_file_entry *ptr;
  214. struct tomoyo_globally_readable_file_entry e = { };
  215. int error = is_delete ? -ENOENT : -ENOMEM;
  216. if (!tomoyo_is_correct_word(filename))
  217. return -EINVAL;
  218. e.filename = tomoyo_get_name(filename);
  219. if (!e.filename)
  220. return -ENOMEM;
  221. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  222. goto out;
  223. list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
  224. if (ptr->filename != e.filename)
  225. continue;
  226. ptr->is_deleted = is_delete;
  227. error = 0;
  228. break;
  229. }
  230. if (!is_delete && error) {
  231. struct tomoyo_globally_readable_file_entry *entry =
  232. tomoyo_commit_ok(&e, sizeof(e));
  233. if (entry) {
  234. list_add_tail_rcu(&entry->list,
  235. &tomoyo_globally_readable_list);
  236. error = 0;
  237. }
  238. }
  239. mutex_unlock(&tomoyo_policy_lock);
  240. out:
  241. tomoyo_put_name(e.filename);
  242. return error;
  243. }
  244. /**
  245. * tomoyo_is_globally_readable_file - Check if the file is unconditionnaly permitted to be open()ed for reading.
  246. *
  247. * @filename: The filename to check.
  248. *
  249. * Returns true if any domain can open @filename for reading, false otherwise.
  250. *
  251. * Caller holds tomoyo_read_lock().
  252. */
  253. static bool tomoyo_is_globally_readable_file(const struct tomoyo_path_info *
  254. filename)
  255. {
  256. struct tomoyo_globally_readable_file_entry *ptr;
  257. bool found = false;
  258. list_for_each_entry_rcu(ptr, &tomoyo_globally_readable_list, list) {
  259. if (!ptr->is_deleted &&
  260. tomoyo_path_matches_pattern(filename, ptr->filename)) {
  261. found = true;
  262. break;
  263. }
  264. }
  265. return found;
  266. }
  267. /**
  268. * tomoyo_write_globally_readable_policy - Write "struct tomoyo_globally_readable_file_entry" list.
  269. *
  270. * @data: String to parse.
  271. * @is_delete: True if it is a delete request.
  272. *
  273. * Returns 0 on success, negative value otherwise.
  274. *
  275. * Caller holds tomoyo_read_lock().
  276. */
  277. int tomoyo_write_globally_readable_policy(char *data, const bool is_delete)
  278. {
  279. return tomoyo_update_globally_readable_entry(data, is_delete);
  280. }
  281. /**
  282. * tomoyo_read_globally_readable_policy - Read "struct tomoyo_globally_readable_file_entry" list.
  283. *
  284. * @head: Pointer to "struct tomoyo_io_buffer".
  285. *
  286. * Returns true on success, false otherwise.
  287. *
  288. * Caller holds tomoyo_read_lock().
  289. */
  290. bool tomoyo_read_globally_readable_policy(struct tomoyo_io_buffer *head)
  291. {
  292. struct list_head *pos;
  293. bool done = true;
  294. list_for_each_cookie(pos, head->read_var2,
  295. &tomoyo_globally_readable_list) {
  296. struct tomoyo_globally_readable_file_entry *ptr;
  297. ptr = list_entry(pos,
  298. struct tomoyo_globally_readable_file_entry,
  299. list);
  300. if (ptr->is_deleted)
  301. continue;
  302. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_ALLOW_READ "%s\n",
  303. ptr->filename->name);
  304. if (!done)
  305. break;
  306. }
  307. return done;
  308. }
  309. /* tomoyo_pattern_list is used for holding list of pathnames which are used for
  310. * converting pathnames to pathname patterns during learning mode.
  311. *
  312. * An entry is added by
  313. *
  314. * # echo 'file_pattern /proc/\$/mounts' > \
  315. * /sys/kernel/security/tomoyo/exception_policy
  316. *
  317. * and is deleted by
  318. *
  319. * # echo 'delete file_pattern /proc/\$/mounts' > \
  320. * /sys/kernel/security/tomoyo/exception_policy
  321. *
  322. * and all entries are retrieved by
  323. *
  324. * # grep ^file_pattern /sys/kernel/security/tomoyo/exception_policy
  325. *
  326. * In the example above, if a process which belongs to a domain which is in
  327. * learning mode requested open("/proc/1/mounts", O_RDONLY),
  328. * "allow_read /proc/\$/mounts" is automatically added to the domain which that
  329. * process belongs to.
  330. *
  331. * It is not a desirable behavior that we have to use /proc/\$/ instead of
  332. * /proc/self/ when current process needs to access only current process's
  333. * information. As of now, LSM version of TOMOYO is using __d_path() for
  334. * calculating pathname. Non LSM version of TOMOYO is using its own function
  335. * which pretends as if /proc/self/ is not a symlink; so that we can forbid
  336. * current process from accessing other process's information.
  337. */
  338. LIST_HEAD(tomoyo_pattern_list);
  339. /**
  340. * tomoyo_update_file_pattern_entry - Update "struct tomoyo_pattern_entry" list.
  341. *
  342. * @pattern: Pathname pattern.
  343. * @is_delete: True if it is a delete request.
  344. *
  345. * Returns 0 on success, negative value otherwise.
  346. *
  347. * Caller holds tomoyo_read_lock().
  348. */
  349. static int tomoyo_update_file_pattern_entry(const char *pattern,
  350. const bool is_delete)
  351. {
  352. struct tomoyo_pattern_entry *ptr;
  353. struct tomoyo_pattern_entry e = { };
  354. int error = is_delete ? -ENOENT : -ENOMEM;
  355. if (!tomoyo_is_correct_word(pattern))
  356. return -EINVAL;
  357. e.pattern = tomoyo_get_name(pattern);
  358. if (!e.pattern)
  359. return error;
  360. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  361. goto out;
  362. list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
  363. if (e.pattern != ptr->pattern)
  364. continue;
  365. ptr->is_deleted = is_delete;
  366. error = 0;
  367. break;
  368. }
  369. if (!is_delete && error) {
  370. struct tomoyo_pattern_entry *entry =
  371. tomoyo_commit_ok(&e, sizeof(e));
  372. if (entry) {
  373. list_add_tail_rcu(&entry->list, &tomoyo_pattern_list);
  374. error = 0;
  375. }
  376. }
  377. mutex_unlock(&tomoyo_policy_lock);
  378. out:
  379. tomoyo_put_name(e.pattern);
  380. return error;
  381. }
  382. /**
  383. * tomoyo_file_pattern - Get patterned pathname.
  384. *
  385. * @filename: The filename to find patterned pathname.
  386. *
  387. * Returns pointer to pathname pattern if matched, @filename otherwise.
  388. *
  389. * Caller holds tomoyo_read_lock().
  390. */
  391. const char *tomoyo_file_pattern(const struct tomoyo_path_info *filename)
  392. {
  393. struct tomoyo_pattern_entry *ptr;
  394. const struct tomoyo_path_info *pattern = NULL;
  395. list_for_each_entry_rcu(ptr, &tomoyo_pattern_list, list) {
  396. if (ptr->is_deleted)
  397. continue;
  398. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  399. continue;
  400. pattern = ptr->pattern;
  401. if (tomoyo_strendswith(pattern->name, "/\\*")) {
  402. /* Do nothing. Try to find the better match. */
  403. } else {
  404. /* This would be the better match. Use this. */
  405. break;
  406. }
  407. }
  408. if (pattern)
  409. filename = pattern;
  410. return filename->name;
  411. }
  412. /**
  413. * tomoyo_write_pattern_policy - Write "struct tomoyo_pattern_entry" list.
  414. *
  415. * @data: String to parse.
  416. * @is_delete: True if it is a delete request.
  417. *
  418. * Returns 0 on success, negative value otherwise.
  419. *
  420. * Caller holds tomoyo_read_lock().
  421. */
  422. int tomoyo_write_pattern_policy(char *data, const bool is_delete)
  423. {
  424. return tomoyo_update_file_pattern_entry(data, is_delete);
  425. }
  426. /**
  427. * tomoyo_read_file_pattern - Read "struct tomoyo_pattern_entry" list.
  428. *
  429. * @head: Pointer to "struct tomoyo_io_buffer".
  430. *
  431. * Returns true on success, false otherwise.
  432. *
  433. * Caller holds tomoyo_read_lock().
  434. */
  435. bool tomoyo_read_file_pattern(struct tomoyo_io_buffer *head)
  436. {
  437. struct list_head *pos;
  438. bool done = true;
  439. list_for_each_cookie(pos, head->read_var2, &tomoyo_pattern_list) {
  440. struct tomoyo_pattern_entry *ptr;
  441. ptr = list_entry(pos, struct tomoyo_pattern_entry, list);
  442. if (ptr->is_deleted)
  443. continue;
  444. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_FILE_PATTERN
  445. "%s\n", ptr->pattern->name);
  446. if (!done)
  447. break;
  448. }
  449. return done;
  450. }
  451. /*
  452. * tomoyo_no_rewrite_list is used for holding list of pathnames which are by
  453. * default forbidden to modify already written content of a file.
  454. *
  455. * An entry is added by
  456. *
  457. * # echo 'deny_rewrite /var/log/messages' > \
  458. * /sys/kernel/security/tomoyo/exception_policy
  459. *
  460. * and is deleted by
  461. *
  462. * # echo 'delete deny_rewrite /var/log/messages' > \
  463. * /sys/kernel/security/tomoyo/exception_policy
  464. *
  465. * and all entries are retrieved by
  466. *
  467. * # grep ^deny_rewrite /sys/kernel/security/tomoyo/exception_policy
  468. *
  469. * In the example above, if a process requested to rewrite /var/log/messages ,
  470. * the process can't rewrite unless the domain which that process belongs to
  471. * has "allow_rewrite /var/log/messages" entry.
  472. *
  473. * It is not a desirable behavior that we have to add "\040(deleted)" suffix
  474. * when we want to allow rewriting already unlink()ed file. As of now,
  475. * LSM version of TOMOYO is using __d_path() for calculating pathname.
  476. * Non LSM version of TOMOYO is using its own function which doesn't append
  477. * " (deleted)" suffix if the file is already unlink()ed; so that we don't
  478. * need to worry whether the file is already unlink()ed or not.
  479. */
  480. LIST_HEAD(tomoyo_no_rewrite_list);
  481. /**
  482. * tomoyo_update_no_rewrite_entry - Update "struct tomoyo_no_rewrite_entry" list.
  483. *
  484. * @pattern: Pathname pattern that are not rewritable by default.
  485. * @is_delete: True if it is a delete request.
  486. *
  487. * Returns 0 on success, negative value otherwise.
  488. *
  489. * Caller holds tomoyo_read_lock().
  490. */
  491. static int tomoyo_update_no_rewrite_entry(const char *pattern,
  492. const bool is_delete)
  493. {
  494. struct tomoyo_no_rewrite_entry *ptr;
  495. struct tomoyo_no_rewrite_entry e = { };
  496. int error = is_delete ? -ENOENT : -ENOMEM;
  497. if (!tomoyo_is_correct_word(pattern))
  498. return -EINVAL;
  499. e.pattern = tomoyo_get_name(pattern);
  500. if (!e.pattern)
  501. return error;
  502. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  503. goto out;
  504. list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
  505. if (ptr->pattern != e.pattern)
  506. continue;
  507. ptr->is_deleted = is_delete;
  508. error = 0;
  509. break;
  510. }
  511. if (!is_delete && error) {
  512. struct tomoyo_no_rewrite_entry *entry =
  513. tomoyo_commit_ok(&e, sizeof(e));
  514. if (entry) {
  515. list_add_tail_rcu(&entry->list,
  516. &tomoyo_no_rewrite_list);
  517. error = 0;
  518. }
  519. }
  520. mutex_unlock(&tomoyo_policy_lock);
  521. out:
  522. tomoyo_put_name(e.pattern);
  523. return error;
  524. }
  525. /**
  526. * tomoyo_is_no_rewrite_file - Check if the given pathname is not permitted to be rewrited.
  527. *
  528. * @filename: Filename to check.
  529. *
  530. * Returns true if @filename is specified by "deny_rewrite" directive,
  531. * false otherwise.
  532. *
  533. * Caller holds tomoyo_read_lock().
  534. */
  535. static bool tomoyo_is_no_rewrite_file(const struct tomoyo_path_info *filename)
  536. {
  537. struct tomoyo_no_rewrite_entry *ptr;
  538. bool found = false;
  539. list_for_each_entry_rcu(ptr, &tomoyo_no_rewrite_list, list) {
  540. if (ptr->is_deleted)
  541. continue;
  542. if (!tomoyo_path_matches_pattern(filename, ptr->pattern))
  543. continue;
  544. found = true;
  545. break;
  546. }
  547. return found;
  548. }
  549. /**
  550. * tomoyo_write_no_rewrite_policy - Write "struct tomoyo_no_rewrite_entry" list.
  551. *
  552. * @data: String to parse.
  553. * @is_delete: True if it is a delete request.
  554. *
  555. * Returns 0 on success, negative value otherwise.
  556. *
  557. * Caller holds tomoyo_read_lock().
  558. */
  559. int tomoyo_write_no_rewrite_policy(char *data, const bool is_delete)
  560. {
  561. return tomoyo_update_no_rewrite_entry(data, is_delete);
  562. }
  563. /**
  564. * tomoyo_read_no_rewrite_policy - Read "struct tomoyo_no_rewrite_entry" list.
  565. *
  566. * @head: Pointer to "struct tomoyo_io_buffer".
  567. *
  568. * Returns true on success, false otherwise.
  569. *
  570. * Caller holds tomoyo_read_lock().
  571. */
  572. bool tomoyo_read_no_rewrite_policy(struct tomoyo_io_buffer *head)
  573. {
  574. struct list_head *pos;
  575. bool done = true;
  576. list_for_each_cookie(pos, head->read_var2, &tomoyo_no_rewrite_list) {
  577. struct tomoyo_no_rewrite_entry *ptr;
  578. ptr = list_entry(pos, struct tomoyo_no_rewrite_entry, list);
  579. if (ptr->is_deleted)
  580. continue;
  581. done = tomoyo_io_printf(head, TOMOYO_KEYWORD_DENY_REWRITE
  582. "%s\n", ptr->pattern->name);
  583. if (!done)
  584. break;
  585. }
  586. return done;
  587. }
  588. /**
  589. * tomoyo_update_file_acl - Update file's read/write/execute ACL.
  590. *
  591. * @perm: Permission (between 1 to 7).
  592. * @filename: Filename.
  593. * @domain: Pointer to "struct tomoyo_domain_info".
  594. * @is_delete: True if it is a delete request.
  595. *
  596. * Returns 0 on success, negative value otherwise.
  597. *
  598. * This is legacy support interface for older policy syntax.
  599. * Current policy syntax uses "allow_read/write" instead of "6",
  600. * "allow_read" instead of "4", "allow_write" instead of "2",
  601. * "allow_execute" instead of "1".
  602. *
  603. * Caller holds tomoyo_read_lock().
  604. */
  605. static int tomoyo_update_file_acl(u8 perm, const char *filename,
  606. struct tomoyo_domain_info * const domain,
  607. const bool is_delete)
  608. {
  609. if (perm > 7 || !perm) {
  610. printk(KERN_DEBUG "%s: Invalid permission '%d %s'\n",
  611. __func__, perm, filename);
  612. return -EINVAL;
  613. }
  614. if (filename[0] != '@' && tomoyo_strendswith(filename, "/"))
  615. /*
  616. * Only 'allow_mkdir' and 'allow_rmdir' are valid for
  617. * directory permissions.
  618. */
  619. return 0;
  620. if (perm & 4)
  621. tomoyo_update_path_acl(TOMOYO_TYPE_READ, filename, domain,
  622. is_delete);
  623. if (perm & 2)
  624. tomoyo_update_path_acl(TOMOYO_TYPE_WRITE, filename, domain,
  625. is_delete);
  626. if (perm & 1)
  627. tomoyo_update_path_acl(TOMOYO_TYPE_EXECUTE, filename, domain,
  628. is_delete);
  629. return 0;
  630. }
  631. /**
  632. * tomoyo_path_acl - Check permission for single path operation.
  633. *
  634. * @r: Pointer to "struct tomoyo_request_info".
  635. * @filename: Filename to check.
  636. * @perm: Permission.
  637. *
  638. * Returns 0 on success, -EPERM otherwise.
  639. *
  640. * Caller holds tomoyo_read_lock().
  641. */
  642. static int tomoyo_path_acl(const struct tomoyo_request_info *r,
  643. const struct tomoyo_path_info *filename,
  644. const u32 perm)
  645. {
  646. struct tomoyo_domain_info *domain = r->domain;
  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_path_acl *acl;
  651. if (ptr->type != TOMOYO_TYPE_PATH_ACL)
  652. continue;
  653. acl = container_of(ptr, struct tomoyo_path_acl, head);
  654. if (!(acl->perm & perm) ||
  655. !tomoyo_compare_name_union(filename, &acl->name))
  656. continue;
  657. error = 0;
  658. break;
  659. }
  660. return error;
  661. }
  662. /**
  663. * tomoyo_file_perm - Check permission for opening files.
  664. *
  665. * @r: Pointer to "struct tomoyo_request_info".
  666. * @filename: Filename to check.
  667. * @mode: Mode ("read" or "write" or "read/write" or "execute").
  668. *
  669. * Returns 0 on success, negative value otherwise.
  670. *
  671. * Caller holds tomoyo_read_lock().
  672. */
  673. static int tomoyo_file_perm(struct tomoyo_request_info *r,
  674. const struct tomoyo_path_info *filename,
  675. const u8 mode)
  676. {
  677. const char *msg = "<unknown>";
  678. int error = 0;
  679. u32 perm = 0;
  680. if (!filename)
  681. return 0;
  682. if (mode == 6) {
  683. msg = tomoyo_path2keyword(TOMOYO_TYPE_READ_WRITE);
  684. perm = 1 << TOMOYO_TYPE_READ_WRITE;
  685. } else if (mode == 4) {
  686. msg = tomoyo_path2keyword(TOMOYO_TYPE_READ);
  687. perm = 1 << TOMOYO_TYPE_READ;
  688. } else if (mode == 2) {
  689. msg = tomoyo_path2keyword(TOMOYO_TYPE_WRITE);
  690. perm = 1 << TOMOYO_TYPE_WRITE;
  691. } else if (mode == 1) {
  692. msg = tomoyo_path2keyword(TOMOYO_TYPE_EXECUTE);
  693. perm = 1 << TOMOYO_TYPE_EXECUTE;
  694. } else
  695. BUG();
  696. do {
  697. error = tomoyo_path_acl(r, filename, perm);
  698. if (error && mode == 4 && !r->domain->ignore_global_allow_read
  699. && tomoyo_is_globally_readable_file(filename))
  700. error = 0;
  701. if (!error)
  702. break;
  703. tomoyo_warn_log(r, "%s %s", msg, filename->name);
  704. error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
  705. tomoyo_file_pattern(filename));
  706. /*
  707. * Do not retry for execute request, for alias may have
  708. * changed.
  709. */
  710. } while (error == TOMOYO_RETRY_REQUEST && mode != 1);
  711. if (r->mode != TOMOYO_CONFIG_ENFORCING)
  712. error = 0;
  713. return error;
  714. }
  715. /**
  716. * tomoyo_update_path_acl - Update "struct tomoyo_path_acl" list.
  717. *
  718. * @type: Type of operation.
  719. * @filename: Filename.
  720. * @domain: Pointer to "struct tomoyo_domain_info".
  721. * @is_delete: True if it is a delete request.
  722. *
  723. * Returns 0 on success, negative value otherwise.
  724. *
  725. * Caller holds tomoyo_read_lock().
  726. */
  727. static int tomoyo_update_path_acl(const u8 type, const char *filename,
  728. struct tomoyo_domain_info *const domain,
  729. const bool is_delete)
  730. {
  731. static const u16 tomoyo_rw_mask =
  732. (1 << TOMOYO_TYPE_READ) | (1 << TOMOYO_TYPE_WRITE);
  733. const u16 perm = 1 << type;
  734. struct tomoyo_acl_info *ptr;
  735. struct tomoyo_path_acl e = {
  736. .head.type = TOMOYO_TYPE_PATH_ACL,
  737. .perm = perm
  738. };
  739. int error = is_delete ? -ENOENT : -ENOMEM;
  740. if (type == TOMOYO_TYPE_READ_WRITE)
  741. e.perm |= tomoyo_rw_mask;
  742. if (!domain)
  743. return -EINVAL;
  744. if (!tomoyo_parse_name_union(filename, &e.name))
  745. return -EINVAL;
  746. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  747. goto out;
  748. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  749. struct tomoyo_path_acl *acl =
  750. container_of(ptr, struct tomoyo_path_acl, head);
  751. if (!tomoyo_is_same_path_acl(acl, &e))
  752. continue;
  753. if (is_delete) {
  754. acl->perm &= ~perm;
  755. if ((acl->perm & tomoyo_rw_mask) != tomoyo_rw_mask)
  756. acl->perm &= ~(1 << TOMOYO_TYPE_READ_WRITE);
  757. else if (!(acl->perm & (1 << TOMOYO_TYPE_READ_WRITE)))
  758. acl->perm &= ~tomoyo_rw_mask;
  759. } else {
  760. acl->perm |= perm;
  761. if ((acl->perm & tomoyo_rw_mask) == tomoyo_rw_mask)
  762. acl->perm |= 1 << TOMOYO_TYPE_READ_WRITE;
  763. else if (acl->perm & (1 << TOMOYO_TYPE_READ_WRITE))
  764. acl->perm |= tomoyo_rw_mask;
  765. }
  766. error = 0;
  767. break;
  768. }
  769. if (!is_delete && error) {
  770. struct tomoyo_path_acl *entry =
  771. tomoyo_commit_ok(&e, sizeof(e));
  772. if (entry) {
  773. list_add_tail_rcu(&entry->head.list,
  774. &domain->acl_info_list);
  775. error = 0;
  776. }
  777. }
  778. mutex_unlock(&tomoyo_policy_lock);
  779. out:
  780. tomoyo_put_name_union(&e.name);
  781. return error;
  782. }
  783. /**
  784. * tomoyo_update_path_number3_acl - Update "struct tomoyo_path_number3_acl" list.
  785. *
  786. * @type: Type of operation.
  787. * @filename: Filename.
  788. * @mode: Create mode.
  789. * @major: Device major number.
  790. * @minor: Device minor number.
  791. * @domain: Pointer to "struct tomoyo_domain_info".
  792. * @is_delete: True if it is a delete request.
  793. *
  794. * Returns 0 on success, negative value otherwise.
  795. */
  796. static inline int tomoyo_update_path_number3_acl(const u8 type,
  797. const char *filename,
  798. char *mode,
  799. char *major, char *minor,
  800. struct tomoyo_domain_info *
  801. const domain,
  802. const bool is_delete)
  803. {
  804. const u8 perm = 1 << type;
  805. struct tomoyo_acl_info *ptr;
  806. struct tomoyo_path_number3_acl e = {
  807. .head.type = TOMOYO_TYPE_PATH_NUMBER3_ACL,
  808. .perm = perm
  809. };
  810. int error = is_delete ? -ENOENT : -ENOMEM;
  811. if (!tomoyo_parse_name_union(filename, &e.name) ||
  812. !tomoyo_parse_number_union(mode, &e.mode) ||
  813. !tomoyo_parse_number_union(major, &e.major) ||
  814. !tomoyo_parse_number_union(minor, &e.minor))
  815. goto out;
  816. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  817. goto out;
  818. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  819. struct tomoyo_path_number3_acl *acl =
  820. container_of(ptr, struct tomoyo_path_number3_acl, head);
  821. if (!tomoyo_is_same_path_number3_acl(acl, &e))
  822. continue;
  823. if (is_delete)
  824. acl->perm &= ~perm;
  825. else
  826. acl->perm |= perm;
  827. error = 0;
  828. break;
  829. }
  830. if (!is_delete && error) {
  831. struct tomoyo_path_number3_acl *entry =
  832. tomoyo_commit_ok(&e, sizeof(e));
  833. if (entry) {
  834. list_add_tail_rcu(&entry->head.list,
  835. &domain->acl_info_list);
  836. error = 0;
  837. }
  838. }
  839. mutex_unlock(&tomoyo_policy_lock);
  840. out:
  841. tomoyo_put_name_union(&e.name);
  842. tomoyo_put_number_union(&e.mode);
  843. tomoyo_put_number_union(&e.major);
  844. tomoyo_put_number_union(&e.minor);
  845. return error;
  846. }
  847. /**
  848. * tomoyo_update_path2_acl - Update "struct tomoyo_path2_acl" list.
  849. *
  850. * @type: Type of operation.
  851. * @filename1: First filename.
  852. * @filename2: Second filename.
  853. * @domain: Pointer to "struct tomoyo_domain_info".
  854. * @is_delete: True if it is a delete request.
  855. *
  856. * Returns 0 on success, negative value otherwise.
  857. *
  858. * Caller holds tomoyo_read_lock().
  859. */
  860. static int tomoyo_update_path2_acl(const u8 type, const char *filename1,
  861. const char *filename2,
  862. struct tomoyo_domain_info *const domain,
  863. const bool is_delete)
  864. {
  865. const u8 perm = 1 << type;
  866. struct tomoyo_path2_acl e = {
  867. .head.type = TOMOYO_TYPE_PATH2_ACL,
  868. .perm = perm
  869. };
  870. struct tomoyo_acl_info *ptr;
  871. int error = is_delete ? -ENOENT : -ENOMEM;
  872. if (!domain)
  873. return -EINVAL;
  874. if (!tomoyo_parse_name_union(filename1, &e.name1) ||
  875. !tomoyo_parse_name_union(filename2, &e.name2))
  876. goto out;
  877. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  878. goto out;
  879. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  880. struct tomoyo_path2_acl *acl =
  881. container_of(ptr, struct tomoyo_path2_acl, head);
  882. if (!tomoyo_is_same_path2_acl(acl, &e))
  883. continue;
  884. if (is_delete)
  885. acl->perm &= ~perm;
  886. else
  887. acl->perm |= perm;
  888. error = 0;
  889. break;
  890. }
  891. if (!is_delete && error) {
  892. struct tomoyo_path2_acl *entry =
  893. tomoyo_commit_ok(&e, sizeof(e));
  894. if (entry) {
  895. list_add_tail_rcu(&entry->head.list,
  896. &domain->acl_info_list);
  897. error = 0;
  898. }
  899. }
  900. mutex_unlock(&tomoyo_policy_lock);
  901. out:
  902. tomoyo_put_name_union(&e.name1);
  903. tomoyo_put_name_union(&e.name2);
  904. return error;
  905. }
  906. /**
  907. * tomoyo_path_number3_acl - Check permission for path/number/number/number operation.
  908. *
  909. * @r: Pointer to "struct tomoyo_request_info".
  910. * @filename: Filename to check.
  911. * @perm: Permission.
  912. * @mode: Create mode.
  913. * @major: Device major number.
  914. * @minor: Device minor number.
  915. *
  916. * Returns 0 on success, -EPERM otherwise.
  917. *
  918. * Caller holds tomoyo_read_lock().
  919. */
  920. static int tomoyo_path_number3_acl(struct tomoyo_request_info *r,
  921. const struct tomoyo_path_info *filename,
  922. const u16 perm, const unsigned int mode,
  923. const unsigned int major,
  924. const unsigned int minor)
  925. {
  926. struct tomoyo_domain_info *domain = r->domain;
  927. struct tomoyo_acl_info *ptr;
  928. int error = -EPERM;
  929. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  930. struct tomoyo_path_number3_acl *acl;
  931. if (ptr->type != TOMOYO_TYPE_PATH_NUMBER3_ACL)
  932. continue;
  933. acl = container_of(ptr, struct tomoyo_path_number3_acl, head);
  934. if (!tomoyo_compare_number_union(mode, &acl->mode))
  935. continue;
  936. if (!tomoyo_compare_number_union(major, &acl->major))
  937. continue;
  938. if (!tomoyo_compare_number_union(minor, &acl->minor))
  939. continue;
  940. if (!(acl->perm & perm))
  941. continue;
  942. if (!tomoyo_compare_name_union(filename, &acl->name))
  943. continue;
  944. error = 0;
  945. break;
  946. }
  947. return error;
  948. }
  949. /**
  950. * tomoyo_path2_acl - Check permission for double path operation.
  951. *
  952. * @r: Pointer to "struct tomoyo_request_info".
  953. * @type: Type of operation.
  954. * @filename1: First filename to check.
  955. * @filename2: Second filename to check.
  956. *
  957. * Returns 0 on success, -EPERM otherwise.
  958. *
  959. * Caller holds tomoyo_read_lock().
  960. */
  961. static int tomoyo_path2_acl(const struct tomoyo_request_info *r, const u8 type,
  962. const struct tomoyo_path_info *filename1,
  963. const struct tomoyo_path_info *filename2)
  964. {
  965. const struct tomoyo_domain_info *domain = r->domain;
  966. struct tomoyo_acl_info *ptr;
  967. const u8 perm = 1 << type;
  968. int error = -EPERM;
  969. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  970. struct tomoyo_path2_acl *acl;
  971. if (ptr->type != TOMOYO_TYPE_PATH2_ACL)
  972. continue;
  973. acl = container_of(ptr, struct tomoyo_path2_acl, head);
  974. if (!(acl->perm & perm))
  975. continue;
  976. if (!tomoyo_compare_name_union(filename1, &acl->name1))
  977. continue;
  978. if (!tomoyo_compare_name_union(filename2, &acl->name2))
  979. continue;
  980. error = 0;
  981. break;
  982. }
  983. return error;
  984. }
  985. /**
  986. * tomoyo_path_permission - Check permission for single path operation.
  987. *
  988. * @r: Pointer to "struct tomoyo_request_info".
  989. * @operation: Type of operation.
  990. * @filename: Filename to check.
  991. *
  992. * Returns 0 on success, negative value otherwise.
  993. *
  994. * Caller holds tomoyo_read_lock().
  995. */
  996. static int tomoyo_path_permission(struct tomoyo_request_info *r, u8 operation,
  997. const struct tomoyo_path_info *filename)
  998. {
  999. const char *msg;
  1000. int error;
  1001. next:
  1002. do {
  1003. error = tomoyo_path_acl(r, filename, 1 << operation);
  1004. if (!error)
  1005. break;
  1006. msg = tomoyo_path2keyword(operation);
  1007. tomoyo_warn_log(r, "%s %s", msg, filename->name);
  1008. error = tomoyo_supervisor(r, "allow_%s %s\n", msg,
  1009. tomoyo_file_pattern(filename));
  1010. } while (error == TOMOYO_RETRY_REQUEST);
  1011. if (r->mode != TOMOYO_CONFIG_ENFORCING)
  1012. error = 0;
  1013. /*
  1014. * Since "allow_truncate" doesn't imply "allow_rewrite" permission,
  1015. * we need to check "allow_rewrite" permission if the filename is
  1016. * specified by "deny_rewrite" keyword.
  1017. */
  1018. if (!error && operation == TOMOYO_TYPE_TRUNCATE &&
  1019. tomoyo_is_no_rewrite_file(filename)) {
  1020. operation = TOMOYO_TYPE_REWRITE;
  1021. goto next;
  1022. }
  1023. return error;
  1024. }
  1025. /**
  1026. * tomoyo_path_number_acl - Check permission for ioctl/chmod/chown/chgrp operation.
  1027. *
  1028. * @r: Pointer to "struct tomoyo_request_info".
  1029. * @type: Operation.
  1030. * @filename: Filename to check.
  1031. * @number: Number.
  1032. *
  1033. * Returns 0 on success, -EPERM otherwise.
  1034. *
  1035. * Caller holds tomoyo_read_lock().
  1036. */
  1037. static int tomoyo_path_number_acl(struct tomoyo_request_info *r, const u8 type,
  1038. const struct tomoyo_path_info *filename,
  1039. const unsigned long number)
  1040. {
  1041. struct tomoyo_domain_info *domain = r->domain;
  1042. struct tomoyo_acl_info *ptr;
  1043. const u8 perm = 1 << type;
  1044. int error = -EPERM;
  1045. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  1046. struct tomoyo_path_number_acl *acl;
  1047. if (ptr->type != TOMOYO_TYPE_PATH_NUMBER_ACL)
  1048. continue;
  1049. acl = container_of(ptr, struct tomoyo_path_number_acl,
  1050. head);
  1051. if (!(acl->perm & perm) ||
  1052. !tomoyo_compare_number_union(number, &acl->number) ||
  1053. !tomoyo_compare_name_union(filename, &acl->name))
  1054. continue;
  1055. error = 0;
  1056. break;
  1057. }
  1058. return error;
  1059. }
  1060. /**
  1061. * tomoyo_update_path_number_acl - Update ioctl/chmod/chown/chgrp ACL.
  1062. *
  1063. * @type: Type of operation.
  1064. * @filename: Filename.
  1065. * @number: Number.
  1066. * @domain: Pointer to "struct tomoyo_domain_info".
  1067. * @is_delete: True if it is a delete request.
  1068. *
  1069. * Returns 0 on success, negative value otherwise.
  1070. */
  1071. static inline int tomoyo_update_path_number_acl(const u8 type,
  1072. const char *filename,
  1073. char *number,
  1074. struct tomoyo_domain_info *
  1075. const domain,
  1076. const bool is_delete)
  1077. {
  1078. const u8 perm = 1 << type;
  1079. struct tomoyo_acl_info *ptr;
  1080. struct tomoyo_path_number_acl e = {
  1081. .head.type = TOMOYO_TYPE_PATH_NUMBER_ACL,
  1082. .perm = perm
  1083. };
  1084. int error = is_delete ? -ENOENT : -ENOMEM;
  1085. if (!domain)
  1086. return -EINVAL;
  1087. if (!tomoyo_parse_name_union(filename, &e.name))
  1088. return -EINVAL;
  1089. if (!tomoyo_parse_number_union(number, &e.number))
  1090. goto out;
  1091. if (mutex_lock_interruptible(&tomoyo_policy_lock))
  1092. goto out;
  1093. list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) {
  1094. struct tomoyo_path_number_acl *acl =
  1095. container_of(ptr, struct tomoyo_path_number_acl, head);
  1096. if (!tomoyo_is_same_path_number_acl(acl, &e))
  1097. continue;
  1098. if (is_delete)
  1099. acl->perm &= ~perm;
  1100. else
  1101. acl->perm |= perm;
  1102. error = 0;
  1103. break;
  1104. }
  1105. if (!is_delete && error) {
  1106. struct tomoyo_path_number_acl *entry =
  1107. tomoyo_commit_ok(&e, sizeof(e));
  1108. if (entry) {
  1109. list_add_tail_rcu(&entry->head.list,
  1110. &domain->acl_info_list);
  1111. error = 0;
  1112. }
  1113. }
  1114. mutex_unlock(&tomoyo_policy_lock);
  1115. out:
  1116. tomoyo_put_name_union(&e.name);
  1117. tomoyo_put_number_union(&e.number);
  1118. return error;
  1119. }
  1120. /**
  1121. * tomoyo_path_number_perm2 - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  1122. *
  1123. * @r: Pointer to "strct tomoyo_request_info".
  1124. * @filename: Filename to check.
  1125. * @number: Number.
  1126. *
  1127. * Returns 0 on success, negative value otherwise.
  1128. *
  1129. * Caller holds tomoyo_read_lock().
  1130. */
  1131. static int tomoyo_path_number_perm2(struct tomoyo_request_info *r,
  1132. const u8 type,
  1133. const struct tomoyo_path_info *filename,
  1134. const unsigned long number)
  1135. {
  1136. char buffer[64];
  1137. int error;
  1138. u8 radix;
  1139. const char *msg;
  1140. if (!filename)
  1141. return 0;
  1142. switch (type) {
  1143. case TOMOYO_TYPE_CREATE:
  1144. case TOMOYO_TYPE_MKDIR:
  1145. case TOMOYO_TYPE_MKFIFO:
  1146. case TOMOYO_TYPE_MKSOCK:
  1147. case TOMOYO_TYPE_CHMOD:
  1148. radix = TOMOYO_VALUE_TYPE_OCTAL;
  1149. break;
  1150. case TOMOYO_TYPE_IOCTL:
  1151. radix = TOMOYO_VALUE_TYPE_HEXADECIMAL;
  1152. break;
  1153. default:
  1154. radix = TOMOYO_VALUE_TYPE_DECIMAL;
  1155. break;
  1156. }
  1157. tomoyo_print_ulong(buffer, sizeof(buffer), number, radix);
  1158. do {
  1159. error = tomoyo_path_number_acl(r, type, filename, number);
  1160. if (!error)
  1161. break;
  1162. msg = tomoyo_path_number2keyword(type);
  1163. tomoyo_warn_log(r, "%s %s %s", msg, filename->name, buffer);
  1164. error = tomoyo_supervisor(r, "allow_%s %s %s\n", msg,
  1165. tomoyo_file_pattern(filename),
  1166. buffer);
  1167. } while (error == TOMOYO_RETRY_REQUEST);
  1168. if (r->mode != TOMOYO_CONFIG_ENFORCING)
  1169. error = 0;
  1170. return error;
  1171. }
  1172. /**
  1173. * tomoyo_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
  1174. *
  1175. * @type: Type of operation.
  1176. * @path: Pointer to "struct path".
  1177. * @number: Number.
  1178. *
  1179. * Returns 0 on success, negative value otherwise.
  1180. */
  1181. int tomoyo_path_number_perm(const u8 type, struct path *path,
  1182. unsigned long number)
  1183. {
  1184. struct tomoyo_request_info r;
  1185. int error = -ENOMEM;
  1186. struct tomoyo_path_info buf;
  1187. int idx;
  1188. if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
  1189. !path->mnt || !path->dentry)
  1190. return 0;
  1191. idx = tomoyo_read_lock();
  1192. if (!tomoyo_get_realpath(&buf, path))
  1193. goto out;
  1194. if (type == TOMOYO_TYPE_MKDIR)
  1195. tomoyo_add_slash(&buf);
  1196. error = tomoyo_path_number_perm2(&r, type, &buf, number);
  1197. out:
  1198. kfree(buf.name);
  1199. tomoyo_read_unlock(idx);
  1200. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1201. error = 0;
  1202. return error;
  1203. }
  1204. /**
  1205. * tomoyo_check_exec_perm - Check permission for "execute".
  1206. *
  1207. * @domain: Pointer to "struct tomoyo_domain_info".
  1208. * @filename: Check permission for "execute".
  1209. *
  1210. * Returns 0 on success, negativevalue otherwise.
  1211. *
  1212. * Caller holds tomoyo_read_lock().
  1213. */
  1214. int tomoyo_check_exec_perm(struct tomoyo_domain_info *domain,
  1215. const struct tomoyo_path_info *filename)
  1216. {
  1217. struct tomoyo_request_info r;
  1218. if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED)
  1219. return 0;
  1220. return tomoyo_file_perm(&r, filename, 1);
  1221. }
  1222. /**
  1223. * tomoyo_check_open_permission - Check permission for "read" and "write".
  1224. *
  1225. * @domain: Pointer to "struct tomoyo_domain_info".
  1226. * @path: Pointer to "struct path".
  1227. * @flag: Flags for open().
  1228. *
  1229. * Returns 0 on success, negative value otherwise.
  1230. */
  1231. int tomoyo_check_open_permission(struct tomoyo_domain_info *domain,
  1232. struct path *path, const int flag)
  1233. {
  1234. const u8 acc_mode = ACC_MODE(flag);
  1235. int error = -ENOMEM;
  1236. struct tomoyo_path_info buf;
  1237. struct tomoyo_request_info r;
  1238. int idx;
  1239. if (tomoyo_init_request_info(&r, domain) == TOMOYO_CONFIG_DISABLED ||
  1240. !path->mnt)
  1241. return 0;
  1242. if (acc_mode == 0)
  1243. return 0;
  1244. if (path->dentry->d_inode && S_ISDIR(path->dentry->d_inode->i_mode))
  1245. /*
  1246. * I don't check directories here because mkdir() and rmdir()
  1247. * don't call me.
  1248. */
  1249. return 0;
  1250. idx = tomoyo_read_lock();
  1251. if (!tomoyo_get_realpath(&buf, path))
  1252. goto out;
  1253. error = 0;
  1254. /*
  1255. * If the filename is specified by "deny_rewrite" keyword,
  1256. * we need to check "allow_rewrite" permission when the filename is not
  1257. * opened for append mode or the filename is truncated at open time.
  1258. */
  1259. if ((acc_mode & MAY_WRITE) &&
  1260. ((flag & O_TRUNC) || !(flag & O_APPEND)) &&
  1261. (tomoyo_is_no_rewrite_file(&buf))) {
  1262. error = tomoyo_path_permission(&r, TOMOYO_TYPE_REWRITE, &buf);
  1263. }
  1264. if (!error)
  1265. error = tomoyo_file_perm(&r, &buf, acc_mode);
  1266. if (!error && (flag & O_TRUNC))
  1267. error = tomoyo_path_permission(&r, TOMOYO_TYPE_TRUNCATE, &buf);
  1268. out:
  1269. kfree(buf.name);
  1270. tomoyo_read_unlock(idx);
  1271. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1272. error = 0;
  1273. return error;
  1274. }
  1275. /**
  1276. * tomoyo_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "rewrite", "chroot" and "unmount".
  1277. *
  1278. * @operation: Type of operation.
  1279. * @path: Pointer to "struct path".
  1280. *
  1281. * Returns 0 on success, negative value otherwise.
  1282. */
  1283. int tomoyo_path_perm(const u8 operation, struct path *path)
  1284. {
  1285. int error = -ENOMEM;
  1286. struct tomoyo_path_info buf;
  1287. struct tomoyo_request_info r;
  1288. int idx;
  1289. if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
  1290. !path->mnt)
  1291. return 0;
  1292. idx = tomoyo_read_lock();
  1293. if (!tomoyo_get_realpath(&buf, path))
  1294. goto out;
  1295. switch (operation) {
  1296. case TOMOYO_TYPE_REWRITE:
  1297. if (!tomoyo_is_no_rewrite_file(&buf)) {
  1298. error = 0;
  1299. goto out;
  1300. }
  1301. break;
  1302. case TOMOYO_TYPE_RMDIR:
  1303. case TOMOYO_TYPE_CHROOT:
  1304. tomoyo_add_slash(&buf);
  1305. break;
  1306. }
  1307. error = tomoyo_path_permission(&r, operation, &buf);
  1308. out:
  1309. kfree(buf.name);
  1310. tomoyo_read_unlock(idx);
  1311. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1312. error = 0;
  1313. return error;
  1314. }
  1315. /**
  1316. * tomoyo_path_number3_perm2 - Check permission for path/number/number/number operation.
  1317. *
  1318. * @r: Pointer to "struct tomoyo_request_info".
  1319. * @operation: Type of operation.
  1320. * @filename: Filename to check.
  1321. * @mode: Create mode.
  1322. * @dev: Device number.
  1323. *
  1324. * Returns 0 on success, negative value otherwise.
  1325. *
  1326. * Caller holds tomoyo_read_lock().
  1327. */
  1328. static int tomoyo_path_number3_perm2(struct tomoyo_request_info *r,
  1329. const u8 operation,
  1330. const struct tomoyo_path_info *filename,
  1331. const unsigned int mode,
  1332. const unsigned int dev)
  1333. {
  1334. int error;
  1335. const char *msg;
  1336. const unsigned int major = MAJOR(dev);
  1337. const unsigned int minor = MINOR(dev);
  1338. do {
  1339. error = tomoyo_path_number3_acl(r, filename, 1 << operation,
  1340. mode, major, minor);
  1341. if (!error)
  1342. break;
  1343. msg = tomoyo_path_number32keyword(operation);
  1344. tomoyo_warn_log(r, "%s %s 0%o %u %u", msg, filename->name,
  1345. mode, major, minor);
  1346. error = tomoyo_supervisor(r, "allow_%s %s 0%o %u %u\n", msg,
  1347. tomoyo_file_pattern(filename), mode,
  1348. major, minor);
  1349. } while (error == TOMOYO_RETRY_REQUEST);
  1350. if (r->mode != TOMOYO_CONFIG_ENFORCING)
  1351. error = 0;
  1352. return error;
  1353. }
  1354. /**
  1355. * tomoyo_path_number3_perm - Check permission for "mkblock" and "mkchar".
  1356. *
  1357. * @operation: Type of operation. (TOMOYO_TYPE_MKCHAR or TOMOYO_TYPE_MKBLOCK)
  1358. * @path: Pointer to "struct path".
  1359. * @mode: Create mode.
  1360. * @dev: Device number.
  1361. *
  1362. * Returns 0 on success, negative value otherwise.
  1363. */
  1364. int tomoyo_path_number3_perm(const u8 operation, struct path *path,
  1365. const unsigned int mode, unsigned int dev)
  1366. {
  1367. struct tomoyo_request_info r;
  1368. int error = -ENOMEM;
  1369. struct tomoyo_path_info buf;
  1370. int idx;
  1371. if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
  1372. !path->mnt)
  1373. return 0;
  1374. idx = tomoyo_read_lock();
  1375. error = -ENOMEM;
  1376. if (tomoyo_get_realpath(&buf, path)) {
  1377. error = tomoyo_path_number3_perm2(&r, operation, &buf, mode,
  1378. new_decode_dev(dev));
  1379. kfree(buf.name);
  1380. }
  1381. tomoyo_read_unlock(idx);
  1382. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1383. error = 0;
  1384. return error;
  1385. }
  1386. /**
  1387. * tomoyo_path2_perm - Check permission for "rename", "link" and "pivot_root".
  1388. *
  1389. * @operation: Type of operation.
  1390. * @path1: Pointer to "struct path".
  1391. * @path2: Pointer to "struct path".
  1392. *
  1393. * Returns 0 on success, negative value otherwise.
  1394. */
  1395. int tomoyo_path2_perm(const u8 operation, struct path *path1,
  1396. struct path *path2)
  1397. {
  1398. int error = -ENOMEM;
  1399. const char *msg;
  1400. struct tomoyo_path_info buf1;
  1401. struct tomoyo_path_info buf2;
  1402. struct tomoyo_request_info r;
  1403. int idx;
  1404. if (tomoyo_init_request_info(&r, NULL) == TOMOYO_CONFIG_DISABLED ||
  1405. !path1->mnt || !path2->mnt)
  1406. return 0;
  1407. buf1.name = NULL;
  1408. buf2.name = NULL;
  1409. idx = tomoyo_read_lock();
  1410. if (!tomoyo_get_realpath(&buf1, path1) ||
  1411. !tomoyo_get_realpath(&buf2, path2))
  1412. goto out;
  1413. {
  1414. struct dentry *dentry = path1->dentry;
  1415. if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
  1416. tomoyo_add_slash(&buf1);
  1417. tomoyo_add_slash(&buf2);
  1418. }
  1419. }
  1420. do {
  1421. error = tomoyo_path2_acl(&r, operation, &buf1, &buf2);
  1422. if (!error)
  1423. break;
  1424. msg = tomoyo_path22keyword(operation);
  1425. tomoyo_warn_log(&r, "%s %s %s", msg, buf1.name, buf2.name);
  1426. error = tomoyo_supervisor(&r, "allow_%s %s %s\n", msg,
  1427. tomoyo_file_pattern(&buf1),
  1428. tomoyo_file_pattern(&buf2));
  1429. } while (error == TOMOYO_RETRY_REQUEST);
  1430. out:
  1431. kfree(buf1.name);
  1432. kfree(buf2.name);
  1433. tomoyo_read_unlock(idx);
  1434. if (r.mode != TOMOYO_CONFIG_ENFORCING)
  1435. error = 0;
  1436. return error;
  1437. }
  1438. /**
  1439. * tomoyo_write_file_policy - Update file related list.
  1440. *
  1441. * @data: String to parse.
  1442. * @domain: Pointer to "struct tomoyo_domain_info".
  1443. * @is_delete: True if it is a delete request.
  1444. *
  1445. * Returns 0 on success, negative value otherwise.
  1446. *
  1447. * Caller holds tomoyo_read_lock().
  1448. */
  1449. int tomoyo_write_file_policy(char *data, struct tomoyo_domain_info *domain,
  1450. const bool is_delete)
  1451. {
  1452. char *w[5];
  1453. u8 type;
  1454. if (!tomoyo_tokenize(data, w, sizeof(w)) || !w[1][0])
  1455. return -EINVAL;
  1456. if (strncmp(w[0], "allow_", 6)) {
  1457. unsigned int perm;
  1458. if (sscanf(w[0], "%u", &perm) == 1)
  1459. return tomoyo_update_file_acl((u8) perm, w[1], domain,
  1460. is_delete);
  1461. goto out;
  1462. }
  1463. w[0] += 6;
  1464. for (type = 0; type < TOMOYO_MAX_PATH_OPERATION; type++) {
  1465. if (strcmp(w[0], tomoyo_path_keyword[type]))
  1466. continue;
  1467. return tomoyo_update_path_acl(type, w[1], domain, is_delete);
  1468. }
  1469. if (!w[2][0])
  1470. goto out;
  1471. for (type = 0; type < TOMOYO_MAX_PATH2_OPERATION; type++) {
  1472. if (strcmp(w[0], tomoyo_path2_keyword[type]))
  1473. continue;
  1474. return tomoyo_update_path2_acl(type, w[1], w[2], domain,
  1475. is_delete);
  1476. }
  1477. for (type = 0; type < TOMOYO_MAX_PATH_NUMBER_OPERATION; type++) {
  1478. if (strcmp(w[0], tomoyo_path_number_keyword[type]))
  1479. continue;
  1480. return tomoyo_update_path_number_acl(type, w[1], w[2], domain,
  1481. is_delete);
  1482. }
  1483. if (!w[3][0] || !w[4][0])
  1484. goto out;
  1485. for (type = 0; type < TOMOYO_MAX_PATH_NUMBER3_OPERATION; type++) {
  1486. if (strcmp(w[0], tomoyo_path_number3_keyword[type]))
  1487. continue;
  1488. return tomoyo_update_path_number3_acl(type, w[1], w[2], w[3],
  1489. w[4], domain, is_delete);
  1490. }
  1491. out:
  1492. return -EINVAL;
  1493. }