file.c 44 KB

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