file.c 43 KB

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