file.c 45 KB

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